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