blob: a6f625497612048dcf2ef5ef9048187e8e1dd63e [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
Linus Torvalds83250492006-10-08 17:28:25 -0700305 error = wait_event_interruptible(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{
David Howells54ceac42006-08-22 20:06:13 -0400413 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 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400504static int nfs_init_client(struct nfs_client *clp,
505 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400506{
David Howells54ceac42006-08-22 20:06:13 -0400507 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 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400525 error = nfs_create_rpc_client(clp, data->nfs_server.protocol,
526 data->timeo, data->retrans, 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 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400541static int nfs_init_server(struct nfs_server *server,
542 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400543{
544 struct nfs_client *clp;
545 int error, nfsvers = 2;
546
547 dprintk("--> nfs_init_server()\n");
548
549#ifdef CONFIG_NFS_V3
550 if (data->flags & NFS_MOUNT_VER3)
551 nfsvers = 3;
552#endif
553
554 /* Allocate or find a client reference we can use */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400555 clp = nfs_get_client(data->nfs_server.hostname,
556 &data->nfs_server.address, nfsvers);
David Howells54ceac42006-08-22 20:06:13 -0400557 if (IS_ERR(clp)) {
558 dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
559 return PTR_ERR(clp);
560 }
561
562 error = nfs_init_client(clp, data);
563 if (error < 0)
564 goto error;
565
566 server->nfs_client = clp;
567
568 /* Initialise the client representation from the mount data */
569 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
570
571 if (data->rsize)
572 server->rsize = nfs_block_size(data->rsize, NULL);
573 if (data->wsize)
574 server->wsize = nfs_block_size(data->wsize, NULL);
575
576 server->acregmin = data->acregmin * HZ;
577 server->acregmax = data->acregmax * HZ;
578 server->acdirmin = data->acdirmin * HZ;
579 server->acdirmax = data->acdirmax * HZ;
580
581 /* Start lockd here, before we might error out */
582 error = nfs_start_lockd(server);
583 if (error < 0)
584 goto error;
585
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400586 error = nfs_init_server_rpcclient(server, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -0400587 if (error < 0)
588 goto error;
589
590 server->namelen = data->namlen;
591 /* Create a client RPC handle for the NFSv3 ACL management interface */
592 nfs_init_server_aclclient(server);
David Howells54ceac42006-08-22 20:06:13 -0400593 dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
594 return 0;
595
596error:
597 server->nfs_client = NULL;
598 nfs_put_client(clp);
599 dprintk("<-- nfs_init_server() = xerror %d\n", error);
600 return error;
601}
602
603/*
604 * Load up the server record from information gained in an fsinfo record
605 */
606static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
607{
608 unsigned long max_rpc_payload;
609
610 /* Work out a lot of parameters */
611 if (server->rsize == 0)
612 server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
613 if (server->wsize == 0)
614 server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
615
616 if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
617 server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
618 if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
619 server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
620
621 max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
622 if (server->rsize > max_rpc_payload)
623 server->rsize = max_rpc_payload;
624 if (server->rsize > NFS_MAX_FILE_IO_SIZE)
625 server->rsize = NFS_MAX_FILE_IO_SIZE;
626 server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700627
David Howells54ceac42006-08-22 20:06:13 -0400628 server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
629
630 if (server->wsize > max_rpc_payload)
631 server->wsize = max_rpc_payload;
632 if (server->wsize > NFS_MAX_FILE_IO_SIZE)
633 server->wsize = NFS_MAX_FILE_IO_SIZE;
634 server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
635 server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
636
637 server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
638 if (server->dtsize > PAGE_CACHE_SIZE)
639 server->dtsize = PAGE_CACHE_SIZE;
640 if (server->dtsize > server->rsize)
641 server->dtsize = server->rsize;
642
643 if (server->flags & NFS_MOUNT_NOAC) {
644 server->acregmin = server->acregmax = 0;
645 server->acdirmin = server->acdirmax = 0;
646 }
647
648 server->maxfilesize = fsinfo->maxfilesize;
649
650 /* We're airborne Set socket buffersize */
651 rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
652}
653
654/*
655 * Probe filesystem information, including the FSID on v2/v3
656 */
657static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
658{
659 struct nfs_fsinfo fsinfo;
660 struct nfs_client *clp = server->nfs_client;
661 int error;
662
663 dprintk("--> nfs_probe_fsinfo()\n");
664
665 if (clp->rpc_ops->set_capabilities != NULL) {
666 error = clp->rpc_ops->set_capabilities(server, mntfh);
667 if (error < 0)
668 goto out_error;
669 }
670
671 fsinfo.fattr = fattr;
672 nfs_fattr_init(fattr);
673 error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
674 if (error < 0)
675 goto out_error;
676
677 nfs_server_set_fsinfo(server, &fsinfo);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700678 error = bdi_init(&server->backing_dev_info);
679 if (error)
680 goto out_error;
681
David Howells54ceac42006-08-22 20:06:13 -0400682
683 /* Get some general file system info */
684 if (server->namelen == 0) {
685 struct nfs_pathconf pathinfo;
686
687 pathinfo.fattr = fattr;
688 nfs_fattr_init(fattr);
689
690 if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
691 server->namelen = pathinfo.max_namelen;
692 }
693
694 dprintk("<-- nfs_probe_fsinfo() = 0\n");
695 return 0;
696
697out_error:
698 dprintk("nfs_probe_fsinfo: error = %d\n", -error);
699 return error;
700}
701
702/*
703 * Copy useful information when duplicating a server record
704 */
705static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
706{
707 target->flags = source->flags;
708 target->acregmin = source->acregmin;
709 target->acregmax = source->acregmax;
710 target->acdirmin = source->acdirmin;
711 target->acdirmax = source->acdirmax;
712 target->caps = source->caps;
713}
714
715/*
716 * Allocate and initialise a server record
717 */
718static struct nfs_server *nfs_alloc_server(void)
719{
720 struct nfs_server *server;
721
722 server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
723 if (!server)
724 return NULL;
725
726 server->client = server->client_acl = ERR_PTR(-EINVAL);
727
728 /* Zero out the NFS state stuff */
729 INIT_LIST_HEAD(&server->client_link);
730 INIT_LIST_HEAD(&server->master_link);
731
732 server->io_stats = nfs_alloc_iostats();
733 if (!server->io_stats) {
734 kfree(server);
735 return NULL;
736 }
737
738 return server;
739}
740
741/*
742 * Free up a server record
743 */
744void nfs_free_server(struct nfs_server *server)
745{
746 dprintk("--> nfs_free_server()\n");
747
748 spin_lock(&nfs_client_lock);
749 list_del(&server->client_link);
750 list_del(&server->master_link);
751 spin_unlock(&nfs_client_lock);
752
753 if (server->destroy != NULL)
754 server->destroy(server);
Trond Myklebust5cef3382007-12-11 22:01:56 -0500755
756 if (!IS_ERR(server->client_acl))
757 rpc_shutdown_client(server->client_acl);
David Howells54ceac42006-08-22 20:06:13 -0400758 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);
Peter Zijlstrae0bf68d2007-10-16 23:25:46 -0700764 bdi_destroy(&server->backing_dev_info);
David Howells54ceac42006-08-22 20:06:13 -0400765 kfree(server);
766 nfs_release_automount_timer();
767 dprintk("<-- nfs_free_server()\n");
768}
769
770/*
771 * Create a version 2 or 3 volume record
772 * - keyed on server and FSID
773 */
\"Talpey, Thomas\2283f8d2007-09-10 13:43:56 -0400774struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -0400775 struct nfs_fh *mntfh)
776{
777 struct nfs_server *server;
778 struct nfs_fattr fattr;
779 int error;
780
781 server = nfs_alloc_server();
782 if (!server)
783 return ERR_PTR(-ENOMEM);
784
785 /* Get a client representation */
786 error = nfs_init_server(server, data);
787 if (error < 0)
788 goto error;
789
790 BUG_ON(!server->nfs_client);
791 BUG_ON(!server->nfs_client->rpc_ops);
792 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
793
794 /* Probe the root fh to retrieve its FSID */
795 error = nfs_probe_fsinfo(server, mntfh, &fattr);
796 if (error < 0)
797 goto error;
Trond Myklebust54af3bb2007-09-28 12:27:41 -0400798 if (server->nfs_client->rpc_ops->version == 3) {
799 if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
800 server->namelen = NFS3_MAXNAMLEN;
801 if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
802 server->caps |= NFS_CAP_READDIRPLUS;
803 } else {
804 if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
805 server->namelen = NFS2_MAXNAMLEN;
806 }
807
David Howells54ceac42006-08-22 20:06:13 -0400808 if (!(fattr.valid & NFS_ATTR_FATTR)) {
809 error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
810 if (error < 0) {
811 dprintk("nfs_create_server: getattr error = %d\n", -error);
812 goto error;
813 }
814 }
815 memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
816
David Howells6daabf12006-08-24 15:44:16 -0400817 dprintk("Server FSID: %llx:%llx\n",
818 (unsigned long long) server->fsid.major,
819 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -0400820
821 BUG_ON(!server->nfs_client);
822 BUG_ON(!server->nfs_client->rpc_ops);
823 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
824
825 spin_lock(&nfs_client_lock);
826 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
827 list_add_tail(&server->master_link, &nfs_volume_list);
828 spin_unlock(&nfs_client_lock);
829
830 server->mount_time = jiffies;
831 return server;
832
833error:
834 nfs_free_server(server);
835 return ERR_PTR(error);
836}
837
838#ifdef CONFIG_NFS_V4
839/*
840 * Initialise an NFS4 client record
841 */
842static int nfs4_init_client(struct nfs_client *clp,
843 int proto, int timeo, int retrans,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -0700844 const char *ip_addr,
David Howells54ceac42006-08-22 20:06:13 -0400845 rpc_authflavor_t authflavour)
846{
847 int error;
848
849 if (clp->cl_cons_state == NFS_CS_READY) {
850 /* the client is initialised already */
851 dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
852 return 0;
853 }
854
855 /* Check NFS protocol revision and initialize RPC op vector */
856 clp->rpc_ops = &nfs_v4_clientops;
857
Chuck Lever43d78ef2007-02-06 18:26:11 -0500858 error = nfs_create_rpc_client(clp, proto, timeo, retrans, authflavour,
859 RPC_CLNT_CREATE_DISCRTRY);
David Howells54ceac42006-08-22 20:06:13 -0400860 if (error < 0)
861 goto error;
J. Bruce Fields7d9ac062006-10-19 23:28:39 -0700862 memcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
David Howells54ceac42006-08-22 20:06:13 -0400863
864 error = nfs_idmap_new(clp);
865 if (error < 0) {
866 dprintk("%s: failed to create idmapper. Error = %d\n",
867 __FUNCTION__, error);
David Howells54ceac42006-08-22 20:06:13 -0400868 goto error;
869 }
Trond Myklebust9c5bf382006-08-22 20:06:14 -0400870 __set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
David Howells54ceac42006-08-22 20:06:13 -0400871
872 nfs_mark_client_ready(clp, NFS_CS_READY);
873 return 0;
874
875error:
876 nfs_mark_client_ready(clp, error);
877 dprintk("<-- nfs4_init_client() = xerror %d\n", error);
878 return error;
879}
880
881/*
882 * Set up an NFS4 client
883 */
884static int nfs4_set_client(struct nfs_server *server,
885 const char *hostname, const struct sockaddr_in *addr,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -0700886 const char *ip_addr,
David Howells54ceac42006-08-22 20:06:13 -0400887 rpc_authflavor_t authflavour,
888 int proto, int timeo, int retrans)
889{
890 struct nfs_client *clp;
891 int error;
892
893 dprintk("--> nfs4_set_client()\n");
894
895 /* Allocate or find a client reference we can use */
896 clp = nfs_get_client(hostname, addr, 4);
897 if (IS_ERR(clp)) {
898 error = PTR_ERR(clp);
899 goto error;
900 }
J. Bruce Fields7d9ac062006-10-19 23:28:39 -0700901 error = nfs4_init_client(clp, proto, timeo, retrans, ip_addr, authflavour);
David Howells54ceac42006-08-22 20:06:13 -0400902 if (error < 0)
903 goto error_put;
904
905 server->nfs_client = clp;
906 dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
907 return 0;
908
909error_put:
910 nfs_put_client(clp);
911error:
912 dprintk("<-- nfs4_set_client() = xerror %d\n", error);
913 return error;
914}
915
916/*
917 * Create a version 4 volume record
918 */
919static int nfs4_init_server(struct nfs_server *server,
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -0400920 const struct nfs_parsed_mount_data *data)
David Howells54ceac42006-08-22 20:06:13 -0400921{
922 int error;
923
924 dprintk("--> nfs4_init_server()\n");
925
926 /* Initialise the client representation from the mount data */
927 server->flags = data->flags & NFS_MOUNT_FLAGMASK;
928 server->caps |= NFS_CAP_ATOMIC_OPEN;
929
930 if (data->rsize)
931 server->rsize = nfs_block_size(data->rsize, NULL);
932 if (data->wsize)
933 server->wsize = nfs_block_size(data->wsize, NULL);
934
935 server->acregmin = data->acregmin * HZ;
936 server->acregmax = data->acregmax * HZ;
937 server->acdirmin = data->acdirmin * HZ;
938 server->acdirmax = data->acdirmax * HZ;
939
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -0400940 error = nfs_init_server_rpcclient(server, data->auth_flavors[0]);
David Howells54ceac42006-08-22 20:06:13 -0400941
942 /* Done */
943 dprintk("<-- nfs4_init_server() = %d\n", error);
944 return error;
945}
946
947/*
948 * Create a version 4 volume record
949 * - keyed on server and FSID
950 */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -0400951struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
David Howells54ceac42006-08-22 20:06:13 -0400952 struct nfs_fh *mntfh)
953{
954 struct nfs_fattr fattr;
955 struct nfs_server *server;
956 int error;
957
958 dprintk("--> nfs4_create_server()\n");
959
960 server = nfs_alloc_server();
961 if (!server)
962 return ERR_PTR(-ENOMEM);
963
964 /* Get a client record */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -0400965 error = nfs4_set_client(server,
966 data->nfs_server.hostname,
967 &data->nfs_server.address,
968 data->client_address,
969 data->auth_flavors[0],
970 data->nfs_server.protocol,
971 data->timeo, data->retrans);
David Howells54ceac42006-08-22 20:06:13 -0400972 if (error < 0)
973 goto error;
974
975 /* set up the general RPC client */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -0400976 error = nfs4_init_server(server, data);
David Howells54ceac42006-08-22 20:06:13 -0400977 if (error < 0)
978 goto error;
979
980 BUG_ON(!server->nfs_client);
981 BUG_ON(!server->nfs_client->rpc_ops);
982 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
983
984 /* Probe the root fh to retrieve its FSID */
\"Talpey, Thomas\91ea40b2007-09-10 13:44:33 -0400985 error = nfs4_path_walk(server, mntfh, data->nfs_server.export_path);
David Howells54ceac42006-08-22 20:06:13 -0400986 if (error < 0)
987 goto error;
988
David Howells6daabf12006-08-24 15:44:16 -0400989 dprintk("Server FSID: %llx:%llx\n",
990 (unsigned long long) server->fsid.major,
991 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -0400992 dprintk("Mount FH: %d\n", mntfh->size);
993
994 error = nfs_probe_fsinfo(server, mntfh, &fattr);
995 if (error < 0)
996 goto error;
997
Trond Myklebust54af3bb2007-09-28 12:27:41 -0400998 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
999 server->namelen = NFS4_MAXNAMLEN;
1000
David Howells54ceac42006-08-22 20:06:13 -04001001 BUG_ON(!server->nfs_client);
1002 BUG_ON(!server->nfs_client->rpc_ops);
1003 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1004
1005 spin_lock(&nfs_client_lock);
1006 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1007 list_add_tail(&server->master_link, &nfs_volume_list);
1008 spin_unlock(&nfs_client_lock);
1009
1010 server->mount_time = jiffies;
1011 dprintk("<-- nfs4_create_server() = %p\n", server);
1012 return server;
1013
1014error:
1015 nfs_free_server(server);
1016 dprintk("<-- nfs4_create_server() = error %d\n", error);
1017 return ERR_PTR(error);
1018}
1019
1020/*
1021 * Create an NFS4 referral server record
1022 */
1023struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001024 struct nfs_fh *mntfh)
David Howells54ceac42006-08-22 20:06:13 -04001025{
1026 struct nfs_client *parent_client;
1027 struct nfs_server *server, *parent_server;
1028 struct nfs_fattr fattr;
1029 int error;
1030
1031 dprintk("--> nfs4_create_referral_server()\n");
1032
1033 server = nfs_alloc_server();
1034 if (!server)
1035 return ERR_PTR(-ENOMEM);
1036
1037 parent_server = NFS_SB(data->sb);
1038 parent_client = parent_server->nfs_client;
1039
1040 /* Get a client representation.
1041 * Note: NFSv4 always uses TCP, */
1042 error = nfs4_set_client(server, data->hostname, data->addr,
J. Bruce Fields7d9ac062006-10-19 23:28:39 -07001043 parent_client->cl_ipaddr,
David Howells54ceac42006-08-22 20:06:13 -04001044 data->authflavor,
1045 parent_server->client->cl_xprt->prot,
1046 parent_client->retrans_timeo,
1047 parent_client->retrans_count);
andros@citi.umich.edu297de4f2006-08-29 12:19:41 -04001048 if (error < 0)
1049 goto error;
David Howells54ceac42006-08-22 20:06:13 -04001050
1051 /* Initialise the client representation from the parent server */
1052 nfs_server_copy_userdata(server, parent_server);
1053 server->caps |= NFS_CAP_ATOMIC_OPEN;
1054
1055 error = nfs_init_server_rpcclient(server, data->authflavor);
1056 if (error < 0)
1057 goto error;
1058
1059 BUG_ON(!server->nfs_client);
1060 BUG_ON(!server->nfs_client->rpc_ops);
1061 BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1062
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001063 /* Probe the root fh to retrieve its FSID and filehandle */
1064 error = nfs4_path_walk(server, mntfh, data->mnt_path);
1065 if (error < 0)
1066 goto error;
1067
David Howells54ceac42006-08-22 20:06:13 -04001068 /* probe the filesystem info for this server filesystem */
Trond Myklebustf2d0d852007-02-02 14:46:09 -08001069 error = nfs_probe_fsinfo(server, mntfh, &fattr);
David Howells54ceac42006-08-22 20:06:13 -04001070 if (error < 0)
1071 goto error;
1072
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001073 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1074 server->namelen = NFS4_MAXNAMLEN;
1075
David Howells54ceac42006-08-22 20:06:13 -04001076 dprintk("Referral FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001077 (unsigned long long) server->fsid.major,
1078 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001079
1080 spin_lock(&nfs_client_lock);
1081 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1082 list_add_tail(&server->master_link, &nfs_volume_list);
1083 spin_unlock(&nfs_client_lock);
1084
1085 server->mount_time = jiffies;
1086
1087 dprintk("<-- nfs_create_referral_server() = %p\n", server);
1088 return server;
1089
1090error:
1091 nfs_free_server(server);
1092 dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1093 return ERR_PTR(error);
1094}
1095
1096#endif /* CONFIG_NFS_V4 */
1097
1098/*
1099 * Clone an NFS2, NFS3 or NFS4 server record
1100 */
1101struct nfs_server *nfs_clone_server(struct nfs_server *source,
1102 struct nfs_fh *fh,
1103 struct nfs_fattr *fattr)
1104{
1105 struct nfs_server *server;
1106 struct nfs_fattr fattr_fsinfo;
1107 int error;
1108
1109 dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
David Howells6daabf12006-08-24 15:44:16 -04001110 (unsigned long long) fattr->fsid.major,
1111 (unsigned long long) fattr->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001112
1113 server = nfs_alloc_server();
1114 if (!server)
1115 return ERR_PTR(-ENOMEM);
1116
1117 /* Copy data from the source */
1118 server->nfs_client = source->nfs_client;
1119 atomic_inc(&server->nfs_client->cl_count);
1120 nfs_server_copy_userdata(server, source);
1121
1122 server->fsid = fattr->fsid;
1123
1124 error = nfs_init_server_rpcclient(server, source->client->cl_auth->au_flavor);
1125 if (error < 0)
1126 goto out_free_server;
1127 if (!IS_ERR(source->client_acl))
1128 nfs_init_server_aclclient(server);
1129
1130 /* probe the filesystem info for this server filesystem */
1131 error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo);
1132 if (error < 0)
1133 goto out_free_server;
1134
Trond Myklebust54af3bb2007-09-28 12:27:41 -04001135 if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1136 server->namelen = NFS4_MAXNAMLEN;
1137
David Howells54ceac42006-08-22 20:06:13 -04001138 dprintk("Cloned FSID: %llx:%llx\n",
David Howells6daabf12006-08-24 15:44:16 -04001139 (unsigned long long) server->fsid.major,
1140 (unsigned long long) server->fsid.minor);
David Howells54ceac42006-08-22 20:06:13 -04001141
1142 error = nfs_start_lockd(server);
1143 if (error < 0)
1144 goto out_free_server;
1145
1146 spin_lock(&nfs_client_lock);
1147 list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1148 list_add_tail(&server->master_link, &nfs_volume_list);
1149 spin_unlock(&nfs_client_lock);
1150
1151 server->mount_time = jiffies;
1152
1153 dprintk("<-- nfs_clone_server() = %p\n", server);
1154 return server;
1155
1156out_free_server:
1157 nfs_free_server(server);
1158 dprintk("<-- nfs_clone_server() = error %d\n", error);
1159 return ERR_PTR(error);
1160}
David Howells6aaca562006-08-22 20:06:13 -04001161
1162#ifdef CONFIG_PROC_FS
1163static struct proc_dir_entry *proc_fs_nfs;
1164
1165static int nfs_server_list_open(struct inode *inode, struct file *file);
1166static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1167static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1168static void nfs_server_list_stop(struct seq_file *p, void *v);
1169static int nfs_server_list_show(struct seq_file *m, void *v);
1170
1171static struct seq_operations nfs_server_list_ops = {
1172 .start = nfs_server_list_start,
1173 .next = nfs_server_list_next,
1174 .stop = nfs_server_list_stop,
1175 .show = nfs_server_list_show,
1176};
1177
Arjan van de Ven00977a52007-02-12 00:55:34 -08001178static const struct file_operations nfs_server_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001179 .open = nfs_server_list_open,
1180 .read = seq_read,
1181 .llseek = seq_lseek,
1182 .release = seq_release,
1183};
1184
1185static int nfs_volume_list_open(struct inode *inode, struct file *file);
1186static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1187static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1188static void nfs_volume_list_stop(struct seq_file *p, void *v);
1189static int nfs_volume_list_show(struct seq_file *m, void *v);
1190
1191static struct seq_operations nfs_volume_list_ops = {
1192 .start = nfs_volume_list_start,
1193 .next = nfs_volume_list_next,
1194 .stop = nfs_volume_list_stop,
1195 .show = nfs_volume_list_show,
1196};
1197
Arjan van de Ven00977a52007-02-12 00:55:34 -08001198static const struct file_operations nfs_volume_list_fops = {
David Howells6aaca562006-08-22 20:06:13 -04001199 .open = nfs_volume_list_open,
1200 .read = seq_read,
1201 .llseek = seq_lseek,
1202 .release = seq_release,
1203};
1204
1205/*
1206 * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1207 * we're dealing
1208 */
1209static int nfs_server_list_open(struct inode *inode, struct file *file)
1210{
1211 struct seq_file *m;
1212 int ret;
1213
1214 ret = seq_open(file, &nfs_server_list_ops);
1215 if (ret < 0)
1216 return ret;
1217
1218 m = file->private_data;
1219 m->private = PDE(inode)->data;
1220
1221 return 0;
1222}
1223
1224/*
1225 * set up the iterator to start reading from the server list and return the first item
1226 */
1227static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1228{
David Howells6aaca562006-08-22 20:06:13 -04001229 /* lock the list against modification */
1230 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001231 return seq_list_start_head(&nfs_client_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001232}
1233
1234/*
1235 * move to next server
1236 */
1237static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1238{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001239 return seq_list_next(v, &nfs_client_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001240}
1241
1242/*
1243 * clean up after reading from the transports list
1244 */
1245static void nfs_server_list_stop(struct seq_file *p, void *v)
1246{
1247 spin_unlock(&nfs_client_lock);
1248}
1249
1250/*
1251 * display a header line followed by a load of call lines
1252 */
1253static int nfs_server_list_show(struct seq_file *m, void *v)
1254{
1255 struct nfs_client *clp;
1256
1257 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001258 if (v == &nfs_client_list) {
David Howells6aaca562006-08-22 20:06:13 -04001259 seq_puts(m, "NV SERVER PORT USE HOSTNAME\n");
1260 return 0;
1261 }
1262
1263 /* display one transport per line on subsequent lines */
1264 clp = list_entry(v, struct nfs_client, cl_share_link);
1265
1266 seq_printf(m, "v%d %02x%02x%02x%02x %4hx %3d %s\n",
1267 clp->cl_nfsversion,
1268 NIPQUAD(clp->cl_addr.sin_addr),
1269 ntohs(clp->cl_addr.sin_port),
1270 atomic_read(&clp->cl_count),
1271 clp->cl_hostname);
1272
1273 return 0;
1274}
1275
1276/*
1277 * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1278 */
1279static int nfs_volume_list_open(struct inode *inode, struct file *file)
1280{
1281 struct seq_file *m;
1282 int ret;
1283
1284 ret = seq_open(file, &nfs_volume_list_ops);
1285 if (ret < 0)
1286 return ret;
1287
1288 m = file->private_data;
1289 m->private = PDE(inode)->data;
1290
1291 return 0;
1292}
1293
1294/*
1295 * set up the iterator to start reading from the volume list and return the first item
1296 */
1297static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1298{
David Howells6aaca562006-08-22 20:06:13 -04001299 /* lock the list against modification */
1300 spin_lock(&nfs_client_lock);
Pavel Emelianov259902e2007-07-15 23:39:56 -07001301 return seq_list_start_head(&nfs_volume_list, *_pos);
David Howells6aaca562006-08-22 20:06:13 -04001302}
1303
1304/*
1305 * move to next volume
1306 */
1307static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1308{
Pavel Emelianov259902e2007-07-15 23:39:56 -07001309 return seq_list_next(v, &nfs_volume_list, pos);
David Howells6aaca562006-08-22 20:06:13 -04001310}
1311
1312/*
1313 * clean up after reading from the transports list
1314 */
1315static void nfs_volume_list_stop(struct seq_file *p, void *v)
1316{
1317 spin_unlock(&nfs_client_lock);
1318}
1319
1320/*
1321 * display a header line followed by a load of call lines
1322 */
1323static int nfs_volume_list_show(struct seq_file *m, void *v)
1324{
1325 struct nfs_server *server;
1326 struct nfs_client *clp;
1327 char dev[8], fsid[17];
1328
1329 /* display header on line 1 */
Pavel Emelianov259902e2007-07-15 23:39:56 -07001330 if (v == &nfs_volume_list) {
David Howells6aaca562006-08-22 20:06:13 -04001331 seq_puts(m, "NV SERVER PORT DEV FSID\n");
1332 return 0;
1333 }
1334 /* display one transport per line on subsequent lines */
1335 server = list_entry(v, struct nfs_server, master_link);
1336 clp = server->nfs_client;
1337
1338 snprintf(dev, 8, "%u:%u",
1339 MAJOR(server->s_dev), MINOR(server->s_dev));
1340
1341 snprintf(fsid, 17, "%llx:%llx",
David Howells6daabf12006-08-24 15:44:16 -04001342 (unsigned long long) server->fsid.major,
1343 (unsigned long long) server->fsid.minor);
David Howells6aaca562006-08-22 20:06:13 -04001344
1345 seq_printf(m, "v%d %02x%02x%02x%02x %4hx %-7s %-17s\n",
1346 clp->cl_nfsversion,
1347 NIPQUAD(clp->cl_addr.sin_addr),
1348 ntohs(clp->cl_addr.sin_port),
1349 dev,
1350 fsid);
1351
1352 return 0;
1353}
1354
1355/*
1356 * initialise the /proc/fs/nfsfs/ directory
1357 */
1358int __init nfs_fs_proc_init(void)
1359{
1360 struct proc_dir_entry *p;
1361
1362 proc_fs_nfs = proc_mkdir("nfsfs", proc_root_fs);
1363 if (!proc_fs_nfs)
1364 goto error_0;
1365
1366 proc_fs_nfs->owner = THIS_MODULE;
1367
1368 /* a file of servers with which we're dealing */
1369 p = create_proc_entry("servers", S_IFREG|S_IRUGO, proc_fs_nfs);
1370 if (!p)
1371 goto error_1;
1372
1373 p->proc_fops = &nfs_server_list_fops;
1374 p->owner = THIS_MODULE;
1375
1376 /* a file of volumes that we have mounted */
1377 p = create_proc_entry("volumes", S_IFREG|S_IRUGO, proc_fs_nfs);
1378 if (!p)
1379 goto error_2;
1380
1381 p->proc_fops = &nfs_volume_list_fops;
1382 p->owner = THIS_MODULE;
1383 return 0;
1384
1385error_2:
1386 remove_proc_entry("servers", proc_fs_nfs);
1387error_1:
1388 remove_proc_entry("nfsfs", proc_root_fs);
1389error_0:
1390 return -ENOMEM;
1391}
1392
1393/*
1394 * clean up the /proc/fs/nfsfs/ directory
1395 */
1396void nfs_fs_proc_exit(void)
1397{
1398 remove_proc_entry("volumes", proc_fs_nfs);
1399 remove_proc_entry("servers", proc_fs_nfs);
1400 remove_proc_entry("nfsfs", proc_root_fs);
1401}
1402
1403#endif /* CONFIG_PROC_FS */