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