xref: /openbmc/linux/fs/nfs/client.c (revision 367b8112)
1 /* 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 
13 #include <linux/module.h>
14 #include <linux/init.h>
15 #include <linux/sched.h>
16 #include <linux/time.h>
17 #include <linux/kernel.h>
18 #include <linux/mm.h>
19 #include <linux/string.h>
20 #include <linux/stat.h>
21 #include <linux/errno.h>
22 #include <linux/unistd.h>
23 #include <linux/sunrpc/clnt.h>
24 #include <linux/sunrpc/stats.h>
25 #include <linux/sunrpc/metrics.h>
26 #include <linux/sunrpc/xprtsock.h>
27 #include <linux/sunrpc/xprtrdma.h>
28 #include <linux/nfs_fs.h>
29 #include <linux/nfs_mount.h>
30 #include <linux/nfs4_mount.h>
31 #include <linux/lockd/bind.h>
32 #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/in6.h>
38 #include <net/ipv6.h>
39 #include <linux/nfs_xdr.h>
40 
41 #include <asm/system.h>
42 
43 #include "nfs4_fs.h"
44 #include "callback.h"
45 #include "delegation.h"
46 #include "iostat.h"
47 #include "internal.h"
48 
49 #define NFSDBG_FACILITY		NFSDBG_CLIENT
50 
51 static DEFINE_SPINLOCK(nfs_client_lock);
52 static LIST_HEAD(nfs_client_list);
53 static LIST_HEAD(nfs_volume_list);
54 static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
55 
56 /*
57  * RPC cruft for NFS
58  */
59 static struct rpc_version *nfs_version[5] = {
60 	[2]			= &nfs_version2,
61 #ifdef CONFIG_NFS_V3
62 	[3]			= &nfs_version3,
63 #endif
64 #ifdef CONFIG_NFS_V4
65 	[4]			= &nfs_version4,
66 #endif
67 };
68 
69 struct rpc_program nfs_program = {
70 	.name			= "nfs",
71 	.number			= NFS_PROGRAM,
72 	.nrvers			= ARRAY_SIZE(nfs_version),
73 	.version		= nfs_version,
74 	.stats			= &nfs_rpcstat,
75 	.pipe_dir_name		= "/nfs",
76 };
77 
78 struct rpc_stat nfs_rpcstat = {
79 	.program		= &nfs_program
80 };
81 
82 
83 #ifdef CONFIG_NFS_V3_ACL
84 static struct rpc_stat		nfsacl_rpcstat = { &nfsacl_program };
85 static struct rpc_version *	nfsacl_version[] = {
86 	[3]			= &nfsacl_version3,
87 };
88 
89 struct rpc_program		nfsacl_program = {
90 	.name			= "nfsacl",
91 	.number			= NFS_ACL_PROGRAM,
92 	.nrvers			= ARRAY_SIZE(nfsacl_version),
93 	.version		= nfsacl_version,
94 	.stats			= &nfsacl_rpcstat,
95 };
96 #endif  /* CONFIG_NFS_V3_ACL */
97 
98 struct nfs_client_initdata {
99 	const char *hostname;
100 	const struct sockaddr *addr;
101 	size_t addrlen;
102 	const struct nfs_rpc_ops *rpc_ops;
103 	int proto;
104 };
105 
106 /*
107  * Allocate a shared client record
108  *
109  * Since these are allocated/deallocated very rarely, we don't
110  * bother putting them in a slab cache...
111  */
112 static struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
113 {
114 	struct nfs_client *clp;
115 	struct rpc_cred *cred;
116 
117 	if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
118 		goto error_0;
119 
120 	clp->rpc_ops = cl_init->rpc_ops;
121 
122 	if (cl_init->rpc_ops->version == 4) {
123 		if (nfs_callback_up() < 0)
124 			goto error_2;
125 		__set_bit(NFS_CS_CALLBACK, &clp->cl_res_state);
126 	}
127 
128 	atomic_set(&clp->cl_count, 1);
129 	clp->cl_cons_state = NFS_CS_INITING;
130 
131 	memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
132 	clp->cl_addrlen = cl_init->addrlen;
133 
134 	if (cl_init->hostname) {
135 		clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
136 		if (!clp->cl_hostname)
137 			goto error_3;
138 	}
139 
140 	INIT_LIST_HEAD(&clp->cl_superblocks);
141 	clp->cl_rpcclient = ERR_PTR(-EINVAL);
142 
143 	clp->cl_proto = cl_init->proto;
144 
145 #ifdef CONFIG_NFS_V4
146 	init_rwsem(&clp->cl_sem);
147 	INIT_LIST_HEAD(&clp->cl_delegations);
148 	spin_lock_init(&clp->cl_lock);
149 	INIT_DELAYED_WORK(&clp->cl_renewd, nfs4_renew_state);
150 	rpc_init_wait_queue(&clp->cl_rpcwaitq, "NFS client");
151 	clp->cl_boot_time = CURRENT_TIME;
152 	clp->cl_state = 1 << NFS4CLNT_LEASE_EXPIRED;
153 #endif
154 	cred = rpc_lookup_machine_cred();
155 	if (!IS_ERR(cred))
156 		clp->cl_machine_cred = cred;
157 
158 	return clp;
159 
160 error_3:
161 	if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
162 		nfs_callback_down();
163 error_2:
164 	kfree(clp);
165 error_0:
166 	return NULL;
167 }
168 
169 static void nfs4_shutdown_client(struct nfs_client *clp)
170 {
171 #ifdef CONFIG_NFS_V4
172 	if (__test_and_clear_bit(NFS_CS_RENEWD, &clp->cl_res_state))
173 		nfs4_kill_renewd(clp);
174 	BUG_ON(!RB_EMPTY_ROOT(&clp->cl_state_owners));
175 	if (__test_and_clear_bit(NFS_CS_IDMAP, &clp->cl_res_state))
176 		nfs_idmap_delete(clp);
177 
178 	rpc_destroy_wait_queue(&clp->cl_rpcwaitq);
179 #endif
180 }
181 
182 /*
183  * Destroy a shared client record
184  */
185 static void nfs_free_client(struct nfs_client *clp)
186 {
187 	dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
188 
189 	nfs4_shutdown_client(clp);
190 
191 	/* -EIO all pending I/O */
192 	if (!IS_ERR(clp->cl_rpcclient))
193 		rpc_shutdown_client(clp->cl_rpcclient);
194 
195 	if (__test_and_clear_bit(NFS_CS_CALLBACK, &clp->cl_res_state))
196 		nfs_callback_down();
197 
198 	if (clp->cl_machine_cred != NULL)
199 		put_rpccred(clp->cl_machine_cred);
200 
201 	kfree(clp->cl_hostname);
202 	kfree(clp);
203 
204 	dprintk("<-- nfs_free_client()\n");
205 }
206 
207 /*
208  * Release a reference to a shared client record
209  */
210 void nfs_put_client(struct nfs_client *clp)
211 {
212 	if (!clp)
213 		return;
214 
215 	dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
216 
217 	if (atomic_dec_and_lock(&clp->cl_count, &nfs_client_lock)) {
218 		list_del(&clp->cl_share_link);
219 		spin_unlock(&nfs_client_lock);
220 
221 		BUG_ON(!list_empty(&clp->cl_superblocks));
222 
223 		nfs_free_client(clp);
224 	}
225 }
226 
227 static int nfs_sockaddr_match_ipaddr4(const struct sockaddr_in *sa1,
228 				 const struct sockaddr_in *sa2)
229 {
230 	return sa1->sin_addr.s_addr == sa2->sin_addr.s_addr;
231 }
232 
233 static int nfs_sockaddr_match_ipaddr6(const struct sockaddr_in6 *sa1,
234 				 const struct sockaddr_in6 *sa2)
235 {
236 	return ipv6_addr_equal(&sa1->sin6_addr, &sa2->sin6_addr);
237 }
238 
239 static int nfs_sockaddr_match_ipaddr(const struct sockaddr *sa1,
240 				 const struct sockaddr *sa2)
241 {
242 	switch (sa1->sa_family) {
243 	case AF_INET:
244 		return nfs_sockaddr_match_ipaddr4((const struct sockaddr_in *)sa1,
245 				(const struct sockaddr_in *)sa2);
246 	case AF_INET6:
247 		return nfs_sockaddr_match_ipaddr6((const struct sockaddr_in6 *)sa1,
248 				(const struct sockaddr_in6 *)sa2);
249 	}
250 	BUG();
251 }
252 
253 /*
254  * Find a client by IP address and protocol version
255  * - returns NULL if no such client
256  */
257 struct nfs_client *nfs_find_client(const struct sockaddr *addr, u32 nfsversion)
258 {
259 	struct nfs_client *clp;
260 
261 	spin_lock(&nfs_client_lock);
262 	list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
263 		struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
264 
265 		/* Don't match clients that failed to initialise properly */
266 		if (clp->cl_cons_state != NFS_CS_READY)
267 			continue;
268 
269 		/* Different NFS versions cannot share the same nfs_client */
270 		if (clp->rpc_ops->version != nfsversion)
271 			continue;
272 
273 		if (addr->sa_family != clap->sa_family)
274 			continue;
275 		/* Match only the IP address, not the port number */
276 		if (!nfs_sockaddr_match_ipaddr(addr, clap))
277 			continue;
278 
279 		atomic_inc(&clp->cl_count);
280 		spin_unlock(&nfs_client_lock);
281 		return clp;
282 	}
283 	spin_unlock(&nfs_client_lock);
284 	return NULL;
285 }
286 
287 /*
288  * Find a client by IP address and protocol version
289  * - returns NULL if no such client
290  */
291 struct nfs_client *nfs_find_client_next(struct nfs_client *clp)
292 {
293 	struct sockaddr *sap = (struct sockaddr *)&clp->cl_addr;
294 	u32 nfsvers = clp->rpc_ops->version;
295 
296 	spin_lock(&nfs_client_lock);
297 	list_for_each_entry_continue(clp, &nfs_client_list, cl_share_link) {
298 		struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
299 
300 		/* Don't match clients that failed to initialise properly */
301 		if (clp->cl_cons_state != NFS_CS_READY)
302 			continue;
303 
304 		/* Different NFS versions cannot share the same nfs_client */
305 		if (clp->rpc_ops->version != nfsvers)
306 			continue;
307 
308 		if (sap->sa_family != clap->sa_family)
309 			continue;
310 		/* Match only the IP address, not the port number */
311 		if (!nfs_sockaddr_match_ipaddr(sap, clap))
312 			continue;
313 
314 		atomic_inc(&clp->cl_count);
315 		spin_unlock(&nfs_client_lock);
316 		return clp;
317 	}
318 	spin_unlock(&nfs_client_lock);
319 	return NULL;
320 }
321 
322 /*
323  * Find an nfs_client on the list that matches the initialisation data
324  * that is supplied.
325  */
326 static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
327 {
328 	struct nfs_client *clp;
329 
330 	list_for_each_entry(clp, &nfs_client_list, cl_share_link) {
331 		/* Don't match clients that failed to initialise properly */
332 		if (clp->cl_cons_state < 0)
333 			continue;
334 
335 		/* Different NFS versions cannot share the same nfs_client */
336 		if (clp->rpc_ops != data->rpc_ops)
337 			continue;
338 
339 		if (clp->cl_proto != data->proto)
340 			continue;
341 
342 		/* Match the full socket address */
343 		if (memcmp(&clp->cl_addr, data->addr, sizeof(clp->cl_addr)) != 0)
344 			continue;
345 
346 		atomic_inc(&clp->cl_count);
347 		return clp;
348 	}
349 	return NULL;
350 }
351 
352 /*
353  * Look up a client by IP address and protocol version
354  * - creates a new record if one doesn't yet exist
355  */
356 static struct nfs_client *nfs_get_client(const struct nfs_client_initdata *cl_init)
357 {
358 	struct nfs_client *clp, *new = NULL;
359 	int error;
360 
361 	dprintk("--> nfs_get_client(%s,v%u)\n",
362 		cl_init->hostname ?: "", cl_init->rpc_ops->version);
363 
364 	/* see if the client already exists */
365 	do {
366 		spin_lock(&nfs_client_lock);
367 
368 		clp = nfs_match_client(cl_init);
369 		if (clp)
370 			goto found_client;
371 		if (new)
372 			goto install_client;
373 
374 		spin_unlock(&nfs_client_lock);
375 
376 		new = nfs_alloc_client(cl_init);
377 	} while (new);
378 
379 	return ERR_PTR(-ENOMEM);
380 
381 	/* install a new client and return with it unready */
382 install_client:
383 	clp = new;
384 	list_add(&clp->cl_share_link, &nfs_client_list);
385 	spin_unlock(&nfs_client_lock);
386 	dprintk("--> nfs_get_client() = %p [new]\n", clp);
387 	return clp;
388 
389 	/* found an existing client
390 	 * - make sure it's ready before returning
391 	 */
392 found_client:
393 	spin_unlock(&nfs_client_lock);
394 
395 	if (new)
396 		nfs_free_client(new);
397 
398 	error = wait_event_killable(nfs_client_active_wq,
399 				clp->cl_cons_state != NFS_CS_INITING);
400 	if (error < 0) {
401 		nfs_put_client(clp);
402 		return ERR_PTR(-ERESTARTSYS);
403 	}
404 
405 	if (clp->cl_cons_state < NFS_CS_READY) {
406 		error = clp->cl_cons_state;
407 		nfs_put_client(clp);
408 		return ERR_PTR(error);
409 	}
410 
411 	BUG_ON(clp->cl_cons_state != NFS_CS_READY);
412 
413 	dprintk("--> nfs_get_client() = %p [share]\n", clp);
414 	return clp;
415 }
416 
417 /*
418  * Mark a server as ready or failed
419  */
420 static void nfs_mark_client_ready(struct nfs_client *clp, int state)
421 {
422 	clp->cl_cons_state = state;
423 	wake_up_all(&nfs_client_active_wq);
424 }
425 
426 /*
427  * Initialise the timeout values for a connection
428  */
429 static void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
430 				    unsigned int timeo, unsigned int retrans)
431 {
432 	to->to_initval = timeo * HZ / 10;
433 	to->to_retries = retrans;
434 
435 	switch (proto) {
436 	case XPRT_TRANSPORT_TCP:
437 	case XPRT_TRANSPORT_RDMA:
438 		if (to->to_retries == 0)
439 			to->to_retries = NFS_DEF_TCP_RETRANS;
440 		if (to->to_initval == 0)
441 			to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
442 		if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
443 			to->to_initval = NFS_MAX_TCP_TIMEOUT;
444 		to->to_increment = to->to_initval;
445 		to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
446 		if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
447 			to->to_maxval = NFS_MAX_TCP_TIMEOUT;
448 		if (to->to_maxval < to->to_initval)
449 			to->to_maxval = to->to_initval;
450 		to->to_exponential = 0;
451 		break;
452 	case XPRT_TRANSPORT_UDP:
453 		if (to->to_retries == 0)
454 			to->to_retries = NFS_DEF_UDP_RETRANS;
455 		if (!to->to_initval)
456 			to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
457 		if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
458 			to->to_initval = NFS_MAX_UDP_TIMEOUT;
459 		to->to_maxval = NFS_MAX_UDP_TIMEOUT;
460 		to->to_exponential = 1;
461 		break;
462 	default:
463 		BUG();
464 	}
465 }
466 
467 /*
468  * Create an RPC client handle
469  */
470 static int nfs_create_rpc_client(struct nfs_client *clp,
471 				 const struct rpc_timeout *timeparms,
472 				 rpc_authflavor_t flavor,
473 				 int flags)
474 {
475 	struct rpc_clnt		*clnt = NULL;
476 	struct rpc_create_args args = {
477 		.protocol	= clp->cl_proto,
478 		.address	= (struct sockaddr *)&clp->cl_addr,
479 		.addrsize	= clp->cl_addrlen,
480 		.timeout	= timeparms,
481 		.servername	= clp->cl_hostname,
482 		.program	= &nfs_program,
483 		.version	= clp->rpc_ops->version,
484 		.authflavor	= flavor,
485 		.flags		= flags,
486 	};
487 
488 	if (!IS_ERR(clp->cl_rpcclient))
489 		return 0;
490 
491 	clnt = rpc_create(&args);
492 	if (IS_ERR(clnt)) {
493 		dprintk("%s: cannot create RPC client. Error = %ld\n",
494 				__func__, PTR_ERR(clnt));
495 		return PTR_ERR(clnt);
496 	}
497 
498 	clp->cl_rpcclient = clnt;
499 	return 0;
500 }
501 
502 /*
503  * Version 2 or 3 client destruction
504  */
505 static void nfs_destroy_server(struct nfs_server *server)
506 {
507 	if (!(server->flags & NFS_MOUNT_NONLM))
508 		nlmclnt_done(server->nlm_host);
509 }
510 
511 /*
512  * Version 2 or 3 lockd setup
513  */
514 static int nfs_start_lockd(struct nfs_server *server)
515 {
516 	struct nlm_host *host;
517 	struct nfs_client *clp = server->nfs_client;
518 	struct nlmclnt_initdata nlm_init = {
519 		.hostname	= clp->cl_hostname,
520 		.address	= (struct sockaddr *)&clp->cl_addr,
521 		.addrlen	= clp->cl_addrlen,
522 		.protocol	= server->flags & NFS_MOUNT_TCP ?
523 						IPPROTO_TCP : IPPROTO_UDP,
524 		.nfs_version	= clp->rpc_ops->version,
525 	};
526 
527 	if (nlm_init.nfs_version > 3)
528 		return 0;
529 	if (server->flags & NFS_MOUNT_NONLM)
530 		return 0;
531 
532 	host = nlmclnt_init(&nlm_init);
533 	if (IS_ERR(host))
534 		return PTR_ERR(host);
535 
536 	server->nlm_host = host;
537 	server->destroy = nfs_destroy_server;
538 	return 0;
539 }
540 
541 /*
542  * Initialise an NFSv3 ACL client connection
543  */
544 #ifdef CONFIG_NFS_V3_ACL
545 static void nfs_init_server_aclclient(struct nfs_server *server)
546 {
547 	if (server->nfs_client->rpc_ops->version != 3)
548 		goto out_noacl;
549 	if (server->flags & NFS_MOUNT_NOACL)
550 		goto out_noacl;
551 
552 	server->client_acl = rpc_bind_new_program(server->client, &nfsacl_program, 3);
553 	if (IS_ERR(server->client_acl))
554 		goto out_noacl;
555 
556 	/* No errors! Assume that Sun nfsacls are supported */
557 	server->caps |= NFS_CAP_ACLS;
558 	return;
559 
560 out_noacl:
561 	server->caps &= ~NFS_CAP_ACLS;
562 }
563 #else
564 static inline void nfs_init_server_aclclient(struct nfs_server *server)
565 {
566 	server->flags &= ~NFS_MOUNT_NOACL;
567 	server->caps &= ~NFS_CAP_ACLS;
568 }
569 #endif
570 
571 /*
572  * Create a general RPC client
573  */
574 static int nfs_init_server_rpcclient(struct nfs_server *server,
575 		const struct rpc_timeout *timeo,
576 		rpc_authflavor_t pseudoflavour)
577 {
578 	struct nfs_client *clp = server->nfs_client;
579 
580 	server->client = rpc_clone_client(clp->cl_rpcclient);
581 	if (IS_ERR(server->client)) {
582 		dprintk("%s: couldn't create rpc_client!\n", __func__);
583 		return PTR_ERR(server->client);
584 	}
585 
586 	memcpy(&server->client->cl_timeout_default,
587 			timeo,
588 			sizeof(server->client->cl_timeout_default));
589 	server->client->cl_timeout = &server->client->cl_timeout_default;
590 
591 	if (pseudoflavour != clp->cl_rpcclient->cl_auth->au_flavor) {
592 		struct rpc_auth *auth;
593 
594 		auth = rpcauth_create(pseudoflavour, server->client);
595 		if (IS_ERR(auth)) {
596 			dprintk("%s: couldn't create credcache!\n", __func__);
597 			return PTR_ERR(auth);
598 		}
599 	}
600 	server->client->cl_softrtry = 0;
601 	if (server->flags & NFS_MOUNT_SOFT)
602 		server->client->cl_softrtry = 1;
603 
604 	return 0;
605 }
606 
607 /*
608  * Initialise an NFS2 or NFS3 client
609  */
610 static int nfs_init_client(struct nfs_client *clp,
611 			   const struct rpc_timeout *timeparms,
612 			   const struct nfs_parsed_mount_data *data)
613 {
614 	int error;
615 
616 	if (clp->cl_cons_state == NFS_CS_READY) {
617 		/* the client is already initialised */
618 		dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
619 		return 0;
620 	}
621 
622 	/*
623 	 * Create a client RPC handle for doing FSSTAT with UNIX auth only
624 	 * - RFC 2623, sec 2.3.2
625 	 */
626 	error = nfs_create_rpc_client(clp, timeparms, RPC_AUTH_UNIX, 0);
627 	if (error < 0)
628 		goto error;
629 	nfs_mark_client_ready(clp, NFS_CS_READY);
630 	return 0;
631 
632 error:
633 	nfs_mark_client_ready(clp, error);
634 	dprintk("<-- nfs_init_client() = xerror %d\n", error);
635 	return error;
636 }
637 
638 /*
639  * Create a version 2 or 3 client
640  */
641 static int nfs_init_server(struct nfs_server *server,
642 			   const struct nfs_parsed_mount_data *data)
643 {
644 	struct nfs_client_initdata cl_init = {
645 		.hostname = data->nfs_server.hostname,
646 		.addr = (const struct sockaddr *)&data->nfs_server.address,
647 		.addrlen = data->nfs_server.addrlen,
648 		.rpc_ops = &nfs_v2_clientops,
649 		.proto = data->nfs_server.protocol,
650 	};
651 	struct rpc_timeout timeparms;
652 	struct nfs_client *clp;
653 	int error;
654 
655 	dprintk("--> nfs_init_server()\n");
656 
657 #ifdef CONFIG_NFS_V3
658 	if (data->flags & NFS_MOUNT_VER3)
659 		cl_init.rpc_ops = &nfs_v3_clientops;
660 #endif
661 
662 	/* Allocate or find a client reference we can use */
663 	clp = nfs_get_client(&cl_init);
664 	if (IS_ERR(clp)) {
665 		dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
666 		return PTR_ERR(clp);
667 	}
668 
669 	nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
670 			data->timeo, data->retrans);
671 	error = nfs_init_client(clp, &timeparms, data);
672 	if (error < 0)
673 		goto error;
674 
675 	server->nfs_client = clp;
676 
677 	/* Initialise the client representation from the mount data */
678 	server->flags = data->flags;
679 
680 	if (data->rsize)
681 		server->rsize = nfs_block_size(data->rsize, NULL);
682 	if (data->wsize)
683 		server->wsize = nfs_block_size(data->wsize, NULL);
684 
685 	server->acregmin = data->acregmin * HZ;
686 	server->acregmax = data->acregmax * HZ;
687 	server->acdirmin = data->acdirmin * HZ;
688 	server->acdirmax = data->acdirmax * HZ;
689 
690 	/* Start lockd here, before we might error out */
691 	error = nfs_start_lockd(server);
692 	if (error < 0)
693 		goto error;
694 
695 	server->port = data->nfs_server.port;
696 
697 	error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
698 	if (error < 0)
699 		goto error;
700 
701 	/* Preserve the values of mount_server-related mount options */
702 	if (data->mount_server.addrlen) {
703 		memcpy(&server->mountd_address, &data->mount_server.address,
704 			data->mount_server.addrlen);
705 		server->mountd_addrlen = data->mount_server.addrlen;
706 	}
707 	server->mountd_version = data->mount_server.version;
708 	server->mountd_port = data->mount_server.port;
709 	server->mountd_protocol = data->mount_server.protocol;
710 
711 	server->namelen  = data->namlen;
712 	/* Create a client RPC handle for the NFSv3 ACL management interface */
713 	nfs_init_server_aclclient(server);
714 	dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
715 	return 0;
716 
717 error:
718 	server->nfs_client = NULL;
719 	nfs_put_client(clp);
720 	dprintk("<-- nfs_init_server() = xerror %d\n", error);
721 	return error;
722 }
723 
724 /*
725  * Load up the server record from information gained in an fsinfo record
726  */
727 static void nfs_server_set_fsinfo(struct nfs_server *server, struct nfs_fsinfo *fsinfo)
728 {
729 	unsigned long max_rpc_payload;
730 
731 	/* Work out a lot of parameters */
732 	if (server->rsize == 0)
733 		server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
734 	if (server->wsize == 0)
735 		server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
736 
737 	if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
738 		server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
739 	if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
740 		server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
741 
742 	max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
743 	if (server->rsize > max_rpc_payload)
744 		server->rsize = max_rpc_payload;
745 	if (server->rsize > NFS_MAX_FILE_IO_SIZE)
746 		server->rsize = NFS_MAX_FILE_IO_SIZE;
747 	server->rpages = (server->rsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
748 
749 	server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
750 
751 	if (server->wsize > max_rpc_payload)
752 		server->wsize = max_rpc_payload;
753 	if (server->wsize > NFS_MAX_FILE_IO_SIZE)
754 		server->wsize = NFS_MAX_FILE_IO_SIZE;
755 	server->wpages = (server->wsize + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
756 	server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
757 
758 	server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
759 	if (server->dtsize > PAGE_CACHE_SIZE)
760 		server->dtsize = PAGE_CACHE_SIZE;
761 	if (server->dtsize > server->rsize)
762 		server->dtsize = server->rsize;
763 
764 	if (server->flags & NFS_MOUNT_NOAC) {
765 		server->acregmin = server->acregmax = 0;
766 		server->acdirmin = server->acdirmax = 0;
767 	}
768 
769 	server->maxfilesize = fsinfo->maxfilesize;
770 
771 	/* We're airborne Set socket buffersize */
772 	rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
773 }
774 
775 /*
776  * Probe filesystem information, including the FSID on v2/v3
777  */
778 static int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
779 {
780 	struct nfs_fsinfo fsinfo;
781 	struct nfs_client *clp = server->nfs_client;
782 	int error;
783 
784 	dprintk("--> nfs_probe_fsinfo()\n");
785 
786 	if (clp->rpc_ops->set_capabilities != NULL) {
787 		error = clp->rpc_ops->set_capabilities(server, mntfh);
788 		if (error < 0)
789 			goto out_error;
790 	}
791 
792 	fsinfo.fattr = fattr;
793 	nfs_fattr_init(fattr);
794 	error = clp->rpc_ops->fsinfo(server, mntfh, &fsinfo);
795 	if (error < 0)
796 		goto out_error;
797 
798 	nfs_server_set_fsinfo(server, &fsinfo);
799 	error = bdi_init(&server->backing_dev_info);
800 	if (error)
801 		goto out_error;
802 
803 
804 	/* Get some general file system info */
805 	if (server->namelen == 0) {
806 		struct nfs_pathconf pathinfo;
807 
808 		pathinfo.fattr = fattr;
809 		nfs_fattr_init(fattr);
810 
811 		if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
812 			server->namelen = pathinfo.max_namelen;
813 	}
814 
815 	dprintk("<-- nfs_probe_fsinfo() = 0\n");
816 	return 0;
817 
818 out_error:
819 	dprintk("nfs_probe_fsinfo: error = %d\n", -error);
820 	return error;
821 }
822 
823 /*
824  * Copy useful information when duplicating a server record
825  */
826 static void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
827 {
828 	target->flags = source->flags;
829 	target->acregmin = source->acregmin;
830 	target->acregmax = source->acregmax;
831 	target->acdirmin = source->acdirmin;
832 	target->acdirmax = source->acdirmax;
833 	target->caps = source->caps;
834 }
835 
836 /*
837  * Allocate and initialise a server record
838  */
839 static struct nfs_server *nfs_alloc_server(void)
840 {
841 	struct nfs_server *server;
842 
843 	server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
844 	if (!server)
845 		return NULL;
846 
847 	server->client = server->client_acl = ERR_PTR(-EINVAL);
848 
849 	/* Zero out the NFS state stuff */
850 	INIT_LIST_HEAD(&server->client_link);
851 	INIT_LIST_HEAD(&server->master_link);
852 
853 	atomic_set(&server->active, 0);
854 
855 	server->io_stats = nfs_alloc_iostats();
856 	if (!server->io_stats) {
857 		kfree(server);
858 		return NULL;
859 	}
860 
861 	return server;
862 }
863 
864 /*
865  * Free up a server record
866  */
867 void nfs_free_server(struct nfs_server *server)
868 {
869 	dprintk("--> nfs_free_server()\n");
870 
871 	spin_lock(&nfs_client_lock);
872 	list_del(&server->client_link);
873 	list_del(&server->master_link);
874 	spin_unlock(&nfs_client_lock);
875 
876 	if (server->destroy != NULL)
877 		server->destroy(server);
878 
879 	if (!IS_ERR(server->client_acl))
880 		rpc_shutdown_client(server->client_acl);
881 	if (!IS_ERR(server->client))
882 		rpc_shutdown_client(server->client);
883 
884 	nfs_put_client(server->nfs_client);
885 
886 	nfs_free_iostats(server->io_stats);
887 	bdi_destroy(&server->backing_dev_info);
888 	kfree(server);
889 	nfs_release_automount_timer();
890 	dprintk("<-- nfs_free_server()\n");
891 }
892 
893 /*
894  * Create a version 2 or 3 volume record
895  * - keyed on server and FSID
896  */
897 struct nfs_server *nfs_create_server(const struct nfs_parsed_mount_data *data,
898 				     struct nfs_fh *mntfh)
899 {
900 	struct nfs_server *server;
901 	struct nfs_fattr fattr;
902 	int error;
903 
904 	server = nfs_alloc_server();
905 	if (!server)
906 		return ERR_PTR(-ENOMEM);
907 
908 	/* Get a client representation */
909 	error = nfs_init_server(server, data);
910 	if (error < 0)
911 		goto error;
912 
913 	BUG_ON(!server->nfs_client);
914 	BUG_ON(!server->nfs_client->rpc_ops);
915 	BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
916 
917 	/* Probe the root fh to retrieve its FSID */
918 	error = nfs_probe_fsinfo(server, mntfh, &fattr);
919 	if (error < 0)
920 		goto error;
921 	if (server->nfs_client->rpc_ops->version == 3) {
922 		if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
923 			server->namelen = NFS3_MAXNAMLEN;
924 		if (!(data->flags & NFS_MOUNT_NORDIRPLUS))
925 			server->caps |= NFS_CAP_READDIRPLUS;
926 	} else {
927 		if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
928 			server->namelen = NFS2_MAXNAMLEN;
929 	}
930 
931 	if (!(fattr.valid & NFS_ATTR_FATTR)) {
932 		error = server->nfs_client->rpc_ops->getattr(server, mntfh, &fattr);
933 		if (error < 0) {
934 			dprintk("nfs_create_server: getattr error = %d\n", -error);
935 			goto error;
936 		}
937 	}
938 	memcpy(&server->fsid, &fattr.fsid, sizeof(server->fsid));
939 
940 	dprintk("Server FSID: %llx:%llx\n",
941 		(unsigned long long) server->fsid.major,
942 		(unsigned long long) server->fsid.minor);
943 
944 	BUG_ON(!server->nfs_client);
945 	BUG_ON(!server->nfs_client->rpc_ops);
946 	BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
947 
948 	spin_lock(&nfs_client_lock);
949 	list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
950 	list_add_tail(&server->master_link, &nfs_volume_list);
951 	spin_unlock(&nfs_client_lock);
952 
953 	server->mount_time = jiffies;
954 	return server;
955 
956 error:
957 	nfs_free_server(server);
958 	return ERR_PTR(error);
959 }
960 
961 #ifdef CONFIG_NFS_V4
962 /*
963  * Initialise an NFS4 client record
964  */
965 static int nfs4_init_client(struct nfs_client *clp,
966 		const struct rpc_timeout *timeparms,
967 		const char *ip_addr,
968 		rpc_authflavor_t authflavour)
969 {
970 	int error;
971 
972 	if (clp->cl_cons_state == NFS_CS_READY) {
973 		/* the client is initialised already */
974 		dprintk("<-- nfs4_init_client() = 0 [already %p]\n", clp);
975 		return 0;
976 	}
977 
978 	/* Check NFS protocol revision and initialize RPC op vector */
979 	clp->rpc_ops = &nfs_v4_clientops;
980 
981 	error = nfs_create_rpc_client(clp, timeparms, authflavour,
982 					RPC_CLNT_CREATE_DISCRTRY);
983 	if (error < 0)
984 		goto error;
985 	memcpy(clp->cl_ipaddr, ip_addr, sizeof(clp->cl_ipaddr));
986 
987 	error = nfs_idmap_new(clp);
988 	if (error < 0) {
989 		dprintk("%s: failed to create idmapper. Error = %d\n",
990 			__func__, error);
991 		goto error;
992 	}
993 	__set_bit(NFS_CS_IDMAP, &clp->cl_res_state);
994 
995 	nfs_mark_client_ready(clp, NFS_CS_READY);
996 	return 0;
997 
998 error:
999 	nfs_mark_client_ready(clp, error);
1000 	dprintk("<-- nfs4_init_client() = xerror %d\n", error);
1001 	return error;
1002 }
1003 
1004 /*
1005  * Set up an NFS4 client
1006  */
1007 static int nfs4_set_client(struct nfs_server *server,
1008 		const char *hostname,
1009 		const struct sockaddr *addr,
1010 		const size_t addrlen,
1011 		const char *ip_addr,
1012 		rpc_authflavor_t authflavour,
1013 		int proto, const struct rpc_timeout *timeparms)
1014 {
1015 	struct nfs_client_initdata cl_init = {
1016 		.hostname = hostname,
1017 		.addr = addr,
1018 		.addrlen = addrlen,
1019 		.rpc_ops = &nfs_v4_clientops,
1020 		.proto = proto,
1021 	};
1022 	struct nfs_client *clp;
1023 	int error;
1024 
1025 	dprintk("--> nfs4_set_client()\n");
1026 
1027 	/* Allocate or find a client reference we can use */
1028 	clp = nfs_get_client(&cl_init);
1029 	if (IS_ERR(clp)) {
1030 		error = PTR_ERR(clp);
1031 		goto error;
1032 	}
1033 	error = nfs4_init_client(clp, timeparms, ip_addr, authflavour);
1034 	if (error < 0)
1035 		goto error_put;
1036 
1037 	server->nfs_client = clp;
1038 	dprintk("<-- nfs4_set_client() = 0 [new %p]\n", clp);
1039 	return 0;
1040 
1041 error_put:
1042 	nfs_put_client(clp);
1043 error:
1044 	dprintk("<-- nfs4_set_client() = xerror %d\n", error);
1045 	return error;
1046 }
1047 
1048 /*
1049  * Create a version 4 volume record
1050  */
1051 static int nfs4_init_server(struct nfs_server *server,
1052 		const struct nfs_parsed_mount_data *data)
1053 {
1054 	struct rpc_timeout timeparms;
1055 	int error;
1056 
1057 	dprintk("--> nfs4_init_server()\n");
1058 
1059 	nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
1060 			data->timeo, data->retrans);
1061 
1062 	/* Get a client record */
1063 	error = nfs4_set_client(server,
1064 			data->nfs_server.hostname,
1065 			(const struct sockaddr *)&data->nfs_server.address,
1066 			data->nfs_server.addrlen,
1067 			data->client_address,
1068 			data->auth_flavors[0],
1069 			data->nfs_server.protocol,
1070 			&timeparms);
1071 	if (error < 0)
1072 		goto error;
1073 
1074 	/* Initialise the client representation from the mount data */
1075 	server->flags = data->flags;
1076 	server->caps |= NFS_CAP_ATOMIC_OPEN;
1077 
1078 	if (data->rsize)
1079 		server->rsize = nfs_block_size(data->rsize, NULL);
1080 	if (data->wsize)
1081 		server->wsize = nfs_block_size(data->wsize, NULL);
1082 
1083 	server->acregmin = data->acregmin * HZ;
1084 	server->acregmax = data->acregmax * HZ;
1085 	server->acdirmin = data->acdirmin * HZ;
1086 	server->acdirmax = data->acdirmax * HZ;
1087 
1088 	server->port = data->nfs_server.port;
1089 
1090 	error = nfs_init_server_rpcclient(server, &timeparms, data->auth_flavors[0]);
1091 
1092 error:
1093 	/* Done */
1094 	dprintk("<-- nfs4_init_server() = %d\n", error);
1095 	return error;
1096 }
1097 
1098 /*
1099  * Create a version 4 volume record
1100  * - keyed on server and FSID
1101  */
1102 struct nfs_server *nfs4_create_server(const struct nfs_parsed_mount_data *data,
1103 				      struct nfs_fh *mntfh)
1104 {
1105 	struct nfs_fattr fattr;
1106 	struct nfs_server *server;
1107 	int error;
1108 
1109 	dprintk("--> nfs4_create_server()\n");
1110 
1111 	server = nfs_alloc_server();
1112 	if (!server)
1113 		return ERR_PTR(-ENOMEM);
1114 
1115 	/* set up the general RPC client */
1116 	error = nfs4_init_server(server, data);
1117 	if (error < 0)
1118 		goto error;
1119 
1120 	BUG_ON(!server->nfs_client);
1121 	BUG_ON(!server->nfs_client->rpc_ops);
1122 	BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1123 
1124 	/* Probe the root fh to retrieve its FSID */
1125 	error = nfs4_path_walk(server, mntfh, data->nfs_server.export_path);
1126 	if (error < 0)
1127 		goto error;
1128 
1129 	dprintk("Server FSID: %llx:%llx\n",
1130 		(unsigned long long) server->fsid.major,
1131 		(unsigned long long) server->fsid.minor);
1132 	dprintk("Mount FH: %d\n", mntfh->size);
1133 
1134 	error = nfs_probe_fsinfo(server, mntfh, &fattr);
1135 	if (error < 0)
1136 		goto error;
1137 
1138 	if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1139 		server->namelen = NFS4_MAXNAMLEN;
1140 
1141 	BUG_ON(!server->nfs_client);
1142 	BUG_ON(!server->nfs_client->rpc_ops);
1143 	BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1144 
1145 	spin_lock(&nfs_client_lock);
1146 	list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1147 	list_add_tail(&server->master_link, &nfs_volume_list);
1148 	spin_unlock(&nfs_client_lock);
1149 
1150 	server->mount_time = jiffies;
1151 	dprintk("<-- nfs4_create_server() = %p\n", server);
1152 	return server;
1153 
1154 error:
1155 	nfs_free_server(server);
1156 	dprintk("<-- nfs4_create_server() = error %d\n", error);
1157 	return ERR_PTR(error);
1158 }
1159 
1160 /*
1161  * Create an NFS4 referral server record
1162  */
1163 struct nfs_server *nfs4_create_referral_server(struct nfs_clone_mount *data,
1164 					       struct nfs_fh *mntfh)
1165 {
1166 	struct nfs_client *parent_client;
1167 	struct nfs_server *server, *parent_server;
1168 	struct nfs_fattr fattr;
1169 	int error;
1170 
1171 	dprintk("--> nfs4_create_referral_server()\n");
1172 
1173 	server = nfs_alloc_server();
1174 	if (!server)
1175 		return ERR_PTR(-ENOMEM);
1176 
1177 	parent_server = NFS_SB(data->sb);
1178 	parent_client = parent_server->nfs_client;
1179 
1180 	/* Get a client representation.
1181 	 * Note: NFSv4 always uses TCP, */
1182 	error = nfs4_set_client(server, data->hostname,
1183 				data->addr,
1184 				data->addrlen,
1185 				parent_client->cl_ipaddr,
1186 				data->authflavor,
1187 				parent_server->client->cl_xprt->prot,
1188 				parent_server->client->cl_timeout);
1189 	if (error < 0)
1190 		goto error;
1191 
1192 	/* Initialise the client representation from the parent server */
1193 	nfs_server_copy_userdata(server, parent_server);
1194 	server->caps |= NFS_CAP_ATOMIC_OPEN;
1195 
1196 	error = nfs_init_server_rpcclient(server, parent_server->client->cl_timeout, data->authflavor);
1197 	if (error < 0)
1198 		goto error;
1199 
1200 	BUG_ON(!server->nfs_client);
1201 	BUG_ON(!server->nfs_client->rpc_ops);
1202 	BUG_ON(!server->nfs_client->rpc_ops->file_inode_ops);
1203 
1204 	/* Probe the root fh to retrieve its FSID and filehandle */
1205 	error = nfs4_path_walk(server, mntfh, data->mnt_path);
1206 	if (error < 0)
1207 		goto error;
1208 
1209 	/* probe the filesystem info for this server filesystem */
1210 	error = nfs_probe_fsinfo(server, mntfh, &fattr);
1211 	if (error < 0)
1212 		goto error;
1213 
1214 	if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1215 		server->namelen = NFS4_MAXNAMLEN;
1216 
1217 	dprintk("Referral FSID: %llx:%llx\n",
1218 		(unsigned long long) server->fsid.major,
1219 		(unsigned long long) server->fsid.minor);
1220 
1221 	spin_lock(&nfs_client_lock);
1222 	list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1223 	list_add_tail(&server->master_link, &nfs_volume_list);
1224 	spin_unlock(&nfs_client_lock);
1225 
1226 	server->mount_time = jiffies;
1227 
1228 	dprintk("<-- nfs_create_referral_server() = %p\n", server);
1229 	return server;
1230 
1231 error:
1232 	nfs_free_server(server);
1233 	dprintk("<-- nfs4_create_referral_server() = error %d\n", error);
1234 	return ERR_PTR(error);
1235 }
1236 
1237 #endif /* CONFIG_NFS_V4 */
1238 
1239 /*
1240  * Clone an NFS2, NFS3 or NFS4 server record
1241  */
1242 struct nfs_server *nfs_clone_server(struct nfs_server *source,
1243 				    struct nfs_fh *fh,
1244 				    struct nfs_fattr *fattr)
1245 {
1246 	struct nfs_server *server;
1247 	struct nfs_fattr fattr_fsinfo;
1248 	int error;
1249 
1250 	dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
1251 		(unsigned long long) fattr->fsid.major,
1252 		(unsigned long long) fattr->fsid.minor);
1253 
1254 	server = nfs_alloc_server();
1255 	if (!server)
1256 		return ERR_PTR(-ENOMEM);
1257 
1258 	/* Copy data from the source */
1259 	server->nfs_client = source->nfs_client;
1260 	atomic_inc(&server->nfs_client->cl_count);
1261 	nfs_server_copy_userdata(server, source);
1262 
1263 	server->fsid = fattr->fsid;
1264 
1265 	error = nfs_init_server_rpcclient(server,
1266 			source->client->cl_timeout,
1267 			source->client->cl_auth->au_flavor);
1268 	if (error < 0)
1269 		goto out_free_server;
1270 	if (!IS_ERR(source->client_acl))
1271 		nfs_init_server_aclclient(server);
1272 
1273 	/* probe the filesystem info for this server filesystem */
1274 	error = nfs_probe_fsinfo(server, fh, &fattr_fsinfo);
1275 	if (error < 0)
1276 		goto out_free_server;
1277 
1278 	if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1279 		server->namelen = NFS4_MAXNAMLEN;
1280 
1281 	dprintk("Cloned FSID: %llx:%llx\n",
1282 		(unsigned long long) server->fsid.major,
1283 		(unsigned long long) server->fsid.minor);
1284 
1285 	error = nfs_start_lockd(server);
1286 	if (error < 0)
1287 		goto out_free_server;
1288 
1289 	spin_lock(&nfs_client_lock);
1290 	list_add_tail(&server->client_link, &server->nfs_client->cl_superblocks);
1291 	list_add_tail(&server->master_link, &nfs_volume_list);
1292 	spin_unlock(&nfs_client_lock);
1293 
1294 	server->mount_time = jiffies;
1295 
1296 	dprintk("<-- nfs_clone_server() = %p\n", server);
1297 	return server;
1298 
1299 out_free_server:
1300 	nfs_free_server(server);
1301 	dprintk("<-- nfs_clone_server() = error %d\n", error);
1302 	return ERR_PTR(error);
1303 }
1304 
1305 #ifdef CONFIG_PROC_FS
1306 static struct proc_dir_entry *proc_fs_nfs;
1307 
1308 static int nfs_server_list_open(struct inode *inode, struct file *file);
1309 static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1310 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1311 static void nfs_server_list_stop(struct seq_file *p, void *v);
1312 static int nfs_server_list_show(struct seq_file *m, void *v);
1313 
1314 static struct seq_operations nfs_server_list_ops = {
1315 	.start	= nfs_server_list_start,
1316 	.next	= nfs_server_list_next,
1317 	.stop	= nfs_server_list_stop,
1318 	.show	= nfs_server_list_show,
1319 };
1320 
1321 static const struct file_operations nfs_server_list_fops = {
1322 	.open		= nfs_server_list_open,
1323 	.read		= seq_read,
1324 	.llseek		= seq_lseek,
1325 	.release	= seq_release,
1326 	.owner		= THIS_MODULE,
1327 };
1328 
1329 static int nfs_volume_list_open(struct inode *inode, struct file *file);
1330 static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1331 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1332 static void nfs_volume_list_stop(struct seq_file *p, void *v);
1333 static int nfs_volume_list_show(struct seq_file *m, void *v);
1334 
1335 static struct seq_operations nfs_volume_list_ops = {
1336 	.start	= nfs_volume_list_start,
1337 	.next	= nfs_volume_list_next,
1338 	.stop	= nfs_volume_list_stop,
1339 	.show	= nfs_volume_list_show,
1340 };
1341 
1342 static const struct file_operations nfs_volume_list_fops = {
1343 	.open		= nfs_volume_list_open,
1344 	.read		= seq_read,
1345 	.llseek		= seq_lseek,
1346 	.release	= seq_release,
1347 	.owner		= THIS_MODULE,
1348 };
1349 
1350 /*
1351  * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1352  * we're dealing
1353  */
1354 static int nfs_server_list_open(struct inode *inode, struct file *file)
1355 {
1356 	struct seq_file *m;
1357 	int ret;
1358 
1359 	ret = seq_open(file, &nfs_server_list_ops);
1360 	if (ret < 0)
1361 		return ret;
1362 
1363 	m = file->private_data;
1364 	m->private = PDE(inode)->data;
1365 
1366 	return 0;
1367 }
1368 
1369 /*
1370  * set up the iterator to start reading from the server list and return the first item
1371  */
1372 static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1373 {
1374 	/* lock the list against modification */
1375 	spin_lock(&nfs_client_lock);
1376 	return seq_list_start_head(&nfs_client_list, *_pos);
1377 }
1378 
1379 /*
1380  * move to next server
1381  */
1382 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1383 {
1384 	return seq_list_next(v, &nfs_client_list, pos);
1385 }
1386 
1387 /*
1388  * clean up after reading from the transports list
1389  */
1390 static void nfs_server_list_stop(struct seq_file *p, void *v)
1391 {
1392 	spin_unlock(&nfs_client_lock);
1393 }
1394 
1395 /*
1396  * display a header line followed by a load of call lines
1397  */
1398 static int nfs_server_list_show(struct seq_file *m, void *v)
1399 {
1400 	struct nfs_client *clp;
1401 
1402 	/* display header on line 1 */
1403 	if (v == &nfs_client_list) {
1404 		seq_puts(m, "NV SERVER   PORT USE HOSTNAME\n");
1405 		return 0;
1406 	}
1407 
1408 	/* display one transport per line on subsequent lines */
1409 	clp = list_entry(v, struct nfs_client, cl_share_link);
1410 
1411 	seq_printf(m, "v%u %s %s %3d %s\n",
1412 		   clp->rpc_ops->version,
1413 		   rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1414 		   rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
1415 		   atomic_read(&clp->cl_count),
1416 		   clp->cl_hostname);
1417 
1418 	return 0;
1419 }
1420 
1421 /*
1422  * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1423  */
1424 static int nfs_volume_list_open(struct inode *inode, struct file *file)
1425 {
1426 	struct seq_file *m;
1427 	int ret;
1428 
1429 	ret = seq_open(file, &nfs_volume_list_ops);
1430 	if (ret < 0)
1431 		return ret;
1432 
1433 	m = file->private_data;
1434 	m->private = PDE(inode)->data;
1435 
1436 	return 0;
1437 }
1438 
1439 /*
1440  * set up the iterator to start reading from the volume list and return the first item
1441  */
1442 static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1443 {
1444 	/* lock the list against modification */
1445 	spin_lock(&nfs_client_lock);
1446 	return seq_list_start_head(&nfs_volume_list, *_pos);
1447 }
1448 
1449 /*
1450  * move to next volume
1451  */
1452 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1453 {
1454 	return seq_list_next(v, &nfs_volume_list, pos);
1455 }
1456 
1457 /*
1458  * clean up after reading from the transports list
1459  */
1460 static void nfs_volume_list_stop(struct seq_file *p, void *v)
1461 {
1462 	spin_unlock(&nfs_client_lock);
1463 }
1464 
1465 /*
1466  * display a header line followed by a load of call lines
1467  */
1468 static int nfs_volume_list_show(struct seq_file *m, void *v)
1469 {
1470 	struct nfs_server *server;
1471 	struct nfs_client *clp;
1472 	char dev[8], fsid[17];
1473 
1474 	/* display header on line 1 */
1475 	if (v == &nfs_volume_list) {
1476 		seq_puts(m, "NV SERVER   PORT DEV     FSID\n");
1477 		return 0;
1478 	}
1479 	/* display one transport per line on subsequent lines */
1480 	server = list_entry(v, struct nfs_server, master_link);
1481 	clp = server->nfs_client;
1482 
1483 	snprintf(dev, 8, "%u:%u",
1484 		 MAJOR(server->s_dev), MINOR(server->s_dev));
1485 
1486 	snprintf(fsid, 17, "%llx:%llx",
1487 		 (unsigned long long) server->fsid.major,
1488 		 (unsigned long long) server->fsid.minor);
1489 
1490 	seq_printf(m, "v%u %s %s %-7s %-17s\n",
1491 		   clp->rpc_ops->version,
1492 		   rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1493 		   rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
1494 		   dev,
1495 		   fsid);
1496 
1497 	return 0;
1498 }
1499 
1500 /*
1501  * initialise the /proc/fs/nfsfs/ directory
1502  */
1503 int __init nfs_fs_proc_init(void)
1504 {
1505 	struct proc_dir_entry *p;
1506 
1507 	proc_fs_nfs = proc_mkdir("fs/nfsfs", NULL);
1508 	if (!proc_fs_nfs)
1509 		goto error_0;
1510 
1511 	proc_fs_nfs->owner = THIS_MODULE;
1512 
1513 	/* a file of servers with which we're dealing */
1514 	p = proc_create("servers", S_IFREG|S_IRUGO,
1515 			proc_fs_nfs, &nfs_server_list_fops);
1516 	if (!p)
1517 		goto error_1;
1518 
1519 	/* a file of volumes that we have mounted */
1520 	p = proc_create("volumes", S_IFREG|S_IRUGO,
1521 			proc_fs_nfs, &nfs_volume_list_fops);
1522 	if (!p)
1523 		goto error_2;
1524 	return 0;
1525 
1526 error_2:
1527 	remove_proc_entry("servers", proc_fs_nfs);
1528 error_1:
1529 	remove_proc_entry("fs/nfsfs", NULL);
1530 error_0:
1531 	return -ENOMEM;
1532 }
1533 
1534 /*
1535  * clean up the /proc/fs/nfsfs/ directory
1536  */
1537 void nfs_fs_proc_exit(void)
1538 {
1539 	remove_proc_entry("volumes", proc_fs_nfs);
1540 	remove_proc_entry("servers", proc_fs_nfs);
1541 	remove_proc_entry("fs/nfsfs", NULL);
1542 }
1543 
1544 #endif /* CONFIG_PROC_FS */
1545