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