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