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

source code of linux/fs/nfsd/nfssvc.c