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