xref: /linux/fs/nfsd/nfssvc.c (revision 1e123fd73deb16cb362ecefb55c90c9196f4a6c2)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Central processing for nfsd.
4  *
5  * Authors:	Olaf Kirch (okir@monad.swb.de)
6  *
7  * Copyright (C) 1995, 1996, 1997 Olaf Kirch <okir@monad.swb.de>
8  */
9 
10 #include <linux/sched/signal.h>
11 #include <linux/freezer.h>
12 #include <linux/module.h>
13 #include <linux/fs_struct.h>
14 #include <linux/swap.h>
15 #include <linux/siphash.h>
16 
17 #include <linux/sunrpc/stats.h>
18 #include <linux/sunrpc/svcsock.h>
19 #include <linux/sunrpc/svc_xprt.h>
20 #include <linux/lockd/bind.h>
21 #include <linux/nfsacl.h>
22 #include <linux/seq_file.h>
23 #include <linux/inetdevice.h>
24 #include <net/addrconf.h>
25 #include <net/ipv6.h>
26 #include <net/net_namespace.h>
27 #include "nfsd.h"
28 #include "cache.h"
29 #include "vfs.h"
30 #include "netns.h"
31 #include "filecache.h"
32 
33 #include "trace.h"
34 
35 #define NFSDDBG_FACILITY	NFSDDBG_SVC
36 
37 atomic_t			nfsd_th_cnt = ATOMIC_INIT(0);
38 extern struct svc_program	nfsd_program;
39 static int			nfsd(void *vrqstp);
40 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
41 static int			nfsd_acl_rpcbind_set(struct net *,
42 						     const struct svc_program *,
43 						     u32, int,
44 						     unsigned short,
45 						     unsigned short);
46 static __be32			nfsd_acl_init_request(struct svc_rqst *,
47 						const struct svc_program *,
48 						struct svc_process_info *);
49 #endif
50 static int			nfsd_rpcbind_set(struct net *,
51 						 const struct svc_program *,
52 						 u32, int,
53 						 unsigned short,
54 						 unsigned short);
55 static __be32			nfsd_init_request(struct svc_rqst *,
56 						const struct svc_program *,
57 						struct svc_process_info *);
58 
59 /*
60  * nfsd_mutex protects nn->nfsd_serv -- both the pointer itself and some members
61  * of the svc_serv struct such as ->sv_temp_socks and ->sv_permsocks.
62  *
63  * Finally, the nfsd_mutex also protects some of the global variables that are
64  * accessed when nfsd starts and that are settable via the write_* routines in
65  * nfsctl.c. In particular:
66  *
67  *	user_recovery_dirname
68  *	user_lease_time
69  *	nfsd_versions
70  */
71 DEFINE_MUTEX(nfsd_mutex);
72 
73 /*
74  * nfsd_drc_lock protects nfsd_drc_max_pages and nfsd_drc_pages_used.
75  * nfsd_drc_max_pages limits the total amount of memory available for
76  * version 4.1 DRC caches.
77  * nfsd_drc_pages_used tracks the current version 4.1 DRC memory usage.
78  */
79 DEFINE_SPINLOCK(nfsd_drc_lock);
80 unsigned long	nfsd_drc_max_mem;
81 unsigned long	nfsd_drc_mem_used;
82 
83 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
84 static const struct svc_version *nfsd_acl_version[] = {
85 # if defined(CONFIG_NFSD_V2_ACL)
86 	[2] = &nfsd_acl_version2,
87 # endif
88 # if defined(CONFIG_NFSD_V3_ACL)
89 	[3] = &nfsd_acl_version3,
90 # endif
91 };
92 
93 #define NFSD_ACL_MINVERS            2
94 #define NFSD_ACL_NRVERS		ARRAY_SIZE(nfsd_acl_version)
95 
96 static struct svc_program	nfsd_acl_program = {
97 	.pg_prog		= NFS_ACL_PROGRAM,
98 	.pg_nvers		= NFSD_ACL_NRVERS,
99 	.pg_vers		= nfsd_acl_version,
100 	.pg_name		= "nfsacl",
101 	.pg_class		= "nfsd",
102 	.pg_authenticate	= &svc_set_client,
103 	.pg_init_request	= nfsd_acl_init_request,
104 	.pg_rpcbind_set		= nfsd_acl_rpcbind_set,
105 };
106 
107 #endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */
108 
109 static const struct svc_version *nfsd_version[NFSD_MAXVERS+1] = {
110 #if defined(CONFIG_NFSD_V2)
111 	[2] = &nfsd_version2,
112 #endif
113 	[3] = &nfsd_version3,
114 #if defined(CONFIG_NFSD_V4)
115 	[4] = &nfsd_version4,
116 #endif
117 };
118 
119 struct svc_program		nfsd_program = {
120 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
121 	.pg_next		= &nfsd_acl_program,
122 #endif
123 	.pg_prog		= NFS_PROGRAM,		/* program number */
124 	.pg_nvers		= NFSD_MAXVERS+1,	/* nr of entries in nfsd_version */
125 	.pg_vers		= nfsd_version,		/* version table */
126 	.pg_name		= "nfsd",		/* program name */
127 	.pg_class		= "nfsd",		/* authentication class */
128 	.pg_authenticate	= &svc_set_client,	/* export authentication */
129 	.pg_init_request	= nfsd_init_request,
130 	.pg_rpcbind_set		= nfsd_rpcbind_set,
131 };
132 
133 bool nfsd_support_version(int vers)
134 {
135 	if (vers >= NFSD_MINVERS && vers <= NFSD_MAXVERS)
136 		return nfsd_version[vers] != NULL;
137 	return false;
138 }
139 
140 int nfsd_vers(struct nfsd_net *nn, int vers, enum vers_op change)
141 {
142 	if (vers < NFSD_MINVERS || vers > NFSD_MAXVERS)
143 		return 0;
144 	switch(change) {
145 	case NFSD_SET:
146 		nn->nfsd_versions[vers] = nfsd_support_version(vers);
147 		break;
148 	case NFSD_CLEAR:
149 		nn->nfsd_versions[vers] = false;
150 		break;
151 	case NFSD_TEST:
152 		return nn->nfsd_versions[vers];
153 	case NFSD_AVAIL:
154 		return nfsd_support_version(vers);
155 	}
156 	return 0;
157 }
158 
159 static void
160 nfsd_adjust_nfsd_versions4(struct nfsd_net *nn)
161 {
162 	unsigned i;
163 
164 	for (i = 0; i <= NFSD_SUPPORTED_MINOR_VERSION; i++) {
165 		if (nn->nfsd4_minorversions[i])
166 			return;
167 	}
168 	nfsd_vers(nn, 4, NFSD_CLEAR);
169 }
170 
171 int nfsd_minorversion(struct nfsd_net *nn, u32 minorversion, enum vers_op change)
172 {
173 	if (minorversion > NFSD_SUPPORTED_MINOR_VERSION &&
174 	    change != NFSD_AVAIL)
175 		return -1;
176 
177 	switch(change) {
178 	case NFSD_SET:
179 		nfsd_vers(nn, 4, NFSD_SET);
180 		nn->nfsd4_minorversions[minorversion] =
181 			nfsd_vers(nn, 4, NFSD_TEST);
182 		break;
183 	case NFSD_CLEAR:
184 		nn->nfsd4_minorversions[minorversion] = false;
185 		nfsd_adjust_nfsd_versions4(nn);
186 		break;
187 	case NFSD_TEST:
188 		return nn->nfsd4_minorversions[minorversion];
189 	case NFSD_AVAIL:
190 		return minorversion <= NFSD_SUPPORTED_MINOR_VERSION &&
191 			nfsd_vers(nn, 4, NFSD_AVAIL);
192 	}
193 	return 0;
194 }
195 
196 /*
197  * Maximum number of nfsd processes
198  */
199 #define	NFSD_MAXSERVS		8192
200 
201 int nfsd_nrthreads(struct net *net)
202 {
203 	int rv = 0;
204 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
205 
206 	mutex_lock(&nfsd_mutex);
207 	if (nn->nfsd_serv)
208 		rv = nn->nfsd_serv->sv_nrthreads;
209 	mutex_unlock(&nfsd_mutex);
210 	return rv;
211 }
212 
213 static int nfsd_init_socks(struct net *net, const struct cred *cred)
214 {
215 	int error;
216 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
217 
218 	if (!list_empty(&nn->nfsd_serv->sv_permsocks))
219 		return 0;
220 
221 	error = svc_xprt_create(nn->nfsd_serv, "udp", net, PF_INET, NFS_PORT,
222 				SVC_SOCK_DEFAULTS, cred);
223 	if (error < 0)
224 		return error;
225 
226 	error = svc_xprt_create(nn->nfsd_serv, "tcp", net, PF_INET, NFS_PORT,
227 				SVC_SOCK_DEFAULTS, cred);
228 	if (error < 0)
229 		return error;
230 
231 	return 0;
232 }
233 
234 static int nfsd_users = 0;
235 
236 static int nfsd_startup_generic(void)
237 {
238 	int ret;
239 
240 	if (nfsd_users++)
241 		return 0;
242 
243 	ret = nfsd_file_cache_init();
244 	if (ret)
245 		goto dec_users;
246 
247 	ret = nfs4_state_start();
248 	if (ret)
249 		goto out_file_cache;
250 	return 0;
251 
252 out_file_cache:
253 	nfsd_file_cache_shutdown();
254 dec_users:
255 	nfsd_users--;
256 	return ret;
257 }
258 
259 static void nfsd_shutdown_generic(void)
260 {
261 	if (--nfsd_users)
262 		return;
263 
264 	nfs4_state_shutdown();
265 	nfsd_file_cache_shutdown();
266 }
267 
268 static bool nfsd_needs_lockd(struct nfsd_net *nn)
269 {
270 	return nfsd_vers(nn, 2, NFSD_TEST) || nfsd_vers(nn, 3, NFSD_TEST);
271 }
272 
273 /**
274  * nfsd_copy_write_verifier - Atomically copy a write verifier
275  * @verf: buffer in which to receive the verifier cookie
276  * @nn: NFS net namespace
277  *
278  * This function provides a wait-free mechanism for copying the
279  * namespace's write verifier without tearing it.
280  */
281 void nfsd_copy_write_verifier(__be32 verf[2], struct nfsd_net *nn)
282 {
283 	unsigned int seq;
284 
285 	do {
286 		seq = read_seqbegin(&nn->writeverf_lock);
287 		memcpy(verf, nn->writeverf, sizeof(nn->writeverf));
288 	} while (read_seqretry(&nn->writeverf_lock, seq));
289 }
290 
291 static void nfsd_reset_write_verifier_locked(struct nfsd_net *nn)
292 {
293 	struct timespec64 now;
294 	u64 verf;
295 
296 	/*
297 	 * Because the time value is hashed, y2038 time_t overflow
298 	 * is irrelevant in this usage.
299 	 */
300 	ktime_get_raw_ts64(&now);
301 	verf = siphash_2u64(now.tv_sec, now.tv_nsec, &nn->siphash_key);
302 	memcpy(nn->writeverf, &verf, sizeof(nn->writeverf));
303 }
304 
305 /**
306  * nfsd_reset_write_verifier - Generate a new write verifier
307  * @nn: NFS net namespace
308  *
309  * This function updates the ->writeverf field of @nn. This field
310  * contains an opaque cookie that, according to Section 18.32.3 of
311  * RFC 8881, "the client can use to determine whether a server has
312  * changed instance state (e.g., server restart) between a call to
313  * WRITE and a subsequent call to either WRITE or COMMIT.  This
314  * cookie MUST be unchanged during a single instance of the NFSv4.1
315  * server and MUST be unique between instances of the NFSv4.1
316  * server."
317  */
318 void nfsd_reset_write_verifier(struct nfsd_net *nn)
319 {
320 	write_seqlock(&nn->writeverf_lock);
321 	nfsd_reset_write_verifier_locked(nn);
322 	write_sequnlock(&nn->writeverf_lock);
323 }
324 
325 /*
326  * Crank up a set of per-namespace resources for a new NFSD instance,
327  * including lockd, a duplicate reply cache, an open file cache
328  * instance, and a cache of NFSv4 state objects.
329  */
330 static int nfsd_startup_net(struct net *net, const struct cred *cred)
331 {
332 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
333 	int ret;
334 
335 	if (nn->nfsd_net_up)
336 		return 0;
337 
338 	ret = nfsd_startup_generic();
339 	if (ret)
340 		return ret;
341 	ret = nfsd_init_socks(net, cred);
342 	if (ret)
343 		goto out_socks;
344 
345 	if (nfsd_needs_lockd(nn) && !nn->lockd_up) {
346 		ret = lockd_up(net, cred);
347 		if (ret)
348 			goto out_socks;
349 		nn->lockd_up = true;
350 	}
351 
352 	ret = nfsd_file_cache_start_net(net);
353 	if (ret)
354 		goto out_lockd;
355 
356 	ret = nfsd_reply_cache_init(nn);
357 	if (ret)
358 		goto out_filecache;
359 
360 	ret = nfs4_state_start_net(net);
361 	if (ret)
362 		goto out_reply_cache;
363 
364 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
365 	nfsd4_ssc_init_umount_work(nn);
366 #endif
367 	nn->nfsd_net_up = true;
368 	return 0;
369 
370 out_reply_cache:
371 	nfsd_reply_cache_shutdown(nn);
372 out_filecache:
373 	nfsd_file_cache_shutdown_net(net);
374 out_lockd:
375 	if (nn->lockd_up) {
376 		lockd_down(net);
377 		nn->lockd_up = false;
378 	}
379 out_socks:
380 	nfsd_shutdown_generic();
381 	return ret;
382 }
383 
384 static void nfsd_shutdown_net(struct net *net)
385 {
386 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
387 
388 	nfs4_state_shutdown_net(net);
389 	nfsd_reply_cache_shutdown(nn);
390 	nfsd_file_cache_shutdown_net(net);
391 	if (nn->lockd_up) {
392 		lockd_down(net);
393 		nn->lockd_up = false;
394 	}
395 	nn->nfsd_net_up = false;
396 	nfsd_shutdown_generic();
397 }
398 
399 static DEFINE_SPINLOCK(nfsd_notifier_lock);
400 static int nfsd_inetaddr_event(struct notifier_block *this, unsigned long event,
401 	void *ptr)
402 {
403 	struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
404 	struct net_device *dev = ifa->ifa_dev->dev;
405 	struct net *net = dev_net(dev);
406 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
407 	struct sockaddr_in sin;
408 
409 	if (event != NETDEV_DOWN || !nn->nfsd_serv)
410 		goto out;
411 
412 	spin_lock(&nfsd_notifier_lock);
413 	if (nn->nfsd_serv) {
414 		dprintk("nfsd_inetaddr_event: removed %pI4\n", &ifa->ifa_local);
415 		sin.sin_family = AF_INET;
416 		sin.sin_addr.s_addr = ifa->ifa_local;
417 		svc_age_temp_xprts_now(nn->nfsd_serv, (struct sockaddr *)&sin);
418 	}
419 	spin_unlock(&nfsd_notifier_lock);
420 
421 out:
422 	return NOTIFY_DONE;
423 }
424 
425 static struct notifier_block nfsd_inetaddr_notifier = {
426 	.notifier_call = nfsd_inetaddr_event,
427 };
428 
429 #if IS_ENABLED(CONFIG_IPV6)
430 static int nfsd_inet6addr_event(struct notifier_block *this,
431 	unsigned long event, void *ptr)
432 {
433 	struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
434 	struct net_device *dev = ifa->idev->dev;
435 	struct net *net = dev_net(dev);
436 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
437 	struct sockaddr_in6 sin6;
438 
439 	if (event != NETDEV_DOWN || !nn->nfsd_serv)
440 		goto out;
441 
442 	spin_lock(&nfsd_notifier_lock);
443 	if (nn->nfsd_serv) {
444 		dprintk("nfsd_inet6addr_event: removed %pI6\n", &ifa->addr);
445 		sin6.sin6_family = AF_INET6;
446 		sin6.sin6_addr = ifa->addr;
447 		if (ipv6_addr_type(&sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
448 			sin6.sin6_scope_id = ifa->idev->dev->ifindex;
449 		svc_age_temp_xprts_now(nn->nfsd_serv, (struct sockaddr *)&sin6);
450 	}
451 	spin_unlock(&nfsd_notifier_lock);
452 
453 out:
454 	return NOTIFY_DONE;
455 }
456 
457 static struct notifier_block nfsd_inet6addr_notifier = {
458 	.notifier_call = nfsd_inet6addr_event,
459 };
460 #endif
461 
462 /* Only used under nfsd_mutex, so this atomic may be overkill: */
463 static atomic_t nfsd_notifier_refcount = ATOMIC_INIT(0);
464 
465 /**
466  * nfsd_destroy_serv - tear down NFSD's svc_serv for a namespace
467  * @net: network namespace the NFS service is associated with
468  */
469 void nfsd_destroy_serv(struct net *net)
470 {
471 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
472 	struct svc_serv *serv = nn->nfsd_serv;
473 
474 	spin_lock(&nfsd_notifier_lock);
475 	nn->nfsd_serv = NULL;
476 	spin_unlock(&nfsd_notifier_lock);
477 
478 	/* check if the notifier still has clients */
479 	if (atomic_dec_return(&nfsd_notifier_refcount) == 0) {
480 		unregister_inetaddr_notifier(&nfsd_inetaddr_notifier);
481 #if IS_ENABLED(CONFIG_IPV6)
482 		unregister_inet6addr_notifier(&nfsd_inet6addr_notifier);
483 #endif
484 	}
485 
486 	svc_xprt_destroy_all(serv, net);
487 
488 	/*
489 	 * write_ports can create the server without actually starting
490 	 * any threads--if we get shut down before any threads are
491 	 * started, then nfsd_destroy_serv will be run before any of this
492 	 * other initialization has been done except the rpcb information.
493 	 */
494 	svc_rpcb_cleanup(serv, net);
495 	if (!nn->nfsd_net_up)
496 		return;
497 
498 	nfsd_shutdown_net(net);
499 	nfsd_export_flush(net);
500 	svc_destroy(&serv);
501 }
502 
503 void nfsd_reset_versions(struct nfsd_net *nn)
504 {
505 	int i;
506 
507 	for (i = 0; i <= NFSD_MAXVERS; i++)
508 		if (nfsd_vers(nn, i, NFSD_TEST))
509 			return;
510 
511 	for (i = 0; i <= NFSD_MAXVERS; i++)
512 		if (i != 4)
513 			nfsd_vers(nn, i, NFSD_SET);
514 		else {
515 			int minor = 0;
516 			while (nfsd_minorversion(nn, minor, NFSD_SET) >= 0)
517 				minor++;
518 		}
519 }
520 
521 /*
522  * Each session guarantees a negotiated per slot memory cache for replies
523  * which in turn consumes memory beyond the v2/v3/v4.0 server. A dedicated
524  * NFSv4.1 server might want to use more memory for a DRC than a machine
525  * with mutiple services.
526  *
527  * Impose a hard limit on the number of pages for the DRC which varies
528  * according to the machines free pages. This is of course only a default.
529  *
530  * For now this is a #defined shift which could be under admin control
531  * in the future.
532  */
533 static void set_max_drc(void)
534 {
535 	#define NFSD_DRC_SIZE_SHIFT	7
536 	nfsd_drc_max_mem = (nr_free_buffer_pages()
537 					>> NFSD_DRC_SIZE_SHIFT) * PAGE_SIZE;
538 	nfsd_drc_mem_used = 0;
539 	dprintk("%s nfsd_drc_max_mem %lu \n", __func__, nfsd_drc_max_mem);
540 }
541 
542 static int nfsd_get_default_max_blksize(void)
543 {
544 	struct sysinfo i;
545 	unsigned long long target;
546 	unsigned long ret;
547 
548 	si_meminfo(&i);
549 	target = (i.totalram - i.totalhigh) << PAGE_SHIFT;
550 	/*
551 	 * Aim for 1/4096 of memory per thread This gives 1MB on 4Gig
552 	 * machines, but only uses 32K on 128M machines.  Bottom out at
553 	 * 8K on 32M and smaller.  Of course, this is only a default.
554 	 */
555 	target >>= 12;
556 
557 	ret = NFSSVC_MAXBLKSIZE;
558 	while (ret > target && ret >= 8*1024*2)
559 		ret /= 2;
560 	return ret;
561 }
562 
563 void nfsd_shutdown_threads(struct net *net)
564 {
565 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
566 	struct svc_serv *serv;
567 
568 	mutex_lock(&nfsd_mutex);
569 	serv = nn->nfsd_serv;
570 	if (serv == NULL) {
571 		mutex_unlock(&nfsd_mutex);
572 		return;
573 	}
574 
575 	/* Kill outstanding nfsd threads */
576 	svc_set_num_threads(serv, NULL, 0);
577 	nfsd_destroy_serv(net);
578 	mutex_unlock(&nfsd_mutex);
579 }
580 
581 struct svc_rqst *nfsd_current_rqst(void)
582 {
583 	if (kthread_func(current) == nfsd)
584 		return kthread_data(current);
585 	return NULL;
586 }
587 
588 int nfsd_create_serv(struct net *net)
589 {
590 	int error;
591 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
592 	struct svc_serv *serv;
593 
594 	WARN_ON(!mutex_is_locked(&nfsd_mutex));
595 	if (nn->nfsd_serv)
596 		return 0;
597 
598 	if (nfsd_max_blksize == 0)
599 		nfsd_max_blksize = nfsd_get_default_max_blksize();
600 	nfsd_reset_versions(nn);
601 	serv = svc_create_pooled(&nfsd_program, &nn->nfsd_svcstats,
602 				 nfsd_max_blksize, nfsd);
603 	if (serv == NULL)
604 		return -ENOMEM;
605 
606 	serv->sv_maxconn = nn->max_connections;
607 	error = svc_bind(serv, net);
608 	if (error < 0) {
609 		svc_destroy(&serv);
610 		return error;
611 	}
612 	spin_lock(&nfsd_notifier_lock);
613 	nn->nfsd_serv = serv;
614 	spin_unlock(&nfsd_notifier_lock);
615 
616 	set_max_drc();
617 	/* check if the notifier is already set */
618 	if (atomic_inc_return(&nfsd_notifier_refcount) == 1) {
619 		register_inetaddr_notifier(&nfsd_inetaddr_notifier);
620 #if IS_ENABLED(CONFIG_IPV6)
621 		register_inet6addr_notifier(&nfsd_inet6addr_notifier);
622 #endif
623 	}
624 	nfsd_reset_write_verifier(nn);
625 	return 0;
626 }
627 
628 int nfsd_nrpools(struct net *net)
629 {
630 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
631 
632 	if (nn->nfsd_serv == NULL)
633 		return 0;
634 	else
635 		return nn->nfsd_serv->sv_nrpools;
636 }
637 
638 int nfsd_get_nrthreads(int n, int *nthreads, struct net *net)
639 {
640 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
641 	struct svc_serv *serv = nn->nfsd_serv;
642 	int i;
643 
644 	if (serv)
645 		for (i = 0; i < serv->sv_nrpools && i < n; i++)
646 			nthreads[i] = serv->sv_pools[i].sp_nrthreads;
647 	return 0;
648 }
649 
650 /**
651  * nfsd_set_nrthreads - set the number of running threads in the net's service
652  * @n: number of array members in @nthreads
653  * @nthreads: array of thread counts for each pool
654  * @net: network namespace to operate within
655  *
656  * This function alters the number of running threads for the given network
657  * namespace in each pool. If passed an array longer then the number of pools
658  * the extra pool settings are ignored. If passed an array shorter than the
659  * number of pools, the missing values are interpreted as 0's.
660  *
661  * Returns 0 on success or a negative errno on error.
662  */
663 int nfsd_set_nrthreads(int n, int *nthreads, struct net *net)
664 {
665 	int i = 0;
666 	int tot = 0;
667 	int err = 0;
668 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
669 
670 	lockdep_assert_held(&nfsd_mutex);
671 
672 	if (nn->nfsd_serv == NULL || n <= 0)
673 		return 0;
674 
675 	/*
676 	 * Special case: When n == 1, pass in NULL for the pool, so that the
677 	 * change is distributed equally among them.
678 	 */
679 	if (n == 1)
680 		return svc_set_num_threads(nn->nfsd_serv, NULL, nthreads[0]);
681 
682 	if (n > nn->nfsd_serv->sv_nrpools)
683 		n = nn->nfsd_serv->sv_nrpools;
684 
685 	/* enforce a global maximum number of threads */
686 	tot = 0;
687 	for (i = 0; i < n; i++) {
688 		nthreads[i] = min(nthreads[i], NFSD_MAXSERVS);
689 		tot += nthreads[i];
690 	}
691 	if (tot > NFSD_MAXSERVS) {
692 		/* total too large: scale down requested numbers */
693 		for (i = 0; i < n && tot > 0; i++) {
694 			int new = nthreads[i] * NFSD_MAXSERVS / tot;
695 			tot -= (nthreads[i] - new);
696 			nthreads[i] = new;
697 		}
698 		for (i = 0; i < n && tot > 0; i++) {
699 			nthreads[i]--;
700 			tot--;
701 		}
702 	}
703 
704 	/* apply the new numbers */
705 	for (i = 0; i < n; i++) {
706 		err = svc_set_num_threads(nn->nfsd_serv,
707 					  &nn->nfsd_serv->sv_pools[i],
708 					  nthreads[i]);
709 		if (err)
710 			goto out;
711 	}
712 
713 	/* Anything undefined in array is considered to be 0 */
714 	for (i = n; i < nn->nfsd_serv->sv_nrpools; ++i) {
715 		err = svc_set_num_threads(nn->nfsd_serv,
716 					  &nn->nfsd_serv->sv_pools[i],
717 					  0);
718 		if (err)
719 			goto out;
720 	}
721 out:
722 	return err;
723 }
724 
725 /**
726  * nfsd_svc: start up or shut down the nfsd server
727  * @n: number of array members in @nthreads
728  * @nthreads: array of thread counts for each pool
729  * @net: network namespace to operate within
730  * @cred: credentials to use for xprt creation
731  * @scope: server scope value (defaults to nodename)
732  *
733  * Adjust the number of threads in each pool and return the new
734  * total number of threads in the service.
735  */
736 int
737 nfsd_svc(int n, int *nthreads, struct net *net, const struct cred *cred, const char *scope)
738 {
739 	int	error;
740 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
741 	struct svc_serv *serv;
742 
743 	lockdep_assert_held(&nfsd_mutex);
744 
745 	dprintk("nfsd: creating service\n");
746 
747 	strscpy(nn->nfsd_name, scope ? scope : utsname()->nodename,
748 		sizeof(nn->nfsd_name));
749 
750 	error = nfsd_create_serv(net);
751 	if (error)
752 		goto out;
753 	serv = nn->nfsd_serv;
754 
755 	error = nfsd_startup_net(net, cred);
756 	if (error)
757 		goto out_put;
758 	error = nfsd_set_nrthreads(n, nthreads, net);
759 	if (error)
760 		goto out_put;
761 	error = serv->sv_nrthreads;
762 out_put:
763 	if (serv->sv_nrthreads == 0)
764 		nfsd_destroy_serv(net);
765 out:
766 	return error;
767 }
768 
769 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
770 static bool
771 nfsd_support_acl_version(int vers)
772 {
773 	if (vers >= NFSD_ACL_MINVERS && vers < NFSD_ACL_NRVERS)
774 		return nfsd_acl_version[vers] != NULL;
775 	return false;
776 }
777 
778 static int
779 nfsd_acl_rpcbind_set(struct net *net, const struct svc_program *progp,
780 		     u32 version, int family, unsigned short proto,
781 		     unsigned short port)
782 {
783 	if (!nfsd_support_acl_version(version) ||
784 	    !nfsd_vers(net_generic(net, nfsd_net_id), version, NFSD_TEST))
785 		return 0;
786 	return svc_generic_rpcbind_set(net, progp, version, family,
787 			proto, port);
788 }
789 
790 static __be32
791 nfsd_acl_init_request(struct svc_rqst *rqstp,
792 		      const struct svc_program *progp,
793 		      struct svc_process_info *ret)
794 {
795 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
796 	int i;
797 
798 	if (likely(nfsd_support_acl_version(rqstp->rq_vers) &&
799 	    nfsd_vers(nn, rqstp->rq_vers, NFSD_TEST)))
800 		return svc_generic_init_request(rqstp, progp, ret);
801 
802 	ret->mismatch.lovers = NFSD_ACL_NRVERS;
803 	for (i = NFSD_ACL_MINVERS; i < NFSD_ACL_NRVERS; i++) {
804 		if (nfsd_support_acl_version(rqstp->rq_vers) &&
805 		    nfsd_vers(nn, i, NFSD_TEST)) {
806 			ret->mismatch.lovers = i;
807 			break;
808 		}
809 	}
810 	if (ret->mismatch.lovers == NFSD_ACL_NRVERS)
811 		return rpc_prog_unavail;
812 	ret->mismatch.hivers = NFSD_ACL_MINVERS;
813 	for (i = NFSD_ACL_NRVERS - 1; i >= NFSD_ACL_MINVERS; i--) {
814 		if (nfsd_support_acl_version(rqstp->rq_vers) &&
815 		    nfsd_vers(nn, i, NFSD_TEST)) {
816 			ret->mismatch.hivers = i;
817 			break;
818 		}
819 	}
820 	return rpc_prog_mismatch;
821 }
822 #endif
823 
824 static int
825 nfsd_rpcbind_set(struct net *net, const struct svc_program *progp,
826 		 u32 version, int family, unsigned short proto,
827 		 unsigned short port)
828 {
829 	if (!nfsd_vers(net_generic(net, nfsd_net_id), version, NFSD_TEST))
830 		return 0;
831 	return svc_generic_rpcbind_set(net, progp, version, family,
832 			proto, port);
833 }
834 
835 static __be32
836 nfsd_init_request(struct svc_rqst *rqstp,
837 		  const struct svc_program *progp,
838 		  struct svc_process_info *ret)
839 {
840 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
841 	int i;
842 
843 	if (likely(nfsd_vers(nn, rqstp->rq_vers, NFSD_TEST)))
844 		return svc_generic_init_request(rqstp, progp, ret);
845 
846 	ret->mismatch.lovers = NFSD_MAXVERS + 1;
847 	for (i = NFSD_MINVERS; i <= NFSD_MAXVERS; i++) {
848 		if (nfsd_vers(nn, i, NFSD_TEST)) {
849 			ret->mismatch.lovers = i;
850 			break;
851 		}
852 	}
853 	if (ret->mismatch.lovers > NFSD_MAXVERS)
854 		return rpc_prog_unavail;
855 	ret->mismatch.hivers = NFSD_MINVERS;
856 	for (i = NFSD_MAXVERS; i >= NFSD_MINVERS; i--) {
857 		if (nfsd_vers(nn, i, NFSD_TEST)) {
858 			ret->mismatch.hivers = i;
859 			break;
860 		}
861 	}
862 	return rpc_prog_mismatch;
863 }
864 
865 /*
866  * This is the NFS server kernel thread
867  */
868 static int
869 nfsd(void *vrqstp)
870 {
871 	struct svc_rqst *rqstp = (struct svc_rqst *) vrqstp;
872 	struct svc_xprt *perm_sock = list_entry(rqstp->rq_server->sv_permsocks.next, typeof(struct svc_xprt), xpt_list);
873 	struct net *net = perm_sock->xpt_net;
874 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
875 
876 	/* At this point, the thread shares current->fs
877 	 * with the init process. We need to create files with the
878 	 * umask as defined by the client instead of init's umask.
879 	 */
880 	svc_thread_init_status(rqstp, unshare_fs_struct());
881 
882 	current->fs->umask = 0;
883 
884 	atomic_inc(&nfsd_th_cnt);
885 
886 	set_freezable();
887 
888 	/*
889 	 * The main request loop
890 	 */
891 	while (!svc_thread_should_stop(rqstp)) {
892 		/* Update sv_maxconn if it has changed */
893 		rqstp->rq_server->sv_maxconn = nn->max_connections;
894 
895 		svc_recv(rqstp);
896 
897 		nfsd_file_net_dispose(nn);
898 	}
899 
900 	atomic_dec(&nfsd_th_cnt);
901 
902 	/* Release the thread */
903 	svc_exit_thread(rqstp);
904 	return 0;
905 }
906 
907 /**
908  * nfsd_dispatch - Process an NFS or NFSACL Request
909  * @rqstp: incoming request
910  *
911  * This RPC dispatcher integrates the NFS server's duplicate reply cache.
912  *
913  * Return values:
914  *  %0: Processing complete; do not send a Reply
915  *  %1: Processing complete; send Reply in rqstp->rq_res
916  */
917 int nfsd_dispatch(struct svc_rqst *rqstp)
918 {
919 	const struct svc_procedure *proc = rqstp->rq_procinfo;
920 	__be32 *statp = rqstp->rq_accept_statp;
921 	struct nfsd_cacherep *rp;
922 	unsigned int start, len;
923 	__be32 *nfs_reply;
924 
925 	/*
926 	 * Give the xdr decoder a chance to change this if it wants
927 	 * (necessary in the NFSv4.0 compound case)
928 	 */
929 	rqstp->rq_cachetype = proc->pc_cachetype;
930 
931 	/*
932 	 * ->pc_decode advances the argument stream past the NFS
933 	 * Call header, so grab the header's starting location and
934 	 * size now for the call to nfsd_cache_lookup().
935 	 */
936 	start = xdr_stream_pos(&rqstp->rq_arg_stream);
937 	len = xdr_stream_remaining(&rqstp->rq_arg_stream);
938 	if (!proc->pc_decode(rqstp, &rqstp->rq_arg_stream))
939 		goto out_decode_err;
940 
941 	/*
942 	 * Release rq_status_counter setting it to an odd value after the rpc
943 	 * request has been properly parsed. rq_status_counter is used to
944 	 * notify the consumers if the rqstp fields are stable
945 	 * (rq_status_counter is odd) or not meaningful (rq_status_counter
946 	 * is even).
947 	 */
948 	smp_store_release(&rqstp->rq_status_counter, rqstp->rq_status_counter | 1);
949 
950 	rp = NULL;
951 	switch (nfsd_cache_lookup(rqstp, start, len, &rp)) {
952 	case RC_DOIT:
953 		break;
954 	case RC_REPLY:
955 		goto out_cached_reply;
956 	case RC_DROPIT:
957 		goto out_dropit;
958 	}
959 
960 	nfs_reply = xdr_inline_decode(&rqstp->rq_res_stream, 0);
961 	*statp = proc->pc_func(rqstp);
962 	if (test_bit(RQ_DROPME, &rqstp->rq_flags))
963 		goto out_update_drop;
964 
965 	if (!proc->pc_encode(rqstp, &rqstp->rq_res_stream))
966 		goto out_encode_err;
967 
968 	/*
969 	 * Release rq_status_counter setting it to an even value after the rpc
970 	 * request has been properly processed.
971 	 */
972 	smp_store_release(&rqstp->rq_status_counter, rqstp->rq_status_counter + 1);
973 
974 	nfsd_cache_update(rqstp, rp, rqstp->rq_cachetype, nfs_reply);
975 out_cached_reply:
976 	return 1;
977 
978 out_decode_err:
979 	trace_nfsd_garbage_args_err(rqstp);
980 	*statp = rpc_garbage_args;
981 	return 1;
982 
983 out_update_drop:
984 	nfsd_cache_update(rqstp, rp, RC_NOCACHE, NULL);
985 out_dropit:
986 	return 0;
987 
988 out_encode_err:
989 	trace_nfsd_cant_encode_err(rqstp);
990 	nfsd_cache_update(rqstp, rp, RC_NOCACHE, NULL);
991 	*statp = rpc_system_err;
992 	return 1;
993 }
994 
995 /**
996  * nfssvc_decode_voidarg - Decode void arguments
997  * @rqstp: Server RPC transaction context
998  * @xdr: XDR stream positioned at arguments to decode
999  *
1000  * Return values:
1001  *   %false: Arguments were not valid
1002  *   %true: Decoding was successful
1003  */
1004 bool nfssvc_decode_voidarg(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1005 {
1006 	return true;
1007 }
1008 
1009 /**
1010  * nfssvc_encode_voidres - Encode void results
1011  * @rqstp: Server RPC transaction context
1012  * @xdr: XDR stream into which to encode results
1013  *
1014  * Return values:
1015  *   %false: Local error while encoding
1016  *   %true: Encoding was successful
1017  */
1018 bool nfssvc_encode_voidres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1019 {
1020 	return true;
1021 }
1022