xref: /linux/fs/nfsd/nfsctl.c (revision 616500232e632dba8b03981eeccadacf2fbf1c30)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Syscall interface to knfsd.
4  *
5  * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de>
6  */
7 
8 #include <linux/slab.h>
9 #include <linux/namei.h>
10 #include <linux/ctype.h>
11 #include <linux/fs_context.h>
12 
13 #include <linux/sunrpc/svcsock.h>
14 #include <linux/lockd/lockd.h>
15 #include <linux/sunrpc/addr.h>
16 #include <linux/sunrpc/gss_api.h>
17 #include <linux/sunrpc/rpc_pipe_fs.h>
18 #include <linux/module.h>
19 #include <linux/fsnotify.h>
20 
21 #include "idmap.h"
22 #include "nfsd.h"
23 #include "cache.h"
24 #include "state.h"
25 #include "netns.h"
26 #include "pnfs.h"
27 #include "filecache.h"
28 
29 /*
30  *	We have a single directory with several nodes in it.
31  */
32 enum {
33 	NFSD_Root = 1,
34 	NFSD_List,
35 	NFSD_Export_Stats,
36 	NFSD_Export_features,
37 	NFSD_Fh,
38 	NFSD_FO_UnlockIP,
39 	NFSD_FO_UnlockFS,
40 	NFSD_Threads,
41 	NFSD_Pool_Threads,
42 	NFSD_Pool_Stats,
43 	NFSD_Reply_Cache_Stats,
44 	NFSD_Versions,
45 	NFSD_Ports,
46 	NFSD_MaxBlkSize,
47 	NFSD_MaxConnections,
48 	NFSD_Filecache,
49 	/*
50 	 * The below MUST come last.  Otherwise we leave a hole in nfsd_files[]
51 	 * with !CONFIG_NFSD_V4 and simple_fill_super() goes oops
52 	 */
53 #ifdef CONFIG_NFSD_V4
54 	NFSD_Leasetime,
55 	NFSD_Gracetime,
56 	NFSD_RecoveryDir,
57 	NFSD_V4EndGrace,
58 #endif
59 	NFSD_MaxReserved
60 };
61 
62 /*
63  * write() for these nodes.
64  */
65 static ssize_t write_filehandle(struct file *file, char *buf, size_t size);
66 static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size);
67 static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size);
68 static ssize_t write_threads(struct file *file, char *buf, size_t size);
69 static ssize_t write_pool_threads(struct file *file, char *buf, size_t size);
70 static ssize_t write_versions(struct file *file, char *buf, size_t size);
71 static ssize_t write_ports(struct file *file, char *buf, size_t size);
72 static ssize_t write_maxblksize(struct file *file, char *buf, size_t size);
73 static ssize_t write_maxconn(struct file *file, char *buf, size_t size);
74 #ifdef CONFIG_NFSD_V4
75 static ssize_t write_leasetime(struct file *file, char *buf, size_t size);
76 static ssize_t write_gracetime(struct file *file, char *buf, size_t size);
77 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size);
78 static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size);
79 #endif
80 
81 static ssize_t (*const write_op[])(struct file *, char *, size_t) = {
82 	[NFSD_Fh] = write_filehandle,
83 	[NFSD_FO_UnlockIP] = write_unlock_ip,
84 	[NFSD_FO_UnlockFS] = write_unlock_fs,
85 	[NFSD_Threads] = write_threads,
86 	[NFSD_Pool_Threads] = write_pool_threads,
87 	[NFSD_Versions] = write_versions,
88 	[NFSD_Ports] = write_ports,
89 	[NFSD_MaxBlkSize] = write_maxblksize,
90 	[NFSD_MaxConnections] = write_maxconn,
91 #ifdef CONFIG_NFSD_V4
92 	[NFSD_Leasetime] = write_leasetime,
93 	[NFSD_Gracetime] = write_gracetime,
94 	[NFSD_RecoveryDir] = write_recoverydir,
95 	[NFSD_V4EndGrace] = write_v4_end_grace,
96 #endif
97 };
98 
99 static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
100 {
101 	ino_t ino =  file_inode(file)->i_ino;
102 	char *data;
103 	ssize_t rv;
104 
105 	if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
106 		return -EINVAL;
107 
108 	data = simple_transaction_get(file, buf, size);
109 	if (IS_ERR(data))
110 		return PTR_ERR(data);
111 
112 	rv =  write_op[ino](file, data, size);
113 	if (rv >= 0) {
114 		simple_transaction_set(file, rv);
115 		rv = size;
116 	}
117 	return rv;
118 }
119 
120 static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
121 {
122 	if (! file->private_data) {
123 		/* An attempt to read a transaction file without writing
124 		 * causes a 0-byte write so that the file can return
125 		 * state information
126 		 */
127 		ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos);
128 		if (rv < 0)
129 			return rv;
130 	}
131 	return simple_transaction_read(file, buf, size, pos);
132 }
133 
134 static const struct file_operations transaction_ops = {
135 	.write		= nfsctl_transaction_write,
136 	.read		= nfsctl_transaction_read,
137 	.release	= simple_transaction_release,
138 	.llseek		= default_llseek,
139 };
140 
141 static int exports_net_open(struct net *net, struct file *file)
142 {
143 	int err;
144 	struct seq_file *seq;
145 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
146 
147 	err = seq_open(file, &nfs_exports_op);
148 	if (err)
149 		return err;
150 
151 	seq = file->private_data;
152 	seq->private = nn->svc_export_cache;
153 	return 0;
154 }
155 
156 static int exports_nfsd_open(struct inode *inode, struct file *file)
157 {
158 	return exports_net_open(inode->i_sb->s_fs_info, file);
159 }
160 
161 static const struct file_operations exports_nfsd_operations = {
162 	.open		= exports_nfsd_open,
163 	.read		= seq_read,
164 	.llseek		= seq_lseek,
165 	.release	= seq_release,
166 };
167 
168 static int export_features_show(struct seq_file *m, void *v)
169 {
170 	seq_printf(m, "0x%x 0x%x\n", NFSEXP_ALLFLAGS, NFSEXP_SECINFO_FLAGS);
171 	return 0;
172 }
173 
174 DEFINE_SHOW_ATTRIBUTE(export_features);
175 
176 static const struct file_operations pool_stats_operations = {
177 	.open		= nfsd_pool_stats_open,
178 	.read		= seq_read,
179 	.llseek		= seq_lseek,
180 	.release	= nfsd_pool_stats_release,
181 };
182 
183 DEFINE_SHOW_ATTRIBUTE(nfsd_reply_cache_stats);
184 
185 DEFINE_SHOW_ATTRIBUTE(nfsd_file_cache_stats);
186 
187 /*----------------------------------------------------------------------------*/
188 /*
189  * payload - write methods
190  */
191 
192 static inline struct net *netns(struct file *file)
193 {
194 	return file_inode(file)->i_sb->s_fs_info;
195 }
196 
197 /*
198  * write_unlock_ip - Release all locks used by a client
199  *
200  * Experimental.
201  *
202  * Input:
203  *			buf:	'\n'-terminated C string containing a
204  *				presentation format IP address
205  *			size:	length of C string in @buf
206  * Output:
207  *	On success:	returns zero if all specified locks were released;
208  *			returns one if one or more locks were not released
209  *	On error:	return code is negative errno value
210  */
211 static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size)
212 {
213 	struct sockaddr_storage address;
214 	struct sockaddr *sap = (struct sockaddr *)&address;
215 	size_t salen = sizeof(address);
216 	char *fo_path;
217 	struct net *net = netns(file);
218 
219 	/* sanity check */
220 	if (size == 0)
221 		return -EINVAL;
222 
223 	if (buf[size-1] != '\n')
224 		return -EINVAL;
225 
226 	fo_path = buf;
227 	if (qword_get(&buf, fo_path, size) < 0)
228 		return -EINVAL;
229 
230 	if (rpc_pton(net, fo_path, size, sap, salen) == 0)
231 		return -EINVAL;
232 
233 	return nlmsvc_unlock_all_by_ip(sap);
234 }
235 
236 /*
237  * write_unlock_fs - Release all locks on a local file system
238  *
239  * Experimental.
240  *
241  * Input:
242  *			buf:	'\n'-terminated C string containing the
243  *				absolute pathname of a local file system
244  *			size:	length of C string in @buf
245  * Output:
246  *	On success:	returns zero if all specified locks were released;
247  *			returns one if one or more locks were not released
248  *	On error:	return code is negative errno value
249  */
250 static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size)
251 {
252 	struct path path;
253 	char *fo_path;
254 	int error;
255 
256 	/* sanity check */
257 	if (size == 0)
258 		return -EINVAL;
259 
260 	if (buf[size-1] != '\n')
261 		return -EINVAL;
262 
263 	fo_path = buf;
264 	if (qword_get(&buf, fo_path, size) < 0)
265 		return -EINVAL;
266 
267 	error = kern_path(fo_path, 0, &path);
268 	if (error)
269 		return error;
270 
271 	/*
272 	 * XXX: Needs better sanity checking.  Otherwise we could end up
273 	 * releasing locks on the wrong file system.
274 	 *
275 	 * For example:
276 	 * 1.  Does the path refer to a directory?
277 	 * 2.  Is that directory a mount point, or
278 	 * 3.  Is that directory the root of an exported file system?
279 	 */
280 	error = nlmsvc_unlock_all_by_sb(path.dentry->d_sb);
281 
282 	path_put(&path);
283 	return error;
284 }
285 
286 /*
287  * write_filehandle - Get a variable-length NFS file handle by path
288  *
289  * On input, the buffer contains a '\n'-terminated C string comprised of
290  * three alphanumeric words separated by whitespace.  The string may
291  * contain escape sequences.
292  *
293  * Input:
294  *			buf:
295  *				domain:		client domain name
296  *				path:		export pathname
297  *				maxsize:	numeric maximum size of
298  *						@buf
299  *			size:	length of C string in @buf
300  * Output:
301  *	On success:	passed-in buffer filled with '\n'-terminated C
302  *			string containing a ASCII hex text version
303  *			of the NFS file handle;
304  *			return code is the size in bytes of the string
305  *	On error:	return code is negative errno value
306  */
307 static ssize_t write_filehandle(struct file *file, char *buf, size_t size)
308 {
309 	char *dname, *path;
310 	int maxsize;
311 	char *mesg = buf;
312 	int len;
313 	struct auth_domain *dom;
314 	struct knfsd_fh fh;
315 
316 	if (size == 0)
317 		return -EINVAL;
318 
319 	if (buf[size-1] != '\n')
320 		return -EINVAL;
321 	buf[size-1] = 0;
322 
323 	dname = mesg;
324 	len = qword_get(&mesg, dname, size);
325 	if (len <= 0)
326 		return -EINVAL;
327 
328 	path = dname+len+1;
329 	len = qword_get(&mesg, path, size);
330 	if (len <= 0)
331 		return -EINVAL;
332 
333 	len = get_int(&mesg, &maxsize);
334 	if (len)
335 		return len;
336 
337 	if (maxsize < NFS_FHSIZE)
338 		return -EINVAL;
339 	maxsize = min(maxsize, NFS3_FHSIZE);
340 
341 	if (qword_get(&mesg, mesg, size)>0)
342 		return -EINVAL;
343 
344 	/* we have all the words, they are in buf.. */
345 	dom = unix_domain_find(dname);
346 	if (!dom)
347 		return -ENOMEM;
348 
349 	len = exp_rootfh(netns(file), dom, path, &fh,  maxsize);
350 	auth_domain_put(dom);
351 	if (len)
352 		return len;
353 
354 	mesg = buf;
355 	len = SIMPLE_TRANSACTION_LIMIT;
356 	qword_addhex(&mesg, &len, fh.fh_raw, fh.fh_size);
357 	mesg[-1] = '\n';
358 	return mesg - buf;
359 }
360 
361 /*
362  * write_threads - Start NFSD, or report the current number of running threads
363  *
364  * Input:
365  *			buf:		ignored
366  *			size:		zero
367  * Output:
368  *	On success:	passed-in buffer filled with '\n'-terminated C
369  *			string numeric value representing the number of
370  *			running NFSD threads;
371  *			return code is the size in bytes of the string
372  *	On error:	return code is zero
373  *
374  * OR
375  *
376  * Input:
377  *			buf:		C string containing an unsigned
378  *					integer value representing the
379  *					number of NFSD threads to start
380  *			size:		non-zero length of C string in @buf
381  * Output:
382  *	On success:	NFS service is started;
383  *			passed-in buffer filled with '\n'-terminated C
384  *			string numeric value representing the number of
385  *			running NFSD threads;
386  *			return code is the size in bytes of the string
387  *	On error:	return code is zero or a negative errno value
388  */
389 static ssize_t write_threads(struct file *file, char *buf, size_t size)
390 {
391 	char *mesg = buf;
392 	int rv;
393 	struct net *net = netns(file);
394 
395 	if (size > 0) {
396 		int newthreads;
397 		rv = get_int(&mesg, &newthreads);
398 		if (rv)
399 			return rv;
400 		if (newthreads < 0)
401 			return -EINVAL;
402 		rv = nfsd_svc(newthreads, net, file->f_cred);
403 		if (rv < 0)
404 			return rv;
405 	} else
406 		rv = nfsd_nrthreads(net);
407 
408 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", rv);
409 }
410 
411 /*
412  * write_pool_threads - Set or report the current number of threads per pool
413  *
414  * Input:
415  *			buf:		ignored
416  *			size:		zero
417  *
418  * OR
419  *
420  * Input:
421  * 			buf:		C string containing whitespace-
422  * 					separated unsigned integer values
423  *					representing the number of NFSD
424  *					threads to start in each pool
425  *			size:		non-zero length of C string in @buf
426  * Output:
427  *	On success:	passed-in buffer filled with '\n'-terminated C
428  *			string containing integer values representing the
429  *			number of NFSD threads in each pool;
430  *			return code is the size in bytes of the string
431  *	On error:	return code is zero or a negative errno value
432  */
433 static ssize_t write_pool_threads(struct file *file, char *buf, size_t size)
434 {
435 	/* if size > 0, look for an array of number of threads per node
436 	 * and apply them  then write out number of threads per node as reply
437 	 */
438 	char *mesg = buf;
439 	int i;
440 	int rv;
441 	int len;
442 	int npools;
443 	int *nthreads;
444 	struct net *net = netns(file);
445 
446 	mutex_lock(&nfsd_mutex);
447 	npools = nfsd_nrpools(net);
448 	if (npools == 0) {
449 		/*
450 		 * NFS is shut down.  The admin can start it by
451 		 * writing to the threads file but NOT the pool_threads
452 		 * file, sorry.  Report zero threads.
453 		 */
454 		mutex_unlock(&nfsd_mutex);
455 		strcpy(buf, "0\n");
456 		return strlen(buf);
457 	}
458 
459 	nthreads = kcalloc(npools, sizeof(int), GFP_KERNEL);
460 	rv = -ENOMEM;
461 	if (nthreads == NULL)
462 		goto out_free;
463 
464 	if (size > 0) {
465 		for (i = 0; i < npools; i++) {
466 			rv = get_int(&mesg, &nthreads[i]);
467 			if (rv == -ENOENT)
468 				break;		/* fewer numbers than pools */
469 			if (rv)
470 				goto out_free;	/* syntax error */
471 			rv = -EINVAL;
472 			if (nthreads[i] < 0)
473 				goto out_free;
474 		}
475 		rv = nfsd_set_nrthreads(i, nthreads, net);
476 		if (rv)
477 			goto out_free;
478 	}
479 
480 	rv = nfsd_get_nrthreads(npools, nthreads, net);
481 	if (rv)
482 		goto out_free;
483 
484 	mesg = buf;
485 	size = SIMPLE_TRANSACTION_LIMIT;
486 	for (i = 0; i < npools && size > 0; i++) {
487 		snprintf(mesg, size, "%d%c", nthreads[i], (i == npools-1 ? '\n' : ' '));
488 		len = strlen(mesg);
489 		size -= len;
490 		mesg += len;
491 	}
492 	rv = mesg - buf;
493 out_free:
494 	kfree(nthreads);
495 	mutex_unlock(&nfsd_mutex);
496 	return rv;
497 }
498 
499 static ssize_t
500 nfsd_print_version_support(struct nfsd_net *nn, char *buf, int remaining,
501 		const char *sep, unsigned vers, int minor)
502 {
503 	const char *format = minor < 0 ? "%s%c%u" : "%s%c%u.%u";
504 	bool supported = !!nfsd_vers(nn, vers, NFSD_TEST);
505 
506 	if (vers == 4 && minor >= 0 &&
507 	    !nfsd_minorversion(nn, minor, NFSD_TEST))
508 		supported = false;
509 	if (minor == 0 && supported)
510 		/*
511 		 * special case for backward compatability.
512 		 * +4.0 is never reported, it is implied by
513 		 * +4, unless -4.0 is present.
514 		 */
515 		return 0;
516 	return snprintf(buf, remaining, format, sep,
517 			supported ? '+' : '-', vers, minor);
518 }
519 
520 static ssize_t __write_versions(struct file *file, char *buf, size_t size)
521 {
522 	char *mesg = buf;
523 	char *vers, *minorp, sign;
524 	int len, num, remaining;
525 	ssize_t tlen = 0;
526 	char *sep;
527 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
528 
529 	if (size>0) {
530 		if (nn->nfsd_serv)
531 			/* Cannot change versions without updating
532 			 * nn->nfsd_serv->sv_xdrsize, and reallocing
533 			 * rq_argp and rq_resp
534 			 */
535 			return -EBUSY;
536 		if (buf[size-1] != '\n')
537 			return -EINVAL;
538 		buf[size-1] = 0;
539 
540 		vers = mesg;
541 		len = qword_get(&mesg, vers, size);
542 		if (len <= 0) return -EINVAL;
543 		do {
544 			enum vers_op cmd;
545 			unsigned minor;
546 			sign = *vers;
547 			if (sign == '+' || sign == '-')
548 				num = simple_strtol((vers+1), &minorp, 0);
549 			else
550 				num = simple_strtol(vers, &minorp, 0);
551 			if (*minorp == '.') {
552 				if (num != 4)
553 					return -EINVAL;
554 				if (kstrtouint(minorp+1, 0, &minor) < 0)
555 					return -EINVAL;
556 			}
557 
558 			cmd = sign == '-' ? NFSD_CLEAR : NFSD_SET;
559 			switch(num) {
560 #ifdef CONFIG_NFSD_V2
561 			case 2:
562 #endif
563 			case 3:
564 				nfsd_vers(nn, num, cmd);
565 				break;
566 			case 4:
567 				if (*minorp == '.') {
568 					if (nfsd_minorversion(nn, minor, cmd) < 0)
569 						return -EINVAL;
570 				} else if ((cmd == NFSD_SET) != nfsd_vers(nn, num, NFSD_TEST)) {
571 					/*
572 					 * Either we have +4 and no minors are enabled,
573 					 * or we have -4 and at least one minor is enabled.
574 					 * In either case, propagate 'cmd' to all minors.
575 					 */
576 					minor = 0;
577 					while (nfsd_minorversion(nn, minor, cmd) >= 0)
578 						minor++;
579 				}
580 				break;
581 			default:
582 				/* Ignore requests to disable non-existent versions */
583 				if (cmd == NFSD_SET)
584 					return -EINVAL;
585 			}
586 			vers += len + 1;
587 		} while ((len = qword_get(&mesg, vers, size)) > 0);
588 		/* If all get turned off, turn them back on, as
589 		 * having no versions is BAD
590 		 */
591 		nfsd_reset_versions(nn);
592 	}
593 
594 	/* Now write current state into reply buffer */
595 	sep = "";
596 	remaining = SIMPLE_TRANSACTION_LIMIT;
597 	for (num=2 ; num <= 4 ; num++) {
598 		int minor;
599 		if (!nfsd_vers(nn, num, NFSD_AVAIL))
600 			continue;
601 
602 		minor = -1;
603 		do {
604 			len = nfsd_print_version_support(nn, buf, remaining,
605 					sep, num, minor);
606 			if (len >= remaining)
607 				goto out;
608 			remaining -= len;
609 			buf += len;
610 			tlen += len;
611 			minor++;
612 			if (len)
613 				sep = " ";
614 		} while (num == 4 && minor <= NFSD_SUPPORTED_MINOR_VERSION);
615 	}
616 out:
617 	len = snprintf(buf, remaining, "\n");
618 	if (len >= remaining)
619 		return -EINVAL;
620 	return tlen + len;
621 }
622 
623 /*
624  * write_versions - Set or report the available NFS protocol versions
625  *
626  * Input:
627  *			buf:		ignored
628  *			size:		zero
629  * Output:
630  *	On success:	passed-in buffer filled with '\n'-terminated C
631  *			string containing positive or negative integer
632  *			values representing the current status of each
633  *			protocol version;
634  *			return code is the size in bytes of the string
635  *	On error:	return code is zero or a negative errno value
636  *
637  * OR
638  *
639  * Input:
640  * 			buf:		C string containing whitespace-
641  * 					separated positive or negative
642  * 					integer values representing NFS
643  * 					protocol versions to enable ("+n")
644  * 					or disable ("-n")
645  *			size:		non-zero length of C string in @buf
646  * Output:
647  *	On success:	status of zero or more protocol versions has
648  *			been updated; passed-in buffer filled with
649  *			'\n'-terminated C string containing positive
650  *			or negative integer values representing the
651  *			current status of each protocol version;
652  *			return code is the size in bytes of the string
653  *	On error:	return code is zero or a negative errno value
654  */
655 static ssize_t write_versions(struct file *file, char *buf, size_t size)
656 {
657 	ssize_t rv;
658 
659 	mutex_lock(&nfsd_mutex);
660 	rv = __write_versions(file, buf, size);
661 	mutex_unlock(&nfsd_mutex);
662 	return rv;
663 }
664 
665 /*
666  * Zero-length write.  Return a list of NFSD's current listener
667  * transports.
668  */
669 static ssize_t __write_ports_names(char *buf, struct net *net)
670 {
671 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
672 
673 	if (nn->nfsd_serv == NULL)
674 		return 0;
675 	return svc_xprt_names(nn->nfsd_serv, buf, SIMPLE_TRANSACTION_LIMIT);
676 }
677 
678 /*
679  * A single 'fd' number was written, in which case it must be for
680  * a socket of a supported family/protocol, and we use it as an
681  * nfsd listener.
682  */
683 static ssize_t __write_ports_addfd(char *buf, struct net *net, const struct cred *cred)
684 {
685 	char *mesg = buf;
686 	int fd, err;
687 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
688 
689 	err = get_int(&mesg, &fd);
690 	if (err != 0 || fd < 0)
691 		return -EINVAL;
692 
693 	err = nfsd_create_serv(net);
694 	if (err != 0)
695 		return err;
696 
697 	err = svc_addsock(nn->nfsd_serv, net, fd, buf, SIMPLE_TRANSACTION_LIMIT, cred);
698 
699 	if (err >= 0 &&
700 	    !nn->nfsd_serv->sv_nrthreads && !xchg(&nn->keep_active, 1))
701 		svc_get(nn->nfsd_serv);
702 
703 	nfsd_put(net);
704 	return err;
705 }
706 
707 /*
708  * A transport listener is added by writing it's transport name and
709  * a port number.
710  */
711 static ssize_t __write_ports_addxprt(char *buf, struct net *net, const struct cred *cred)
712 {
713 	char transport[16];
714 	struct svc_xprt *xprt;
715 	int port, err;
716 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
717 
718 	if (sscanf(buf, "%15s %5u", transport, &port) != 2)
719 		return -EINVAL;
720 
721 	if (port < 1 || port > USHRT_MAX)
722 		return -EINVAL;
723 
724 	err = nfsd_create_serv(net);
725 	if (err != 0)
726 		return err;
727 
728 	err = svc_xprt_create(nn->nfsd_serv, transport, net,
729 			      PF_INET, port, SVC_SOCK_ANONYMOUS, cred);
730 	if (err < 0)
731 		goto out_err;
732 
733 	err = svc_xprt_create(nn->nfsd_serv, transport, net,
734 			      PF_INET6, port, SVC_SOCK_ANONYMOUS, cred);
735 	if (err < 0 && err != -EAFNOSUPPORT)
736 		goto out_close;
737 
738 	if (!nn->nfsd_serv->sv_nrthreads && !xchg(&nn->keep_active, 1))
739 		svc_get(nn->nfsd_serv);
740 
741 	nfsd_put(net);
742 	return 0;
743 out_close:
744 	xprt = svc_find_xprt(nn->nfsd_serv, transport, net, PF_INET, port);
745 	if (xprt != NULL) {
746 		svc_xprt_close(xprt);
747 		svc_xprt_put(xprt);
748 	}
749 out_err:
750 	nfsd_put(net);
751 	return err;
752 }
753 
754 static ssize_t __write_ports(struct file *file, char *buf, size_t size,
755 			     struct net *net)
756 {
757 	if (size == 0)
758 		return __write_ports_names(buf, net);
759 
760 	if (isdigit(buf[0]))
761 		return __write_ports_addfd(buf, net, file->f_cred);
762 
763 	if (isalpha(buf[0]))
764 		return __write_ports_addxprt(buf, net, file->f_cred);
765 
766 	return -EINVAL;
767 }
768 
769 /*
770  * write_ports - Pass a socket file descriptor or transport name to listen on
771  *
772  * Input:
773  *			buf:		ignored
774  *			size:		zero
775  * Output:
776  *	On success:	passed-in buffer filled with a '\n'-terminated C
777  *			string containing a whitespace-separated list of
778  *			named NFSD listeners;
779  *			return code is the size in bytes of the string
780  *	On error:	return code is zero or a negative errno value
781  *
782  * OR
783  *
784  * Input:
785  *			buf:		C string containing an unsigned
786  *					integer value representing a bound
787  *					but unconnected socket that is to be
788  *					used as an NFSD listener; listen(3)
789  *					must be called for a SOCK_STREAM
790  *					socket, otherwise it is ignored
791  *			size:		non-zero length of C string in @buf
792  * Output:
793  *	On success:	NFS service is started;
794  *			passed-in buffer filled with a '\n'-terminated C
795  *			string containing a unique alphanumeric name of
796  *			the listener;
797  *			return code is the size in bytes of the string
798  *	On error:	return code is a negative errno value
799  *
800  * OR
801  *
802  * Input:
803  *			buf:		C string containing a transport
804  *					name and an unsigned integer value
805  *					representing the port to listen on,
806  *					separated by whitespace
807  *			size:		non-zero length of C string in @buf
808  * Output:
809  *	On success:	returns zero; NFS service is started
810  *	On error:	return code is a negative errno value
811  */
812 static ssize_t write_ports(struct file *file, char *buf, size_t size)
813 {
814 	ssize_t rv;
815 
816 	mutex_lock(&nfsd_mutex);
817 	rv = __write_ports(file, buf, size, netns(file));
818 	mutex_unlock(&nfsd_mutex);
819 	return rv;
820 }
821 
822 
823 int nfsd_max_blksize;
824 
825 /*
826  * write_maxblksize - Set or report the current NFS blksize
827  *
828  * Input:
829  *			buf:		ignored
830  *			size:		zero
831  *
832  * OR
833  *
834  * Input:
835  * 			buf:		C string containing an unsigned
836  * 					integer value representing the new
837  * 					NFS blksize
838  *			size:		non-zero length of C string in @buf
839  * Output:
840  *	On success:	passed-in buffer filled with '\n'-terminated C string
841  *			containing numeric value of the current NFS blksize
842  *			setting;
843  *			return code is the size in bytes of the string
844  *	On error:	return code is zero or a negative errno value
845  */
846 static ssize_t write_maxblksize(struct file *file, char *buf, size_t size)
847 {
848 	char *mesg = buf;
849 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
850 
851 	if (size > 0) {
852 		int bsize;
853 		int rv = get_int(&mesg, &bsize);
854 		if (rv)
855 			return rv;
856 		/* force bsize into allowed range and
857 		 * required alignment.
858 		 */
859 		bsize = max_t(int, bsize, 1024);
860 		bsize = min_t(int, bsize, NFSSVC_MAXBLKSIZE);
861 		bsize &= ~(1024-1);
862 		mutex_lock(&nfsd_mutex);
863 		if (nn->nfsd_serv) {
864 			mutex_unlock(&nfsd_mutex);
865 			return -EBUSY;
866 		}
867 		nfsd_max_blksize = bsize;
868 		mutex_unlock(&nfsd_mutex);
869 	}
870 
871 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n",
872 							nfsd_max_blksize);
873 }
874 
875 /*
876  * write_maxconn - Set or report the current max number of connections
877  *
878  * Input:
879  *			buf:		ignored
880  *			size:		zero
881  * OR
882  *
883  * Input:
884  * 			buf:		C string containing an unsigned
885  * 					integer value representing the new
886  * 					number of max connections
887  *			size:		non-zero length of C string in @buf
888  * Output:
889  *	On success:	passed-in buffer filled with '\n'-terminated C string
890  *			containing numeric value of max_connections setting
891  *			for this net namespace;
892  *			return code is the size in bytes of the string
893  *	On error:	return code is zero or a negative errno value
894  */
895 static ssize_t write_maxconn(struct file *file, char *buf, size_t size)
896 {
897 	char *mesg = buf;
898 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
899 	unsigned int maxconn = nn->max_connections;
900 
901 	if (size > 0) {
902 		int rv = get_uint(&mesg, &maxconn);
903 
904 		if (rv)
905 			return rv;
906 		nn->max_connections = maxconn;
907 	}
908 
909 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%u\n", maxconn);
910 }
911 
912 #ifdef CONFIG_NFSD_V4
913 static ssize_t __nfsd4_write_time(struct file *file, char *buf, size_t size,
914 				  time64_t *time, struct nfsd_net *nn)
915 {
916 	char *mesg = buf;
917 	int rv, i;
918 
919 	if (size > 0) {
920 		if (nn->nfsd_serv)
921 			return -EBUSY;
922 		rv = get_int(&mesg, &i);
923 		if (rv)
924 			return rv;
925 		/*
926 		 * Some sanity checking.  We don't have a reason for
927 		 * these particular numbers, but problems with the
928 		 * extremes are:
929 		 *	- Too short: the briefest network outage may
930 		 *	  cause clients to lose all their locks.  Also,
931 		 *	  the frequent polling may be wasteful.
932 		 *	- Too long: do you really want reboot recovery
933 		 *	  to take more than an hour?  Or to make other
934 		 *	  clients wait an hour before being able to
935 		 *	  revoke a dead client's locks?
936 		 */
937 		if (i < 10 || i > 3600)
938 			return -EINVAL;
939 		*time = i;
940 	}
941 
942 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%lld\n", *time);
943 }
944 
945 static ssize_t nfsd4_write_time(struct file *file, char *buf, size_t size,
946 				time64_t *time, struct nfsd_net *nn)
947 {
948 	ssize_t rv;
949 
950 	mutex_lock(&nfsd_mutex);
951 	rv = __nfsd4_write_time(file, buf, size, time, nn);
952 	mutex_unlock(&nfsd_mutex);
953 	return rv;
954 }
955 
956 /*
957  * write_leasetime - Set or report the current NFSv4 lease time
958  *
959  * Input:
960  *			buf:		ignored
961  *			size:		zero
962  *
963  * OR
964  *
965  * Input:
966  *			buf:		C string containing an unsigned
967  *					integer value representing the new
968  *					NFSv4 lease expiry time
969  *			size:		non-zero length of C string in @buf
970  * Output:
971  *	On success:	passed-in buffer filled with '\n'-terminated C
972  *			string containing unsigned integer value of the
973  *			current lease expiry time;
974  *			return code is the size in bytes of the string
975  *	On error:	return code is zero or a negative errno value
976  */
977 static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
978 {
979 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
980 	return nfsd4_write_time(file, buf, size, &nn->nfsd4_lease, nn);
981 }
982 
983 /*
984  * write_gracetime - Set or report current NFSv4 grace period time
985  *
986  * As above, but sets the time of the NFSv4 grace period.
987  *
988  * Note this should never be set to less than the *previous*
989  * lease-period time, but we don't try to enforce this.  (In the common
990  * case (a new boot), we don't know what the previous lease time was
991  * anyway.)
992  */
993 static ssize_t write_gracetime(struct file *file, char *buf, size_t size)
994 {
995 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
996 	return nfsd4_write_time(file, buf, size, &nn->nfsd4_grace, nn);
997 }
998 
999 static ssize_t __write_recoverydir(struct file *file, char *buf, size_t size,
1000 				   struct nfsd_net *nn)
1001 {
1002 	char *mesg = buf;
1003 	char *recdir;
1004 	int len, status;
1005 
1006 	if (size > 0) {
1007 		if (nn->nfsd_serv)
1008 			return -EBUSY;
1009 		if (size > PATH_MAX || buf[size-1] != '\n')
1010 			return -EINVAL;
1011 		buf[size-1] = 0;
1012 
1013 		recdir = mesg;
1014 		len = qword_get(&mesg, recdir, size);
1015 		if (len <= 0)
1016 			return -EINVAL;
1017 
1018 		status = nfs4_reset_recoverydir(recdir);
1019 		if (status)
1020 			return status;
1021 	}
1022 
1023 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%s\n",
1024 							nfs4_recoverydir());
1025 }
1026 
1027 /*
1028  * write_recoverydir - Set or report the pathname of the recovery directory
1029  *
1030  * Input:
1031  *			buf:		ignored
1032  *			size:		zero
1033  *
1034  * OR
1035  *
1036  * Input:
1037  *			buf:		C string containing the pathname
1038  *					of the directory on a local file
1039  *					system containing permanent NFSv4
1040  *					recovery data
1041  *			size:		non-zero length of C string in @buf
1042  * Output:
1043  *	On success:	passed-in buffer filled with '\n'-terminated C string
1044  *			containing the current recovery pathname setting;
1045  *			return code is the size in bytes of the string
1046  *	On error:	return code is zero or a negative errno value
1047  */
1048 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
1049 {
1050 	ssize_t rv;
1051 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
1052 
1053 	mutex_lock(&nfsd_mutex);
1054 	rv = __write_recoverydir(file, buf, size, nn);
1055 	mutex_unlock(&nfsd_mutex);
1056 	return rv;
1057 }
1058 
1059 /*
1060  * write_v4_end_grace - release grace period for nfsd's v4.x lock manager
1061  *
1062  * Input:
1063  *			buf:		ignored
1064  *			size:		zero
1065  * OR
1066  *
1067  * Input:
1068  * 			buf:		any value
1069  *			size:		non-zero length of C string in @buf
1070  * Output:
1071  *			passed-in buffer filled with "Y" or "N" with a newline
1072  *			and NULL-terminated C string. This indicates whether
1073  *			the grace period has ended in the current net
1074  *			namespace. Return code is the size in bytes of the
1075  *			string. Writing a string that starts with 'Y', 'y', or
1076  *			'1' to the file will end the grace period for nfsd's v4
1077  *			lock manager.
1078  */
1079 static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size)
1080 {
1081 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
1082 
1083 	if (size > 0) {
1084 		switch(buf[0]) {
1085 		case 'Y':
1086 		case 'y':
1087 		case '1':
1088 			if (!nn->nfsd_serv)
1089 				return -EBUSY;
1090 			nfsd4_end_grace(nn);
1091 			break;
1092 		default:
1093 			return -EINVAL;
1094 		}
1095 	}
1096 
1097 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%c\n",
1098 			 nn->grace_ended ? 'Y' : 'N');
1099 }
1100 
1101 #endif
1102 
1103 /*----------------------------------------------------------------------------*/
1104 /*
1105  *	populating the filesystem.
1106  */
1107 
1108 /* Basically copying rpc_get_inode. */
1109 static struct inode *nfsd_get_inode(struct super_block *sb, umode_t mode)
1110 {
1111 	struct inode *inode = new_inode(sb);
1112 	if (!inode)
1113 		return NULL;
1114 	/* Following advice from simple_fill_super documentation: */
1115 	inode->i_ino = iunique(sb, NFSD_MaxReserved);
1116 	inode->i_mode = mode;
1117 	inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
1118 	switch (mode & S_IFMT) {
1119 	case S_IFDIR:
1120 		inode->i_fop = &simple_dir_operations;
1121 		inode->i_op = &simple_dir_inode_operations;
1122 		inc_nlink(inode);
1123 		break;
1124 	case S_IFLNK:
1125 		inode->i_op = &simple_symlink_inode_operations;
1126 		break;
1127 	default:
1128 		break;
1129 	}
1130 	return inode;
1131 }
1132 
1133 static int __nfsd_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode, struct nfsdfs_client *ncl)
1134 {
1135 	struct inode *inode;
1136 
1137 	inode = nfsd_get_inode(dir->i_sb, mode);
1138 	if (!inode)
1139 		return -ENOMEM;
1140 	if (ncl) {
1141 		inode->i_private = ncl;
1142 		kref_get(&ncl->cl_ref);
1143 	}
1144 	d_add(dentry, inode);
1145 	inc_nlink(dir);
1146 	fsnotify_mkdir(dir, dentry);
1147 	return 0;
1148 }
1149 
1150 static struct dentry *nfsd_mkdir(struct dentry *parent, struct nfsdfs_client *ncl, char *name)
1151 {
1152 	struct inode *dir = parent->d_inode;
1153 	struct dentry *dentry;
1154 	int ret = -ENOMEM;
1155 
1156 	inode_lock(dir);
1157 	dentry = d_alloc_name(parent, name);
1158 	if (!dentry)
1159 		goto out_err;
1160 	ret = __nfsd_mkdir(d_inode(parent), dentry, S_IFDIR | 0600, ncl);
1161 	if (ret)
1162 		goto out_err;
1163 out:
1164 	inode_unlock(dir);
1165 	return dentry;
1166 out_err:
1167 	dput(dentry);
1168 	dentry = ERR_PTR(ret);
1169 	goto out;
1170 }
1171 
1172 #if IS_ENABLED(CONFIG_SUNRPC_GSS)
1173 static int __nfsd_symlink(struct inode *dir, struct dentry *dentry,
1174 			  umode_t mode, const char *content)
1175 {
1176 	struct inode *inode;
1177 
1178 	inode = nfsd_get_inode(dir->i_sb, mode);
1179 	if (!inode)
1180 		return -ENOMEM;
1181 
1182 	inode->i_link = (char *)content;
1183 	inode->i_size = strlen(content);
1184 
1185 	d_add(dentry, inode);
1186 	inc_nlink(dir);
1187 	fsnotify_create(dir, dentry);
1188 	return 0;
1189 }
1190 
1191 /*
1192  * @content is assumed to be a NUL-terminated string that lives
1193  * longer than the symlink itself.
1194  */
1195 static void nfsd_symlink(struct dentry *parent, const char *name,
1196 			 const char *content)
1197 {
1198 	struct inode *dir = parent->d_inode;
1199 	struct dentry *dentry;
1200 	int ret;
1201 
1202 	inode_lock(dir);
1203 	dentry = d_alloc_name(parent, name);
1204 	if (!dentry)
1205 		goto out;
1206 	ret = __nfsd_symlink(d_inode(parent), dentry, S_IFLNK | 0777, content);
1207 	if (ret)
1208 		dput(dentry);
1209 out:
1210 	inode_unlock(dir);
1211 }
1212 #else
1213 static inline void nfsd_symlink(struct dentry *parent, const char *name,
1214 				const char *content)
1215 {
1216 }
1217 
1218 #endif
1219 
1220 static void clear_ncl(struct inode *inode)
1221 {
1222 	struct nfsdfs_client *ncl = inode->i_private;
1223 
1224 	inode->i_private = NULL;
1225 	kref_put(&ncl->cl_ref, ncl->cl_release);
1226 }
1227 
1228 static struct nfsdfs_client *__get_nfsdfs_client(struct inode *inode)
1229 {
1230 	struct nfsdfs_client *nc = inode->i_private;
1231 
1232 	if (nc)
1233 		kref_get(&nc->cl_ref);
1234 	return nc;
1235 }
1236 
1237 struct nfsdfs_client *get_nfsdfs_client(struct inode *inode)
1238 {
1239 	struct nfsdfs_client *nc;
1240 
1241 	inode_lock_shared(inode);
1242 	nc = __get_nfsdfs_client(inode);
1243 	inode_unlock_shared(inode);
1244 	return nc;
1245 }
1246 /* from __rpc_unlink */
1247 static void nfsdfs_remove_file(struct inode *dir, struct dentry *dentry)
1248 {
1249 	int ret;
1250 
1251 	clear_ncl(d_inode(dentry));
1252 	dget(dentry);
1253 	ret = simple_unlink(dir, dentry);
1254 	d_drop(dentry);
1255 	fsnotify_unlink(dir, dentry);
1256 	dput(dentry);
1257 	WARN_ON_ONCE(ret);
1258 }
1259 
1260 static void nfsdfs_remove_files(struct dentry *root)
1261 {
1262 	struct dentry *dentry, *tmp;
1263 
1264 	list_for_each_entry_safe(dentry, tmp, &root->d_subdirs, d_child) {
1265 		if (!simple_positive(dentry)) {
1266 			WARN_ON_ONCE(1); /* I think this can't happen? */
1267 			continue;
1268 		}
1269 		nfsdfs_remove_file(d_inode(root), dentry);
1270 	}
1271 }
1272 
1273 /* XXX: cut'n'paste from simple_fill_super; figure out if we could share
1274  * code instead. */
1275 static  int nfsdfs_create_files(struct dentry *root,
1276 				const struct tree_descr *files,
1277 				struct dentry **fdentries)
1278 {
1279 	struct inode *dir = d_inode(root);
1280 	struct inode *inode;
1281 	struct dentry *dentry;
1282 	int i;
1283 
1284 	inode_lock(dir);
1285 	for (i = 0; files->name && files->name[0]; i++, files++) {
1286 		dentry = d_alloc_name(root, files->name);
1287 		if (!dentry)
1288 			goto out;
1289 		inode = nfsd_get_inode(d_inode(root)->i_sb,
1290 					S_IFREG | files->mode);
1291 		if (!inode) {
1292 			dput(dentry);
1293 			goto out;
1294 		}
1295 		inode->i_fop = files->ops;
1296 		inode->i_private = __get_nfsdfs_client(dir);
1297 		d_add(dentry, inode);
1298 		fsnotify_create(dir, dentry);
1299 		if (fdentries)
1300 			fdentries[i] = dentry;
1301 	}
1302 	inode_unlock(dir);
1303 	return 0;
1304 out:
1305 	nfsdfs_remove_files(root);
1306 	inode_unlock(dir);
1307 	return -ENOMEM;
1308 }
1309 
1310 /* on success, returns positive number unique to that client. */
1311 struct dentry *nfsd_client_mkdir(struct nfsd_net *nn,
1312 				 struct nfsdfs_client *ncl, u32 id,
1313 				 const struct tree_descr *files,
1314 				 struct dentry **fdentries)
1315 {
1316 	struct dentry *dentry;
1317 	char name[11];
1318 	int ret;
1319 
1320 	sprintf(name, "%u", id);
1321 
1322 	dentry = nfsd_mkdir(nn->nfsd_client_dir, ncl, name);
1323 	if (IS_ERR(dentry)) /* XXX: tossing errors? */
1324 		return NULL;
1325 	ret = nfsdfs_create_files(dentry, files, fdentries);
1326 	if (ret) {
1327 		nfsd_client_rmdir(dentry);
1328 		return NULL;
1329 	}
1330 	return dentry;
1331 }
1332 
1333 /* Taken from __rpc_rmdir: */
1334 void nfsd_client_rmdir(struct dentry *dentry)
1335 {
1336 	struct inode *dir = d_inode(dentry->d_parent);
1337 	struct inode *inode = d_inode(dentry);
1338 	int ret;
1339 
1340 	inode_lock(dir);
1341 	nfsdfs_remove_files(dentry);
1342 	clear_ncl(inode);
1343 	dget(dentry);
1344 	ret = simple_rmdir(dir, dentry);
1345 	WARN_ON_ONCE(ret);
1346 	d_drop(dentry);
1347 	fsnotify_rmdir(dir, dentry);
1348 	dput(dentry);
1349 	inode_unlock(dir);
1350 }
1351 
1352 static int nfsd_fill_super(struct super_block *sb, struct fs_context *fc)
1353 {
1354 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
1355 							nfsd_net_id);
1356 	struct dentry *dentry;
1357 	int ret;
1358 
1359 	static const struct tree_descr nfsd_files[] = {
1360 		[NFSD_List] = {"exports", &exports_nfsd_operations, S_IRUGO},
1361 		/* Per-export io stats use same ops as exports file */
1362 		[NFSD_Export_Stats] = {"export_stats", &exports_nfsd_operations, S_IRUGO},
1363 		[NFSD_Export_features] = {"export_features",
1364 					&export_features_fops, S_IRUGO},
1365 		[NFSD_FO_UnlockIP] = {"unlock_ip",
1366 					&transaction_ops, S_IWUSR|S_IRUSR},
1367 		[NFSD_FO_UnlockFS] = {"unlock_filesystem",
1368 					&transaction_ops, S_IWUSR|S_IRUSR},
1369 		[NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
1370 		[NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
1371 		[NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR},
1372 		[NFSD_Pool_Stats] = {"pool_stats", &pool_stats_operations, S_IRUGO},
1373 		[NFSD_Reply_Cache_Stats] = {"reply_cache_stats",
1374 					&nfsd_reply_cache_stats_fops, S_IRUGO},
1375 		[NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR},
1376 		[NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO},
1377 		[NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO},
1378 		[NFSD_MaxConnections] = {"max_connections", &transaction_ops, S_IWUSR|S_IRUGO},
1379 		[NFSD_Filecache] = {"filecache", &nfsd_file_cache_stats_fops, S_IRUGO},
1380 #ifdef CONFIG_NFSD_V4
1381 		[NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
1382 		[NFSD_Gracetime] = {"nfsv4gracetime", &transaction_ops, S_IWUSR|S_IRUSR},
1383 		[NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
1384 		[NFSD_V4EndGrace] = {"v4_end_grace", &transaction_ops, S_IWUSR|S_IRUGO},
1385 #endif
1386 		/* last one */ {""}
1387 	};
1388 
1389 	ret = simple_fill_super(sb, 0x6e667364, nfsd_files);
1390 	if (ret)
1391 		return ret;
1392 	nfsd_symlink(sb->s_root, "supported_krb5_enctypes",
1393 		     "/proc/net/rpc/gss_krb5_enctypes");
1394 	dentry = nfsd_mkdir(sb->s_root, NULL, "clients");
1395 	if (IS_ERR(dentry))
1396 		return PTR_ERR(dentry);
1397 	nn->nfsd_client_dir = dentry;
1398 	return 0;
1399 }
1400 
1401 static int nfsd_fs_get_tree(struct fs_context *fc)
1402 {
1403 	return get_tree_keyed(fc, nfsd_fill_super, get_net(fc->net_ns));
1404 }
1405 
1406 static void nfsd_fs_free_fc(struct fs_context *fc)
1407 {
1408 	if (fc->s_fs_info)
1409 		put_net(fc->s_fs_info);
1410 }
1411 
1412 static const struct fs_context_operations nfsd_fs_context_ops = {
1413 	.free		= nfsd_fs_free_fc,
1414 	.get_tree	= nfsd_fs_get_tree,
1415 };
1416 
1417 static int nfsd_init_fs_context(struct fs_context *fc)
1418 {
1419 	put_user_ns(fc->user_ns);
1420 	fc->user_ns = get_user_ns(fc->net_ns->user_ns);
1421 	fc->ops = &nfsd_fs_context_ops;
1422 	return 0;
1423 }
1424 
1425 static void nfsd_umount(struct super_block *sb)
1426 {
1427 	struct net *net = sb->s_fs_info;
1428 
1429 	nfsd_shutdown_threads(net);
1430 
1431 	kill_litter_super(sb);
1432 	put_net(net);
1433 }
1434 
1435 static struct file_system_type nfsd_fs_type = {
1436 	.owner		= THIS_MODULE,
1437 	.name		= "nfsd",
1438 	.init_fs_context = nfsd_init_fs_context,
1439 	.kill_sb	= nfsd_umount,
1440 };
1441 MODULE_ALIAS_FS("nfsd");
1442 
1443 #ifdef CONFIG_PROC_FS
1444 
1445 static int exports_proc_open(struct inode *inode, struct file *file)
1446 {
1447 	return exports_net_open(current->nsproxy->net_ns, file);
1448 }
1449 
1450 static const struct proc_ops exports_proc_ops = {
1451 	.proc_open	= exports_proc_open,
1452 	.proc_read	= seq_read,
1453 	.proc_lseek	= seq_lseek,
1454 	.proc_release	= seq_release,
1455 };
1456 
1457 static int create_proc_exports_entry(void)
1458 {
1459 	struct proc_dir_entry *entry;
1460 
1461 	entry = proc_mkdir("fs/nfs", NULL);
1462 	if (!entry)
1463 		return -ENOMEM;
1464 	entry = proc_create("exports", 0, entry, &exports_proc_ops);
1465 	if (!entry) {
1466 		remove_proc_entry("fs/nfs", NULL);
1467 		return -ENOMEM;
1468 	}
1469 	return 0;
1470 }
1471 #else /* CONFIG_PROC_FS */
1472 static int create_proc_exports_entry(void)
1473 {
1474 	return 0;
1475 }
1476 #endif
1477 
1478 unsigned int nfsd_net_id;
1479 
1480 static __net_init int nfsd_init_net(struct net *net)
1481 {
1482 	int retval;
1483 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1484 
1485 	retval = nfsd_export_init(net);
1486 	if (retval)
1487 		goto out_export_error;
1488 	retval = nfsd_idmap_init(net);
1489 	if (retval)
1490 		goto out_idmap_error;
1491 	nn->nfsd_versions = NULL;
1492 	nn->nfsd4_minorversions = NULL;
1493 	nfsd4_init_leases_net(nn);
1494 	get_random_bytes(&nn->siphash_key, sizeof(nn->siphash_key));
1495 	seqlock_init(&nn->writeverf_lock);
1496 
1497 	return 0;
1498 
1499 out_idmap_error:
1500 	nfsd_export_shutdown(net);
1501 out_export_error:
1502 	return retval;
1503 }
1504 
1505 static __net_exit void nfsd_exit_net(struct net *net)
1506 {
1507 	nfsd_idmap_shutdown(net);
1508 	nfsd_export_shutdown(net);
1509 	nfsd_netns_free_versions(net_generic(net, nfsd_net_id));
1510 }
1511 
1512 static struct pernet_operations nfsd_net_ops = {
1513 	.init = nfsd_init_net,
1514 	.exit = nfsd_exit_net,
1515 	.id   = &nfsd_net_id,
1516 	.size = sizeof(struct nfsd_net),
1517 };
1518 
1519 static int __init init_nfsd(void)
1520 {
1521 	int retval;
1522 
1523 	retval = nfsd4_init_slabs();
1524 	if (retval)
1525 		return retval;
1526 	retval = nfsd4_init_pnfs();
1527 	if (retval)
1528 		goto out_free_slabs;
1529 	retval = nfsd_stat_init();	/* Statistics */
1530 	if (retval)
1531 		goto out_free_pnfs;
1532 	retval = nfsd_drc_slab_create();
1533 	if (retval)
1534 		goto out_free_stat;
1535 	nfsd_lockd_init();	/* lockd->nfsd callbacks */
1536 	retval = create_proc_exports_entry();
1537 	if (retval)
1538 		goto out_free_lockd;
1539 	retval = register_pernet_subsys(&nfsd_net_ops);
1540 	if (retval < 0)
1541 		goto out_free_exports;
1542 	retval = register_cld_notifier();
1543 	if (retval)
1544 		goto out_free_subsys;
1545 	retval = nfsd4_create_laundry_wq();
1546 	if (retval)
1547 		goto out_free_cld;
1548 	retval = register_filesystem(&nfsd_fs_type);
1549 	if (retval)
1550 		goto out_free_all;
1551 	return 0;
1552 out_free_all:
1553 	nfsd4_destroy_laundry_wq();
1554 out_free_cld:
1555 	unregister_cld_notifier();
1556 out_free_subsys:
1557 	unregister_pernet_subsys(&nfsd_net_ops);
1558 out_free_exports:
1559 	remove_proc_entry("fs/nfs/exports", NULL);
1560 	remove_proc_entry("fs/nfs", NULL);
1561 out_free_lockd:
1562 	nfsd_lockd_shutdown();
1563 	nfsd_drc_slab_free();
1564 out_free_stat:
1565 	nfsd_stat_shutdown();
1566 out_free_pnfs:
1567 	nfsd4_exit_pnfs();
1568 out_free_slabs:
1569 	nfsd4_free_slabs();
1570 	return retval;
1571 }
1572 
1573 static void __exit exit_nfsd(void)
1574 {
1575 	unregister_filesystem(&nfsd_fs_type);
1576 	nfsd4_destroy_laundry_wq();
1577 	unregister_cld_notifier();
1578 	unregister_pernet_subsys(&nfsd_net_ops);
1579 	nfsd_drc_slab_free();
1580 	remove_proc_entry("fs/nfs/exports", NULL);
1581 	remove_proc_entry("fs/nfs", NULL);
1582 	nfsd_stat_shutdown();
1583 	nfsd_lockd_shutdown();
1584 	nfsd4_free_slabs();
1585 	nfsd4_exit_pnfs();
1586 }
1587 
1588 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
1589 MODULE_LICENSE("GPL");
1590 module_init(init_nfsd)
1591 module_exit(exit_nfsd)
1592