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