blob: 59a6dccab548b098ee1d932a62965354134c5cc4 [file] [log] [blame]
David Howells24c8dbb2006-08-22 20:06:10 -04001/* client.c: NFS client sharing and management code
2 *
3 * Copyright (C) 2006 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
11
12
David Howells24c8dbb2006-08-22 20:06:10 -040013#include <linux/module.h>
14#include <linux/init.h>
Alexey Dobriyane8edc6e2007-05-21 01:22:52 +040015#include <linux/sched.h>
David Howells24c8dbb2006-08-22 20:06:10 -040016#include <linux/time.h>
17#include <linux/kernel.h>
18#include <linux/mm.h>
19#include <linux/string.h>
20#include <linux/stat.h>
21#include <linux/errno.h>
22#include <linux/unistd.h>
23#include <linux/sunrpc/clnt.h>
24#include <linux/sunrpc/stats.h>
25#include <linux/sunrpc/metrics.h>
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -040026#include <linux/sunrpc/xprtsock.h>
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -040027#include <linux/sunrpc/xprtrdma.h>
David Howells24c8dbb2006-08-22 20:06:10 -040028#include <linux/nfs_fs.h>
29#include <linux/nfs_mount.h>
30#include <linux/nfs4_mount.h>
31#include <linux/lockd/bind.h>
David Howells24c8dbb2006-08-22 20:06:10 -040032#include <linux/seq_file.h>
33#include <linux/mount.h>
34#include <linux/nfs_idmap.h>
35#include <linux/vfs.h>
36#include <linux/inet.h>
Trond Myklebust3b0d3f92008-01-03 13:28:58 -050037#include <linux/in6.h>
38#include <net/ipv6.h>
David Howells24c8dbb2006-08-22 20:06:10 -040039#include <linux/nfs_xdr.h>
40
41#include <asm/system.h>
42
43#include "nfs4_fs.h"
44#include "callback.h"
45#include "delegation.h"
46#include "iostat.h"
47#include "internal.h"
48
49#define NFSDBG_FACILITY NFSDBG_CLIENT
50
51static DEFINE_SPINLOCK(nfs_client_lock);
52static LIST_HEAD(nfs_client_list);
David Howells54ceac42006-08-22 20:06:13 -040053static LIST_HEAD(nfs_volume_list);
David Howells24c8dbb2006-08-22 20:06:10 -040054static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
55
56/*
David Howells5006a762006-08-22 20:06:12 -040057 * RPC cruft for NFS
58 */
59static struct rpc_version *nfs_version[5] = {
60 [2] = &nfs_version2,
61#ifdef CONFIG_NFS_V3
62 [3] = &nfs_version3,
63#endif
64#ifdef CONFIG_NFS_V4
65 [4] = &nfs_version4,
66#endif
67};
68
69struct rpc_program nfs_program = {
70 .name = "nfs",
71 .number = NFS_PROGRAM,
72 .nrvers = ARRAY_SIZE(nfs_version),
73 .version = nfs_version,
74 .stats = &nfs_rpcstat,
75 .pipe_dir_name = "/nfs",
76};
77
78struct rpc_stat nfs_rpcstat = {
79 .program = &nfs_program
80};
81
82
83#ifdef CONFIG_NFS_V3_ACL
84static struct rpc_stat nfsacl_rpcstat = { &nfsacl_program };
85static struct rpc_version * nfsacl_version[] = {
86 [3] = &nfsacl_version3,
87};
88
89struct rpc_program nfsacl_program = {
90 .name = "nfsacl",
91 .number = NFS_ACL_PROGRAM,
92 .nrvers = ARRAY_SIZE(nfsacl_version),
93 .version = nfsacl_version,
94 .stats = &nfsacl_rpcstat,
95};
96#endif /* CONFIG_NFS_V3_ACL */
97
Trond Myklebust3a498022007-12-14 14:56:04 -050098struct nfs_client_initdata {
99 const char *hostname;
Chuck Leverd7422c42007-12-10 14:58:51 -0500100 const struct sockaddr *addr;
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500101 size_t addrlen;
Trond Myklebust40c553192007-12-14 14:56:07 -0500102 const struct nfs_rpc_ops *rpc_ops;
Trond Myklebust3a498022007-12-14 14:56:04 -0500103};
104
David Howells5006a762006-08-22 20:06:12 -0400105/*
David Howells24c8dbb2006-08-22 20:06:10 -0400106 * Allocate a shared client record
107 *
108 * Since these are allocated/deallocated very rarely, we don't
109 * bother putting them in a slab cache...
110 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500111static struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400112{
113 struct nfs_client *clp;
David Howells24c8dbb2006-08-22 20:06:10 -0400114
115 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
116 goto error_0;
117
Trond Myklebust40c553192007-12-14 14:56:07 -0500118 clp->rpc_ops = cl_init->rpc_ops;
119
120 if (cl_init->rpc_ops->version == 4) {
David Howells24c8dbb2006-08-22 20:06:10 -0400121 if (nfs_callback_up() < 0)
122 goto error_2;
123 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
124 }
125
126 atomic_set(&clp->cl_count, 1);
127 clp->cl_cons_state = NFS_CS_INITING;
128
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500129 memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
130 clp->cl_addrlen = cl_init->addrlen;
David Howells24c8dbb2006-08-22 20:06:10 -0400131
Trond Myklebust3a498022007-12-14 14:56:04 -0500132 if (cl_init->hostname) {
133 clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
David Howells24c8dbb2006-08-22 20:06:10 -0400134 if (!clp->cl_hostname)
135 goto error_3;
136 }
137
138 INIT_LIST_HEAD(&clp->cl_superblocks);
139 clp->cl_rpcclient = ERR_PTR(-EINVAL);
140
141#ifdef CONFIG_NFS_V4
142 init_rwsem(&clp->cl_sem);
143 INIT_LIST_HEAD(&clp->cl_delegations);
David Howells24c8dbb2006-08-22 20:06:10 -0400144 spin_lock_init(&clp->cl_lock);
David Howells65f27f32006-11-22 14:55:48 +0000145 INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
David Howells24c8dbb2006-08-22 20:06:10 -0400146 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
147 clp->cl_boot_time = CURRENT_TIME;
148 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
149#endif
150
151 return clp;
152
153error_3:
Trond Myklebust9c5bf382006-08-22 20:06:14 -0400154 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
155 nfs_callback_down();
David Howells24c8dbb2006-08-22 20:06:10 -0400156error_2:
David Howells24c8dbb2006-08-22 20:06:10 -0400157 kfree(clp);
158error_0:
159 return NULL;
160}
161
Trond Myklebust5dd31772006-08-24 01:03:05 -0400162static void nfs4_shutdown_client(struct nfs_client *clp)
163{
164#ifdef CONFIG_NFS_V4
165 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
166 nfs4_kill_renewd(clp);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400167 BUG_ON(!RB_EMPTY_ROOT(&clp->cl_state_owners));
Trond Myklebust5dd31772006-08-24 01:03:05 -0400168 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
169 nfs_idmap_delete(clp);
170#endif
171}
172
David Howells24c8dbb2006-08-22 20:06:10 -0400173/*
174 * Destroy a shared client record
175 */
176static void nfs_free_client(struct nfs_client *clp)
177{
Trond Myklebust40c553192007-12-14 14:56:07 -0500178 dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400179
Trond Myklebust5dd31772006-08-24 01:03:05 -0400180 nfs4_shutdown_client(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400181
182 /* -EIO all pending I/O */
183 if (!IS_ERR(clp->cl_rpcclient))
184 rpc_shutdown_client(clp->cl_rpcclient);
185
186 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
187 nfs_callback_down();
188
David Howells24c8dbb2006-08-22 20:06:10 -0400189 kfree(clp->cl_hostname);
190 kfree(clp);
191
192 dprintk("<-- nfs_free_client()\n");
193}
194
195/*
196 * Release a reference to a shared client record
197 */
198void nfs_put_client(struct nfs_client *clp)
199{
David Howells27ba8512006-07-30 14:40:56 -0400200 if (!clp)
201 return;
202
David Howells24c8dbb2006-08-22 20:06:10 -0400203 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
204
205 if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
206 list_del(&clp->cl_share_link);
207 spin_unlock(&nfs_client_lock);
208
209 BUG_ON(!list_empty(&clp->cl_superblocks));
210
211 nfs_free_client(clp);
212 }
213}
214
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500215static int nfs_sockaddr_match_ipaddr4(const struct sockaddr_in *sa1,
216 const struct sockaddr_in *sa2)
217{
218 return sa1->sin_addr.s_addr == sa2->sin_addr.s_addr;
219}
220
221static int nfs_sockaddr_match_ipaddr6(const struct sockaddr_in6 *sa1,
222 const struct sockaddr_in6 *sa2)
223{
224 return ipv6_addr_equal(&sa1->sin6_addr, &sa2->sin6_addr);
225}
226
227static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
228 const struct sockaddr *sa2)
229{
230 switch (sa1->sa_family) {
231 case AF_INET:
232 return nfs_sockaddr_match_ipaddr4((const struct sockaddr_in *)sa1,
233 (const struct sockaddr_in *)sa2);
234 case AF_INET6:
235 return nfs_sockaddr_match_ipaddr6((const struct sockaddr_in6 *)sa1,
236 (const struct sockaddr_in6 *)sa2);
237 }
238 BUG();
239}
240
David Howells24c8dbb2006-08-22 20:06:10 -0400241/*
Trond Myklebustc81468a2007-12-14 14:56:05 -0500242 * Find a client by IP address and protocol version
243 * - returns NULL if no such client
David Howells24c8dbb2006-08-22 20:06:10 -0400244 */
Chuck Leverff052642007-12-10 14:58:44 -0500245struct nfs_client *nfs_find_client(const struct sockaddr *addr, u32 nfsversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500246{
247 struct nfs_client *clp;
248
249 spin_lock(&nfs_client_lock);
250 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500251 struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
252
Trond Myklebustc81468a2007-12-14 14:56:05 -0500253 /* Don't match clients that failed to initialise properly */
254 if (clp->cl_cons_state != NFS_CS_READY)
255 continue;
256
257 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500258 if (clp->rpc_ops->version != nfsversion)
Trond Myklebustc81468a2007-12-14 14:56:05 -0500259 continue;
260
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500261 if (addr->sa_family != clap->sa_family)
262 continue;
Trond Myklebustc81468a2007-12-14 14:56:05 -0500263 /* Match only the IP address, not the port number */
Trond Myklebust3b0d3f92008-01-03 13:28:58 -0500264 if (!nfs_sockaddr_match_ipaddr(addr, clap))
Trond Myklebustc81468a2007-12-14 14:56:05 -0500265 continue;
266
267 atomic_inc(&clp->cl_count);
268 spin_unlock(&nfs_client_lock);
269 return clp;
270 }
271 spin_unlock(&nfs_client_lock);
272 return NULL;
273}
274
275/*
276 * Find an nfs_client on the list that matches the initialisation data
277 * that is supplied.
278 */
279static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
David Howells24c8dbb2006-08-22 20:06:10 -0400280{
281 struct nfs_client *clp;
282
283 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Trond Myklebust13bbc062006-10-19 23:28:40 -0700284 /* Don't match clients that failed to initialise properly */
285 if (clp->cl_cons_state < 0)
286 continue;
287
David Howells24c8dbb2006-08-22 20:06:10 -0400288 /* Different NFS versions cannot share the same nfs_client */
Trond Myklebust40c553192007-12-14 14:56:07 -0500289 if (clp->rpc_ops != data->rpc_ops)
David Howells24c8dbb2006-08-22 20:06:10 -0400290 continue;
291
Trond Myklebustc81468a2007-12-14 14:56:05 -0500292 /* Match the full socket address */
293 if (memcmp(&clp->cl_addr, data->addr, sizeof(clp->cl_addr)) != 0)
David Howells24c8dbb2006-08-22 20:06:10 -0400294 continue;
295
Trond Myklebustc81468a2007-12-14 14:56:05 -0500296 atomic_inc(&clp->cl_count);
297 return clp;
David Howells24c8dbb2006-08-22 20:06:10 -0400298 }
David Howells24c8dbb2006-08-22 20:06:10 -0400299 return NULL;
David Howells24c8dbb2006-08-22 20:06:10 -0400300}
301
302/*
303 * Look up a client by IP address and protocol version
304 * - creates a new record if one doesn't yet exist
305 */
Trond Myklebust3a498022007-12-14 14:56:04 -0500306static struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init)
David Howells24c8dbb2006-08-22 20:06:10 -0400307{
308 struct nfs_client *clp, *new = NULL;
309 int error;
310
Chuck Leverd7422c42007-12-10 14:58:51 -0500311 dprintk("--> nfs_get_client(%s,v%u)\n",
312 cl_init->hostname ?: "", cl_init->rpc_ops->version);
David Howells24c8dbb2006-08-22 20:06:10 -0400313
314 /* see if the client already exists */
315 do {
316 spin_lock(&nfs_client_lock);
317
Trond Myklebustc81468a2007-12-14 14:56:05 -0500318 clp = nfs_match_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400319 if (clp)
320 goto found_client;
321 if (new)
322 goto install_client;
323
324 spin_unlock(&nfs_client_lock);
325
Trond Myklebust3a498022007-12-14 14:56:04 -0500326 new = nfs_alloc_client(cl_init);
David Howells24c8dbb2006-08-22 20:06:10 -0400327 } while (new);
328
329 return ERR_PTR(-ENOMEM);
330
331 /* install a new client and return with it unready */
332install_client:
333 clp = new;
334 list_add(&clp->cl_share_link, &nfs_client_list);
335 spin_unlock(&nfs_client_lock);
336 dprintk("--> nfs_get_client() = %p [new]\n", clp);
337 return clp;
338
339 /* found an existing client
340 * - make sure it's ready before returning
341 */
342found_client:
343 spin_unlock(&nfs_client_lock);
344
345 if (new)
346 nfs_free_client(new);
347
Linus Torvalds83250492006-10-08 17:28:25 -0700348 error = wait_event_interruptible(nfs_client_active_wq,
Trond Myklebust0bae89e2006-10-08 14:33:24 -0400349 clp->cl_cons_state != NFS_CS_INITING);
350 if (error < 0) {
351 nfs_put_client(clp);
352 return ERR_PTR(-ERESTARTSYS);
David Howells24c8dbb2006-08-22 20:06:10 -0400353 }
354
355 if (clp->cl_cons_state < NFS_CS_READY) {
356 error = clp->cl_cons_state;
357 nfs_put_client(clp);
358 return ERR_PTR(error);
359 }
360
David Howells54ceac42006-08-22 20:06:13 -0400361 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
362
David Howells24c8dbb2006-08-22 20:06:10 -0400363 dprintk("--> nfs_get_client() = %p [share]\n", clp);
364 return clp;
365}
366
367/*
368 * Mark a server as ready or failed
369 */
David Howells54ceac42006-08-22 20:06:13 -0400370static void nfs_mark_client_ready(struct nfs_client *clp, int state)
David Howells24c8dbb2006-08-22 20:06:10 -0400371{
372 clp->cl_cons_state = state;
373 wake_up_all(&nfs_client_active_wq);
374}
David Howells5006a762006-08-22 20:06:12 -0400375
376/*
377 * Initialise the timeout values for a connection
378 */
379static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
380 unsigned int timeo, unsigned int retrans)
381{
382 to->to_initval = timeo * HZ / 10;
383 to->to_retries = retrans;
384 if (!to->to_retries)
385 to->to_retries = 2;
386
387 switch (proto) {
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400388 case XPRT_TRANSPORT_TCP:
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400389 case XPRT_TRANSPORT_RDMA:
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500390 if (to->to_initval == 0)
David Howells5006a762006-08-22 20:06:12 -0400391 to->to_initval = 60 * HZ;
392 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
393 to->to_initval = NFS_MAX_TCP_TIMEOUT;
394 to->to_increment = to->to_initval;
395 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
Trond Myklebust7a3e3e12007-12-20 16:03:57 -0500396 if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
397 to->to_maxval = NFS_MAX_TCP_TIMEOUT;
398 if (to->to_maxval < to->to_initval)
399 to->to_maxval = to->to_initval;
David Howells5006a762006-08-22 20:06:12 -0400400 to->to_exponential = 0;
401 break;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400402 case XPRT_TRANSPORT_UDP:
David Howells5006a762006-08-22 20:06:12 -0400403 default:
404 if (!to->to_initval)
405 to->to_initval = 11 * HZ / 10;
406 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
407 to->to_initval = NFS_MAX_UDP_TIMEOUT;
408 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
409 to->to_exponential = 1;
410 break;
411 }
412}
413
414/*
415 * Create an RPC client handle
416 */
David Howells54ceac42006-08-22 20:06:13 -0400417static int nfs_create_rpc_client(struct nfs_client *clp, int proto,
418 unsigned int timeo,
419 unsigned int retrans,
Chuck Lever43d78ef2007-02-06 18:26:11 -0500420 rpc_authflavor_t flavor,
421 int flags)
David Howells5006a762006-08-22 20:06:12 -0400422{
423 struct rpc_timeout timeparms;
David Howells5006a762006-08-22 20:06:12 -0400424 struct rpc_clnt *clnt = NULL;
Chuck Lever41877d22006-08-22 20:06:20 -0400425 struct rpc_create_args args = {
426 .protocol = proto,
427 .address = (struct sockaddr *)&clp->cl_addr,
Chuck Lever6e4cffd2007-12-10 14:58:15 -0500428 .addrsize = clp->cl_addrlen,
Chuck Lever41877d22006-08-22 20:06:20 -0400429 .timeout = &timeparms,
430 .servername = clp->cl_hostname,
431 .program = &nfs_program,
432 .version = clp->rpc_ops->version,
433 .authflavor = flavor,
Chuck Lever43d78ef2007-02-06 18:26:11 -0500434 .flags = flags,
Chuck Lever41877d22006-08-22 20:06:20 -0400435 };
David Howells5006a762006-08-22 20:06:12 -0400436
437 if (!IS_ERR(clp->cl_rpcclient))
438 return 0;
439
440 nfs_init_timeout_values(&timeparms, proto, timeo, retrans);
441 clp->retrans_timeo = timeparms.to_initval;
442 clp->retrans_count = timeparms.to_retries;
443
Chuck Lever41877d22006-08-22 20:06:20 -0400444 clnt = rpc_create(&args);
David Howells5006a762006-08-22 20:06:12 -0400445 if (IS_ERR(clnt)) {
446 dprintk("%s: cannot create RPC client. Error = %ld\n",
447 __FUNCTION__, PTR_ERR(clnt));
448 return PTR_ERR(clnt);
449 }
450
David Howells5006a762006-08-22 20:06:12 -0400451 clp->cl_rpcclient = clnt;
452 return 0;
453}
David Howells54ceac42006-08-22 20:06:13 -0400454
455/*
456 * Version 2 or 3 client destruction
457 */
458static void nfs_destroy_server(struct nfs_server *server)
459{
David Howells54ceac42006-08-22 20:06:13 -0400460 if (!(server->flags & NFS_MOUNT_NONLM))
461 lockd_down(); /* release rpc.lockd */
462}
463
464/*
465 * Version 2 or 3 lockd setup
466 */
467static int nfs_start_lockd(struct nfs_server *server)
468{
469 int error = 0;
470
Trond Myklebust40c553192007-12-14 14:56:07 -0500471 if (server->nfs_client->rpc_ops->version > 3)
David Howells54ceac42006-08-22 20:06:13 -0400472 goto out;
473 if (server->flags & NFS_MOUNT_NONLM)
474 goto out;
NeilBrown24e36662006-10-02 02:17:45 -0700475 error = lockd_up((server->flags & NFS_MOUNT_TCP) ?
476 IPPROTO_TCP : IPPROTO_UDP);
David Howells54ceac42006-08-22 20:06:13 -0400477 if (error < 0)
478 server->flags |= NFS_MOUNT_NONLM;
479 else
480 server->destroy = nfs_destroy_server;
481out:
482 return error;
483}
484
485/*
486 * Initialise an NFSv3 ACL client connection
487 */
488#ifdef CONFIG_NFS_V3_ACL
489static void nfs_init_server_aclclient(struct nfs_server *server)
490{
Trond Myklebust40c553192007-12-14 14:56:07 -0500491 if (server->nfs_client->rpc_ops->version != 3)
David Howells54ceac42006-08-22 20:06:13 -0400492 goto out_noacl;
493 if (server->flags & NFS_MOUNT_NOACL)
494 goto out_noacl;
495
496 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
497 if (IS_ERR(server->client_acl))
498 goto out_noacl;
499
500 /* No errors! Assume that Sun nfsacls are supported */
501 server->caps |= NFS_CAP_ACLS;
502 return;
503
504out_noacl:
505 server->caps &= ~NFS_CAP_ACLS;
506}
507#else
508static inline void nfs_init_server_aclclient(struct nfs_server *server)
509{
510 server->flags &= ~NFS_MOUNT_NOACL;
511 server->caps &= ~NFS_CAP_ACLS;
512}
513#endif
514
515/*
516 * Create a general RPC client
517 */
518static int nfs_init_server_rpcclient(struct nfs_server *server, rpc_authflavor_t pseudoflavour)
519{
520 struct nfs_client *clp = server->nfs_client;
521
522 server->client = rpc_clone_client(clp->cl_rpcclient);
523 if (IS_ERR(server->client)) {
524 dprintk("%s: couldn't create rpc_client!\n", __FUNCTION__);
525 return PTR_ERR(server->client);
526 }
527
528 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
529 struct rpc_auth *auth;
530
531 auth = rpcauth_create(pseudoflavour, server->client);
532 if (IS_ERR(auth)) {
533 dprintk("%s: couldn't create credcache!\n", __FUNCTION__);
534 return PTR_ERR(auth);
535 }
536 }
537 server->client->cl_softrtry = 0;
538 if (server->flags & NFS_MOUNT_SOFT)
539 server->client->cl_softrtry = 1;
540
541 server->client->cl_intr = 0;
542 if (server->flags & NFS4_MOUNT_INTR)
543 server->client->cl_intr = 1;
544
545 return 0;
546}
547
548/*
549 * Initialise an NFS2 or NFS3 client
550 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400551static int nfs_init_client(struct nfs_client *clp,
552 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400553{
David Howells54ceac42006-08-22 20:06:13 -0400554 int error;
555
556 if (clp->cl_cons_state == NFS_CS_READY) {
557 /* the client is already initialised */
558 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
559 return 0;
560 }
561
David Howells54ceac42006-08-22 20:06:13 -0400562 /*
563 * Create a client RPC handle for doing FSSTAT with UNIX auth only
564 * - RFC 2623, sec 2.3.2
565 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400566 error = nfs_create_rpc_client(clp, data->nfs_server.protocol,
567 data->timeo, data->retrans, RPC_AUTH_UNIX, 0);
David Howells54ceac42006-08-22 20:06:13 -0400568 if (error < 0)
569 goto error;
570 nfs_mark_client_ready(clp, NFS_CS_READY);
571 return 0;
572
573error:
574 nfs_mark_client_ready(clp, error);
575 dprintk("<-- nfs_init_client() = xerror %d\n", error);
576 return error;
577}
578
579/*
580 * Create a version 2 or 3 client
581 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400582static int nfs_init_server(struct nfs_server *server,
583 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400584{
Trond Myklebust3a498022007-12-14 14:56:04 -0500585 struct nfs_client_initdata cl_init = {
586 .hostname = data->nfs_server.hostname,
Chuck Leverd7422c42007-12-10 14:58:51 -0500587 .addr = (const struct sockaddr *)&data->nfs_server.address,
Chuck Lever4c568012007-12-10 14:59:28 -0500588 .addrlen = data->nfs_server.addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -0500589 .rpc_ops = &nfs_v2_clientops,
Trond Myklebust3a498022007-12-14 14:56:04 -0500590 };
David Howells54ceac42006-08-22 20:06:13 -0400591 struct nfs_client *clp;
Trond Myklebust3a498022007-12-14 14:56:04 -0500592 int error;
David Howells54ceac42006-08-22 20:06:13 -0400593
594 dprintk("--> nfs_init_server()\n");
595
596#ifdef CONFIG_NFS_V3
597 if (data->flags & NFS_MOUNT_VER3)
Trond Myklebust40c553192007-12-14 14:56:07 -0500598 cl_init.rpc_ops = &nfs_v3_clientops;
David Howells54ceac42006-08-22 20:06:13 -0400599#endif
600
601 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -0500602 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -0400603 if (IS_ERR(clp)) {
604 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
605 return PTR_ERR(clp);
606 }
607
608 error = nfs_init_client(clp, data);
609 if (error < 0)
610 goto error;
611
612 server->nfs_client = clp;
613
614 /* Initialise the client representation from the mount data */
615 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
616
617 if (data->rsize)
618 server->rsize = nfs_block_size(data->rsize, NULL);
619 if (data->wsize)
620 server->wsize = nfs_block_size(data->wsize, NULL);
621
622 server->acregmin = data->acregmin * HZ;
623 server->acregmax = data->acregmax * HZ;
624 server->acdirmin = data->acdirmin * HZ;
625 server->acdirmax = data->acdirmax * HZ;
626
627 /* Start lockd here, before we might error out */
628 error = nfs_start_lockd(server);
629 if (error < 0)
630 goto error;
631
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400632 error = nfs_init_server_rpcclient(server, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -0400633 if (error < 0)
634 goto error;
635
636 server->namelen = data->namlen;
637 /* Create a client RPC handle for the NFSv3 ACL management interface */
638 nfs_init_server_aclclient(server);
David Howells54ceac42006-08-22 20:06:13 -0400639 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
640 return 0;
641
642error:
643 server->nfs_client = NULL;
644 nfs_put_client(clp);
645 dprintk("<-- nfs_init_server() = xerror %d\n", error);
646 return error;
647}
648
649/*
650 * Load up the server record from information gained in an fsinfo record
651 */
652static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
653{
654 unsigned long max_rpc_payload;
655
656 /* Work out a lot of parameters */
657 if (server->rsize == 0)
658 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
659 if (server->wsize == 0)
660 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
661
662 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
663 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
664 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
665 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
666
667 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
668 if (server->rsize > max_rpc_payload)
669 server->rsize = max_rpc_payload;
670 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
671 server->rsize = NFS_MAX_FILE_IO_SIZE;
672 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700673
David Howells54ceac42006-08-22 20:06:13 -0400674 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
675
676 if (server->wsize > max_rpc_payload)
677 server->wsize = max_rpc_payload;
678 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
679 server->wsize = NFS_MAX_FILE_IO_SIZE;
680 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
681 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
682
683 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
684 if (server->dtsize > PAGE_CACHE_SIZE)
685 server->dtsize = PAGE_CACHE_SIZE;
686 if (server->dtsize > server->rsize)
687 server->dtsize = server->rsize;
688
689 if (server->flags & NFS_MOUNT_NOAC) {
690 server->acregmin = server->acregmax = 0;
691 server->acdirmin = server->acdirmax = 0;
692 }
693
694 server->maxfilesize = fsinfo->maxfilesize;
695
696 /* We're airborne Set socket buffersize */
697 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
698}
699
700/*
701 * Probe filesystem information, including the FSID on v2/v3
702 */
703static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
704{
705 struct nfs_fsinfo fsinfo;
706 struct nfs_client *clp = server->nfs_client;
707 int error;
708
709 dprintk("--> nfs_probe_fsinfo()\n");
710
711 if (clp->rpc_ops->set_capabilities != NULL) {
712 error = clp->rpc_ops->set_capabilities(server, mntfh);
713 if (error < 0)
714 goto out_error;
715 }
716
717 fsinfo.fattr = fattr;
718 nfs_fattr_init(fattr);
719 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
720 if (error < 0)
721 goto out_error;
722
723 nfs_server_set_fsinfo(server, &fsinfo);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700724 error = bdi_init(&server->backing_dev_info);
725 if (error)
726 goto out_error;
727
David Howells54ceac42006-08-22 20:06:13 -0400728
729 /* Get some general file system info */
730 if (server->namelen == 0) {
731 struct nfs_pathconf pathinfo;
732
733 pathinfo.fattr = fattr;
734 nfs_fattr_init(fattr);
735
736 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
737 server->namelen = pathinfo.max_namelen;
738 }
739
740 dprintk("<-- nfs_probe_fsinfo() = 0\n");
741 return 0;
742
743out_error:
744 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
745 return error;
746}
747
748/*
749 * Copy useful information when duplicating a server record
750 */
751static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
752{
753 target->flags = source->flags;
754 target->acregmin = source->acregmin;
755 target->acregmax = source->acregmax;
756 target->acdirmin = source->acdirmin;
757 target->acdirmax = source->acdirmax;
758 target->caps = source->caps;
759}
760
761/*
762 * Allocate and initialise a server record
763 */
764static struct nfs_server *nfs_alloc_server(void)
765{
766 struct nfs_server *server;
767
768 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
769 if (!server)
770 return NULL;
771
772 server->client = server->client_acl = ERR_PTR(-EINVAL);
773
774 /* Zero out the NFS state stuff */
775 INIT_LIST_HEAD(&server->client_link);
776 INIT_LIST_HEAD(&server->master_link);
777
Steve Dicksonef818a22007-11-08 04:05:04 -0500778 init_waitqueue_head(&server->active_wq);
779 atomic_set(&server->active, 0);
780
David Howells54ceac42006-08-22 20:06:13 -0400781 server->io_stats = nfs_alloc_iostats();
782 if (!server->io_stats) {
783 kfree(server);
784 return NULL;
785 }
786
787 return server;
788}
789
790/*
791 * Free up a server record
792 */
793void nfs_free_server(struct nfs_server *server)
794{
795 dprintk("--> nfs_free_server()\n");
796
797 spin_lock(&nfs_client_lock);
798 list_del(&server->client_link);
799 list_del(&server->master_link);
800 spin_unlock(&nfs_client_lock);
801
802 if (server->destroy != NULL)
803 server->destroy(server);
Trond Myklebust5cef3382007-12-11 22:01:56 -0500804
805 if (!IS_ERR(server->client_acl))
806 rpc_shutdown_client(server->client_acl);
David Howells54ceac42006-08-22 20:06:13 -0400807 if (!IS_ERR(server->client))
808 rpc_shutdown_client(server->client);
809
810 nfs_put_client(server->nfs_client);
811
812 nfs_free_iostats(server->io_stats);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700813 bdi_destroy(&server->backing_dev_info);
David Howells54ceac42006-08-22 20:06:13 -0400814 kfree(server);
815 nfs_release_automount_timer();
816 dprintk("<-- nfs_free_server()\n");
817}
818
819/*
820 * Create a version 2 or 3 volume record
821 * - keyed on server and FSID
822 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400823struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -0400824 struct nfs_fh *mntfh)
825{
826 struct nfs_server *server;
827 struct nfs_fattr fattr;
828 int error;
829
830 server = nfs_alloc_server();
831 if (!server)
832 return ERR_PTR(-ENOMEM);
833
834 /* Get a client representation */
835 error = nfs_init_server(server, data);
836 if (error < 0)
837 goto error;
838
839 BUG_ON(!server->nfs_client);
840 BUG_ON(!server->nfs_client->rpc_ops);
841 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
842
843 /* Probe the root fh to retrieve its FSID */
844 error = nfs_probe_fsinfo(server, mntfh, &fattr);
845 if (error < 0)
846 goto error;
Trond Myklebust54af3bb2007-09-28 12:27:41 -0400847 if (server->nfs_client->rpc_ops->version == 3) {
848 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
849 server->namelen = NFS3_MAXNAMLEN;
850 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
851 server->caps |= NFS_CAP_READDIRPLUS;
852 } else {
853 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
854 server->namelen = NFS2_MAXNAMLEN;
855 }
856
David Howells54ceac42006-08-22 20:06:13 -0400857 if (!(fattr.valid & NFS_ATTR_FATTR)) {
858 error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
859 if (error < 0) {
860 dprintk("nfs_create_server: getattr error = %d\n", -error);
861 goto error;
862 }
863 }
864 memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
865
David Howells6daabf12006-08-24 15:44:16 -0400866 dprintk("Server FSID: %llx:%llx\n",
867 (unsigned long long) server->fsid.major,
868 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -0400869
870 BUG_ON(!server->nfs_client);
871 BUG_ON(!server->nfs_client->rpc_ops);
872 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
873
874 spin_lock(&nfs_client_lock);
875 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
876 list_add_tail(&server->master_link, &nfs_volume_list);
877 spin_unlock(&nfs_client_lock);
878
879 server->mount_time = jiffies;
880 return server;
881
882error:
883 nfs_free_server(server);
884 return ERR_PTR(error);
885}
886
887#ifdef CONFIG_NFS_V4
888/*
889 * Initialise an NFS4 client record
890 */
891static int nfs4_init_client(struct nfs_client *clp,
892 int proto, int timeo, int retrans,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -0700893 const char *ip_addr,
David Howells54ceac42006-08-22 20:06:13 -0400894 rpc_authflavor_t authflavour)
895{
896 int error;
897
898 if (clp->cl_cons_state == NFS_CS_READY) {
899 /* the client is initialised already */
900 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
901 return 0;
902 }
903
904 /* Check NFS protocol revision and initialize RPC op vector */
905 clp->rpc_ops = &nfs_v4_clientops;
906
Chuck Lever43d78ef2007-02-06 18:26:11 -0500907 error = nfs_create_rpc_client(clp, proto, timeo, retrans, authflavour,
908 RPC_CLNT_CREATE_DISCRTRY);
David Howells54ceac42006-08-22 20:06:13 -0400909 if (error < 0)
910 goto error;
J. Bruce Fields7d9ac062006-10-19 23:28:39 -0700911 memcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
David Howells54ceac42006-08-22 20:06:13 -0400912
913 error = nfs_idmap_new(clp);
914 if (error < 0) {
915 dprintk("%s: failed to create idmapper. Error = %d\n",
916 __FUNCTION__, error);
David Howells54ceac42006-08-22 20:06:13 -0400917 goto error;
918 }
Trond Myklebust9c5bf382006-08-22 20:06:14 -0400919 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
David Howells54ceac42006-08-22 20:06:13 -0400920
921 nfs_mark_client_ready(clp, NFS_CS_READY);
922 return 0;
923
924error:
925 nfs_mark_client_ready(clp, error);
926 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
927 return error;
928}
929
930/*
931 * Set up an NFS4 client
932 */
933static int nfs4_set_client(struct nfs_server *server,
Chuck Leverdcecae02007-12-10 14:58:59 -0500934 const char *hostname,
935 const struct sockaddr *addr,
936 const size_t addrlen,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -0700937 const char *ip_addr,
David Howells54ceac42006-08-22 20:06:13 -0400938 rpc_authflavor_t authflavour,
939 int proto, int timeo, int retrans)
940{
Trond Myklebust3a498022007-12-14 14:56:04 -0500941 struct nfs_client_initdata cl_init = {
942 .hostname = hostname,
Chuck Leverdcecae02007-12-10 14:58:59 -0500943 .addr = addr,
944 .addrlen = addrlen,
Trond Myklebust40c553192007-12-14 14:56:07 -0500945 .rpc_ops = &nfs_v4_clientops,
Trond Myklebust3a498022007-12-14 14:56:04 -0500946 };
David Howells54ceac42006-08-22 20:06:13 -0400947 struct nfs_client *clp;
948 int error;
949
950 dprintk("--> nfs4_set_client()\n");
951
952 /* Allocate or find a client reference we can use */
Trond Myklebust3a498022007-12-14 14:56:04 -0500953 clp = nfs_get_client(&cl_init);
David Howells54ceac42006-08-22 20:06:13 -0400954 if (IS_ERR(clp)) {
955 error = PTR_ERR(clp);
956 goto error;
957 }
J. Bruce Fields7d9ac062006-10-19 23:28:39 -0700958 error = nfs4_init_client(clp, proto, timeo, retrans, ip_addr, authflavour);
David Howells54ceac42006-08-22 20:06:13 -0400959 if (error < 0)
960 goto error_put;
961
962 server->nfs_client = clp;
963 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
964 return 0;
965
966error_put:
967 nfs_put_client(clp);
968error:
969 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
970 return error;
971}
972
973/*
974 * Create a version 4 volume record
975 */
976static int nfs4_init_server(struct nfs_server *server,
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -0400977 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400978{
979 int error;
980
981 dprintk("--> nfs4_init_server()\n");
982
983 /* Initialise the client representation from the mount data */
984 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
985 server->caps |= NFS_CAP_ATOMIC_OPEN;
986
987 if (data->rsize)
988 server->rsize = nfs_block_size(data->rsize, NULL);
989 if (data->wsize)
990 server->wsize = nfs_block_size(data->wsize, NULL);
991
992 server->acregmin = data->acregmin * HZ;
993 server->acregmax = data->acregmax * HZ;
994 server->acdirmin = data->acdirmin * HZ;
995 server->acdirmax = data->acdirmax * HZ;
996
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -0400997 error = nfs_init_server_rpcclient(server, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -0400998
999 /* Done */
1000 dprintk("<-- nfs4_init_server() = %d\n", error);
1001 return error;
1002}
1003
1004/*
1005 * Create a version 4 volume record
1006 * - keyed on server and FSID
1007 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001008struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -04001009 struct nfs_fh *mntfh)
1010{
1011 struct nfs_fattr fattr;
1012 struct nfs_server *server;
1013 int error;
1014
1015 dprintk("--> nfs4_create_server()\n");
1016
1017 server = nfs_alloc_server();
1018 if (!server)
1019 return ERR_PTR(-ENOMEM);
1020
1021 /* Get a client record */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001022 error = nfs4_set_client(server,
1023 data->nfs_server.hostname,
Chuck Leverdcecae02007-12-10 14:58:59 -05001024 (struct sockaddr *)&data->nfs_server.address,
Chuck Lever4c568012007-12-10 14:59:28 -05001025 data->nfs_server.addrlen,
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001026 data->client_address,
1027 data->auth_flavors[0],
1028 data->nfs_server.protocol,
1029 data->timeo, data->retrans);
David Howells54ceac42006-08-22 20:06:13 -04001030 if (error < 0)
1031 goto error;
1032
1033 /* set up the general RPC client */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001034 error = nfs4_init_server(server, data);
David Howells54ceac42006-08-22 20:06:13 -04001035 if (error < 0)
1036 goto error;
1037
1038 BUG_ON(!server->nfs_client);
1039 BUG_ON(!server->nfs_client->rpc_ops);
1040 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1041
1042 /* Probe the root fh to retrieve its FSID */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -04001043 error = nfs4_path_walk(server, mntfh, data->nfs_server.export_path);
David Howells54ceac42006-08-22 20:06:13 -04001044 if (error < 0)
1045 goto error;
1046
David Howells6daabf12006-08-24 15:44:16 -04001047 dprintk("Server FSID: %llx:%llx\n",
1048 (unsigned long long) server->fsid.major,
1049 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001050 dprintk("Mount FH: %d\n", mntfh->size);
1051
1052 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1053 if (error < 0)
1054 goto error;
1055
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001056 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1057 server->namelen = NFS4_MAXNAMLEN;
1058
David Howells54ceac42006-08-22 20:06:13 -04001059 BUG_ON(!server->nfs_client);
1060 BUG_ON(!server->nfs_client->rpc_ops);
1061 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1062
1063 spin_lock(&nfs_client_lock);
1064 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1065 list_add_tail(&server->master_link, &nfs_volume_list);
1066 spin_unlock(&nfs_client_lock);
1067
1068 server->mount_time = jiffies;
1069 dprintk("<-- nfs4_create_server() = %p\n", server);
1070 return server;
1071
1072error:
1073 nfs_free_server(server);
1074 dprintk("<-- nfs4_create_server() = error %d\n", error);
1075 return ERR_PTR(error);
1076}
1077
1078/*
1079 * Create an NFS4 referral server record
1080 */
1081struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001082 struct nfs_fh *mntfh)
David Howells54ceac42006-08-22 20:06:13 -04001083{
1084 struct nfs_client *parent_client;
1085 struct nfs_server *server, *parent_server;
1086 struct nfs_fattr fattr;
1087 int error;
1088
1089 dprintk("--> nfs4_create_referral_server()\n");
1090
1091 server = nfs_alloc_server();
1092 if (!server)
1093 return ERR_PTR(-ENOMEM);
1094
1095 parent_server = NFS_SB(data->sb);
1096 parent_client = parent_server->nfs_client;
1097
1098 /* Get a client representation.
1099 * Note: NFSv4 always uses TCP, */
Chuck Leverdcecae02007-12-10 14:58:59 -05001100 error = nfs4_set_client(server, data->hostname,
Chuck Lever6677d092007-12-10 14:59:06 -05001101 data->addr,
1102 data->addrlen,
Chuck Leverdcecae02007-12-10 14:58:59 -05001103 parent_client->cl_ipaddr,
1104 data->authflavor,
1105 parent_server->client->cl_xprt->prot,
1106 parent_client->retrans_timeo,
1107 parent_client->retrans_count);
andros@citi.umich.edu297de4f2006-08-29 12:19:41 -04001108 if (error < 0)
1109 goto error;
David Howells54ceac42006-08-22 20:06:13 -04001110
1111 /* Initialise the client representation from the parent server */
1112 nfs_server_copy_userdata(server, parent_server);
1113 server->caps |= NFS_CAP_ATOMIC_OPEN;
1114
1115 error = nfs_init_server_rpcclient(server, data->authflavor);
1116 if (error < 0)
1117 goto error;
1118
1119 BUG_ON(!server->nfs_client);
1120 BUG_ON(!server->nfs_client->rpc_ops);
1121 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1122
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001123 /* Probe the root fh to retrieve its FSID and filehandle */
1124 error = nfs4_path_walk(server, mntfh, data->mnt_path);
1125 if (error < 0)
1126 goto error;
1127
David Howells54ceac42006-08-22 20:06:13 -04001128 /* probe the filesystem info for this server filesystem */
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001129 error = nfs_probe_fsinfo(server, mntfh, &fattr);
David Howells54ceac42006-08-22 20:06:13 -04001130 if (error < 0)
1131 goto error;
1132
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001133 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1134 server->namelen = NFS4_MAXNAMLEN;
1135
David Howells54ceac42006-08-22 20:06:13 -04001136 dprintk("Referral FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001137 (unsigned long long) server->fsid.major,
1138 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001139
1140 spin_lock(&nfs_client_lock);
1141 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1142 list_add_tail(&server->master_link, &nfs_volume_list);
1143 spin_unlock(&nfs_client_lock);
1144
1145 server->mount_time = jiffies;
1146
1147 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1148 return server;
1149
1150error:
1151 nfs_free_server(server);
1152 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1153 return ERR_PTR(error);
1154}
1155
1156#endif /* CONFIG_NFS_V4 */
1157
1158/*
1159 * Clone an NFS2, NFS3 or NFS4 server record
1160 */
1161struct nfs_server *nfs_clone_server(struct nfs_server *source,
1162 struct nfs_fh *fh,
1163 struct nfs_fattr *fattr)
1164{
1165 struct nfs_server *server;
1166 struct nfs_fattr fattr_fsinfo;
1167 int error;
1168
1169 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
David Howells6daabf12006-08-24 15:44:16 -04001170 (unsigned long long) fattr->fsid.major,
1171 (unsigned long long) fattr->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001172
1173 server = nfs_alloc_server();
1174 if (!server)
1175 return ERR_PTR(-ENOMEM);
1176
1177 /* Copy data from the source */
1178 server->nfs_client = source->nfs_client;
1179 atomic_inc(&server->nfs_client->cl_count);
1180 nfs_server_copy_userdata(server, source);
1181
1182 server->fsid = fattr->fsid;
1183
1184 error = nfs_init_server_rpcclient(server, source->client->cl_auth->au_flavor);
1185 if (error < 0)
1186 goto out_free_server;
1187 if (!IS_ERR(source->client_acl))
1188 nfs_init_server_aclclient(server);
1189
1190 /* probe the filesystem info for this server filesystem */
1191 error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo);
1192 if (error < 0)
1193 goto out_free_server;
1194
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001195 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1196 server->namelen = NFS4_MAXNAMLEN;
1197
David Howells54ceac42006-08-22 20:06:13 -04001198 dprintk("Cloned FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001199 (unsigned long long) server->fsid.major,
1200 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001201
1202 error = nfs_start_lockd(server);
1203 if (error < 0)
1204 goto out_free_server;
1205
1206 spin_lock(&nfs_client_lock);
1207 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1208 list_add_tail(&server->master_link, &nfs_volume_list);
1209 spin_unlock(&nfs_client_lock);
1210
1211 server->mount_time = jiffies;
1212
1213 dprintk("<-- nfs_clone_server() = %p\n", server);
1214 return server;
1215
1216out_free_server:
1217 nfs_free_server(server);
1218 dprintk("<-- nfs_clone_server() = error %d\n", error);
1219 return ERR_PTR(error);
1220}
David Howells6aaca562006-08-22 20:06:13 -04001221
1222#ifdef CONFIG_PROC_FS
1223static struct proc_dir_entry *proc_fs_nfs;
1224
1225static int nfs_server_list_open(struct inode *inode, struct file *file);
1226static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1227static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1228static void nfs_server_list_stop(struct seq_file *p, void *v);
1229static int nfs_server_list_show(struct seq_file *m, void *v);
1230
1231static struct seq_operations nfs_server_list_ops = {
1232 .start = nfs_server_list_start,
1233 .next = nfs_server_list_next,
1234 .stop = nfs_server_list_stop,
1235 .show = nfs_server_list_show,
1236};
1237
Arjan van de Ven00977a52007-02-12 00:55:34 -08001238static const struct file_operations nfs_server_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001239 .open = nfs_server_list_open,
1240 .read = seq_read,
1241 .llseek = seq_lseek,
1242 .release = seq_release,
1243};
1244
1245static int nfs_volume_list_open(struct inode *inode, struct file *file);
1246static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1247static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1248static void nfs_volume_list_stop(struct seq_file *p, void *v);
1249static int nfs_volume_list_show(struct seq_file *m, void *v);
1250
1251static struct seq_operations nfs_volume_list_ops = {
1252 .start = nfs_volume_list_start,
1253 .next = nfs_volume_list_next,
1254 .stop = nfs_volume_list_stop,
1255 .show = nfs_volume_list_show,
1256};
1257
Arjan van de Ven00977a52007-02-12 00:55:34 -08001258static const struct file_operations nfs_volume_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001259 .open = nfs_volume_list_open,
1260 .read = seq_read,
1261 .llseek = seq_lseek,
1262 .release = seq_release,
1263};
1264
1265/*
1266 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1267 * we're dealing
1268 */
1269static int nfs_server_list_open(struct inode *inode, struct file *file)
1270{
1271 struct seq_file *m;
1272 int ret;
1273
1274 ret = seq_open(file, &nfs_server_list_ops);
1275 if (ret < 0)
1276 return ret;
1277
1278 m = file->private_data;
1279 m->private = PDE(inode)->data;
1280
1281 return 0;
1282}
1283
1284/*
1285 * set up the iterator to start reading from the server list and return the first item
1286 */
1287static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1288{
David Howells6aaca562006-08-22 20:06:13 -04001289 /* lock the list against modification */
1290 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001291 return seq_list_start_head(&nfs_client_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001292}
1293
1294/*
1295 * move to next server
1296 */
1297static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1298{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001299 return seq_list_next(v, &nfs_client_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001300}
1301
1302/*
1303 * clean up after reading from the transports list
1304 */
1305static void nfs_server_list_stop(struct seq_file *p, void *v)
1306{
1307 spin_unlock(&nfs_client_lock);
1308}
1309
1310/*
1311 * display a header line followed by a load of call lines
1312 */
1313static int nfs_server_list_show(struct seq_file *m, void *v)
1314{
1315 struct nfs_client *clp;
1316
1317 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001318 if (v == &nfs_client_list) {
David Howells6aaca562006-08-22 20:06:13 -04001319 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1320 return 0;
1321 }
1322
1323 /* display one transport per line on subsequent lines */
1324 clp = list_entry(v, struct nfs_client, cl_share_link);
1325
Chuck Lever5d8515c2007-12-10 14:57:16 -05001326 seq_printf(m, "v%u %s %s %3d %s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001327 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001328 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1329 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001330 atomic_read(&clp->cl_count),
1331 clp->cl_hostname);
1332
1333 return 0;
1334}
1335
1336/*
1337 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1338 */
1339static int nfs_volume_list_open(struct inode *inode, struct file *file)
1340{
1341 struct seq_file *m;
1342 int ret;
1343
1344 ret = seq_open(file, &nfs_volume_list_ops);
1345 if (ret < 0)
1346 return ret;
1347
1348 m = file->private_data;
1349 m->private = PDE(inode)->data;
1350
1351 return 0;
1352}
1353
1354/*
1355 * set up the iterator to start reading from the volume list and return the first item
1356 */
1357static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1358{
David Howells6aaca562006-08-22 20:06:13 -04001359 /* lock the list against modification */
1360 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001361 return seq_list_start_head(&nfs_volume_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001362}
1363
1364/*
1365 * move to next volume
1366 */
1367static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1368{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001369 return seq_list_next(v, &nfs_volume_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001370}
1371
1372/*
1373 * clean up after reading from the transports list
1374 */
1375static void nfs_volume_list_stop(struct seq_file *p, void *v)
1376{
1377 spin_unlock(&nfs_client_lock);
1378}
1379
1380/*
1381 * display a header line followed by a load of call lines
1382 */
1383static int nfs_volume_list_show(struct seq_file *m, void *v)
1384{
1385 struct nfs_server *server;
1386 struct nfs_client *clp;
1387 char dev[8], fsid[17];
1388
1389 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001390 if (v == &nfs_volume_list) {
David Howells6aaca562006-08-22 20:06:13 -04001391 seq_puts(m, "NV SERVER PORT DEV FSID\n");
1392 return 0;
1393 }
1394 /* display one transport per line on subsequent lines */
1395 server = list_entry(v, struct nfs_server, master_link);
1396 clp = server->nfs_client;
1397
1398 snprintf(dev, 8, "%u:%u",
1399 MAJOR(server->s_dev), MINOR(server->s_dev));
1400
1401 snprintf(fsid, 17, "%llx:%llx",
David Howells6daabf12006-08-24 15:44:16 -04001402 (unsigned long long) server->fsid.major,
1403 (unsigned long long) server->fsid.minor);
David Howells6aaca562006-08-22 20:06:13 -04001404
Chuck Lever5d8515c2007-12-10 14:57:16 -05001405 seq_printf(m, "v%u %s %s %-7s %-17s\n",
Trond Myklebust40c553192007-12-14 14:56:07 -05001406 clp->rpc_ops->version,
Chuck Lever5d8515c2007-12-10 14:57:16 -05001407 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1408 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
David Howells6aaca562006-08-22 20:06:13 -04001409 dev,
1410 fsid);
1411
1412 return 0;
1413}
1414
1415/*
1416 * initialise the /proc/fs/nfsfs/ directory
1417 */
1418int __init nfs_fs_proc_init(void)
1419{
1420 struct proc_dir_entry *p;
1421
1422 proc_fs_nfs = proc_mkdir("nfsfs", proc_root_fs);
1423 if (!proc_fs_nfs)
1424 goto error_0;
1425
1426 proc_fs_nfs->owner = THIS_MODULE;
1427
1428 /* a file of servers with which we're dealing */
1429 p = create_proc_entry("servers", S_IFREG|S_IRUGO, proc_fs_nfs);
1430 if (!p)
1431 goto error_1;
1432
1433 p->proc_fops = &nfs_server_list_fops;
1434 p->owner = THIS_MODULE;
1435
1436 /* a file of volumes that we have mounted */
1437 p = create_proc_entry("volumes", S_IFREG|S_IRUGO, proc_fs_nfs);
1438 if (!p)
1439 goto error_2;
1440
1441 p->proc_fops = &nfs_volume_list_fops;
1442 p->owner = THIS_MODULE;
1443 return 0;
1444
1445error_2:
1446 remove_proc_entry("servers", proc_fs_nfs);
1447error_1:
1448 remove_proc_entry("nfsfs", proc_root_fs);
1449error_0:
1450 return -ENOMEM;
1451}
1452
1453/*
1454 * clean up the /proc/fs/nfsfs/ directory
1455 */
1456void nfs_fs_proc_exit(void)
1457{
1458 remove_proc_entry("volumes", proc_fs_nfs);
1459 remove_proc_entry("servers", proc_fs_nfs);
1460 remove_proc_entry("nfsfs", proc_root_fs);
1461}
1462
1463#endif /* CONFIG_PROC_FS */