nfssvc.c 26.8 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
Linus Torvalds's avatar
Linus Torvalds committed
2 3 4 5 6 7 8 9
/*
 * Central processing for nfsd.
 *
 * Authors:	Olaf Kirch (okir@monad.swb.de)
 *
 * Copyright (C) 1995, 1996, 1997 Olaf Kirch <okir@monad.swb.de>
 */

10
#include <linux/sched/signal.h>
11
#include <linux/freezer.h>
12
#include <linux/module.h>
Linus Torvalds's avatar
Linus Torvalds committed
13
#include <linux/fs_struct.h>
Andy Adamson's avatar
Andy Adamson committed
14
#include <linux/swap.h>
Linus Torvalds's avatar
Linus Torvalds committed
15 16 17

#include <linux/sunrpc/stats.h>
#include <linux/sunrpc/svcsock.h>
18
#include <linux/sunrpc/svc_xprt.h>
Linus Torvalds's avatar
Linus Torvalds committed
19
#include <linux/lockd/bind.h>
20
#include <linux/nfsacl.h>
21
#include <linux/seq_file.h>
22 23 24
#include <linux/inetdevice.h>
#include <net/addrconf.h>
#include <net/ipv6.h>
25
#include <net/net_namespace.h>
26 27
#include "nfsd.h"
#include "cache.h"
28
#include "vfs.h"
29
#include "netns.h"
30
#include "filecache.h"
Linus Torvalds's avatar
Linus Torvalds committed
31

32 33
#include "trace.h"

Linus Torvalds's avatar
Linus Torvalds committed
34 35 36
#define NFSDDBG_FACILITY	NFSDDBG_SVC

extern struct svc_program	nfsd_program;
37
static int			nfsd(void *vrqstp);
38 39 40 41 42 43
#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
static int			nfsd_acl_rpcbind_set(struct net *,
						     const struct svc_program *,
						     u32, int,
						     unsigned short,
						     unsigned short);
44 45 46
static __be32			nfsd_acl_init_request(struct svc_rqst *,
						const struct svc_program *,
						struct svc_process_info *);
47 48 49 50 51 52
#endif
static int			nfsd_rpcbind_set(struct net *,
						 const struct svc_program *,
						 u32, int,
						 unsigned short,
						 unsigned short);
53 54 55
static __be32			nfsd_init_request(struct svc_rqst *,
						const struct svc_program *,
						struct svc_process_info *);
Linus Torvalds's avatar
Linus Torvalds committed
56

57
/*
58
 * nfsd_mutex protects nn->nfsd_serv -- both the pointer itself and the members
59 60 61
 * of the svc_serv struct. In particular, ->sv_nrthreads but also to some
 * extent ->sv_temp_socks and ->sv_permsocks. It also protects nfsdstats.th_cnt
 *
62
 * If (out side the lock) nn->nfsd_serv is non-NULL, then it must point to a
63 64 65 66 67 68 69
 * properly initialised 'struct svc_serv' with ->sv_nrthreads > 0. That number
 * of nfsd threads must exist and each must listed in ->sp_all_threads in each
 * entry of ->sv_pools[].
 *
 * Transitions of the thread count between zero and non-zero are of particular
 * interest since the svc_serv needs to be created and initialized at that
 * point, or freed.
70 71 72 73 74 75 76 77
 *
 * Finally, the nfsd_mutex also protects some of the global variables that are
 * accessed when nfsd starts and that are settable via the write_* routines in
 * nfsctl.c. In particular:
 *
 *	user_recovery_dirname
 *	user_lease_time
 *	nfsd_versions
78 79 80
 */
DEFINE_MUTEX(nfsd_mutex);

81 82 83 84 85 86
/*
 * nfsd_drc_lock protects nfsd_drc_max_pages and nfsd_drc_pages_used.
 * nfsd_drc_max_pages limits the total amount of memory available for
 * version 4.1 DRC caches.
 * nfsd_drc_pages_used tracks the current version 4.1 DRC memory usage.
 */
87
DEFINE_SPINLOCK(nfsd_drc_lock);
88 89
unsigned long	nfsd_drc_max_mem;
unsigned long	nfsd_drc_mem_used;
90

91 92
#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
static struct svc_stat	nfsd_acl_svcstats;
93
static const struct svc_version *nfsd_acl_version[] = {
94 95 96 97 98
	[2] = &nfsd_acl_version2,
	[3] = &nfsd_acl_version3,
};

#define NFSD_ACL_MINVERS            2
99
#define NFSD_ACL_NRVERS		ARRAY_SIZE(nfsd_acl_version)
100 101 102 103

static struct svc_program	nfsd_acl_program = {
	.pg_prog		= NFS_ACL_PROGRAM,
	.pg_nvers		= NFSD_ACL_NRVERS,
104
	.pg_vers		= nfsd_acl_version,
105
	.pg_name		= "nfsacl",
106 107 108
	.pg_class		= "nfsd",
	.pg_stats		= &nfsd_acl_svcstats,
	.pg_authenticate	= &svc_set_client,
109
	.pg_init_request	= nfsd_acl_init_request,
110
	.pg_rpcbind_set		= nfsd_acl_rpcbind_set,
111 112 113 114 115 116 117
};

static struct svc_stat	nfsd_acl_svcstats = {
	.program	= &nfsd_acl_program,
};
#endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */

118
static const struct svc_version *nfsd_version[] = {
119 120 121 122 123 124 125 126 127 128
	[2] = &nfsd_version2,
#if defined(CONFIG_NFSD_V3)
	[3] = &nfsd_version3,
#endif
#if defined(CONFIG_NFSD_V4)
	[4] = &nfsd_version4,
#endif
};

#define NFSD_MINVERS    	2
129
#define NFSD_NRVERS		ARRAY_SIZE(nfsd_version)
130 131

struct svc_program		nfsd_program = {
132 133 134
#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
	.pg_next		= &nfsd_acl_program,
#endif
135 136
	.pg_prog		= NFS_PROGRAM,		/* program number */
	.pg_nvers		= NFSD_NRVERS,		/* nr of entries in nfsd_version */
137
	.pg_vers		= nfsd_version,		/* version table */
138 139 140 141
	.pg_name		= "nfsd",		/* program name */
	.pg_class		= "nfsd",		/* authentication class */
	.pg_stats		= &nfsd_svcstats,	/* version table */
	.pg_authenticate	= &svc_set_client,	/* export authentication */
142
	.pg_init_request	= nfsd_init_request,
143
	.pg_rpcbind_set		= nfsd_rpcbind_set,
144 145
};

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
static bool
nfsd_support_version(int vers)
{
	if (vers >= NFSD_MINVERS && vers < NFSD_NRVERS)
		return nfsd_version[vers] != NULL;
	return false;
}

static bool *
nfsd_alloc_versions(void)
{
	bool *vers = kmalloc_array(NFSD_NRVERS, sizeof(bool), GFP_KERNEL);
	unsigned i;

	if (vers) {
		/* All compiled versions are enabled by default */
		for (i = 0; i < NFSD_NRVERS; i++)
			vers[i] = nfsd_support_version(i);
	}
	return vers;
}

static bool *
nfsd_alloc_minorversions(void)
{
	bool *vers = kmalloc_array(NFSD_SUPPORTED_MINOR_VERSION + 1,
			sizeof(bool), GFP_KERNEL);
	unsigned i;
174

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
	if (vers) {
		/* All minor versions are enabled by default */
		for (i = 0; i <= NFSD_SUPPORTED_MINOR_VERSION; i++)
			vers[i] = nfsd_support_version(4);
	}
	return vers;
}

void
nfsd_netns_free_versions(struct nfsd_net *nn)
{
	kfree(nn->nfsd_versions);
	kfree(nn->nfsd4_minorversions);
	nn->nfsd_versions = NULL;
	nn->nfsd4_minorversions = NULL;
}

static void
nfsd_netns_init_versions(struct nfsd_net *nn)
{
	if (!nn->nfsd_versions) {
		nn->nfsd_versions = nfsd_alloc_versions();
		nn->nfsd4_minorversions = nfsd_alloc_minorversions();
		if (!nn->nfsd_versions || !nn->nfsd4_minorversions)
			nfsd_netns_free_versions(nn);
	}
}

int nfsd_vers(struct nfsd_net *nn, int vers, enum vers_op change)
204 205
{
	if (vers < NFSD_MINVERS || vers >= NFSD_NRVERS)
206
		return 0;
207 208
	switch(change) {
	case NFSD_SET:
209 210
		if (nn->nfsd_versions)
			nn->nfsd_versions[vers] = nfsd_support_version(vers);
211
		break;
212
	case NFSD_CLEAR:
213 214 215
		nfsd_netns_init_versions(nn);
		if (nn->nfsd_versions)
			nn->nfsd_versions[vers] = false;
216 217
		break;
	case NFSD_TEST:
218 219
		if (nn->nfsd_versions)
			return nn->nfsd_versions[vers];
220
		fallthrough;
221
	case NFSD_AVAIL:
222
		return nfsd_support_version(vers);
223 224 225
	}
	return 0;
}
226

227
static void
228
nfsd_adjust_nfsd_versions4(struct nfsd_net *nn)
229 230 231 232
{
	unsigned i;

	for (i = 0; i <= NFSD_SUPPORTED_MINOR_VERSION; i++) {
233
		if (nn->nfsd4_minorversions[i])
234 235
			return;
	}
236
	nfsd_vers(nn, 4, NFSD_CLEAR);
237 238
}

239
int nfsd_minorversion(struct nfsd_net *nn, u32 minorversion, enum vers_op change)
240
{
241 242
	if (minorversion > NFSD_SUPPORTED_MINOR_VERSION &&
	    change != NFSD_AVAIL)
243
		return -1;
244

245 246
	switch(change) {
	case NFSD_SET:
247 248 249 250 251
		if (nn->nfsd4_minorversions) {
			nfsd_vers(nn, 4, NFSD_SET);
			nn->nfsd4_minorversions[minorversion] =
				nfsd_vers(nn, 4, NFSD_TEST);
		}
252 253
		break;
	case NFSD_CLEAR:
254 255 256 257 258
		nfsd_netns_init_versions(nn);
		if (nn->nfsd4_minorversions) {
			nn->nfsd4_minorversions[minorversion] = false;
			nfsd_adjust_nfsd_versions4(nn);
		}
259 260
		break;
	case NFSD_TEST:
261 262 263
		if (nn->nfsd4_minorversions)
			return nn->nfsd4_minorversions[minorversion];
		return nfsd_vers(nn, 4, NFSD_TEST);
264
	case NFSD_AVAIL:
265 266
		return minorversion <= NFSD_SUPPORTED_MINOR_VERSION &&
			nfsd_vers(nn, 4, NFSD_AVAIL);
267 268 269 270
	}
	return 0;
}

Linus Torvalds's avatar
Linus Torvalds committed
271 272 273 274 275
/*
 * Maximum number of nfsd processes
 */
#define	NFSD_MAXSERVS		8192

276
int nfsd_nrthreads(struct net *net)
Linus Torvalds's avatar
Linus Torvalds committed
277
{
278
	int rv = 0;
279 280
	struct nfsd_net *nn = net_generic(net, nfsd_net_id);

281
	mutex_lock(&nfsd_mutex);
282 283
	if (nn->nfsd_serv)
		rv = nn->nfsd_serv->sv_nrthreads;
284 285
	mutex_unlock(&nfsd_mutex);
	return rv;
Linus Torvalds's avatar
Linus Torvalds committed
286 287
}

288
static int nfsd_init_socks(struct net *net, const struct cred *cred)
289 290
{
	int error;
291 292 293
	struct nfsd_net *nn = net_generic(net, nfsd_net_id);

	if (!list_empty(&nn->nfsd_serv->sv_permsocks))
294 295
		return 0;

296
	error = svc_create_xprt(nn->nfsd_serv, "udp", net, PF_INET, NFS_PORT,
297
					SVC_SOCK_DEFAULTS, cred);
298 299 300
	if (error < 0)
		return error;

301
	error = svc_create_xprt(nn->nfsd_serv, "tcp", net, PF_INET, NFS_PORT,
302
					SVC_SOCK_DEFAULTS, cred);
303 304 305 306 307 308
	if (error < 0)
		return error;

	return 0;
}

309
static int nfsd_users = 0;
310

311
static int nfsd_startup_generic(void)
312 313 314
{
	int ret;

315
	if (nfsd_users++)
316 317
		return 0;

318 319 320
	ret = nfsd_file_cache_init();
	if (ret)
		goto dec_users;
321

322 323
	ret = nfs4_state_start();
	if (ret)
Jeff Layton's avatar
Jeff Layton committed
324
		goto out_file_cache;
325 326
	return 0;

327 328
out_file_cache:
	nfsd_file_cache_shutdown();
329 330
dec_users:
	nfsd_users--;
331 332 333 334 335
	return ret;
}

static void nfsd_shutdown_generic(void)
{
336 337 338
	if (--nfsd_users)
		return;

339
	nfs4_state_shutdown();
340
	nfsd_file_cache_shutdown();
341 342
}

343
static bool nfsd_needs_lockd(struct nfsd_net *nn)
344
{
345
	return nfsd_vers(nn, 2, NFSD_TEST) || nfsd_vers(nn, 3, NFSD_TEST);
346 347
}

348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364
void nfsd_copy_boot_verifier(__be32 verf[2], struct nfsd_net *nn)
{
	int seq = 0;

	do {
		read_seqbegin_or_lock(&nn->boot_lock, &seq);
		/*
		 * This is opaque to client, so no need to byte-swap. Use
		 * __force to keep sparse happy. y2038 time_t overflow is
		 * irrelevant in this usage
		 */
		verf[0] = (__force __be32)nn->nfssvc_boot.tv_sec;
		verf[1] = (__force __be32)nn->nfssvc_boot.tv_nsec;
	} while (need_seqretry(&nn->boot_lock, seq));
	done_seqretry(&nn->boot_lock, seq);
}

365
static void nfsd_reset_boot_verifier_locked(struct nfsd_net *nn)
366 367 368 369 370 371 372 373 374 375 376
{
	ktime_get_real_ts64(&nn->nfssvc_boot);
}

void nfsd_reset_boot_verifier(struct nfsd_net *nn)
{
	write_seqlock(&nn->boot_lock);
	nfsd_reset_boot_verifier_locked(nn);
	write_sequnlock(&nn->boot_lock);
}

377
static int nfsd_startup_net(struct net *net, const struct cred *cred)
378
{
379
	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
380 381
	int ret;

382 383 384
	if (nn->nfsd_net_up)
		return 0;

385
	ret = nfsd_startup_generic();
386 387
	if (ret)
		return ret;
388
	ret = nfsd_init_socks(net, cred);
389 390
	if (ret)
		goto out_socks;
391

392
	if (nfsd_needs_lockd(nn) && !nn->lockd_up) {
393
		ret = lockd_up(net, cred);
394 395
		if (ret)
			goto out_socks;
396
		nn->lockd_up = true;
397 398
	}

399
	ret = nfsd_file_cache_start_net(net);
400 401
	if (ret)
		goto out_lockd;
402 403 404
	ret = nfs4_state_start_net(net);
	if (ret)
		goto out_filecache;
405

406 407 408
#ifdef CONFIG_NFSD_V4_2_INTER_SSC
	nfsd4_ssc_init_umount_work(nn);
#endif
409
	nn->nfsd_net_up = true;
410 411
	return 0;

412 413
out_filecache:
	nfsd_file_cache_shutdown_net(net);
414
out_lockd:
415 416
	if (nn->lockd_up) {
		lockd_down(net);
417
		nn->lockd_up = false;
418
	}
419
out_socks:
420
	nfsd_shutdown_generic();
421 422 423
	return ret;
}

424 425
static void nfsd_shutdown_net(struct net *net)
{
426 427
	struct nfsd_net *nn = net_generic(net, nfsd_net_id);

428
	nfsd_file_cache_shutdown_net(net);
429
	nfs4_state_shutdown_net(net);
430 431
	if (nn->lockd_up) {
		lockd_down(net);
432
		nn->lockd_up = false;
433
	}
434
	nn->nfsd_net_up = false;
435
	nfsd_shutdown_generic();
436 437
}

438 439 440 441 442 443 444 445 446
static int nfsd_inetaddr_event(struct notifier_block *this, unsigned long event,
	void *ptr)
{
	struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
	struct net_device *dev = ifa->ifa_dev->dev;
	struct net *net = dev_net(dev);
	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
	struct sockaddr_in sin;

447 448
	if ((event != NETDEV_DOWN) ||
	    !atomic_inc_not_zero(&nn->ntf_refcnt))
449 450 451 452 453 454 455 456
		goto out;

	if (nn->nfsd_serv) {
		dprintk("nfsd_inetaddr_event: removed %pI4\n", &ifa->ifa_local);
		sin.sin_family = AF_INET;
		sin.sin_addr.s_addr = ifa->ifa_local;
		svc_age_temp_xprts_now(nn->nfsd_serv, (struct sockaddr *)&sin);
	}
457 458
	atomic_dec(&nn->ntf_refcnt);
	wake_up(&nn->ntf_wq);
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477

out:
	return NOTIFY_DONE;
}

static struct notifier_block nfsd_inetaddr_notifier = {
	.notifier_call = nfsd_inetaddr_event,
};

#if IS_ENABLED(CONFIG_IPV6)
static int nfsd_inet6addr_event(struct notifier_block *this,
	unsigned long event, void *ptr)
{
	struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
	struct net_device *dev = ifa->idev->dev;
	struct net *net = dev_net(dev);
	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
	struct sockaddr_in6 sin6;

478 479
	if ((event != NETDEV_DOWN) ||
	    !atomic_inc_not_zero(&nn->ntf_refcnt))
480 481 482 483 484 485
		goto out;

	if (nn->nfsd_serv) {
		dprintk("nfsd_inet6addr_event: removed %pI6\n", &ifa->addr);
		sin6.sin6_family = AF_INET6;
		sin6.sin6_addr = ifa->addr;
486 487
		if (ipv6_addr_type(&sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
			sin6.sin6_scope_id = ifa->idev->dev->ifindex;
488 489
		svc_age_temp_xprts_now(nn->nfsd_serv, (struct sockaddr *)&sin6);
	}
490 491
	atomic_dec(&nn->ntf_refcnt);
	wake_up(&nn->ntf_wq);
492 493 494 495 496 497 498 499 500
out:
	return NOTIFY_DONE;
}

static struct notifier_block nfsd_inet6addr_notifier = {
	.notifier_call = nfsd_inet6addr_event,
};
#endif

501 502 503
/* Only used under nfsd_mutex, so this atomic may be overkill: */
static atomic_t nfsd_notifier_refcount = ATOMIC_INIT(0);

504
static void nfsd_last_thread(struct svc_serv *serv, struct net *net)
505
{
506 507
	struct nfsd_net *nn = net_generic(net, nfsd_net_id);

508
	atomic_dec(&nn->ntf_refcnt);
509 510 511
	/* check if the notifier still has clients */
	if (atomic_dec_return(&nfsd_notifier_refcount) == 0) {
		unregister_inetaddr_notifier(&nfsd_inetaddr_notifier);
512
#if IS_ENABLED(CONFIG_IPV6)
513
		unregister_inet6addr_notifier(&nfsd_inet6addr_notifier);
514
#endif
515
	}
516
	wait_event(nn->ntf_wq, atomic_read(&nn->ntf_refcnt) == 0);
517

518 519 520 521
	/*
	 * write_ports can create the server without actually starting
	 * any threads--if we get shut down before any threads are
	 * started, then nfsd_last_thread will be run before any of this
522
	 * other initialization has been done except the rpcb information.
523
	 */
524
	svc_rpcb_cleanup(serv, net);
525
	if (!nn->nfsd_net_up)
526
		return;
527

528
	nfsd_shutdown_net(net);
529
	pr_info("nfsd: last server has exited, flushing export cache\n");
530
	nfsd_export_flush(net);
531
}
532

533
void nfsd_reset_versions(struct nfsd_net *nn)
534 535 536
{
	int i;

537
	for (i = 0; i < NFSD_NRVERS; i++)
538
		if (nfsd_vers(nn, i, NFSD_TEST))
539
			return;
540

541 542
	for (i = 0; i < NFSD_NRVERS; i++)
		if (i != 4)
543
			nfsd_vers(nn, i, NFSD_SET);
544 545
		else {
			int minor = 0;
546
			while (nfsd_minorversion(nn, minor, NFSD_SET) >= 0)
547 548
				minor++;
		}
549 550
}

Andy Adamson's avatar
Andy Adamson committed
551 552 553 554 555 556 557 558 559 560 561 562 563 564
/*
 * Each session guarantees a negotiated per slot memory cache for replies
 * which in turn consumes memory beyond the v2/v3/v4.0 server. A dedicated
 * NFSv4.1 server might want to use more memory for a DRC than a machine
 * with mutiple services.
 *
 * Impose a hard limit on the number of pages for the DRC which varies
 * according to the machines free pages. This is of course only a default.
 *
 * For now this is a #defined shift which could be under admin control
 * in the future.
 */
static void set_max_drc(void)
{
565
	#define NFSD_DRC_SIZE_SHIFT	7
566 567 568
	nfsd_drc_max_mem = (nr_free_buffer_pages()
					>> NFSD_DRC_SIZE_SHIFT) * PAGE_SIZE;
	nfsd_drc_mem_used = 0;
569
	dprintk("%s nfsd_drc_max_mem %lu \n", __func__, nfsd_drc_max_mem);
Andy Adamson's avatar
Andy Adamson committed
570
}
571

572
static int nfsd_get_default_max_blksize(void)
573
{
574 575 576
	struct sysinfo i;
	unsigned long long target;
	unsigned long ret;
577

578
	si_meminfo(&i);
579
	target = (i.totalram - i.totalhigh) << PAGE_SHIFT;
580 581 582 583 584 585 586 587 588 589 590 591 592
	/*
	 * Aim for 1/4096 of memory per thread This gives 1MB on 4Gig
	 * machines, but only uses 32K on 128M machines.  Bottom out at
	 * 8K on 32M and smaller.  Of course, this is only a default.
	 */
	target >>= 12;

	ret = NFSSVC_MAXBLKSIZE;
	while (ret > target && ret >= 8*1024*2)
		ret /= 2;
	return ret;
}

593
static const struct svc_serv_ops nfsd_thread_sv_ops = {
594 595 596
	.svo_shutdown		= nfsd_last_thread,
	.svo_function		= nfsd,
	.svo_enqueue_xprt	= svc_xprt_do_enqueue,
597
	.svo_setup		= svc_set_num_threads,
598
	.svo_module		= THIS_MODULE,
599 600
};

601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
static void nfsd_complete_shutdown(struct net *net)
{
	struct nfsd_net *nn = net_generic(net, nfsd_net_id);

	WARN_ON(!mutex_is_locked(&nfsd_mutex));

	nn->nfsd_serv = NULL;
	complete(&nn->nfsd_shutdown_complete);
}

void nfsd_shutdown_threads(struct net *net)
{
	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
	struct svc_serv *serv;

	mutex_lock(&nfsd_mutex);
	serv = nn->nfsd_serv;
	if (serv == NULL) {
		mutex_unlock(&nfsd_mutex);
		return;
	}

	svc_get(serv);
	/* Kill outstanding nfsd threads */
	serv->sv_ops->svo_setup(serv, NULL, 0);
	nfsd_destroy(net);
	mutex_unlock(&nfsd_mutex);
	/* Wait for shutdown of nfsd_serv to complete */
	wait_for_completion(&nn->nfsd_shutdown_complete);
}

632
bool i_am_nfsd(void)
633 634 635 636
{
	return kthread_func(current) == nfsd;
}

637
int nfsd_create_serv(struct net *net)
638
{
639
	int error;
640
	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
641

642
	WARN_ON(!mutex_is_locked(&nfsd_mutex));
643 644
	if (nn->nfsd_serv) {
		svc_get(nn->nfsd_serv);
645 646
		return 0;
	}
647 648
	if (nfsd_max_blksize == 0)
		nfsd_max_blksize = nfsd_get_default_max_blksize();
649
	nfsd_reset_versions(nn);
650
	nn->nfsd_serv = svc_create_pooled(&nfsd_program, nfsd_max_blksize,
651
						&nfsd_thread_sv_ops);
652
	if (nn->nfsd_serv == NULL)
653
		return -ENOMEM;
654
	init_completion(&nn->nfsd_shutdown_complete);
655

656
	nn->nfsd_serv->sv_maxconn = nn->max_connections;
657
	error = svc_bind(nn->nfsd_serv, net);
658
	if (error < 0) {
659
		svc_destroy(nn->nfsd_serv);
660
		nfsd_complete_shutdown(net);
661 662 663
		return error;
	}

664
	set_max_drc();
665 666 667
	/* check if the notifier is already set */
	if (atomic_inc_return(&nfsd_notifier_refcount) == 1) {
		register_inetaddr_notifier(&nfsd_inetaddr_notifier);
668
#if IS_ENABLED(CONFIG_IPV6)
669
		register_inet6addr_notifier(&nfsd_inet6addr_notifier);
670
#endif
671
	}
672
	atomic_inc(&nn->ntf_refcnt);
673
	nfsd_reset_boot_verifier(nn);
674
	return 0;
675 676
}

677
int nfsd_nrpools(struct net *net)
678
{
679 680 681
	struct nfsd_net *nn = net_generic(net, nfsd_net_id);

	if (nn->nfsd_serv == NULL)
682 683
		return 0;
	else
684
		return nn->nfsd_serv->sv_nrpools;
685 686
}

687
int nfsd_get_nrthreads(int n, int *nthreads, struct net *net)
688 689
{
	int i = 0;
690
	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
691

692 693 694
	if (nn->nfsd_serv != NULL) {
		for (i = 0; i < nn->nfsd_serv->sv_nrpools && i < n; i++)
			nthreads[i] = nn->nfsd_serv->sv_pools[i].sp_nrthreads;
695 696 697 698 699
	}

	return 0;
}

700 701 702 703 704 705 706 707 708
void nfsd_destroy(struct net *net)
{
	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
	int destroy = (nn->nfsd_serv->sv_nrthreads == 1);

	if (destroy)
		svc_shutdown_net(nn->nfsd_serv, net);
	svc_destroy(nn->nfsd_serv);
	if (destroy)
709
		nfsd_complete_shutdown(net);
710 711
}

712
int nfsd_set_nrthreads(int n, int *nthreads, struct net *net)
713 714 715 716
{
	int i = 0;
	int tot = 0;
	int err = 0;
717
	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
718

719 720
	WARN_ON(!mutex_is_locked(&nfsd_mutex));

721
	if (nn->nfsd_serv == NULL || n <= 0)
722 723
		return 0;

724 725
	if (n > nn->nfsd_serv->sv_nrpools)
		n = nn->nfsd_serv->sv_nrpools;
726 727 728 729

	/* enforce a global maximum number of threads */
	tot = 0;
	for (i = 0; i < n; i++) {
730
		nthreads[i] = min(nthreads[i], NFSD_MAXSERVS);
731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
		tot += nthreads[i];
	}
	if (tot > NFSD_MAXSERVS) {
		/* total too large: scale down requested numbers */
		for (i = 0; i < n && tot > 0; i++) {
		    	int new = nthreads[i] * NFSD_MAXSERVS / tot;
			tot -= (nthreads[i] - new);
			nthreads[i] = new;
		}
		for (i = 0; i < n && tot > 0; i++) {
			nthreads[i]--;
			tot--;
		}
	}

	/*
	 * There must always be a thread in pool 0; the admin
	 * can't shut down NFS completely using pool_threads.
	 */
	if (nthreads[0] == 0)
		nthreads[0] = 1;

	/* apply the new numbers */
754
	svc_get(nn->nfsd_serv);
755
	for (i = 0; i < n; i++) {
756 757
		err = nn->nfsd_serv->sv_ops->svo_setup(nn->nfsd_serv,
				&nn->nfsd_serv->sv_pools[i], nthreads[i]);
758 759 760
		if (err)
			break;
	}
761
	nfsd_destroy(net);
762 763 764
	return err;
}

765 766 767 768 769
/*
 * Adjust the number of threads and return the new number of threads.
 * This is also the function that starts the server if necessary, if
 * this is the first time nrservs is nonzero.
 */
Linus Torvalds's avatar
Linus Torvalds committed
770
int
771
nfsd_svc(int nrservs, struct net *net, const struct cred *cred)
Linus Torvalds's avatar
Linus Torvalds committed
772 773
{
	int	error;
774
	bool	nfsd_up_before;
775
	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
776 777

	mutex_lock(&nfsd_mutex);
778
	dprintk("nfsd: creating service\n");
779 780 781

	nrservs = max(nrservs, 0);
	nrservs = min(nrservs, NFSD_MAXSERVS);
782
	error = 0;
783

784
	if (nrservs == 0 && nn->nfsd_serv == NULL)
785 786
		goto out;

787 788 789
	strlcpy(nn->nfsd_name, utsname()->nodename,
		sizeof(nn->nfsd_name));

790
	error = nfsd_create_serv(net);
791
	if (error)
792 793
		goto out;

794
	nfsd_up_before = nn->nfsd_net_up;
795

796
	error = nfsd_startup_net(net, cred);
797 798
	if (error)
		goto out_destroy;
799 800
	error = nn->nfsd_serv->sv_ops->svo_setup(nn->nfsd_serv,
			NULL, nrservs);
801 802
	if (error)
		goto out_shutdown;
803
	/* We are holding a reference to nn->nfsd_serv which
804 805 806
	 * we don't want to count in the return value,
	 * so subtract 1
	 */
807
	error = nn->nfsd_serv->sv_nrthreads - 1;
808
out_shutdown:
809
	if (error < 0 && !nfsd_up_before)
810
		nfsd_shutdown_net(net);
811
out_destroy:
812
	nfsd_destroy(net);		/* Release server */
813
out:
814
	mutex_unlock(&nfsd_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
815 816 817
	return error;
}

818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
static bool
nfsd_support_acl_version(int vers)
{
	if (vers >= NFSD_ACL_MINVERS && vers < NFSD_ACL_NRVERS)
		return nfsd_acl_version[vers] != NULL;
	return false;
}

static int
nfsd_acl_rpcbind_set(struct net *net, const struct svc_program *progp,
		     u32 version, int family, unsigned short proto,
		     unsigned short port)
{
	if (!nfsd_support_acl_version(version) ||
833
	    !nfsd_vers(net_generic(net, nfsd_net_id), version, NFSD_TEST))
834 835 836 837
		return 0;
	return svc_generic_rpcbind_set(net, progp, version, family,
			proto, port);
}
838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870

static __be32
nfsd_acl_init_request(struct svc_rqst *rqstp,
		      const struct svc_program *progp,
		      struct svc_process_info *ret)
{
	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
	int i;

	if (likely(nfsd_support_acl_version(rqstp->rq_vers) &&
	    nfsd_vers(nn, rqstp->rq_vers, NFSD_TEST)))
		return svc_generic_init_request(rqstp, progp, ret);

	ret->mismatch.lovers = NFSD_ACL_NRVERS;
	for (i = NFSD_ACL_MINVERS; i < NFSD_ACL_NRVERS; i++) {
		if (nfsd_support_acl_version(rqstp->rq_vers) &&
		    nfsd_vers(nn, i, NFSD_TEST)) {
			ret->mismatch.lovers = i;
			break;
		}
	}
	if (ret->mismatch.lovers == NFSD_ACL_NRVERS)
		return rpc_prog_unavail;
	ret->mismatch.hivers = NFSD_ACL_MINVERS;
	for (i = NFSD_ACL_NRVERS - 1; i >= NFSD_ACL_MINVERS; i--) {
		if (nfsd_support_acl_version(rqstp->rq_vers) &&
		    nfsd_vers(nn, i, NFSD_TEST)) {
			ret->mismatch.hivers = i;
			break;
		}
	}
	return rpc_prog_mismatch;
}
871 872 873 874 875 876 877
#endif

static int
nfsd_rpcbind_set(struct net *net, const struct svc_program *progp,
		 u32 version, int family, unsigned short proto,
		 unsigned short port)
{
878
	if (!nfsd_vers(net_generic(net, nfsd_net_id), version, NFSD_TEST))
879 880 881 882
		return 0;
	return svc_generic_rpcbind_set(net, progp, version, family,
			proto, port);
}
Linus Torvalds's avatar
Linus Torvalds committed
883

884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913
static __be32
nfsd_init_request(struct svc_rqst *rqstp,
		  const struct svc_program *progp,
		  struct svc_process_info *ret)
{
	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
	int i;

	if (likely(nfsd_vers(nn, rqstp->rq_vers, NFSD_TEST)))
		return svc_generic_init_request(rqstp, progp, ret);

	ret->mismatch.lovers = NFSD_NRVERS;
	for (i = NFSD_MINVERS; i < NFSD_NRVERS; i++) {
		if (nfsd_vers(nn, i, NFSD_TEST)) {
			ret->mismatch.lovers = i;
			break;
		}
	}
	if (ret->mismatch.lovers == NFSD_NRVERS)
		return rpc_prog_unavail;
	ret->mismatch.hivers = NFSD_MINVERS;
	for (i = NFSD_NRVERS - 1; i >= NFSD_MINVERS; i--) {
		if (nfsd_vers(nn, i, NFSD_TEST)) {
			ret->mismatch.hivers = i;
			break;
		}
	}
	return rpc_prog_mismatch;
}

Linus Torvalds's avatar
Linus Torvalds committed
914 915 916
/*
 * This is the NFS server kernel thread
 */
917 918
static int
nfsd(void *vrqstp)
Linus Torvalds's avatar
Linus Torvalds committed
919
{
920
	struct svc_rqst *rqstp = (struct svc_rqst *) vrqstp;
921 922
	struct svc_xprt *perm_sock = list_entry(rqstp->rq_server->sv_permsocks.next, typeof(struct svc_xprt), xpt_list);
	struct net *net = perm_sock->xpt_net;
923
	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
924
	int err;
Linus Torvalds's avatar
Linus Torvalds committed
925 926

	/* Lock module and set up kernel thread */
927
	mutex_lock(&nfsd_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
928

929
	/* At this point, the thread shares current->fs
930 931
	 * with the init process. We need to create files with the
	 * umask as defined by the client instead of init's umask. */
932
	if (unshare_fs_struct() < 0) {
Linus Torvalds's avatar
Linus Torvalds committed
933 934 935
		printk("Unable to start nfsd thread: out of memory\n");
		goto out;
	}
936

Linus Torvalds's avatar
Linus Torvalds committed
937 938
	current->fs->umask = 0;

939 940
	/*
	 * thread is spawned with all signals set to SIG_IGN, re-enable
941
	 * the ones that will bring down the thread
942
	 */
943 944 945 946
	allow_signal(SIGKILL);
	allow_signal(SIGHUP);
	allow_signal(SIGINT);
	allow_signal(SIGQUIT);
947

Linus Torvalds's avatar
Linus Torvalds committed
948
	nfsdstats.th_cnt++;
949 950
	mutex_unlock(&nfsd_mutex);

951
	set_freezable();
Linus Torvalds's avatar
Linus Torvalds committed
952 953 954 955 956

	/*
	 * The main request loop
	 */
	for (;;) {
957 958 959
		/* Update sv_maxconn if it has changed */
		rqstp->rq_server->sv_maxconn = nn->max_connections;

Linus Torvalds's avatar
Linus Torvalds committed
960 961 962 963
		/*
		 * Find a socket with data available and call its
		 * recvfrom routine.
		 */
964
		while ((err = svc_recv(rqstp, 60*60*HZ)) == -EAGAIN)
Linus Torvalds's avatar
Linus Torvalds committed
965
			;
966
		if (err == -EINTR)
Linus Torvalds's avatar
Linus Torvalds committed
967
			break;
968
		validate_process_creds();
969
		svc_process(rqstp);
970
		validate_process_creds();
Linus Torvalds's avatar
Linus Torvalds committed
971 972
	}

973
	/* Clear signals before calling svc_exit_thread() */
974
	flush_signals(current);
Linus Torvalds's avatar
Linus Torvalds committed
975

976
	mutex_lock(&nfsd_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
977 978 979
	nfsdstats.th_cnt --;

out:
980
	rqstp->rq_server = NULL;
981

Linus Torvalds's avatar
Linus Torvalds committed
982 983 984
	/* Release the thread */
	svc_exit_thread(rqstp);

985
	nfsd_destroy(net);
986

Linus Torvalds's avatar
Linus Torvalds committed
987
	/* Release module */
988
	mutex_unlock(&nfsd_mutex);
Linus Torvalds's avatar
Linus Torvalds committed
989
	module_put_and_exit(0);
990
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
991 992
}

993 994 995 996 997 998 999 1000 1001 1002 1003 1004
/**
 * nfsd_dispatch - Process an NFS or NFSACL Request
 * @rqstp: incoming request
 * @statp: pointer to location of accept_stat field in RPC Reply buffer
 *
 * This RPC dispatcher integrates the NFS server's duplicate reply cache.
 *
 * Return values:
 *  %0: Processing complete; do not send a Reply
 *  %1: Processing complete; send Reply in rqstp->rq_res
 */
int nfsd_dispatch(struct svc_rqst *rqstp, __be32 *statp)
Linus Torvalds's avatar
Linus Torvalds committed
1005
{
1006 1007
	const struct svc_procedure *proc = rqstp->rq_procinfo;
	struct kvec *resv = &rqstp->rq_res.head[0];
1008
	__be32 *p;
Linus Torvalds's avatar
Linus Torvalds committed
1009

1010 1011 1012 1013 1014
	/*
	 * Give the xdr decoder a chance to change this if it wants
	 * (necessary in the NFSv4.0 compound case)
	 */
	rqstp->rq_cachetype = proc->pc_cachetype;
1015 1016

	svcxdr_init_decode(rqstp);
1017
	if (!proc->pc_decode(rqstp, &rqstp->rq_arg_stream))
1018
		goto out_decode_err;
1019 1020

	switch (nfsd_cache_lookup(rqstp)) {
1021 1022
	case RC_DOIT:
		break;
Linus Torvalds's avatar
Linus Torvalds committed
1023
	case RC_REPLY:
1024
		goto out_cached_reply;
1025
	case RC_DROPIT:
1026
		goto out_dropit;
Linus Torvalds's avatar
Linus Torvalds committed
1027 1028
	}

1029 1030 1031
	/*
	 * Need to grab the location to store the status, as
	 * NFSv4 does some encoding while processing
Linus Torvalds's avatar
Linus Torvalds committed
1032
	 */
1033
	p = resv->iov_base + resv->iov_len;
1034
	svcxdr_init_encode(rqstp);
Linus Torvalds's avatar
Linus Torvalds committed
1035

1036 1037
	*statp = proc->pc_func(rqstp);
	if (*statp == rpc_drop_reply || test_bit(RQ_DROPME, &rqstp->rq_flags))
1038
		goto out_update_drop;
Linus Torvalds's avatar
Linus Torvalds committed
1039

1040
	if (!proc->pc_encode(rqstp, p))
1041
		goto out_encode_err;
Linus Torvalds's avatar
Linus Torvalds committed
1042

J. Bruce Fields's avatar
J. Bruce Fields committed
1043
	nfsd_cache_update(rqstp, rqstp->rq_cachetype, statp + 1);
1044 1045 1046 1047
out_cached_reply:
	return 1;

out_decode_err:
1048
	trace_nfsd_garbage_args_err(rqstp);
1049 1050 1051 1052 1053 1054 1055 1056 1057
	*statp = rpc_garbage_args;
	return 1;

out_update_drop:
	nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
out_dropit:
	return 0;

out_encode_err:
1058
	trace_nfsd_cant_encode_err(rqstp);
1059 1060
	nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
	*statp = rpc_system_err;
Linus Torvalds's avatar
Linus Torvalds committed
1061 1062
	return 1;
}
1063

1064 1065 1066
/**
 * nfssvc_decode_voidarg - Decode void arguments
 * @rqstp: Server RPC transaction context
1067
 * @xdr: XDR stream positioned at arguments to decode
1068 1069 1070 1071 1072
 *
 * Return values:
 *   %0: Arguments were not valid
 *   %1: Decoding was successful
 */
1073
int nfssvc_decode_voidarg(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
{
	return 1;
}

/**
 * nfssvc_encode_voidres - Encode void results
 * @rqstp: Server RPC transaction context
 * @p: buffer in which to encode results
 *
 * Return values:
 *   %0: Local error while encoding
 *   %1: Encoding was successful
 */
int nfssvc_encode_voidres(struct svc_rqst *rqstp, __be32 *p)
{
1089
	return 1;
1090 1091
}

1092 1093
int nfsd_pool_stats_open(struct inode *inode, struct file *file)
{
1094
	int ret;
1095
	struct nfsd_net *nn = net_generic(inode->i_sb->s_fs_info, nfsd_net_id);
1096

1097
	mutex_lock(&nfsd_mutex);
1098
	if (nn->nfsd_serv == NULL) {
1099
		mutex_unlock(&nfsd_mutex);
1100
		return -ENODEV;
1101 1102
	}
	/* bump up the psudo refcount while traversing */
1103 1104
	svc_get(nn->nfsd_serv);
	ret = svc_pool_stats_open(nn->nfsd_serv, file);
1105 1106 1107 1108 1109 1110 1111
	mutex_unlock(&nfsd_mutex);
	return ret;
}

int nfsd_pool_stats_release(struct inode *inode, struct file *file)
{
	int ret = seq_release(inode, file);
1112
	struct net *net = inode->i_sb->s_fs_info;
1113

1114 1115
	mutex_lock(&nfsd_mutex);
	/* this function really, really should have been called svc_put() */
1116
	nfsd_destroy(net);
1117 1118
	mutex_unlock(&nfsd_mutex);
	return ret;
1119
}