xref: /linux/fs/nfsd/nfsctl.c (revision a5210135489ae7bc1ef1cb4a8157361dd7b468cd)
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/bind.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_fh_key_set - helper to copy fh_key from userspace
1586  * @attr: nlattr NFSD_A_SERVER_FH_KEY
1587  * @nn: nfsd_net
1588  *
1589  * Callers should hold nfsd_mutex, returns 0 on success or negative errno.
1590  * Callers must ensure the server is shut down (sv_nrthreads == 0),
1591  * userspace documentation asserts the key may only be set when the server
1592  * is not running.
1593  */
nfsd_nl_fh_key_set(const struct nlattr * attr,struct nfsd_net * nn)1594 static int nfsd_nl_fh_key_set(const struct nlattr *attr, struct nfsd_net *nn)
1595 {
1596 	siphash_key_t *fh_key = nn->fh_key;
1597 
1598 	if (!fh_key) {
1599 		fh_key = kmalloc(sizeof(siphash_key_t), GFP_KERNEL);
1600 		if (!fh_key)
1601 			return -ENOMEM;
1602 		nn->fh_key = fh_key;
1603 	}
1604 
1605 	fh_key->key[0] = get_unaligned_le64(nla_data(attr));
1606 	fh_key->key[1] = get_unaligned_le64(nla_data(attr) + 8);
1607 	return 0;
1608 }
1609 
1610 /**
1611  * nfsd_nl_threads_set_doit - set the number of running threads
1612  * @skb: reply buffer
1613  * @info: netlink metadata and command arguments
1614  *
1615  * Return 0 on success or a negative errno.
1616  */
nfsd_nl_threads_set_doit(struct sk_buff * skb,struct genl_info * info)1617 int nfsd_nl_threads_set_doit(struct sk_buff *skb, struct genl_info *info)
1618 {
1619 	int *nthreads, nrpools = 0, i, ret = -EOPNOTSUPP, rem;
1620 	struct net *net = genl_info_net(info);
1621 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1622 	const struct nlattr *attr;
1623 	const char *scope = NULL;
1624 
1625 	if (GENL_REQ_ATTR_CHECK(info, NFSD_A_SERVER_THREADS))
1626 		return -EINVAL;
1627 
1628 	/* count number of SERVER_THREADS values */
1629 	nlmsg_for_each_attr_type(attr, NFSD_A_SERVER_THREADS, info->nlhdr,
1630 				 GENL_HDRLEN, rem)
1631 		nrpools++;
1632 
1633 	mutex_lock(&nfsd_mutex);
1634 
1635 	nthreads = kzalloc_objs(int, nrpools);
1636 	if (!nthreads) {
1637 		ret = -ENOMEM;
1638 		goto out_unlock;
1639 	}
1640 
1641 	i = 0;
1642 	nlmsg_for_each_attr_type(attr, NFSD_A_SERVER_THREADS, info->nlhdr,
1643 				 GENL_HDRLEN, rem) {
1644 		nthreads[i++] = nla_get_u32(attr);
1645 		if (i >= nrpools)
1646 			break;
1647 	}
1648 
1649 	if (info->attrs[NFSD_A_SERVER_GRACETIME] ||
1650 	    info->attrs[NFSD_A_SERVER_LEASETIME] ||
1651 	    info->attrs[NFSD_A_SERVER_SCOPE] ||
1652 	    info->attrs[NFSD_A_SERVER_FH_KEY]) {
1653 		ret = -EBUSY;
1654 		if (nn->nfsd_serv && nn->nfsd_serv->sv_nrthreads)
1655 			goto out_unlock;
1656 
1657 		ret = -EINVAL;
1658 		attr = info->attrs[NFSD_A_SERVER_GRACETIME];
1659 		if (attr) {
1660 			u32 gracetime = nla_get_u32(attr);
1661 
1662 			if (gracetime < 10 || gracetime > 3600)
1663 				goto out_unlock;
1664 
1665 			nn->nfsd4_grace = gracetime;
1666 		}
1667 
1668 		attr = info->attrs[NFSD_A_SERVER_LEASETIME];
1669 		if (attr) {
1670 			u32 leasetime = nla_get_u32(attr);
1671 
1672 			if (leasetime < 10 || leasetime > 3600)
1673 				goto out_unlock;
1674 
1675 			nn->nfsd4_lease = leasetime;
1676 		}
1677 
1678 		attr = info->attrs[NFSD_A_SERVER_SCOPE];
1679 		if (attr)
1680 			scope = nla_data(attr);
1681 
1682 		attr = info->attrs[NFSD_A_SERVER_FH_KEY];
1683 		if (attr) {
1684 			ret = nfsd_nl_fh_key_set(attr, nn);
1685 			trace_nfsd_ctl_fh_key_set((const char *)nn->fh_key, ret);
1686 			if (ret)
1687 				goto out_unlock;
1688 		}
1689 	}
1690 
1691 	attr = info->attrs[NFSD_A_SERVER_MIN_THREADS];
1692 	if (attr)
1693 		nn->min_threads = nla_get_u32(attr);
1694 
1695 	ret = nfsd_svc(nrpools, nthreads, net, current_cred(), scope);
1696 	if (ret > 0)
1697 		ret = 0;
1698 out_unlock:
1699 	mutex_unlock(&nfsd_mutex);
1700 	kfree(nthreads);
1701 	return ret;
1702 }
1703 
1704 /**
1705  * nfsd_nl_threads_get_doit - get the maximum number of running threads
1706  * @skb: reply buffer
1707  * @info: netlink metadata and command arguments
1708  *
1709  * Return 0 on success or a negative errno.
1710  */
nfsd_nl_threads_get_doit(struct sk_buff * skb,struct genl_info * info)1711 int nfsd_nl_threads_get_doit(struct sk_buff *skb, struct genl_info *info)
1712 {
1713 	struct net *net = genl_info_net(info);
1714 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
1715 	void *hdr;
1716 	int err;
1717 
1718 	skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
1719 	if (!skb)
1720 		return -ENOMEM;
1721 
1722 	hdr = genlmsg_iput(skb, info);
1723 	if (!hdr) {
1724 		err = -EMSGSIZE;
1725 		goto err_free_msg;
1726 	}
1727 
1728 	mutex_lock(&nfsd_mutex);
1729 
1730 	err = nla_put_u32(skb, NFSD_A_SERVER_GRACETIME,
1731 			  nn->nfsd4_grace) ||
1732 	      nla_put_u32(skb, NFSD_A_SERVER_LEASETIME,
1733 			  nn->nfsd4_lease) ||
1734 	      nla_put_u32(skb, NFSD_A_SERVER_MIN_THREADS,
1735 			  nn->min_threads) ||
1736 	      nla_put_string(skb, NFSD_A_SERVER_SCOPE,
1737 			  nn->nfsd_name);
1738 	if (err)
1739 		goto err_unlock;
1740 
1741 	if (nn->nfsd_serv) {
1742 		int i;
1743 
1744 		for (i = 0; i < nfsd_nrpools(net); ++i) {
1745 			struct svc_pool *sp = &nn->nfsd_serv->sv_pools[i];
1746 
1747 			err = nla_put_u32(skb, NFSD_A_SERVER_THREADS,
1748 					  sp->sp_nrthrmax);
1749 			if (err)
1750 				goto err_unlock;
1751 		}
1752 	} else {
1753 		err = nla_put_u32(skb, NFSD_A_SERVER_THREADS, 0);
1754 		if (err)
1755 			goto err_unlock;
1756 	}
1757 
1758 	mutex_unlock(&nfsd_mutex);
1759 
1760 	genlmsg_end(skb, hdr);
1761 
1762 	return genlmsg_reply(skb, info);
1763 
1764 err_unlock:
1765 	mutex_unlock(&nfsd_mutex);
1766 err_free_msg:
1767 	nlmsg_free(skb);
1768 
1769 	return err;
1770 }
1771 
1772 /**
1773  * nfsd_nl_version_set_doit - set the nfs enabled versions
1774  * @skb: reply buffer
1775  * @info: netlink metadata and command arguments
1776  *
1777  * Return 0 on success or a negative errno.
1778  */
nfsd_nl_version_set_doit(struct sk_buff * skb,struct genl_info * info)1779 int nfsd_nl_version_set_doit(struct sk_buff *skb, struct genl_info *info)
1780 {
1781 	const struct nlattr *attr;
1782 	struct nfsd_net *nn;
1783 	int i, rem;
1784 
1785 	if (GENL_REQ_ATTR_CHECK(info, NFSD_A_SERVER_PROTO_VERSION))
1786 		return -EINVAL;
1787 
1788 	mutex_lock(&nfsd_mutex);
1789 
1790 	nn = net_generic(genl_info_net(info), nfsd_net_id);
1791 	if (nn->nfsd_serv) {
1792 		mutex_unlock(&nfsd_mutex);
1793 		return -EBUSY;
1794 	}
1795 
1796 	/* clear current supported versions. */
1797 	nfsd_vers(nn, 2, NFSD_CLEAR);
1798 	nfsd_vers(nn, 3, NFSD_CLEAR);
1799 	for (i = 0; i <= NFSD_SUPPORTED_MINOR_VERSION; i++)
1800 		nfsd_minorversion(nn, i, NFSD_CLEAR);
1801 
1802 	nlmsg_for_each_attr_type(attr, NFSD_A_SERVER_PROTO_VERSION, info->nlhdr,
1803 				 GENL_HDRLEN, rem) {
1804 		struct nlattr *tb[NFSD_A_VERSION_MAX + 1];
1805 		u32 major, minor = 0;
1806 		bool enabled;
1807 
1808 		if (nla_parse_nested(tb, NFSD_A_VERSION_MAX, attr,
1809 				     nfsd_version_nl_policy, info->extack) < 0)
1810 			continue;
1811 
1812 		if (!tb[NFSD_A_VERSION_MAJOR])
1813 			continue;
1814 
1815 		major = nla_get_u32(tb[NFSD_A_VERSION_MAJOR]);
1816 		if (tb[NFSD_A_VERSION_MINOR])
1817 			minor = nla_get_u32(tb[NFSD_A_VERSION_MINOR]);
1818 
1819 		enabled = nla_get_flag(tb[NFSD_A_VERSION_ENABLED]);
1820 
1821 		switch (major) {
1822 		case 4:
1823 			nfsd_minorversion(nn, minor, enabled ? NFSD_SET : NFSD_CLEAR);
1824 			break;
1825 		case 3:
1826 		case 2:
1827 			if (!minor)
1828 				nfsd_vers(nn, major, enabled ? NFSD_SET : NFSD_CLEAR);
1829 			break;
1830 		default:
1831 			break;
1832 		}
1833 	}
1834 
1835 	mutex_unlock(&nfsd_mutex);
1836 
1837 	return 0;
1838 }
1839 
1840 /**
1841  * nfsd_nl_version_get_doit - get the enabled status for all supported nfs versions
1842  * @skb: reply buffer
1843  * @info: netlink metadata and command arguments
1844  *
1845  * Return 0 on success or a negative errno.
1846  */
nfsd_nl_version_get_doit(struct sk_buff * skb,struct genl_info * info)1847 int nfsd_nl_version_get_doit(struct sk_buff *skb, struct genl_info *info)
1848 {
1849 	struct nfsd_net *nn;
1850 	int i, err;
1851 	void *hdr;
1852 
1853 	skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
1854 	if (!skb)
1855 		return -ENOMEM;
1856 
1857 	hdr = genlmsg_iput(skb, info);
1858 	if (!hdr) {
1859 		err = -EMSGSIZE;
1860 		goto err_free_msg;
1861 	}
1862 
1863 	mutex_lock(&nfsd_mutex);
1864 	nn = net_generic(genl_info_net(info), nfsd_net_id);
1865 
1866 	for (i = 2; i <= 4; i++) {
1867 		int j;
1868 
1869 		for (j = 0; j <= NFSD_SUPPORTED_MINOR_VERSION; j++) {
1870 			struct nlattr *attr;
1871 
1872 			/* Don't record any versions the kernel doesn't have
1873 			 * compiled in
1874 			 */
1875 			if (!nfsd_support_version(i))
1876 				continue;
1877 
1878 			/* NFSv{2,3} does not support minor numbers */
1879 			if (i < 4 && j)
1880 				continue;
1881 
1882 			attr = nla_nest_start(skb,
1883 					      NFSD_A_SERVER_PROTO_VERSION);
1884 			if (!attr) {
1885 				err = -EINVAL;
1886 				goto err_nfsd_unlock;
1887 			}
1888 
1889 			if (nla_put_u32(skb, NFSD_A_VERSION_MAJOR, i) ||
1890 			    nla_put_u32(skb, NFSD_A_VERSION_MINOR, j)) {
1891 				err = -EINVAL;
1892 				goto err_nfsd_unlock;
1893 			}
1894 
1895 			/* Set the enabled flag if the version is enabled */
1896 			if (nfsd_vers(nn, i, NFSD_TEST) &&
1897 			    (i < 4 || nfsd_minorversion(nn, j, NFSD_TEST)) &&
1898 			    nla_put_flag(skb, NFSD_A_VERSION_ENABLED)) {
1899 				err = -EINVAL;
1900 				goto err_nfsd_unlock;
1901 			}
1902 
1903 			nla_nest_end(skb, attr);
1904 		}
1905 	}
1906 
1907 	mutex_unlock(&nfsd_mutex);
1908 	genlmsg_end(skb, hdr);
1909 
1910 	return genlmsg_reply(skb, info);
1911 
1912 err_nfsd_unlock:
1913 	mutex_unlock(&nfsd_mutex);
1914 err_free_msg:
1915 	nlmsg_free(skb);
1916 
1917 	return err;
1918 }
1919 
1920 /**
1921  * nfsd_nl_listener_set_doit - set the nfs running sockets
1922  * @skb: reply buffer
1923  * @info: netlink metadata and command arguments
1924  *
1925  * Return 0 on success or a negative errno.
1926  */
nfsd_nl_listener_set_doit(struct sk_buff * skb,struct genl_info * info)1927 int nfsd_nl_listener_set_doit(struct sk_buff *skb, struct genl_info *info)
1928 {
1929 	struct net *net = genl_info_net(info);
1930 	struct svc_xprt *xprt, *tmp;
1931 	const struct nlattr *attr;
1932 	struct svc_serv *serv;
1933 	LIST_HEAD(permsocks);
1934 	struct nfsd_net *nn;
1935 	bool delete = false;
1936 	int err, rem;
1937 
1938 	mutex_lock(&nfsd_mutex);
1939 
1940 	err = nfsd_create_serv(net);
1941 	if (err) {
1942 		mutex_unlock(&nfsd_mutex);
1943 		return err;
1944 	}
1945 
1946 	nn = net_generic(net, nfsd_net_id);
1947 	serv = nn->nfsd_serv;
1948 
1949 	spin_lock_bh(&serv->sv_lock);
1950 
1951 	/* Move all of the old listener sockets to a temp list */
1952 	list_splice_init(&serv->sv_permsocks, &permsocks);
1953 
1954 	/*
1955 	 * Walk the list of server_socks from userland and move any that match
1956 	 * back to sv_permsocks
1957 	 */
1958 	nlmsg_for_each_attr_type(attr, NFSD_A_SERVER_SOCK_ADDR, info->nlhdr,
1959 				 GENL_HDRLEN, rem) {
1960 		struct nlattr *tb[NFSD_A_SOCK_MAX + 1];
1961 		const char *xcl_name;
1962 		struct sockaddr *sa;
1963 
1964 		if (nla_parse_nested(tb, NFSD_A_SOCK_MAX, attr,
1965 				     nfsd_sock_nl_policy, info->extack) < 0)
1966 			continue;
1967 
1968 		if (!tb[NFSD_A_SOCK_ADDR] || !tb[NFSD_A_SOCK_TRANSPORT_NAME])
1969 			continue;
1970 
1971 		if (nla_len(tb[NFSD_A_SOCK_ADDR]) < sizeof(*sa))
1972 			continue;
1973 
1974 		xcl_name = nla_data(tb[NFSD_A_SOCK_TRANSPORT_NAME]);
1975 		sa = nla_data(tb[NFSD_A_SOCK_ADDR]);
1976 
1977 		/* Put back any matching sockets */
1978 		list_for_each_entry_safe(xprt, tmp, &permsocks, xpt_list) {
1979 			/* This shouldn't be possible */
1980 			if (WARN_ON_ONCE(xprt->xpt_net != net)) {
1981 				list_move(&xprt->xpt_list, &serv->sv_permsocks);
1982 				continue;
1983 			}
1984 
1985 			/* If everything matches, put it back */
1986 			if (!strcmp(xprt->xpt_class->xcl_name, xcl_name) &&
1987 			    rpc_cmp_addr_port(sa, (struct sockaddr *)&xprt->xpt_local)) {
1988 				list_move(&xprt->xpt_list, &serv->sv_permsocks);
1989 				break;
1990 			}
1991 		}
1992 	}
1993 
1994 	/*
1995 	 * If there are listener transports remaining on the permsocks list,
1996 	 * it means we were asked to remove a listener.
1997 	 */
1998 	if (!list_empty(&permsocks)) {
1999 		list_splice_init(&permsocks, &serv->sv_permsocks);
2000 		delete = true;
2001 	}
2002 	spin_unlock_bh(&serv->sv_lock);
2003 
2004 	/* Do not remove listeners while there are active threads. */
2005 	if (serv->sv_nrthreads) {
2006 		err = -EBUSY;
2007 		goto out_unlock_mtx;
2008 	}
2009 
2010 	/*
2011 	 * Since we can't delete an arbitrary llist entry, destroy the
2012 	 * remaining listeners and recreate the list.
2013 	 */
2014 	if (delete)
2015 		svc_xprt_destroy_all(serv, net, false);
2016 
2017 	/* walk list of addrs again, open any that still don't exist */
2018 	nlmsg_for_each_attr_type(attr, NFSD_A_SERVER_SOCK_ADDR, info->nlhdr,
2019 				 GENL_HDRLEN, rem) {
2020 		struct nlattr *tb[NFSD_A_SOCK_MAX + 1];
2021 		const char *xcl_name;
2022 		struct sockaddr *sa;
2023 		int ret;
2024 
2025 		if (nla_parse_nested(tb, NFSD_A_SOCK_MAX, attr,
2026 				     nfsd_sock_nl_policy, info->extack) < 0)
2027 			continue;
2028 
2029 		if (!tb[NFSD_A_SOCK_ADDR] || !tb[NFSD_A_SOCK_TRANSPORT_NAME])
2030 			continue;
2031 
2032 		if (nla_len(tb[NFSD_A_SOCK_ADDR]) < sizeof(*sa))
2033 			continue;
2034 
2035 		xcl_name = nla_data(tb[NFSD_A_SOCK_TRANSPORT_NAME]);
2036 		sa = nla_data(tb[NFSD_A_SOCK_ADDR]);
2037 
2038 		xprt = svc_find_listener(serv, xcl_name, net, sa);
2039 		if (xprt) {
2040 			if (delete)
2041 				WARN_ONCE(1, "Transport type=%s already exists\n",
2042 					  xcl_name);
2043 			svc_xprt_put(xprt);
2044 			continue;
2045 		}
2046 
2047 		ret = svc_xprt_create_from_sa(serv, xcl_name, net, sa, 0,
2048 					      current_cred());
2049 		/* always save the latest error */
2050 		if (ret < 0)
2051 			err = ret;
2052 	}
2053 
2054 	if (!serv->sv_nrthreads && list_empty(&nn->nfsd_serv->sv_permsocks))
2055 		nfsd_destroy_serv(net);
2056 
2057 out_unlock_mtx:
2058 	mutex_unlock(&nfsd_mutex);
2059 
2060 	return err;
2061 }
2062 
2063 /**
2064  * nfsd_nl_listener_get_doit - get the nfs running listeners
2065  * @skb: reply buffer
2066  * @info: netlink metadata and command arguments
2067  *
2068  * Return 0 on success or a negative errno.
2069  */
nfsd_nl_listener_get_doit(struct sk_buff * skb,struct genl_info * info)2070 int nfsd_nl_listener_get_doit(struct sk_buff *skb, struct genl_info *info)
2071 {
2072 	struct svc_xprt *xprt;
2073 	struct svc_serv *serv;
2074 	struct nfsd_net *nn;
2075 	void *hdr;
2076 	int err;
2077 
2078 	skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2079 	if (!skb)
2080 		return -ENOMEM;
2081 
2082 	hdr = genlmsg_iput(skb, info);
2083 	if (!hdr) {
2084 		err = -EMSGSIZE;
2085 		goto err_free_msg;
2086 	}
2087 
2088 	mutex_lock(&nfsd_mutex);
2089 	nn = net_generic(genl_info_net(info), nfsd_net_id);
2090 
2091 	/* no nfs server? Just send empty socket list */
2092 	if (!nn->nfsd_serv)
2093 		goto out_unlock_mtx;
2094 
2095 	serv = nn->nfsd_serv;
2096 	spin_lock_bh(&serv->sv_lock);
2097 	list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list) {
2098 		struct nlattr *attr;
2099 
2100 		attr = nla_nest_start(skb, NFSD_A_SERVER_SOCK_ADDR);
2101 		if (!attr) {
2102 			err = -EINVAL;
2103 			goto err_serv_unlock;
2104 		}
2105 
2106 		if (nla_put_string(skb, NFSD_A_SOCK_TRANSPORT_NAME,
2107 				   xprt->xpt_class->xcl_name) ||
2108 		    nla_put(skb, NFSD_A_SOCK_ADDR,
2109 			    sizeof(struct sockaddr_storage),
2110 			    &xprt->xpt_local)) {
2111 			err = -EINVAL;
2112 			goto err_serv_unlock;
2113 		}
2114 
2115 		nla_nest_end(skb, attr);
2116 	}
2117 	spin_unlock_bh(&serv->sv_lock);
2118 out_unlock_mtx:
2119 	mutex_unlock(&nfsd_mutex);
2120 	genlmsg_end(skb, hdr);
2121 
2122 	return genlmsg_reply(skb, info);
2123 
2124 err_serv_unlock:
2125 	spin_unlock_bh(&serv->sv_lock);
2126 	mutex_unlock(&nfsd_mutex);
2127 err_free_msg:
2128 	nlmsg_free(skb);
2129 
2130 	return err;
2131 }
2132 
2133 /**
2134  * nfsd_nl_pool_mode_set_doit - set the number of running threads
2135  * @skb: reply buffer
2136  * @info: netlink metadata and command arguments
2137  *
2138  * Return 0 on success or a negative errno.
2139  */
nfsd_nl_pool_mode_set_doit(struct sk_buff * skb,struct genl_info * info)2140 int nfsd_nl_pool_mode_set_doit(struct sk_buff *skb, struct genl_info *info)
2141 {
2142 	const struct nlattr *attr;
2143 
2144 	if (GENL_REQ_ATTR_CHECK(info, NFSD_A_POOL_MODE_MODE))
2145 		return -EINVAL;
2146 
2147 	attr = info->attrs[NFSD_A_POOL_MODE_MODE];
2148 	return sunrpc_set_pool_mode(nla_data(attr));
2149 }
2150 
2151 /**
2152  * nfsd_nl_pool_mode_get_doit - get info about pool_mode
2153  * @skb: reply buffer
2154  * @info: netlink metadata and command arguments
2155  *
2156  * Return 0 on success or a negative errno.
2157  */
nfsd_nl_pool_mode_get_doit(struct sk_buff * skb,struct genl_info * info)2158 int nfsd_nl_pool_mode_get_doit(struct sk_buff *skb, struct genl_info *info)
2159 {
2160 	struct net *net = genl_info_net(info);
2161 	char buf[16];
2162 	void *hdr;
2163 	int err;
2164 
2165 	if (sunrpc_get_pool_mode(buf, ARRAY_SIZE(buf)) >= ARRAY_SIZE(buf))
2166 		return -ERANGE;
2167 
2168 	skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL);
2169 	if (!skb)
2170 		return -ENOMEM;
2171 
2172 	err = -EMSGSIZE;
2173 	hdr = genlmsg_iput(skb, info);
2174 	if (!hdr)
2175 		goto err_free_msg;
2176 
2177 	err = nla_put_string(skb, NFSD_A_POOL_MODE_MODE, buf) |
2178 	      nla_put_u32(skb, NFSD_A_POOL_MODE_NPOOLS, nfsd_nrpools(net));
2179 	if (err)
2180 		goto err_free_msg;
2181 
2182 	genlmsg_end(skb, hdr);
2183 	return genlmsg_reply(skb, info);
2184 
2185 err_free_msg:
2186 	nlmsg_free(skb);
2187 	return err;
2188 }
2189 
2190 /**
2191  * nfsd_net_init - Prepare the nfsd_net portion of a new net namespace
2192  * @net: a freshly-created network namespace
2193  *
2194  * This information stays around as long as the network namespace is
2195  * alive whether or not there is an NFSD instance running in the
2196  * namespace.
2197  *
2198  * Returns zero on success, or a negative errno otherwise.
2199  */
nfsd_net_init(struct net * net)2200 static __net_init int nfsd_net_init(struct net *net)
2201 {
2202 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2203 	int retval;
2204 	int i;
2205 
2206 	retval = nfsd_net_cb_init(nn);
2207 	if (retval)
2208 		return retval;
2209 	retval = nfsd_export_init(net);
2210 	if (retval)
2211 		goto out_export_error;
2212 	retval = nfsd_idmap_init(net);
2213 	if (retval)
2214 		goto out_idmap_error;
2215 	retval = percpu_counter_init_many(nn->counter, 0, GFP_KERNEL,
2216 					  NFSD_STATS_COUNTERS_NUM);
2217 	if (retval)
2218 		goto out_repcache_error;
2219 
2220 	memset(&nn->nfsd_svcstats, 0, sizeof(nn->nfsd_svcstats));
2221 	nn->nfsd_svcstats.program = &nfsd_programs[0];
2222 	if (!nfsd_proc_stat_init(net)) {
2223 		retval = -ENOMEM;
2224 		goto out_proc_error;
2225 	}
2226 
2227 	for (i = 0; i < sizeof(nn->nfsd_versions); i++)
2228 		nn->nfsd_versions[i] = nfsd_support_version(i);
2229 	for (i = 0; i < sizeof(nn->nfsd4_minorversions); i++)
2230 		nn->nfsd4_minorversions[i] = nfsd_support_version(4);
2231 	nn->nfsd_info.mutex = &nfsd_mutex;
2232 	nn->nfsd_serv = NULL;
2233 	nfsd4_init_leases_net(nn);
2234 	get_random_bytes(&nn->siphash_key, sizeof(nn->siphash_key));
2235 	seqlock_init(&nn->writeverf_lock);
2236 #if IS_ENABLED(CONFIG_NFS_LOCALIO)
2237 	spin_lock_init(&nn->local_clients_lock);
2238 	INIT_LIST_HEAD(&nn->local_clients);
2239 #endif
2240 	return 0;
2241 
2242 out_proc_error:
2243 	percpu_counter_destroy_many(nn->counter, NFSD_STATS_COUNTERS_NUM);
2244 out_repcache_error:
2245 	nfsd_idmap_shutdown(net);
2246 out_idmap_error:
2247 	nfsd_export_shutdown(net);
2248 out_export_error:
2249 	nfsd_net_cb_shutdown(nn);
2250 	return retval;
2251 }
2252 
2253 #if IS_ENABLED(CONFIG_NFS_LOCALIO)
2254 /**
2255  * nfsd_net_pre_exit - Disconnect localio clients from net namespace
2256  * @net: a network namespace that is about to be destroyed
2257  *
2258  * This invalidates ->net pointers held by localio clients
2259  * while they can still safely access nn->counter.
2260  */
nfsd_net_pre_exit(struct net * net)2261 static __net_exit void nfsd_net_pre_exit(struct net *net)
2262 {
2263 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2264 
2265 	nfs_localio_invalidate_clients(&nn->local_clients,
2266 				       &nn->local_clients_lock);
2267 }
2268 #endif
2269 
2270 /**
2271  * nfsd_net_exit - Release the nfsd_net portion of a net namespace
2272  * @net: a network namespace that is about to be destroyed
2273  *
2274  */
nfsd_net_exit(struct net * net)2275 static __net_exit void nfsd_net_exit(struct net *net)
2276 {
2277 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
2278 
2279 	kfree_sensitive(nn->fh_key);
2280 	nfsd_net_cb_shutdown(nn);
2281 	nfsd_proc_stat_shutdown(net);
2282 	percpu_counter_destroy_many(nn->counter, NFSD_STATS_COUNTERS_NUM);
2283 	nfsd_idmap_shutdown(net);
2284 	nfsd_export_shutdown(net);
2285 }
2286 
2287 static struct pernet_operations nfsd_net_ops = {
2288 	.init = nfsd_net_init,
2289 #if IS_ENABLED(CONFIG_NFS_LOCALIO)
2290 	.pre_exit = nfsd_net_pre_exit,
2291 #endif
2292 	.exit = nfsd_net_exit,
2293 	.id   = &nfsd_net_id,
2294 	.size = sizeof(struct nfsd_net),
2295 };
2296 
init_nfsd(void)2297 static int __init init_nfsd(void)
2298 {
2299 	int retval;
2300 
2301 	nfsd_debugfs_init();
2302 
2303 	retval = nfsd4_init_slabs();
2304 	if (retval)
2305 		return retval;
2306 	retval = nfsd4_init_pnfs();
2307 	if (retval)
2308 		goto out_free_slabs;
2309 	retval = nfsd_drc_slab_create();
2310 	if (retval)
2311 		goto out_free_pnfs;
2312 	nfsd_lockd_init();	/* lockd->nfsd callbacks */
2313 	retval = nfsd_export_wq_init();
2314 	if (retval)
2315 		goto out_free_lockd;
2316 	retval = register_pernet_subsys(&nfsd_net_ops);
2317 	if (retval < 0)
2318 		goto out_free_export_wq;
2319 	retval = register_cld_notifier();
2320 	if (retval)
2321 		goto out_free_subsys;
2322 	retval = nfsd4_create_laundry_wq();
2323 	if (retval)
2324 		goto out_free_cld;
2325 	retval = register_filesystem(&nfsd_fs_type);
2326 	if (retval)
2327 		goto out_free_nfsd4;
2328 	retval = genl_register_family(&nfsd_nl_family);
2329 	if (retval)
2330 		goto out_free_filesystem;
2331 	retval = create_proc_exports_entry();
2332 	if (retval)
2333 		goto out_free_all;
2334 	nfsd_localio_ops_init();
2335 
2336 	return 0;
2337 out_free_all:
2338 	genl_unregister_family(&nfsd_nl_family);
2339 out_free_filesystem:
2340 	unregister_filesystem(&nfsd_fs_type);
2341 out_free_nfsd4:
2342 	nfsd4_destroy_laundry_wq();
2343 out_free_cld:
2344 	unregister_cld_notifier();
2345 out_free_subsys:
2346 	unregister_pernet_subsys(&nfsd_net_ops);
2347 out_free_export_wq:
2348 	nfsd_export_wq_shutdown();
2349 out_free_lockd:
2350 	nfsd_lockd_shutdown();
2351 	nfsd_drc_slab_free();
2352 out_free_pnfs:
2353 	nfsd4_exit_pnfs();
2354 out_free_slabs:
2355 	nfsd4_free_slabs();
2356 	nfsd_debugfs_exit();
2357 	return retval;
2358 }
2359 
exit_nfsd(void)2360 static void __exit exit_nfsd(void)
2361 {
2362 	remove_proc_entry("fs/nfs/exports", NULL);
2363 	remove_proc_entry("fs/nfs", NULL);
2364 	genl_unregister_family(&nfsd_nl_family);
2365 	unregister_filesystem(&nfsd_fs_type);
2366 	nfsd4_destroy_laundry_wq();
2367 	unregister_cld_notifier();
2368 	unregister_pernet_subsys(&nfsd_net_ops);
2369 	nfsd_export_wq_shutdown();
2370 	nfsd_drc_slab_free();
2371 	nfsd_lockd_shutdown();
2372 	nfsd4_free_slabs();
2373 	nfsd4_exit_pnfs();
2374 	nfsd_debugfs_exit();
2375 }
2376 
2377 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>");
2378 MODULE_DESCRIPTION("In-kernel NFS server");
2379 MODULE_LICENSE("GPL");
2380 module_init(init_nfsd)
2381 module_exit(exit_nfsd)
2382