xref: /openbmc/linux/fs/nfsd/nfsctl.c (revision 726ccdba)
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 	if (svc_alien_sock(net, fd)) {
694 		printk(KERN_ERR "%s: socket net is different to NFSd's one\n", __func__);
695 		return -EINVAL;
696 	}
697 
698 	err = nfsd_create_serv(net);
699 	if (err != 0)
700 		return err;
701 
702 	err = svc_addsock(nn->nfsd_serv, fd, buf, SIMPLE_TRANSACTION_LIMIT, cred);
703 
704 	if (err >= 0 &&
705 	    !nn->nfsd_serv->sv_nrthreads && !xchg(&nn->keep_active, 1))
706 		svc_get(nn->nfsd_serv);
707 
708 	nfsd_put(net);
709 	return err;
710 }
711 
712 /*
713  * A transport listener is added by writing it's transport name and
714  * a port number.
715  */
716 static ssize_t __write_ports_addxprt(char *buf, struct net *net, const struct cred *cred)
717 {
718 	char transport[16];
719 	struct svc_xprt *xprt;
720 	int port, err;
721 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
722 
723 	if (sscanf(buf, "%15s %5u", transport, &port) != 2)
724 		return -EINVAL;
725 
726 	if (port < 1 || port > USHRT_MAX)
727 		return -EINVAL;
728 
729 	err = nfsd_create_serv(net);
730 	if (err != 0)
731 		return err;
732 
733 	err = svc_xprt_create(nn->nfsd_serv, transport, net,
734 			      PF_INET, port, SVC_SOCK_ANONYMOUS, cred);
735 	if (err < 0)
736 		goto out_err;
737 
738 	err = svc_xprt_create(nn->nfsd_serv, transport, net,
739 			      PF_INET6, port, SVC_SOCK_ANONYMOUS, cred);
740 	if (err < 0 && err != -EAFNOSUPPORT)
741 		goto out_close;
742 
743 	if (!nn->nfsd_serv->sv_nrthreads && !xchg(&nn->keep_active, 1))
744 		svc_get(nn->nfsd_serv);
745 
746 	nfsd_put(net);
747 	return 0;
748 out_close:
749 	xprt = svc_find_xprt(nn->nfsd_serv, transport, net, PF_INET, port);
750 	if (xprt != NULL) {
751 		svc_xprt_close(xprt);
752 		svc_xprt_put(xprt);
753 	}
754 out_err:
755 	nfsd_put(net);
756 	return err;
757 }
758 
759 static ssize_t __write_ports(struct file *file, char *buf, size_t size,
760 			     struct net *net)
761 {
762 	if (size == 0)
763 		return __write_ports_names(buf, net);
764 
765 	if (isdigit(buf[0]))
766 		return __write_ports_addfd(buf, net, file->f_cred);
767 
768 	if (isalpha(buf[0]))
769 		return __write_ports_addxprt(buf, net, file->f_cred);
770 
771 	return -EINVAL;
772 }
773 
774 /*
775  * write_ports - Pass a socket file descriptor or transport name to listen on
776  *
777  * Input:
778  *			buf:		ignored
779  *			size:		zero
780  * Output:
781  *	On success:	passed-in buffer filled with a '\n'-terminated C
782  *			string containing a whitespace-separated list of
783  *			named NFSD listeners;
784  *			return code is the size in bytes of the string
785  *	On error:	return code is zero or a negative errno value
786  *
787  * OR
788  *
789  * Input:
790  *			buf:		C string containing an unsigned
791  *					integer value representing a bound
792  *					but unconnected socket that is to be
793  *					used as an NFSD listener; listen(3)
794  *					must be called for a SOCK_STREAM
795  *					socket, otherwise it is ignored
796  *			size:		non-zero length of C string in @buf
797  * Output:
798  *	On success:	NFS service is started;
799  *			passed-in buffer filled with a '\n'-terminated C
800  *			string containing a unique alphanumeric name of
801  *			the listener;
802  *			return code is the size in bytes of the string
803  *	On error:	return code is a negative errno value
804  *
805  * OR
806  *
807  * Input:
808  *			buf:		C string containing a transport
809  *					name and an unsigned integer value
810  *					representing the port to listen on,
811  *					separated by whitespace
812  *			size:		non-zero length of C string in @buf
813  * Output:
814  *	On success:	returns zero; NFS service is started
815  *	On error:	return code is a negative errno value
816  */
817 static ssize_t write_ports(struct file *file, char *buf, size_t size)
818 {
819 	ssize_t rv;
820 
821 	mutex_lock(&nfsd_mutex);
822 	rv = __write_ports(file, buf, size, netns(file));
823 	mutex_unlock(&nfsd_mutex);
824 	return rv;
825 }
826 
827 
828 int nfsd_max_blksize;
829 
830 /*
831  * write_maxblksize - Set or report the current NFS blksize
832  *
833  * Input:
834  *			buf:		ignored
835  *			size:		zero
836  *
837  * OR
838  *
839  * Input:
840  * 			buf:		C string containing an unsigned
841  * 					integer value representing the new
842  * 					NFS blksize
843  *			size:		non-zero length of C string in @buf
844  * Output:
845  *	On success:	passed-in buffer filled with '\n'-terminated C string
846  *			containing numeric value of the current NFS blksize
847  *			setting;
848  *			return code is the size in bytes of the string
849  *	On error:	return code is zero or a negative errno value
850  */
851 static ssize_t write_maxblksize(struct file *file, char *buf, size_t size)
852 {
853 	char *mesg = buf;
854 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
855 
856 	if (size > 0) {
857 		int bsize;
858 		int rv = get_int(&mesg, &bsize);
859 		if (rv)
860 			return rv;
861 		/* force bsize into allowed range and
862 		 * required alignment.
863 		 */
864 		bsize = max_t(int, bsize, 1024);
865 		bsize = min_t(int, bsize, NFSSVC_MAXBLKSIZE);
866 		bsize &= ~(1024-1);
867 		mutex_lock(&nfsd_mutex);
868 		if (nn->nfsd_serv) {
869 			mutex_unlock(&nfsd_mutex);
870 			return -EBUSY;
871 		}
872 		nfsd_max_blksize = bsize;
873 		mutex_unlock(&nfsd_mutex);
874 	}
875 
876 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n",
877 							nfsd_max_blksize);
878 }
879 
880 /*
881  * write_maxconn - Set or report the current max number of connections
882  *
883  * Input:
884  *			buf:		ignored
885  *			size:		zero
886  * OR
887  *
888  * Input:
889  * 			buf:		C string containing an unsigned
890  * 					integer value representing the new
891  * 					number of max connections
892  *			size:		non-zero length of C string in @buf
893  * Output:
894  *	On success:	passed-in buffer filled with '\n'-terminated C string
895  *			containing numeric value of max_connections setting
896  *			for this net namespace;
897  *			return code is the size in bytes of the string
898  *	On error:	return code is zero or a negative errno value
899  */
900 static ssize_t write_maxconn(struct file *file, char *buf, size_t size)
901 {
902 	char *mesg = buf;
903 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
904 	unsigned int maxconn = nn->max_connections;
905 
906 	if (size > 0) {
907 		int rv = get_uint(&mesg, &maxconn);
908 
909 		if (rv)
910 			return rv;
911 		nn->max_connections = maxconn;
912 	}
913 
914 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%u\n", maxconn);
915 }
916 
917 #ifdef CONFIG_NFSD_V4
918 static ssize_t __nfsd4_write_time(struct file *file, char *buf, size_t size,
919 				  time64_t *time, struct nfsd_net *nn)
920 {
921 	char *mesg = buf;
922 	int rv, i;
923 
924 	if (size > 0) {
925 		if (nn->nfsd_serv)
926 			return -EBUSY;
927 		rv = get_int(&mesg, &i);
928 		if (rv)
929 			return rv;
930 		/*
931 		 * Some sanity checking.  We don't have a reason for
932 		 * these particular numbers, but problems with the
933 		 * extremes are:
934 		 *	- Too short: the briefest network outage may
935 		 *	  cause clients to lose all their locks.  Also,
936 		 *	  the frequent polling may be wasteful.
937 		 *	- Too long: do you really want reboot recovery
938 		 *	  to take more than an hour?  Or to make other
939 		 *	  clients wait an hour before being able to
940 		 *	  revoke a dead client's locks?
941 		 */
942 		if (i < 10 || i > 3600)
943 			return -EINVAL;
944 		*time = i;
945 	}
946 
947 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%lld\n", *time);
948 }
949 
950 static ssize_t nfsd4_write_time(struct file *file, char *buf, size_t size,
951 				time64_t *time, struct nfsd_net *nn)
952 {
953 	ssize_t rv;
954 
955 	mutex_lock(&nfsd_mutex);
956 	rv = __nfsd4_write_time(file, buf, size, time, nn);
957 	mutex_unlock(&nfsd_mutex);
958 	return rv;
959 }
960 
961 /*
962  * write_leasetime - Set or report the current NFSv4 lease time
963  *
964  * Input:
965  *			buf:		ignored
966  *			size:		zero
967  *
968  * OR
969  *
970  * Input:
971  *			buf:		C string containing an unsigned
972  *					integer value representing the new
973  *					NFSv4 lease expiry time
974  *			size:		non-zero length of C string in @buf
975  * Output:
976  *	On success:	passed-in buffer filled with '\n'-terminated C
977  *			string containing unsigned integer value of the
978  *			current lease expiry time;
979  *			return code is the size in bytes of the string
980  *	On error:	return code is zero or a negative errno value
981  */
982 static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
983 {
984 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
985 	return nfsd4_write_time(file, buf, size, &nn->nfsd4_lease, nn);
986 }
987 
988 /*
989  * write_gracetime - Set or report current NFSv4 grace period time
990  *
991  * As above, but sets the time of the NFSv4 grace period.
992  *
993  * Note this should never be set to less than the *previous*
994  * lease-period time, but we don't try to enforce this.  (In the common
995  * case (a new boot), we don't know what the previous lease time was
996  * anyway.)
997  */
998 static ssize_t write_gracetime(struct file *file, char *buf, size_t size)
999 {
1000 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
1001 	return nfsd4_write_time(file, buf, size, &nn->nfsd4_grace, nn);
1002 }
1003 
1004 static ssize_t __write_recoverydir(struct file *file, char *buf, size_t size,
1005 				   struct nfsd_net *nn)
1006 {
1007 	char *mesg = buf;
1008 	char *recdir;
1009 	int len, status;
1010 
1011 	if (size > 0) {
1012 		if (nn->nfsd_serv)
1013 			return -EBUSY;
1014 		if (size > PATH_MAX || buf[size-1] != '\n')
1015 			return -EINVAL;
1016 		buf[size-1] = 0;
1017 
1018 		recdir = mesg;
1019 		len = qword_get(&mesg, recdir, size);
1020 		if (len <= 0)
1021 			return -EINVAL;
1022 
1023 		status = nfs4_reset_recoverydir(recdir);
1024 		if (status)
1025 			return status;
1026 	}
1027 
1028 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%s\n",
1029 							nfs4_recoverydir());
1030 }
1031 
1032 /*
1033  * write_recoverydir - Set or report the pathname of the recovery directory
1034  *
1035  * Input:
1036  *			buf:		ignored
1037  *			size:		zero
1038  *
1039  * OR
1040  *
1041  * Input:
1042  *			buf:		C string containing the pathname
1043  *					of the directory on a local file
1044  *					system containing permanent NFSv4
1045  *					recovery data
1046  *			size:		non-zero length of C string in @buf
1047  * Output:
1048  *	On success:	passed-in buffer filled with '\n'-terminated C string
1049  *			containing the current recovery pathname setting;
1050  *			return code is the size in bytes of the string
1051  *	On error:	return code is zero or a negative errno value
1052  */
1053 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
1054 {
1055 	ssize_t rv;
1056 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
1057 
1058 	mutex_lock(&nfsd_mutex);
1059 	rv = __write_recoverydir(file, buf, size, nn);
1060 	mutex_unlock(&nfsd_mutex);
1061 	return rv;
1062 }
1063 
1064 /*
1065  * write_v4_end_grace - release grace period for nfsd's v4.x lock manager
1066  *
1067  * Input:
1068  *			buf:		ignored
1069  *			size:		zero
1070  * OR
1071  *
1072  * Input:
1073  * 			buf:		any value
1074  *			size:		non-zero length of C string in @buf
1075  * Output:
1076  *			passed-in buffer filled with "Y" or "N" with a newline
1077  *			and NULL-terminated C string. This indicates whether
1078  *			the grace period has ended in the current net
1079  *			namespace. Return code is the size in bytes of the
1080  *			string. Writing a string that starts with 'Y', 'y', or
1081  *			'1' to the file will end the grace period for nfsd's v4
1082  *			lock manager.
1083  */
1084 static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size)
1085 {
1086 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
1087 
1088 	if (size > 0) {
1089 		switch(buf[0]) {
1090 		case 'Y':
1091 		case 'y':
1092 		case '1':
1093 			if (!nn->nfsd_serv)
1094 				return -EBUSY;
1095 			nfsd4_end_grace(nn);
1096 			break;
1097 		default:
1098 			return -EINVAL;
1099 		}
1100 	}
1101 
1102 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%c\n",
1103 			 nn->grace_ended ? 'Y' : 'N');
1104 }
1105 
1106 #endif
1107 
1108 /*----------------------------------------------------------------------------*/
1109 /*
1110  *	populating the filesystem.
1111  */
1112 
1113 /* Basically copying rpc_get_inode. */
1114 static struct inode *nfsd_get_inode(struct super_block *sb, umode_t mode)
1115 {
1116 	struct inode *inode = new_inode(sb);
1117 	if (!inode)
1118 		return NULL;
1119 	/* Following advice from simple_fill_super documentation: */
1120 	inode->i_ino = iunique(sb, NFSD_MaxReserved);
1121 	inode->i_mode = mode;
1122 	inode->i_atime = inode->i_mtime = inode->i_ctime = current_time(inode);
1123 	switch (mode & S_IFMT) {
1124 	case S_IFDIR:
1125 		inode->i_fop = &simple_dir_operations;
1126 		inode->i_op = &simple_dir_inode_operations;
1127 		inc_nlink(inode);
1128 		break;
1129 	case S_IFLNK:
1130 		inode->i_op = &simple_symlink_inode_operations;
1131 		break;
1132 	default:
1133 		break;
1134 	}
1135 	return inode;
1136 }
1137 
1138 static int __nfsd_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode, struct nfsdfs_client *ncl)
1139 {
1140 	struct inode *inode;
1141 
1142 	inode = nfsd_get_inode(dir->i_sb, mode);
1143 	if (!inode)
1144 		return -ENOMEM;
1145 	if (ncl) {
1146 		inode->i_private = ncl;
1147 		kref_get(&ncl->cl_ref);
1148 	}
1149 	d_add(dentry, inode);
1150 	inc_nlink(dir);
1151 	fsnotify_mkdir(dir, dentry);
1152 	return 0;
1153 }
1154 
1155 static struct dentry *nfsd_mkdir(struct dentry *parent, struct nfsdfs_client *ncl, char *name)
1156 {
1157 	struct inode *dir = parent->d_inode;
1158 	struct dentry *dentry;
1159 	int ret = -ENOMEM;
1160 
1161 	inode_lock(dir);
1162 	dentry = d_alloc_name(parent, name);
1163 	if (!dentry)
1164 		goto out_err;
1165 	ret = __nfsd_mkdir(d_inode(parent), dentry, S_IFDIR | 0600, ncl);
1166 	if (ret)
1167 		goto out_err;
1168 out:
1169 	inode_unlock(dir);
1170 	return dentry;
1171 out_err:
1172 	dput(dentry);
1173 	dentry = ERR_PTR(ret);
1174 	goto out;
1175 }
1176 
1177 #if IS_ENABLED(CONFIG_SUNRPC_GSS)
1178 static int __nfsd_symlink(struct inode *dir, struct dentry *dentry,
1179 			  umode_t mode, const char *content)
1180 {
1181 	struct inode *inode;
1182 
1183 	inode = nfsd_get_inode(dir->i_sb, mode);
1184 	if (!inode)
1185 		return -ENOMEM;
1186 
1187 	inode->i_link = (char *)content;
1188 	inode->i_size = strlen(content);
1189 
1190 	d_add(dentry, inode);
1191 	inc_nlink(dir);
1192 	fsnotify_create(dir, dentry);
1193 	return 0;
1194 }
1195 
1196 /*
1197  * @content is assumed to be a NUL-terminated string that lives
1198  * longer than the symlink itself.
1199  */
1200 static void nfsd_symlink(struct dentry *parent, const char *name,
1201 			 const char *content)
1202 {
1203 	struct inode *dir = parent->d_inode;
1204 	struct dentry *dentry;
1205 	int ret;
1206 
1207 	inode_lock(dir);
1208 	dentry = d_alloc_name(parent, name);
1209 	if (!dentry)
1210 		goto out;
1211 	ret = __nfsd_symlink(d_inode(parent), dentry, S_IFLNK | 0777, content);
1212 	if (ret)
1213 		dput(dentry);
1214 out:
1215 	inode_unlock(dir);
1216 }
1217 #else
1218 static inline void nfsd_symlink(struct dentry *parent, const char *name,
1219 				const char *content)
1220 {
1221 }
1222 
1223 #endif
1224 
1225 static void clear_ncl(struct inode *inode)
1226 {
1227 	struct nfsdfs_client *ncl = inode->i_private;
1228 
1229 	inode->i_private = NULL;
1230 	kref_put(&ncl->cl_ref, ncl->cl_release);
1231 }
1232 
1233 static struct nfsdfs_client *__get_nfsdfs_client(struct inode *inode)
1234 {
1235 	struct nfsdfs_client *nc = inode->i_private;
1236 
1237 	if (nc)
1238 		kref_get(&nc->cl_ref);
1239 	return nc;
1240 }
1241 
1242 struct nfsdfs_client *get_nfsdfs_client(struct inode *inode)
1243 {
1244 	struct nfsdfs_client *nc;
1245 
1246 	inode_lock_shared(inode);
1247 	nc = __get_nfsdfs_client(inode);
1248 	inode_unlock_shared(inode);
1249 	return nc;
1250 }
1251 /* from __rpc_unlink */
1252 static void nfsdfs_remove_file(struct inode *dir, struct dentry *dentry)
1253 {
1254 	int ret;
1255 
1256 	clear_ncl(d_inode(dentry));
1257 	dget(dentry);
1258 	ret = simple_unlink(dir, dentry);
1259 	d_drop(dentry);
1260 	fsnotify_unlink(dir, dentry);
1261 	dput(dentry);
1262 	WARN_ON_ONCE(ret);
1263 }
1264 
1265 static void nfsdfs_remove_files(struct dentry *root)
1266 {
1267 	struct dentry *dentry, *tmp;
1268 
1269 	list_for_each_entry_safe(dentry, tmp, &root->d_subdirs, d_child) {
1270 		if (!simple_positive(dentry)) {
1271 			WARN_ON_ONCE(1); /* I think this can't happen? */
1272 			continue;
1273 		}
1274 		nfsdfs_remove_file(d_inode(root), dentry);
1275 	}
1276 }
1277 
1278 /* XXX: cut'n'paste from simple_fill_super; figure out if we could share
1279  * code instead. */
1280 static  int nfsdfs_create_files(struct dentry *root,
1281 				const struct tree_descr *files,
1282 				struct dentry **fdentries)
1283 {
1284 	struct inode *dir = d_inode(root);
1285 	struct inode *inode;
1286 	struct dentry *dentry;
1287 	int i;
1288 
1289 	inode_lock(dir);
1290 	for (i = 0; files->name && files->name[0]; i++, files++) {
1291 		dentry = d_alloc_name(root, files->name);
1292 		if (!dentry)
1293 			goto out;
1294 		inode = nfsd_get_inode(d_inode(root)->i_sb,
1295 					S_IFREG | files->mode);
1296 		if (!inode) {
1297 			dput(dentry);
1298 			goto out;
1299 		}
1300 		inode->i_fop = files->ops;
1301 		inode->i_private = __get_nfsdfs_client(dir);
1302 		d_add(dentry, inode);
1303 		fsnotify_create(dir, dentry);
1304 		if (fdentries)
1305 			fdentries[i] = dentry;
1306 	}
1307 	inode_unlock(dir);
1308 	return 0;
1309 out:
1310 	nfsdfs_remove_files(root);
1311 	inode_unlock(dir);
1312 	return -ENOMEM;
1313 }
1314 
1315 /* on success, returns positive number unique to that client. */
1316 struct dentry *nfsd_client_mkdir(struct nfsd_net *nn,
1317 				 struct nfsdfs_client *ncl, u32 id,
1318 				 const struct tree_descr *files,
1319 				 struct dentry **fdentries)
1320 {
1321 	struct dentry *dentry;
1322 	char name[11];
1323 	int ret;
1324 
1325 	sprintf(name, "%u", id);
1326 
1327 	dentry = nfsd_mkdir(nn->nfsd_client_dir, ncl, name);
1328 	if (IS_ERR(dentry)) /* XXX: tossing errors? */
1329 		return NULL;
1330 	ret = nfsdfs_create_files(dentry, files, fdentries);
1331 	if (ret) {
1332 		nfsd_client_rmdir(dentry);
1333 		return NULL;
1334 	}
1335 	return dentry;
1336 }
1337 
1338 /* Taken from __rpc_rmdir: */
1339 void nfsd_client_rmdir(struct dentry *dentry)
1340 {
1341 	struct inode *dir = d_inode(dentry->d_parent);
1342 	struct inode *inode = d_inode(dentry);
1343 	int ret;
1344 
1345 	inode_lock(dir);
1346 	nfsdfs_remove_files(dentry);
1347 	clear_ncl(inode);
1348 	dget(dentry);
1349 	ret = simple_rmdir(dir, dentry);
1350 	WARN_ON_ONCE(ret);
1351 	d_drop(dentry);
1352 	fsnotify_rmdir(dir, dentry);
1353 	dput(dentry);
1354 	inode_unlock(dir);
1355 }
1356 
1357 static int nfsd_fill_super(struct super_block *sb, struct fs_context *fc)
1358 {
1359 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
1360 							nfsd_net_id);
1361 	struct dentry *dentry;
1362 	int ret;
1363 
1364 	static const struct tree_descr nfsd_files[] = {
1365 		[NFSD_List] = {"exports", &exports_nfsd_operations, S_IRUGO},
1366 		/* Per-export io stats use same ops as exports file */
1367 		[NFSD_Export_Stats] = {"export_stats", &exports_nfsd_operations, S_IRUGO},
1368 		[NFSD_Export_features] = {"export_features",
1369 					&export_features_fops, S_IRUGO},
1370 		[NFSD_FO_UnlockIP] = {"unlock_ip",
1371 					&transaction_ops, S_IWUSR|S_IRUSR},
1372 		[NFSD_FO_UnlockFS] = {"unlock_filesystem",
1373 					&transaction_ops, S_IWUSR|S_IRUSR},
1374 		[NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
1375 		[NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
1376 		[NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR},
1377 		[NFSD_Pool_Stats] = {"pool_stats", &pool_stats_operations, S_IRUGO},
1378 		[NFSD_Reply_Cache_Stats] = {"reply_cache_stats",
1379 					&nfsd_reply_cache_stats_fops, S_IRUGO},
1380 		[NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR},
1381 		[NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO},
1382 		[NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO},
1383 		[NFSD_MaxConnections] = {"max_connections", &transaction_ops, S_IWUSR|S_IRUGO},
1384 		[NFSD_Filecache] = {"filecache", &nfsd_file_cache_stats_fops, S_IRUGO},
1385 #ifdef CONFIG_NFSD_V4
1386 		[NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
1387 		[NFSD_Gracetime] = {"nfsv4gracetime", &transaction_ops, S_IWUSR|S_IRUSR},
1388 		[NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
1389 		[NFSD_V4EndGrace] = {"v4_end_grace", &transaction_ops, S_IWUSR|S_IRUGO},
1390 #endif
1391 		/* last one */ {""}
1392 	};
1393 
1394 	ret = simple_fill_super(sb, 0x6e667364, nfsd_files);
1395 	if (ret)
1396 		return ret;
1397 	nfsd_symlink(sb->s_root, "supported_krb5_enctypes",
1398 		     "/proc/net/rpc/gss_krb5_enctypes");
1399 	dentry = nfsd_mkdir(sb->s_root, NULL, "clients");
1400 	if (IS_ERR(dentry))
1401 		return PTR_ERR(dentry);
1402 	nn->nfsd_client_dir = dentry;
1403 	return 0;
1404 }
1405 
1406 static int nfsd_fs_get_tree(struct fs_context *fc)
1407 {
1408 	return get_tree_keyed(fc, nfsd_fill_super, get_net(fc->net_ns));
1409 }
1410 
1411 static void nfsd_fs_free_fc(struct fs_context *fc)
1412 {
1413 	if (fc->s_fs_info)
1414 		put_net(fc->s_fs_info);
1415 }
1416 
1417 static const struct fs_context_operations nfsd_fs_context_ops = {
1418 	.free		= nfsd_fs_free_fc,
1419 	.get_tree	= nfsd_fs_get_tree,
1420 };
1421 
1422 static int nfsd_init_fs_context(struct fs_context *fc)
1423 {
1424 	put_user_ns(fc->user_ns);
1425 	fc->user_ns = get_user_ns(fc->net_ns->user_ns);
1426 	fc->ops = &nfsd_fs_context_ops;
1427 	return 0;
1428 }
1429 
1430 static void nfsd_umount(struct super_block *sb)
1431 {
1432 	struct net *net = sb->s_fs_info;
1433 
1434 	nfsd_shutdown_threads(net);
1435 
1436 	kill_litter_super(sb);
1437 	put_net(net);
1438 }
1439 
1440 static struct file_system_type nfsd_fs_type = {
1441 	.owner		= THIS_MODULE,
1442 	.name		= "nfsd",
1443 	.init_fs_context = nfsd_init_fs_context,
1444 	.kill_sb	= nfsd_umount,
1445 };
1446 MODULE_ALIAS_FS("nfsd");
1447 
1448 #ifdef CONFIG_PROC_FS
1449 
1450 static int exports_proc_open(struct inode *inode, struct file *file)
1451 {
1452 	return exports_net_open(current->nsproxy->net_ns, file);
1453 }
1454 
1455 static const struct proc_ops exports_proc_ops = {
1456 	.proc_open	= exports_proc_open,
1457 	.proc_read	= seq_read,
1458 	.proc_lseek	= seq_lseek,
1459 	.proc_release	= seq_release,
1460 };
1461 
1462 static int create_proc_exports_entry(void)
1463 {
1464 	struct proc_dir_entry *entry;
1465 
1466 	entry = proc_mkdir("fs/nfs", NULL);
1467 	if (!entry)
1468 		return -ENOMEM;
1469 	entry = proc_create("exports", 0, entry, &exports_proc_ops);
1470 	if (!entry) {
1471 		remove_proc_entry("fs/nfs", NULL);
1472 		return -ENOMEM;
1473 	}
1474 	return 0;
1475 }
1476 #else /* CONFIG_PROC_FS */
1477 static int create_proc_exports_entry(void)
1478 {
1479 	return 0;
1480 }
1481 #endif
1482 
1483 unsigned int nfsd_net_id;
1484 
1485 static __net_init int nfsd_init_net(struct net *net)
1486 {
1487 	int retval;
1488 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1489 
1490 	retval = nfsd_export_init(net);
1491 	if (retval)
1492 		goto out_export_error;
1493 	retval = nfsd_idmap_init(net);
1494 	if (retval)
1495 		goto out_idmap_error;
1496 	nn->nfsd_versions = NULL;
1497 	nn->nfsd4_minorversions = NULL;
1498 	nfsd4_init_leases_net(nn);
1499 	get_random_bytes(&nn->siphash_key, sizeof(nn->siphash_key));
1500 	seqlock_init(&nn->writeverf_lock);
1501 
1502 	return 0;
1503 
1504 out_idmap_error:
1505 	nfsd_export_shutdown(net);
1506 out_export_error:
1507 	return retval;
1508 }
1509 
1510 static __net_exit void nfsd_exit_net(struct net *net)
1511 {
1512 	nfsd_idmap_shutdown(net);
1513 	nfsd_export_shutdown(net);
1514 	nfsd_netns_free_versions(net_generic(net, nfsd_net_id));
1515 }
1516 
1517 static struct pernet_operations nfsd_net_ops = {
1518 	.init = nfsd_init_net,
1519 	.exit = nfsd_exit_net,
1520 	.id   = &nfsd_net_id,
1521 	.size = sizeof(struct nfsd_net),
1522 };
1523 
1524 static int __init init_nfsd(void)
1525 {
1526 	int retval;
1527 
1528 	retval = nfsd4_init_slabs();
1529 	if (retval)
1530 		return retval;
1531 	retval = nfsd4_init_pnfs();
1532 	if (retval)
1533 		goto out_free_slabs;
1534 	retval = nfsd_stat_init();	/* Statistics */
1535 	if (retval)
1536 		goto out_free_pnfs;
1537 	retval = nfsd_drc_slab_create();
1538 	if (retval)
1539 		goto out_free_stat;
1540 	nfsd_lockd_init();	/* lockd->nfsd callbacks */
1541 	retval = create_proc_exports_entry();
1542 	if (retval)
1543 		goto out_free_lockd;
1544 	retval = register_pernet_subsys(&nfsd_net_ops);
1545 	if (retval < 0)
1546 		goto out_free_exports;
1547 	retval = register_cld_notifier();
1548 	if (retval)
1549 		goto out_free_subsys;
1550 	retval = nfsd4_create_laundry_wq();
1551 	if (retval)
1552 		goto out_free_cld;
1553 	retval = register_filesystem(&nfsd_fs_type);
1554 	if (retval)
1555 		goto out_free_all;
1556 	return 0;
1557 out_free_all:
1558 	nfsd4_destroy_laundry_wq();
1559 out_free_cld:
1560 	unregister_cld_notifier();
1561 out_free_subsys:
1562 	unregister_pernet_subsys(&nfsd_net_ops);
1563 out_free_exports:
1564 	remove_proc_entry("fs/nfs/exports", NULL);
1565 	remove_proc_entry("fs/nfs", NULL);
1566 out_free_lockd:
1567 	nfsd_lockd_shutdown();
1568 	nfsd_drc_slab_free();
1569 out_free_stat:
1570 	nfsd_stat_shutdown();
1571 out_free_pnfs:
1572 	nfsd4_exit_pnfs();
1573 out_free_slabs:
1574 	nfsd4_free_slabs();
1575 	return retval;
1576 }
1577 
1578 static void __exit exit_nfsd(void)
1579 {
1580 	unregister_filesystem(&nfsd_fs_type);
1581 	nfsd4_destroy_laundry_wq();
1582 	unregister_cld_notifier();
1583 	unregister_pernet_subsys(&nfsd_net_ops);
1584 	nfsd_drc_slab_free();
1585 	remove_proc_entry("fs/nfs/exports", NULL);
1586 	remove_proc_entry("fs/nfs", NULL);
1587 	nfsd_stat_shutdown();
1588 	nfsd_lockd_shutdown();
1589 	nfsd4_free_slabs();
1590 	nfsd4_exit_pnfs();
1591 }
1592 
1593 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
1594 MODULE_LICENSE("GPL");
1595 module_init(init_nfsd)
1596 module_exit(exit_nfsd)
1597