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