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