xref: /linux/fs/nfsd/nfsctl.c (revision 906fd46a65383cd639e5eec72a047efc33045d86)
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/sunrpc/svc.h>
19 #include <linux/module.h>
20 #include <linux/fsnotify.h>
21 
22 #include "idmap.h"
23 #include "nfsd.h"
24 #include "cache.h"
25 #include "state.h"
26 #include "netns.h"
27 #include "pnfs.h"
28 #include "filecache.h"
29 #include "trace.h"
30 #include "netlink.h"
31 
32 /*
33  *	We have a single directory with several nodes in it.
34  */
35 enum {
36 	NFSD_Root = 1,
37 	NFSD_List,
38 	NFSD_Export_Stats,
39 	NFSD_Export_features,
40 	NFSD_Fh,
41 	NFSD_FO_UnlockIP,
42 	NFSD_FO_UnlockFS,
43 	NFSD_Threads,
44 	NFSD_Pool_Threads,
45 	NFSD_Pool_Stats,
46 	NFSD_Reply_Cache_Stats,
47 	NFSD_Versions,
48 	NFSD_Ports,
49 	NFSD_MaxBlkSize,
50 	NFSD_MaxConnections,
51 	NFSD_Filecache,
52 	NFSD_Leasetime,
53 	NFSD_Gracetime,
54 	NFSD_RecoveryDir,
55 	NFSD_V4EndGrace,
56 	NFSD_MaxReserved
57 };
58 
59 /*
60  * write() for these nodes.
61  */
62 static ssize_t write_filehandle(struct file *file, char *buf, size_t size);
63 static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size);
64 static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size);
65 static ssize_t write_threads(struct file *file, char *buf, size_t size);
66 static ssize_t write_pool_threads(struct file *file, char *buf, size_t size);
67 static ssize_t write_versions(struct file *file, char *buf, size_t size);
68 static ssize_t write_ports(struct file *file, char *buf, size_t size);
69 static ssize_t write_maxblksize(struct file *file, char *buf, size_t size);
70 static ssize_t write_maxconn(struct file *file, char *buf, size_t size);
71 #ifdef CONFIG_NFSD_V4
72 static ssize_t write_leasetime(struct file *file, char *buf, size_t size);
73 static ssize_t write_gracetime(struct file *file, char *buf, size_t size);
74 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
75 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size);
76 #endif
77 static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size);
78 #endif
79 
80 static ssize_t (*const write_op[])(struct file *, char *, size_t) = {
81 	[NFSD_Fh] = write_filehandle,
82 	[NFSD_FO_UnlockIP] = write_unlock_ip,
83 	[NFSD_FO_UnlockFS] = write_unlock_fs,
84 	[NFSD_Threads] = write_threads,
85 	[NFSD_Pool_Threads] = write_pool_threads,
86 	[NFSD_Versions] = write_versions,
87 	[NFSD_Ports] = write_ports,
88 	[NFSD_MaxBlkSize] = write_maxblksize,
89 	[NFSD_MaxConnections] = write_maxconn,
90 #ifdef CONFIG_NFSD_V4
91 	[NFSD_Leasetime] = write_leasetime,
92 	[NFSD_Gracetime] = write_gracetime,
93 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
94 	[NFSD_RecoveryDir] = write_recoverydir,
95 #endif
96 	[NFSD_V4EndGrace] = write_v4_end_grace,
97 #endif
98 };
99 
100 static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos)
101 {
102 	ino_t ino =  file_inode(file)->i_ino;
103 	char *data;
104 	ssize_t rv;
105 
106 	if (ino >= ARRAY_SIZE(write_op) || !write_op[ino])
107 		return -EINVAL;
108 
109 	data = simple_transaction_get(file, buf, size);
110 	if (IS_ERR(data))
111 		return PTR_ERR(data);
112 
113 	rv = write_op[ino](file, data, size);
114 	if (rv < 0)
115 		return rv;
116 
117 	simple_transaction_set(file, rv);
118 	return size;
119 }
120 
121 static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos)
122 {
123 	if (! file->private_data) {
124 		/* An attempt to read a transaction file without writing
125 		 * causes a 0-byte write so that the file can return
126 		 * state information
127 		 */
128 		ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos);
129 		if (rv < 0)
130 			return rv;
131 	}
132 	return simple_transaction_read(file, buf, size, pos);
133 }
134 
135 static const struct file_operations transaction_ops = {
136 	.write		= nfsctl_transaction_write,
137 	.read		= nfsctl_transaction_read,
138 	.release	= simple_transaction_release,
139 	.llseek		= default_llseek,
140 };
141 
142 static int exports_net_open(struct net *net, struct file *file)
143 {
144 	int err;
145 	struct seq_file *seq;
146 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
147 
148 	err = seq_open(file, &nfs_exports_op);
149 	if (err)
150 		return err;
151 
152 	seq = file->private_data;
153 	seq->private = nn->svc_export_cache;
154 	return 0;
155 }
156 
157 static int exports_nfsd_open(struct inode *inode, struct file *file)
158 {
159 	return exports_net_open(inode->i_sb->s_fs_info, file);
160 }
161 
162 static const struct file_operations exports_nfsd_operations = {
163 	.open		= exports_nfsd_open,
164 	.read		= seq_read,
165 	.llseek		= seq_lseek,
166 	.release	= seq_release,
167 };
168 
169 static int export_features_show(struct seq_file *m, void *v)
170 {
171 	seq_printf(m, "0x%x 0x%x\n", NFSEXP_ALLFLAGS, NFSEXP_SECINFO_FLAGS);
172 	return 0;
173 }
174 
175 DEFINE_SHOW_ATTRIBUTE(export_features);
176 
177 static const struct file_operations pool_stats_operations = {
178 	.open		= nfsd_pool_stats_open,
179 	.read		= seq_read,
180 	.llseek		= seq_lseek,
181 	.release	= seq_release,
182 };
183 
184 DEFINE_SHOW_ATTRIBUTE(nfsd_reply_cache_stats);
185 
186 DEFINE_SHOW_ATTRIBUTE(nfsd_file_cache_stats);
187 
188 /*----------------------------------------------------------------------------*/
189 /*
190  * payload - write methods
191  */
192 
193 static inline struct net *netns(struct file *file)
194 {
195 	return file_inode(file)->i_sb->s_fs_info;
196 }
197 
198 /*
199  * write_unlock_ip - Release all locks used by a client
200  *
201  * Experimental.
202  *
203  * Input:
204  *			buf:	'\n'-terminated C string containing a
205  *				presentation format IP address
206  *			size:	length of C string in @buf
207  * Output:
208  *	On success:	returns zero if all specified locks were released;
209  *			returns one if one or more locks were not released
210  *	On error:	return code is negative errno value
211  */
212 static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size)
213 {
214 	struct sockaddr_storage address;
215 	struct sockaddr *sap = (struct sockaddr *)&address;
216 	size_t salen = sizeof(address);
217 	char *fo_path;
218 	struct net *net = netns(file);
219 
220 	/* sanity check */
221 	if (size == 0)
222 		return -EINVAL;
223 
224 	if (buf[size-1] != '\n')
225 		return -EINVAL;
226 
227 	fo_path = buf;
228 	if (qword_get(&buf, fo_path, size) < 0)
229 		return -EINVAL;
230 
231 	if (rpc_pton(net, fo_path, size, sap, salen) == 0)
232 		return -EINVAL;
233 
234 	trace_nfsd_ctl_unlock_ip(net, buf);
235 	return nlmsvc_unlock_all_by_ip(sap);
236 }
237 
238 /*
239  * write_unlock_fs - Release all locks on a local file system
240  *
241  * Experimental.
242  *
243  * Input:
244  *			buf:	'\n'-terminated C string containing the
245  *				absolute pathname of a local file system
246  *			size:	length of C string in @buf
247  * Output:
248  *	On success:	returns zero if all specified locks were released;
249  *			returns one if one or more locks were not released
250  *	On error:	return code is negative errno value
251  */
252 static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size)
253 {
254 	struct path path;
255 	char *fo_path;
256 	int error;
257 
258 	/* sanity check */
259 	if (size == 0)
260 		return -EINVAL;
261 
262 	if (buf[size-1] != '\n')
263 		return -EINVAL;
264 
265 	fo_path = buf;
266 	if (qword_get(&buf, fo_path, size) < 0)
267 		return -EINVAL;
268 	trace_nfsd_ctl_unlock_fs(netns(file), fo_path);
269 	error = kern_path(fo_path, 0, &path);
270 	if (error)
271 		return error;
272 
273 	/*
274 	 * XXX: Needs better sanity checking.  Otherwise we could end up
275 	 * releasing locks on the wrong file system.
276 	 *
277 	 * For example:
278 	 * 1.  Does the path refer to a directory?
279 	 * 2.  Is that directory a mount point, or
280 	 * 3.  Is that directory the root of an exported file system?
281 	 */
282 	error = nlmsvc_unlock_all_by_sb(path.dentry->d_sb);
283 	nfsd4_revoke_states(netns(file), path.dentry->d_sb);
284 
285 	path_put(&path);
286 	return error;
287 }
288 
289 /*
290  * write_filehandle - Get a variable-length NFS file handle by path
291  *
292  * On input, the buffer contains a '\n'-terminated C string comprised of
293  * three alphanumeric words separated by whitespace.  The string may
294  * contain escape sequences.
295  *
296  * Input:
297  *			buf:
298  *				domain:		client domain name
299  *				path:		export pathname
300  *				maxsize:	numeric maximum size of
301  *						@buf
302  *			size:	length of C string in @buf
303  * Output:
304  *	On success:	passed-in buffer filled with '\n'-terminated C
305  *			string containing a ASCII hex text version
306  *			of the NFS file handle;
307  *			return code is the size in bytes of the string
308  *	On error:	return code is negative errno value
309  */
310 static ssize_t write_filehandle(struct file *file, char *buf, size_t size)
311 {
312 	char *dname, *path;
313 	int maxsize;
314 	char *mesg = buf;
315 	int len;
316 	struct auth_domain *dom;
317 	struct knfsd_fh fh;
318 
319 	if (size == 0)
320 		return -EINVAL;
321 
322 	if (buf[size-1] != '\n')
323 		return -EINVAL;
324 	buf[size-1] = 0;
325 
326 	dname = mesg;
327 	len = qword_get(&mesg, dname, size);
328 	if (len <= 0)
329 		return -EINVAL;
330 
331 	path = dname+len+1;
332 	len = qword_get(&mesg, path, size);
333 	if (len <= 0)
334 		return -EINVAL;
335 
336 	len = get_int(&mesg, &maxsize);
337 	if (len)
338 		return len;
339 
340 	if (maxsize < NFS_FHSIZE)
341 		return -EINVAL;
342 	maxsize = min(maxsize, NFS3_FHSIZE);
343 
344 	if (qword_get(&mesg, mesg, size) > 0)
345 		return -EINVAL;
346 
347 	trace_nfsd_ctl_filehandle(netns(file), dname, path, maxsize);
348 
349 	/* we have all the words, they are in buf.. */
350 	dom = unix_domain_find(dname);
351 	if (!dom)
352 		return -ENOMEM;
353 
354 	len = exp_rootfh(netns(file), dom, path, &fh, maxsize);
355 	auth_domain_put(dom);
356 	if (len)
357 		return len;
358 
359 	mesg = buf;
360 	len = SIMPLE_TRANSACTION_LIMIT;
361 	qword_addhex(&mesg, &len, fh.fh_raw, fh.fh_size);
362 	mesg[-1] = '\n';
363 	return mesg - buf;
364 }
365 
366 /*
367  * write_threads - Start NFSD, or report the current number of running threads
368  *
369  * Input:
370  *			buf:		ignored
371  *			size:		zero
372  * Output:
373  *	On success:	passed-in buffer filled with '\n'-terminated C
374  *			string numeric value representing the number of
375  *			running NFSD threads;
376  *			return code is the size in bytes of the string
377  *	On error:	return code is zero
378  *
379  * OR
380  *
381  * Input:
382  *			buf:		C string containing an unsigned
383  *					integer value representing the
384  *					number of NFSD threads to start
385  *			size:		non-zero length of C string in @buf
386  * Output:
387  *	On success:	NFS service is started;
388  *			passed-in buffer filled with '\n'-terminated C
389  *			string numeric value representing the number of
390  *			running NFSD threads;
391  *			return code is the size in bytes of the string
392  *	On error:	return code is zero or a negative errno value
393  */
394 static ssize_t write_threads(struct file *file, char *buf, size_t size)
395 {
396 	char *mesg = buf;
397 	int rv;
398 	struct net *net = netns(file);
399 
400 	if (size > 0) {
401 		int newthreads;
402 		rv = get_int(&mesg, &newthreads);
403 		if (rv)
404 			return rv;
405 		if (newthreads < 0)
406 			return -EINVAL;
407 		trace_nfsd_ctl_threads(net, newthreads);
408 		mutex_lock(&nfsd_mutex);
409 		rv = nfsd_svc(newthreads, net, file->f_cred, NULL);
410 		mutex_unlock(&nfsd_mutex);
411 		if (rv < 0)
412 			return rv;
413 	} else
414 		rv = nfsd_nrthreads(net);
415 
416 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", rv);
417 }
418 
419 /*
420  * write_pool_threads - Set or report the current number of threads per pool
421  *
422  * Input:
423  *			buf:		ignored
424  *			size:		zero
425  *
426  * OR
427  *
428  * Input:
429  *			buf:		C string containing whitespace-
430  *					separated unsigned integer values
431  *					representing the number of NFSD
432  *					threads to start in each pool
433  *			size:		non-zero length of C string in @buf
434  * Output:
435  *	On success:	passed-in buffer filled with '\n'-terminated C
436  *			string containing integer values representing the
437  *			number of NFSD threads in each pool;
438  *			return code is the size in bytes of the string
439  *	On error:	return code is zero or a negative errno value
440  */
441 static ssize_t write_pool_threads(struct file *file, char *buf, size_t size)
442 {
443 	/* if size > 0, look for an array of number of threads per node
444 	 * and apply them  then write out number of threads per node as reply
445 	 */
446 	char *mesg = buf;
447 	int i;
448 	int rv;
449 	int len;
450 	int npools;
451 	int *nthreads;
452 	struct net *net = netns(file);
453 
454 	mutex_lock(&nfsd_mutex);
455 	npools = nfsd_nrpools(net);
456 	if (npools == 0) {
457 		/*
458 		 * NFS is shut down.  The admin can start it by
459 		 * writing to the threads file but NOT the pool_threads
460 		 * file, sorry.  Report zero threads.
461 		 */
462 		mutex_unlock(&nfsd_mutex);
463 		strcpy(buf, "0\n");
464 		return strlen(buf);
465 	}
466 
467 	nthreads = kcalloc(npools, sizeof(int), GFP_KERNEL);
468 	rv = -ENOMEM;
469 	if (nthreads == NULL)
470 		goto out_free;
471 
472 	if (size > 0) {
473 		for (i = 0; i < npools; i++) {
474 			rv = get_int(&mesg, &nthreads[i]);
475 			if (rv == -ENOENT)
476 				break;		/* fewer numbers than pools */
477 			if (rv)
478 				goto out_free;	/* syntax error */
479 			rv = -EINVAL;
480 			if (nthreads[i] < 0)
481 				goto out_free;
482 			trace_nfsd_ctl_pool_threads(net, i, nthreads[i]);
483 		}
484 		rv = nfsd_set_nrthreads(i, nthreads, net);
485 		if (rv)
486 			goto out_free;
487 	}
488 
489 	rv = nfsd_get_nrthreads(npools, nthreads, net);
490 	if (rv)
491 		goto out_free;
492 
493 	mesg = buf;
494 	size = SIMPLE_TRANSACTION_LIMIT;
495 	for (i = 0; i < npools && size > 0; i++) {
496 		snprintf(mesg, size, "%d%c", nthreads[i], (i == npools-1 ? '\n' : ' '));
497 		len = strlen(mesg);
498 		size -= len;
499 		mesg += len;
500 	}
501 	rv = mesg - buf;
502 out_free:
503 	kfree(nthreads);
504 	mutex_unlock(&nfsd_mutex);
505 	return rv;
506 }
507 
508 static ssize_t
509 nfsd_print_version_support(struct nfsd_net *nn, char *buf, int remaining,
510 		const char *sep, unsigned vers, int minor)
511 {
512 	const char *format = minor < 0 ? "%s%c%u" : "%s%c%u.%u";
513 	bool supported = !!nfsd_vers(nn, vers, NFSD_TEST);
514 
515 	if (vers == 4 && minor >= 0 &&
516 	    !nfsd_minorversion(nn, minor, NFSD_TEST))
517 		supported = false;
518 	if (minor == 0 && supported)
519 		/*
520 		 * special case for backward compatability.
521 		 * +4.0 is never reported, it is implied by
522 		 * +4, unless -4.0 is present.
523 		 */
524 		return 0;
525 	return snprintf(buf, remaining, format, sep,
526 			supported ? '+' : '-', vers, minor);
527 }
528 
529 static ssize_t __write_versions(struct file *file, char *buf, size_t size)
530 {
531 	char *mesg = buf;
532 	char *vers, *minorp, sign;
533 	int len, num, remaining;
534 	ssize_t tlen = 0;
535 	char *sep;
536 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
537 
538 	if (size > 0) {
539 		if (nn->nfsd_serv)
540 			/* Cannot change versions without updating
541 			 * nn->nfsd_serv->sv_xdrsize, and reallocing
542 			 * rq_argp and rq_resp
543 			 */
544 			return -EBUSY;
545 		if (buf[size-1] != '\n')
546 			return -EINVAL;
547 		buf[size-1] = 0;
548 		trace_nfsd_ctl_version(netns(file), buf);
549 
550 		vers = mesg;
551 		len = qword_get(&mesg, vers, size);
552 		if (len <= 0) return -EINVAL;
553 		do {
554 			enum vers_op cmd;
555 			unsigned minor;
556 			sign = *vers;
557 			if (sign == '+' || sign == '-')
558 				num = simple_strtol((vers+1), &minorp, 0);
559 			else
560 				num = simple_strtol(vers, &minorp, 0);
561 			if (*minorp == '.') {
562 				if (num != 4)
563 					return -EINVAL;
564 				if (kstrtouint(minorp+1, 0, &minor) < 0)
565 					return -EINVAL;
566 			}
567 
568 			cmd = sign == '-' ? NFSD_CLEAR : NFSD_SET;
569 			switch(num) {
570 #ifdef CONFIG_NFSD_V2
571 			case 2:
572 #endif
573 			case 3:
574 				nfsd_vers(nn, num, cmd);
575 				break;
576 			case 4:
577 				if (*minorp == '.') {
578 					if (nfsd_minorversion(nn, minor, cmd) < 0)
579 						return -EINVAL;
580 				} else if ((cmd == NFSD_SET) != nfsd_vers(nn, num, NFSD_TEST)) {
581 					/*
582 					 * Either we have +4 and no minors are enabled,
583 					 * or we have -4 and at least one minor is enabled.
584 					 * In either case, propagate 'cmd' to all minors.
585 					 */
586 					minor = 0;
587 					while (nfsd_minorversion(nn, minor, cmd) >= 0)
588 						minor++;
589 				}
590 				break;
591 			default:
592 				/* Ignore requests to disable non-existent versions */
593 				if (cmd == NFSD_SET)
594 					return -EINVAL;
595 			}
596 			vers += len + 1;
597 		} while ((len = qword_get(&mesg, vers, size)) > 0);
598 		/* If all get turned off, turn them back on, as
599 		 * having no versions is BAD
600 		 */
601 		nfsd_reset_versions(nn);
602 	}
603 
604 	/* Now write current state into reply buffer */
605 	sep = "";
606 	remaining = SIMPLE_TRANSACTION_LIMIT;
607 	for (num=2 ; num <= 4 ; num++) {
608 		int minor;
609 		if (!nfsd_vers(nn, num, NFSD_AVAIL))
610 			continue;
611 
612 		minor = -1;
613 		do {
614 			len = nfsd_print_version_support(nn, buf, remaining,
615 					sep, num, minor);
616 			if (len >= remaining)
617 				goto out;
618 			remaining -= len;
619 			buf += len;
620 			tlen += len;
621 			minor++;
622 			if (len)
623 				sep = " ";
624 		} while (num == 4 && minor <= NFSD_SUPPORTED_MINOR_VERSION);
625 	}
626 out:
627 	len = snprintf(buf, remaining, "\n");
628 	if (len >= remaining)
629 		return -EINVAL;
630 	return tlen + len;
631 }
632 
633 /*
634  * write_versions - Set or report the available NFS protocol versions
635  *
636  * Input:
637  *			buf:		ignored
638  *			size:		zero
639  * Output:
640  *	On success:	passed-in buffer filled with '\n'-terminated C
641  *			string containing positive or negative integer
642  *			values representing the current status of each
643  *			protocol version;
644  *			return code is the size in bytes of the string
645  *	On error:	return code is zero or a negative errno value
646  *
647  * OR
648  *
649  * Input:
650  *			buf:		C string containing whitespace-
651  *					separated positive or negative
652  *					integer values representing NFS
653  *					protocol versions to enable ("+n")
654  *					or disable ("-n")
655  *			size:		non-zero length of C string in @buf
656  * Output:
657  *	On success:	status of zero or more protocol versions has
658  *			been updated; passed-in buffer filled with
659  *			'\n'-terminated C string containing positive
660  *			or negative integer values representing the
661  *			current status of each protocol version;
662  *			return code is the size in bytes of the string
663  *	On error:	return code is zero or a negative errno value
664  */
665 static ssize_t write_versions(struct file *file, char *buf, size_t size)
666 {
667 	ssize_t rv;
668 
669 	mutex_lock(&nfsd_mutex);
670 	rv = __write_versions(file, buf, size);
671 	mutex_unlock(&nfsd_mutex);
672 	return rv;
673 }
674 
675 /*
676  * Zero-length write.  Return a list of NFSD's current listener
677  * transports.
678  */
679 static ssize_t __write_ports_names(char *buf, struct net *net)
680 {
681 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
682 
683 	if (nn->nfsd_serv == NULL)
684 		return 0;
685 	return svc_xprt_names(nn->nfsd_serv, buf, SIMPLE_TRANSACTION_LIMIT);
686 }
687 
688 /*
689  * A single 'fd' number was written, in which case it must be for
690  * a socket of a supported family/protocol, and we use it as an
691  * nfsd listener.
692  */
693 static ssize_t __write_ports_addfd(char *buf, struct net *net, const struct cred *cred)
694 {
695 	char *mesg = buf;
696 	int fd, err;
697 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
698 	struct svc_serv *serv;
699 
700 	err = get_int(&mesg, &fd);
701 	if (err != 0 || fd < 0)
702 		return -EINVAL;
703 	trace_nfsd_ctl_ports_addfd(net, fd);
704 
705 	err = nfsd_create_serv(net);
706 	if (err != 0)
707 		return err;
708 
709 	serv = nn->nfsd_serv;
710 	err = svc_addsock(serv, net, fd, buf, SIMPLE_TRANSACTION_LIMIT, cred);
711 
712 	if (!serv->sv_nrthreads && list_empty(&nn->nfsd_serv->sv_permsocks))
713 		nfsd_destroy_serv(net);
714 
715 	return err;
716 }
717 
718 /*
719  * A transport listener is added by writing its transport name and
720  * a port number.
721  */
722 static ssize_t __write_ports_addxprt(char *buf, struct net *net, const struct cred *cred)
723 {
724 	char transport[16];
725 	struct svc_xprt *xprt;
726 	int port, err;
727 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
728 	struct svc_serv *serv;
729 
730 	if (sscanf(buf, "%15s %5u", transport, &port) != 2)
731 		return -EINVAL;
732 
733 	if (port < 1 || port > USHRT_MAX)
734 		return -EINVAL;
735 	trace_nfsd_ctl_ports_addxprt(net, transport, port);
736 
737 	err = nfsd_create_serv(net);
738 	if (err != 0)
739 		return err;
740 
741 	serv = nn->nfsd_serv;
742 	err = svc_xprt_create(serv, transport, net,
743 			      PF_INET, port, SVC_SOCK_ANONYMOUS, cred);
744 	if (err < 0)
745 		goto out_err;
746 
747 	err = svc_xprt_create(serv, transport, net,
748 			      PF_INET6, port, SVC_SOCK_ANONYMOUS, cred);
749 	if (err < 0 && err != -EAFNOSUPPORT)
750 		goto out_close;
751 
752 	return 0;
753 out_close:
754 	xprt = svc_find_xprt(serv, transport, net, PF_INET, port);
755 	if (xprt != NULL) {
756 		svc_xprt_close(xprt);
757 		svc_xprt_put(xprt);
758 	}
759 out_err:
760 	if (!serv->sv_nrthreads && list_empty(&nn->nfsd_serv->sv_permsocks))
761 		nfsd_destroy_serv(net);
762 
763 	return err;
764 }
765 
766 static ssize_t __write_ports(struct file *file, char *buf, size_t size,
767 			     struct net *net)
768 {
769 	if (size == 0)
770 		return __write_ports_names(buf, net);
771 
772 	if (isdigit(buf[0]))
773 		return __write_ports_addfd(buf, net, file->f_cred);
774 
775 	if (isalpha(buf[0]))
776 		return __write_ports_addxprt(buf, net, file->f_cred);
777 
778 	return -EINVAL;
779 }
780 
781 /*
782  * write_ports - Pass a socket file descriptor or transport name to listen on
783  *
784  * Input:
785  *			buf:		ignored
786  *			size:		zero
787  * Output:
788  *	On success:	passed-in buffer filled with a '\n'-terminated C
789  *			string containing a whitespace-separated list of
790  *			named NFSD listeners;
791  *			return code is the size in bytes of the string
792  *	On error:	return code is zero or a negative errno value
793  *
794  * OR
795  *
796  * Input:
797  *			buf:		C string containing an unsigned
798  *					integer value representing a bound
799  *					but unconnected socket that is to be
800  *					used as an NFSD listener; listen(3)
801  *					must be called for a SOCK_STREAM
802  *					socket, otherwise it is ignored
803  *			size:		non-zero length of C string in @buf
804  * Output:
805  *	On success:	NFS service is started;
806  *			passed-in buffer filled with a '\n'-terminated C
807  *			string containing a unique alphanumeric name of
808  *			the listener;
809  *			return code is the size in bytes of the string
810  *	On error:	return code is a negative errno value
811  *
812  * OR
813  *
814  * Input:
815  *			buf:		C string containing a transport
816  *					name and an unsigned integer value
817  *					representing the port to listen on,
818  *					separated by whitespace
819  *			size:		non-zero length of C string in @buf
820  * Output:
821  *	On success:	returns zero; NFS service is started
822  *	On error:	return code is a negative errno value
823  */
824 static ssize_t write_ports(struct file *file, char *buf, size_t size)
825 {
826 	ssize_t rv;
827 
828 	mutex_lock(&nfsd_mutex);
829 	rv = __write_ports(file, buf, size, netns(file));
830 	mutex_unlock(&nfsd_mutex);
831 	return rv;
832 }
833 
834 
835 int nfsd_max_blksize;
836 
837 /*
838  * write_maxblksize - Set or report the current NFS blksize
839  *
840  * Input:
841  *			buf:		ignored
842  *			size:		zero
843  *
844  * OR
845  *
846  * Input:
847  *			buf:		C string containing an unsigned
848  *					integer value representing the new
849  *					NFS blksize
850  *			size:		non-zero length of C string in @buf
851  * Output:
852  *	On success:	passed-in buffer filled with '\n'-terminated C string
853  *			containing numeric value of the current NFS blksize
854  *			setting;
855  *			return code is the size in bytes of the string
856  *	On error:	return code is zero or a negative errno value
857  */
858 static ssize_t write_maxblksize(struct file *file, char *buf, size_t size)
859 {
860 	char *mesg = buf;
861 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
862 
863 	if (size > 0) {
864 		int bsize;
865 		int rv = get_int(&mesg, &bsize);
866 		if (rv)
867 			return rv;
868 		trace_nfsd_ctl_maxblksize(netns(file), bsize);
869 
870 		/* force bsize into allowed range and
871 		 * required alignment.
872 		 */
873 		bsize = max_t(int, bsize, 1024);
874 		bsize = min_t(int, bsize, NFSSVC_MAXBLKSIZE);
875 		bsize &= ~(1024-1);
876 		mutex_lock(&nfsd_mutex);
877 		if (nn->nfsd_serv) {
878 			mutex_unlock(&nfsd_mutex);
879 			return -EBUSY;
880 		}
881 		nfsd_max_blksize = bsize;
882 		mutex_unlock(&nfsd_mutex);
883 	}
884 
885 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n",
886 							nfsd_max_blksize);
887 }
888 
889 /*
890  * write_maxconn - Set or report the current max number of connections
891  *
892  * Input:
893  *			buf:		ignored
894  *			size:		zero
895  * OR
896  *
897  * Input:
898  *			buf:		C string containing an unsigned
899  *					integer value representing the new
900  *					number of max connections
901  *			size:		non-zero length of C string in @buf
902  * Output:
903  *	On success:	passed-in buffer filled with '\n'-terminated C string
904  *			containing numeric value of max_connections setting
905  *			for this net namespace;
906  *			return code is the size in bytes of the string
907  *	On error:	return code is zero or a negative errno value
908  */
909 static ssize_t write_maxconn(struct file *file, char *buf, size_t size)
910 {
911 	char *mesg = buf;
912 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
913 	unsigned int maxconn = nn->max_connections;
914 
915 	if (size > 0) {
916 		int rv = get_uint(&mesg, &maxconn);
917 
918 		if (rv)
919 			return rv;
920 		trace_nfsd_ctl_maxconn(netns(file), maxconn);
921 		nn->max_connections = maxconn;
922 	}
923 
924 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%u\n", maxconn);
925 }
926 
927 #ifdef CONFIG_NFSD_V4
928 static ssize_t __nfsd4_write_time(struct file *file, char *buf, size_t size,
929 				  time64_t *time, struct nfsd_net *nn)
930 {
931 	struct dentry *dentry = file_dentry(file);
932 	char *mesg = buf;
933 	int rv, i;
934 
935 	if (size > 0) {
936 		if (nn->nfsd_serv)
937 			return -EBUSY;
938 		rv = get_int(&mesg, &i);
939 		if (rv)
940 			return rv;
941 		trace_nfsd_ctl_time(netns(file), dentry->d_name.name,
942 				    dentry->d_name.len, i);
943 
944 		/*
945 		 * Some sanity checking.  We don't have a reason for
946 		 * these particular numbers, but problems with the
947 		 * extremes are:
948 		 *	- Too short: the briefest network outage may
949 		 *	  cause clients to lose all their locks.  Also,
950 		 *	  the frequent polling may be wasteful.
951 		 *	- Too long: do you really want reboot recovery
952 		 *	  to take more than an hour?  Or to make other
953 		 *	  clients wait an hour before being able to
954 		 *	  revoke a dead client's locks?
955 		 */
956 		if (i < 10 || i > 3600)
957 			return -EINVAL;
958 		*time = i;
959 	}
960 
961 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%lld\n", *time);
962 }
963 
964 static ssize_t nfsd4_write_time(struct file *file, char *buf, size_t size,
965 				time64_t *time, struct nfsd_net *nn)
966 {
967 	ssize_t rv;
968 
969 	mutex_lock(&nfsd_mutex);
970 	rv = __nfsd4_write_time(file, buf, size, time, nn);
971 	mutex_unlock(&nfsd_mutex);
972 	return rv;
973 }
974 
975 /*
976  * write_leasetime - Set or report the current NFSv4 lease time
977  *
978  * Input:
979  *			buf:		ignored
980  *			size:		zero
981  *
982  * OR
983  *
984  * Input:
985  *			buf:		C string containing an unsigned
986  *					integer value representing the new
987  *					NFSv4 lease expiry time
988  *			size:		non-zero length of C string in @buf
989  * Output:
990  *	On success:	passed-in buffer filled with '\n'-terminated C
991  *			string containing unsigned integer value of the
992  *			current lease expiry time;
993  *			return code is the size in bytes of the string
994  *	On error:	return code is zero or a negative errno value
995  */
996 static ssize_t write_leasetime(struct file *file, char *buf, size_t size)
997 {
998 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
999 	return nfsd4_write_time(file, buf, size, &nn->nfsd4_lease, nn);
1000 }
1001 
1002 /*
1003  * write_gracetime - Set or report current NFSv4 grace period time
1004  *
1005  * As above, but sets the time of the NFSv4 grace period.
1006  *
1007  * Note this should never be set to less than the *previous*
1008  * lease-period time, but we don't try to enforce this.  (In the common
1009  * case (a new boot), we don't know what the previous lease time was
1010  * anyway.)
1011  */
1012 static ssize_t write_gracetime(struct file *file, char *buf, size_t size)
1013 {
1014 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
1015 	return nfsd4_write_time(file, buf, size, &nn->nfsd4_grace, nn);
1016 }
1017 
1018 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
1019 static ssize_t __write_recoverydir(struct file *file, char *buf, size_t size,
1020 				   struct nfsd_net *nn)
1021 {
1022 	char *mesg = buf;
1023 	char *recdir;
1024 	int len, status;
1025 
1026 	if (size > 0) {
1027 		if (nn->nfsd_serv)
1028 			return -EBUSY;
1029 		if (size > PATH_MAX || buf[size-1] != '\n')
1030 			return -EINVAL;
1031 		buf[size-1] = 0;
1032 
1033 		recdir = mesg;
1034 		len = qword_get(&mesg, recdir, size);
1035 		if (len <= 0)
1036 			return -EINVAL;
1037 		trace_nfsd_ctl_recoverydir(netns(file), recdir);
1038 
1039 		status = nfs4_reset_recoverydir(recdir);
1040 		if (status)
1041 			return status;
1042 	}
1043 
1044 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%s\n",
1045 							nfs4_recoverydir());
1046 }
1047 
1048 /*
1049  * write_recoverydir - Set or report the pathname of the recovery directory
1050  *
1051  * Input:
1052  *			buf:		ignored
1053  *			size:		zero
1054  *
1055  * OR
1056  *
1057  * Input:
1058  *			buf:		C string containing the pathname
1059  *					of the directory on a local file
1060  *					system containing permanent NFSv4
1061  *					recovery data
1062  *			size:		non-zero length of C string in @buf
1063  * Output:
1064  *	On success:	passed-in buffer filled with '\n'-terminated C string
1065  *			containing the current recovery pathname setting;
1066  *			return code is the size in bytes of the string
1067  *	On error:	return code is zero or a negative errno value
1068  */
1069 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size)
1070 {
1071 	ssize_t rv;
1072 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
1073 
1074 	mutex_lock(&nfsd_mutex);
1075 	rv = __write_recoverydir(file, buf, size, nn);
1076 	mutex_unlock(&nfsd_mutex);
1077 	return rv;
1078 }
1079 #endif
1080 
1081 /*
1082  * write_v4_end_grace - release grace period for nfsd's v4.x lock manager
1083  *
1084  * Input:
1085  *			buf:		ignored
1086  *			size:		zero
1087  * OR
1088  *
1089  * Input:
1090  *			buf:		any value
1091  *			size:		non-zero length of C string in @buf
1092  * Output:
1093  *			passed-in buffer filled with "Y" or "N" with a newline
1094  *			and NULL-terminated C string. This indicates whether
1095  *			the grace period has ended in the current net
1096  *			namespace. Return code is the size in bytes of the
1097  *			string. Writing a string that starts with 'Y', 'y', or
1098  *			'1' to the file will end the grace period for nfsd's v4
1099  *			lock manager.
1100  */
1101 static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size)
1102 {
1103 	struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id);
1104 
1105 	if (size > 0) {
1106 		switch(buf[0]) {
1107 		case 'Y':
1108 		case 'y':
1109 		case '1':
1110 			if (!nn->nfsd_serv)
1111 				return -EBUSY;
1112 			trace_nfsd_end_grace(netns(file));
1113 			nfsd4_end_grace(nn);
1114 			break;
1115 		default:
1116 			return -EINVAL;
1117 		}
1118 	}
1119 
1120 	return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%c\n",
1121 			 nn->grace_ended ? 'Y' : 'N');
1122 }
1123 
1124 #endif
1125 
1126 /*----------------------------------------------------------------------------*/
1127 /*
1128  *	populating the filesystem.
1129  */
1130 
1131 /* Basically copying rpc_get_inode. */
1132 static struct inode *nfsd_get_inode(struct super_block *sb, umode_t mode)
1133 {
1134 	struct inode *inode = new_inode(sb);
1135 	if (!inode)
1136 		return NULL;
1137 	/* Following advice from simple_fill_super documentation: */
1138 	inode->i_ino = iunique(sb, NFSD_MaxReserved);
1139 	inode->i_mode = mode;
1140 	simple_inode_init_ts(inode);
1141 	switch (mode & S_IFMT) {
1142 	case S_IFDIR:
1143 		inode->i_fop = &simple_dir_operations;
1144 		inode->i_op = &simple_dir_inode_operations;
1145 		inc_nlink(inode);
1146 		break;
1147 	case S_IFLNK:
1148 		inode->i_op = &simple_symlink_inode_operations;
1149 		break;
1150 	default:
1151 		break;
1152 	}
1153 	return inode;
1154 }
1155 
1156 static int __nfsd_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode, struct nfsdfs_client *ncl)
1157 {
1158 	struct inode *inode;
1159 
1160 	inode = nfsd_get_inode(dir->i_sb, mode);
1161 	if (!inode)
1162 		return -ENOMEM;
1163 	if (ncl) {
1164 		inode->i_private = ncl;
1165 		kref_get(&ncl->cl_ref);
1166 	}
1167 	d_add(dentry, inode);
1168 	inc_nlink(dir);
1169 	fsnotify_mkdir(dir, dentry);
1170 	return 0;
1171 }
1172 
1173 static struct dentry *nfsd_mkdir(struct dentry *parent, struct nfsdfs_client *ncl, char *name)
1174 {
1175 	struct inode *dir = parent->d_inode;
1176 	struct dentry *dentry;
1177 	int ret = -ENOMEM;
1178 
1179 	inode_lock(dir);
1180 	dentry = d_alloc_name(parent, name);
1181 	if (!dentry)
1182 		goto out_err;
1183 	ret = __nfsd_mkdir(d_inode(parent), dentry, S_IFDIR | 0600, ncl);
1184 	if (ret)
1185 		goto out_err;
1186 out:
1187 	inode_unlock(dir);
1188 	return dentry;
1189 out_err:
1190 	dput(dentry);
1191 	dentry = ERR_PTR(ret);
1192 	goto out;
1193 }
1194 
1195 #if IS_ENABLED(CONFIG_SUNRPC_GSS)
1196 static int __nfsd_symlink(struct inode *dir, struct dentry *dentry,
1197 			  umode_t mode, const char *content)
1198 {
1199 	struct inode *inode;
1200 
1201 	inode = nfsd_get_inode(dir->i_sb, mode);
1202 	if (!inode)
1203 		return -ENOMEM;
1204 
1205 	inode->i_link = (char *)content;
1206 	inode->i_size = strlen(content);
1207 
1208 	d_add(dentry, inode);
1209 	inc_nlink(dir);
1210 	fsnotify_create(dir, dentry);
1211 	return 0;
1212 }
1213 
1214 /*
1215  * @content is assumed to be a NUL-terminated string that lives
1216  * longer than the symlink itself.
1217  */
1218 static void _nfsd_symlink(struct dentry *parent, const char *name,
1219 			  const char *content)
1220 {
1221 	struct inode *dir = parent->d_inode;
1222 	struct dentry *dentry;
1223 	int ret;
1224 
1225 	inode_lock(dir);
1226 	dentry = d_alloc_name(parent, name);
1227 	if (!dentry)
1228 		goto out;
1229 	ret = __nfsd_symlink(d_inode(parent), dentry, S_IFLNK | 0777, content);
1230 	if (ret)
1231 		dput(dentry);
1232 out:
1233 	inode_unlock(dir);
1234 }
1235 #else
1236 static inline void _nfsd_symlink(struct dentry *parent, const char *name,
1237 				 const char *content)
1238 {
1239 }
1240 
1241 #endif
1242 
1243 static void clear_ncl(struct dentry *dentry)
1244 {
1245 	struct inode *inode = d_inode(dentry);
1246 	struct nfsdfs_client *ncl = inode->i_private;
1247 
1248 	spin_lock(&inode->i_lock);
1249 	inode->i_private = NULL;
1250 	spin_unlock(&inode->i_lock);
1251 	kref_put(&ncl->cl_ref, ncl->cl_release);
1252 }
1253 
1254 struct nfsdfs_client *get_nfsdfs_client(struct inode *inode)
1255 {
1256 	struct nfsdfs_client *nc;
1257 
1258 	spin_lock(&inode->i_lock);
1259 	nc = inode->i_private;
1260 	if (nc)
1261 		kref_get(&nc->cl_ref);
1262 	spin_unlock(&inode->i_lock);
1263 	return nc;
1264 }
1265 
1266 /* XXX: cut'n'paste from simple_fill_super; figure out if we could share
1267  * code instead. */
1268 static  int nfsdfs_create_files(struct dentry *root,
1269 				const struct tree_descr *files,
1270 				struct nfsdfs_client *ncl,
1271 				struct dentry **fdentries)
1272 {
1273 	struct inode *dir = d_inode(root);
1274 	struct inode *inode;
1275 	struct dentry *dentry;
1276 	int i;
1277 
1278 	inode_lock(dir);
1279 	for (i = 0; files->name && files->name[0]; i++, files++) {
1280 		dentry = d_alloc_name(root, files->name);
1281 		if (!dentry)
1282 			goto out;
1283 		inode = nfsd_get_inode(d_inode(root)->i_sb,
1284 					S_IFREG | files->mode);
1285 		if (!inode) {
1286 			dput(dentry);
1287 			goto out;
1288 		}
1289 		kref_get(&ncl->cl_ref);
1290 		inode->i_fop = files->ops;
1291 		inode->i_private = ncl;
1292 		d_add(dentry, inode);
1293 		fsnotify_create(dir, dentry);
1294 		if (fdentries)
1295 			fdentries[i] = dentry;
1296 	}
1297 	inode_unlock(dir);
1298 	return 0;
1299 out:
1300 	inode_unlock(dir);
1301 	return -ENOMEM;
1302 }
1303 
1304 /* on success, returns positive number unique to that client. */
1305 struct dentry *nfsd_client_mkdir(struct nfsd_net *nn,
1306 				 struct nfsdfs_client *ncl, u32 id,
1307 				 const struct tree_descr *files,
1308 				 struct dentry **fdentries)
1309 {
1310 	struct dentry *dentry;
1311 	char name[11];
1312 	int ret;
1313 
1314 	sprintf(name, "%u", id);
1315 
1316 	dentry = nfsd_mkdir(nn->nfsd_client_dir, ncl, name);
1317 	if (IS_ERR(dentry)) /* XXX: tossing errors? */
1318 		return NULL;
1319 	ret = nfsdfs_create_files(dentry, files, ncl, fdentries);
1320 	if (ret) {
1321 		nfsd_client_rmdir(dentry);
1322 		return NULL;
1323 	}
1324 	return dentry;
1325 }
1326 
1327 /* Taken from __rpc_rmdir: */
1328 void nfsd_client_rmdir(struct dentry *dentry)
1329 {
1330 	simple_recursive_removal(dentry, clear_ncl);
1331 }
1332 
1333 static int nfsd_fill_super(struct super_block *sb, struct fs_context *fc)
1334 {
1335 	struct nfsd_net *nn = net_generic(current->nsproxy->net_ns,
1336 							nfsd_net_id);
1337 	struct dentry *dentry;
1338 	int ret;
1339 
1340 	static const struct tree_descr nfsd_files[] = {
1341 		[NFSD_List] = {"exports", &exports_nfsd_operations, S_IRUGO},
1342 		/* Per-export io stats use same ops as exports file */
1343 		[NFSD_Export_Stats] = {"export_stats", &exports_nfsd_operations, S_IRUGO},
1344 		[NFSD_Export_features] = {"export_features",
1345 					&export_features_fops, S_IRUGO},
1346 		[NFSD_FO_UnlockIP] = {"unlock_ip",
1347 					&transaction_ops, S_IWUSR|S_IRUSR},
1348 		[NFSD_FO_UnlockFS] = {"unlock_filesystem",
1349 					&transaction_ops, S_IWUSR|S_IRUSR},
1350 		[NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR},
1351 		[NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR},
1352 		[NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR},
1353 		[NFSD_Pool_Stats] = {"pool_stats", &pool_stats_operations, S_IRUGO},
1354 		[NFSD_Reply_Cache_Stats] = {"reply_cache_stats",
1355 					&nfsd_reply_cache_stats_fops, S_IRUGO},
1356 		[NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR},
1357 		[NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO},
1358 		[NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO},
1359 		[NFSD_MaxConnections] = {"max_connections", &transaction_ops, S_IWUSR|S_IRUGO},
1360 		[NFSD_Filecache] = {"filecache", &nfsd_file_cache_stats_fops, S_IRUGO},
1361 #ifdef CONFIG_NFSD_V4
1362 		[NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR},
1363 		[NFSD_Gracetime] = {"nfsv4gracetime", &transaction_ops, S_IWUSR|S_IRUSR},
1364 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING
1365 		[NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR},
1366 #endif
1367 		[NFSD_V4EndGrace] = {"v4_end_grace", &transaction_ops, S_IWUSR|S_IRUGO},
1368 #endif
1369 		/* last one */ {""}
1370 	};
1371 
1372 	ret = simple_fill_super(sb, 0x6e667364, nfsd_files);
1373 	if (ret)
1374 		return ret;
1375 	_nfsd_symlink(sb->s_root, "supported_krb5_enctypes",
1376 		      "/proc/net/rpc/gss_krb5_enctypes");
1377 	dentry = nfsd_mkdir(sb->s_root, NULL, "clients");
1378 	if (IS_ERR(dentry))
1379 		return PTR_ERR(dentry);
1380 	nn->nfsd_client_dir = dentry;
1381 	return 0;
1382 }
1383 
1384 static int nfsd_fs_get_tree(struct fs_context *fc)
1385 {
1386 	return get_tree_keyed(fc, nfsd_fill_super, get_net(fc->net_ns));
1387 }
1388 
1389 static void nfsd_fs_free_fc(struct fs_context *fc)
1390 {
1391 	if (fc->s_fs_info)
1392 		put_net(fc->s_fs_info);
1393 }
1394 
1395 static const struct fs_context_operations nfsd_fs_context_ops = {
1396 	.free		= nfsd_fs_free_fc,
1397 	.get_tree	= nfsd_fs_get_tree,
1398 };
1399 
1400 static int nfsd_init_fs_context(struct fs_context *fc)
1401 {
1402 	put_user_ns(fc->user_ns);
1403 	fc->user_ns = get_user_ns(fc->net_ns->user_ns);
1404 	fc->ops = &nfsd_fs_context_ops;
1405 	return 0;
1406 }
1407 
1408 static void nfsd_umount(struct super_block *sb)
1409 {
1410 	struct net *net = sb->s_fs_info;
1411 
1412 	nfsd_shutdown_threads(net);
1413 
1414 	kill_litter_super(sb);
1415 	put_net(net);
1416 }
1417 
1418 static struct file_system_type nfsd_fs_type = {
1419 	.owner		= THIS_MODULE,
1420 	.name		= "nfsd",
1421 	.init_fs_context = nfsd_init_fs_context,
1422 	.kill_sb	= nfsd_umount,
1423 };
1424 MODULE_ALIAS_FS("nfsd");
1425 
1426 #ifdef CONFIG_PROC_FS
1427 
1428 static int exports_proc_open(struct inode *inode, struct file *file)
1429 {
1430 	return exports_net_open(current->nsproxy->net_ns, file);
1431 }
1432 
1433 static const struct proc_ops exports_proc_ops = {
1434 	.proc_open	= exports_proc_open,
1435 	.proc_read	= seq_read,
1436 	.proc_lseek	= seq_lseek,
1437 	.proc_release	= seq_release,
1438 };
1439 
1440 static int create_proc_exports_entry(void)
1441 {
1442 	struct proc_dir_entry *entry;
1443 
1444 	entry = proc_mkdir("fs/nfs", NULL);
1445 	if (!entry)
1446 		return -ENOMEM;
1447 	entry = proc_create("exports", 0, entry, &exports_proc_ops);
1448 	if (!entry) {
1449 		remove_proc_entry("fs/nfs", NULL);
1450 		return -ENOMEM;
1451 	}
1452 	return 0;
1453 }
1454 #else /* CONFIG_PROC_FS */
1455 static int create_proc_exports_entry(void)
1456 {
1457 	return 0;
1458 }
1459 #endif
1460 
1461 unsigned int nfsd_net_id;
1462 
1463 static int nfsd_genl_rpc_status_compose_msg(struct sk_buff *skb,
1464 					    struct netlink_callback *cb,
1465 					    struct nfsd_genl_rqstp *rqstp)
1466 {
1467 	void *hdr;
1468 	u32 i;
1469 
1470 	hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
1471 			  &nfsd_nl_family, 0, NFSD_CMD_RPC_STATUS_GET);
1472 	if (!hdr)
1473 		return -ENOBUFS;
1474 
1475 	if (nla_put_be32(skb, NFSD_A_RPC_STATUS_XID, rqstp->rq_xid) ||
1476 	    nla_put_u32(skb, NFSD_A_RPC_STATUS_FLAGS, rqstp->rq_flags) ||
1477 	    nla_put_u32(skb, NFSD_A_RPC_STATUS_PROG, rqstp->rq_prog) ||
1478 	    nla_put_u32(skb, NFSD_A_RPC_STATUS_PROC, rqstp->rq_proc) ||
1479 	    nla_put_u8(skb, NFSD_A_RPC_STATUS_VERSION, rqstp->rq_vers) ||
1480 	    nla_put_s64(skb, NFSD_A_RPC_STATUS_SERVICE_TIME,
1481 			ktime_to_us(rqstp->rq_stime),
1482 			NFSD_A_RPC_STATUS_PAD))
1483 		return -ENOBUFS;
1484 
1485 	switch (rqstp->rq_saddr.sa_family) {
1486 	case AF_INET: {
1487 		const struct sockaddr_in *s_in, *d_in;
1488 
1489 		s_in = (const struct sockaddr_in *)&rqstp->rq_saddr;
1490 		d_in = (const struct sockaddr_in *)&rqstp->rq_daddr;
1491 		if (nla_put_in_addr(skb, NFSD_A_RPC_STATUS_SADDR4,
1492 				    s_in->sin_addr.s_addr) ||
1493 		    nla_put_in_addr(skb, NFSD_A_RPC_STATUS_DADDR4,
1494 				    d_in->sin_addr.s_addr) ||
1495 		    nla_put_be16(skb, NFSD_A_RPC_STATUS_SPORT,
1496 				 s_in->sin_port) ||
1497 		    nla_put_be16(skb, NFSD_A_RPC_STATUS_DPORT,
1498 				 d_in->sin_port))
1499 			return -ENOBUFS;
1500 		break;
1501 	}
1502 	case AF_INET6: {
1503 		const struct sockaddr_in6 *s_in, *d_in;
1504 
1505 		s_in = (const struct sockaddr_in6 *)&rqstp->rq_saddr;
1506 		d_in = (const struct sockaddr_in6 *)&rqstp->rq_daddr;
1507 		if (nla_put_in6_addr(skb, NFSD_A_RPC_STATUS_SADDR6,
1508 				     &s_in->sin6_addr) ||
1509 		    nla_put_in6_addr(skb, NFSD_A_RPC_STATUS_DADDR6,
1510 				     &d_in->sin6_addr) ||
1511 		    nla_put_be16(skb, NFSD_A_RPC_STATUS_SPORT,
1512 				 s_in->sin6_port) ||
1513 		    nla_put_be16(skb, NFSD_A_RPC_STATUS_DPORT,
1514 				 d_in->sin6_port))
1515 			return -ENOBUFS;
1516 		break;
1517 	}
1518 	}
1519 
1520 	for (i = 0; i < rqstp->rq_opcnt; i++)
1521 		if (nla_put_u32(skb, NFSD_A_RPC_STATUS_COMPOUND_OPS,
1522 				rqstp->rq_opnum[i]))
1523 			return -ENOBUFS;
1524 
1525 	genlmsg_end(skb, hdr);
1526 	return 0;
1527 }
1528 
1529 /**
1530  * nfsd_nl_rpc_status_get_dumpit - Handle rpc_status_get dumpit
1531  * @skb: reply buffer
1532  * @cb: netlink metadata and command arguments
1533  *
1534  * Returns the size of the reply or a negative errno.
1535  */
1536 int nfsd_nl_rpc_status_get_dumpit(struct sk_buff *skb,
1537 				  struct netlink_callback *cb)
1538 {
1539 	int i, ret, rqstp_index = 0;
1540 	struct nfsd_net *nn;
1541 
1542 	mutex_lock(&nfsd_mutex);
1543 
1544 	nn = net_generic(sock_net(skb->sk), nfsd_net_id);
1545 	if (!nn->nfsd_serv) {
1546 		ret = -ENODEV;
1547 		goto out_unlock;
1548 	}
1549 
1550 	rcu_read_lock();
1551 
1552 	for (i = 0; i < nn->nfsd_serv->sv_nrpools; i++) {
1553 		struct svc_rqst *rqstp;
1554 
1555 		if (i < cb->args[0]) /* already consumed */
1556 			continue;
1557 
1558 		rqstp_index = 0;
1559 		list_for_each_entry_rcu(rqstp,
1560 				&nn->nfsd_serv->sv_pools[i].sp_all_threads,
1561 				rq_all) {
1562 			struct nfsd_genl_rqstp genl_rqstp;
1563 			unsigned int status_counter;
1564 
1565 			if (rqstp_index++ < cb->args[1]) /* already consumed */
1566 				continue;
1567 			/*
1568 			 * Acquire rq_status_counter before parsing the rqst
1569 			 * fields. rq_status_counter is set to an odd value in
1570 			 * order to notify the consumers the rqstp fields are
1571 			 * meaningful.
1572 			 */
1573 			status_counter =
1574 				smp_load_acquire(&rqstp->rq_status_counter);
1575 			if (!(status_counter & 1))
1576 				continue;
1577 
1578 			genl_rqstp.rq_xid = rqstp->rq_xid;
1579 			genl_rqstp.rq_flags = rqstp->rq_flags;
1580 			genl_rqstp.rq_vers = rqstp->rq_vers;
1581 			genl_rqstp.rq_prog = rqstp->rq_prog;
1582 			genl_rqstp.rq_proc = rqstp->rq_proc;
1583 			genl_rqstp.rq_stime = rqstp->rq_stime;
1584 			genl_rqstp.rq_opcnt = 0;
1585 			memcpy(&genl_rqstp.rq_daddr, svc_daddr(rqstp),
1586 			       sizeof(struct sockaddr));
1587 			memcpy(&genl_rqstp.rq_saddr, svc_addr(rqstp),
1588 			       sizeof(struct sockaddr));
1589 
1590 #ifdef CONFIG_NFSD_V4
1591 			if (rqstp->rq_vers == NFS4_VERSION &&
1592 			    rqstp->rq_proc == NFSPROC4_COMPOUND) {
1593 				/* NFSv4 compound */
1594 				struct nfsd4_compoundargs *args;
1595 				int j;
1596 
1597 				args = rqstp->rq_argp;
1598 				genl_rqstp.rq_opcnt = args->opcnt;
1599 				for (j = 0; j < genl_rqstp.rq_opcnt; j++)
1600 					genl_rqstp.rq_opnum[j] =
1601 						args->ops[j].opnum;
1602 			}
1603 #endif /* CONFIG_NFSD_V4 */
1604 
1605 			/*
1606 			 * Acquire rq_status_counter before reporting the rqst
1607 			 * fields to the user.
1608 			 */
1609 			if (smp_load_acquire(&rqstp->rq_status_counter) !=
1610 			    status_counter)
1611 				continue;
1612 
1613 			ret = nfsd_genl_rpc_status_compose_msg(skb, cb,
1614 							       &genl_rqstp);
1615 			if (ret)
1616 				goto out;
1617 		}
1618 	}
1619 
1620 	cb->args[0] = i;
1621 	cb->args[1] = rqstp_index;
1622 	ret = skb->len;
1623 out:
1624 	rcu_read_unlock();
1625 out_unlock:
1626 	mutex_unlock(&nfsd_mutex);
1627 
1628 	return ret;
1629 }
1630 
1631 /**
1632  * nfsd_nl_threads_set_doit - set the number of running threads
1633  * @skb: reply buffer
1634  * @info: netlink metadata and command arguments
1635  *
1636  * Return 0 on success or a negative errno.
1637  */
1638 int nfsd_nl_threads_set_doit(struct sk_buff *skb, struct genl_info *info)
1639 {
1640 	int nthreads = 0, count = 0, nrpools, ret = -EOPNOTSUPP, rem;
1641 	struct net *net = genl_info_net(info);
1642 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1643 	const struct nlattr *attr;
1644 	const char *scope = NULL;
1645 
1646 	if (GENL_REQ_ATTR_CHECK(info, NFSD_A_SERVER_THREADS))
1647 		return -EINVAL;
1648 
1649 	/* count number of SERVER_THREADS values */
1650 	nlmsg_for_each_attr(attr, info->nlhdr, GENL_HDRLEN, rem) {
1651 		if (nla_type(attr) == NFSD_A_SERVER_THREADS)
1652 			count++;
1653 	}
1654 
1655 	mutex_lock(&nfsd_mutex);
1656 
1657 	nrpools = nfsd_nrpools(net);
1658 	if (nrpools && count > nrpools)
1659 		count = nrpools;
1660 
1661 	/* XXX: make this handle non-global pool-modes */
1662 	if (count > 1)
1663 		goto out_unlock;
1664 
1665 	nthreads = nla_get_u32(info->attrs[NFSD_A_SERVER_THREADS]);
1666 	if (info->attrs[NFSD_A_SERVER_GRACETIME] ||
1667 	    info->attrs[NFSD_A_SERVER_LEASETIME] ||
1668 	    info->attrs[NFSD_A_SERVER_SCOPE]) {
1669 		ret = -EBUSY;
1670 		if (nn->nfsd_serv && nn->nfsd_serv->sv_nrthreads)
1671 			goto out_unlock;
1672 
1673 		ret = -EINVAL;
1674 		attr = info->attrs[NFSD_A_SERVER_GRACETIME];
1675 		if (attr) {
1676 			u32 gracetime = nla_get_u32(attr);
1677 
1678 			if (gracetime < 10 || gracetime > 3600)
1679 				goto out_unlock;
1680 
1681 			nn->nfsd4_grace = gracetime;
1682 		}
1683 
1684 		attr = info->attrs[NFSD_A_SERVER_LEASETIME];
1685 		if (attr) {
1686 			u32 leasetime = nla_get_u32(attr);
1687 
1688 			if (leasetime < 10 || leasetime > 3600)
1689 				goto out_unlock;
1690 
1691 			nn->nfsd4_lease = leasetime;
1692 		}
1693 
1694 		attr = info->attrs[NFSD_A_SERVER_SCOPE];
1695 		if (attr)
1696 			scope = nla_data(attr);
1697 	}
1698 
1699 	ret = nfsd_svc(nthreads, net, get_current_cred(), scope);
1700 
1701 out_unlock:
1702 	mutex_unlock(&nfsd_mutex);
1703 
1704 	return ret == nthreads ? 0 : ret;
1705 }
1706 
1707 /**
1708  * nfsd_nl_threads_get_doit - get the number of running threads
1709  * @skb: reply buffer
1710  * @info: netlink metadata and command arguments
1711  *
1712  * Return 0 on success or a negative errno.
1713  */
1714 int nfsd_nl_threads_get_doit(struct sk_buff *skb, struct genl_info *info)
1715 {
1716 	struct net *net = genl_info_net(info);
1717 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1718 	void *hdr;
1719 	int err;
1720 
1721 	skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
1722 	if (!skb)
1723 		return -ENOMEM;
1724 
1725 	hdr = genlmsg_iput(skb, info);
1726 	if (!hdr) {
1727 		err = -EMSGSIZE;
1728 		goto err_free_msg;
1729 	}
1730 
1731 	mutex_lock(&nfsd_mutex);
1732 
1733 	err = nla_put_u32(skb, NFSD_A_SERVER_GRACETIME,
1734 			  nn->nfsd4_grace) ||
1735 	      nla_put_u32(skb, NFSD_A_SERVER_LEASETIME,
1736 			  nn->nfsd4_lease) ||
1737 	      nla_put_string(skb, NFSD_A_SERVER_SCOPE,
1738 			  nn->nfsd_name);
1739 	if (err)
1740 		goto err_unlock;
1741 
1742 	if (nn->nfsd_serv) {
1743 		int i;
1744 
1745 		for (i = 0; i < nfsd_nrpools(net); ++i) {
1746 			struct svc_pool *sp = &nn->nfsd_serv->sv_pools[i];
1747 
1748 			err = nla_put_u32(skb, NFSD_A_SERVER_THREADS,
1749 					  atomic_read(&sp->sp_nrthreads));
1750 			if (err)
1751 				goto err_unlock;
1752 		}
1753 	} else {
1754 		err = nla_put_u32(skb, NFSD_A_SERVER_THREADS, 0);
1755 		if (err)
1756 			goto err_unlock;
1757 	}
1758 
1759 	mutex_unlock(&nfsd_mutex);
1760 
1761 	genlmsg_end(skb, hdr);
1762 
1763 	return genlmsg_reply(skb, info);
1764 
1765 err_unlock:
1766 	mutex_unlock(&nfsd_mutex);
1767 err_free_msg:
1768 	nlmsg_free(skb);
1769 
1770 	return err;
1771 }
1772 
1773 /**
1774  * nfsd_nl_version_set_doit - set the nfs enabled versions
1775  * @skb: reply buffer
1776  * @info: netlink metadata and command arguments
1777  *
1778  * Return 0 on success or a negative errno.
1779  */
1780 int nfsd_nl_version_set_doit(struct sk_buff *skb, struct genl_info *info)
1781 {
1782 	const struct nlattr *attr;
1783 	struct nfsd_net *nn;
1784 	int i, rem;
1785 
1786 	if (GENL_REQ_ATTR_CHECK(info, NFSD_A_SERVER_PROTO_VERSION))
1787 		return -EINVAL;
1788 
1789 	mutex_lock(&nfsd_mutex);
1790 
1791 	nn = net_generic(genl_info_net(info), nfsd_net_id);
1792 	if (nn->nfsd_serv) {
1793 		mutex_unlock(&nfsd_mutex);
1794 		return -EBUSY;
1795 	}
1796 
1797 	/* clear current supported versions. */
1798 	nfsd_vers(nn, 2, NFSD_CLEAR);
1799 	nfsd_vers(nn, 3, NFSD_CLEAR);
1800 	for (i = 0; i <= NFSD_SUPPORTED_MINOR_VERSION; i++)
1801 		nfsd_minorversion(nn, i, NFSD_CLEAR);
1802 
1803 	nlmsg_for_each_attr(attr, info->nlhdr, GENL_HDRLEN, rem) {
1804 		struct nlattr *tb[NFSD_A_VERSION_MAX + 1];
1805 		u32 major, minor = 0;
1806 		bool enabled;
1807 
1808 		if (nla_type(attr) != NFSD_A_SERVER_PROTO_VERSION)
1809 			continue;
1810 
1811 		if (nla_parse_nested(tb, NFSD_A_VERSION_MAX, attr,
1812 				     nfsd_version_nl_policy, info->extack) < 0)
1813 			continue;
1814 
1815 		if (!tb[NFSD_A_VERSION_MAJOR])
1816 			continue;
1817 
1818 		major = nla_get_u32(tb[NFSD_A_VERSION_MAJOR]);
1819 		if (tb[NFSD_A_VERSION_MINOR])
1820 			minor = nla_get_u32(tb[NFSD_A_VERSION_MINOR]);
1821 
1822 		enabled = nla_get_flag(tb[NFSD_A_VERSION_ENABLED]);
1823 
1824 		switch (major) {
1825 		case 4:
1826 			nfsd_minorversion(nn, minor, enabled ? NFSD_SET : NFSD_CLEAR);
1827 			break;
1828 		case 3:
1829 		case 2:
1830 			if (!minor)
1831 				nfsd_vers(nn, major, enabled ? NFSD_SET : NFSD_CLEAR);
1832 			break;
1833 		default:
1834 			break;
1835 		}
1836 	}
1837 
1838 	mutex_unlock(&nfsd_mutex);
1839 
1840 	return 0;
1841 }
1842 
1843 /**
1844  * nfsd_nl_version_get_doit - get the enabled status for all supported nfs versions
1845  * @skb: reply buffer
1846  * @info: netlink metadata and command arguments
1847  *
1848  * Return 0 on success or a negative errno.
1849  */
1850 int nfsd_nl_version_get_doit(struct sk_buff *skb, struct genl_info *info)
1851 {
1852 	struct nfsd_net *nn;
1853 	int i, err;
1854 	void *hdr;
1855 
1856 	skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
1857 	if (!skb)
1858 		return -ENOMEM;
1859 
1860 	hdr = genlmsg_iput(skb, info);
1861 	if (!hdr) {
1862 		err = -EMSGSIZE;
1863 		goto err_free_msg;
1864 	}
1865 
1866 	mutex_lock(&nfsd_mutex);
1867 	nn = net_generic(genl_info_net(info), nfsd_net_id);
1868 
1869 	for (i = 2; i <= 4; i++) {
1870 		int j;
1871 
1872 		for (j = 0; j <= NFSD_SUPPORTED_MINOR_VERSION; j++) {
1873 			struct nlattr *attr;
1874 
1875 			/* Don't record any versions the kernel doesn't have
1876 			 * compiled in
1877 			 */
1878 			if (!nfsd_support_version(i))
1879 				continue;
1880 
1881 			/* NFSv{2,3} does not support minor numbers */
1882 			if (i < 4 && j)
1883 				continue;
1884 
1885 			attr = nla_nest_start(skb,
1886 					      NFSD_A_SERVER_PROTO_VERSION);
1887 			if (!attr) {
1888 				err = -EINVAL;
1889 				goto err_nfsd_unlock;
1890 			}
1891 
1892 			if (nla_put_u32(skb, NFSD_A_VERSION_MAJOR, i) ||
1893 			    nla_put_u32(skb, NFSD_A_VERSION_MINOR, j)) {
1894 				err = -EINVAL;
1895 				goto err_nfsd_unlock;
1896 			}
1897 
1898 			/* Set the enabled flag if the version is enabled */
1899 			if (nfsd_vers(nn, i, NFSD_TEST) &&
1900 			    (i < 4 || nfsd_minorversion(nn, j, NFSD_TEST)) &&
1901 			    nla_put_flag(skb, NFSD_A_VERSION_ENABLED)) {
1902 				err = -EINVAL;
1903 				goto err_nfsd_unlock;
1904 			}
1905 
1906 			nla_nest_end(skb, attr);
1907 		}
1908 	}
1909 
1910 	mutex_unlock(&nfsd_mutex);
1911 	genlmsg_end(skb, hdr);
1912 
1913 	return genlmsg_reply(skb, info);
1914 
1915 err_nfsd_unlock:
1916 	mutex_unlock(&nfsd_mutex);
1917 err_free_msg:
1918 	nlmsg_free(skb);
1919 
1920 	return err;
1921 }
1922 
1923 /**
1924  * nfsd_nl_listener_set_doit - set the nfs running sockets
1925  * @skb: reply buffer
1926  * @info: netlink metadata and command arguments
1927  *
1928  * Return 0 on success or a negative errno.
1929  */
1930 int nfsd_nl_listener_set_doit(struct sk_buff *skb, struct genl_info *info)
1931 {
1932 	struct net *net = genl_info_net(info);
1933 	struct svc_xprt *xprt, *tmp;
1934 	const struct nlattr *attr;
1935 	struct svc_serv *serv;
1936 	LIST_HEAD(permsocks);
1937 	struct nfsd_net *nn;
1938 	int err, rem;
1939 
1940 	mutex_lock(&nfsd_mutex);
1941 
1942 	err = nfsd_create_serv(net);
1943 	if (err) {
1944 		mutex_unlock(&nfsd_mutex);
1945 		return err;
1946 	}
1947 
1948 	nn = net_generic(net, nfsd_net_id);
1949 	serv = nn->nfsd_serv;
1950 
1951 	spin_lock_bh(&serv->sv_lock);
1952 
1953 	/* Move all of the old listener sockets to a temp list */
1954 	list_splice_init(&serv->sv_permsocks, &permsocks);
1955 
1956 	/*
1957 	 * Walk the list of server_socks from userland and move any that match
1958 	 * back to sv_permsocks
1959 	 */
1960 	nlmsg_for_each_attr(attr, info->nlhdr, GENL_HDRLEN, rem) {
1961 		struct nlattr *tb[NFSD_A_SOCK_MAX + 1];
1962 		const char *xcl_name;
1963 		struct sockaddr *sa;
1964 
1965 		if (nla_type(attr) != NFSD_A_SERVER_SOCK_ADDR)
1966 			continue;
1967 
1968 		if (nla_parse_nested(tb, NFSD_A_SOCK_MAX, attr,
1969 				     nfsd_sock_nl_policy, info->extack) < 0)
1970 			continue;
1971 
1972 		if (!tb[NFSD_A_SOCK_ADDR] || !tb[NFSD_A_SOCK_TRANSPORT_NAME])
1973 			continue;
1974 
1975 		if (nla_len(tb[NFSD_A_SOCK_ADDR]) < sizeof(*sa))
1976 			continue;
1977 
1978 		xcl_name = nla_data(tb[NFSD_A_SOCK_TRANSPORT_NAME]);
1979 		sa = nla_data(tb[NFSD_A_SOCK_ADDR]);
1980 
1981 		/* Put back any matching sockets */
1982 		list_for_each_entry_safe(xprt, tmp, &permsocks, xpt_list) {
1983 			/* This shouldn't be possible */
1984 			if (WARN_ON_ONCE(xprt->xpt_net != net)) {
1985 				list_move(&xprt->xpt_list, &serv->sv_permsocks);
1986 				continue;
1987 			}
1988 
1989 			/* If everything matches, put it back */
1990 			if (!strcmp(xprt->xpt_class->xcl_name, xcl_name) &&
1991 			    rpc_cmp_addr_port(sa, (struct sockaddr *)&xprt->xpt_local)) {
1992 				list_move(&xprt->xpt_list, &serv->sv_permsocks);
1993 				break;
1994 			}
1995 		}
1996 	}
1997 
1998 	/* For now, no removing old sockets while server is running */
1999 	if (serv->sv_nrthreads && !list_empty(&permsocks)) {
2000 		list_splice_init(&permsocks, &serv->sv_permsocks);
2001 		spin_unlock_bh(&serv->sv_lock);
2002 		err = -EBUSY;
2003 		goto out_unlock_mtx;
2004 	}
2005 
2006 	/* Close the remaining sockets on the permsocks list */
2007 	while (!list_empty(&permsocks)) {
2008 		xprt = list_first_entry(&permsocks, struct svc_xprt, xpt_list);
2009 		list_move(&xprt->xpt_list, &serv->sv_permsocks);
2010 
2011 		/*
2012 		 * Newly-created sockets are born with the BUSY bit set. Clear
2013 		 * it if there are no threads, since nothing can pick it up
2014 		 * in that case.
2015 		 */
2016 		if (!serv->sv_nrthreads)
2017 			clear_bit(XPT_BUSY, &xprt->xpt_flags);
2018 
2019 		set_bit(XPT_CLOSE, &xprt->xpt_flags);
2020 		spin_unlock_bh(&serv->sv_lock);
2021 		svc_xprt_close(xprt);
2022 		spin_lock_bh(&serv->sv_lock);
2023 	}
2024 
2025 	spin_unlock_bh(&serv->sv_lock);
2026 
2027 	/* walk list of addrs again, open any that still don't exist */
2028 	nlmsg_for_each_attr(attr, info->nlhdr, GENL_HDRLEN, rem) {
2029 		struct nlattr *tb[NFSD_A_SOCK_MAX + 1];
2030 		const char *xcl_name;
2031 		struct sockaddr *sa;
2032 		int ret;
2033 
2034 		if (nla_type(attr) != NFSD_A_SERVER_SOCK_ADDR)
2035 			continue;
2036 
2037 		if (nla_parse_nested(tb, NFSD_A_SOCK_MAX, attr,
2038 				     nfsd_sock_nl_policy, info->extack) < 0)
2039 			continue;
2040 
2041 		if (!tb[NFSD_A_SOCK_ADDR] || !tb[NFSD_A_SOCK_TRANSPORT_NAME])
2042 			continue;
2043 
2044 		if (nla_len(tb[NFSD_A_SOCK_ADDR]) < sizeof(*sa))
2045 			continue;
2046 
2047 		xcl_name = nla_data(tb[NFSD_A_SOCK_TRANSPORT_NAME]);
2048 		sa = nla_data(tb[NFSD_A_SOCK_ADDR]);
2049 
2050 		xprt = svc_find_listener(serv, xcl_name, net, sa);
2051 		if (xprt) {
2052 			svc_xprt_put(xprt);
2053 			continue;
2054 		}
2055 
2056 		ret = svc_xprt_create_from_sa(serv, xcl_name, net, sa,
2057 					      SVC_SOCK_ANONYMOUS,
2058 					      get_current_cred());
2059 		/* always save the latest error */
2060 		if (ret < 0)
2061 			err = ret;
2062 	}
2063 
2064 	if (!serv->sv_nrthreads && list_empty(&nn->nfsd_serv->sv_permsocks))
2065 		nfsd_destroy_serv(net);
2066 
2067 out_unlock_mtx:
2068 	mutex_unlock(&nfsd_mutex);
2069 
2070 	return err;
2071 }
2072 
2073 /**
2074  * nfsd_nl_listener_get_doit - get the nfs running listeners
2075  * @skb: reply buffer
2076  * @info: netlink metadata and command arguments
2077  *
2078  * Return 0 on success or a negative errno.
2079  */
2080 int nfsd_nl_listener_get_doit(struct sk_buff *skb, struct genl_info *info)
2081 {
2082 	struct svc_xprt *xprt;
2083 	struct svc_serv *serv;
2084 	struct nfsd_net *nn;
2085 	void *hdr;
2086 	int err;
2087 
2088 	skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2089 	if (!skb)
2090 		return -ENOMEM;
2091 
2092 	hdr = genlmsg_iput(skb, info);
2093 	if (!hdr) {
2094 		err = -EMSGSIZE;
2095 		goto err_free_msg;
2096 	}
2097 
2098 	mutex_lock(&nfsd_mutex);
2099 	nn = net_generic(genl_info_net(info), nfsd_net_id);
2100 
2101 	/* no nfs server? Just send empty socket list */
2102 	if (!nn->nfsd_serv)
2103 		goto out_unlock_mtx;
2104 
2105 	serv = nn->nfsd_serv;
2106 	spin_lock_bh(&serv->sv_lock);
2107 	list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list) {
2108 		struct nlattr *attr;
2109 
2110 		attr = nla_nest_start(skb, NFSD_A_SERVER_SOCK_ADDR);
2111 		if (!attr) {
2112 			err = -EINVAL;
2113 			goto err_serv_unlock;
2114 		}
2115 
2116 		if (nla_put_string(skb, NFSD_A_SOCK_TRANSPORT_NAME,
2117 				   xprt->xpt_class->xcl_name) ||
2118 		    nla_put(skb, NFSD_A_SOCK_ADDR,
2119 			    sizeof(struct sockaddr_storage),
2120 			    &xprt->xpt_local)) {
2121 			err = -EINVAL;
2122 			goto err_serv_unlock;
2123 		}
2124 
2125 		nla_nest_end(skb, attr);
2126 	}
2127 	spin_unlock_bh(&serv->sv_lock);
2128 out_unlock_mtx:
2129 	mutex_unlock(&nfsd_mutex);
2130 	genlmsg_end(skb, hdr);
2131 
2132 	return genlmsg_reply(skb, info);
2133 
2134 err_serv_unlock:
2135 	spin_unlock_bh(&serv->sv_lock);
2136 	mutex_unlock(&nfsd_mutex);
2137 err_free_msg:
2138 	nlmsg_free(skb);
2139 
2140 	return err;
2141 }
2142 
2143 /**
2144  * nfsd_net_init - Prepare the nfsd_net portion of a new net namespace
2145  * @net: a freshly-created network namespace
2146  *
2147  * This information stays around as long as the network namespace is
2148  * alive whether or not there is an NFSD instance running in the
2149  * namespace.
2150  *
2151  * Returns zero on success, or a negative errno otherwise.
2152  */
2153 static __net_init int nfsd_net_init(struct net *net)
2154 {
2155 	int retval;
2156 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2157 
2158 	retval = nfsd_export_init(net);
2159 	if (retval)
2160 		goto out_export_error;
2161 	retval = nfsd_idmap_init(net);
2162 	if (retval)
2163 		goto out_idmap_error;
2164 	retval = percpu_counter_init_many(nn->counter, 0, GFP_KERNEL,
2165 					  NFSD_STATS_COUNTERS_NUM);
2166 	if (retval)
2167 		goto out_repcache_error;
2168 	memset(&nn->nfsd_svcstats, 0, sizeof(nn->nfsd_svcstats));
2169 	nn->nfsd_svcstats.program = &nfsd_program;
2170 	nn->nfsd_versions = NULL;
2171 	nn->nfsd4_minorversions = NULL;
2172 	nn->nfsd_info.mutex = &nfsd_mutex;
2173 	nn->nfsd_serv = NULL;
2174 	nfsd4_init_leases_net(nn);
2175 	get_random_bytes(&nn->siphash_key, sizeof(nn->siphash_key));
2176 	seqlock_init(&nn->writeverf_lock);
2177 	nfsd_proc_stat_init(net);
2178 
2179 	return 0;
2180 
2181 out_repcache_error:
2182 	nfsd_idmap_shutdown(net);
2183 out_idmap_error:
2184 	nfsd_export_shutdown(net);
2185 out_export_error:
2186 	return retval;
2187 }
2188 
2189 /**
2190  * nfsd_net_exit - Release the nfsd_net portion of a net namespace
2191  * @net: a network namespace that is about to be destroyed
2192  *
2193  */
2194 static __net_exit void nfsd_net_exit(struct net *net)
2195 {
2196 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2197 
2198 	nfsd_proc_stat_shutdown(net);
2199 	percpu_counter_destroy_many(nn->counter, NFSD_STATS_COUNTERS_NUM);
2200 	nfsd_idmap_shutdown(net);
2201 	nfsd_export_shutdown(net);
2202 	nfsd_netns_free_versions(nn);
2203 }
2204 
2205 static struct pernet_operations nfsd_net_ops = {
2206 	.init = nfsd_net_init,
2207 	.exit = nfsd_net_exit,
2208 	.id   = &nfsd_net_id,
2209 	.size = sizeof(struct nfsd_net),
2210 };
2211 
2212 static int __init init_nfsd(void)
2213 {
2214 	int retval;
2215 
2216 	retval = nfsd4_init_slabs();
2217 	if (retval)
2218 		return retval;
2219 	retval = nfsd4_init_pnfs();
2220 	if (retval)
2221 		goto out_free_slabs;
2222 	retval = nfsd_drc_slab_create();
2223 	if (retval)
2224 		goto out_free_pnfs;
2225 	nfsd_lockd_init();	/* lockd->nfsd callbacks */
2226 	retval = create_proc_exports_entry();
2227 	if (retval)
2228 		goto out_free_lockd;
2229 	retval = register_pernet_subsys(&nfsd_net_ops);
2230 	if (retval < 0)
2231 		goto out_free_exports;
2232 	retval = register_cld_notifier();
2233 	if (retval)
2234 		goto out_free_subsys;
2235 	retval = nfsd4_create_laundry_wq();
2236 	if (retval)
2237 		goto out_free_cld;
2238 	retval = register_filesystem(&nfsd_fs_type);
2239 	if (retval)
2240 		goto out_free_all;
2241 	retval = genl_register_family(&nfsd_nl_family);
2242 	if (retval)
2243 		goto out_free_all;
2244 
2245 	return 0;
2246 out_free_all:
2247 	nfsd4_destroy_laundry_wq();
2248 out_free_cld:
2249 	unregister_cld_notifier();
2250 out_free_subsys:
2251 	unregister_pernet_subsys(&nfsd_net_ops);
2252 out_free_exports:
2253 	remove_proc_entry("fs/nfs/exports", NULL);
2254 	remove_proc_entry("fs/nfs", NULL);
2255 out_free_lockd:
2256 	nfsd_lockd_shutdown();
2257 	nfsd_drc_slab_free();
2258 out_free_pnfs:
2259 	nfsd4_exit_pnfs();
2260 out_free_slabs:
2261 	nfsd4_free_slabs();
2262 	return retval;
2263 }
2264 
2265 static void __exit exit_nfsd(void)
2266 {
2267 	genl_unregister_family(&nfsd_nl_family);
2268 	unregister_filesystem(&nfsd_fs_type);
2269 	nfsd4_destroy_laundry_wq();
2270 	unregister_cld_notifier();
2271 	unregister_pernet_subsys(&nfsd_net_ops);
2272 	nfsd_drc_slab_free();
2273 	remove_proc_entry("fs/nfs/exports", NULL);
2274 	remove_proc_entry("fs/nfs", NULL);
2275 	nfsd_lockd_shutdown();
2276 	nfsd4_free_slabs();
2277 	nfsd4_exit_pnfs();
2278 }
2279 
2280 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
2281 MODULE_DESCRIPTION("In-kernel NFS server");
2282 MODULE_LICENSE("GPL");
2283 module_init(init_nfsd)
2284 module_exit(exit_nfsd)
2285