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