blob: 2190e6c2792e6f7ed1383f49b1e2f96c6565ef3d [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 Howells65f27f32006-11-22 14:55:48 +0000146 INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
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,
Chuck Lever43d78ef2007-02-06 18:26:11 -0500397 rpc_authflavor_t flavor,
398 int flags)
David Howells5006a762006-08-22 20:06:12 -0400399{
400 struct rpc_timeout timeparms;
David Howells5006a762006-08-22 20:06:12 -0400401 struct rpc_clnt *clnt = NULL;
Chuck Lever41877d22006-08-22 20:06:20 -0400402 struct rpc_create_args args = {
403 .protocol = proto,
404 .address = (struct sockaddr *)&clp->cl_addr,
405 .addrsize = sizeof(clp->cl_addr),
406 .timeout = &timeparms,
407 .servername = clp->cl_hostname,
408 .program = &nfs_program,
409 .version = clp->rpc_ops->version,
410 .authflavor = flavor,
Chuck Lever43d78ef2007-02-06 18:26:11 -0500411 .flags = flags,
Chuck Lever41877d22006-08-22 20:06:20 -0400412 };
David Howells5006a762006-08-22 20:06:12 -0400413
414 if (!IS_ERR(clp->cl_rpcclient))
415 return 0;
416
417 nfs_init_timeout_values(&timeparms, proto, timeo, retrans);
418 clp->retrans_timeo = timeparms.to_initval;
419 clp->retrans_count = timeparms.to_retries;
420
Chuck Lever41877d22006-08-22 20:06:20 -0400421 clnt = rpc_create(&args);
David Howells5006a762006-08-22 20:06:12 -0400422 if (IS_ERR(clnt)) {
423 dprintk("%s: cannot create RPC client. Error = %ld\n",
424 __FUNCTION__, PTR_ERR(clnt));
425 return PTR_ERR(clnt);
426 }
427
David Howells5006a762006-08-22 20:06:12 -0400428 clp->cl_rpcclient = clnt;
429 return 0;
430}
David Howells54ceac42006-08-22 20:06:13 -0400431
432/*
433 * Version 2 or 3 client destruction
434 */
435static void nfs_destroy_server(struct nfs_server *server)
436{
437 if (!IS_ERR(server->client_acl))
438 rpc_shutdown_client(server->client_acl);
439
440 if (!(server->flags & NFS_MOUNT_NONLM))
441 lockd_down(); /* release rpc.lockd */
442}
443
444/*
445 * Version 2 or 3 lockd setup
446 */
447static int nfs_start_lockd(struct nfs_server *server)
448{
449 int error = 0;
450
451 if (server->nfs_client->cl_nfsversion > 3)
452 goto out;
453 if (server->flags & NFS_MOUNT_NONLM)
454 goto out;
NeilBrown24e36662006-10-02 02:17:45 -0700455 error = lockd_up((server->flags & NFS_MOUNT_TCP) ?
456 IPPROTO_TCP : IPPROTO_UDP);
David Howells54ceac42006-08-22 20:06:13 -0400457 if (error < 0)
458 server->flags |= NFS_MOUNT_NONLM;
459 else
460 server->destroy = nfs_destroy_server;
461out:
462 return error;
463}
464
465/*
466 * Initialise an NFSv3 ACL client connection
467 */
468#ifdef CONFIG_NFS_V3_ACL
469static void nfs_init_server_aclclient(struct nfs_server *server)
470{
471 if (server->nfs_client->cl_nfsversion != 3)
472 goto out_noacl;
473 if (server->flags & NFS_MOUNT_NOACL)
474 goto out_noacl;
475
476 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
477 if (IS_ERR(server->client_acl))
478 goto out_noacl;
479
480 /* No errors! Assume that Sun nfsacls are supported */
481 server->caps |= NFS_CAP_ACLS;
482 return;
483
484out_noacl:
485 server->caps &= ~NFS_CAP_ACLS;
486}
487#else
488static inline void nfs_init_server_aclclient(struct nfs_server *server)
489{
490 server->flags &= ~NFS_MOUNT_NOACL;
491 server->caps &= ~NFS_CAP_ACLS;
492}
493#endif
494
495/*
496 * Create a general RPC client
497 */
498static int nfs_init_server_rpcclient(struct nfs_server *server, rpc_authflavor_t pseudoflavour)
499{
500 struct nfs_client *clp = server->nfs_client;
501
502 server->client = rpc_clone_client(clp->cl_rpcclient);
503 if (IS_ERR(server->client)) {
504 dprintk("%s: couldn't create rpc_client!\n", __FUNCTION__);
505 return PTR_ERR(server->client);
506 }
507
508 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
509 struct rpc_auth *auth;
510
511 auth = rpcauth_create(pseudoflavour, server->client);
512 if (IS_ERR(auth)) {
513 dprintk("%s: couldn't create credcache!\n", __FUNCTION__);
514 return PTR_ERR(auth);
515 }
516 }
517 server->client->cl_softrtry = 0;
518 if (server->flags & NFS_MOUNT_SOFT)
519 server->client->cl_softrtry = 1;
520
521 server->client->cl_intr = 0;
522 if (server->flags & NFS4_MOUNT_INTR)
523 server->client->cl_intr = 1;
524
525 return 0;
526}
527
528/*
529 * Initialise an NFS2 or NFS3 client
530 */
531static int nfs_init_client(struct nfs_client *clp, const struct nfs_mount_data *data)
532{
533 int proto = (data->flags & NFS_MOUNT_TCP) ? IPPROTO_TCP : IPPROTO_UDP;
534 int error;
535
536 if (clp->cl_cons_state == NFS_CS_READY) {
537 /* the client is already initialised */
538 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
539 return 0;
540 }
541
542 /* Check NFS protocol revision and initialize RPC op vector */
543 clp->rpc_ops = &nfs_v2_clientops;
544#ifdef CONFIG_NFS_V3
545 if (clp->cl_nfsversion == 3)
546 clp->rpc_ops = &nfs_v3_clientops;
547#endif
548 /*
549 * Create a client RPC handle for doing FSSTAT with UNIX auth only
550 * - RFC 2623, sec 2.3.2
551 */
552 error = nfs_create_rpc_client(clp, proto, data->timeo, data->retrans,
Chuck Lever43d78ef2007-02-06 18:26:11 -0500553 RPC_AUTH_UNIX, 0);
David Howells54ceac42006-08-22 20:06:13 -0400554 if (error < 0)
555 goto error;
556 nfs_mark_client_ready(clp, NFS_CS_READY);
557 return 0;
558
559error:
560 nfs_mark_client_ready(clp, error);
561 dprintk("<-- nfs_init_client() = xerror %d\n", error);
562 return error;
563}
564
565/*
566 * Create a version 2 or 3 client
567 */
568static int nfs_init_server(struct nfs_server *server, const struct nfs_mount_data *data)
569{
570 struct nfs_client *clp;
571 int error, nfsvers = 2;
572
573 dprintk("--> nfs_init_server()\n");
574
575#ifdef CONFIG_NFS_V3
576 if (data->flags & NFS_MOUNT_VER3)
577 nfsvers = 3;
578#endif
579
580 /* Allocate or find a client reference we can use */
581 clp = nfs_get_client(data->hostname, &data->addr, nfsvers);
582 if (IS_ERR(clp)) {
583 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
584 return PTR_ERR(clp);
585 }
586
587 error = nfs_init_client(clp, data);
588 if (error < 0)
589 goto error;
590
591 server->nfs_client = clp;
592
593 /* Initialise the client representation from the mount data */
594 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
595
596 if (data->rsize)
597 server->rsize = nfs_block_size(data->rsize, NULL);
598 if (data->wsize)
599 server->wsize = nfs_block_size(data->wsize, NULL);
600
601 server->acregmin = data->acregmin * HZ;
602 server->acregmax = data->acregmax * HZ;
603 server->acdirmin = data->acdirmin * HZ;
604 server->acdirmax = data->acdirmax * HZ;
605
606 /* Start lockd here, before we might error out */
607 error = nfs_start_lockd(server);
608 if (error < 0)
609 goto error;
610
611 error = nfs_init_server_rpcclient(server, data->pseudoflavor);
612 if (error < 0)
613 goto error;
614
615 server->namelen = data->namlen;
616 /* Create a client RPC handle for the NFSv3 ACL management interface */
617 nfs_init_server_aclclient(server);
618 if (clp->cl_nfsversion == 3) {
619 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
620 server->namelen = NFS3_MAXNAMLEN;
621 server->caps |= NFS_CAP_READDIRPLUS;
622 } 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 */