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