1 // SPDX-License-Identifier: GPL-2.0
3 * Central processing for nfsd.
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>
17 #include <linux/sunrpc/stats.h>
18 #include <linux/sunrpc/svcsock.h>
19 #include <linux/sunrpc/svc_xprt.h>
20 #include <linux/lockd/bind.h>
21 #include <linux/nfsacl.h>
22 #include <linux/seq_file.h>
23 #include <linux/inetdevice.h>
24 #include <net/addrconf.h>
26 #include <net/net_namespace.h>
31 #include "filecache.h"
35 #define NFSDDBG_FACILITY NFSDDBG_SVC
37 atomic_t nfsd_th_cnt = ATOMIC_INIT(0);
38 extern struct svc_program nfsd_program;
39 static int nfsd(void *vrqstp);
40 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
41 static int nfsd_acl_rpcbind_set(struct net *,
42 const struct svc_program *,
46 static __be32 nfsd_acl_init_request(struct svc_rqst *,
47 const struct svc_program *,
48 struct svc_process_info *);
50 static int nfsd_rpcbind_set(struct net *,
51 const struct svc_program *,
55 static __be32 nfsd_init_request(struct svc_rqst *,
56 const struct svc_program *,
57 struct svc_process_info *);
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.
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:
67 * user_recovery_dirname
71 DEFINE_MUTEX(nfsd_mutex);
74 * nfsd_drc_lock protects nfsd_drc_max_pages and nfsd_drc_pages_used.
75 * nfsd_drc_max_pages limits the total amount of memory available for
76 * version 4.1 DRC caches.
77 * nfsd_drc_pages_used tracks the current version 4.1 DRC memory usage.
79 DEFINE_SPINLOCK(nfsd_drc_lock);
80 unsigned long nfsd_drc_max_mem;
81 unsigned long nfsd_drc_mem_used;
83 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
84 static const struct svc_version *nfsd_acl_version[] = {
85 # if defined(CONFIG_NFSD_V2_ACL)
86 [2] = &nfsd_acl_version2,
88 # if defined(CONFIG_NFSD_V3_ACL)
89 [3] = &nfsd_acl_version3,
93 #define NFSD_ACL_MINVERS 2
94 #define NFSD_ACL_NRVERS ARRAY_SIZE(nfsd_acl_version)
96 static struct svc_program nfsd_acl_program = {
97 .pg_prog = NFS_ACL_PROGRAM,
98 .pg_nvers = NFSD_ACL_NRVERS,
99 .pg_vers = nfsd_acl_version,
102 .pg_authenticate = &svc_set_client,
103 .pg_init_request = nfsd_acl_init_request,
104 .pg_rpcbind_set = nfsd_acl_rpcbind_set,
107 #endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */
109 static const struct svc_version *nfsd_version[] = {
110 #if defined(CONFIG_NFSD_V2)
111 [2] = &nfsd_version2,
113 [3] = &nfsd_version3,
114 #if defined(CONFIG_NFSD_V4)
115 [4] = &nfsd_version4,
119 #define NFSD_MINVERS 2
120 #define NFSD_NRVERS ARRAY_SIZE(nfsd_version)
122 struct svc_program nfsd_program = {
123 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
124 .pg_next = &nfsd_acl_program,
126 .pg_prog = NFS_PROGRAM, /* program number */
127 .pg_nvers = NFSD_NRVERS, /* nr of entries in nfsd_version */
128 .pg_vers = nfsd_version, /* version table */
129 .pg_name = "nfsd", /* program name */
130 .pg_class = "nfsd", /* authentication class */
131 .pg_authenticate = &svc_set_client, /* export authentication */
132 .pg_init_request = nfsd_init_request,
133 .pg_rpcbind_set = nfsd_rpcbind_set,
136 bool nfsd_support_version(int vers)
138 if (vers >= NFSD_MINVERS && vers < NFSD_NRVERS)
139 return nfsd_version[vers] != NULL;
144 nfsd_alloc_versions(void)
146 bool *vers = kmalloc_array(NFSD_NRVERS, sizeof(bool), GFP_KERNEL);
150 /* All compiled versions are enabled by default */
151 for (i = 0; i < NFSD_NRVERS; i++)
152 vers[i] = nfsd_support_version(i);
158 nfsd_alloc_minorversions(void)
160 bool *vers = kmalloc_array(NFSD_SUPPORTED_MINOR_VERSION + 1,
161 sizeof(bool), GFP_KERNEL);
165 /* All minor versions are enabled by default */
166 for (i = 0; i <= NFSD_SUPPORTED_MINOR_VERSION; i++)
167 vers[i] = nfsd_support_version(4);
173 nfsd_netns_free_versions(struct nfsd_net *nn)
175 kfree(nn->nfsd_versions);
176 kfree(nn->nfsd4_minorversions);
177 nn->nfsd_versions = NULL;
178 nn->nfsd4_minorversions = NULL;
182 nfsd_netns_init_versions(struct nfsd_net *nn)
184 if (!nn->nfsd_versions) {
185 nn->nfsd_versions = nfsd_alloc_versions();
186 nn->nfsd4_minorversions = nfsd_alloc_minorversions();
187 if (!nn->nfsd_versions || !nn->nfsd4_minorversions)
188 nfsd_netns_free_versions(nn);
192 int nfsd_vers(struct nfsd_net *nn, int vers, enum vers_op change)
194 if (vers < NFSD_MINVERS || vers >= NFSD_NRVERS)
198 if (nn->nfsd_versions)
199 nn->nfsd_versions[vers] = nfsd_support_version(vers);
202 nfsd_netns_init_versions(nn);
203 if (nn->nfsd_versions)
204 nn->nfsd_versions[vers] = false;
207 if (nn->nfsd_versions)
208 return nn->nfsd_versions[vers];
211 return nfsd_support_version(vers);
217 nfsd_adjust_nfsd_versions4(struct nfsd_net *nn)
221 for (i = 0; i <= NFSD_SUPPORTED_MINOR_VERSION; i++) {
222 if (nn->nfsd4_minorversions[i])
225 nfsd_vers(nn, 4, NFSD_CLEAR);
228 int nfsd_minorversion(struct nfsd_net *nn, u32 minorversion, enum vers_op change)
230 if (minorversion > NFSD_SUPPORTED_MINOR_VERSION &&
231 change != NFSD_AVAIL)
236 if (nn->nfsd4_minorversions) {
237 nfsd_vers(nn, 4, NFSD_SET);
238 nn->nfsd4_minorversions[minorversion] =
239 nfsd_vers(nn, 4, NFSD_TEST);
243 nfsd_netns_init_versions(nn);
244 if (nn->nfsd4_minorversions) {
245 nn->nfsd4_minorversions[minorversion] = false;
246 nfsd_adjust_nfsd_versions4(nn);
250 if (nn->nfsd4_minorversions)
251 return nn->nfsd4_minorversions[minorversion];
252 return nfsd_vers(nn, 4, NFSD_TEST);
254 return minorversion <= NFSD_SUPPORTED_MINOR_VERSION &&
255 nfsd_vers(nn, 4, NFSD_AVAIL);
261 * Maximum number of nfsd processes
263 #define NFSD_MAXSERVS 8192
265 int nfsd_nrthreads(struct net *net)
268 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
270 mutex_lock(&nfsd_mutex);
272 rv = nn->nfsd_serv->sv_nrthreads;
273 mutex_unlock(&nfsd_mutex);
277 static int nfsd_init_socks(struct net *net, const struct cred *cred)
280 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
282 if (!list_empty(&nn->nfsd_serv->sv_permsocks))
285 error = svc_xprt_create(nn->nfsd_serv, "udp", net, PF_INET, NFS_PORT,
286 SVC_SOCK_DEFAULTS, cred);
290 error = svc_xprt_create(nn->nfsd_serv, "tcp", net, PF_INET, NFS_PORT,
291 SVC_SOCK_DEFAULTS, cred);
298 static int nfsd_users = 0;
300 static int nfsd_startup_generic(void)
307 ret = nfsd_file_cache_init();
311 ret = nfs4_state_start();
317 nfsd_file_cache_shutdown();
323 static void nfsd_shutdown_generic(void)
328 nfs4_state_shutdown();
329 nfsd_file_cache_shutdown();
332 static bool nfsd_needs_lockd(struct nfsd_net *nn)
334 return nfsd_vers(nn, 2, NFSD_TEST) || nfsd_vers(nn, 3, NFSD_TEST);
338 * nfsd_copy_write_verifier - Atomically copy a write verifier
339 * @verf: buffer in which to receive the verifier cookie
340 * @nn: NFS net namespace
342 * This function provides a wait-free mechanism for copying the
343 * namespace's write verifier without tearing it.
345 void nfsd_copy_write_verifier(__be32 verf[2], struct nfsd_net *nn)
350 seq = read_seqbegin(&nn->writeverf_lock);
351 memcpy(verf, nn->writeverf, sizeof(nn->writeverf));
352 } while (read_seqretry(&nn->writeverf_lock, seq));
355 static void nfsd_reset_write_verifier_locked(struct nfsd_net *nn)
357 struct timespec64 now;
361 * Because the time value is hashed, y2038 time_t overflow
362 * is irrelevant in this usage.
364 ktime_get_raw_ts64(&now);
365 verf = siphash_2u64(now.tv_sec, now.tv_nsec, &nn->siphash_key);
366 memcpy(nn->writeverf, &verf, sizeof(nn->writeverf));
370 * nfsd_reset_write_verifier - Generate a new write verifier
371 * @nn: NFS net namespace
373 * This function updates the ->writeverf field of @nn. This field
374 * contains an opaque cookie that, according to Section 18.32.3 of
375 * RFC 8881, "the client can use to determine whether a server has
376 * changed instance state (e.g., server restart) between a call to
377 * WRITE and a subsequent call to either WRITE or COMMIT. This
378 * cookie MUST be unchanged during a single instance of the NFSv4.1
379 * server and MUST be unique between instances of the NFSv4.1
382 void nfsd_reset_write_verifier(struct nfsd_net *nn)
384 write_seqlock(&nn->writeverf_lock);
385 nfsd_reset_write_verifier_locked(nn);
386 write_sequnlock(&nn->writeverf_lock);
390 * Crank up a set of per-namespace resources for a new NFSD instance,
391 * including lockd, a duplicate reply cache, an open file cache
392 * instance, and a cache of NFSv4 state objects.
394 static int nfsd_startup_net(struct net *net, const struct cred *cred)
396 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
402 ret = nfsd_startup_generic();
405 ret = nfsd_init_socks(net, cred);
409 if (nfsd_needs_lockd(nn) && !nn->lockd_up) {
410 ret = lockd_up(net, cred);
416 ret = nfsd_file_cache_start_net(net);
420 ret = nfsd_reply_cache_init(nn);
424 ret = nfs4_state_start_net(net);
426 goto out_reply_cache;
428 #ifdef CONFIG_NFSD_V4_2_INTER_SSC
429 nfsd4_ssc_init_umount_work(nn);
431 nn->nfsd_net_up = true;
435 nfsd_reply_cache_shutdown(nn);
437 nfsd_file_cache_shutdown_net(net);
441 nn->lockd_up = false;
444 nfsd_shutdown_generic();
448 static void nfsd_shutdown_net(struct net *net)
450 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
452 nfs4_state_shutdown_net(net);
453 nfsd_reply_cache_shutdown(nn);
454 nfsd_file_cache_shutdown_net(net);
457 nn->lockd_up = false;
459 nn->nfsd_net_up = false;
460 nfsd_shutdown_generic();
463 static DEFINE_SPINLOCK(nfsd_notifier_lock);
464 static int nfsd_inetaddr_event(struct notifier_block *this, unsigned long event,
467 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
468 struct net_device *dev = ifa->ifa_dev->dev;
469 struct net *net = dev_net(dev);
470 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
471 struct sockaddr_in sin;
473 if (event != NETDEV_DOWN || !nn->nfsd_serv)
476 spin_lock(&nfsd_notifier_lock);
478 dprintk("nfsd_inetaddr_event: removed %pI4\n", &ifa->ifa_local);
479 sin.sin_family = AF_INET;
480 sin.sin_addr.s_addr = ifa->ifa_local;
481 svc_age_temp_xprts_now(nn->nfsd_serv, (struct sockaddr *)&sin);
483 spin_unlock(&nfsd_notifier_lock);
489 static struct notifier_block nfsd_inetaddr_notifier = {
490 .notifier_call = nfsd_inetaddr_event,
493 #if IS_ENABLED(CONFIG_IPV6)
494 static int nfsd_inet6addr_event(struct notifier_block *this,
495 unsigned long event, void *ptr)
497 struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
498 struct net_device *dev = ifa->idev->dev;
499 struct net *net = dev_net(dev);
500 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
501 struct sockaddr_in6 sin6;
503 if (event != NETDEV_DOWN || !nn->nfsd_serv)
506 spin_lock(&nfsd_notifier_lock);
508 dprintk("nfsd_inet6addr_event: removed %pI6\n", &ifa->addr);
509 sin6.sin6_family = AF_INET6;
510 sin6.sin6_addr = ifa->addr;
511 if (ipv6_addr_type(&sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
512 sin6.sin6_scope_id = ifa->idev->dev->ifindex;
513 svc_age_temp_xprts_now(nn->nfsd_serv, (struct sockaddr *)&sin6);
515 spin_unlock(&nfsd_notifier_lock);
521 static struct notifier_block nfsd_inet6addr_notifier = {
522 .notifier_call = nfsd_inet6addr_event,
526 /* Only used under nfsd_mutex, so this atomic may be overkill: */
527 static atomic_t nfsd_notifier_refcount = ATOMIC_INIT(0);
530 * nfsd_destroy_serv - tear down NFSD's svc_serv for a namespace
531 * @net: network namespace the NFS service is associated with
533 void nfsd_destroy_serv(struct net *net)
535 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
536 struct svc_serv *serv = nn->nfsd_serv;
538 spin_lock(&nfsd_notifier_lock);
539 nn->nfsd_serv = NULL;
540 spin_unlock(&nfsd_notifier_lock);
542 /* check if the notifier still has clients */
543 if (atomic_dec_return(&nfsd_notifier_refcount) == 0) {
544 unregister_inetaddr_notifier(&nfsd_inetaddr_notifier);
545 #if IS_ENABLED(CONFIG_IPV6)
546 unregister_inet6addr_notifier(&nfsd_inet6addr_notifier);
550 svc_xprt_destroy_all(serv, net);
553 * write_ports can create the server without actually starting
554 * any threads--if we get shut down before any threads are
555 * started, then nfsd_destroy_serv will be run before any of this
556 * other initialization has been done except the rpcb information.
558 svc_rpcb_cleanup(serv, net);
559 if (!nn->nfsd_net_up)
562 nfsd_shutdown_net(net);
563 nfsd_export_flush(net);
567 void nfsd_reset_versions(struct nfsd_net *nn)
571 for (i = 0; i < NFSD_NRVERS; i++)
572 if (nfsd_vers(nn, i, NFSD_TEST))
575 for (i = 0; i < NFSD_NRVERS; i++)
577 nfsd_vers(nn, i, NFSD_SET);
580 while (nfsd_minorversion(nn, minor, NFSD_SET) >= 0)
586 * Each session guarantees a negotiated per slot memory cache for replies
587 * which in turn consumes memory beyond the v2/v3/v4.0 server. A dedicated
588 * NFSv4.1 server might want to use more memory for a DRC than a machine
589 * with mutiple services.
591 * Impose a hard limit on the number of pages for the DRC which varies
592 * according to the machines free pages. This is of course only a default.
594 * For now this is a #defined shift which could be under admin control
597 static void set_max_drc(void)
599 #define NFSD_DRC_SIZE_SHIFT 7
600 nfsd_drc_max_mem = (nr_free_buffer_pages()
601 >> NFSD_DRC_SIZE_SHIFT) * PAGE_SIZE;
602 nfsd_drc_mem_used = 0;
603 dprintk("%s nfsd_drc_max_mem %lu \n", __func__, nfsd_drc_max_mem);
606 static int nfsd_get_default_max_blksize(void)
609 unsigned long long target;
613 target = (i.totalram - i.totalhigh) << PAGE_SHIFT;
615 * Aim for 1/4096 of memory per thread This gives 1MB on 4Gig
616 * machines, but only uses 32K on 128M machines. Bottom out at
617 * 8K on 32M and smaller. Of course, this is only a default.
621 ret = NFSSVC_MAXBLKSIZE;
622 while (ret > target && ret >= 8*1024*2)
627 void nfsd_shutdown_threads(struct net *net)
629 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
630 struct svc_serv *serv;
632 mutex_lock(&nfsd_mutex);
633 serv = nn->nfsd_serv;
635 mutex_unlock(&nfsd_mutex);
639 /* Kill outstanding nfsd threads */
640 svc_set_num_threads(serv, NULL, 0);
641 nfsd_destroy_serv(net);
642 mutex_unlock(&nfsd_mutex);
647 return kthread_func(current) == nfsd;
650 int nfsd_create_serv(struct net *net)
653 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
654 struct svc_serv *serv;
656 WARN_ON(!mutex_is_locked(&nfsd_mutex));
660 if (nfsd_max_blksize == 0)
661 nfsd_max_blksize = nfsd_get_default_max_blksize();
662 nfsd_reset_versions(nn);
663 serv = svc_create_pooled(&nfsd_program, &nn->nfsd_svcstats,
664 nfsd_max_blksize, nfsd);
668 serv->sv_maxconn = nn->max_connections;
669 error = svc_bind(serv, net);
674 spin_lock(&nfsd_notifier_lock);
675 nn->nfsd_info.mutex = &nfsd_mutex;
676 nn->nfsd_serv = serv;
677 spin_unlock(&nfsd_notifier_lock);
680 /* check if the notifier is already set */
681 if (atomic_inc_return(&nfsd_notifier_refcount) == 1) {
682 register_inetaddr_notifier(&nfsd_inetaddr_notifier);
683 #if IS_ENABLED(CONFIG_IPV6)
684 register_inet6addr_notifier(&nfsd_inet6addr_notifier);
687 nfsd_reset_write_verifier(nn);
691 int nfsd_nrpools(struct net *net)
693 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
695 if (nn->nfsd_serv == NULL)
698 return nn->nfsd_serv->sv_nrpools;
701 int nfsd_get_nrthreads(int n, int *nthreads, struct net *net)
703 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
704 struct svc_serv *serv = nn->nfsd_serv;
708 for (i = 0; i < serv->sv_nrpools && i < n; i++)
709 nthreads[i] = atomic_read(&serv->sv_pools[i].sp_nrthreads);
713 int nfsd_set_nrthreads(int n, int *nthreads, struct net *net)
718 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
720 WARN_ON(!mutex_is_locked(&nfsd_mutex));
722 if (nn->nfsd_serv == NULL || n <= 0)
725 if (n > nn->nfsd_serv->sv_nrpools)
726 n = nn->nfsd_serv->sv_nrpools;
728 /* enforce a global maximum number of threads */
730 for (i = 0; i < n; i++) {
731 nthreads[i] = min(nthreads[i], NFSD_MAXSERVS);
734 if (tot > NFSD_MAXSERVS) {
735 /* total too large: scale down requested numbers */
736 for (i = 0; i < n && tot > 0; i++) {
737 int new = nthreads[i] * NFSD_MAXSERVS / tot;
738 tot -= (nthreads[i] - new);
741 for (i = 0; i < n && tot > 0; i++) {
748 * There must always be a thread in pool 0; the admin
749 * can't shut down NFS completely using pool_threads.
751 if (nthreads[0] == 0)
754 /* apply the new numbers */
755 for (i = 0; i < n; i++) {
756 err = svc_set_num_threads(nn->nfsd_serv,
757 &nn->nfsd_serv->sv_pools[i],
766 * Adjust the number of threads and return the new number of threads.
767 * This is also the function that starts the server if necessary, if
768 * this is the first time nrservs is nonzero.
771 nfsd_svc(int nrservs, struct net *net, const struct cred *cred, const char *scope)
774 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
775 struct svc_serv *serv;
777 lockdep_assert_held(&nfsd_mutex);
779 dprintk("nfsd: creating service\n");
781 nrservs = max(nrservs, 0);
782 nrservs = min(nrservs, NFSD_MAXSERVS);
785 if (nrservs == 0 && nn->nfsd_serv == NULL)
788 strscpy(nn->nfsd_name, scope ? scope : utsname()->nodename,
789 sizeof(nn->nfsd_name));
791 error = nfsd_create_serv(net);
794 serv = nn->nfsd_serv;
796 error = nfsd_startup_net(net, cred);
799 error = svc_set_num_threads(serv, NULL, nrservs);
802 error = serv->sv_nrthreads;
804 if (serv->sv_nrthreads == 0)
805 nfsd_destroy_serv(net);
810 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
812 nfsd_support_acl_version(int vers)
814 if (vers >= NFSD_ACL_MINVERS && vers < NFSD_ACL_NRVERS)
815 return nfsd_acl_version[vers] != NULL;
820 nfsd_acl_rpcbind_set(struct net *net, const struct svc_program *progp,
821 u32 version, int family, unsigned short proto,
824 if (!nfsd_support_acl_version(version) ||
825 !nfsd_vers(net_generic(net, nfsd_net_id), version, NFSD_TEST))
827 return svc_generic_rpcbind_set(net, progp, version, family,
832 nfsd_acl_init_request(struct svc_rqst *rqstp,
833 const struct svc_program *progp,
834 struct svc_process_info *ret)
836 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
839 if (likely(nfsd_support_acl_version(rqstp->rq_vers) &&
840 nfsd_vers(nn, rqstp->rq_vers, NFSD_TEST)))
841 return svc_generic_init_request(rqstp, progp, ret);
843 ret->mismatch.lovers = NFSD_ACL_NRVERS;
844 for (i = NFSD_ACL_MINVERS; i < NFSD_ACL_NRVERS; i++) {
845 if (nfsd_support_acl_version(rqstp->rq_vers) &&
846 nfsd_vers(nn, i, NFSD_TEST)) {
847 ret->mismatch.lovers = i;
851 if (ret->mismatch.lovers == NFSD_ACL_NRVERS)
852 return rpc_prog_unavail;
853 ret->mismatch.hivers = NFSD_ACL_MINVERS;
854 for (i = NFSD_ACL_NRVERS - 1; i >= NFSD_ACL_MINVERS; i--) {
855 if (nfsd_support_acl_version(rqstp->rq_vers) &&
856 nfsd_vers(nn, i, NFSD_TEST)) {
857 ret->mismatch.hivers = i;
861 return rpc_prog_mismatch;
866 nfsd_rpcbind_set(struct net *net, const struct svc_program *progp,
867 u32 version, int family, unsigned short proto,
870 if (!nfsd_vers(net_generic(net, nfsd_net_id), version, NFSD_TEST))
872 return svc_generic_rpcbind_set(net, progp, version, family,
877 nfsd_init_request(struct svc_rqst *rqstp,
878 const struct svc_program *progp,
879 struct svc_process_info *ret)
881 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
884 if (likely(nfsd_vers(nn, rqstp->rq_vers, NFSD_TEST)))
885 return svc_generic_init_request(rqstp, progp, ret);
887 ret->mismatch.lovers = NFSD_NRVERS;
888 for (i = NFSD_MINVERS; i < NFSD_NRVERS; i++) {
889 if (nfsd_vers(nn, i, NFSD_TEST)) {
890 ret->mismatch.lovers = i;
894 if (ret->mismatch.lovers == NFSD_NRVERS)
895 return rpc_prog_unavail;
896 ret->mismatch.hivers = NFSD_MINVERS;
897 for (i = NFSD_NRVERS - 1; i >= NFSD_MINVERS; i--) {
898 if (nfsd_vers(nn, i, NFSD_TEST)) {
899 ret->mismatch.hivers = i;
903 return rpc_prog_mismatch;
907 * This is the NFS server kernel thread
912 struct svc_rqst *rqstp = (struct svc_rqst *) vrqstp;
913 struct svc_xprt *perm_sock = list_entry(rqstp->rq_server->sv_permsocks.next, typeof(struct svc_xprt), xpt_list);
914 struct net *net = perm_sock->xpt_net;
915 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
917 /* At this point, the thread shares current->fs
918 * with the init process. We need to create files with the
919 * umask as defined by the client instead of init's umask. */
920 if (unshare_fs_struct() < 0) {
921 printk("Unable to start nfsd thread: out of memory\n");
925 current->fs->umask = 0;
927 atomic_inc(&nfsd_th_cnt);
932 * The main request loop
934 while (!svc_thread_should_stop(rqstp)) {
935 /* Update sv_maxconn if it has changed */
936 rqstp->rq_server->sv_maxconn = nn->max_connections;
940 nfsd_file_net_dispose(nn);
943 atomic_dec(&nfsd_th_cnt);
946 /* Release the thread */
947 svc_exit_thread(rqstp);
952 * nfsd_dispatch - Process an NFS or NFSACL Request
953 * @rqstp: incoming request
955 * This RPC dispatcher integrates the NFS server's duplicate reply cache.
958 * %0: Processing complete; do not send a Reply
959 * %1: Processing complete; send Reply in rqstp->rq_res
961 int nfsd_dispatch(struct svc_rqst *rqstp)
963 const struct svc_procedure *proc = rqstp->rq_procinfo;
964 __be32 *statp = rqstp->rq_accept_statp;
965 struct nfsd_cacherep *rp;
966 unsigned int start, len;
970 * Give the xdr decoder a chance to change this if it wants
971 * (necessary in the NFSv4.0 compound case)
973 rqstp->rq_cachetype = proc->pc_cachetype;
976 * ->pc_decode advances the argument stream past the NFS
977 * Call header, so grab the header's starting location and
978 * size now for the call to nfsd_cache_lookup().
980 start = xdr_stream_pos(&rqstp->rq_arg_stream);
981 len = xdr_stream_remaining(&rqstp->rq_arg_stream);
982 if (!proc->pc_decode(rqstp, &rqstp->rq_arg_stream))
986 * Release rq_status_counter setting it to an odd value after the rpc
987 * request has been properly parsed. rq_status_counter is used to
988 * notify the consumers if the rqstp fields are stable
989 * (rq_status_counter is odd) or not meaningful (rq_status_counter
992 smp_store_release(&rqstp->rq_status_counter, rqstp->rq_status_counter | 1);
995 switch (nfsd_cache_lookup(rqstp, start, len, &rp)) {
999 goto out_cached_reply;
1004 nfs_reply = xdr_inline_decode(&rqstp->rq_res_stream, 0);
1005 *statp = proc->pc_func(rqstp);
1006 if (test_bit(RQ_DROPME, &rqstp->rq_flags))
1007 goto out_update_drop;
1009 if (!proc->pc_encode(rqstp, &rqstp->rq_res_stream))
1010 goto out_encode_err;
1013 * Release rq_status_counter setting it to an even value after the rpc
1014 * request has been properly processed.
1016 smp_store_release(&rqstp->rq_status_counter, rqstp->rq_status_counter + 1);
1018 nfsd_cache_update(rqstp, rp, rqstp->rq_cachetype, nfs_reply);
1023 trace_nfsd_garbage_args_err(rqstp);
1024 *statp = rpc_garbage_args;
1028 nfsd_cache_update(rqstp, rp, RC_NOCACHE, NULL);
1033 trace_nfsd_cant_encode_err(rqstp);
1034 nfsd_cache_update(rqstp, rp, RC_NOCACHE, NULL);
1035 *statp = rpc_system_err;
1040 * nfssvc_decode_voidarg - Decode void arguments
1041 * @rqstp: Server RPC transaction context
1042 * @xdr: XDR stream positioned at arguments to decode
1045 * %false: Arguments were not valid
1046 * %true: Decoding was successful
1048 bool nfssvc_decode_voidarg(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1054 * nfssvc_encode_voidres - Encode void results
1055 * @rqstp: Server RPC transaction context
1056 * @xdr: XDR stream into which to encode results
1059 * %false: Local error while encoding
1060 * %true: Encoding was successful
1062 bool nfssvc_encode_voidres(struct svc_rqst *rqstp, struct xdr_stream *xdr)
1067 int nfsd_pool_stats_open(struct inode *inode, struct file *file)
1069 struct nfsd_net *nn = net_generic(inode->i_sb->s_fs_info, nfsd_net_id);
1071 return svc_pool_stats_open(&nn->nfsd_info, file);