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