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