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