blob: 50c6821bad269b50a84a7f8ccb30210d045f9777 [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>
15
16#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>
26#include <linux/nfs_fs.h>
27#include <linux/nfs_mount.h>
28#include <linux/nfs4_mount.h>
29#include <linux/lockd/bind.h>
David Howells24c8dbb2006-08-22 20:06:10 -040030#include <linux/seq_file.h>
31#include <linux/mount.h>
32#include <linux/nfs_idmap.h>
33#include <linux/vfs.h>
34#include <linux/inet.h>
35#include <linux/nfs_xdr.h>
36
37#include <asm/system.h>
38
39#include "nfs4_fs.h"
40#include "callback.h"
41#include "delegation.h"
42#include "iostat.h"
43#include "internal.h"
44
45#define NFSDBG_FACILITY NFSDBG_CLIENT
46
47static DEFINE_SPINLOCK(nfs_client_lock);
48static LIST_HEAD(nfs_client_list);
David Howells54ceac42006-08-22 20:06:13 -040049static LIST_HEAD(nfs_volume_list);
David Howells24c8dbb2006-08-22 20:06:10 -040050static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
51
52/*
David Howells5006a762006-08-22 20:06:12 -040053 * RPC cruft for NFS
54 */
55static struct rpc_version *nfs_version[5] = {
56 [2] = &nfs_version2,
57#ifdef CONFIG_NFS_V3
58 [3] = &nfs_version3,
59#endif
60#ifdef CONFIG_NFS_V4
61 [4] = &nfs_version4,
62#endif
63};
64
65struct rpc_program nfs_program = {
66 .name = "nfs",
67 .number = NFS_PROGRAM,
68 .nrvers = ARRAY_SIZE(nfs_version),
69 .version = nfs_version,
70 .stats = &nfs_rpcstat,
71 .pipe_dir_name = "/nfs",
72};
73
74struct rpc_stat nfs_rpcstat = {
75 .program = &nfs_program
76};
77
78
79#ifdef CONFIG_NFS_V3_ACL
80static struct rpc_stat nfsacl_rpcstat = { &nfsacl_program };
81static struct rpc_version * nfsacl_version[] = {
82 [3] = &nfsacl_version3,
83};
84
85struct rpc_program nfsacl_program = {
86 .name = "nfsacl",
87 .number = NFS_ACL_PROGRAM,
88 .nrvers = ARRAY_SIZE(nfsacl_version),
89 .version = nfsacl_version,
90 .stats = &nfsacl_rpcstat,
91};
92#endif /* CONFIG_NFS_V3_ACL */
93
94/*
David Howells24c8dbb2006-08-22 20:06:10 -040095 * Allocate a shared client record
96 *
97 * Since these are allocated/deallocated very rarely, we don't
98 * bother putting them in a slab cache...
99 */
100static struct nfs_client *nfs_alloc_client(const char *hostname,
101 const struct sockaddr_in *addr,
102 int nfsversion)
103{
104 struct nfs_client *clp;
105 int error;
106
107 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
108 goto error_0;
109
110 error = rpciod_up();
111 if (error < 0) {
112 dprintk("%s: couldn't start rpciod! Error = %d\n",
113 __FUNCTION__, error);
David Howells24c8dbb2006-08-22 20:06:10 -0400114 goto error_1;
115 }
Trond Myklebust9c5bf382006-08-22 20:06:14 -0400116 __set_bit(NFS_CS_RPCIOD, &clp->cl_res_state);
David Howells24c8dbb2006-08-22 20:06:10 -0400117
118 if (nfsversion == 4) {
119 if (nfs_callback_up() < 0)
120 goto error_2;
121 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
122 }
123
124 atomic_set(&clp->cl_count, 1);
125 clp->cl_cons_state = NFS_CS_INITING;
126
127 clp->cl_nfsversion = nfsversion;
128 memcpy(&clp->cl_addr, addr, sizeof(clp->cl_addr));
129
130 if (hostname) {
131 clp->cl_hostname = kstrdup(hostname, GFP_KERNEL);
132 if (!clp->cl_hostname)
133 goto error_3;
134 }
135
136 INIT_LIST_HEAD(&clp->cl_superblocks);
137 clp->cl_rpcclient = ERR_PTR(-EINVAL);
138
139#ifdef CONFIG_NFS_V4
140 init_rwsem(&clp->cl_sem);
141 INIT_LIST_HEAD(&clp->cl_delegations);
142 INIT_LIST_HEAD(&clp->cl_state_owners);
143 INIT_LIST_HEAD(&clp->cl_unused);
144 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:
157 rpciod_down();
158 __clear_bit(NFS_CS_RPCIOD, &clp->cl_res_state);
159error_1:
160 kfree(clp);
161error_0:
162 return NULL;
163}
164
Trond Myklebust5dd31772006-08-24 01:03:05 -0400165static void nfs4_shutdown_client(struct nfs_client *clp)
166{
167#ifdef CONFIG_NFS_V4
168 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
169 nfs4_kill_renewd(clp);
170 while (!list_empty(&clp->cl_unused)) {
171 struct nfs4_state_owner *sp;
172
173 sp = list_entry(clp->cl_unused.next,
174 struct nfs4_state_owner,
175 so_list);
176 list_del(&sp->so_list);
177 kfree(sp);
178 }
179 BUG_ON(!list_empty(&clp->cl_state_owners));
180 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
181 nfs_idmap_delete(clp);
182#endif
183}
184
David Howells24c8dbb2006-08-22 20:06:10 -0400185/*
186 * Destroy a shared client record
187 */
188static void nfs_free_client(struct nfs_client *clp)
189{
190 dprintk("--> nfs_free_client(%d)\n", clp->cl_nfsversion);
191
Trond Myklebust5dd31772006-08-24 01:03:05 -0400192 nfs4_shutdown_client(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400193
194 /* -EIO all pending I/O */
195 if (!IS_ERR(clp->cl_rpcclient))
196 rpc_shutdown_client(clp->cl_rpcclient);
197
198 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
199 nfs_callback_down();
200
201 if (__test_and_clear_bit(NFS_CS_RPCIOD, &clp->cl_res_state))
Trond Myklebust9c5bf382006-08-22 20:06:14 -0400202 rpciod_down();
David Howells24c8dbb2006-08-22 20:06:10 -0400203
204 kfree(clp->cl_hostname);
205 kfree(clp);
206
207 dprintk("<-- nfs_free_client()\n");
208}
209
210/*
211 * Release a reference to a shared client record
212 */
213void nfs_put_client(struct nfs_client *clp)
214{
David Howells27ba8512006-07-30 14:40:56 -0400215 if (!clp)
216 return;
217
David Howells24c8dbb2006-08-22 20:06:10 -0400218 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
219
220 if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
221 list_del(&clp->cl_share_link);
222 spin_unlock(&nfs_client_lock);
223
224 BUG_ON(!list_empty(&clp->cl_superblocks));
225
226 nfs_free_client(clp);
227 }
228}
229
230/*
231 * Find a client by address
232 * - caller must hold nfs_client_lock
233 */
Trond Myklebust13bbc062006-10-19 23:28:40 -0700234static struct nfs_client *__nfs_find_client(const struct sockaddr_in *addr, int nfsversion, int match_port)
David Howells24c8dbb2006-08-22 20:06:10 -0400235{
236 struct nfs_client *clp;
237
238 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Trond Myklebust13bbc062006-10-19 23:28:40 -0700239 /* Don't match clients that failed to initialise properly */
240 if (clp->cl_cons_state < 0)
241 continue;
242
David Howells24c8dbb2006-08-22 20:06:10 -0400243 /* Different NFS versions cannot share the same nfs_client */
244 if (clp->cl_nfsversion != nfsversion)
245 continue;
246
247 if (memcmp(&clp->cl_addr.sin_addr, &addr->sin_addr,
248 sizeof(clp->cl_addr.sin_addr)) != 0)
249 continue;
250
Trond Myklebust13bbc062006-10-19 23:28:40 -0700251 if (!match_port || clp->cl_addr.sin_port == addr->sin_port)
David Howells24c8dbb2006-08-22 20:06:10 -0400252 goto found;
253 }
254
255 return NULL;
256
257found:
258 atomic_inc(&clp->cl_count);
259 return clp;
260}
261
262/*
263 * Find a client by IP address and protocol version
264 * - returns NULL if no such client
265 */
266struct nfs_client *nfs_find_client(const struct sockaddr_in *addr, int nfsversion)
267{
268 struct nfs_client *clp;
269
270 spin_lock(&nfs_client_lock);
Trond Myklebust13bbc062006-10-19 23:28:40 -0700271 clp = __nfs_find_client(addr, nfsversion, 0);
David Howells24c8dbb2006-08-22 20:06:10 -0400272 spin_unlock(&nfs_client_lock);
Trond Myklebust13bbc062006-10-19 23:28:40 -0700273 if (clp != NULL && clp->cl_cons_state != NFS_CS_READY) {
274 nfs_put_client(clp);
275 clp = NULL;
276 }
David Howells24c8dbb2006-08-22 20:06:10 -0400277 return clp;
278}
279
280/*
281 * Look up a client by IP address and protocol version
282 * - creates a new record if one doesn't yet exist
283 */
David Howells54ceac42006-08-22 20:06:13 -0400284static struct nfs_client *nfs_get_client(const char *hostname,
285 const struct sockaddr_in *addr,
286 int nfsversion)
David Howells24c8dbb2006-08-22 20:06:10 -0400287{
288 struct nfs_client *clp, *new = NULL;
289 int error;
290
291 dprintk("--> nfs_get_client(%s,"NIPQUAD_FMT":%d,%d)\n",
292 hostname ?: "", NIPQUAD(addr->sin_addr),
293 addr->sin_port, nfsversion);
294
295 /* see if the client already exists */
296 do {
297 spin_lock(&nfs_client_lock);
298
Trond Myklebust13bbc062006-10-19 23:28:40 -0700299 clp = __nfs_find_client(addr, nfsversion, 1);
David Howells24c8dbb2006-08-22 20:06:10 -0400300 if (clp)
301 goto found_client;
302 if (new)
303 goto install_client;
304
305 spin_unlock(&nfs_client_lock);
306
307 new = nfs_alloc_client(hostname, addr, nfsversion);
308 } while (new);
309
310 return ERR_PTR(-ENOMEM);
311
312 /* install a new client and return with it unready */
313install_client:
314 clp = new;
315 list_add(&clp->cl_share_link, &nfs_client_list);
316 spin_unlock(&nfs_client_lock);
317 dprintk("--> nfs_get_client() = %p [new]\n", clp);
318 return clp;
319
320 /* found an existing client
321 * - make sure it's ready before returning
322 */
323found_client:
324 spin_unlock(&nfs_client_lock);
325
326 if (new)
327 nfs_free_client(new);
328
Linus Torvalds83250492006-10-08 17:28:25 -0700329 error = wait_event_interruptible(nfs_client_active_wq,
Trond Myklebust0bae89e2006-10-08 14:33:24 -0400330 clp->cl_cons_state != NFS_CS_INITING);
331 if (error < 0) {
332 nfs_put_client(clp);
333 return ERR_PTR(-ERESTARTSYS);
David Howells24c8dbb2006-08-22 20:06:10 -0400334 }
335
336 if (clp->cl_cons_state < NFS_CS_READY) {
337 error = clp->cl_cons_state;
338 nfs_put_client(clp);
339 return ERR_PTR(error);
340 }
341
David Howells54ceac42006-08-22 20:06:13 -0400342 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
343
David Howells24c8dbb2006-08-22 20:06:10 -0400344 dprintk("--> nfs_get_client() = %p [share]\n", clp);
345 return clp;
346}
347
348/*
349 * Mark a server as ready or failed
350 */
David Howells54ceac42006-08-22 20:06:13 -0400351static void nfs_mark_client_ready(struct nfs_client *clp, int state)
David Howells24c8dbb2006-08-22 20:06:10 -0400352{
353 clp->cl_cons_state = state;
354 wake_up_all(&nfs_client_active_wq);
355}
David Howells5006a762006-08-22 20:06:12 -0400356
357/*
358 * Initialise the timeout values for a connection
359 */
360static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
361 unsigned int timeo, unsigned int retrans)
362{
363 to->to_initval = timeo * HZ / 10;
364 to->to_retries = retrans;
365 if (!to->to_retries)
366 to->to_retries = 2;
367
368 switch (proto) {
369 case IPPROTO_TCP:
370 if (!to->to_initval)
371 to->to_initval = 60 * HZ;
372 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
373 to->to_initval = NFS_MAX_TCP_TIMEOUT;
374 to->to_increment = to->to_initval;
375 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
376 to->to_exponential = 0;
377 break;
378 case IPPROTO_UDP:
379 default:
380 if (!to->to_initval)
381 to->to_initval = 11 * HZ / 10;
382 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
383 to->to_initval = NFS_MAX_UDP_TIMEOUT;
384 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
385 to->to_exponential = 1;
386 break;
387 }
388}
389
390/*
391 * Create an RPC client handle
392 */
David Howells54ceac42006-08-22 20:06:13 -0400393static int nfs_create_rpc_client(struct nfs_client *clp, int proto,
394 unsigned int timeo,
395 unsigned int retrans,
Chuck Lever43d78ef2007-02-06 18:26:11 -0500396 rpc_authflavor_t flavor,
397 int flags)
David Howells5006a762006-08-22 20:06:12 -0400398{
399 struct rpc_timeout timeparms;
David Howells5006a762006-08-22 20:06:12 -0400400 struct rpc_clnt *clnt = NULL;
Chuck Lever41877d22006-08-22 20:06:20 -0400401 struct rpc_create_args args = {
402 .protocol = proto,
403 .address = (struct sockaddr *)&clp->cl_addr,
404 .addrsize = sizeof(clp->cl_addr),
405 .timeout = &timeparms,
406 .servername = clp->cl_hostname,
407 .program = &nfs_program,
408 .version = clp->rpc_ops->version,
409 .authflavor = flavor,
Chuck Lever43d78ef2007-02-06 18:26:11 -0500410 .flags = flags,
Chuck Lever41877d22006-08-22 20:06:20 -0400411 };
David Howells5006a762006-08-22 20:06:12 -0400412
413 if (!IS_ERR(clp->cl_rpcclient))
414 return 0;
415
416 nfs_init_timeout_values(&timeparms, proto, timeo, retrans);
417 clp->retrans_timeo = timeparms.to_initval;
418 clp->retrans_count = timeparms.to_retries;
419
Chuck Lever41877d22006-08-22 20:06:20 -0400420 clnt = rpc_create(&args);
David Howells5006a762006-08-22 20:06:12 -0400421 if (IS_ERR(clnt)) {
422 dprintk("%s: cannot create RPC client. Error = %ld\n",
423 __FUNCTION__, PTR_ERR(clnt));
424 return PTR_ERR(clnt);
425 }
426
David Howells5006a762006-08-22 20:06:12 -0400427 clp->cl_rpcclient = clnt;
428 return 0;
429}
David Howells54ceac42006-08-22 20:06:13 -0400430
431/*
432 * Version 2 or 3 client destruction
433 */
434static void nfs_destroy_server(struct nfs_server *server)
435{
436 if (!IS_ERR(server->client_acl))
437 rpc_shutdown_client(server->client_acl);
438
439 if (!(server->flags & NFS_MOUNT_NONLM))
440 lockd_down(); /* release rpc.lockd */
441}
442
443/*
444 * Version 2 or 3 lockd setup
445 */
446static int nfs_start_lockd(struct nfs_server *server)
447{
448 int error = 0;
449
450 if (server->nfs_client->cl_nfsversion > 3)
451 goto out;
452 if (server->flags & NFS_MOUNT_NONLM)
453 goto out;
NeilBrown24e36662006-10-02 02:17:45 -0700454 error = lockd_up((server->flags & NFS_MOUNT_TCP) ?
455 IPPROTO_TCP : IPPROTO_UDP);
David Howells54ceac42006-08-22 20:06:13 -0400456 if (error < 0)
457 server->flags |= NFS_MOUNT_NONLM;
458 else
459 server->destroy = nfs_destroy_server;
460out:
461 return error;
462}
463
464/*
465 * Initialise an NFSv3 ACL client connection
466 */
467#ifdef CONFIG_NFS_V3_ACL
468static void nfs_init_server_aclclient(struct nfs_server *server)
469{
470 if (server->nfs_client->cl_nfsversion != 3)
471 goto out_noacl;
472 if (server->flags & NFS_MOUNT_NOACL)
473 goto out_noacl;
474
475 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
476 if (IS_ERR(server->client_acl))
477 goto out_noacl;
478
479 /* No errors! Assume that Sun nfsacls are supported */
480 server->caps |= NFS_CAP_ACLS;
481 return;
482
483out_noacl:
484 server->caps &= ~NFS_CAP_ACLS;
485}
486#else
487static inline void nfs_init_server_aclclient(struct nfs_server *server)
488{
489 server->flags &= ~NFS_MOUNT_NOACL;
490 server->caps &= ~NFS_CAP_ACLS;
491}
492#endif
493
494/*
495 * Create a general RPC client
496 */
497static int nfs_init_server_rpcclient(struct nfs_server *server, rpc_authflavor_t pseudoflavour)
498{
499 struct nfs_client *clp = server->nfs_client;
500
501 server->client = rpc_clone_client(clp->cl_rpcclient);
502 if (IS_ERR(server->client)) {
503 dprintk("%s: couldn't create rpc_client!\n", __FUNCTION__);
504 return PTR_ERR(server->client);
505 }
506
507 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
508 struct rpc_auth *auth;
509
510 auth = rpcauth_create(pseudoflavour, server->client);
511 if (IS_ERR(auth)) {
512 dprintk("%s: couldn't create credcache!\n", __FUNCTION__);
513 return PTR_ERR(auth);
514 }
515 }
516 server->client->cl_softrtry = 0;
517 if (server->flags & NFS_MOUNT_SOFT)
518 server->client->cl_softrtry = 1;
519
520 server->client->cl_intr = 0;
521 if (server->flags & NFS4_MOUNT_INTR)
522 server->client->cl_intr = 1;
523
524 return 0;
525}
526
527/*
528 * Initialise an NFS2 or NFS3 client
529 */
530static int nfs_init_client(struct nfs_client *clp, const struct nfs_mount_data *data)
531{
532 int proto = (data->flags & NFS_MOUNT_TCP) ? IPPROTO_TCP : IPPROTO_UDP;
533 int error;
534
535 if (clp->cl_cons_state == NFS_CS_READY) {
536 /* the client is already initialised */
537 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
538 return 0;
539 }
540
541 /* Check NFS protocol revision and initialize RPC op vector */
542 clp->rpc_ops = &nfs_v2_clientops;
543#ifdef CONFIG_NFS_V3
544 if (clp->cl_nfsversion == 3)
545 clp->rpc_ops = &nfs_v3_clientops;
546#endif
547 /*
548 * Create a client RPC handle for doing FSSTAT with UNIX auth only
549 * - RFC 2623, sec 2.3.2
550 */
551 error = nfs_create_rpc_client(clp, proto, data->timeo, data->retrans,
Chuck Lever43d78ef2007-02-06 18:26:11 -0500552 RPC_AUTH_UNIX, 0);
David Howells54ceac42006-08-22 20:06:13 -0400553 if (error < 0)
554 goto error;
555 nfs_mark_client_ready(clp, NFS_CS_READY);
556 return 0;
557
558error:
559 nfs_mark_client_ready(clp, error);
560 dprintk("<-- nfs_init_client() = xerror %d\n", error);
561 return error;
562}
563
564/*
565 * Create a version 2 or 3 client
566 */
567static int nfs_init_server(struct nfs_server *server, const struct nfs_mount_data *data)
568{
569 struct nfs_client *clp;
570 int error, nfsvers = 2;
571
572 dprintk("--> nfs_init_server()\n");
573
574#ifdef CONFIG_NFS_V3
575 if (data->flags & NFS_MOUNT_VER3)
576 nfsvers = 3;
577#endif
578
579 /* Allocate or find a client reference we can use */
580 clp = nfs_get_client(data->hostname, &data->addr, nfsvers);
581 if (IS_ERR(clp)) {
582 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
583 return PTR_ERR(clp);
584 }
585
586 error = nfs_init_client(clp, data);
587 if (error < 0)
588 goto error;
589
590 server->nfs_client = clp;
591
592 /* Initialise the client representation from the mount data */
593 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
594
595 if (data->rsize)
596 server->rsize = nfs_block_size(data->rsize, NULL);
597 if (data->wsize)
598 server->wsize = nfs_block_size(data->wsize, NULL);
599
600 server->acregmin = data->acregmin * HZ;
601 server->acregmax = data->acregmax * HZ;
602 server->acdirmin = data->acdirmin * HZ;
603 server->acdirmax = data->acdirmax * HZ;
604
605 /* Start lockd here, before we might error out */
606 error = nfs_start_lockd(server);
607 if (error < 0)
608 goto error;
609
610 error = nfs_init_server_rpcclient(server, data->pseudoflavor);
611 if (error < 0)
612 goto error;
613
614 server->namelen = data->namlen;
615 /* Create a client RPC handle for the NFSv3 ACL management interface */
616 nfs_init_server_aclclient(server);
617 if (clp->cl_nfsversion == 3) {
618 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
619 server->namelen = NFS3_MAXNAMLEN;
Steve Dickson74dd34e2007-04-14 17:01:15 -0400620 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
621 server->caps |= NFS_CAP_READDIRPLUS;
David Howells54ceac42006-08-22 20:06:13 -0400622 } else {
623 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
624 server->namelen = NFS2_MAXNAMLEN;
625 }
626
627 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
628 return 0;
629
630error:
631 server->nfs_client = NULL;
632 nfs_put_client(clp);
633 dprintk("<-- nfs_init_server() = xerror %d\n", error);
634 return error;
635}
636
637/*
638 * Load up the server record from information gained in an fsinfo record
639 */
640static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
641{
642 unsigned long max_rpc_payload;
643
644 /* Work out a lot of parameters */
645 if (server->rsize == 0)
646 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
647 if (server->wsize == 0)
648 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
649
650 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
651 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
652 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
653 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
654
655 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
656 if (server->rsize > max_rpc_payload)
657 server->rsize = max_rpc_payload;
658 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
659 server->rsize = NFS_MAX_FILE_IO_SIZE;
660 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
661 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
662
663 if (server->wsize > max_rpc_payload)
664 server->wsize = max_rpc_payload;
665 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
666 server->wsize = NFS_MAX_FILE_IO_SIZE;
667 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
668 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
669
670 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
671 if (server->dtsize > PAGE_CACHE_SIZE)
672 server->dtsize = PAGE_CACHE_SIZE;
673 if (server->dtsize > server->rsize)
674 server->dtsize = server->rsize;
675
676 if (server->flags & NFS_MOUNT_NOAC) {
677 server->acregmin = server->acregmax = 0;
678 server->acdirmin = server->acdirmax = 0;
679 }
680
681 server->maxfilesize = fsinfo->maxfilesize;
682
683 /* We're airborne Set socket buffersize */
684 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
685}
686
687/*
688 * Probe filesystem information, including the FSID on v2/v3
689 */
690static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
691{
692 struct nfs_fsinfo fsinfo;
693 struct nfs_client *clp = server->nfs_client;
694 int error;
695
696 dprintk("--> nfs_probe_fsinfo()\n");
697
698 if (clp->rpc_ops->set_capabilities != NULL) {
699 error = clp->rpc_ops->set_capabilities(server, mntfh);
700 if (error < 0)
701 goto out_error;
702 }
703
704 fsinfo.fattr = fattr;
705 nfs_fattr_init(fattr);
706 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
707 if (error < 0)
708 goto out_error;
709
710 nfs_server_set_fsinfo(server, &fsinfo);
711
712 /* Get some general file system info */
713 if (server->namelen == 0) {
714 struct nfs_pathconf pathinfo;
715
716 pathinfo.fattr = fattr;
717 nfs_fattr_init(fattr);
718
719 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
720 server->namelen = pathinfo.max_namelen;
721 }
722
723 dprintk("<-- nfs_probe_fsinfo() = 0\n");
724 return 0;
725
726out_error:
727 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
728 return error;
729}
730
731/*
732 * Copy useful information when duplicating a server record
733 */
734static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
735{
736 target->flags = source->flags;
737 target->acregmin = source->acregmin;
738 target->acregmax = source->acregmax;
739 target->acdirmin = source->acdirmin;
740 target->acdirmax = source->acdirmax;
741 target->caps = source->caps;
742}
743
744/*
745 * Allocate and initialise a server record
746 */
747static struct nfs_server *nfs_alloc_server(void)
748{
749 struct nfs_server *server;
750
751 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
752 if (!server)
753 return NULL;
754
755 server->client = server->client_acl = ERR_PTR(-EINVAL);
756
757 /* Zero out the NFS state stuff */
758 INIT_LIST_HEAD(&server->client_link);
759 INIT_LIST_HEAD(&server->master_link);
760
761 server->io_stats = nfs_alloc_iostats();
762 if (!server->io_stats) {
763 kfree(server);
764 return NULL;
765 }
766
767 return server;
768}
769
770/*
771 * Free up a server record
772 */
773void nfs_free_server(struct nfs_server *server)
774{
775 dprintk("--> nfs_free_server()\n");
776
777 spin_lock(&nfs_client_lock);
778 list_del(&server->client_link);
779 list_del(&server->master_link);
780 spin_unlock(&nfs_client_lock);
781
782 if (server->destroy != NULL)
783 server->destroy(server);
784 if (!IS_ERR(server->client))
785 rpc_shutdown_client(server->client);
786
787 nfs_put_client(server->nfs_client);
788
789 nfs_free_iostats(server->io_stats);
790 kfree(server);
791 nfs_release_automount_timer();
792 dprintk("<-- nfs_free_server()\n");
793}
794
795/*
796 * Create a version 2 or 3 volume record
797 * - keyed on server and FSID
798 */
799struct nfs_server *nfs_create_server(const struct nfs_mount_data *data,
800 struct nfs_fh *mntfh)
801{
802 struct nfs_server *server;
803 struct nfs_fattr fattr;
804 int error;
805
806 server = nfs_alloc_server();
807 if (!server)
808 return ERR_PTR(-ENOMEM);
809
810 /* Get a client representation */
811 error = nfs_init_server(server, data);
812 if (error < 0)
813 goto error;
814
815 BUG_ON(!server->nfs_client);
816 BUG_ON(!server->nfs_client->rpc_ops);
817 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
818
819 /* Probe the root fh to retrieve its FSID */
820 error = nfs_probe_fsinfo(server, mntfh, &fattr);
821 if (error < 0)
822 goto error;
823 if (!(fattr.valid & NFS_ATTR_FATTR)) {
824 error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
825 if (error < 0) {
826 dprintk("nfs_create_server: getattr error = %d\n", -error);
827 goto error;
828 }
829 }
830 memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
831
David Howells6daabf12006-08-24 15:44:16 -0400832 dprintk("Server FSID: %llx:%llx\n",
833 (unsigned long long) server->fsid.major,
834 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -0400835
836 BUG_ON(!server->nfs_client);
837 BUG_ON(!server->nfs_client->rpc_ops);
838 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
839
840 spin_lock(&nfs_client_lock);
841 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
842 list_add_tail(&server->master_link, &nfs_volume_list);
843 spin_unlock(&nfs_client_lock);
844
845 server->mount_time = jiffies;
846 return server;
847
848error:
849 nfs_free_server(server);
850 return ERR_PTR(error);
851}
852
853#ifdef CONFIG_NFS_V4
854/*
855 * Initialise an NFS4 client record
856 */
857static int nfs4_init_client(struct nfs_client *clp,
858 int proto, int timeo, int retrans,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -0700859 const char *ip_addr,
David Howells54ceac42006-08-22 20:06:13 -0400860 rpc_authflavor_t authflavour)
861{
862 int error;
863
864 if (clp->cl_cons_state == NFS_CS_READY) {
865 /* the client is initialised already */
866 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
867 return 0;
868 }
869
870 /* Check NFS protocol revision and initialize RPC op vector */
871 clp->rpc_ops = &nfs_v4_clientops;
872
Chuck Lever43d78ef2007-02-06 18:26:11 -0500873 error = nfs_create_rpc_client(clp, proto, timeo, retrans, authflavour,
874 RPC_CLNT_CREATE_DISCRTRY);
David Howells54ceac42006-08-22 20:06:13 -0400875 if (error < 0)
876 goto error;
J. Bruce Fields7d9ac062006-10-19 23:28:39 -0700877 memcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
David Howells54ceac42006-08-22 20:06:13 -0400878
879 error = nfs_idmap_new(clp);
880 if (error < 0) {
881 dprintk("%s: failed to create idmapper. Error = %d\n",
882 __FUNCTION__, error);
David Howells54ceac42006-08-22 20:06:13 -0400883 goto error;
884 }
Trond Myklebust9c5bf382006-08-22 20:06:14 -0400885 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
David Howells54ceac42006-08-22 20:06:13 -0400886
887 nfs_mark_client_ready(clp, NFS_CS_READY);
888 return 0;
889
890error:
891 nfs_mark_client_ready(clp, error);
892 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
893 return error;
894}
895
896/*
897 * Set up an NFS4 client
898 */
899static int nfs4_set_client(struct nfs_server *server,
900 const char *hostname, const struct sockaddr_in *addr,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -0700901 const char *ip_addr,
David Howells54ceac42006-08-22 20:06:13 -0400902 rpc_authflavor_t authflavour,
903 int proto, int timeo, int retrans)
904{
905 struct nfs_client *clp;
906 int error;
907
908 dprintk("--> nfs4_set_client()\n");
909
910 /* Allocate or find a client reference we can use */
911 clp = nfs_get_client(hostname, addr, 4);
912 if (IS_ERR(clp)) {
913 error = PTR_ERR(clp);
914 goto error;
915 }
J. Bruce Fields7d9ac062006-10-19 23:28:39 -0700916 error = nfs4_init_client(clp, proto, timeo, retrans, ip_addr, authflavour);
David Howells54ceac42006-08-22 20:06:13 -0400917 if (error < 0)
918 goto error_put;
919
920 server->nfs_client = clp;
921 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
922 return 0;
923
924error_put:
925 nfs_put_client(clp);
926error:
927 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
928 return error;
929}
930
931/*
932 * Create a version 4 volume record
933 */
934static int nfs4_init_server(struct nfs_server *server,
935 const struct nfs4_mount_data *data, rpc_authflavor_t authflavour)
936{
937 int error;
938
939 dprintk("--> nfs4_init_server()\n");
940
941 /* Initialise the client representation from the mount data */
942 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
943 server->caps |= NFS_CAP_ATOMIC_OPEN;
944
945 if (data->rsize)
946 server->rsize = nfs_block_size(data->rsize, NULL);
947 if (data->wsize)
948 server->wsize = nfs_block_size(data->wsize, NULL);
949
950 server->acregmin = data->acregmin * HZ;
951 server->acregmax = data->acregmax * HZ;
952 server->acdirmin = data->acdirmin * HZ;
953 server->acdirmax = data->acdirmax * HZ;
954
955 error = nfs_init_server_rpcclient(server, authflavour);
956
957 /* Done */
958 dprintk("<-- nfs4_init_server() = %d\n", error);
959 return error;
960}
961
962/*
963 * Create a version 4 volume record
964 * - keyed on server and FSID
965 */
966struct nfs_server *nfs4_create_server(const struct nfs4_mount_data *data,
967 const char *hostname,
968 const struct sockaddr_in *addr,
969 const char *mntpath,
970 const char *ip_addr,
971 rpc_authflavor_t authflavour,
972 struct nfs_fh *mntfh)
973{
974 struct nfs_fattr fattr;
975 struct nfs_server *server;
976 int error;
977
978 dprintk("--> nfs4_create_server()\n");
979
980 server = nfs_alloc_server();
981 if (!server)
982 return ERR_PTR(-ENOMEM);
983
984 /* Get a client record */
J. Bruce Fields7d9ac062006-10-19 23:28:39 -0700985 error = nfs4_set_client(server, hostname, addr, ip_addr, authflavour,
David Howells54ceac42006-08-22 20:06:13 -0400986 data->proto, data->timeo, data->retrans);
987 if (error < 0)
988 goto error;
989
990 /* set up the general RPC client */
991 error = nfs4_init_server(server, data, authflavour);
992 if (error < 0)
993 goto error;
994
995 BUG_ON(!server->nfs_client);
996 BUG_ON(!server->nfs_client->rpc_ops);
997 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
998
999 /* Probe the root fh to retrieve its FSID */
1000 error = nfs4_path_walk(server, mntfh, mntpath);
1001 if (error < 0)
1002 goto error;
1003
David Howells6daabf12006-08-24 15:44:16 -04001004 dprintk("Server FSID: %llx:%llx\n",
1005 (unsigned long long) server->fsid.major,
1006 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001007 dprintk("Mount FH: %d\n", mntfh->size);
1008
1009 error = nfs_probe_fsinfo(server, mntfh, &fattr);
1010 if (error < 0)
1011 goto error;
1012
1013 BUG_ON(!server->nfs_client);
1014 BUG_ON(!server->nfs_client->rpc_ops);
1015 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1016
1017 spin_lock(&nfs_client_lock);
1018 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1019 list_add_tail(&server->master_link, &nfs_volume_list);
1020 spin_unlock(&nfs_client_lock);
1021
1022 server->mount_time = jiffies;
1023 dprintk("<-- nfs4_create_server() = %p\n", server);
1024 return server;
1025
1026error:
1027 nfs_free_server(server);
1028 dprintk("<-- nfs4_create_server() = error %d\n", error);
1029 return ERR_PTR(error);
1030}
1031
1032/*
1033 * Create an NFS4 referral server record
1034 */
1035struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001036 struct nfs_fh *mntfh)
David Howells54ceac42006-08-22 20:06:13 -04001037{
1038 struct nfs_client *parent_client;
1039 struct nfs_server *server, *parent_server;
1040 struct nfs_fattr fattr;
1041 int error;
1042
1043 dprintk("--> nfs4_create_referral_server()\n");
1044
1045 server = nfs_alloc_server();
1046 if (!server)
1047 return ERR_PTR(-ENOMEM);
1048
1049 parent_server = NFS_SB(data->sb);
1050 parent_client = parent_server->nfs_client;
1051
1052 /* Get a client representation.
1053 * Note: NFSv4 always uses TCP, */
1054 error = nfs4_set_client(server, data->hostname, data->addr,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001055 parent_client->cl_ipaddr,
David Howells54ceac42006-08-22 20:06:13 -04001056 data->authflavor,
1057 parent_server->client->cl_xprt->prot,
1058 parent_client->retrans_timeo,
1059 parent_client->retrans_count);
andros@citi.umich.edu297de4f2006-08-29 12:19:41 -04001060 if (error < 0)
1061 goto error;
David Howells54ceac42006-08-22 20:06:13 -04001062
1063 /* Initialise the client representation from the parent server */
1064 nfs_server_copy_userdata(server, parent_server);
1065 server->caps |= NFS_CAP_ATOMIC_OPEN;
1066
1067 error = nfs_init_server_rpcclient(server, data->authflavor);
1068 if (error < 0)
1069 goto error;
1070
1071 BUG_ON(!server->nfs_client);
1072 BUG_ON(!server->nfs_client->rpc_ops);
1073 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1074
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001075 /* Probe the root fh to retrieve its FSID and filehandle */
1076 error = nfs4_path_walk(server, mntfh, data->mnt_path);
1077 if (error < 0)
1078 goto error;
1079
David Howells54ceac42006-08-22 20:06:13 -04001080 /* probe the filesystem info for this server filesystem */
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001081 error = nfs_probe_fsinfo(server, mntfh, &fattr);
David Howells54ceac42006-08-22 20:06:13 -04001082 if (error < 0)
1083 goto error;
1084
1085 dprintk("Referral FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001086 (unsigned long long) server->fsid.major,
1087 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001088
1089 spin_lock(&nfs_client_lock);
1090 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1091 list_add_tail(&server->master_link, &nfs_volume_list);
1092 spin_unlock(&nfs_client_lock);
1093
1094 server->mount_time = jiffies;
1095
1096 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1097 return server;
1098
1099error:
1100 nfs_free_server(server);
1101 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1102 return ERR_PTR(error);
1103}
1104
1105#endif /* CONFIG_NFS_V4 */
1106
1107/*
1108 * Clone an NFS2, NFS3 or NFS4 server record
1109 */
1110struct nfs_server *nfs_clone_server(struct nfs_server *source,
1111 struct nfs_fh *fh,
1112 struct nfs_fattr *fattr)
1113{
1114 struct nfs_server *server;
1115 struct nfs_fattr fattr_fsinfo;
1116 int error;
1117
1118 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
David Howells6daabf12006-08-24 15:44:16 -04001119 (unsigned long long) fattr->fsid.major,
1120 (unsigned long long) fattr->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001121
1122 server = nfs_alloc_server();
1123 if (!server)
1124 return ERR_PTR(-ENOMEM);
1125
1126 /* Copy data from the source */
1127 server->nfs_client = source->nfs_client;
1128 atomic_inc(&server->nfs_client->cl_count);
1129 nfs_server_copy_userdata(server, source);
1130
1131 server->fsid = fattr->fsid;
1132
1133 error = nfs_init_server_rpcclient(server, source->client->cl_auth->au_flavor);
1134 if (error < 0)
1135 goto out_free_server;
1136 if (!IS_ERR(source->client_acl))
1137 nfs_init_server_aclclient(server);
1138
1139 /* probe the filesystem info for this server filesystem */
1140 error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo);
1141 if (error < 0)
1142 goto out_free_server;
1143
1144 dprintk("Cloned FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001145 (unsigned long long) server->fsid.major,
1146 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001147
1148 error = nfs_start_lockd(server);
1149 if (error < 0)
1150 goto out_free_server;
1151
1152 spin_lock(&nfs_client_lock);
1153 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1154 list_add_tail(&server->master_link, &nfs_volume_list);
1155 spin_unlock(&nfs_client_lock);
1156
1157 server->mount_time = jiffies;
1158
1159 dprintk("<-- nfs_clone_server() = %p\n", server);
1160 return server;
1161
1162out_free_server:
1163 nfs_free_server(server);
1164 dprintk("<-- nfs_clone_server() = error %d\n", error);
1165 return ERR_PTR(error);
1166}
David Howells6aaca562006-08-22 20:06:13 -04001167
1168#ifdef CONFIG_PROC_FS
1169static struct proc_dir_entry *proc_fs_nfs;
1170
1171static int nfs_server_list_open(struct inode *inode, struct file *file);
1172static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1173static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1174static void nfs_server_list_stop(struct seq_file *p, void *v);
1175static int nfs_server_list_show(struct seq_file *m, void *v);
1176
1177static struct seq_operations nfs_server_list_ops = {
1178 .start = nfs_server_list_start,
1179 .next = nfs_server_list_next,
1180 .stop = nfs_server_list_stop,
1181 .show = nfs_server_list_show,
1182};
1183
Arjan van de Ven00977a52007-02-12 00:55:34 -08001184static const struct file_operations nfs_server_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001185 .open = nfs_server_list_open,
1186 .read = seq_read,
1187 .llseek = seq_lseek,
1188 .release = seq_release,
1189};
1190
1191static int nfs_volume_list_open(struct inode *inode, struct file *file);
1192static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1193static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1194static void nfs_volume_list_stop(struct seq_file *p, void *v);
1195static int nfs_volume_list_show(struct seq_file *m, void *v);
1196
1197static struct seq_operations nfs_volume_list_ops = {
1198 .start = nfs_volume_list_start,
1199 .next = nfs_volume_list_next,
1200 .stop = nfs_volume_list_stop,
1201 .show = nfs_volume_list_show,
1202};
1203
Arjan van de Ven00977a52007-02-12 00:55:34 -08001204static const struct file_operations nfs_volume_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001205 .open = nfs_volume_list_open,
1206 .read = seq_read,
1207 .llseek = seq_lseek,
1208 .release = seq_release,
1209};
1210
1211/*
1212 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1213 * we're dealing
1214 */
1215static int nfs_server_list_open(struct inode *inode, struct file *file)
1216{
1217 struct seq_file *m;
1218 int ret;
1219
1220 ret = seq_open(file, &nfs_server_list_ops);
1221 if (ret < 0)
1222 return ret;
1223
1224 m = file->private_data;
1225 m->private = PDE(inode)->data;
1226
1227 return 0;
1228}
1229
1230/*
1231 * set up the iterator to start reading from the server list and return the first item
1232 */
1233static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1234{
1235 struct list_head *_p;
1236 loff_t pos = *_pos;
1237
1238 /* lock the list against modification */
1239 spin_lock(&nfs_client_lock);
1240
1241 /* allow for the header line */
1242 if (!pos)
1243 return SEQ_START_TOKEN;
1244 pos--;
1245
1246 /* find the n'th element in the list */
1247 list_for_each(_p, &nfs_client_list)
1248 if (!pos--)
1249 break;
1250
1251 return _p != &nfs_client_list ? _p : NULL;
1252}
1253
1254/*
1255 * move to next server
1256 */
1257static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1258{
1259 struct list_head *_p;
1260
1261 (*pos)++;
1262
1263 _p = v;
1264 _p = (v == SEQ_START_TOKEN) ? nfs_client_list.next : _p->next;
1265
1266 return _p != &nfs_client_list ? _p : NULL;
1267}
1268
1269/*
1270 * clean up after reading from the transports list
1271 */
1272static void nfs_server_list_stop(struct seq_file *p, void *v)
1273{
1274 spin_unlock(&nfs_client_lock);
1275}
1276
1277/*
1278 * display a header line followed by a load of call lines
1279 */
1280static int nfs_server_list_show(struct seq_file *m, void *v)
1281{
1282 struct nfs_client *clp;
1283
1284 /* display header on line 1 */
1285 if (v == SEQ_START_TOKEN) {
1286 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1287 return 0;
1288 }
1289
1290 /* display one transport per line on subsequent lines */
1291 clp = list_entry(v, struct nfs_client, cl_share_link);
1292
1293 seq_printf(m, "v%d %02x%02x%02x%02x %4hx %3d %s\n",
1294 clp->cl_nfsversion,
1295 NIPQUAD(clp->cl_addr.sin_addr),
1296 ntohs(clp->cl_addr.sin_port),
1297 atomic_read(&clp->cl_count),
1298 clp->cl_hostname);
1299
1300 return 0;
1301}
1302
1303/*
1304 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1305 */
1306static int nfs_volume_list_open(struct inode *inode, struct file *file)
1307{
1308 struct seq_file *m;
1309 int ret;
1310
1311 ret = seq_open(file, &nfs_volume_list_ops);
1312 if (ret < 0)
1313 return ret;
1314
1315 m = file->private_data;
1316 m->private = PDE(inode)->data;
1317
1318 return 0;
1319}
1320
1321/*
1322 * set up the iterator to start reading from the volume list and return the first item
1323 */
1324static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1325{
1326 struct list_head *_p;
1327 loff_t pos = *_pos;
1328
1329 /* lock the list against modification */
1330 spin_lock(&nfs_client_lock);
1331
1332 /* allow for the header line */
1333 if (!pos)
1334 return SEQ_START_TOKEN;
1335 pos--;
1336
1337 /* find the n'th element in the list */
1338 list_for_each(_p, &nfs_volume_list)
1339 if (!pos--)
1340 break;
1341
1342 return _p != &nfs_volume_list ? _p : NULL;
1343}
1344
1345/*
1346 * move to next volume
1347 */
1348static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1349{
1350 struct list_head *_p;
1351
1352 (*pos)++;
1353
1354 _p = v;
1355 _p = (v == SEQ_START_TOKEN) ? nfs_volume_list.next : _p->next;
1356
1357 return _p != &nfs_volume_list ? _p : NULL;
1358}
1359
1360/*
1361 * clean up after reading from the transports list
1362 */
1363static void nfs_volume_list_stop(struct seq_file *p, void *v)
1364{
1365 spin_unlock(&nfs_client_lock);
1366}
1367
1368/*
1369 * display a header line followed by a load of call lines
1370 */
1371static int nfs_volume_list_show(struct seq_file *m, void *v)
1372{
1373 struct nfs_server *server;
1374 struct nfs_client *clp;
1375 char dev[8], fsid[17];
1376
1377 /* display header on line 1 */
1378 if (v == SEQ_START_TOKEN) {
1379 seq_puts(m, "NV SERVER PORT DEV FSID\n");
1380 return 0;
1381 }
1382 /* display one transport per line on subsequent lines */
1383 server = list_entry(v, struct nfs_server, master_link);
1384 clp = server->nfs_client;
1385
1386 snprintf(dev, 8, "%u:%u",
1387 MAJOR(server->s_dev), MINOR(server->s_dev));
1388
1389 snprintf(fsid, 17, "%llx:%llx",
David Howells6daabf12006-08-24 15:44:16 -04001390 (unsigned long long) server->fsid.major,
1391 (unsigned long long) server->fsid.minor);
David Howells6aaca562006-08-22 20:06:13 -04001392
1393 seq_printf(m, "v%d %02x%02x%02x%02x %4hx %-7s %-17s\n",
1394 clp->cl_nfsversion,
1395 NIPQUAD(clp->cl_addr.sin_addr),
1396 ntohs(clp->cl_addr.sin_port),
1397 dev,
1398 fsid);
1399
1400 return 0;
1401}
1402
1403/*
1404 * initialise the /proc/fs/nfsfs/ directory
1405 */
1406int __init nfs_fs_proc_init(void)
1407{
1408 struct proc_dir_entry *p;
1409
1410 proc_fs_nfs = proc_mkdir("nfsfs", proc_root_fs);
1411 if (!proc_fs_nfs)
1412 goto error_0;
1413
1414 proc_fs_nfs->owner = THIS_MODULE;
1415
1416 /* a file of servers with which we're dealing */
1417 p = create_proc_entry("servers", S_IFREG|S_IRUGO, proc_fs_nfs);
1418 if (!p)
1419 goto error_1;
1420
1421 p->proc_fops = &nfs_server_list_fops;
1422 p->owner = THIS_MODULE;
1423
1424 /* a file of volumes that we have mounted */
1425 p = create_proc_entry("volumes", S_IFREG|S_IRUGO, proc_fs_nfs);
1426 if (!p)
1427 goto error_2;
1428
1429 p->proc_fops = &nfs_volume_list_fops;
1430 p->owner = THIS_MODULE;
1431 return 0;
1432
1433error_2:
1434 remove_proc_entry("servers", proc_fs_nfs);
1435error_1:
1436 remove_proc_entry("nfsfs", proc_root_fs);
1437error_0:
1438 return -ENOMEM;
1439}
1440
1441/*
1442 * clean up the /proc/fs/nfsfs/ directory
1443 */
1444void nfs_fs_proc_exit(void)
1445{
1446 remove_proc_entry("volumes", proc_fs_nfs);
1447 remove_proc_entry("servers", proc_fs_nfs);
1448 remove_proc_entry("nfsfs", proc_root_fs);
1449}
1450
1451#endif /* CONFIG_PROC_FS */