nfssvc.c 26.1 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
L
Linus Torvalds 已提交
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>
L
Linus Torvalds 已提交
13
#include <linux/fs_struct.h>
A
Andy Adamson 已提交
14
#include <linux/swap.h>
L
Linus Torvalds 已提交
15 16 17

#include <linux/sunrpc/stats.h>
#include <linux/sunrpc/svcsock.h>
18
#include <linux/sunrpc/svc_xprt.h>
L
Linus Torvalds 已提交
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"
L
Linus Torvalds 已提交
31

32 33
#include "trace.h"

L
Linus Torvalds 已提交
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 *);
L
Linus Torvalds 已提交
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 87
/*
 * 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.
 */
spinlock_t	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,
J
J. Bruce Fields 已提交
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;
}

L
Linus Torvalds 已提交
271 272 273 274 275
/*
 * Maximum number of nfsd processes
 */
#define	NFSD_MAXSERVS		8192

276
int nfsd_nrthreads(struct net *net)
L
Linus Torvalds 已提交
277
{
N
Neil Brown 已提交
278
	int rv = 0;
279 280
	struct nfsd_net *nn = net_generic(net, nfsd_net_id);

N
Neil Brown 已提交
281
	mutex_lock(&nfsd_mutex);
282 283
	if (nn->nfsd_serv)
		rv = nn->nfsd_serv->sv_nrthreads;
N
Neil Brown 已提交
284 285
	mutex_unlock(&nfsd_mutex);
	return rv;
L
Linus Torvalds 已提交
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 312 313 314
static int nfsd_startup_generic(int nrservs)
{
	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)
J
Jeff Layton 已提交
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(int nrservs, 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(nrservs);
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
	nn->nfsd_net_up = true;
407 408
	return 0;

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

421 422
static void nfsd_shutdown_net(struct net *net)
{
423 424
	struct nfsd_net *nn = net_generic(net, nfsd_net_id);

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

435 436 437 438 439 440 441 442 443
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;

444 445
	if ((event != NETDEV_DOWN) ||
	    !atomic_inc_not_zero(&nn->ntf_refcnt))
446 447 448 449 450 451 452 453
		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);
	}
454 455
	atomic_dec(&nn->ntf_refcnt);
	wake_up(&nn->ntf_wq);
456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474

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;

475 476
	if ((event != NETDEV_DOWN) ||
	    !atomic_inc_not_zero(&nn->ntf_refcnt))
477 478 479 480 481 482
		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;
483 484
		if (ipv6_addr_type(&sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
			sin6.sin6_scope_id = ifa->idev->dev->ifindex;
485 486
		svc_age_temp_xprts_now(nn->nfsd_serv, (struct sockaddr *)&sin6);
	}
487 488
	atomic_dec(&nn->ntf_refcnt);
	wake_up(&nn->ntf_wq);
489 490 491 492 493 494 495 496 497
out:
	return NOTIFY_DONE;
}

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

498 499 500
/* Only used under nfsd_mutex, so this atomic may be overkill: */
static atomic_t nfsd_notifier_refcount = ATOMIC_INIT(0);

501
static void nfsd_last_thread(struct svc_serv *serv, struct net *net)
502
{
503 504
	struct nfsd_net *nn = net_generic(net, nfsd_net_id);

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

515 516 517 518
	/*
	 * 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
519
	 * other initialization has been done except the rpcb information.
520
	 */
521
	svc_rpcb_cleanup(serv, net);
522
	if (!nn->nfsd_net_up)
523
		return;
524

525
	nfsd_shutdown_net(net);
526
	pr_info("nfsd: last server has exited, flushing export cache\n");
527
	nfsd_export_flush(net);
528
}
529

530
void nfsd_reset_versions(struct nfsd_net *nn)
531 532 533
{
	int i;

534
	for (i = 0; i < NFSD_NRVERS; i++)
535
		if (nfsd_vers(nn, i, NFSD_TEST))
536
			return;
537

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

A
Andy Adamson 已提交
548 549 550 551 552 553 554 555 556 557 558 559 560 561
/*
 * 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)
{
J
J. Bruce Fields 已提交
562
	#define NFSD_DRC_SIZE_SHIFT	7
563 564 565
	nfsd_drc_max_mem = (nr_free_buffer_pages()
					>> NFSD_DRC_SIZE_SHIFT) * PAGE_SIZE;
	nfsd_drc_mem_used = 0;
566
	spin_lock_init(&nfsd_drc_lock);
567
	dprintk("%s nfsd_drc_max_mem %lu \n", __func__, nfsd_drc_max_mem);
A
Andy Adamson 已提交
568
}
569

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

576
	si_meminfo(&i);
577
	target = (i.totalram - i.totalhigh) << PAGE_SHIFT;
578 579 580 581 582 583 584 585 586 587 588 589 590
	/*
	 * 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;
}

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

599
bool i_am_nfsd(void)
600 601 602 603
{
	return kthread_func(current) == nfsd;
}

604
int nfsd_create_serv(struct net *net)
605
{
606
	int error;
607
	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
608

609
	WARN_ON(!mutex_is_locked(&nfsd_mutex));
610 611
	if (nn->nfsd_serv) {
		svc_get(nn->nfsd_serv);
612 613
		return 0;
	}
614 615
	if (nfsd_max_blksize == 0)
		nfsd_max_blksize = nfsd_get_default_max_blksize();
616
	nfsd_reset_versions(nn);
617
	nn->nfsd_serv = svc_create_pooled(&nfsd_program, nfsd_max_blksize,
618
						&nfsd_thread_sv_ops);
619
	if (nn->nfsd_serv == NULL)
620
		return -ENOMEM;
621

622
	nn->nfsd_serv->sv_maxconn = nn->max_connections;
623
	error = svc_bind(nn->nfsd_serv, net);
624
	if (error < 0) {
625
		svc_destroy(nn->nfsd_serv);
626 627 628
		return error;
	}

629
	set_max_drc();
630 631 632
	/* check if the notifier is already set */
	if (atomic_inc_return(&nfsd_notifier_refcount) == 1) {
		register_inetaddr_notifier(&nfsd_inetaddr_notifier);
633
#if IS_ENABLED(CONFIG_IPV6)
634
		register_inet6addr_notifier(&nfsd_inet6addr_notifier);
635
#endif
636
	}
637
	atomic_inc(&nn->ntf_refcnt);
638
	nfsd_reset_boot_verifier(nn);
639
	return 0;
640 641
}

642
int nfsd_nrpools(struct net *net)
643
{
644 645 646
	struct nfsd_net *nn = net_generic(net, nfsd_net_id);

	if (nn->nfsd_serv == NULL)
647 648
		return 0;
	else
649
		return nn->nfsd_serv->sv_nrpools;
650 651
}

652
int nfsd_get_nrthreads(int n, int *nthreads, struct net *net)
653 654
{
	int i = 0;
655
	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
656

657 658 659
	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;
660 661 662 663 664
	}

	return 0;
}

665 666 667 668 669 670 671 672 673 674 675 676
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)
		nn->nfsd_serv = NULL;
}

677
int nfsd_set_nrthreads(int n, int *nthreads, struct net *net)
678 679 680 681
{
	int i = 0;
	int tot = 0;
	int err = 0;
682
	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
683

684 685
	WARN_ON(!mutex_is_locked(&nfsd_mutex));

686
	if (nn->nfsd_serv == NULL || n <= 0)
687 688
		return 0;

689 690
	if (n > nn->nfsd_serv->sv_nrpools)
		n = nn->nfsd_serv->sv_nrpools;
691 692 693 694

	/* enforce a global maximum number of threads */
	tot = 0;
	for (i = 0; i < n; i++) {
695
		nthreads[i] = min(nthreads[i], NFSD_MAXSERVS);
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718
		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 */
719
	svc_get(nn->nfsd_serv);
720
	for (i = 0; i < n; i++) {
721 722
		err = nn->nfsd_serv->sv_ops->svo_setup(nn->nfsd_serv,
				&nn->nfsd_serv->sv_pools[i], nthreads[i]);
723 724 725
		if (err)
			break;
	}
726
	nfsd_destroy(net);
727 728 729
	return err;
}

730 731 732 733 734
/*
 * 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.
 */
L
Linus Torvalds 已提交
735
int
736
nfsd_svc(int nrservs, struct net *net, const struct cred *cred)
L
Linus Torvalds 已提交
737 738
{
	int	error;
739
	bool	nfsd_up_before;
740
	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
741 742

	mutex_lock(&nfsd_mutex);
743
	dprintk("nfsd: creating service\n");
744 745 746

	nrservs = max(nrservs, 0);
	nrservs = min(nrservs, NFSD_MAXSERVS);
747
	error = 0;
748

749
	if (nrservs == 0 && nn->nfsd_serv == NULL)
750 751
		goto out;

752 753 754
	strlcpy(nn->nfsd_name, utsname()->nodename,
		sizeof(nn->nfsd_name));

755
	error = nfsd_create_serv(net);
756
	if (error)
757 758
		goto out;

759
	nfsd_up_before = nn->nfsd_net_up;
760

761
	error = nfsd_startup_net(nrservs, net, cred);
J
J. Bruce Fields 已提交
762 763
	if (error)
		goto out_destroy;
764 765
	error = nn->nfsd_serv->sv_ops->svo_setup(nn->nfsd_serv,
			NULL, nrservs);
766 767
	if (error)
		goto out_shutdown;
768
	/* We are holding a reference to nn->nfsd_serv which
J
J. Bruce Fields 已提交
769 770 771
	 * we don't want to count in the return value,
	 * so subtract 1
	 */
772
	error = nn->nfsd_serv->sv_nrthreads - 1;
773
out_shutdown:
774
	if (error < 0 && !nfsd_up_before)
775
		nfsd_shutdown_net(net);
776
out_destroy:
777
	nfsd_destroy(net);		/* Release server */
778
out:
779
	mutex_unlock(&nfsd_mutex);
L
Linus Torvalds 已提交
780 781 782
	return error;
}

783 784 785 786 787 788 789 790 791 792 793 794 795 796 797
#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) ||
798
	    !nfsd_vers(net_generic(net, nfsd_net_id), version, NFSD_TEST))
799 800 801 802
		return 0;
	return svc_generic_rpcbind_set(net, progp, version, family,
			proto, port);
}
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835

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;
}
836 837 838 839 840 841 842
#endif

static int
nfsd_rpcbind_set(struct net *net, const struct svc_program *progp,
		 u32 version, int family, unsigned short proto,
		 unsigned short port)
{
843
	if (!nfsd_vers(net_generic(net, nfsd_net_id), version, NFSD_TEST))
844 845 846 847
		return 0;
	return svc_generic_rpcbind_set(net, progp, version, family,
			proto, port);
}
L
Linus Torvalds 已提交
848

849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878
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;
}

L
Linus Torvalds 已提交
879 880 881
/*
 * This is the NFS server kernel thread
 */
882 883
static int
nfsd(void *vrqstp)
L
Linus Torvalds 已提交
884
{
885
	struct svc_rqst *rqstp = (struct svc_rqst *) vrqstp;
886 887
	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;
888
	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
889
	int err;
L
Linus Torvalds 已提交
890 891

	/* Lock module and set up kernel thread */
892
	mutex_lock(&nfsd_mutex);
L
Linus Torvalds 已提交
893

894
	/* At this point, the thread shares current->fs
895 896
	 * with the init process. We need to create files with the
	 * umask as defined by the client instead of init's umask. */
897
	if (unshare_fs_struct() < 0) {
L
Linus Torvalds 已提交
898 899 900
		printk("Unable to start nfsd thread: out of memory\n");
		goto out;
	}
901

L
Linus Torvalds 已提交
902 903
	current->fs->umask = 0;

904 905
	/*
	 * thread is spawned with all signals set to SIG_IGN, re-enable
906
	 * the ones that will bring down the thread
907
	 */
908 909 910 911
	allow_signal(SIGKILL);
	allow_signal(SIGHUP);
	allow_signal(SIGINT);
	allow_signal(SIGQUIT);
912

L
Linus Torvalds 已提交
913
	nfsdstats.th_cnt++;
914 915
	mutex_unlock(&nfsd_mutex);

916
	set_freezable();
L
Linus Torvalds 已提交
917 918 919 920 921

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

L
Linus Torvalds 已提交
925 926 927 928
		/*
		 * Find a socket with data available and call its
		 * recvfrom routine.
		 */
929
		while ((err = svc_recv(rqstp, 60*60*HZ)) == -EAGAIN)
L
Linus Torvalds 已提交
930
			;
931
		if (err == -EINTR)
L
Linus Torvalds 已提交
932
			break;
933
		validate_process_creds();
934
		svc_process(rqstp);
935
		validate_process_creds();
L
Linus Torvalds 已提交
936 937
	}

938
	/* Clear signals before calling svc_exit_thread() */
939
	flush_signals(current);
L
Linus Torvalds 已提交
940

941
	mutex_lock(&nfsd_mutex);
L
Linus Torvalds 已提交
942 943 944
	nfsdstats.th_cnt --;

out:
945
	rqstp->rq_server = NULL;
946

L
Linus Torvalds 已提交
947 948 949
	/* Release the thread */
	svc_exit_thread(rqstp);

950
	nfsd_destroy(net);
951

L
Linus Torvalds 已提交
952
	/* Release module */
953
	mutex_unlock(&nfsd_mutex);
L
Linus Torvalds 已提交
954
	module_put_and_exit(0);
955
	return 0;
L
Linus Torvalds 已提交
956 957
}

958 959 960 961 962 963 964 965 966 967 968 969
/**
 * 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)
L
Linus Torvalds 已提交
970
{
971 972 973
	const struct svc_procedure *proc = rqstp->rq_procinfo;
	struct kvec *argv = &rqstp->rq_arg.head[0];
	struct kvec *resv = &rqstp->rq_res.head[0];
974
	__be32 *p;
L
Linus Torvalds 已提交
975

976 977 978 979 980
	/*
	 * 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;
981 982

	svcxdr_init_decode(rqstp);
983 984
	if (!proc->pc_decode(rqstp, argv->iov_base))
		goto out_decode_err;
985 986

	switch (nfsd_cache_lookup(rqstp)) {
987 988
	case RC_DOIT:
		break;
L
Linus Torvalds 已提交
989
	case RC_REPLY:
990
		goto out_cached_reply;
991
	case RC_DROPIT:
992
		goto out_dropit;
L
Linus Torvalds 已提交
993 994
	}

995 996 997
	/*
	 * Need to grab the location to store the status, as
	 * NFSv4 does some encoding while processing
L
Linus Torvalds 已提交
998
	 */
999
	p = resv->iov_base + resv->iov_len;
1000
	svcxdr_init_encode(rqstp);
L
Linus Torvalds 已提交
1001

1002 1003
	*statp = proc->pc_func(rqstp);
	if (*statp == rpc_drop_reply || test_bit(RQ_DROPME, &rqstp->rq_flags))
1004
		goto out_update_drop;
L
Linus Torvalds 已提交
1005

1006
	if (!proc->pc_encode(rqstp, p))
1007
		goto out_encode_err;
L
Linus Torvalds 已提交
1008

J
J. Bruce Fields 已提交
1009
	nfsd_cache_update(rqstp, rqstp->rq_cachetype, statp + 1);
1010 1011 1012 1013
out_cached_reply:
	return 1;

out_decode_err:
1014
	trace_nfsd_garbage_args_err(rqstp);
1015 1016 1017 1018 1019 1020 1021 1022 1023
	*statp = rpc_garbage_args;
	return 1;

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

out_encode_err:
1024
	trace_nfsd_cant_encode_err(rqstp);
1025 1026
	nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
	*statp = rpc_system_err;
L
Linus Torvalds 已提交
1027 1028
	return 1;
}
1029

1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
/**
 * nfssvc_decode_voidarg - Decode void arguments
 * @rqstp: Server RPC transaction context
 * @p: buffer containing arguments to decode
 *
 * Return values:
 *   %0: Arguments were not valid
 *   %1: Decoding was successful
 */
int nfssvc_decode_voidarg(struct svc_rqst *rqstp, __be32 *p)
{
	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)
{
1055
	return 1;
1056 1057
}

1058 1059
int nfsd_pool_stats_open(struct inode *inode, struct file *file)
{
1060
	int ret;
1061
	struct nfsd_net *nn = net_generic(inode->i_sb->s_fs_info, nfsd_net_id);
1062

1063
	mutex_lock(&nfsd_mutex);
1064
	if (nn->nfsd_serv == NULL) {
1065
		mutex_unlock(&nfsd_mutex);
1066
		return -ENODEV;
1067 1068
	}
	/* bump up the psudo refcount while traversing */
1069 1070
	svc_get(nn->nfsd_serv);
	ret = svc_pool_stats_open(nn->nfsd_serv, file);
1071 1072 1073 1074 1075 1076 1077
	mutex_unlock(&nfsd_mutex);
	return ret;
}

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

1080 1081
	mutex_lock(&nfsd_mutex);
	/* this function really, really should have been called svc_put() */
1082
	nfsd_destroy(net);
1083 1084
	mutex_unlock(&nfsd_mutex);
	return ret;
1085
}