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