File Index
Files and directories
Sign in
FILE

nfsctl.c

File preview with syntax highlighting.

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