xref: /openbmc/linux/fs/nfsd/nfssvc.c (revision b7019ac5)
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 
16 #include <linux/sunrpc/stats.h>
17 #include <linux/sunrpc/svcsock.h>
18 #include <linux/sunrpc/svc_xprt.h>
19 #include <linux/lockd/bind.h>
20 #include <linux/nfsacl.h>
21 #include <linux/seq_file.h>
22 #include <linux/inetdevice.h>
23 #include <net/addrconf.h>
24 #include <net/ipv6.h>
25 #include <net/net_namespace.h>
26 #include "nfsd.h"
27 #include "cache.h"
28 #include "vfs.h"
29 #include "netns.h"
30 
31 #define NFSDDBG_FACILITY	NFSDDBG_SVC
32 
33 extern struct svc_program	nfsd_program;
34 static int			nfsd(void *vrqstp);
35 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
36 static int			nfsd_acl_rpcbind_set(struct net *,
37 						     const struct svc_program *,
38 						     u32, int,
39 						     unsigned short,
40 						     unsigned short);
41 static __be32			nfsd_acl_init_request(struct svc_rqst *,
42 						const struct svc_program *,
43 						struct svc_process_info *);
44 #endif
45 static int			nfsd_rpcbind_set(struct net *,
46 						 const struct svc_program *,
47 						 u32, int,
48 						 unsigned short,
49 						 unsigned short);
50 static __be32			nfsd_init_request(struct svc_rqst *,
51 						const struct svc_program *,
52 						struct svc_process_info *);
53 
54 /*
55  * nfsd_mutex protects nn->nfsd_serv -- both the pointer itself and the members
56  * of the svc_serv struct. In particular, ->sv_nrthreads but also to some
57  * extent ->sv_temp_socks and ->sv_permsocks. It also protects nfsdstats.th_cnt
58  *
59  * If (out side the lock) nn->nfsd_serv is non-NULL, then it must point to a
60  * properly initialised 'struct svc_serv' with ->sv_nrthreads > 0. That number
61  * of nfsd threads must exist and each must listed in ->sp_all_threads in each
62  * entry of ->sv_pools[].
63  *
64  * Transitions of the thread count between zero and non-zero are of particular
65  * interest since the svc_serv needs to be created and initialized at that
66  * point, or freed.
67  *
68  * Finally, the nfsd_mutex also protects some of the global variables that are
69  * accessed when nfsd starts and that are settable via the write_* routines in
70  * nfsctl.c. In particular:
71  *
72  *	user_recovery_dirname
73  *	user_lease_time
74  *	nfsd_versions
75  */
76 DEFINE_MUTEX(nfsd_mutex);
77 
78 /*
79  * nfsd_drc_lock protects nfsd_drc_max_pages and nfsd_drc_pages_used.
80  * nfsd_drc_max_pages limits the total amount of memory available for
81  * version 4.1 DRC caches.
82  * nfsd_drc_pages_used tracks the current version 4.1 DRC memory usage.
83  */
84 spinlock_t	nfsd_drc_lock;
85 unsigned long	nfsd_drc_max_mem;
86 unsigned long	nfsd_drc_mem_used;
87 
88 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
89 static struct svc_stat	nfsd_acl_svcstats;
90 static const struct svc_version *nfsd_acl_version[] = {
91 	[2] = &nfsd_acl_version2,
92 	[3] = &nfsd_acl_version3,
93 };
94 
95 #define NFSD_ACL_MINVERS            2
96 #define NFSD_ACL_NRVERS		ARRAY_SIZE(nfsd_acl_version)
97 static const struct svc_version *nfsd_acl_versions[NFSD_ACL_NRVERS];
98 
99 static struct svc_program	nfsd_acl_program = {
100 	.pg_prog		= NFS_ACL_PROGRAM,
101 	.pg_nvers		= NFSD_ACL_NRVERS,
102 	.pg_vers		= nfsd_acl_versions,
103 	.pg_name		= "nfsacl",
104 	.pg_class		= "nfsd",
105 	.pg_stats		= &nfsd_acl_svcstats,
106 	.pg_authenticate	= &svc_set_client,
107 	.pg_init_request	= nfsd_acl_init_request,
108 	.pg_rpcbind_set		= nfsd_acl_rpcbind_set,
109 };
110 
111 static struct svc_stat	nfsd_acl_svcstats = {
112 	.program	= &nfsd_acl_program,
113 };
114 #endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */
115 
116 static const struct svc_version *nfsd_version[] = {
117 	[2] = &nfsd_version2,
118 #if defined(CONFIG_NFSD_V3)
119 	[3] = &nfsd_version3,
120 #endif
121 #if defined(CONFIG_NFSD_V4)
122 	[4] = &nfsd_version4,
123 #endif
124 };
125 
126 #define NFSD_MINVERS    	2
127 #define NFSD_NRVERS		ARRAY_SIZE(nfsd_version)
128 
129 struct svc_program		nfsd_program = {
130 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
131 	.pg_next		= &nfsd_acl_program,
132 #endif
133 	.pg_prog		= NFS_PROGRAM,		/* program number */
134 	.pg_nvers		= NFSD_NRVERS,		/* nr of entries in nfsd_version */
135 	.pg_vers		= nfsd_version,		/* version table */
136 	.pg_name		= "nfsd",		/* program name */
137 	.pg_class		= "nfsd",		/* authentication class */
138 	.pg_stats		= &nfsd_svcstats,	/* version table */
139 	.pg_authenticate	= &svc_set_client,	/* export authentication */
140 	.pg_init_request	= nfsd_init_request,
141 	.pg_rpcbind_set		= nfsd_rpcbind_set,
142 };
143 
144 static bool
145 nfsd_support_version(int vers)
146 {
147 	if (vers >= NFSD_MINVERS && vers < NFSD_NRVERS)
148 		return nfsd_version[vers] != NULL;
149 	return false;
150 }
151 
152 static bool *
153 nfsd_alloc_versions(void)
154 {
155 	bool *vers = kmalloc_array(NFSD_NRVERS, sizeof(bool), GFP_KERNEL);
156 	unsigned i;
157 
158 	if (vers) {
159 		/* All compiled versions are enabled by default */
160 		for (i = 0; i < NFSD_NRVERS; i++)
161 			vers[i] = nfsd_support_version(i);
162 	}
163 	return vers;
164 }
165 
166 static bool *
167 nfsd_alloc_minorversions(void)
168 {
169 	bool *vers = kmalloc_array(NFSD_SUPPORTED_MINOR_VERSION + 1,
170 			sizeof(bool), GFP_KERNEL);
171 	unsigned i;
172 
173 	if (vers) {
174 		/* All minor versions are enabled by default */
175 		for (i = 0; i <= NFSD_SUPPORTED_MINOR_VERSION; i++)
176 			vers[i] = nfsd_support_version(4);
177 	}
178 	return vers;
179 }
180 
181 void
182 nfsd_netns_free_versions(struct nfsd_net *nn)
183 {
184 	kfree(nn->nfsd_versions);
185 	kfree(nn->nfsd4_minorversions);
186 	nn->nfsd_versions = NULL;
187 	nn->nfsd4_minorversions = NULL;
188 }
189 
190 static void
191 nfsd_netns_init_versions(struct nfsd_net *nn)
192 {
193 	if (!nn->nfsd_versions) {
194 		nn->nfsd_versions = nfsd_alloc_versions();
195 		nn->nfsd4_minorversions = nfsd_alloc_minorversions();
196 		if (!nn->nfsd_versions || !nn->nfsd4_minorversions)
197 			nfsd_netns_free_versions(nn);
198 	}
199 }
200 
201 int nfsd_vers(struct nfsd_net *nn, int vers, enum vers_op change)
202 {
203 	if (vers < NFSD_MINVERS || vers >= NFSD_NRVERS)
204 		return 0;
205 	switch(change) {
206 	case NFSD_SET:
207 		if (nn->nfsd_versions)
208 			nn->nfsd_versions[vers] = nfsd_support_version(vers);
209 		break;
210 	case NFSD_CLEAR:
211 		nfsd_netns_init_versions(nn);
212 		if (nn->nfsd_versions)
213 			nn->nfsd_versions[vers] = false;
214 		break;
215 	case NFSD_TEST:
216 		if (nn->nfsd_versions)
217 			return nn->nfsd_versions[vers];
218 		/* Fallthrough */
219 	case NFSD_AVAIL:
220 		return nfsd_support_version(vers);
221 	}
222 	return 0;
223 }
224 
225 static void
226 nfsd_adjust_nfsd_versions4(struct nfsd_net *nn)
227 {
228 	unsigned i;
229 
230 	for (i = 0; i <= NFSD_SUPPORTED_MINOR_VERSION; i++) {
231 		if (nn->nfsd4_minorversions[i])
232 			return;
233 	}
234 	nfsd_vers(nn, 4, NFSD_CLEAR);
235 }
236 
237 int nfsd_minorversion(struct nfsd_net *nn, u32 minorversion, enum vers_op change)
238 {
239 	if (minorversion > NFSD_SUPPORTED_MINOR_VERSION &&
240 	    change != NFSD_AVAIL)
241 		return -1;
242 
243 	switch(change) {
244 	case NFSD_SET:
245 		if (nn->nfsd4_minorversions) {
246 			nfsd_vers(nn, 4, NFSD_SET);
247 			nn->nfsd4_minorversions[minorversion] =
248 				nfsd_vers(nn, 4, NFSD_TEST);
249 		}
250 		break;
251 	case NFSD_CLEAR:
252 		nfsd_netns_init_versions(nn);
253 		if (nn->nfsd4_minorversions) {
254 			nn->nfsd4_minorversions[minorversion] = false;
255 			nfsd_adjust_nfsd_versions4(nn);
256 		}
257 		break;
258 	case NFSD_TEST:
259 		if (nn->nfsd4_minorversions)
260 			return nn->nfsd4_minorversions[minorversion];
261 		return nfsd_vers(nn, 4, NFSD_TEST);
262 	case NFSD_AVAIL:
263 		return minorversion <= NFSD_SUPPORTED_MINOR_VERSION &&
264 			nfsd_vers(nn, 4, NFSD_AVAIL);
265 	}
266 	return 0;
267 }
268 
269 /*
270  * Maximum number of nfsd processes
271  */
272 #define	NFSD_MAXSERVS		8192
273 
274 int nfsd_nrthreads(struct net *net)
275 {
276 	int rv = 0;
277 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
278 
279 	mutex_lock(&nfsd_mutex);
280 	if (nn->nfsd_serv)
281 		rv = nn->nfsd_serv->sv_nrthreads;
282 	mutex_unlock(&nfsd_mutex);
283 	return rv;
284 }
285 
286 static int nfsd_init_socks(struct net *net, const struct cred *cred)
287 {
288 	int error;
289 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
290 
291 	if (!list_empty(&nn->nfsd_serv->sv_permsocks))
292 		return 0;
293 
294 	error = svc_create_xprt(nn->nfsd_serv, "udp", net, PF_INET, NFS_PORT,
295 					SVC_SOCK_DEFAULTS, cred);
296 	if (error < 0)
297 		return error;
298 
299 	error = svc_create_xprt(nn->nfsd_serv, "tcp", net, PF_INET, NFS_PORT,
300 					SVC_SOCK_DEFAULTS, cred);
301 	if (error < 0)
302 		return error;
303 
304 	return 0;
305 }
306 
307 static int nfsd_users = 0;
308 
309 static int nfsd_startup_generic(int nrservs)
310 {
311 	int ret;
312 
313 	if (nfsd_users++)
314 		return 0;
315 
316 	/*
317 	 * Readahead param cache - will no-op if it already exists.
318 	 * (Note therefore results will be suboptimal if number of
319 	 * threads is modified after nfsd start.)
320 	 */
321 	ret = nfsd_racache_init(2*nrservs);
322 	if (ret)
323 		goto dec_users;
324 
325 	ret = nfs4_state_start();
326 	if (ret)
327 		goto out_racache;
328 	return 0;
329 
330 out_racache:
331 	nfsd_racache_shutdown();
332 dec_users:
333 	nfsd_users--;
334 	return ret;
335 }
336 
337 static void nfsd_shutdown_generic(void)
338 {
339 	if (--nfsd_users)
340 		return;
341 
342 	nfs4_state_shutdown();
343 	nfsd_racache_shutdown();
344 }
345 
346 static bool nfsd_needs_lockd(struct nfsd_net *nn)
347 {
348 	return nfsd_vers(nn, 2, NFSD_TEST) || nfsd_vers(nn, 3, NFSD_TEST);
349 }
350 
351 static int nfsd_startup_net(int nrservs, struct net *net, const struct cred *cred)
352 {
353 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
354 	int ret;
355 
356 	if (nn->nfsd_net_up)
357 		return 0;
358 
359 	ret = nfsd_startup_generic(nrservs);
360 	if (ret)
361 		return ret;
362 	ret = nfsd_init_socks(net, cred);
363 	if (ret)
364 		goto out_socks;
365 
366 	if (nfsd_needs_lockd(nn) && !nn->lockd_up) {
367 		ret = lockd_up(net, cred);
368 		if (ret)
369 			goto out_socks;
370 		nn->lockd_up = 1;
371 	}
372 
373 	ret = nfs4_state_start_net(net);
374 	if (ret)
375 		goto out_lockd;
376 
377 	nn->nfsd_net_up = true;
378 	return 0;
379 
380 out_lockd:
381 	if (nn->lockd_up) {
382 		lockd_down(net);
383 		nn->lockd_up = 0;
384 	}
385 out_socks:
386 	nfsd_shutdown_generic();
387 	return ret;
388 }
389 
390 static void nfsd_shutdown_net(struct net *net)
391 {
392 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
393 
394 	nfs4_state_shutdown_net(net);
395 	if (nn->lockd_up) {
396 		lockd_down(net);
397 		nn->lockd_up = 0;
398 	}
399 	nn->nfsd_net_up = false;
400 	nfsd_shutdown_generic();
401 }
402 
403 static int nfsd_inetaddr_event(struct notifier_block *this, unsigned long event,
404 	void *ptr)
405 {
406 	struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
407 	struct net_device *dev = ifa->ifa_dev->dev;
408 	struct net *net = dev_net(dev);
409 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
410 	struct sockaddr_in sin;
411 
412 	if ((event != NETDEV_DOWN) ||
413 	    !atomic_inc_not_zero(&nn->ntf_refcnt))
414 		goto out;
415 
416 	if (nn->nfsd_serv) {
417 		dprintk("nfsd_inetaddr_event: removed %pI4\n", &ifa->ifa_local);
418 		sin.sin_family = AF_INET;
419 		sin.sin_addr.s_addr = ifa->ifa_local;
420 		svc_age_temp_xprts_now(nn->nfsd_serv, (struct sockaddr *)&sin);
421 	}
422 	atomic_dec(&nn->ntf_refcnt);
423 	wake_up(&nn->ntf_wq);
424 
425 out:
426 	return NOTIFY_DONE;
427 }
428 
429 static struct notifier_block nfsd_inetaddr_notifier = {
430 	.notifier_call = nfsd_inetaddr_event,
431 };
432 
433 #if IS_ENABLED(CONFIG_IPV6)
434 static int nfsd_inet6addr_event(struct notifier_block *this,
435 	unsigned long event, void *ptr)
436 {
437 	struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
438 	struct net_device *dev = ifa->idev->dev;
439 	struct net *net = dev_net(dev);
440 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
441 	struct sockaddr_in6 sin6;
442 
443 	if ((event != NETDEV_DOWN) ||
444 	    !atomic_inc_not_zero(&nn->ntf_refcnt))
445 		goto out;
446 
447 	if (nn->nfsd_serv) {
448 		dprintk("nfsd_inet6addr_event: removed %pI6\n", &ifa->addr);
449 		sin6.sin6_family = AF_INET6;
450 		sin6.sin6_addr = ifa->addr;
451 		if (ipv6_addr_type(&sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
452 			sin6.sin6_scope_id = ifa->idev->dev->ifindex;
453 		svc_age_temp_xprts_now(nn->nfsd_serv, (struct sockaddr *)&sin6);
454 	}
455 	atomic_dec(&nn->ntf_refcnt);
456 	wake_up(&nn->ntf_wq);
457 out:
458 	return NOTIFY_DONE;
459 }
460 
461 static struct notifier_block nfsd_inet6addr_notifier = {
462 	.notifier_call = nfsd_inet6addr_event,
463 };
464 #endif
465 
466 /* Only used under nfsd_mutex, so this atomic may be overkill: */
467 static atomic_t nfsd_notifier_refcount = ATOMIC_INIT(0);
468 
469 static void nfsd_last_thread(struct svc_serv *serv, struct net *net)
470 {
471 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
472 
473 	atomic_dec(&nn->ntf_refcnt);
474 	/* check if the notifier still has clients */
475 	if (atomic_dec_return(&nfsd_notifier_refcount) == 0) {
476 		unregister_inetaddr_notifier(&nfsd_inetaddr_notifier);
477 #if IS_ENABLED(CONFIG_IPV6)
478 		unregister_inet6addr_notifier(&nfsd_inet6addr_notifier);
479 #endif
480 	}
481 	wait_event(nn->ntf_wq, atomic_read(&nn->ntf_refcnt) == 0);
482 
483 	/*
484 	 * write_ports can create the server without actually starting
485 	 * any threads--if we get shut down before any threads are
486 	 * started, then nfsd_last_thread will be run before any of this
487 	 * other initialization has been done except the rpcb information.
488 	 */
489 	svc_rpcb_cleanup(serv, net);
490 	if (!nn->nfsd_net_up)
491 		return;
492 
493 	nfsd_shutdown_net(net);
494 	printk(KERN_WARNING "nfsd: last server has exited, flushing export "
495 			    "cache\n");
496 	nfsd_export_flush(net);
497 }
498 
499 void nfsd_reset_versions(struct nfsd_net *nn)
500 {
501 	int i;
502 
503 	for (i = 0; i < NFSD_NRVERS; i++)
504 		if (nfsd_vers(nn, i, NFSD_TEST))
505 			return;
506 
507 	for (i = 0; i < NFSD_NRVERS; i++)
508 		if (i != 4)
509 			nfsd_vers(nn, i, NFSD_SET);
510 		else {
511 			int minor = 0;
512 			while (nfsd_minorversion(nn, minor, NFSD_SET) >= 0)
513 				minor++;
514 		}
515 }
516 
517 /*
518  * Each session guarantees a negotiated per slot memory cache for replies
519  * which in turn consumes memory beyond the v2/v3/v4.0 server. A dedicated
520  * NFSv4.1 server might want to use more memory for a DRC than a machine
521  * with mutiple services.
522  *
523  * Impose a hard limit on the number of pages for the DRC which varies
524  * according to the machines free pages. This is of course only a default.
525  *
526  * For now this is a #defined shift which could be under admin control
527  * in the future.
528  */
529 static void set_max_drc(void)
530 {
531 	#define NFSD_DRC_SIZE_SHIFT	7
532 	nfsd_drc_max_mem = (nr_free_buffer_pages()
533 					>> NFSD_DRC_SIZE_SHIFT) * PAGE_SIZE;
534 	nfsd_drc_mem_used = 0;
535 	spin_lock_init(&nfsd_drc_lock);
536 	dprintk("%s nfsd_drc_max_mem %lu \n", __func__, nfsd_drc_max_mem);
537 }
538 
539 static int nfsd_get_default_max_blksize(void)
540 {
541 	struct sysinfo i;
542 	unsigned long long target;
543 	unsigned long ret;
544 
545 	si_meminfo(&i);
546 	target = (i.totalram - i.totalhigh) << PAGE_SHIFT;
547 	/*
548 	 * Aim for 1/4096 of memory per thread This gives 1MB on 4Gig
549 	 * machines, but only uses 32K on 128M machines.  Bottom out at
550 	 * 8K on 32M and smaller.  Of course, this is only a default.
551 	 */
552 	target >>= 12;
553 
554 	ret = NFSSVC_MAXBLKSIZE;
555 	while (ret > target && ret >= 8*1024*2)
556 		ret /= 2;
557 	return ret;
558 }
559 
560 static const struct svc_serv_ops nfsd_thread_sv_ops = {
561 	.svo_shutdown		= nfsd_last_thread,
562 	.svo_function		= nfsd,
563 	.svo_enqueue_xprt	= svc_xprt_do_enqueue,
564 	.svo_setup		= svc_set_num_threads,
565 	.svo_module		= THIS_MODULE,
566 };
567 
568 int nfsd_create_serv(struct net *net)
569 {
570 	int error;
571 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
572 
573 	WARN_ON(!mutex_is_locked(&nfsd_mutex));
574 	if (nn->nfsd_serv) {
575 		svc_get(nn->nfsd_serv);
576 		return 0;
577 	}
578 	if (nfsd_max_blksize == 0)
579 		nfsd_max_blksize = nfsd_get_default_max_blksize();
580 	nfsd_reset_versions(nn);
581 	nn->nfsd_serv = svc_create_pooled(&nfsd_program, nfsd_max_blksize,
582 						&nfsd_thread_sv_ops);
583 	if (nn->nfsd_serv == NULL)
584 		return -ENOMEM;
585 
586 	nn->nfsd_serv->sv_maxconn = nn->max_connections;
587 	error = svc_bind(nn->nfsd_serv, net);
588 	if (error < 0) {
589 		svc_destroy(nn->nfsd_serv);
590 		return error;
591 	}
592 
593 	set_max_drc();
594 	/* check if the notifier is already set */
595 	if (atomic_inc_return(&nfsd_notifier_refcount) == 1) {
596 		register_inetaddr_notifier(&nfsd_inetaddr_notifier);
597 #if IS_ENABLED(CONFIG_IPV6)
598 		register_inet6addr_notifier(&nfsd_inet6addr_notifier);
599 #endif
600 	}
601 	atomic_inc(&nn->ntf_refcnt);
602 	ktime_get_real_ts64(&nn->nfssvc_boot); /* record boot time */
603 	return 0;
604 }
605 
606 int nfsd_nrpools(struct net *net)
607 {
608 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
609 
610 	if (nn->nfsd_serv == NULL)
611 		return 0;
612 	else
613 		return nn->nfsd_serv->sv_nrpools;
614 }
615 
616 int nfsd_get_nrthreads(int n, int *nthreads, struct net *net)
617 {
618 	int i = 0;
619 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
620 
621 	if (nn->nfsd_serv != NULL) {
622 		for (i = 0; i < nn->nfsd_serv->sv_nrpools && i < n; i++)
623 			nthreads[i] = nn->nfsd_serv->sv_pools[i].sp_nrthreads;
624 	}
625 
626 	return 0;
627 }
628 
629 void nfsd_destroy(struct net *net)
630 {
631 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
632 	int destroy = (nn->nfsd_serv->sv_nrthreads == 1);
633 
634 	if (destroy)
635 		svc_shutdown_net(nn->nfsd_serv, net);
636 	svc_destroy(nn->nfsd_serv);
637 	if (destroy)
638 		nn->nfsd_serv = NULL;
639 }
640 
641 int nfsd_set_nrthreads(int n, int *nthreads, struct net *net)
642 {
643 	int i = 0;
644 	int tot = 0;
645 	int err = 0;
646 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
647 
648 	WARN_ON(!mutex_is_locked(&nfsd_mutex));
649 
650 	if (nn->nfsd_serv == NULL || n <= 0)
651 		return 0;
652 
653 	if (n > nn->nfsd_serv->sv_nrpools)
654 		n = nn->nfsd_serv->sv_nrpools;
655 
656 	/* enforce a global maximum number of threads */
657 	tot = 0;
658 	for (i = 0; i < n; i++) {
659 		nthreads[i] = min(nthreads[i], NFSD_MAXSERVS);
660 		tot += nthreads[i];
661 	}
662 	if (tot > NFSD_MAXSERVS) {
663 		/* total too large: scale down requested numbers */
664 		for (i = 0; i < n && tot > 0; i++) {
665 		    	int new = nthreads[i] * NFSD_MAXSERVS / tot;
666 			tot -= (nthreads[i] - new);
667 			nthreads[i] = new;
668 		}
669 		for (i = 0; i < n && tot > 0; i++) {
670 			nthreads[i]--;
671 			tot--;
672 		}
673 	}
674 
675 	/*
676 	 * There must always be a thread in pool 0; the admin
677 	 * can't shut down NFS completely using pool_threads.
678 	 */
679 	if (nthreads[0] == 0)
680 		nthreads[0] = 1;
681 
682 	/* apply the new numbers */
683 	svc_get(nn->nfsd_serv);
684 	for (i = 0; i < n; i++) {
685 		err = nn->nfsd_serv->sv_ops->svo_setup(nn->nfsd_serv,
686 				&nn->nfsd_serv->sv_pools[i], nthreads[i]);
687 		if (err)
688 			break;
689 	}
690 	nfsd_destroy(net);
691 	return err;
692 }
693 
694 /*
695  * Adjust the number of threads and return the new number of threads.
696  * This is also the function that starts the server if necessary, if
697  * this is the first time nrservs is nonzero.
698  */
699 int
700 nfsd_svc(int nrservs, struct net *net, const struct cred *cred)
701 {
702 	int	error;
703 	bool	nfsd_up_before;
704 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
705 
706 	mutex_lock(&nfsd_mutex);
707 	dprintk("nfsd: creating service\n");
708 
709 	nrservs = max(nrservs, 0);
710 	nrservs = min(nrservs, NFSD_MAXSERVS);
711 	error = 0;
712 
713 	if (nrservs == 0 && nn->nfsd_serv == NULL)
714 		goto out;
715 
716 	error = nfsd_create_serv(net);
717 	if (error)
718 		goto out;
719 
720 	nfsd_up_before = nn->nfsd_net_up;
721 
722 	error = nfsd_startup_net(nrservs, net, cred);
723 	if (error)
724 		goto out_destroy;
725 	error = nn->nfsd_serv->sv_ops->svo_setup(nn->nfsd_serv,
726 			NULL, nrservs);
727 	if (error)
728 		goto out_shutdown;
729 	/* We are holding a reference to nn->nfsd_serv which
730 	 * we don't want to count in the return value,
731 	 * so subtract 1
732 	 */
733 	error = nn->nfsd_serv->sv_nrthreads - 1;
734 out_shutdown:
735 	if (error < 0 && !nfsd_up_before)
736 		nfsd_shutdown_net(net);
737 out_destroy:
738 	nfsd_destroy(net);		/* Release server */
739 out:
740 	mutex_unlock(&nfsd_mutex);
741 	return error;
742 }
743 
744 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
745 static bool
746 nfsd_support_acl_version(int vers)
747 {
748 	if (vers >= NFSD_ACL_MINVERS && vers < NFSD_ACL_NRVERS)
749 		return nfsd_acl_version[vers] != NULL;
750 	return false;
751 }
752 
753 static int
754 nfsd_acl_rpcbind_set(struct net *net, const struct svc_program *progp,
755 		     u32 version, int family, unsigned short proto,
756 		     unsigned short port)
757 {
758 	if (!nfsd_support_acl_version(version) ||
759 	    !nfsd_vers(net_generic(net, nfsd_net_id), version, NFSD_TEST))
760 		return 0;
761 	return svc_generic_rpcbind_set(net, progp, version, family,
762 			proto, port);
763 }
764 
765 static __be32
766 nfsd_acl_init_request(struct svc_rqst *rqstp,
767 		      const struct svc_program *progp,
768 		      struct svc_process_info *ret)
769 {
770 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
771 	int i;
772 
773 	if (likely(nfsd_support_acl_version(rqstp->rq_vers) &&
774 	    nfsd_vers(nn, rqstp->rq_vers, NFSD_TEST)))
775 		return svc_generic_init_request(rqstp, progp, ret);
776 
777 	ret->mismatch.lovers = NFSD_ACL_NRVERS;
778 	for (i = NFSD_ACL_MINVERS; i < NFSD_ACL_NRVERS; i++) {
779 		if (nfsd_support_acl_version(rqstp->rq_vers) &&
780 		    nfsd_vers(nn, i, NFSD_TEST)) {
781 			ret->mismatch.lovers = i;
782 			break;
783 		}
784 	}
785 	if (ret->mismatch.lovers == NFSD_ACL_NRVERS)
786 		return rpc_prog_unavail;
787 	ret->mismatch.hivers = NFSD_ACL_MINVERS;
788 	for (i = NFSD_ACL_NRVERS - 1; i >= NFSD_ACL_MINVERS; i--) {
789 		if (nfsd_support_acl_version(rqstp->rq_vers) &&
790 		    nfsd_vers(nn, i, NFSD_TEST)) {
791 			ret->mismatch.hivers = i;
792 			break;
793 		}
794 	}
795 	return rpc_prog_mismatch;
796 }
797 #endif
798 
799 static int
800 nfsd_rpcbind_set(struct net *net, const struct svc_program *progp,
801 		 u32 version, int family, unsigned short proto,
802 		 unsigned short port)
803 {
804 	if (!nfsd_vers(net_generic(net, nfsd_net_id), version, NFSD_TEST))
805 		return 0;
806 	return svc_generic_rpcbind_set(net, progp, version, family,
807 			proto, port);
808 }
809 
810 static __be32
811 nfsd_init_request(struct svc_rqst *rqstp,
812 		  const struct svc_program *progp,
813 		  struct svc_process_info *ret)
814 {
815 	struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
816 	int i;
817 
818 	if (likely(nfsd_vers(nn, rqstp->rq_vers, NFSD_TEST)))
819 		return svc_generic_init_request(rqstp, progp, ret);
820 
821 	ret->mismatch.lovers = NFSD_NRVERS;
822 	for (i = NFSD_MINVERS; i < NFSD_NRVERS; i++) {
823 		if (nfsd_vers(nn, i, NFSD_TEST)) {
824 			ret->mismatch.lovers = i;
825 			break;
826 		}
827 	}
828 	if (ret->mismatch.lovers == NFSD_NRVERS)
829 		return rpc_prog_unavail;
830 	ret->mismatch.hivers = NFSD_MINVERS;
831 	for (i = NFSD_NRVERS - 1; i >= NFSD_MINVERS; i--) {
832 		if (nfsd_vers(nn, i, NFSD_TEST)) {
833 			ret->mismatch.hivers = i;
834 			break;
835 		}
836 	}
837 	return rpc_prog_mismatch;
838 }
839 
840 /*
841  * This is the NFS server kernel thread
842  */
843 static int
844 nfsd(void *vrqstp)
845 {
846 	struct svc_rqst *rqstp = (struct svc_rqst *) vrqstp;
847 	struct svc_xprt *perm_sock = list_entry(rqstp->rq_server->sv_permsocks.next, typeof(struct svc_xprt), xpt_list);
848 	struct net *net = perm_sock->xpt_net;
849 	struct nfsd_net *nn = net_generic(net, nfsd_net_id);
850 	int err;
851 
852 	/* Lock module and set up kernel thread */
853 	mutex_lock(&nfsd_mutex);
854 
855 	/* At this point, the thread shares current->fs
856 	 * with the init process. We need to create files with the
857 	 * umask as defined by the client instead of init's umask. */
858 	if (unshare_fs_struct() < 0) {
859 		printk("Unable to start nfsd thread: out of memory\n");
860 		goto out;
861 	}
862 
863 	current->fs->umask = 0;
864 
865 	/*
866 	 * thread is spawned with all signals set to SIG_IGN, re-enable
867 	 * the ones that will bring down the thread
868 	 */
869 	allow_signal(SIGKILL);
870 	allow_signal(SIGHUP);
871 	allow_signal(SIGINT);
872 	allow_signal(SIGQUIT);
873 
874 	nfsdstats.th_cnt++;
875 	mutex_unlock(&nfsd_mutex);
876 
877 	set_freezable();
878 
879 	/*
880 	 * The main request loop
881 	 */
882 	for (;;) {
883 		/* Update sv_maxconn if it has changed */
884 		rqstp->rq_server->sv_maxconn = nn->max_connections;
885 
886 		/*
887 		 * Find a socket with data available and call its
888 		 * recvfrom routine.
889 		 */
890 		while ((err = svc_recv(rqstp, 60*60*HZ)) == -EAGAIN)
891 			;
892 		if (err == -EINTR)
893 			break;
894 		validate_process_creds();
895 		svc_process(rqstp);
896 		validate_process_creds();
897 	}
898 
899 	/* Clear signals before calling svc_exit_thread() */
900 	flush_signals(current);
901 
902 	mutex_lock(&nfsd_mutex);
903 	nfsdstats.th_cnt --;
904 
905 out:
906 	rqstp->rq_server = NULL;
907 
908 	/* Release the thread */
909 	svc_exit_thread(rqstp);
910 
911 	nfsd_destroy(net);
912 
913 	/* Release module */
914 	mutex_unlock(&nfsd_mutex);
915 	module_put_and_exit(0);
916 	return 0;
917 }
918 
919 static __be32 map_new_errors(u32 vers, __be32 nfserr)
920 {
921 	if (nfserr == nfserr_jukebox && vers == 2)
922 		return nfserr_dropit;
923 	if (nfserr == nfserr_wrongsec && vers < 4)
924 		return nfserr_acces;
925 	return nfserr;
926 }
927 
928 /*
929  * A write procedure can have a large argument, and a read procedure can
930  * have a large reply, but no NFSv2 or NFSv3 procedure has argument and
931  * reply that can both be larger than a page.  The xdr code has taken
932  * advantage of this assumption to be a sloppy about bounds checking in
933  * some cases.  Pending a rewrite of the NFSv2/v3 xdr code to fix that
934  * problem, we enforce these assumptions here:
935  */
936 static bool nfs_request_too_big(struct svc_rqst *rqstp,
937 				const struct svc_procedure *proc)
938 {
939 	/*
940 	 * The ACL code has more careful bounds-checking and is not
941 	 * susceptible to this problem:
942 	 */
943 	if (rqstp->rq_prog != NFS_PROGRAM)
944 		return false;
945 	/*
946 	 * Ditto NFSv4 (which can in theory have argument and reply both
947 	 * more than a page):
948 	 */
949 	if (rqstp->rq_vers >= 4)
950 		return false;
951 	/* The reply will be small, we're OK: */
952 	if (proc->pc_xdrressize > 0 &&
953 	    proc->pc_xdrressize < XDR_QUADLEN(PAGE_SIZE))
954 		return false;
955 
956 	return rqstp->rq_arg.len > PAGE_SIZE;
957 }
958 
959 int
960 nfsd_dispatch(struct svc_rqst *rqstp, __be32 *statp)
961 {
962 	const struct svc_procedure *proc;
963 	__be32			nfserr;
964 	__be32			*nfserrp;
965 
966 	dprintk("nfsd_dispatch: vers %d proc %d\n",
967 				rqstp->rq_vers, rqstp->rq_proc);
968 	proc = rqstp->rq_procinfo;
969 
970 	if (nfs_request_too_big(rqstp, proc)) {
971 		dprintk("nfsd: NFSv%d argument too large\n", rqstp->rq_vers);
972 		*statp = rpc_garbage_args;
973 		return 1;
974 	}
975 	/*
976 	 * Give the xdr decoder a chance to change this if it wants
977 	 * (necessary in the NFSv4.0 compound case)
978 	 */
979 	rqstp->rq_cachetype = proc->pc_cachetype;
980 	/* Decode arguments */
981 	if (proc->pc_decode &&
982 	    !proc->pc_decode(rqstp, (__be32*)rqstp->rq_arg.head[0].iov_base)) {
983 		dprintk("nfsd: failed to decode arguments!\n");
984 		*statp = rpc_garbage_args;
985 		return 1;
986 	}
987 
988 	/* Check whether we have this call in the cache. */
989 	switch (nfsd_cache_lookup(rqstp)) {
990 	case RC_DROPIT:
991 		return 0;
992 	case RC_REPLY:
993 		return 1;
994 	case RC_DOIT:;
995 		/* do it */
996 	}
997 
998 	/* need to grab the location to store the status, as
999 	 * nfsv4 does some encoding while processing
1000 	 */
1001 	nfserrp = rqstp->rq_res.head[0].iov_base
1002 		+ rqstp->rq_res.head[0].iov_len;
1003 	rqstp->rq_res.head[0].iov_len += sizeof(__be32);
1004 
1005 	/* Now call the procedure handler, and encode NFS status. */
1006 	nfserr = proc->pc_func(rqstp);
1007 	nfserr = map_new_errors(rqstp->rq_vers, nfserr);
1008 	if (nfserr == nfserr_dropit || test_bit(RQ_DROPME, &rqstp->rq_flags)) {
1009 		dprintk("nfsd: Dropping request; may be revisited later\n");
1010 		nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
1011 		return 0;
1012 	}
1013 
1014 	if (rqstp->rq_proc != 0)
1015 		*nfserrp++ = nfserr;
1016 
1017 	/* Encode result.
1018 	 * For NFSv2, additional info is never returned in case of an error.
1019 	 */
1020 	if (!(nfserr && rqstp->rq_vers == 2)) {
1021 		if (proc->pc_encode && !proc->pc_encode(rqstp, nfserrp)) {
1022 			/* Failed to encode result. Release cache entry */
1023 			dprintk("nfsd: failed to encode result!\n");
1024 			nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
1025 			*statp = rpc_system_err;
1026 			return 1;
1027 		}
1028 	}
1029 
1030 	/* Store reply in cache. */
1031 	nfsd_cache_update(rqstp, rqstp->rq_cachetype, statp + 1);
1032 	return 1;
1033 }
1034 
1035 int nfsd_pool_stats_open(struct inode *inode, struct file *file)
1036 {
1037 	int ret;
1038 	struct nfsd_net *nn = net_generic(inode->i_sb->s_fs_info, nfsd_net_id);
1039 
1040 	mutex_lock(&nfsd_mutex);
1041 	if (nn->nfsd_serv == NULL) {
1042 		mutex_unlock(&nfsd_mutex);
1043 		return -ENODEV;
1044 	}
1045 	/* bump up the psudo refcount while traversing */
1046 	svc_get(nn->nfsd_serv);
1047 	ret = svc_pool_stats_open(nn->nfsd_serv, file);
1048 	mutex_unlock(&nfsd_mutex);
1049 	return ret;
1050 }
1051 
1052 int nfsd_pool_stats_release(struct inode *inode, struct file *file)
1053 {
1054 	int ret = seq_release(inode, file);
1055 	struct net *net = inode->i_sb->s_fs_info;
1056 
1057 	mutex_lock(&nfsd_mutex);
1058 	/* this function really, really should have been called svc_put() */
1059 	nfsd_destroy(net);
1060 	mutex_unlock(&nfsd_mutex);
1061 	return ret;
1062 }
1063