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