xref: /openbmc/linux/fs/nfs/client.c (revision c51d39010a1bccc9c1294e2d7c00005aefeb2b5c)
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/addr.h>
24 #include <linux/sunrpc/clnt.h>
25 #include <linux/sunrpc/stats.h>
26 #include <linux/sunrpc/metrics.h>
27 #include <linux/sunrpc/xprtsock.h>
28 #include <linux/sunrpc/xprtrdma.h>
29 #include <linux/nfs_fs.h>
30 #include <linux/nfs_mount.h>
31 #include <linux/nfs4_mount.h>
32 #include <linux/lockd/bind.h>
33 #include <linux/seq_file.h>
34 #include <linux/mount.h>
35 #include <linux/vfs.h>
36 #include <linux/inet.h>
37 #include <linux/in6.h>
38 #include <linux/slab.h>
39 #include <linux/idr.h>
40 #include <net/ipv6.h>
41 #include <linux/nfs_xdr.h>
42 #include <linux/sunrpc/bc_xprt.h>
43 #include <linux/nsproxy.h>
44 #include <linux/pid_namespace.h>
45 
46 
47 #include "nfs4_fs.h"
48 #include "callback.h"
49 #include "delegation.h"
50 #include "iostat.h"
51 #include "internal.h"
52 #include "fscache.h"
53 #include "pnfs.h"
54 #include "nfs.h"
55 #include "netns.h"
56 
57 #define NFSDBG_FACILITY		NFSDBG_CLIENT
58 
59 static DECLARE_WAIT_QUEUE_HEAD(nfs_client_active_wq);
60 static DEFINE_SPINLOCK(nfs_version_lock);
61 static DEFINE_MUTEX(nfs_version_mutex);
62 static LIST_HEAD(nfs_versions);
63 
64 /*
65  * RPC cruft for NFS
66  */
67 static const struct rpc_version *nfs_version[5] = {
68 	[2] = NULL,
69 	[3] = NULL,
70 	[4] = NULL,
71 };
72 
73 const struct rpc_program nfs_program = {
74 	.name			= "nfs",
75 	.number			= NFS_PROGRAM,
76 	.nrvers			= ARRAY_SIZE(nfs_version),
77 	.version		= nfs_version,
78 	.stats			= &nfs_rpcstat,
79 	.pipe_dir_name		= NFS_PIPE_DIRNAME,
80 };
81 
82 struct rpc_stat nfs_rpcstat = {
83 	.program		= &nfs_program
84 };
85 
86 static struct nfs_subversion *find_nfs_version(unsigned int version)
87 {
88 	struct nfs_subversion *nfs;
89 	spin_lock(&nfs_version_lock);
90 
91 	list_for_each_entry(nfs, &nfs_versions, list) {
92 		if (nfs->rpc_ops->version == version) {
93 			spin_unlock(&nfs_version_lock);
94 			return nfs;
95 		}
96 	}
97 
98 	spin_unlock(&nfs_version_lock);
99 	return ERR_PTR(-EPROTONOSUPPORT);
100 }
101 
102 struct nfs_subversion *get_nfs_version(unsigned int version)
103 {
104 	struct nfs_subversion *nfs = find_nfs_version(version);
105 
106 	if (IS_ERR(nfs)) {
107 		mutex_lock(&nfs_version_mutex);
108 		request_module("nfsv%d", version);
109 		nfs = find_nfs_version(version);
110 		mutex_unlock(&nfs_version_mutex);
111 	}
112 
113 	if (!IS_ERR(nfs) && !try_module_get(nfs->owner))
114 		return ERR_PTR(-EAGAIN);
115 	return nfs;
116 }
117 
118 void put_nfs_version(struct nfs_subversion *nfs)
119 {
120 	module_put(nfs->owner);
121 }
122 
123 void register_nfs_version(struct nfs_subversion *nfs)
124 {
125 	spin_lock(&nfs_version_lock);
126 
127 	list_add(&nfs->list, &nfs_versions);
128 	nfs_version[nfs->rpc_ops->version] = nfs->rpc_vers;
129 
130 	spin_unlock(&nfs_version_lock);
131 }
132 EXPORT_SYMBOL_GPL(register_nfs_version);
133 
134 void unregister_nfs_version(struct nfs_subversion *nfs)
135 {
136 	spin_lock(&nfs_version_lock);
137 
138 	nfs_version[nfs->rpc_ops->version] = NULL;
139 	list_del(&nfs->list);
140 
141 	spin_unlock(&nfs_version_lock);
142 }
143 EXPORT_SYMBOL_GPL(unregister_nfs_version);
144 
145 /*
146  * Allocate a shared client record
147  *
148  * Since these are allocated/deallocated very rarely, we don't
149  * bother putting them in a slab cache...
150  */
151 struct nfs_client *nfs_alloc_client(const struct nfs_client_initdata *cl_init)
152 {
153 	struct nfs_client *clp;
154 	struct rpc_cred *cred;
155 	int err = -ENOMEM;
156 
157 	if ((clp = kzalloc(sizeof(*clp), GFP_KERNEL)) == NULL)
158 		goto error_0;
159 
160 	clp->cl_nfs_mod = cl_init->nfs_mod;
161 	if (!try_module_get(clp->cl_nfs_mod->owner))
162 		goto error_dealloc;
163 
164 	clp->rpc_ops = clp->cl_nfs_mod->rpc_ops;
165 
166 	atomic_set(&clp->cl_count, 1);
167 	clp->cl_cons_state = NFS_CS_INITING;
168 
169 	memcpy(&clp->cl_addr, cl_init->addr, cl_init->addrlen);
170 	clp->cl_addrlen = cl_init->addrlen;
171 
172 	if (cl_init->hostname) {
173 		err = -ENOMEM;
174 		clp->cl_hostname = kstrdup(cl_init->hostname, GFP_KERNEL);
175 		if (!clp->cl_hostname)
176 			goto error_cleanup;
177 	}
178 
179 	INIT_LIST_HEAD(&clp->cl_superblocks);
180 	clp->cl_rpcclient = ERR_PTR(-EINVAL);
181 
182 	clp->cl_proto = cl_init->proto;
183 	clp->cl_net = get_net(cl_init->net);
184 
185 	cred = rpc_lookup_machine_cred("*");
186 	if (!IS_ERR(cred))
187 		clp->cl_machine_cred = cred;
188 	nfs_fscache_get_client_cookie(clp);
189 
190 	return clp;
191 
192 error_cleanup:
193 	put_nfs_version(clp->cl_nfs_mod);
194 error_dealloc:
195 	kfree(clp);
196 error_0:
197 	return ERR_PTR(err);
198 }
199 EXPORT_SYMBOL_GPL(nfs_alloc_client);
200 
201 #if IS_ENABLED(CONFIG_NFS_V4)
202 void nfs_cleanup_cb_ident_idr(struct net *net)
203 {
204 	struct nfs_net *nn = net_generic(net, nfs_net_id);
205 
206 	idr_destroy(&nn->cb_ident_idr);
207 }
208 
209 /* nfs_client_lock held */
210 static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
211 {
212 	struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
213 
214 	if (clp->cl_cb_ident)
215 		idr_remove(&nn->cb_ident_idr, clp->cl_cb_ident);
216 }
217 
218 static void pnfs_init_server(struct nfs_server *server)
219 {
220 	rpc_init_wait_queue(&server->roc_rpcwaitq, "pNFS ROC");
221 }
222 
223 #else
224 void nfs_cleanup_cb_ident_idr(struct net *net)
225 {
226 }
227 
228 static void nfs_cb_idr_remove_locked(struct nfs_client *clp)
229 {
230 }
231 
232 static void pnfs_init_server(struct nfs_server *server)
233 {
234 }
235 
236 #endif /* CONFIG_NFS_V4 */
237 
238 /*
239  * Destroy a shared client record
240  */
241 void nfs_free_client(struct nfs_client *clp)
242 {
243 	dprintk("--> nfs_free_client(%u)\n", clp->rpc_ops->version);
244 
245 	nfs_fscache_release_client_cookie(clp);
246 
247 	/* -EIO all pending I/O */
248 	if (!IS_ERR(clp->cl_rpcclient))
249 		rpc_shutdown_client(clp->cl_rpcclient);
250 
251 	if (clp->cl_machine_cred != NULL)
252 		put_rpccred(clp->cl_machine_cred);
253 
254 	put_net(clp->cl_net);
255 	put_nfs_version(clp->cl_nfs_mod);
256 	kfree(clp->cl_hostname);
257 	kfree(clp->cl_acceptor);
258 	kfree(clp);
259 
260 	dprintk("<-- nfs_free_client()\n");
261 }
262 EXPORT_SYMBOL_GPL(nfs_free_client);
263 
264 /*
265  * Release a reference to a shared client record
266  */
267 void nfs_put_client(struct nfs_client *clp)
268 {
269 	struct nfs_net *nn;
270 
271 	if (!clp)
272 		return;
273 
274 	dprintk("--> nfs_put_client({%d})\n", atomic_read(&clp->cl_count));
275 	nn = net_generic(clp->cl_net, nfs_net_id);
276 
277 	if (atomic_dec_and_lock(&clp->cl_count, &nn->nfs_client_lock)) {
278 		list_del(&clp->cl_share_link);
279 		nfs_cb_idr_remove_locked(clp);
280 		spin_unlock(&nn->nfs_client_lock);
281 
282 		WARN_ON_ONCE(!list_empty(&clp->cl_superblocks));
283 
284 		clp->rpc_ops->free_client(clp);
285 	}
286 }
287 EXPORT_SYMBOL_GPL(nfs_put_client);
288 
289 /*
290  * Find an nfs_client on the list that matches the initialisation data
291  * that is supplied.
292  */
293 static struct nfs_client *nfs_match_client(const struct nfs_client_initdata *data)
294 {
295 	struct nfs_client *clp;
296 	const struct sockaddr *sap = data->addr;
297 	struct nfs_net *nn = net_generic(data->net, nfs_net_id);
298 
299 	list_for_each_entry(clp, &nn->nfs_client_list, cl_share_link) {
300 	        const struct sockaddr *clap = (struct sockaddr *)&clp->cl_addr;
301 		/* Don't match clients that failed to initialise properly */
302 		if (clp->cl_cons_state < 0)
303 			continue;
304 
305 		/* Different NFS versions cannot share the same nfs_client */
306 		if (clp->rpc_ops != data->nfs_mod->rpc_ops)
307 			continue;
308 
309 		if (clp->cl_proto != data->proto)
310 			continue;
311 		/* Match nfsv4 minorversion */
312 		if (clp->cl_minorversion != data->minorversion)
313 			continue;
314 		/* Match the full socket address */
315 		if (!rpc_cmp_addr_port(sap, clap))
316 			/* Match all xprt_switch full socket addresses */
317 			if (IS_ERR(clp->cl_rpcclient) ||
318                             !rpc_clnt_xprt_switch_has_addr(clp->cl_rpcclient,
319 							   sap))
320 				continue;
321 
322 		atomic_inc(&clp->cl_count);
323 		return clp;
324 	}
325 	return NULL;
326 }
327 
328 static bool nfs_client_init_is_complete(const struct nfs_client *clp)
329 {
330 	return clp->cl_cons_state <= NFS_CS_READY;
331 }
332 
333 int nfs_wait_client_init_complete(const struct nfs_client *clp)
334 {
335 	return wait_event_killable(nfs_client_active_wq,
336 			nfs_client_init_is_complete(clp));
337 }
338 EXPORT_SYMBOL_GPL(nfs_wait_client_init_complete);
339 
340 /*
341  * Found an existing client.  Make sure it's ready before returning.
342  */
343 static struct nfs_client *
344 nfs_found_client(const struct nfs_client_initdata *cl_init,
345 		 struct nfs_client *clp)
346 {
347 	int error;
348 
349 	error = nfs_wait_client_init_complete(clp);
350 	if (error < 0) {
351 		nfs_put_client(clp);
352 		return ERR_PTR(-ERESTARTSYS);
353 	}
354 
355 	if (clp->cl_cons_state < NFS_CS_READY) {
356 		error = clp->cl_cons_state;
357 		nfs_put_client(clp);
358 		return ERR_PTR(error);
359 	}
360 
361 	smp_rmb();
362 
363 	dprintk("<-- %s found nfs_client %p for %s\n",
364 		__func__, clp, cl_init->hostname ?: "");
365 	return clp;
366 }
367 
368 /*
369  * Look up a client by IP address and protocol version
370  * - creates a new record if one doesn't yet exist
371  */
372 struct nfs_client *
373 nfs_get_client(const struct nfs_client_initdata *cl_init,
374 	       rpc_authflavor_t authflavour)
375 {
376 	struct nfs_client *clp, *new = NULL;
377 	struct nfs_net *nn = net_generic(cl_init->net, nfs_net_id);
378 	const struct nfs_rpc_ops *rpc_ops = cl_init->nfs_mod->rpc_ops;
379 
380 	if (cl_init->hostname == NULL) {
381 		WARN_ON(1);
382 		return NULL;
383 	}
384 
385 	dprintk("--> nfs_get_client(%s,v%u)\n",
386 		cl_init->hostname, rpc_ops->version);
387 
388 	/* see if the client already exists */
389 	do {
390 		spin_lock(&nn->nfs_client_lock);
391 
392 		clp = nfs_match_client(cl_init);
393 		if (clp) {
394 			spin_unlock(&nn->nfs_client_lock);
395 			if (new)
396 				new->rpc_ops->free_client(new);
397 			return nfs_found_client(cl_init, clp);
398 		}
399 		if (new) {
400 			list_add_tail(&new->cl_share_link,
401 					&nn->nfs_client_list);
402 			spin_unlock(&nn->nfs_client_lock);
403 			new->cl_flags = cl_init->init_flags;
404 			return rpc_ops->init_client(new, cl_init);
405 		}
406 
407 		spin_unlock(&nn->nfs_client_lock);
408 
409 		new = rpc_ops->alloc_client(cl_init);
410 	} while (!IS_ERR(new));
411 
412 	dprintk("<-- nfs_get_client() Failed to find %s (%ld)\n",
413 		cl_init->hostname, PTR_ERR(new));
414 	return new;
415 }
416 EXPORT_SYMBOL_GPL(nfs_get_client);
417 
418 /*
419  * Mark a server as ready or failed
420  */
421 void nfs_mark_client_ready(struct nfs_client *clp, int state)
422 {
423 	smp_wmb();
424 	clp->cl_cons_state = state;
425 	wake_up_all(&nfs_client_active_wq);
426 }
427 EXPORT_SYMBOL_GPL(nfs_mark_client_ready);
428 
429 /*
430  * Initialise the timeout values for a connection
431  */
432 void nfs_init_timeout_values(struct rpc_timeout *to, int proto,
433 				    int timeo, int retrans)
434 {
435 	to->to_initval = timeo * HZ / 10;
436 	to->to_retries = retrans;
437 
438 	switch (proto) {
439 	case XPRT_TRANSPORT_TCP:
440 	case XPRT_TRANSPORT_RDMA:
441 		if (retrans == NFS_UNSPEC_RETRANS)
442 			to->to_retries = NFS_DEF_TCP_RETRANS;
443 		if (timeo == NFS_UNSPEC_TIMEO || to->to_retries == 0)
444 			to->to_initval = NFS_DEF_TCP_TIMEO * HZ / 10;
445 		if (to->to_initval > NFS_MAX_TCP_TIMEOUT)
446 			to->to_initval = NFS_MAX_TCP_TIMEOUT;
447 		to->to_increment = to->to_initval;
448 		to->to_maxval = to->to_initval + (to->to_increment * to->to_retries);
449 		if (to->to_maxval > NFS_MAX_TCP_TIMEOUT)
450 			to->to_maxval = NFS_MAX_TCP_TIMEOUT;
451 		if (to->to_maxval < to->to_initval)
452 			to->to_maxval = to->to_initval;
453 		to->to_exponential = 0;
454 		break;
455 	case XPRT_TRANSPORT_UDP:
456 		if (retrans == NFS_UNSPEC_RETRANS)
457 			to->to_retries = NFS_DEF_UDP_RETRANS;
458 		if (timeo == NFS_UNSPEC_TIMEO || to->to_initval == 0)
459 			to->to_initval = NFS_DEF_UDP_TIMEO * HZ / 10;
460 		if (to->to_initval > NFS_MAX_UDP_TIMEOUT)
461 			to->to_initval = NFS_MAX_UDP_TIMEOUT;
462 		to->to_maxval = NFS_MAX_UDP_TIMEOUT;
463 		to->to_exponential = 1;
464 		break;
465 	default:
466 		BUG();
467 	}
468 }
469 EXPORT_SYMBOL_GPL(nfs_init_timeout_values);
470 
471 /*
472  * Create an RPC client handle
473  */
474 int nfs_create_rpc_client(struct nfs_client *clp,
475 			  const struct nfs_client_initdata *cl_init,
476 			  rpc_authflavor_t flavor)
477 {
478 	struct rpc_clnt		*clnt = NULL;
479 	struct rpc_create_args args = {
480 		.net		= clp->cl_net,
481 		.protocol	= clp->cl_proto,
482 		.address	= (struct sockaddr *)&clp->cl_addr,
483 		.addrsize	= clp->cl_addrlen,
484 		.timeout	= cl_init->timeparms,
485 		.servername	= clp->cl_hostname,
486 		.nodename	= cl_init->nodename,
487 		.program	= &nfs_program,
488 		.version	= clp->rpc_ops->version,
489 		.authflavor	= flavor,
490 	};
491 
492 	if (test_bit(NFS_CS_DISCRTRY, &clp->cl_flags))
493 		args.flags |= RPC_CLNT_CREATE_DISCRTRY;
494 	if (test_bit(NFS_CS_NO_RETRANS_TIMEOUT, &clp->cl_flags))
495 		args.flags |= RPC_CLNT_CREATE_NO_RETRANS_TIMEOUT;
496 	if (test_bit(NFS_CS_NORESVPORT, &clp->cl_flags))
497 		args.flags |= RPC_CLNT_CREATE_NONPRIVPORT;
498 	if (test_bit(NFS_CS_INFINITE_SLOTS, &clp->cl_flags))
499 		args.flags |= RPC_CLNT_CREATE_INFINITE_SLOTS;
500 
501 	if (!IS_ERR(clp->cl_rpcclient))
502 		return 0;
503 
504 	clnt = rpc_create(&args);
505 	if (IS_ERR(clnt)) {
506 		dprintk("%s: cannot create RPC client. Error = %ld\n",
507 				__func__, PTR_ERR(clnt));
508 		return PTR_ERR(clnt);
509 	}
510 
511 	clp->cl_rpcclient = clnt;
512 	return 0;
513 }
514 EXPORT_SYMBOL_GPL(nfs_create_rpc_client);
515 
516 /*
517  * Version 2 or 3 client destruction
518  */
519 static void nfs_destroy_server(struct nfs_server *server)
520 {
521 	if (server->nlm_host)
522 		nlmclnt_done(server->nlm_host);
523 }
524 
525 /*
526  * Version 2 or 3 lockd setup
527  */
528 static int nfs_start_lockd(struct nfs_server *server)
529 {
530 	struct nlm_host *host;
531 	struct nfs_client *clp = server->nfs_client;
532 	struct nlmclnt_initdata nlm_init = {
533 		.hostname	= clp->cl_hostname,
534 		.address	= (struct sockaddr *)&clp->cl_addr,
535 		.addrlen	= clp->cl_addrlen,
536 		.nfs_version	= clp->rpc_ops->version,
537 		.noresvport	= server->flags & NFS_MOUNT_NORESVPORT ?
538 					1 : 0,
539 		.net		= clp->cl_net,
540 	};
541 
542 	if (nlm_init.nfs_version > 3)
543 		return 0;
544 	if ((server->flags & NFS_MOUNT_LOCAL_FLOCK) &&
545 			(server->flags & NFS_MOUNT_LOCAL_FCNTL))
546 		return 0;
547 
548 	switch (clp->cl_proto) {
549 		default:
550 			nlm_init.protocol = IPPROTO_TCP;
551 			break;
552 		case XPRT_TRANSPORT_UDP:
553 			nlm_init.protocol = IPPROTO_UDP;
554 	}
555 
556 	host = nlmclnt_init(&nlm_init);
557 	if (IS_ERR(host))
558 		return PTR_ERR(host);
559 
560 	server->nlm_host = host;
561 	server->destroy = nfs_destroy_server;
562 	return 0;
563 }
564 
565 /*
566  * Create a general RPC client
567  */
568 int nfs_init_server_rpcclient(struct nfs_server *server,
569 		const struct rpc_timeout *timeo,
570 		rpc_authflavor_t pseudoflavour)
571 {
572 	struct nfs_client *clp = server->nfs_client;
573 
574 	server->client = rpc_clone_client_set_auth(clp->cl_rpcclient,
575 							pseudoflavour);
576 	if (IS_ERR(server->client)) {
577 		dprintk("%s: couldn't create rpc_client!\n", __func__);
578 		return PTR_ERR(server->client);
579 	}
580 
581 	memcpy(&server->client->cl_timeout_default,
582 			timeo,
583 			sizeof(server->client->cl_timeout_default));
584 	server->client->cl_timeout = &server->client->cl_timeout_default;
585 	server->client->cl_softrtry = 0;
586 	if (server->flags & NFS_MOUNT_SOFT)
587 		server->client->cl_softrtry = 1;
588 
589 	return 0;
590 }
591 EXPORT_SYMBOL_GPL(nfs_init_server_rpcclient);
592 
593 /**
594  * nfs_init_client - Initialise an NFS2 or NFS3 client
595  *
596  * @clp: nfs_client to initialise
597  * @cl_init: Initialisation parameters
598  *
599  * Returns pointer to an NFS client, or an ERR_PTR value.
600  */
601 struct nfs_client *nfs_init_client(struct nfs_client *clp,
602 				   const struct nfs_client_initdata *cl_init)
603 {
604 	int error;
605 
606 	if (clp->cl_cons_state == NFS_CS_READY) {
607 		/* the client is already initialised */
608 		dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
609 		return clp;
610 	}
611 
612 	/*
613 	 * Create a client RPC handle for doing FSSTAT with UNIX auth only
614 	 * - RFC 2623, sec 2.3.2
615 	 */
616 	error = nfs_create_rpc_client(clp, cl_init, RPC_AUTH_UNIX);
617 	if (error < 0)
618 		goto error;
619 	nfs_mark_client_ready(clp, NFS_CS_READY);
620 	return clp;
621 
622 error:
623 	nfs_mark_client_ready(clp, error);
624 	nfs_put_client(clp);
625 	dprintk("<-- nfs_init_client() = xerror %d\n", error);
626 	return ERR_PTR(error);
627 }
628 EXPORT_SYMBOL_GPL(nfs_init_client);
629 
630 /*
631  * Create a version 2 or 3 client
632  */
633 static int nfs_init_server(struct nfs_server *server,
634 			   const struct nfs_parsed_mount_data *data,
635 			   struct nfs_subversion *nfs_mod)
636 {
637 	struct rpc_timeout timeparms;
638 	struct nfs_client_initdata cl_init = {
639 		.hostname = data->nfs_server.hostname,
640 		.addr = (const struct sockaddr *)&data->nfs_server.address,
641 		.addrlen = data->nfs_server.addrlen,
642 		.nfs_mod = nfs_mod,
643 		.proto = data->nfs_server.protocol,
644 		.net = data->net,
645 		.timeparms = &timeparms,
646 	};
647 	struct nfs_client *clp;
648 	int error;
649 
650 	dprintk("--> nfs_init_server()\n");
651 
652 	nfs_init_timeout_values(&timeparms, data->nfs_server.protocol,
653 			data->timeo, data->retrans);
654 	if (data->flags & NFS_MOUNT_NORESVPORT)
655 		set_bit(NFS_CS_NORESVPORT, &cl_init.init_flags);
656 
657 	/* Allocate or find a client reference we can use */
658 	clp = nfs_get_client(&cl_init, RPC_AUTH_UNIX);
659 	if (IS_ERR(clp)) {
660 		dprintk("<-- nfs_init_server() = error %ld\n", PTR_ERR(clp));
661 		return PTR_ERR(clp);
662 	}
663 
664 	server->nfs_client = clp;
665 
666 	/* Initialise the client representation from the mount data */
667 	server->flags = data->flags;
668 	server->options = data->options;
669 	server->caps |= NFS_CAP_HARDLINKS|NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
670 		NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|NFS_CAP_OWNER_GROUP|
671 		NFS_CAP_ATIME|NFS_CAP_CTIME|NFS_CAP_MTIME;
672 
673 	if (data->rsize)
674 		server->rsize = nfs_block_size(data->rsize, NULL);
675 	if (data->wsize)
676 		server->wsize = nfs_block_size(data->wsize, NULL);
677 
678 	server->acregmin = data->acregmin * HZ;
679 	server->acregmax = data->acregmax * HZ;
680 	server->acdirmin = data->acdirmin * HZ;
681 	server->acdirmax = data->acdirmax * HZ;
682 
683 	/* Start lockd here, before we might error out */
684 	error = nfs_start_lockd(server);
685 	if (error < 0)
686 		goto error;
687 
688 	server->port = data->nfs_server.port;
689 	server->auth_info = data->auth_info;
690 
691 	error = nfs_init_server_rpcclient(server, &timeparms,
692 					  data->selected_flavor);
693 	if (error < 0)
694 		goto error;
695 
696 	/* Preserve the values of mount_server-related mount options */
697 	if (data->mount_server.addrlen) {
698 		memcpy(&server->mountd_address, &data->mount_server.address,
699 			data->mount_server.addrlen);
700 		server->mountd_addrlen = data->mount_server.addrlen;
701 	}
702 	server->mountd_version = data->mount_server.version;
703 	server->mountd_port = data->mount_server.port;
704 	server->mountd_protocol = data->mount_server.protocol;
705 
706 	server->namelen  = data->namlen;
707 	dprintk("<-- nfs_init_server() = 0 [new %p]\n", clp);
708 	return 0;
709 
710 error:
711 	server->nfs_client = NULL;
712 	nfs_put_client(clp);
713 	dprintk("<-- nfs_init_server() = xerror %d\n", error);
714 	return error;
715 }
716 
717 /*
718  * Load up the server record from information gained in an fsinfo record
719  */
720 static void nfs_server_set_fsinfo(struct nfs_server *server,
721 				  struct nfs_fsinfo *fsinfo)
722 {
723 	unsigned long max_rpc_payload;
724 
725 	/* Work out a lot of parameters */
726 	if (server->rsize == 0)
727 		server->rsize = nfs_block_size(fsinfo->rtpref, NULL);
728 	if (server->wsize == 0)
729 		server->wsize = nfs_block_size(fsinfo->wtpref, NULL);
730 
731 	if (fsinfo->rtmax >= 512 && server->rsize > fsinfo->rtmax)
732 		server->rsize = nfs_block_size(fsinfo->rtmax, NULL);
733 	if (fsinfo->wtmax >= 512 && server->wsize > fsinfo->wtmax)
734 		server->wsize = nfs_block_size(fsinfo->wtmax, NULL);
735 
736 	max_rpc_payload = nfs_block_size(rpc_max_payload(server->client), NULL);
737 	if (server->rsize > max_rpc_payload)
738 		server->rsize = max_rpc_payload;
739 	if (server->rsize > NFS_MAX_FILE_IO_SIZE)
740 		server->rsize = NFS_MAX_FILE_IO_SIZE;
741 	server->rpages = (server->rsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
742 
743 	server->backing_dev_info.name = "nfs";
744 	server->backing_dev_info.ra_pages = server->rpages * NFS_MAX_READAHEAD;
745 
746 	if (server->wsize > max_rpc_payload)
747 		server->wsize = max_rpc_payload;
748 	if (server->wsize > NFS_MAX_FILE_IO_SIZE)
749 		server->wsize = NFS_MAX_FILE_IO_SIZE;
750 	server->wpages = (server->wsize + PAGE_SIZE - 1) >> PAGE_SHIFT;
751 
752 	server->wtmult = nfs_block_bits(fsinfo->wtmult, NULL);
753 
754 	server->dtsize = nfs_block_size(fsinfo->dtpref, NULL);
755 	if (server->dtsize > PAGE_SIZE * NFS_MAX_READDIR_PAGES)
756 		server->dtsize = PAGE_SIZE * NFS_MAX_READDIR_PAGES;
757 	if (server->dtsize > server->rsize)
758 		server->dtsize = server->rsize;
759 
760 	if (server->flags & NFS_MOUNT_NOAC) {
761 		server->acregmin = server->acregmax = 0;
762 		server->acdirmin = server->acdirmax = 0;
763 	}
764 
765 	server->maxfilesize = fsinfo->maxfilesize;
766 
767 	server->time_delta = fsinfo->time_delta;
768 
769 	server->clone_blksize = fsinfo->clone_blksize;
770 	/* We're airborne Set socket buffersize */
771 	rpc_setbufsize(server->client, server->wsize + 100, server->rsize + 100);
772 }
773 
774 /*
775  * Probe filesystem information, including the FSID on v2/v3
776  */
777 int nfs_probe_fsinfo(struct nfs_server *server, struct nfs_fh *mntfh, struct nfs_fattr *fattr)
778 {
779 	struct nfs_fsinfo fsinfo;
780 	struct nfs_client *clp = server->nfs_client;
781 	int error;
782 
783 	dprintk("--> nfs_probe_fsinfo()\n");
784 
785 	if (clp->rpc_ops->set_capabilities != NULL) {
786 		error = clp->rpc_ops->set_capabilities(server, mntfh);
787 		if (error < 0)
788 			goto out_error;
789 	}
790 
791 	fsinfo.fattr = fattr;
792 	fsinfo.nlayouttypes = 0;
793 	memset(fsinfo.layouttype, 0, sizeof(fsinfo.layouttype));
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 
800 	/* Get some general file system info */
801 	if (server->namelen == 0) {
802 		struct nfs_pathconf pathinfo;
803 
804 		pathinfo.fattr = fattr;
805 		nfs_fattr_init(fattr);
806 
807 		if (clp->rpc_ops->pathconf(server, mntfh, &pathinfo) >= 0)
808 			server->namelen = pathinfo.max_namelen;
809 	}
810 
811 	dprintk("<-- nfs_probe_fsinfo() = 0\n");
812 	return 0;
813 
814 out_error:
815 	dprintk("nfs_probe_fsinfo: error = %d\n", -error);
816 	return error;
817 }
818 EXPORT_SYMBOL_GPL(nfs_probe_fsinfo);
819 
820 /*
821  * Copy useful information when duplicating a server record
822  */
823 void nfs_server_copy_userdata(struct nfs_server *target, struct nfs_server *source)
824 {
825 	target->flags = source->flags;
826 	target->rsize = source->rsize;
827 	target->wsize = source->wsize;
828 	target->acregmin = source->acregmin;
829 	target->acregmax = source->acregmax;
830 	target->acdirmin = source->acdirmin;
831 	target->acdirmax = source->acdirmax;
832 	target->caps = source->caps;
833 	target->options = source->options;
834 	target->auth_info = source->auth_info;
835 }
836 EXPORT_SYMBOL_GPL(nfs_server_copy_userdata);
837 
838 void nfs_server_insert_lists(struct nfs_server *server)
839 {
840 	struct nfs_client *clp = server->nfs_client;
841 	struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
842 
843 	spin_lock(&nn->nfs_client_lock);
844 	list_add_tail_rcu(&server->client_link, &clp->cl_superblocks);
845 	list_add_tail(&server->master_link, &nn->nfs_volume_list);
846 	clear_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
847 	spin_unlock(&nn->nfs_client_lock);
848 
849 }
850 EXPORT_SYMBOL_GPL(nfs_server_insert_lists);
851 
852 void nfs_server_remove_lists(struct nfs_server *server)
853 {
854 	struct nfs_client *clp = server->nfs_client;
855 	struct nfs_net *nn;
856 
857 	if (clp == NULL)
858 		return;
859 	nn = net_generic(clp->cl_net, nfs_net_id);
860 	spin_lock(&nn->nfs_client_lock);
861 	list_del_rcu(&server->client_link);
862 	if (list_empty(&clp->cl_superblocks))
863 		set_bit(NFS_CS_STOP_RENEW, &clp->cl_res_state);
864 	list_del(&server->master_link);
865 	spin_unlock(&nn->nfs_client_lock);
866 
867 	synchronize_rcu();
868 }
869 EXPORT_SYMBOL_GPL(nfs_server_remove_lists);
870 
871 /*
872  * Allocate and initialise a server record
873  */
874 struct nfs_server *nfs_alloc_server(void)
875 {
876 	struct nfs_server *server;
877 
878 	server = kzalloc(sizeof(struct nfs_server), GFP_KERNEL);
879 	if (!server)
880 		return NULL;
881 
882 	server->client = server->client_acl = ERR_PTR(-EINVAL);
883 
884 	/* Zero out the NFS state stuff */
885 	INIT_LIST_HEAD(&server->client_link);
886 	INIT_LIST_HEAD(&server->master_link);
887 	INIT_LIST_HEAD(&server->delegations);
888 	INIT_LIST_HEAD(&server->layouts);
889 	INIT_LIST_HEAD(&server->state_owners_lru);
890 
891 	atomic_set(&server->active, 0);
892 
893 	server->io_stats = nfs_alloc_iostats();
894 	if (!server->io_stats) {
895 		kfree(server);
896 		return NULL;
897 	}
898 
899 	if (bdi_init(&server->backing_dev_info)) {
900 		nfs_free_iostats(server->io_stats);
901 		kfree(server);
902 		return NULL;
903 	}
904 
905 	ida_init(&server->openowner_id);
906 	ida_init(&server->lockowner_id);
907 	pnfs_init_server(server);
908 
909 	return server;
910 }
911 EXPORT_SYMBOL_GPL(nfs_alloc_server);
912 
913 /*
914  * Free up a server record
915  */
916 void nfs_free_server(struct nfs_server *server)
917 {
918 	dprintk("--> nfs_free_server()\n");
919 
920 	nfs_server_remove_lists(server);
921 
922 	if (server->destroy != NULL)
923 		server->destroy(server);
924 
925 	if (!IS_ERR(server->client_acl))
926 		rpc_shutdown_client(server->client_acl);
927 	if (!IS_ERR(server->client))
928 		rpc_shutdown_client(server->client);
929 
930 	nfs_put_client(server->nfs_client);
931 
932 	ida_destroy(&server->lockowner_id);
933 	ida_destroy(&server->openowner_id);
934 	nfs_free_iostats(server->io_stats);
935 	bdi_destroy(&server->backing_dev_info);
936 	kfree(server);
937 	nfs_release_automount_timer();
938 	dprintk("<-- nfs_free_server()\n");
939 }
940 EXPORT_SYMBOL_GPL(nfs_free_server);
941 
942 /*
943  * Create a version 2 or 3 volume record
944  * - keyed on server and FSID
945  */
946 struct nfs_server *nfs_create_server(struct nfs_mount_info *mount_info,
947 				     struct nfs_subversion *nfs_mod)
948 {
949 	struct nfs_server *server;
950 	struct nfs_fattr *fattr;
951 	int error;
952 
953 	server = nfs_alloc_server();
954 	if (!server)
955 		return ERR_PTR(-ENOMEM);
956 
957 	error = -ENOMEM;
958 	fattr = nfs_alloc_fattr();
959 	if (fattr == NULL)
960 		goto error;
961 
962 	/* Get a client representation */
963 	error = nfs_init_server(server, mount_info->parsed, nfs_mod);
964 	if (error < 0)
965 		goto error;
966 
967 	/* Probe the root fh to retrieve its FSID */
968 	error = nfs_probe_fsinfo(server, mount_info->mntfh, fattr);
969 	if (error < 0)
970 		goto error;
971 	if (server->nfs_client->rpc_ops->version == 3) {
972 		if (server->namelen == 0 || server->namelen > NFS3_MAXNAMLEN)
973 			server->namelen = NFS3_MAXNAMLEN;
974 		if (!(mount_info->parsed->flags & NFS_MOUNT_NORDIRPLUS))
975 			server->caps |= NFS_CAP_READDIRPLUS;
976 	} else {
977 		if (server->namelen == 0 || server->namelen > NFS2_MAXNAMLEN)
978 			server->namelen = NFS2_MAXNAMLEN;
979 	}
980 
981 	if (!(fattr->valid & NFS_ATTR_FATTR)) {
982 		error = nfs_mod->rpc_ops->getattr(server, mount_info->mntfh, fattr, NULL);
983 		if (error < 0) {
984 			dprintk("nfs_create_server: getattr error = %d\n", -error);
985 			goto error;
986 		}
987 	}
988 	memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
989 
990 	dprintk("Server FSID: %llx:%llx\n",
991 		(unsigned long long) server->fsid.major,
992 		(unsigned long long) server->fsid.minor);
993 
994 	nfs_server_insert_lists(server);
995 	server->mount_time = jiffies;
996 	nfs_free_fattr(fattr);
997 	return server;
998 
999 error:
1000 	nfs_free_fattr(fattr);
1001 	nfs_free_server(server);
1002 	return ERR_PTR(error);
1003 }
1004 EXPORT_SYMBOL_GPL(nfs_create_server);
1005 
1006 /*
1007  * Clone an NFS2, NFS3 or NFS4 server record
1008  */
1009 struct nfs_server *nfs_clone_server(struct nfs_server *source,
1010 				    struct nfs_fh *fh,
1011 				    struct nfs_fattr *fattr,
1012 				    rpc_authflavor_t flavor)
1013 {
1014 	struct nfs_server *server;
1015 	struct nfs_fattr *fattr_fsinfo;
1016 	int error;
1017 
1018 	dprintk("--> nfs_clone_server(,%llx:%llx,)\n",
1019 		(unsigned long long) fattr->fsid.major,
1020 		(unsigned long long) fattr->fsid.minor);
1021 
1022 	server = nfs_alloc_server();
1023 	if (!server)
1024 		return ERR_PTR(-ENOMEM);
1025 
1026 	error = -ENOMEM;
1027 	fattr_fsinfo = nfs_alloc_fattr();
1028 	if (fattr_fsinfo == NULL)
1029 		goto out_free_server;
1030 
1031 	/* Copy data from the source */
1032 	server->nfs_client = source->nfs_client;
1033 	server->destroy = source->destroy;
1034 	atomic_inc(&server->nfs_client->cl_count);
1035 	nfs_server_copy_userdata(server, source);
1036 
1037 	server->fsid = fattr->fsid;
1038 
1039 	error = nfs_init_server_rpcclient(server,
1040 			source->client->cl_timeout,
1041 			flavor);
1042 	if (error < 0)
1043 		goto out_free_server;
1044 
1045 	/* probe the filesystem info for this server filesystem */
1046 	error = nfs_probe_fsinfo(server, fh, fattr_fsinfo);
1047 	if (error < 0)
1048 		goto out_free_server;
1049 
1050 	if (server->namelen == 0 || server->namelen > NFS4_MAXNAMLEN)
1051 		server->namelen = NFS4_MAXNAMLEN;
1052 
1053 	dprintk("Cloned FSID: %llx:%llx\n",
1054 		(unsigned long long) server->fsid.major,
1055 		(unsigned long long) server->fsid.minor);
1056 
1057 	error = nfs_start_lockd(server);
1058 	if (error < 0)
1059 		goto out_free_server;
1060 
1061 	nfs_server_insert_lists(server);
1062 	server->mount_time = jiffies;
1063 
1064 	nfs_free_fattr(fattr_fsinfo);
1065 	dprintk("<-- nfs_clone_server() = %p\n", server);
1066 	return server;
1067 
1068 out_free_server:
1069 	nfs_free_fattr(fattr_fsinfo);
1070 	nfs_free_server(server);
1071 	dprintk("<-- nfs_clone_server() = error %d\n", error);
1072 	return ERR_PTR(error);
1073 }
1074 EXPORT_SYMBOL_GPL(nfs_clone_server);
1075 
1076 void nfs_clients_init(struct net *net)
1077 {
1078 	struct nfs_net *nn = net_generic(net, nfs_net_id);
1079 
1080 	INIT_LIST_HEAD(&nn->nfs_client_list);
1081 	INIT_LIST_HEAD(&nn->nfs_volume_list);
1082 #if IS_ENABLED(CONFIG_NFS_V4)
1083 	idr_init(&nn->cb_ident_idr);
1084 #endif
1085 	spin_lock_init(&nn->nfs_client_lock);
1086 	nn->boot_time = ktime_get_real();
1087 }
1088 
1089 #ifdef CONFIG_PROC_FS
1090 static int nfs_server_list_open(struct inode *inode, struct file *file);
1091 static void *nfs_server_list_start(struct seq_file *p, loff_t *pos);
1092 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos);
1093 static void nfs_server_list_stop(struct seq_file *p, void *v);
1094 static int nfs_server_list_show(struct seq_file *m, void *v);
1095 
1096 static const struct seq_operations nfs_server_list_ops = {
1097 	.start	= nfs_server_list_start,
1098 	.next	= nfs_server_list_next,
1099 	.stop	= nfs_server_list_stop,
1100 	.show	= nfs_server_list_show,
1101 };
1102 
1103 static const struct file_operations nfs_server_list_fops = {
1104 	.open		= nfs_server_list_open,
1105 	.read		= seq_read,
1106 	.llseek		= seq_lseek,
1107 	.release	= seq_release_net,
1108 };
1109 
1110 static int nfs_volume_list_open(struct inode *inode, struct file *file);
1111 static void *nfs_volume_list_start(struct seq_file *p, loff_t *pos);
1112 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos);
1113 static void nfs_volume_list_stop(struct seq_file *p, void *v);
1114 static int nfs_volume_list_show(struct seq_file *m, void *v);
1115 
1116 static const struct seq_operations nfs_volume_list_ops = {
1117 	.start	= nfs_volume_list_start,
1118 	.next	= nfs_volume_list_next,
1119 	.stop	= nfs_volume_list_stop,
1120 	.show	= nfs_volume_list_show,
1121 };
1122 
1123 static const struct file_operations nfs_volume_list_fops = {
1124 	.open		= nfs_volume_list_open,
1125 	.read		= seq_read,
1126 	.llseek		= seq_lseek,
1127 	.release	= seq_release_net,
1128 };
1129 
1130 /*
1131  * open "/proc/fs/nfsfs/servers" which provides a summary of servers with which
1132  * we're dealing
1133  */
1134 static int nfs_server_list_open(struct inode *inode, struct file *file)
1135 {
1136 	return seq_open_net(inode, file, &nfs_server_list_ops,
1137 			   sizeof(struct seq_net_private));
1138 }
1139 
1140 /*
1141  * set up the iterator to start reading from the server list and return the first item
1142  */
1143 static void *nfs_server_list_start(struct seq_file *m, loff_t *_pos)
1144 				__acquires(&nn->nfs_client_lock)
1145 {
1146 	struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id);
1147 
1148 	/* lock the list against modification */
1149 	spin_lock(&nn->nfs_client_lock);
1150 	return seq_list_start_head(&nn->nfs_client_list, *_pos);
1151 }
1152 
1153 /*
1154  * move to next server
1155  */
1156 static void *nfs_server_list_next(struct seq_file *p, void *v, loff_t *pos)
1157 {
1158 	struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id);
1159 
1160 	return seq_list_next(v, &nn->nfs_client_list, pos);
1161 }
1162 
1163 /*
1164  * clean up after reading from the transports list
1165  */
1166 static void nfs_server_list_stop(struct seq_file *p, void *v)
1167 				__releases(&nn->nfs_client_lock)
1168 {
1169 	struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id);
1170 
1171 	spin_unlock(&nn->nfs_client_lock);
1172 }
1173 
1174 /*
1175  * display a header line followed by a load of call lines
1176  */
1177 static int nfs_server_list_show(struct seq_file *m, void *v)
1178 {
1179 	struct nfs_client *clp;
1180 	struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id);
1181 
1182 	/* display header on line 1 */
1183 	if (v == &nn->nfs_client_list) {
1184 		seq_puts(m, "NV SERVER   PORT USE HOSTNAME\n");
1185 		return 0;
1186 	}
1187 
1188 	/* display one transport per line on subsequent lines */
1189 	clp = list_entry(v, struct nfs_client, cl_share_link);
1190 
1191 	/* Check if the client is initialized */
1192 	if (clp->cl_cons_state != NFS_CS_READY)
1193 		return 0;
1194 
1195 	rcu_read_lock();
1196 	seq_printf(m, "v%u %s %s %3d %s\n",
1197 		   clp->rpc_ops->version,
1198 		   rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1199 		   rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
1200 		   atomic_read(&clp->cl_count),
1201 		   clp->cl_hostname);
1202 	rcu_read_unlock();
1203 
1204 	return 0;
1205 }
1206 
1207 /*
1208  * open "/proc/fs/nfsfs/volumes" which provides a summary of extant volumes
1209  */
1210 static int nfs_volume_list_open(struct inode *inode, struct file *file)
1211 {
1212 	return seq_open_net(inode, file, &nfs_volume_list_ops,
1213 			   sizeof(struct seq_net_private));
1214 }
1215 
1216 /*
1217  * set up the iterator to start reading from the volume list and return the first item
1218  */
1219 static void *nfs_volume_list_start(struct seq_file *m, loff_t *_pos)
1220 				__acquires(&nn->nfs_client_lock)
1221 {
1222 	struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id);
1223 
1224 	/* lock the list against modification */
1225 	spin_lock(&nn->nfs_client_lock);
1226 	return seq_list_start_head(&nn->nfs_volume_list, *_pos);
1227 }
1228 
1229 /*
1230  * move to next volume
1231  */
1232 static void *nfs_volume_list_next(struct seq_file *p, void *v, loff_t *pos)
1233 {
1234 	struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id);
1235 
1236 	return seq_list_next(v, &nn->nfs_volume_list, pos);
1237 }
1238 
1239 /*
1240  * clean up after reading from the transports list
1241  */
1242 static void nfs_volume_list_stop(struct seq_file *p, void *v)
1243 				__releases(&nn->nfs_client_lock)
1244 {
1245 	struct nfs_net *nn = net_generic(seq_file_net(p), nfs_net_id);
1246 
1247 	spin_unlock(&nn->nfs_client_lock);
1248 }
1249 
1250 /*
1251  * display a header line followed by a load of call lines
1252  */
1253 static int nfs_volume_list_show(struct seq_file *m, void *v)
1254 {
1255 	struct nfs_server *server;
1256 	struct nfs_client *clp;
1257 	char dev[13];	// 8 for 2^24, 1 for ':', 3 for 2^8, 1 for '\0'
1258 	char fsid[34];	// 2 * 16 for %llx, 1 for ':', 1 for '\0'
1259 	struct nfs_net *nn = net_generic(seq_file_net(m), nfs_net_id);
1260 
1261 	/* display header on line 1 */
1262 	if (v == &nn->nfs_volume_list) {
1263 		seq_puts(m, "NV SERVER   PORT DEV          FSID"
1264 			    "                              FSC\n");
1265 		return 0;
1266 	}
1267 	/* display one transport per line on subsequent lines */
1268 	server = list_entry(v, struct nfs_server, master_link);
1269 	clp = server->nfs_client;
1270 
1271 	snprintf(dev, sizeof(dev), "%u:%u",
1272 		 MAJOR(server->s_dev), MINOR(server->s_dev));
1273 
1274 	snprintf(fsid, sizeof(fsid), "%llx:%llx",
1275 		 (unsigned long long) server->fsid.major,
1276 		 (unsigned long long) server->fsid.minor);
1277 
1278 	rcu_read_lock();
1279 	seq_printf(m, "v%u %s %s %-12s %-33s %s\n",
1280 		   clp->rpc_ops->version,
1281 		   rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_ADDR),
1282 		   rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_HEX_PORT),
1283 		   dev,
1284 		   fsid,
1285 		   nfs_server_fscache_state(server));
1286 	rcu_read_unlock();
1287 
1288 	return 0;
1289 }
1290 
1291 int nfs_fs_proc_net_init(struct net *net)
1292 {
1293 	struct nfs_net *nn = net_generic(net, nfs_net_id);
1294 	struct proc_dir_entry *p;
1295 
1296 	nn->proc_nfsfs = proc_net_mkdir(net, "nfsfs", net->proc_net);
1297 	if (!nn->proc_nfsfs)
1298 		goto error_0;
1299 
1300 	/* a file of servers with which we're dealing */
1301 	p = proc_create("servers", S_IFREG|S_IRUGO,
1302 			nn->proc_nfsfs, &nfs_server_list_fops);
1303 	if (!p)
1304 		goto error_1;
1305 
1306 	/* a file of volumes that we have mounted */
1307 	p = proc_create("volumes", S_IFREG|S_IRUGO,
1308 			nn->proc_nfsfs, &nfs_volume_list_fops);
1309 	if (!p)
1310 		goto error_1;
1311 	return 0;
1312 
1313 error_1:
1314 	remove_proc_subtree("nfsfs", net->proc_net);
1315 error_0:
1316 	return -ENOMEM;
1317 }
1318 
1319 void nfs_fs_proc_net_exit(struct net *net)
1320 {
1321 	remove_proc_subtree("nfsfs", net->proc_net);
1322 }
1323 
1324 /*
1325  * initialise the /proc/fs/nfsfs/ directory
1326  */
1327 int __init nfs_fs_proc_init(void)
1328 {
1329 	if (!proc_mkdir("fs/nfsfs", NULL))
1330 		goto error_0;
1331 
1332 	/* a file of servers with which we're dealing */
1333 	if (!proc_symlink("fs/nfsfs/servers", NULL, "../../net/nfsfs/servers"))
1334 		goto error_1;
1335 
1336 	/* a file of volumes that we have mounted */
1337 	if (!proc_symlink("fs/nfsfs/volumes", NULL, "../../net/nfsfs/volumes"))
1338 		goto error_1;
1339 
1340 	return 0;
1341 error_1:
1342 	remove_proc_subtree("fs/nfsfs", NULL);
1343 error_0:
1344 	return -ENOMEM;
1345 }
1346 
1347 /*
1348  * clean up the /proc/fs/nfsfs/ directory
1349  */
1350 void nfs_fs_proc_exit(void)
1351 {
1352 	remove_proc_subtree("fs/nfsfs", NULL);
1353 }
1354 
1355 #endif /* CONFIG_PROC_FS */
1356