blob: 34c3996bd0f582438164602a64a6dbd5254e1247 [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);
146 INIT_WORK(&clp->cl_renewd, nfs4_renew_state, clp);
147 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 */
235static struct nfs_client *__nfs_find_client(const struct sockaddr_in *addr, int nfsversion)
236{
237 struct nfs_client *clp;
238
239 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
240 /* Different NFS versions cannot share the same nfs_client */
241 if (clp->cl_nfsversion != nfsversion)
242 continue;
243
244 if (memcmp(&clp->cl_addr.sin_addr, &addr->sin_addr,
245 sizeof(clp->cl_addr.sin_addr)) != 0)
246 continue;
247
248 if (clp->cl_addr.sin_port == addr->sin_port)
249 goto found;
250 }
251
252 return NULL;
253
254found:
255 atomic_inc(&clp->cl_count);
256 return clp;
257}
258
259/*
260 * Find a client by IP address and protocol version
261 * - returns NULL if no such client
262 */
263struct nfs_client *nfs_find_client(const struct sockaddr_in *addr, int nfsversion)
264{
265 struct nfs_client *clp;
266
267 spin_lock(&nfs_client_lock);
268 clp = __nfs_find_client(addr, nfsversion);
269 spin_unlock(&nfs_client_lock);
270
J. Bruce Fields2dec5142006-09-12 11:53:23 -0400271 BUG_ON(clp && clp->cl_cons_state == 0);
David Howells24c8dbb2006-08-22 20:06:10 -0400272
273 return clp;
274}
275
276/*
277 * Look up a client by IP address and protocol version
278 * - creates a new record if one doesn't yet exist
279 */
David Howells54ceac42006-08-22 20:06:13 -0400280static struct nfs_client *nfs_get_client(const char *hostname,
281 const struct sockaddr_in *addr,
282 int nfsversion)
David Howells24c8dbb2006-08-22 20:06:10 -0400283{
284 struct nfs_client *clp, *new = NULL;
285 int error;
286
287 dprintk("--> nfs_get_client(%s,"NIPQUAD_FMT":%d,%d)\n",
288 hostname ?: "", NIPQUAD(addr->sin_addr),
289 addr->sin_port, nfsversion);
290
291 /* see if the client already exists */
292 do {
293 spin_lock(&nfs_client_lock);
294
295 clp = __nfs_find_client(addr, nfsversion);
296 if (clp)
297 goto found_client;
298 if (new)
299 goto install_client;
300
301 spin_unlock(&nfs_client_lock);
302
303 new = nfs_alloc_client(hostname, addr, nfsversion);
304 } while (new);
305
306 return ERR_PTR(-ENOMEM);
307
308 /* install a new client and return with it unready */
309install_client:
310 clp = new;
311 list_add(&clp->cl_share_link, &nfs_client_list);
312 spin_unlock(&nfs_client_lock);
313 dprintk("--> nfs_get_client() = %p [new]\n", clp);
314 return clp;
315
316 /* found an existing client
317 * - make sure it's ready before returning
318 */
319found_client:
320 spin_unlock(&nfs_client_lock);
321
322 if (new)
323 nfs_free_client(new);
324
Linus Torvalds83250492006-10-08 17:28:25 -0700325 error = wait_event_interruptible(nfs_client_active_wq,
Trond Myklebust0bae89e2006-10-08 14:33:24 -0400326 clp->cl_cons_state != NFS_CS_INITING);
327 if (error < 0) {
328 nfs_put_client(clp);
329 return ERR_PTR(-ERESTARTSYS);
David Howells24c8dbb2006-08-22 20:06:10 -0400330 }
331
332 if (clp->cl_cons_state < NFS_CS_READY) {
333 error = clp->cl_cons_state;
334 nfs_put_client(clp);
335 return ERR_PTR(error);
336 }
337
David Howells54ceac42006-08-22 20:06:13 -0400338 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
339
David Howells24c8dbb2006-08-22 20:06:10 -0400340 dprintk("--> nfs_get_client() = %p [share]\n", clp);
341 return clp;
342}
343
344/*
345 * Mark a server as ready or failed
346 */
David Howells54ceac42006-08-22 20:06:13 -0400347static void nfs_mark_client_ready(struct nfs_client *clp, int state)
David Howells24c8dbb2006-08-22 20:06:10 -0400348{
349 clp->cl_cons_state = state;
350 wake_up_all(&nfs_client_active_wq);
351}
David Howells5006a762006-08-22 20:06:12 -0400352
353/*
354 * Initialise the timeout values for a connection
355 */
356static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
357 unsigned int timeo, unsigned int retrans)
358{
359 to->to_initval = timeo * HZ / 10;
360 to->to_retries = retrans;
361 if (!to->to_retries)
362 to->to_retries = 2;
363
364 switch (proto) {
365 case IPPROTO_TCP:
366 if (!to->to_initval)
367 to->to_initval = 60 * HZ;
368 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
369 to->to_initval = NFS_MAX_TCP_TIMEOUT;
370 to->to_increment = to->to_initval;
371 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
372 to->to_exponential = 0;
373 break;
374 case IPPROTO_UDP:
375 default:
376 if (!to->to_initval)
377 to->to_initval = 11 * HZ / 10;
378 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
379 to->to_initval = NFS_MAX_UDP_TIMEOUT;
380 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
381 to->to_exponential = 1;
382 break;
383 }
384}
385
386/*
387 * Create an RPC client handle
388 */
David Howells54ceac42006-08-22 20:06:13 -0400389static int nfs_create_rpc_client(struct nfs_client *clp, int proto,
390 unsigned int timeo,
391 unsigned int retrans,
392 rpc_authflavor_t flavor)
David Howells5006a762006-08-22 20:06:12 -0400393{
394 struct rpc_timeout timeparms;
David Howells5006a762006-08-22 20:06:12 -0400395 struct rpc_clnt *clnt = NULL;
Chuck Lever41877d22006-08-22 20:06:20 -0400396 struct rpc_create_args args = {
397 .protocol = proto,
398 .address = (struct sockaddr *)&clp->cl_addr,
399 .addrsize = sizeof(clp->cl_addr),
400 .timeout = &timeparms,
401 .servername = clp->cl_hostname,
402 .program = &nfs_program,
403 .version = clp->rpc_ops->version,
404 .authflavor = flavor,
405 };
David Howells5006a762006-08-22 20:06:12 -0400406
407 if (!IS_ERR(clp->cl_rpcclient))
408 return 0;
409
410 nfs_init_timeout_values(&timeparms, proto, timeo, retrans);
411 clp->retrans_timeo = timeparms.to_initval;
412 clp->retrans_count = timeparms.to_retries;
413
Chuck Lever41877d22006-08-22 20:06:20 -0400414 clnt = rpc_create(&args);
David Howells5006a762006-08-22 20:06:12 -0400415 if (IS_ERR(clnt)) {
416 dprintk("%s: cannot create RPC client. Error = %ld\n",
417 __FUNCTION__, PTR_ERR(clnt));
418 return PTR_ERR(clnt);
419 }
420
David Howells5006a762006-08-22 20:06:12 -0400421 clp->cl_rpcclient = clnt;
422 return 0;
423}
David Howells54ceac42006-08-22 20:06:13 -0400424
425/*
426 * Version 2 or 3 client destruction
427 */
428static void nfs_destroy_server(struct nfs_server *server)
429{
430 if (!IS_ERR(server->client_acl))
431 rpc_shutdown_client(server->client_acl);
432
433 if (!(server->flags & NFS_MOUNT_NONLM))
434 lockd_down(); /* release rpc.lockd */
435}
436
437/*
438 * Version 2 or 3 lockd setup
439 */
440static int nfs_start_lockd(struct nfs_server *server)
441{
442 int error = 0;
443
444 if (server->nfs_client->cl_nfsversion > 3)
445 goto out;
446 if (server->flags & NFS_MOUNT_NONLM)
447 goto out;
NeilBrown24e36662006-10-02 02:17:45 -0700448 error = lockd_up((server->flags & NFS_MOUNT_TCP) ?
449 IPPROTO_TCP : IPPROTO_UDP);
David Howells54ceac42006-08-22 20:06:13 -0400450 if (error < 0)
451 server->flags |= NFS_MOUNT_NONLM;
452 else
453 server->destroy = nfs_destroy_server;
454out:
455 return error;
456}
457
458/*
459 * Initialise an NFSv3 ACL client connection
460 */
461#ifdef CONFIG_NFS_V3_ACL
462static void nfs_init_server_aclclient(struct nfs_server *server)
463{
464 if (server->nfs_client->cl_nfsversion != 3)
465 goto out_noacl;
466 if (server->flags & NFS_MOUNT_NOACL)
467 goto out_noacl;
468
469 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
470 if (IS_ERR(server->client_acl))
471 goto out_noacl;
472
473 /* No errors! Assume that Sun nfsacls are supported */
474 server->caps |= NFS_CAP_ACLS;
475 return;
476
477out_noacl:
478 server->caps &= ~NFS_CAP_ACLS;
479}
480#else
481static inline void nfs_init_server_aclclient(struct nfs_server *server)
482{
483 server->flags &= ~NFS_MOUNT_NOACL;
484 server->caps &= ~NFS_CAP_ACLS;
485}
486#endif
487
488/*
489 * Create a general RPC client
490 */
491static int nfs_init_server_rpcclient(struct nfs_server *server, rpc_authflavor_t pseudoflavour)
492{
493 struct nfs_client *clp = server->nfs_client;
494
495 server->client = rpc_clone_client(clp->cl_rpcclient);
496 if (IS_ERR(server->client)) {
497 dprintk("%s: couldn't create rpc_client!\n", __FUNCTION__);
498 return PTR_ERR(server->client);
499 }
500
501 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
502 struct rpc_auth *auth;
503
504 auth = rpcauth_create(pseudoflavour, server->client);
505 if (IS_ERR(auth)) {
506 dprintk("%s: couldn't create credcache!\n", __FUNCTION__);
507 return PTR_ERR(auth);
508 }
509 }
510 server->client->cl_softrtry = 0;
511 if (server->flags & NFS_MOUNT_SOFT)
512 server->client->cl_softrtry = 1;
513
514 server->client->cl_intr = 0;
515 if (server->flags & NFS4_MOUNT_INTR)
516 server->client->cl_intr = 1;
517
518 return 0;
519}
520
521/*
522 * Initialise an NFS2 or NFS3 client
523 */
524static int nfs_init_client(struct nfs_client *clp, const struct nfs_mount_data *data)
525{
526 int proto = (data->flags & NFS_MOUNT_TCP) ? IPPROTO_TCP : IPPROTO_UDP;
527 int error;
528
529 if (clp->cl_cons_state == NFS_CS_READY) {
530 /* the client is already initialised */
531 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
532 return 0;
533 }
534
535 /* Check NFS protocol revision and initialize RPC op vector */
536 clp->rpc_ops = &nfs_v2_clientops;
537#ifdef CONFIG_NFS_V3
538 if (clp->cl_nfsversion == 3)
539 clp->rpc_ops = &nfs_v3_clientops;
540#endif
541 /*
542 * Create a client RPC handle for doing FSSTAT with UNIX auth only
543 * - RFC 2623, sec 2.3.2
544 */
545 error = nfs_create_rpc_client(clp, proto, data->timeo, data->retrans,
546 RPC_AUTH_UNIX);
547 if (error < 0)
548 goto error;
549 nfs_mark_client_ready(clp, NFS_CS_READY);
550 return 0;
551
552error:
553 nfs_mark_client_ready(clp, error);
554 dprintk("<-- nfs_init_client() = xerror %d\n", error);
555 return error;
556}
557
558/*
559 * Create a version 2 or 3 client
560 */
561static int nfs_init_server(struct nfs_server *server, const struct nfs_mount_data *data)
562{
563 struct nfs_client *clp;
564 int error, nfsvers = 2;
565
566 dprintk("--> nfs_init_server()\n");
567
568#ifdef CONFIG_NFS_V3
569 if (data->flags & NFS_MOUNT_VER3)
570 nfsvers = 3;
571#endif
572
573 /* Allocate or find a client reference we can use */
574 clp = nfs_get_client(data->hostname, &data->addr, nfsvers);
575 if (IS_ERR(clp)) {
576 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
577 return PTR_ERR(clp);
578 }
579
580 error = nfs_init_client(clp, data);
581 if (error < 0)
582 goto error;
583
584 server->nfs_client = clp;
585
586 /* Initialise the client representation from the mount data */
587 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
588
589 if (data->rsize)
590 server->rsize = nfs_block_size(data->rsize, NULL);
591 if (data->wsize)
592 server->wsize = nfs_block_size(data->wsize, NULL);
593
594 server->acregmin = data->acregmin * HZ;
595 server->acregmax = data->acregmax * HZ;
596 server->acdirmin = data->acdirmin * HZ;
597 server->acdirmax = data->acdirmax * HZ;
598
599 /* Start lockd here, before we might error out */
600 error = nfs_start_lockd(server);
601 if (error < 0)
602 goto error;
603
604 error = nfs_init_server_rpcclient(server, data->pseudoflavor);
605 if (error < 0)
606 goto error;
607
608 server->namelen = data->namlen;
609 /* Create a client RPC handle for the NFSv3 ACL management interface */
610 nfs_init_server_aclclient(server);
611 if (clp->cl_nfsversion == 3) {
612 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
613 server->namelen = NFS3_MAXNAMLEN;
614 server->caps |= NFS_CAP_READDIRPLUS;
615 } else {
616 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
617 server->namelen = NFS2_MAXNAMLEN;
618 }
619
620 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
621 return 0;
622
623error:
624 server->nfs_client = NULL;
625 nfs_put_client(clp);
626 dprintk("<-- nfs_init_server() = xerror %d\n", error);
627 return error;
628}
629
630/*
631 * Load up the server record from information gained in an fsinfo record
632 */
633static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
634{
635 unsigned long max_rpc_payload;
636
637 /* Work out a lot of parameters */
638 if (server->rsize == 0)
639 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
640 if (server->wsize == 0)
641 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
642
643 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
644 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
645 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
646 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
647
648 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
649 if (server->rsize > max_rpc_payload)
650 server->rsize = max_rpc_payload;
651 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
652 server->rsize = NFS_MAX_FILE_IO_SIZE;
653 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
654 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
655
656 if (server->wsize > max_rpc_payload)
657 server->wsize = max_rpc_payload;
658 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
659 server->wsize = NFS_MAX_FILE_IO_SIZE;
660 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
661 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
662
663 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
664 if (server->dtsize > PAGE_CACHE_SIZE)
665 server->dtsize = PAGE_CACHE_SIZE;
666 if (server->dtsize > server->rsize)
667 server->dtsize = server->rsize;
668
669 if (server->flags & NFS_MOUNT_NOAC) {
670 server->acregmin = server->acregmax = 0;
671 server->acdirmin = server->acdirmax = 0;
672 }
673
674 server->maxfilesize = fsinfo->maxfilesize;
675
676 /* We're airborne Set socket buffersize */
677 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
678}
679
680/*
681 * Probe filesystem information, including the FSID on v2/v3
682 */
683static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
684{
685 struct nfs_fsinfo fsinfo;
686 struct nfs_client *clp = server->nfs_client;
687 int error;
688
689 dprintk("--> nfs_probe_fsinfo()\n");
690
691 if (clp->rpc_ops->set_capabilities != NULL) {
692 error = clp->rpc_ops->set_capabilities(server, mntfh);
693 if (error < 0)
694 goto out_error;
695 }
696
697 fsinfo.fattr = fattr;
698 nfs_fattr_init(fattr);
699 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
700 if (error < 0)
701 goto out_error;
702
703 nfs_server_set_fsinfo(server, &fsinfo);
704
705 /* Get some general file system info */
706 if (server->namelen == 0) {
707 struct nfs_pathconf pathinfo;
708
709 pathinfo.fattr = fattr;
710 nfs_fattr_init(fattr);
711
712 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
713 server->namelen = pathinfo.max_namelen;
714 }
715
716 dprintk("<-- nfs_probe_fsinfo() = 0\n");
717 return 0;
718
719out_error:
720 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
721 return error;
722}
723
724/*
725 * Copy useful information when duplicating a server record
726 */
727static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
728{
729 target->flags = source->flags;
730 target->acregmin = source->acregmin;
731 target->acregmax = source->acregmax;
732 target->acdirmin = source->acdirmin;
733 target->acdirmax = source->acdirmax;
734 target->caps = source->caps;
735}
736
737/*
738 * Allocate and initialise a server record
739 */
740static struct nfs_server *nfs_alloc_server(void)
741{
742 struct nfs_server *server;
743
744 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
745 if (!server)
746 return NULL;
747
748 server->client = server->client_acl = ERR_PTR(-EINVAL);
749
750 /* Zero out the NFS state stuff */
751 INIT_LIST_HEAD(&server->client_link);
752 INIT_LIST_HEAD(&server->master_link);
753
754 server->io_stats = nfs_alloc_iostats();
755 if (!server->io_stats) {
756 kfree(server);
757 return NULL;
758 }
759
760 return server;
761}
762
763/*
764 * Free up a server record
765 */
766void nfs_free_server(struct nfs_server *server)
767{
768 dprintk("--> nfs_free_server()\n");
769
770 spin_lock(&nfs_client_lock);
771 list_del(&server->client_link);
772 list_del(&server->master_link);
773 spin_unlock(&nfs_client_lock);
774
775 if (server->destroy != NULL)
776 server->destroy(server);
777 if (!IS_ERR(server->client))
778 rpc_shutdown_client(server->client);
779
780 nfs_put_client(server->nfs_client);
781
782 nfs_free_iostats(server->io_stats);
783 kfree(server);
784 nfs_release_automount_timer();
785 dprintk("<-- nfs_free_server()\n");
786}
787
788/*
789 * Create a version 2 or 3 volume record
790 * - keyed on server and FSID
791 */
792struct nfs_server *nfs_create_server(const struct nfs_mount_data *data,
793 struct nfs_fh *mntfh)
794{
795 struct nfs_server *server;
796 struct nfs_fattr fattr;
797 int error;
798
799 server = nfs_alloc_server();
800 if (!server)
801 return ERR_PTR(-ENOMEM);
802
803 /* Get a client representation */
804 error = nfs_init_server(server, data);
805 if (error < 0)
806 goto error;
807
808 BUG_ON(!server->nfs_client);
809 BUG_ON(!server->nfs_client->rpc_ops);
810 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
811
812 /* Probe the root fh to retrieve its FSID */
813 error = nfs_probe_fsinfo(server, mntfh, &fattr);
814 if (error < 0)
815 goto error;
816 if (!(fattr.valid & NFS_ATTR_FATTR)) {
817 error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
818 if (error < 0) {
819 dprintk("nfs_create_server: getattr error = %d\n", -error);
820 goto error;
821 }
822 }
823 memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
824
David Howells6daabf12006-08-24 15:44:16 -0400825 dprintk("Server FSID: %llx:%llx\n",
826 (unsigned long long) server->fsid.major,
827 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -0400828
829 BUG_ON(!server->nfs_client);
830 BUG_ON(!server->nfs_client->rpc_ops);
831 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
832
833 spin_lock(&nfs_client_lock);
834 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
835 list_add_tail(&server->master_link, &nfs_volume_list);
836 spin_unlock(&nfs_client_lock);
837
838 server->mount_time = jiffies;
839 return server;
840
841error:
842 nfs_free_server(server);
843 return ERR_PTR(error);
844}
845
846#ifdef CONFIG_NFS_V4
847/*
848 * Initialise an NFS4 client record
849 */
850static int nfs4_init_client(struct nfs_client *clp,
851 int proto, int timeo, int retrans,
852 rpc_authflavor_t authflavour)
853{
854 int error;
855
856 if (clp->cl_cons_state == NFS_CS_READY) {
857 /* the client is initialised already */
858 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
859 return 0;
860 }
861
862 /* Check NFS protocol revision and initialize RPC op vector */
863 clp->rpc_ops = &nfs_v4_clientops;
864
865 error = nfs_create_rpc_client(clp, proto, timeo, retrans, authflavour);
866 if (error < 0)
867 goto error;
868
869 error = nfs_idmap_new(clp);
870 if (error < 0) {
871 dprintk("%s: failed to create idmapper. Error = %d\n",
872 __FUNCTION__, error);
David Howells54ceac42006-08-22 20:06:13 -0400873 goto error;
874 }
Trond Myklebust9c5bf382006-08-22 20:06:14 -0400875 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
David Howells54ceac42006-08-22 20:06:13 -0400876
877 nfs_mark_client_ready(clp, NFS_CS_READY);
878 return 0;
879
880error:
881 nfs_mark_client_ready(clp, error);
882 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
883 return error;
884}
885
886/*
887 * Set up an NFS4 client
888 */
889static int nfs4_set_client(struct nfs_server *server,
890 const char *hostname, const struct sockaddr_in *addr,
891 rpc_authflavor_t authflavour,
892 int proto, int timeo, int retrans)
893{
894 struct nfs_client *clp;
895 int error;
896
897 dprintk("--> nfs4_set_client()\n");
898
899 /* Allocate or find a client reference we can use */
900 clp = nfs_get_client(hostname, addr, 4);
901 if (IS_ERR(clp)) {
902 error = PTR_ERR(clp);
903 goto error;
904 }
905 error = nfs4_init_client(clp, proto, timeo, retrans, authflavour);
906 if (error < 0)
907 goto error_put;
908
909 server->nfs_client = clp;
910 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
911 return 0;
912
913error_put:
914 nfs_put_client(clp);
915error:
916 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
917 return error;
918}
919
920/*
921 * Create a version 4 volume record
922 */
923static int nfs4_init_server(struct nfs_server *server,
924 const struct nfs4_mount_data *data, rpc_authflavor_t authflavour)
925{
926 int error;
927
928 dprintk("--> nfs4_init_server()\n");
929
930 /* Initialise the client representation from the mount data */
931 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
932 server->caps |= NFS_CAP_ATOMIC_OPEN;
933
934 if (data->rsize)
935 server->rsize = nfs_block_size(data->rsize, NULL);
936 if (data->wsize)
937 server->wsize = nfs_block_size(data->wsize, NULL);
938
939 server->acregmin = data->acregmin * HZ;
940 server->acregmax = data->acregmax * HZ;
941 server->acdirmin = data->acdirmin * HZ;
942 server->acdirmax = data->acdirmax * HZ;
943
944 error = nfs_init_server_rpcclient(server, authflavour);
945
946 /* Done */
947 dprintk("<-- nfs4_init_server() = %d\n", error);
948 return error;
949}
950
951/*
952 * Create a version 4 volume record
953 * - keyed on server and FSID
954 */
955struct nfs_server *nfs4_create_server(const struct nfs4_mount_data *data,
956 const char *hostname,
957 const struct sockaddr_in *addr,
958 const char *mntpath,
959 const char *ip_addr,
960 rpc_authflavor_t authflavour,
961 struct nfs_fh *mntfh)
962{
963 struct nfs_fattr fattr;
964 struct nfs_server *server;
965 int error;
966
967 dprintk("--> nfs4_create_server()\n");
968
969 server = nfs_alloc_server();
970 if (!server)
971 return ERR_PTR(-ENOMEM);
972
973 /* Get a client record */
974 error = nfs4_set_client(server, hostname, addr, authflavour,
975 data->proto, data->timeo, data->retrans);
976 if (error < 0)
977 goto error;
978
979 /* set up the general RPC client */
980 error = nfs4_init_server(server, data, authflavour);
981 if (error < 0)
982 goto error;
983
984 BUG_ON(!server->nfs_client);
985 BUG_ON(!server->nfs_client->rpc_ops);
986 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
987
988 /* Probe the root fh to retrieve its FSID */
989 error = nfs4_path_walk(server, mntfh, mntpath);
990 if (error < 0)
991 goto error;
992
David Howells6daabf12006-08-24 15:44:16 -0400993 dprintk("Server FSID: %llx:%llx\n",
994 (unsigned long long) server->fsid.major,
995 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -0400996 dprintk("Mount FH: %d\n", mntfh->size);
997
998 error = nfs_probe_fsinfo(server, mntfh, &fattr);
999 if (error < 0)
1000 goto error;
1001
1002 BUG_ON(!server->nfs_client);
1003 BUG_ON(!server->nfs_client->rpc_ops);
1004 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1005
1006 spin_lock(&nfs_client_lock);
1007 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1008 list_add_tail(&server->master_link, &nfs_volume_list);
1009 spin_unlock(&nfs_client_lock);
1010
1011 server->mount_time = jiffies;
1012 dprintk("<-- nfs4_create_server() = %p\n", server);
1013 return server;
1014
1015error:
1016 nfs_free_server(server);
1017 dprintk("<-- nfs4_create_server() = error %d\n", error);
1018 return ERR_PTR(error);
1019}
1020
1021/*
1022 * Create an NFS4 referral server record
1023 */
1024struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
1025 struct nfs_fh *fh)
1026{
1027 struct nfs_client *parent_client;
1028 struct nfs_server *server, *parent_server;
1029 struct nfs_fattr fattr;
1030 int error;
1031
1032 dprintk("--> nfs4_create_referral_server()\n");
1033
1034 server = nfs_alloc_server();
1035 if (!server)
1036 return ERR_PTR(-ENOMEM);
1037
1038 parent_server = NFS_SB(data->sb);
1039 parent_client = parent_server->nfs_client;
1040
1041 /* Get a client representation.
1042 * Note: NFSv4 always uses TCP, */
1043 error = nfs4_set_client(server, data->hostname, data->addr,
1044 data->authflavor,
1045 parent_server->client->cl_xprt->prot,
1046 parent_client->retrans_timeo,
1047 parent_client->retrans_count);
andros@citi.umich.edu297de4f2006-08-29 12:19:41 -04001048 if (error < 0)
1049 goto error;
David Howells54ceac42006-08-22 20:06:13 -04001050
1051 /* Initialise the client representation from the parent server */
1052 nfs_server_copy_userdata(server, parent_server);
1053 server->caps |= NFS_CAP_ATOMIC_OPEN;
1054
1055 error = nfs_init_server_rpcclient(server, data->authflavor);
1056 if (error < 0)
1057 goto error;
1058
1059 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 /* probe the filesystem info for this server filesystem */
1064 error = nfs_probe_fsinfo(server, fh, &fattr);
1065 if (error < 0)
1066 goto error;
1067
1068 dprintk("Referral FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001069 (unsigned long long) server->fsid.major,
1070 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001071
1072 spin_lock(&nfs_client_lock);
1073 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1074 list_add_tail(&server->master_link, &nfs_volume_list);
1075 spin_unlock(&nfs_client_lock);
1076
1077 server->mount_time = jiffies;
1078
1079 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1080 return server;
1081
1082error:
1083 nfs_free_server(server);
1084 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1085 return ERR_PTR(error);
1086}
1087
1088#endif /* CONFIG_NFS_V4 */
1089
1090/*
1091 * Clone an NFS2, NFS3 or NFS4 server record
1092 */
1093struct nfs_server *nfs_clone_server(struct nfs_server *source,
1094 struct nfs_fh *fh,
1095 struct nfs_fattr *fattr)
1096{
1097 struct nfs_server *server;
1098 struct nfs_fattr fattr_fsinfo;
1099 int error;
1100
1101 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
David Howells6daabf12006-08-24 15:44:16 -04001102 (unsigned long long) fattr->fsid.major,
1103 (unsigned long long) fattr->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001104
1105 server = nfs_alloc_server();
1106 if (!server)
1107 return ERR_PTR(-ENOMEM);
1108
1109 /* Copy data from the source */
1110 server->nfs_client = source->nfs_client;
1111 atomic_inc(&server->nfs_client->cl_count);
1112 nfs_server_copy_userdata(server, source);
1113
1114 server->fsid = fattr->fsid;
1115
1116 error = nfs_init_server_rpcclient(server, source->client->cl_auth->au_flavor);
1117 if (error < 0)
1118 goto out_free_server;
1119 if (!IS_ERR(source->client_acl))
1120 nfs_init_server_aclclient(server);
1121
1122 /* probe the filesystem info for this server filesystem */
1123 error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo);
1124 if (error < 0)
1125 goto out_free_server;
1126
1127 dprintk("Cloned FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001128 (unsigned long long) server->fsid.major,
1129 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001130
1131 error = nfs_start_lockd(server);
1132 if (error < 0)
1133 goto out_free_server;
1134
1135 spin_lock(&nfs_client_lock);
1136 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1137 list_add_tail(&server->master_link, &nfs_volume_list);
1138 spin_unlock(&nfs_client_lock);
1139
1140 server->mount_time = jiffies;
1141
1142 dprintk("<-- nfs_clone_server() = %p\n", server);
1143 return server;
1144
1145out_free_server:
1146 nfs_free_server(server);
1147 dprintk("<-- nfs_clone_server() = error %d\n", error);
1148 return ERR_PTR(error);
1149}
David Howells6aaca562006-08-22 20:06:13 -04001150
1151#ifdef CONFIG_PROC_FS
1152static struct proc_dir_entry *proc_fs_nfs;
1153
1154static int nfs_server_list_open(struct inode *inode, struct file *file);
1155static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1156static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1157static void nfs_server_list_stop(struct seq_file *p, void *v);
1158static int nfs_server_list_show(struct seq_file *m, void *v);
1159
1160static struct seq_operations nfs_server_list_ops = {
1161 .start = nfs_server_list_start,
1162 .next = nfs_server_list_next,
1163 .stop = nfs_server_list_stop,
1164 .show = nfs_server_list_show,
1165};
1166
1167static struct file_operations nfs_server_list_fops = {
1168 .open = nfs_server_list_open,
1169 .read = seq_read,
1170 .llseek = seq_lseek,
1171 .release = seq_release,
1172};
1173
1174static int nfs_volume_list_open(struct inode *inode, struct file *file);
1175static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1176static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1177static void nfs_volume_list_stop(struct seq_file *p, void *v);
1178static int nfs_volume_list_show(struct seq_file *m, void *v);
1179
1180static struct seq_operations nfs_volume_list_ops = {
1181 .start = nfs_volume_list_start,
1182 .next = nfs_volume_list_next,
1183 .stop = nfs_volume_list_stop,
1184 .show = nfs_volume_list_show,
1185};
1186
1187static struct file_operations nfs_volume_list_fops = {
1188 .open = nfs_volume_list_open,
1189 .read = seq_read,
1190 .llseek = seq_lseek,
1191 .release = seq_release,
1192};
1193
1194/*
1195 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1196 * we're dealing
1197 */
1198static int nfs_server_list_open(struct inode *inode, struct file *file)
1199{
1200 struct seq_file *m;
1201 int ret;
1202
1203 ret = seq_open(file, &nfs_server_list_ops);
1204 if (ret < 0)
1205 return ret;
1206
1207 m = file->private_data;
1208 m->private = PDE(inode)->data;
1209
1210 return 0;
1211}
1212
1213/*
1214 * set up the iterator to start reading from the server list and return the first item
1215 */
1216static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1217{
1218 struct list_head *_p;
1219 loff_t pos = *_pos;
1220
1221 /* lock the list against modification */
1222 spin_lock(&nfs_client_lock);
1223
1224 /* allow for the header line */
1225 if (!pos)
1226 return SEQ_START_TOKEN;
1227 pos--;
1228
1229 /* find the n'th element in the list */
1230 list_for_each(_p, &nfs_client_list)
1231 if (!pos--)
1232 break;
1233
1234 return _p != &nfs_client_list ? _p : NULL;
1235}
1236
1237/*
1238 * move to next server
1239 */
1240static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1241{
1242 struct list_head *_p;
1243
1244 (*pos)++;
1245
1246 _p = v;
1247 _p = (v == SEQ_START_TOKEN) ? nfs_client_list.next : _p->next;
1248
1249 return _p != &nfs_client_list ? _p : NULL;
1250}
1251
1252/*
1253 * clean up after reading from the transports list
1254 */
1255static void nfs_server_list_stop(struct seq_file *p, void *v)
1256{
1257 spin_unlock(&nfs_client_lock);
1258}
1259
1260/*
1261 * display a header line followed by a load of call lines
1262 */
1263static int nfs_server_list_show(struct seq_file *m, void *v)
1264{
1265 struct nfs_client *clp;
1266
1267 /* display header on line 1 */
1268 if (v == SEQ_START_TOKEN) {
1269 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1270 return 0;
1271 }
1272
1273 /* display one transport per line on subsequent lines */
1274 clp = list_entry(v, struct nfs_client, cl_share_link);
1275
1276 seq_printf(m, "v%d %02x%02x%02x%02x %4hx %3d %s\n",
1277 clp->cl_nfsversion,
1278 NIPQUAD(clp->cl_addr.sin_addr),
1279 ntohs(clp->cl_addr.sin_port),
1280 atomic_read(&clp->cl_count),
1281 clp->cl_hostname);
1282
1283 return 0;
1284}
1285
1286/*
1287 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1288 */
1289static int nfs_volume_list_open(struct inode *inode, struct file *file)
1290{
1291 struct seq_file *m;
1292 int ret;
1293
1294 ret = seq_open(file, &nfs_volume_list_ops);
1295 if (ret < 0)
1296 return ret;
1297
1298 m = file->private_data;
1299 m->private = PDE(inode)->data;
1300
1301 return 0;
1302}
1303
1304/*
1305 * set up the iterator to start reading from the volume list and return the first item
1306 */
1307static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1308{
1309 struct list_head *_p;
1310 loff_t pos = *_pos;
1311
1312 /* lock the list against modification */
1313 spin_lock(&nfs_client_lock);
1314
1315 /* allow for the header line */
1316 if (!pos)
1317 return SEQ_START_TOKEN;
1318 pos--;
1319
1320 /* find the n'th element in the list */
1321 list_for_each(_p, &nfs_volume_list)
1322 if (!pos--)
1323 break;
1324
1325 return _p != &nfs_volume_list ? _p : NULL;
1326}
1327
1328/*
1329 * move to next volume
1330 */
1331static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1332{
1333 struct list_head *_p;
1334
1335 (*pos)++;
1336
1337 _p = v;
1338 _p = (v == SEQ_START_TOKEN) ? nfs_volume_list.next : _p->next;
1339
1340 return _p != &nfs_volume_list ? _p : NULL;
1341}
1342
1343/*
1344 * clean up after reading from the transports list
1345 */
1346static void nfs_volume_list_stop(struct seq_file *p, void *v)
1347{
1348 spin_unlock(&nfs_client_lock);
1349}
1350
1351/*
1352 * display a header line followed by a load of call lines
1353 */
1354static int nfs_volume_list_show(struct seq_file *m, void *v)
1355{
1356 struct nfs_server *server;
1357 struct nfs_client *clp;
1358 char dev[8], fsid[17];
1359
1360 /* display header on line 1 */
1361 if (v == SEQ_START_TOKEN) {
1362 seq_puts(m, "NV SERVER PORT DEV FSID\n");
1363 return 0;
1364 }
1365 /* display one transport per line on subsequent lines */
1366 server = list_entry(v, struct nfs_server, master_link);
1367 clp = server->nfs_client;
1368
1369 snprintf(dev, 8, "%u:%u",
1370 MAJOR(server->s_dev), MINOR(server->s_dev));
1371
1372 snprintf(fsid, 17, "%llx:%llx",
David Howells6daabf12006-08-24 15:44:16 -04001373 (unsigned long long) server->fsid.major,
1374 (unsigned long long) server->fsid.minor);
David Howells6aaca562006-08-22 20:06:13 -04001375
1376 seq_printf(m, "v%d %02x%02x%02x%02x %4hx %-7s %-17s\n",
1377 clp->cl_nfsversion,
1378 NIPQUAD(clp->cl_addr.sin_addr),
1379 ntohs(clp->cl_addr.sin_port),
1380 dev,
1381 fsid);
1382
1383 return 0;
1384}
1385
1386/*
1387 * initialise the /proc/fs/nfsfs/ directory
1388 */
1389int __init nfs_fs_proc_init(void)
1390{
1391 struct proc_dir_entry *p;
1392
1393 proc_fs_nfs = proc_mkdir("nfsfs", proc_root_fs);
1394 if (!proc_fs_nfs)
1395 goto error_0;
1396
1397 proc_fs_nfs->owner = THIS_MODULE;
1398
1399 /* a file of servers with which we're dealing */
1400 p = create_proc_entry("servers", S_IFREG|S_IRUGO, proc_fs_nfs);
1401 if (!p)
1402 goto error_1;
1403
1404 p->proc_fops = &nfs_server_list_fops;
1405 p->owner = THIS_MODULE;
1406
1407 /* a file of volumes that we have mounted */
1408 p = create_proc_entry("volumes", S_IFREG|S_IRUGO, proc_fs_nfs);
1409 if (!p)
1410 goto error_2;
1411
1412 p->proc_fops = &nfs_volume_list_fops;
1413 p->owner = THIS_MODULE;
1414 return 0;
1415
1416error_2:
1417 remove_proc_entry("servers", proc_fs_nfs);
1418error_1:
1419 remove_proc_entry("nfsfs", proc_root_fs);
1420error_0:
1421 return -ENOMEM;
1422}
1423
1424/*
1425 * clean up the /proc/fs/nfsfs/ directory
1426 */
1427void nfs_fs_proc_exit(void)
1428{
1429 remove_proc_entry("volumes", proc_fs_nfs);
1430 remove_proc_entry("servers", proc_fs_nfs);
1431 remove_proc_entry("nfsfs", proc_root_fs);
1432}
1433
1434#endif /* CONFIG_PROC_FS */