blob: 310fa2f4cbb837132a57ffea845ae66d31669de3 [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>
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -040026#include <linux/sunrpc/xprtsock.h>
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -040027#include <linux/sunrpc/xprtrdma.h>
David Howells24c8dbb2006-08-22 20:06:10 -040028#include <linux/nfs_fs.h>
29#include <linux/nfs_mount.h>
30#include <linux/nfs4_mount.h>
31#include <linux/lockd/bind.h>
David Howells24c8dbb2006-08-22 20:06:10 -040032#include <linux/seq_file.h>
33#include <linux/mount.h>
34#include <linux/nfs_idmap.h>
35#include <linux/vfs.h>
36#include <linux/inet.h>
37#include <linux/nfs_xdr.h>
38
39#include <asm/system.h>
40
41#include "nfs4_fs.h"
42#include "callback.h"
43#include "delegation.h"
44#include "iostat.h"
45#include "internal.h"
46
47#define NFSDBG_FACILITY NFSDBG_CLIENT
48
49static DEFINE_SPINLOCK(nfs_client_lock);
50static LIST_HEAD(nfs_client_list);
David Howells54ceac42006-08-22 20:06:13 -040051static LIST_HEAD(nfs_volume_list);
David Howells24c8dbb2006-08-22 20:06:10 -040052static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
53
54/*
David Howells5006a762006-08-22 20:06:12 -040055 * RPC cruft for NFS
56 */
57static struct rpc_version *nfs_version[5] = {
58 [2] = &nfs_version2,
59#ifdef CONFIG_NFS_V3
60 [3] = &nfs_version3,
61#endif
62#ifdef CONFIG_NFS_V4
63 [4] = &nfs_version4,
64#endif
65};
66
67struct rpc_program nfs_program = {
68 .name = "nfs",
69 .number = NFS_PROGRAM,
70 .nrvers = ARRAY_SIZE(nfs_version),
71 .version = nfs_version,
72 .stats = &nfs_rpcstat,
73 .pipe_dir_name = "/nfs",
74};
75
76struct rpc_stat nfs_rpcstat = {
77 .program = &nfs_program
78};
79
80
81#ifdef CONFIG_NFS_V3_ACL
82static struct rpc_stat nfsacl_rpcstat = { &nfsacl_program };
83static struct rpc_version * nfsacl_version[] = {
84 [3] = &nfsacl_version3,
85};
86
87struct rpc_program nfsacl_program = {
88 .name = "nfsacl",
89 .number = NFS_ACL_PROGRAM,
90 .nrvers = ARRAY_SIZE(nfsacl_version),
91 .version = nfsacl_version,
92 .stats = &nfsacl_rpcstat,
93};
94#endif /* CONFIG_NFS_V3_ACL */
95
96/*
David Howells24c8dbb2006-08-22 20:06:10 -040097 * Allocate a shared client record
98 *
99 * Since these are allocated/deallocated very rarely, we don't
100 * bother putting them in a slab cache...
101 */
102static struct nfs_client *nfs_alloc_client(const char *hostname,
103 const struct sockaddr_in *addr,
104 int nfsversion)
105{
106 struct nfs_client *clp;
David Howells24c8dbb2006-08-22 20:06:10 -0400107
108 if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
109 goto error_0;
110
David Howells24c8dbb2006-08-22 20:06:10 -0400111 if (nfsversion == 4) {
112 if (nfs_callback_up() < 0)
113 goto error_2;
114 __set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
115 }
116
117 atomic_set(&clp->cl_count, 1);
118 clp->cl_cons_state = NFS_CS_INITING;
119
120 clp->cl_nfsversion = nfsversion;
121 memcpy(&clp->cl_addr, addr, sizeof(clp->cl_addr));
122
123 if (hostname) {
124 clp->cl_hostname = kstrdup(hostname, GFP_KERNEL);
125 if (!clp->cl_hostname)
126 goto error_3;
127 }
128
129 INIT_LIST_HEAD(&clp->cl_superblocks);
130 clp->cl_rpcclient = ERR_PTR(-EINVAL);
131
132#ifdef CONFIG_NFS_V4
133 init_rwsem(&clp->cl_sem);
134 INIT_LIST_HEAD(&clp->cl_delegations);
David Howells24c8dbb2006-08-22 20:06:10 -0400135 spin_lock_init(&clp->cl_lock);
David Howells65f27f32006-11-22 14:55:48 +0000136 INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
David Howells24c8dbb2006-08-22 20:06:10 -0400137 rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
138 clp->cl_boot_time = CURRENT_TIME;
139 clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
140#endif
141
142 return clp;
143
144error_3:
Trond Myklebust9c5bf382006-08-22 20:06:14 -0400145 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
146 nfs_callback_down();
David Howells24c8dbb2006-08-22 20:06:10 -0400147error_2:
David Howells24c8dbb2006-08-22 20:06:10 -0400148 kfree(clp);
149error_0:
150 return NULL;
151}
152
Trond Myklebust5dd31772006-08-24 01:03:05 -0400153static void nfs4_shutdown_client(struct nfs_client *clp)
154{
155#ifdef CONFIG_NFS_V4
156 if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
157 nfs4_kill_renewd(clp);
Trond Myklebust9f958ab2007-07-02 13:58:33 -0400158 BUG_ON(!RB_EMPTY_ROOT(&clp->cl_state_owners));
Trond Myklebust5dd31772006-08-24 01:03:05 -0400159 if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
160 nfs_idmap_delete(clp);
161#endif
162}
163
David Howells24c8dbb2006-08-22 20:06:10 -0400164/*
165 * Destroy a shared client record
166 */
167static void nfs_free_client(struct nfs_client *clp)
168{
169 dprintk("--> nfs_free_client(%d)\n", clp->cl_nfsversion);
170
Trond Myklebust5dd31772006-08-24 01:03:05 -0400171 nfs4_shutdown_client(clp);
David Howells24c8dbb2006-08-22 20:06:10 -0400172
173 /* -EIO all pending I/O */
174 if (!IS_ERR(clp->cl_rpcclient))
175 rpc_shutdown_client(clp->cl_rpcclient);
176
177 if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
178 nfs_callback_down();
179
David Howells24c8dbb2006-08-22 20:06:10 -0400180 kfree(clp->cl_hostname);
181 kfree(clp);
182
183 dprintk("<-- nfs_free_client()\n");
184}
185
186/*
187 * Release a reference to a shared client record
188 */
189void nfs_put_client(struct nfs_client *clp)
190{
David Howells27ba8512006-07-30 14:40:56 -0400191 if (!clp)
192 return;
193
David Howells24c8dbb2006-08-22 20:06:10 -0400194 dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
195
196 if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
197 list_del(&clp->cl_share_link);
198 spin_unlock(&nfs_client_lock);
199
200 BUG_ON(!list_empty(&clp->cl_superblocks));
201
202 nfs_free_client(clp);
203 }
204}
205
206/*
207 * Find a client by address
208 * - caller must hold nfs_client_lock
209 */
Trond Myklebust13bbc062006-10-19 23:28:40 -0700210static struct nfs_client *__nfs_find_client(const struct sockaddr_in *addr, int nfsversion, int match_port)
David Howells24c8dbb2006-08-22 20:06:10 -0400211{
212 struct nfs_client *clp;
213
214 list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
Trond Myklebust13bbc062006-10-19 23:28:40 -0700215 /* Don't match clients that failed to initialise properly */
216 if (clp->cl_cons_state < 0)
217 continue;
218
David Howells24c8dbb2006-08-22 20:06:10 -0400219 /* Different NFS versions cannot share the same nfs_client */
220 if (clp->cl_nfsversion != nfsversion)
221 continue;
222
223 if (memcmp(&clp->cl_addr.sin_addr, &addr->sin_addr,
224 sizeof(clp->cl_addr.sin_addr)) != 0)
225 continue;
226
Trond Myklebust13bbc062006-10-19 23:28:40 -0700227 if (!match_port || clp->cl_addr.sin_port == addr->sin_port)
David Howells24c8dbb2006-08-22 20:06:10 -0400228 goto found;
229 }
230
231 return NULL;
232
233found:
234 atomic_inc(&clp->cl_count);
235 return clp;
236}
237
238/*
239 * Find a client by IP address and protocol version
240 * - returns NULL if no such client
241 */
242struct nfs_client *nfs_find_client(const struct sockaddr_in *addr, int nfsversion)
243{
244 struct nfs_client *clp;
245
246 spin_lock(&nfs_client_lock);
Trond Myklebust13bbc062006-10-19 23:28:40 -0700247 clp = __nfs_find_client(addr, nfsversion, 0);
David Howells24c8dbb2006-08-22 20:06:10 -0400248 spin_unlock(&nfs_client_lock);
Trond Myklebust13bbc062006-10-19 23:28:40 -0700249 if (clp != NULL && clp->cl_cons_state != NFS_CS_READY) {
250 nfs_put_client(clp);
251 clp = NULL;
252 }
David Howells24c8dbb2006-08-22 20:06:10 -0400253 return clp;
254}
255
256/*
257 * Look up a client by IP address and protocol version
258 * - creates a new record if one doesn't yet exist
259 */
David Howells54ceac42006-08-22 20:06:13 -0400260static struct nfs_client *nfs_get_client(const char *hostname,
261 const struct sockaddr_in *addr,
262 int nfsversion)
David Howells24c8dbb2006-08-22 20:06:10 -0400263{
264 struct nfs_client *clp, *new = NULL;
265 int error;
266
267 dprintk("--> nfs_get_client(%s,"NIPQUAD_FMT":%d,%d)\n",
268 hostname ?: "", NIPQUAD(addr->sin_addr),
269 addr->sin_port, nfsversion);
270
271 /* see if the client already exists */
272 do {
273 spin_lock(&nfs_client_lock);
274
Trond Myklebust13bbc062006-10-19 23:28:40 -0700275 clp = __nfs_find_client(addr, nfsversion, 1);
David Howells24c8dbb2006-08-22 20:06:10 -0400276 if (clp)
277 goto found_client;
278 if (new)
279 goto install_client;
280
281 spin_unlock(&nfs_client_lock);
282
283 new = nfs_alloc_client(hostname, addr, nfsversion);
284 } while (new);
285
286 return ERR_PTR(-ENOMEM);
287
288 /* install a new client and return with it unready */
289install_client:
290 clp = new;
291 list_add(&clp->cl_share_link, &nfs_client_list);
292 spin_unlock(&nfs_client_lock);
293 dprintk("--> nfs_get_client() = %p [new]\n", clp);
294 return clp;
295
296 /* found an existing client
297 * - make sure it's ready before returning
298 */
299found_client:
300 spin_unlock(&nfs_client_lock);
301
302 if (new)
303 nfs_free_client(new);
304
Matthew Wilcox150030b2007-12-06 16:24:39 -0500305 error = wait_event_killable(nfs_client_active_wq,
Trond Myklebust0bae89e2006-10-08 14:33:24 -0400306 clp->cl_cons_state != NFS_CS_INITING);
307 if (error < 0) {
308 nfs_put_client(clp);
309 return ERR_PTR(-ERESTARTSYS);
David Howells24c8dbb2006-08-22 20:06:10 -0400310 }
311
312 if (clp->cl_cons_state < NFS_CS_READY) {
313 error = clp->cl_cons_state;
314 nfs_put_client(clp);
315 return ERR_PTR(error);
316 }
317
David Howells54ceac42006-08-22 20:06:13 -0400318 BUG_ON(clp->cl_cons_state != NFS_CS_READY);
319
David Howells24c8dbb2006-08-22 20:06:10 -0400320 dprintk("--> nfs_get_client() = %p [share]\n", clp);
321 return clp;
322}
323
324/*
325 * Mark a server as ready or failed
326 */
David Howells54ceac42006-08-22 20:06:13 -0400327static void nfs_mark_client_ready(struct nfs_client *clp, int state)
David Howells24c8dbb2006-08-22 20:06:10 -0400328{
329 clp->cl_cons_state = state;
330 wake_up_all(&nfs_client_active_wq);
331}
David Howells5006a762006-08-22 20:06:12 -0400332
333/*
334 * Initialise the timeout values for a connection
335 */
336static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
337 unsigned int timeo, unsigned int retrans)
338{
339 to->to_initval = timeo * HZ / 10;
340 to->to_retries = retrans;
341 if (!to->to_retries)
342 to->to_retries = 2;
343
344 switch (proto) {
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400345 case XPRT_TRANSPORT_TCP:
\"Talpey, Thomas\2cf7ff72007-09-10 13:49:41 -0400346 case XPRT_TRANSPORT_RDMA:
David Howells5006a762006-08-22 20:06:12 -0400347 if (!to->to_initval)
348 to->to_initval = 60 * HZ;
349 if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
350 to->to_initval = NFS_MAX_TCP_TIMEOUT;
351 to->to_increment = to->to_initval;
352 to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
353 to->to_exponential = 0;
354 break;
\"Talpey, Thomas\0896a722007-09-10 13:48:23 -0400355 case XPRT_TRANSPORT_UDP:
David Howells5006a762006-08-22 20:06:12 -0400356 default:
357 if (!to->to_initval)
358 to->to_initval = 11 * HZ / 10;
359 if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
360 to->to_initval = NFS_MAX_UDP_TIMEOUT;
361 to->to_maxval = NFS_MAX_UDP_TIMEOUT;
362 to->to_exponential = 1;
363 break;
364 }
365}
366
367/*
368 * Create an RPC client handle
369 */
David Howells54ceac42006-08-22 20:06:13 -0400370static int nfs_create_rpc_client(struct nfs_client *clp, int proto,
371 unsigned int timeo,
372 unsigned int retrans,
Chuck Lever43d78ef2007-02-06 18:26:11 -0500373 rpc_authflavor_t flavor,
374 int flags)
David Howells5006a762006-08-22 20:06:12 -0400375{
376 struct rpc_timeout timeparms;
David Howells5006a762006-08-22 20:06:12 -0400377 struct rpc_clnt *clnt = NULL;
Chuck Lever41877d22006-08-22 20:06:20 -0400378 struct rpc_create_args args = {
379 .protocol = proto,
380 .address = (struct sockaddr *)&clp->cl_addr,
381 .addrsize = sizeof(clp->cl_addr),
382 .timeout = &timeparms,
383 .servername = clp->cl_hostname,
384 .program = &nfs_program,
385 .version = clp->rpc_ops->version,
386 .authflavor = flavor,
Chuck Lever43d78ef2007-02-06 18:26:11 -0500387 .flags = flags,
Chuck Lever41877d22006-08-22 20:06:20 -0400388 };
David Howells5006a762006-08-22 20:06:12 -0400389
390 if (!IS_ERR(clp->cl_rpcclient))
391 return 0;
392
393 nfs_init_timeout_values(&timeparms, proto, timeo, retrans);
394 clp->retrans_timeo = timeparms.to_initval;
395 clp->retrans_count = timeparms.to_retries;
396
Chuck Lever41877d22006-08-22 20:06:20 -0400397 clnt = rpc_create(&args);
David Howells5006a762006-08-22 20:06:12 -0400398 if (IS_ERR(clnt)) {
399 dprintk("%s: cannot create RPC client. Error = %ld\n",
400 __FUNCTION__, PTR_ERR(clnt));
401 return PTR_ERR(clnt);
402 }
403
David Howells5006a762006-08-22 20:06:12 -0400404 clp->cl_rpcclient = clnt;
405 return 0;
406}
David Howells54ceac42006-08-22 20:06:13 -0400407
408/*
409 * Version 2 or 3 client destruction
410 */
411static void nfs_destroy_server(struct nfs_server *server)
412{
413 if (!IS_ERR(server->client_acl))
414 rpc_shutdown_client(server->client_acl);
415
416 if (!(server->flags & NFS_MOUNT_NONLM))
417 lockd_down(); /* release rpc.lockd */
418}
419
420/*
421 * Version 2 or 3 lockd setup
422 */
423static int nfs_start_lockd(struct nfs_server *server)
424{
425 int error = 0;
426
427 if (server->nfs_client->cl_nfsversion > 3)
428 goto out;
429 if (server->flags & NFS_MOUNT_NONLM)
430 goto out;
NeilBrown24e36662006-10-02 02:17:45 -0700431 error = lockd_up((server->flags & NFS_MOUNT_TCP) ?
432 IPPROTO_TCP : IPPROTO_UDP);
David Howells54ceac42006-08-22 20:06:13 -0400433 if (error < 0)
434 server->flags |= NFS_MOUNT_NONLM;
435 else
436 server->destroy = nfs_destroy_server;
437out:
438 return error;
439}
440
441/*
442 * Initialise an NFSv3 ACL client connection
443 */
444#ifdef CONFIG_NFS_V3_ACL
445static void nfs_init_server_aclclient(struct nfs_server *server)
446{
447 if (server->nfs_client->cl_nfsversion != 3)
448 goto out_noacl;
449 if (server->flags & NFS_MOUNT_NOACL)
450 goto out_noacl;
451
452 server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
453 if (IS_ERR(server->client_acl))
454 goto out_noacl;
455
456 /* No errors! Assume that Sun nfsacls are supported */
457 server->caps |= NFS_CAP_ACLS;
458 return;
459
460out_noacl:
461 server->caps &= ~NFS_CAP_ACLS;
462}
463#else
464static inline void nfs_init_server_aclclient(struct nfs_server *server)
465{
466 server->flags &= ~NFS_MOUNT_NOACL;
467 server->caps &= ~NFS_CAP_ACLS;
468}
469#endif
470
471/*
472 * Create a general RPC client
473 */
474static int nfs_init_server_rpcclient(struct nfs_server *server, rpc_authflavor_t pseudoflavour)
475{
476 struct nfs_client *clp = server->nfs_client;
477
478 server->client = rpc_clone_client(clp->cl_rpcclient);
479 if (IS_ERR(server->client)) {
480 dprintk("%s: couldn't create rpc_client!\n", __FUNCTION__);
481 return PTR_ERR(server->client);
482 }
483
484 if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
485 struct rpc_auth *auth;
486
487 auth = rpcauth_create(pseudoflavour, server->client);
488 if (IS_ERR(auth)) {
489 dprintk("%s: couldn't create credcache!\n", __FUNCTION__);
490 return PTR_ERR(auth);
491 }
492 }
493 server->client->cl_softrtry = 0;
494 if (server->flags & NFS_MOUNT_SOFT)
495 server->client->cl_softrtry = 1;
496
David Howells54ceac42006-08-22 20:06:13 -0400497 return 0;
498}
499
500/*
501 * Initialise an NFS2 or NFS3 client
502 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400503static int nfs_init_client(struct nfs_client *clp,
504 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400505{
David Howells54ceac42006-08-22 20:06:13 -0400506 int error;
507
508 if (clp->cl_cons_state == NFS_CS_READY) {
509 /* the client is already initialised */
510 dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
511 return 0;
512 }
513
514 /* Check NFS protocol revision and initialize RPC op vector */
515 clp->rpc_ops = &nfs_v2_clientops;
516#ifdef CONFIG_NFS_V3
517 if (clp->cl_nfsversion == 3)
518 clp->rpc_ops = &nfs_v3_clientops;
519#endif
520 /*
521 * Create a client RPC handle for doing FSSTAT with UNIX auth only
522 * - RFC 2623, sec 2.3.2
523 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400524 error = nfs_create_rpc_client(clp, data->nfs_server.protocol,
525 data->timeo, data->retrans, RPC_AUTH_UNIX, 0);
David Howells54ceac42006-08-22 20:06:13 -0400526 if (error < 0)
527 goto error;
528 nfs_mark_client_ready(clp, NFS_CS_READY);
529 return 0;
530
531error:
532 nfs_mark_client_ready(clp, error);
533 dprintk("<-- nfs_init_client() = xerror %d\n", error);
534 return error;
535}
536
537/*
538 * Create a version 2 or 3 client
539 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400540static int nfs_init_server(struct nfs_server *server,
541 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400542{
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 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400554 clp = nfs_get_client(data->nfs_server.hostname,
555 &data->nfs_server.address, nfsvers);
David Howells54ceac42006-08-22 20:06:13 -0400556 if (IS_ERR(clp)) {
557 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
558 return PTR_ERR(clp);
559 }
560
561 error = nfs_init_client(clp, data);
562 if (error < 0)
563 goto error;
564
565 server->nfs_client = clp;
566
567 /* Initialise the client representation from the mount data */
568 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
569
570 if (data->rsize)
571 server->rsize = nfs_block_size(data->rsize, NULL);
572 if (data->wsize)
573 server->wsize = nfs_block_size(data->wsize, NULL);
574
575 server->acregmin = data->acregmin * HZ;
576 server->acregmax = data->acregmax * HZ;
577 server->acdirmin = data->acdirmin * HZ;
578 server->acdirmax = data->acdirmax * HZ;
579
580 /* Start lockd here, before we might error out */
581 error = nfs_start_lockd(server);
582 if (error < 0)
583 goto error;
584
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400585 error = nfs_init_server_rpcclient(server, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -0400586 if (error < 0)
587 goto error;
588
589 server->namelen = data->namlen;
590 /* Create a client RPC handle for the NFSv3 ACL management interface */
591 nfs_init_server_aclclient(server);
David Howells54ceac42006-08-22 20:06:13 -0400592 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
593 return 0;
594
595error:
596 server->nfs_client = NULL;
597 nfs_put_client(clp);
598 dprintk("<-- nfs_init_server() = xerror %d\n", error);
599 return error;
600}
601
602/*
603 * Load up the server record from information gained in an fsinfo record
604 */
605static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
606{
607 unsigned long max_rpc_payload;
608
609 /* Work out a lot of parameters */
610 if (server->rsize == 0)
611 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
612 if (server->wsize == 0)
613 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
614
615 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
616 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
617 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
618 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
619
620 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
621 if (server->rsize > max_rpc_payload)
622 server->rsize = max_rpc_payload;
623 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
624 server->rsize = NFS_MAX_FILE_IO_SIZE;
625 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700626
David Howells54ceac42006-08-22 20:06:13 -0400627 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
628
629 if (server->wsize > max_rpc_payload)
630 server->wsize = max_rpc_payload;
631 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
632 server->wsize = NFS_MAX_FILE_IO_SIZE;
633 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
634 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
635
636 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
637 if (server->dtsize > PAGE_CACHE_SIZE)
638 server->dtsize = PAGE_CACHE_SIZE;
639 if (server->dtsize > server->rsize)
640 server->dtsize = server->rsize;
641
642 if (server->flags & NFS_MOUNT_NOAC) {
643 server->acregmin = server->acregmax = 0;
644 server->acdirmin = server->acdirmax = 0;
645 }
646
647 server->maxfilesize = fsinfo->maxfilesize;
648
649 /* We're airborne Set socket buffersize */
650 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
651}
652
653/*
654 * Probe filesystem information, including the FSID on v2/v3
655 */
656static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
657{
658 struct nfs_fsinfo fsinfo;
659 struct nfs_client *clp = server->nfs_client;
660 int error;
661
662 dprintk("--> nfs_probe_fsinfo()\n");
663
664 if (clp->rpc_ops->set_capabilities != NULL) {
665 error = clp->rpc_ops->set_capabilities(server, mntfh);
666 if (error < 0)
667 goto out_error;
668 }
669
670 fsinfo.fattr = fattr;
671 nfs_fattr_init(fattr);
672 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
673 if (error < 0)
674 goto out_error;
675
676 nfs_server_set_fsinfo(server, &fsinfo);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700677 error = bdi_init(&server->backing_dev_info);
678 if (error)
679 goto out_error;
680
David Howells54ceac42006-08-22 20:06:13 -0400681
682 /* Get some general file system info */
683 if (server->namelen == 0) {
684 struct nfs_pathconf pathinfo;
685
686 pathinfo.fattr = fattr;
687 nfs_fattr_init(fattr);
688
689 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
690 server->namelen = pathinfo.max_namelen;
691 }
692
693 dprintk("<-- nfs_probe_fsinfo() = 0\n");
694 return 0;
695
696out_error:
697 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
698 return error;
699}
700
701/*
702 * Copy useful information when duplicating a server record
703 */
704static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
705{
706 target->flags = source->flags;
707 target->acregmin = source->acregmin;
708 target->acregmax = source->acregmax;
709 target->acdirmin = source->acdirmin;
710 target->acdirmax = source->acdirmax;
711 target->caps = source->caps;
712}
713
714/*
715 * Allocate and initialise a server record
716 */
717static struct nfs_server *nfs_alloc_server(void)
718{
719 struct nfs_server *server;
720
721 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
722 if (!server)
723 return NULL;
724
725 server->client = server->client_acl = ERR_PTR(-EINVAL);
726
727 /* Zero out the NFS state stuff */
728 INIT_LIST_HEAD(&server->client_link);
729 INIT_LIST_HEAD(&server->master_link);
730
731 server->io_stats = nfs_alloc_iostats();
732 if (!server->io_stats) {
733 kfree(server);
734 return NULL;
735 }
736
737 return server;
738}
739
740/*
741 * Free up a server record
742 */
743void nfs_free_server(struct nfs_server *server)
744{
745 dprintk("--> nfs_free_server()\n");
746
747 spin_lock(&nfs_client_lock);
748 list_del(&server->client_link);
749 list_del(&server->master_link);
750 spin_unlock(&nfs_client_lock);
751
752 if (server->destroy != NULL)
753 server->destroy(server);
754 if (!IS_ERR(server->client))
755 rpc_shutdown_client(server->client);
756
757 nfs_put_client(server->nfs_client);
758
759 nfs_free_iostats(server->io_stats);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700760 bdi_destroy(&server->backing_dev_info);
David Howells54ceac42006-08-22 20:06:13 -0400761 kfree(server);
762 nfs_release_automount_timer();
763 dprintk("<-- nfs_free_server()\n");
764}
765
766/*
767 * Create a version 2 or 3 volume record
768 * - keyed on server and FSID
769 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400770struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -0400771 struct nfs_fh *mntfh)
772{
773 struct nfs_server *server;
774 struct nfs_fattr fattr;
775 int error;
776
777 server = nfs_alloc_server();
778 if (!server)
779 return ERR_PTR(-ENOMEM);
780
781 /* Get a client representation */
782 error = nfs_init_server(server, data);
783 if (error < 0)
784 goto error;
785
786 BUG_ON(!server->nfs_client);
787 BUG_ON(!server->nfs_client->rpc_ops);
788 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
789
790 /* Probe the root fh to retrieve its FSID */
791 error = nfs_probe_fsinfo(server, mntfh, &fattr);
792 if (error < 0)
793 goto error;
Trond Myklebust54af3bb2007-09-28 12:27:41 -0400794 if (server->nfs_client->rpc_ops->version == 3) {
795 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
796 server->namelen = NFS3_MAXNAMLEN;
797 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
798 server->caps |= NFS_CAP_READDIRPLUS;
799 } else {
800 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
801 server->namelen = NFS2_MAXNAMLEN;
802 }
803
David Howells54ceac42006-08-22 20:06:13 -0400804 if (!(fattr.valid & NFS_ATTR_FATTR)) {
805 error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
806 if (error < 0) {
807 dprintk("nfs_create_server: getattr error = %d\n", -error);
808 goto error;
809 }
810 }
811 memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
812
David Howells6daabf12006-08-24 15:44:16 -0400813 dprintk("Server FSID: %llx:%llx\n",
814 (unsigned long long) server->fsid.major,
815 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -0400816
817 BUG_ON(!server->nfs_client);
818 BUG_ON(!server->nfs_client->rpc_ops);
819 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
820
821 spin_lock(&nfs_client_lock);
822 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
823 list_add_tail(&server->master_link, &nfs_volume_list);
824 spin_unlock(&nfs_client_lock);
825
826 server->mount_time = jiffies;
827 return server;
828
829error:
830 nfs_free_server(server);
831 return ERR_PTR(error);
832}
833
834#ifdef CONFIG_NFS_V4
835/*
836 * Initialise an NFS4 client record
837 */
838static int nfs4_init_client(struct nfs_client *clp,
839 int proto, int timeo, int retrans,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -0700840 const char *ip_addr,
David Howells54ceac42006-08-22 20:06:13 -0400841 rpc_authflavor_t authflavour)
842{
843 int error;
844
845 if (clp->cl_cons_state == NFS_CS_READY) {
846 /* the client is initialised already */
847 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
848 return 0;
849 }
850
851 /* Check NFS protocol revision and initialize RPC op vector */
852 clp->rpc_ops = &nfs_v4_clientops;
853
Chuck Lever43d78ef2007-02-06 18:26:11 -0500854 error = nfs_create_rpc_client(clp, proto, timeo, retrans, authflavour,
855 RPC_CLNT_CREATE_DISCRTRY);
David Howells54ceac42006-08-22 20:06:13 -0400856 if (error < 0)
857 goto error;
J. Bruce Fields7d9ac062006-10-19 23:28:39 -0700858 memcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
David Howells54ceac42006-08-22 20:06:13 -0400859
860 error = nfs_idmap_new(clp);
861 if (error < 0) {
862 dprintk("%s: failed to create idmapper. Error = %d\n",
863 __FUNCTION__, error);
David Howells54ceac42006-08-22 20:06:13 -0400864 goto error;
865 }
Trond Myklebust9c5bf382006-08-22 20:06:14 -0400866 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
David Howells54ceac42006-08-22 20:06:13 -0400867
868 nfs_mark_client_ready(clp, NFS_CS_READY);
869 return 0;
870
871error:
872 nfs_mark_client_ready(clp, error);
873 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
874 return error;
875}
876
877/*
878 * Set up an NFS4 client
879 */
880static int nfs4_set_client(struct nfs_server *server,
881 const char *hostname, const struct sockaddr_in *addr,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -0700882 const char *ip_addr,
David Howells54ceac42006-08-22 20:06:13 -0400883 rpc_authflavor_t authflavour,
884 int proto, int timeo, int retrans)
885{
886 struct nfs_client *clp;
887 int error;
888
889 dprintk("--> nfs4_set_client()\n");
890
891 /* Allocate or find a client reference we can use */
892 clp = nfs_get_client(hostname, addr, 4);
893 if (IS_ERR(clp)) {
894 error = PTR_ERR(clp);
895 goto error;
896 }
J. Bruce Fields7d9ac062006-10-19 23:28:39 -0700897 error = nfs4_init_client(clp, proto, timeo, retrans, ip_addr, authflavour);
David Howells54ceac42006-08-22 20:06:13 -0400898 if (error < 0)
899 goto error_put;
900
901 server->nfs_client = clp;
902 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
903 return 0;
904
905error_put:
906 nfs_put_client(clp);
907error:
908 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
909 return error;
910}
911
912/*
913 * Create a version 4 volume record
914 */
915static int nfs4_init_server(struct nfs_server *server,
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -0400916 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400917{
918 int error;
919
920 dprintk("--> nfs4_init_server()\n");
921
922 /* Initialise the client representation from the mount data */
923 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
924 server->caps |= NFS_CAP_ATOMIC_OPEN;
925
926 if (data->rsize)
927 server->rsize = nfs_block_size(data->rsize, NULL);
928 if (data->wsize)
929 server->wsize = nfs_block_size(data->wsize, NULL);
930
931 server->acregmin = data->acregmin * HZ;
932 server->acregmax = data->acregmax * HZ;
933 server->acdirmin = data->acdirmin * HZ;
934 server->acdirmax = data->acdirmax * HZ;
935
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -0400936 error = nfs_init_server_rpcclient(server, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -0400937
938 /* Done */
939 dprintk("<-- nfs4_init_server() = %d\n", error);
940 return error;
941}
942
943/*
944 * Create a version 4 volume record
945 * - keyed on server and FSID
946 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -0400947struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -0400948 struct nfs_fh *mntfh)
949{
950 struct nfs_fattr fattr;
951 struct nfs_server *server;
952 int error;
953
954 dprintk("--> nfs4_create_server()\n");
955
956 server = nfs_alloc_server();
957 if (!server)
958 return ERR_PTR(-ENOMEM);
959
960 /* Get a client record */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -0400961 error = nfs4_set_client(server,
962 data->nfs_server.hostname,
963 &data->nfs_server.address,
964 data->client_address,
965 data->auth_flavors[0],
966 data->nfs_server.protocol,
967 data->timeo, data->retrans);
David Howells54ceac42006-08-22 20:06:13 -0400968 if (error < 0)
969 goto error;
970
971 /* set up the general RPC client */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -0400972 error = nfs4_init_server(server, data);
David Howells54ceac42006-08-22 20:06:13 -0400973 if (error < 0)
974 goto error;
975
976 BUG_ON(!server->nfs_client);
977 BUG_ON(!server->nfs_client->rpc_ops);
978 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
979
980 /* Probe the root fh to retrieve its FSID */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -0400981 error = nfs4_path_walk(server, mntfh, data->nfs_server.export_path);
David Howells54ceac42006-08-22 20:06:13 -0400982 if (error < 0)
983 goto error;
984
David Howells6daabf12006-08-24 15:44:16 -0400985 dprintk("Server FSID: %llx:%llx\n",
986 (unsigned long long) server->fsid.major,
987 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -0400988 dprintk("Mount FH: %d\n", mntfh->size);
989
990 error = nfs_probe_fsinfo(server, mntfh, &fattr);
991 if (error < 0)
992 goto error;
993
Trond Myklebust54af3bb2007-09-28 12:27:41 -0400994 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
995 server->namelen = NFS4_MAXNAMLEN;
996
David Howells54ceac42006-08-22 20:06:13 -0400997 BUG_ON(!server->nfs_client);
998 BUG_ON(!server->nfs_client->rpc_ops);
999 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1000
1001 spin_lock(&nfs_client_lock);
1002 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1003 list_add_tail(&server->master_link, &nfs_volume_list);
1004 spin_unlock(&nfs_client_lock);
1005
1006 server->mount_time = jiffies;
1007 dprintk("<-- nfs4_create_server() = %p\n", server);
1008 return server;
1009
1010error:
1011 nfs_free_server(server);
1012 dprintk("<-- nfs4_create_server() = error %d\n", error);
1013 return ERR_PTR(error);
1014}
1015
1016/*
1017 * Create an NFS4 referral server record
1018 */
1019struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001020 struct nfs_fh *mntfh)
David Howells54ceac42006-08-22 20:06:13 -04001021{
1022 struct nfs_client *parent_client;
1023 struct nfs_server *server, *parent_server;
1024 struct nfs_fattr fattr;
1025 int error;
1026
1027 dprintk("--> nfs4_create_referral_server()\n");
1028
1029 server = nfs_alloc_server();
1030 if (!server)
1031 return ERR_PTR(-ENOMEM);
1032
1033 parent_server = NFS_SB(data->sb);
1034 parent_client = parent_server->nfs_client;
1035
1036 /* Get a client representation.
1037 * Note: NFSv4 always uses TCP, */
1038 error = nfs4_set_client(server, data->hostname, data->addr,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001039 parent_client->cl_ipaddr,
David Howells54ceac42006-08-22 20:06:13 -04001040 data->authflavor,
1041 parent_server->client->cl_xprt->prot,
1042 parent_client->retrans_timeo,
1043 parent_client->retrans_count);
andros@citi.umich.edu297de4f2006-08-29 12:19:41 -04001044 if (error < 0)
1045 goto error;
David Howells54ceac42006-08-22 20:06:13 -04001046
1047 /* Initialise the client representation from the parent server */
1048 nfs_server_copy_userdata(server, parent_server);
1049 server->caps |= NFS_CAP_ATOMIC_OPEN;
1050
1051 error = nfs_init_server_rpcclient(server, data->authflavor);
1052 if (error < 0)
1053 goto error;
1054
1055 BUG_ON(!server->nfs_client);
1056 BUG_ON(!server->nfs_client->rpc_ops);
1057 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1058
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001059 /* Probe the root fh to retrieve its FSID and filehandle */
1060 error = nfs4_path_walk(server, mntfh, data->mnt_path);
1061 if (error < 0)
1062 goto error;
1063
David Howells54ceac42006-08-22 20:06:13 -04001064 /* probe the filesystem info for this server filesystem */
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001065 error = nfs_probe_fsinfo(server, mntfh, &fattr);
David Howells54ceac42006-08-22 20:06:13 -04001066 if (error < 0)
1067 goto error;
1068
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001069 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1070 server->namelen = NFS4_MAXNAMLEN;
1071
David Howells54ceac42006-08-22 20:06:13 -04001072 dprintk("Referral FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001073 (unsigned long long) server->fsid.major,
1074 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001075
1076 spin_lock(&nfs_client_lock);
1077 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1078 list_add_tail(&server->master_link, &nfs_volume_list);
1079 spin_unlock(&nfs_client_lock);
1080
1081 server->mount_time = jiffies;
1082
1083 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1084 return server;
1085
1086error:
1087 nfs_free_server(server);
1088 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1089 return ERR_PTR(error);
1090}
1091
1092#endif /* CONFIG_NFS_V4 */
1093
1094/*
1095 * Clone an NFS2, NFS3 or NFS4 server record
1096 */
1097struct nfs_server *nfs_clone_server(struct nfs_server *source,
1098 struct nfs_fh *fh,
1099 struct nfs_fattr *fattr)
1100{
1101 struct nfs_server *server;
1102 struct nfs_fattr fattr_fsinfo;
1103 int error;
1104
1105 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
David Howells6daabf12006-08-24 15:44:16 -04001106 (unsigned long long) fattr->fsid.major,
1107 (unsigned long long) fattr->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001108
1109 server = nfs_alloc_server();
1110 if (!server)
1111 return ERR_PTR(-ENOMEM);
1112
1113 /* Copy data from the source */
1114 server->nfs_client = source->nfs_client;
1115 atomic_inc(&server->nfs_client->cl_count);
1116 nfs_server_copy_userdata(server, source);
1117
1118 server->fsid = fattr->fsid;
1119
1120 error = nfs_init_server_rpcclient(server, source->client->cl_auth->au_flavor);
1121 if (error < 0)
1122 goto out_free_server;
1123 if (!IS_ERR(source->client_acl))
1124 nfs_init_server_aclclient(server);
1125
1126 /* probe the filesystem info for this server filesystem */
1127 error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo);
1128 if (error < 0)
1129 goto out_free_server;
1130
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001131 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1132 server->namelen = NFS4_MAXNAMLEN;
1133
David Howells54ceac42006-08-22 20:06:13 -04001134 dprintk("Cloned FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001135 (unsigned long long) server->fsid.major,
1136 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001137
1138 error = nfs_start_lockd(server);
1139 if (error < 0)
1140 goto out_free_server;
1141
1142 spin_lock(&nfs_client_lock);
1143 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1144 list_add_tail(&server->master_link, &nfs_volume_list);
1145 spin_unlock(&nfs_client_lock);
1146
1147 server->mount_time = jiffies;
1148
1149 dprintk("<-- nfs_clone_server() = %p\n", server);
1150 return server;
1151
1152out_free_server:
1153 nfs_free_server(server);
1154 dprintk("<-- nfs_clone_server() = error %d\n", error);
1155 return ERR_PTR(error);
1156}
David Howells6aaca562006-08-22 20:06:13 -04001157
1158#ifdef CONFIG_PROC_FS
1159static struct proc_dir_entry *proc_fs_nfs;
1160
1161static int nfs_server_list_open(struct inode *inode, struct file *file);
1162static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1163static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1164static void nfs_server_list_stop(struct seq_file *p, void *v);
1165static int nfs_server_list_show(struct seq_file *m, void *v);
1166
1167static struct seq_operations nfs_server_list_ops = {
1168 .start = nfs_server_list_start,
1169 .next = nfs_server_list_next,
1170 .stop = nfs_server_list_stop,
1171 .show = nfs_server_list_show,
1172};
1173
Arjan van de Ven00977a52007-02-12 00:55:34 -08001174static const struct file_operations nfs_server_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001175 .open = nfs_server_list_open,
1176 .read = seq_read,
1177 .llseek = seq_lseek,
1178 .release = seq_release,
1179};
1180
1181static int nfs_volume_list_open(struct inode *inode, struct file *file);
1182static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1183static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1184static void nfs_volume_list_stop(struct seq_file *p, void *v);
1185static int nfs_volume_list_show(struct seq_file *m, void *v);
1186
1187static struct seq_operations nfs_volume_list_ops = {
1188 .start = nfs_volume_list_start,
1189 .next = nfs_volume_list_next,
1190 .stop = nfs_volume_list_stop,
1191 .show = nfs_volume_list_show,
1192};
1193
Arjan van de Ven00977a52007-02-12 00:55:34 -08001194static const struct file_operations nfs_volume_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001195 .open = nfs_volume_list_open,
1196 .read = seq_read,
1197 .llseek = seq_lseek,
1198 .release = seq_release,
1199};
1200
1201/*
1202 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1203 * we're dealing
1204 */
1205static int nfs_server_list_open(struct inode *inode, struct file *file)
1206{
1207 struct seq_file *m;
1208 int ret;
1209
1210 ret = seq_open(file, &nfs_server_list_ops);
1211 if (ret < 0)
1212 return ret;
1213
1214 m = file->private_data;
1215 m->private = PDE(inode)->data;
1216
1217 return 0;
1218}
1219
1220/*
1221 * set up the iterator to start reading from the server list and return the first item
1222 */
1223static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1224{
David Howells6aaca562006-08-22 20:06:13 -04001225 /* lock the list against modification */
1226 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001227 return seq_list_start_head(&nfs_client_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001228}
1229
1230/*
1231 * move to next server
1232 */
1233static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1234{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001235 return seq_list_next(v, &nfs_client_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001236}
1237
1238/*
1239 * clean up after reading from the transports list
1240 */
1241static void nfs_server_list_stop(struct seq_file *p, void *v)
1242{
1243 spin_unlock(&nfs_client_lock);
1244}
1245
1246/*
1247 * display a header line followed by a load of call lines
1248 */
1249static int nfs_server_list_show(struct seq_file *m, void *v)
1250{
1251 struct nfs_client *clp;
1252
1253 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001254 if (v == &nfs_client_list) {
David Howells6aaca562006-08-22 20:06:13 -04001255 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1256 return 0;
1257 }
1258
1259 /* display one transport per line on subsequent lines */
1260 clp = list_entry(v, struct nfs_client, cl_share_link);
1261
1262 seq_printf(m, "v%d %02x%02x%02x%02x %4hx %3d %s\n",
1263 clp->cl_nfsversion,
1264 NIPQUAD(clp->cl_addr.sin_addr),
1265 ntohs(clp->cl_addr.sin_port),
1266 atomic_read(&clp->cl_count),
1267 clp->cl_hostname);
1268
1269 return 0;
1270}
1271
1272/*
1273 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1274 */
1275static int nfs_volume_list_open(struct inode *inode, struct file *file)
1276{
1277 struct seq_file *m;
1278 int ret;
1279
1280 ret = seq_open(file, &nfs_volume_list_ops);
1281 if (ret < 0)
1282 return ret;
1283
1284 m = file->private_data;
1285 m->private = PDE(inode)->data;
1286
1287 return 0;
1288}
1289
1290/*
1291 * set up the iterator to start reading from the volume list and return the first item
1292 */
1293static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1294{
David Howells6aaca562006-08-22 20:06:13 -04001295 /* lock the list against modification */
1296 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001297 return seq_list_start_head(&nfs_volume_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001298}
1299
1300/*
1301 * move to next volume
1302 */
1303static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1304{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001305 return seq_list_next(v, &nfs_volume_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001306}
1307
1308/*
1309 * clean up after reading from the transports list
1310 */
1311static void nfs_volume_list_stop(struct seq_file *p, void *v)
1312{
1313 spin_unlock(&nfs_client_lock);
1314}
1315
1316/*
1317 * display a header line followed by a load of call lines
1318 */
1319static int nfs_volume_list_show(struct seq_file *m, void *v)
1320{
1321 struct nfs_server *server;
1322 struct nfs_client *clp;
1323 char dev[8], fsid[17];
1324
1325 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001326 if (v == &nfs_volume_list) {
David Howells6aaca562006-08-22 20:06:13 -04001327 seq_puts(m, "NV SERVER PORT DEV FSID\n");
1328 return 0;
1329 }
1330 /* display one transport per line on subsequent lines */
1331 server = list_entry(v, struct nfs_server, master_link);
1332 clp = server->nfs_client;
1333
1334 snprintf(dev, 8, "%u:%u",
1335 MAJOR(server->s_dev), MINOR(server->s_dev));
1336
1337 snprintf(fsid, 17, "%llx:%llx",
David Howells6daabf12006-08-24 15:44:16 -04001338 (unsigned long long) server->fsid.major,
1339 (unsigned long long) server->fsid.minor);
David Howells6aaca562006-08-22 20:06:13 -04001340
1341 seq_printf(m, "v%d %02x%02x%02x%02x %4hx %-7s %-17s\n",
1342 clp->cl_nfsversion,
1343 NIPQUAD(clp->cl_addr.sin_addr),
1344 ntohs(clp->cl_addr.sin_port),
1345 dev,
1346 fsid);
1347
1348 return 0;
1349}
1350
1351/*
1352 * initialise the /proc/fs/nfsfs/ directory
1353 */
1354int __init nfs_fs_proc_init(void)
1355{
1356 struct proc_dir_entry *p;
1357
1358 proc_fs_nfs = proc_mkdir("nfsfs", proc_root_fs);
1359 if (!proc_fs_nfs)
1360 goto error_0;
1361
1362 proc_fs_nfs->owner = THIS_MODULE;
1363
1364 /* a file of servers with which we're dealing */
1365 p = create_proc_entry("servers", S_IFREG|S_IRUGO, proc_fs_nfs);
1366 if (!p)
1367 goto error_1;
1368
1369 p->proc_fops = &nfs_server_list_fops;
1370 p->owner = THIS_MODULE;
1371
1372 /* a file of volumes that we have mounted */
1373 p = create_proc_entry("volumes", S_IFREG|S_IRUGO, proc_fs_nfs);
1374 if (!p)
1375 goto error_2;
1376
1377 p->proc_fops = &nfs_volume_list_fops;
1378 p->owner = THIS_MODULE;
1379 return 0;
1380
1381error_2:
1382 remove_proc_entry("servers", proc_fs_nfs);
1383error_1:
1384 remove_proc_entry("nfsfs", proc_root_fs);
1385error_0:
1386 return -ENOMEM;
1387}
1388
1389/*
1390 * clean up the /proc/fs/nfsfs/ directory
1391 */
1392void nfs_fs_proc_exit(void)
1393{
1394 remove_proc_entry("volumes", proc_fs_nfs);
1395 remove_proc_entry("servers", proc_fs_nfs);
1396 remove_proc_entry("nfsfs", proc_root_fs);
1397}
1398
1399#endif /* CONFIG_PROC_FS */