1 // SPDX-License-Identifier: GPL-2.0
3 * Central processing for nfsd.
5 * Authors: Olaf Kirch (okir@monad.swb.de)
7 * Copyright (C) 1995, 1996, 1997 Olaf Kirch <okir@monad.swb.de>
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>
16 #include <linux/sunrpc/stats.h>
17 #include <linux/sunrpc/svcsock.h>
18 #include <linux/sunrpc/svc_xprt.h>
19 #include <linux/lockd/bind.h>
20 #include <linux/nfsacl.h>
21 #include <linux/seq_file.h>
22 #include <linux/inetdevice.h>
23 #include <net/addrconf.h>
25 #include <net/net_namespace.h>
30 #include "filecache.h"
34 #define NFSDDBG_FACILITY NFSDDBG_SVC
36 bool inter_copy_offload_enable
;
37 EXPORT_SYMBOL_GPL(inter_copy_offload_enable
);
38 module_param(inter_copy_offload_enable
, bool, 0644);
39 MODULE_PARM_DESC(inter_copy_offload_enable
,
40 "Enable inter server to server copy offload. Default: false");
42 extern struct svc_program nfsd_program
;
43 static int nfsd(void *vrqstp
);
44 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
45 static int nfsd_acl_rpcbind_set(struct net
*,
46 const struct svc_program
*,
50 static __be32
nfsd_acl_init_request(struct svc_rqst
*,
51 const struct svc_program
*,
52 struct svc_process_info
*);
54 static int nfsd_rpcbind_set(struct net
*,
55 const struct svc_program
*,
59 static __be32
nfsd_init_request(struct svc_rqst
*,
60 const struct svc_program
*,
61 struct svc_process_info
*);
64 * nfsd_mutex protects nn->nfsd_serv -- both the pointer itself and the members
65 * of the svc_serv struct. In particular, ->sv_nrthreads but also to some
66 * extent ->sv_temp_socks and ->sv_permsocks. It also protects nfsdstats.th_cnt
68 * If (out side the lock) nn->nfsd_serv is non-NULL, then it must point to a
69 * properly initialised 'struct svc_serv' with ->sv_nrthreads > 0. That number
70 * of nfsd threads must exist and each must listed in ->sp_all_threads in each
71 * entry of ->sv_pools[].
73 * Transitions of the thread count between zero and non-zero are of particular
74 * interest since the svc_serv needs to be created and initialized at that
77 * Finally, the nfsd_mutex also protects some of the global variables that are
78 * accessed when nfsd starts and that are settable via the write_* routines in
79 * nfsctl.c. In particular:
81 * user_recovery_dirname
85 DEFINE_MUTEX(nfsd_mutex
);
88 * nfsd_drc_lock protects nfsd_drc_max_pages and nfsd_drc_pages_used.
89 * nfsd_drc_max_pages limits the total amount of memory available for
90 * version 4.1 DRC caches.
91 * nfsd_drc_pages_used tracks the current version 4.1 DRC memory usage.
93 spinlock_t nfsd_drc_lock
;
94 unsigned long nfsd_drc_max_mem
;
95 unsigned long nfsd_drc_mem_used
;
97 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
98 static struct svc_stat nfsd_acl_svcstats
;
99 static const struct svc_version
*nfsd_acl_version
[] = {
100 [2] = &nfsd_acl_version2
,
101 [3] = &nfsd_acl_version3
,
104 #define NFSD_ACL_MINVERS 2
105 #define NFSD_ACL_NRVERS ARRAY_SIZE(nfsd_acl_version)
107 static struct svc_program nfsd_acl_program
= {
108 .pg_prog
= NFS_ACL_PROGRAM
,
109 .pg_nvers
= NFSD_ACL_NRVERS
,
110 .pg_vers
= nfsd_acl_version
,
113 .pg_stats
= &nfsd_acl_svcstats
,
114 .pg_authenticate
= &svc_set_client
,
115 .pg_init_request
= nfsd_acl_init_request
,
116 .pg_rpcbind_set
= nfsd_acl_rpcbind_set
,
119 static struct svc_stat nfsd_acl_svcstats
= {
120 .program
= &nfsd_acl_program
,
122 #endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */
124 static const struct svc_version
*nfsd_version
[] = {
125 [2] = &nfsd_version2
,
126 #if defined(CONFIG_NFSD_V3)
127 [3] = &nfsd_version3
,
129 #if defined(CONFIG_NFSD_V4)
130 [4] = &nfsd_version4
,
134 #define NFSD_MINVERS 2
135 #define NFSD_NRVERS ARRAY_SIZE(nfsd_version)
137 struct svc_program nfsd_program
= {
138 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
139 .pg_next
= &nfsd_acl_program
,
141 .pg_prog
= NFS_PROGRAM
, /* program number */
142 .pg_nvers
= NFSD_NRVERS
, /* nr of entries in nfsd_version */
143 .pg_vers
= nfsd_version
, /* version table */
144 .pg_name
= "nfsd", /* program name */
145 .pg_class
= "nfsd", /* authentication class */
146 .pg_stats
= &nfsd_svcstats
, /* version table */
147 .pg_authenticate
= &svc_set_client
, /* export authentication */
148 .pg_init_request
= nfsd_init_request
,
149 .pg_rpcbind_set
= nfsd_rpcbind_set
,
153 nfsd_support_version(int vers
)
155 if (vers
>= NFSD_MINVERS
&& vers
< NFSD_NRVERS
)
156 return nfsd_version
[vers
] != NULL
;
161 nfsd_alloc_versions(void)
163 bool *vers
= kmalloc_array(NFSD_NRVERS
, sizeof(bool), GFP_KERNEL
);
167 /* All compiled versions are enabled by default */
168 for (i
= 0; i
< NFSD_NRVERS
; i
++)
169 vers
[i
] = nfsd_support_version(i
);
175 nfsd_alloc_minorversions(void)
177 bool *vers
= kmalloc_array(NFSD_SUPPORTED_MINOR_VERSION
+ 1,
178 sizeof(bool), GFP_KERNEL
);
182 /* All minor versions are enabled by default */
183 for (i
= 0; i
<= NFSD_SUPPORTED_MINOR_VERSION
; i
++)
184 vers
[i
] = nfsd_support_version(4);
190 nfsd_netns_free_versions(struct nfsd_net
*nn
)
192 kfree(nn
->nfsd_versions
);
193 kfree(nn
->nfsd4_minorversions
);
194 nn
->nfsd_versions
= NULL
;
195 nn
->nfsd4_minorversions
= NULL
;
199 nfsd_netns_init_versions(struct nfsd_net
*nn
)
201 if (!nn
->nfsd_versions
) {
202 nn
->nfsd_versions
= nfsd_alloc_versions();
203 nn
->nfsd4_minorversions
= nfsd_alloc_minorversions();
204 if (!nn
->nfsd_versions
|| !nn
->nfsd4_minorversions
)
205 nfsd_netns_free_versions(nn
);
209 int nfsd_vers(struct nfsd_net
*nn
, int vers
, enum vers_op change
)
211 if (vers
< NFSD_MINVERS
|| vers
>= NFSD_NRVERS
)
215 if (nn
->nfsd_versions
)
216 nn
->nfsd_versions
[vers
] = nfsd_support_version(vers
);
219 nfsd_netns_init_versions(nn
);
220 if (nn
->nfsd_versions
)
221 nn
->nfsd_versions
[vers
] = false;
224 if (nn
->nfsd_versions
)
225 return nn
->nfsd_versions
[vers
];
228 return nfsd_support_version(vers
);
234 nfsd_adjust_nfsd_versions4(struct nfsd_net
*nn
)
238 for (i
= 0; i
<= NFSD_SUPPORTED_MINOR_VERSION
; i
++) {
239 if (nn
->nfsd4_minorversions
[i
])
242 nfsd_vers(nn
, 4, NFSD_CLEAR
);
245 int nfsd_minorversion(struct nfsd_net
*nn
, u32 minorversion
, enum vers_op change
)
247 if (minorversion
> NFSD_SUPPORTED_MINOR_VERSION
&&
248 change
!= NFSD_AVAIL
)
253 if (nn
->nfsd4_minorversions
) {
254 nfsd_vers(nn
, 4, NFSD_SET
);
255 nn
->nfsd4_minorversions
[minorversion
] =
256 nfsd_vers(nn
, 4, NFSD_TEST
);
260 nfsd_netns_init_versions(nn
);
261 if (nn
->nfsd4_minorversions
) {
262 nn
->nfsd4_minorversions
[minorversion
] = false;
263 nfsd_adjust_nfsd_versions4(nn
);
267 if (nn
->nfsd4_minorversions
)
268 return nn
->nfsd4_minorversions
[minorversion
];
269 return nfsd_vers(nn
, 4, NFSD_TEST
);
271 return minorversion
<= NFSD_SUPPORTED_MINOR_VERSION
&&
272 nfsd_vers(nn
, 4, NFSD_AVAIL
);
278 * Maximum number of nfsd processes
280 #define NFSD_MAXSERVS 8192
282 int nfsd_nrthreads(struct net
*net
)
285 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
287 mutex_lock(&nfsd_mutex
);
289 rv
= nn
->nfsd_serv
->sv_nrthreads
;
290 mutex_unlock(&nfsd_mutex
);
294 static int nfsd_init_socks(struct net
*net
, const struct cred
*cred
)
297 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
299 if (!list_empty(&nn
->nfsd_serv
->sv_permsocks
))
302 error
= svc_create_xprt(nn
->nfsd_serv
, "udp", net
, PF_INET
, NFS_PORT
,
303 SVC_SOCK_DEFAULTS
, cred
);
307 error
= svc_create_xprt(nn
->nfsd_serv
, "tcp", net
, PF_INET
, NFS_PORT
,
308 SVC_SOCK_DEFAULTS
, cred
);
315 static int nfsd_users
= 0;
317 static int nfsd_startup_generic(int nrservs
)
324 ret
= nfsd_file_cache_init();
328 ret
= nfs4_state_start();
334 nfsd_file_cache_shutdown();
340 static void nfsd_shutdown_generic(void)
345 nfs4_state_shutdown();
346 nfsd_file_cache_shutdown();
349 static bool nfsd_needs_lockd(struct nfsd_net
*nn
)
351 return nfsd_vers(nn
, 2, NFSD_TEST
) || nfsd_vers(nn
, 3, NFSD_TEST
);
354 void nfsd_copy_boot_verifier(__be32 verf
[2], struct nfsd_net
*nn
)
359 read_seqbegin_or_lock(&nn
->boot_lock
, &seq
);
361 * This is opaque to client, so no need to byte-swap. Use
362 * __force to keep sparse happy. y2038 time_t overflow is
363 * irrelevant in this usage
365 verf
[0] = (__force __be32
)nn
->nfssvc_boot
.tv_sec
;
366 verf
[1] = (__force __be32
)nn
->nfssvc_boot
.tv_nsec
;
367 } while (need_seqretry(&nn
->boot_lock
, seq
));
368 done_seqretry(&nn
->boot_lock
, seq
);
371 static void nfsd_reset_boot_verifier_locked(struct nfsd_net
*nn
)
373 ktime_get_real_ts64(&nn
->nfssvc_boot
);
376 void nfsd_reset_boot_verifier(struct nfsd_net
*nn
)
378 write_seqlock(&nn
->boot_lock
);
379 nfsd_reset_boot_verifier_locked(nn
);
380 write_sequnlock(&nn
->boot_lock
);
383 static int nfsd_startup_net(int nrservs
, struct net
*net
, const struct cred
*cred
)
385 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
391 ret
= nfsd_startup_generic(nrservs
);
394 ret
= nfsd_init_socks(net
, cred
);
398 if (nfsd_needs_lockd(nn
) && !nn
->lockd_up
) {
399 ret
= lockd_up(net
, cred
);
405 ret
= nfsd_file_cache_start_net(net
);
408 ret
= nfs4_state_start_net(net
);
412 nn
->nfsd_net_up
= true;
416 nfsd_file_cache_shutdown_net(net
);
420 nn
->lockd_up
= false;
423 nfsd_shutdown_generic();
427 static void nfsd_shutdown_net(struct net
*net
)
429 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
431 nfsd_file_cache_shutdown_net(net
);
432 nfs4_state_shutdown_net(net
);
435 nn
->lockd_up
= false;
437 nn
->nfsd_net_up
= false;
438 nfsd_shutdown_generic();
441 static int nfsd_inetaddr_event(struct notifier_block
*this, unsigned long event
,
444 struct in_ifaddr
*ifa
= (struct in_ifaddr
*)ptr
;
445 struct net_device
*dev
= ifa
->ifa_dev
->dev
;
446 struct net
*net
= dev_net(dev
);
447 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
448 struct sockaddr_in sin
;
450 if ((event
!= NETDEV_DOWN
) ||
451 !atomic_inc_not_zero(&nn
->ntf_refcnt
))
455 dprintk("nfsd_inetaddr_event: removed %pI4\n", &ifa
->ifa_local
);
456 sin
.sin_family
= AF_INET
;
457 sin
.sin_addr
.s_addr
= ifa
->ifa_local
;
458 svc_age_temp_xprts_now(nn
->nfsd_serv
, (struct sockaddr
*)&sin
);
460 atomic_dec(&nn
->ntf_refcnt
);
461 wake_up(&nn
->ntf_wq
);
467 static struct notifier_block nfsd_inetaddr_notifier
= {
468 .notifier_call
= nfsd_inetaddr_event
,
471 #if IS_ENABLED(CONFIG_IPV6)
472 static int nfsd_inet6addr_event(struct notifier_block
*this,
473 unsigned long event
, void *ptr
)
475 struct inet6_ifaddr
*ifa
= (struct inet6_ifaddr
*)ptr
;
476 struct net_device
*dev
= ifa
->idev
->dev
;
477 struct net
*net
= dev_net(dev
);
478 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
479 struct sockaddr_in6 sin6
;
481 if ((event
!= NETDEV_DOWN
) ||
482 !atomic_inc_not_zero(&nn
->ntf_refcnt
))
486 dprintk("nfsd_inet6addr_event: removed %pI6\n", &ifa
->addr
);
487 sin6
.sin6_family
= AF_INET6
;
488 sin6
.sin6_addr
= ifa
->addr
;
489 if (ipv6_addr_type(&sin6
.sin6_addr
) & IPV6_ADDR_LINKLOCAL
)
490 sin6
.sin6_scope_id
= ifa
->idev
->dev
->ifindex
;
491 svc_age_temp_xprts_now(nn
->nfsd_serv
, (struct sockaddr
*)&sin6
);
493 atomic_dec(&nn
->ntf_refcnt
);
494 wake_up(&nn
->ntf_wq
);
499 static struct notifier_block nfsd_inet6addr_notifier
= {
500 .notifier_call
= nfsd_inet6addr_event
,
504 /* Only used under nfsd_mutex, so this atomic may be overkill: */
505 static atomic_t nfsd_notifier_refcount
= ATOMIC_INIT(0);
507 static void nfsd_last_thread(struct svc_serv
*serv
, struct net
*net
)
509 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
511 atomic_dec(&nn
->ntf_refcnt
);
512 /* check if the notifier still has clients */
513 if (atomic_dec_return(&nfsd_notifier_refcount
) == 0) {
514 unregister_inetaddr_notifier(&nfsd_inetaddr_notifier
);
515 #if IS_ENABLED(CONFIG_IPV6)
516 unregister_inet6addr_notifier(&nfsd_inet6addr_notifier
);
519 wait_event(nn
->ntf_wq
, atomic_read(&nn
->ntf_refcnt
) == 0);
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_last_thread will be run before any of this
525 * other initialization has been done except the rpcb information.
527 svc_rpcb_cleanup(serv
, net
);
528 if (!nn
->nfsd_net_up
)
531 nfsd_shutdown_net(net
);
532 pr_info("nfsd: last server has exited, flushing export cache\n");
533 nfsd_export_flush(net
);
536 void nfsd_reset_versions(struct nfsd_net
*nn
)
540 for (i
= 0; i
< NFSD_NRVERS
; i
++)
541 if (nfsd_vers(nn
, i
, NFSD_TEST
))
544 for (i
= 0; i
< NFSD_NRVERS
; i
++)
546 nfsd_vers(nn
, i
, NFSD_SET
);
549 while (nfsd_minorversion(nn
, minor
, NFSD_SET
) >= 0)
555 * Each session guarantees a negotiated per slot memory cache for replies
556 * which in turn consumes memory beyond the v2/v3/v4.0 server. A dedicated
557 * NFSv4.1 server might want to use more memory for a DRC than a machine
558 * with mutiple services.
560 * Impose a hard limit on the number of pages for the DRC which varies
561 * according to the machines free pages. This is of course only a default.
563 * For now this is a #defined shift which could be under admin control
566 static void set_max_drc(void)
568 #define NFSD_DRC_SIZE_SHIFT 7
569 nfsd_drc_max_mem
= (nr_free_buffer_pages()
570 >> NFSD_DRC_SIZE_SHIFT
) * PAGE_SIZE
;
571 nfsd_drc_mem_used
= 0;
572 spin_lock_init(&nfsd_drc_lock
);
573 dprintk("%s nfsd_drc_max_mem %lu \n", __func__
, nfsd_drc_max_mem
);
576 static int nfsd_get_default_max_blksize(void)
579 unsigned long long target
;
583 target
= (i
.totalram
- i
.totalhigh
) << PAGE_SHIFT
;
585 * Aim for 1/4096 of memory per thread This gives 1MB on 4Gig
586 * machines, but only uses 32K on 128M machines. Bottom out at
587 * 8K on 32M and smaller. Of course, this is only a default.
591 ret
= NFSSVC_MAXBLKSIZE
;
592 while (ret
> target
&& ret
>= 8*1024*2)
597 static const struct svc_serv_ops nfsd_thread_sv_ops
= {
598 .svo_shutdown
= nfsd_last_thread
,
599 .svo_function
= nfsd
,
600 .svo_enqueue_xprt
= svc_xprt_do_enqueue
,
601 .svo_setup
= svc_set_num_threads
,
602 .svo_module
= THIS_MODULE
,
607 return kthread_func(current
) == nfsd
;
610 int nfsd_create_serv(struct net
*net
)
613 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
615 WARN_ON(!mutex_is_locked(&nfsd_mutex
));
617 svc_get(nn
->nfsd_serv
);
620 if (nfsd_max_blksize
== 0)
621 nfsd_max_blksize
= nfsd_get_default_max_blksize();
622 nfsd_reset_versions(nn
);
623 nn
->nfsd_serv
= svc_create_pooled(&nfsd_program
, nfsd_max_blksize
,
624 &nfsd_thread_sv_ops
);
625 if (nn
->nfsd_serv
== NULL
)
628 nn
->nfsd_serv
->sv_maxconn
= nn
->max_connections
;
629 error
= svc_bind(nn
->nfsd_serv
, net
);
631 svc_destroy(nn
->nfsd_serv
);
636 /* check if the notifier is already set */
637 if (atomic_inc_return(&nfsd_notifier_refcount
) == 1) {
638 register_inetaddr_notifier(&nfsd_inetaddr_notifier
);
639 #if IS_ENABLED(CONFIG_IPV6)
640 register_inet6addr_notifier(&nfsd_inet6addr_notifier
);
643 atomic_inc(&nn
->ntf_refcnt
);
644 nfsd_reset_boot_verifier(nn
);
648 int nfsd_nrpools(struct net
*net
)
650 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
652 if (nn
->nfsd_serv
== NULL
)
655 return nn
->nfsd_serv
->sv_nrpools
;
658 int nfsd_get_nrthreads(int n
, int *nthreads
, struct net
*net
)
661 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
663 if (nn
->nfsd_serv
!= NULL
) {
664 for (i
= 0; i
< nn
->nfsd_serv
->sv_nrpools
&& i
< n
; i
++)
665 nthreads
[i
] = nn
->nfsd_serv
->sv_pools
[i
].sp_nrthreads
;
671 void nfsd_destroy(struct net
*net
)
673 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
674 int destroy
= (nn
->nfsd_serv
->sv_nrthreads
== 1);
677 svc_shutdown_net(nn
->nfsd_serv
, net
);
678 svc_destroy(nn
->nfsd_serv
);
680 nn
->nfsd_serv
= NULL
;
683 int nfsd_set_nrthreads(int n
, int *nthreads
, struct net
*net
)
688 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
690 WARN_ON(!mutex_is_locked(&nfsd_mutex
));
692 if (nn
->nfsd_serv
== NULL
|| n
<= 0)
695 if (n
> nn
->nfsd_serv
->sv_nrpools
)
696 n
= nn
->nfsd_serv
->sv_nrpools
;
698 /* enforce a global maximum number of threads */
700 for (i
= 0; i
< n
; i
++) {
701 nthreads
[i
] = min(nthreads
[i
], NFSD_MAXSERVS
);
704 if (tot
> NFSD_MAXSERVS
) {
705 /* total too large: scale down requested numbers */
706 for (i
= 0; i
< n
&& tot
> 0; i
++) {
707 int new = nthreads
[i
] * NFSD_MAXSERVS
/ tot
;
708 tot
-= (nthreads
[i
] - new);
711 for (i
= 0; i
< n
&& tot
> 0; i
++) {
718 * There must always be a thread in pool 0; the admin
719 * can't shut down NFS completely using pool_threads.
721 if (nthreads
[0] == 0)
724 /* apply the new numbers */
725 svc_get(nn
->nfsd_serv
);
726 for (i
= 0; i
< n
; i
++) {
727 err
= nn
->nfsd_serv
->sv_ops
->svo_setup(nn
->nfsd_serv
,
728 &nn
->nfsd_serv
->sv_pools
[i
], nthreads
[i
]);
737 * Adjust the number of threads and return the new number of threads.
738 * This is also the function that starts the server if necessary, if
739 * this is the first time nrservs is nonzero.
742 nfsd_svc(int nrservs
, struct net
*net
, const struct cred
*cred
)
746 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
748 mutex_lock(&nfsd_mutex
);
749 dprintk("nfsd: creating service\n");
751 nrservs
= max(nrservs
, 0);
752 nrservs
= min(nrservs
, NFSD_MAXSERVS
);
755 if (nrservs
== 0 && nn
->nfsd_serv
== NULL
)
758 strlcpy(nn
->nfsd_name
, utsname()->nodename
,
759 sizeof(nn
->nfsd_name
));
761 error
= nfsd_create_serv(net
);
765 nfsd_up_before
= nn
->nfsd_net_up
;
767 error
= nfsd_startup_net(nrservs
, net
, cred
);
770 error
= nn
->nfsd_serv
->sv_ops
->svo_setup(nn
->nfsd_serv
,
774 /* We are holding a reference to nn->nfsd_serv which
775 * we don't want to count in the return value,
778 error
= nn
->nfsd_serv
->sv_nrthreads
- 1;
780 if (error
< 0 && !nfsd_up_before
)
781 nfsd_shutdown_net(net
);
783 nfsd_destroy(net
); /* Release server */
785 mutex_unlock(&nfsd_mutex
);
789 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
791 nfsd_support_acl_version(int vers
)
793 if (vers
>= NFSD_ACL_MINVERS
&& vers
< NFSD_ACL_NRVERS
)
794 return nfsd_acl_version
[vers
] != NULL
;
799 nfsd_acl_rpcbind_set(struct net
*net
, const struct svc_program
*progp
,
800 u32 version
, int family
, unsigned short proto
,
803 if (!nfsd_support_acl_version(version
) ||
804 !nfsd_vers(net_generic(net
, nfsd_net_id
), version
, NFSD_TEST
))
806 return svc_generic_rpcbind_set(net
, progp
, version
, family
,
811 nfsd_acl_init_request(struct svc_rqst
*rqstp
,
812 const struct svc_program
*progp
,
813 struct svc_process_info
*ret
)
815 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
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
);
822 ret
->mismatch
.lovers
= NFSD_ACL_NRVERS
;
823 for (i
= NFSD_ACL_MINVERS
; i
< NFSD_ACL_NRVERS
; i
++) {
824 if (nfsd_support_acl_version(rqstp
->rq_vers
) &&
825 nfsd_vers(nn
, i
, NFSD_TEST
)) {
826 ret
->mismatch
.lovers
= i
;
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(rqstp
->rq_vers
) &&
835 nfsd_vers(nn
, i
, NFSD_TEST
)) {
836 ret
->mismatch
.hivers
= i
;
840 return rpc_prog_mismatch
;
845 nfsd_rpcbind_set(struct net
*net
, const struct svc_program
*progp
,
846 u32 version
, int family
, unsigned short proto
,
849 if (!nfsd_vers(net_generic(net
, nfsd_net_id
), version
, NFSD_TEST
))
851 return svc_generic_rpcbind_set(net
, progp
, version
, family
,
856 nfsd_init_request(struct svc_rqst
*rqstp
,
857 const struct svc_program
*progp
,
858 struct svc_process_info
*ret
)
860 struct nfsd_net
*nn
= net_generic(SVC_NET(rqstp
), nfsd_net_id
);
863 if (likely(nfsd_vers(nn
, rqstp
->rq_vers
, NFSD_TEST
)))
864 return svc_generic_init_request(rqstp
, progp
, ret
);
866 ret
->mismatch
.lovers
= NFSD_NRVERS
;
867 for (i
= NFSD_MINVERS
; i
< NFSD_NRVERS
; i
++) {
868 if (nfsd_vers(nn
, i
, NFSD_TEST
)) {
869 ret
->mismatch
.lovers
= i
;
873 if (ret
->mismatch
.lovers
== NFSD_NRVERS
)
874 return rpc_prog_unavail
;
875 ret
->mismatch
.hivers
= NFSD_MINVERS
;
876 for (i
= NFSD_NRVERS
- 1; i
>= NFSD_MINVERS
; i
--) {
877 if (nfsd_vers(nn
, i
, NFSD_TEST
)) {
878 ret
->mismatch
.hivers
= i
;
882 return rpc_prog_mismatch
;
886 * This is the NFS server kernel thread
891 struct svc_rqst
*rqstp
= (struct svc_rqst
*) vrqstp
;
892 struct svc_xprt
*perm_sock
= list_entry(rqstp
->rq_server
->sv_permsocks
.next
, typeof(struct svc_xprt
), xpt_list
);
893 struct net
*net
= perm_sock
->xpt_net
;
894 struct nfsd_net
*nn
= net_generic(net
, nfsd_net_id
);
897 /* Lock module and set up kernel thread */
898 mutex_lock(&nfsd_mutex
);
900 /* At this point, the thread shares current->fs
901 * with the init process. We need to create files with the
902 * umask as defined by the client instead of init's umask. */
903 if (unshare_fs_struct() < 0) {
904 printk("Unable to start nfsd thread: out of memory\n");
908 current
->fs
->umask
= 0;
911 * thread is spawned with all signals set to SIG_IGN, re-enable
912 * the ones that will bring down the thread
914 allow_signal(SIGKILL
);
915 allow_signal(SIGHUP
);
916 allow_signal(SIGINT
);
917 allow_signal(SIGQUIT
);
920 mutex_unlock(&nfsd_mutex
);
925 * The main request loop
928 /* Update sv_maxconn if it has changed */
929 rqstp
->rq_server
->sv_maxconn
= nn
->max_connections
;
932 * Find a socket with data available and call its
935 while ((err
= svc_recv(rqstp
, 60*60*HZ
)) == -EAGAIN
)
939 validate_process_creds();
941 validate_process_creds();
944 /* Clear signals before calling svc_exit_thread() */
945 flush_signals(current
);
947 mutex_lock(&nfsd_mutex
);
951 rqstp
->rq_server
= NULL
;
953 /* Release the thread */
954 svc_exit_thread(rqstp
);
959 mutex_unlock(&nfsd_mutex
);
960 module_put_and_exit(0);
965 * A write procedure can have a large argument, and a read procedure can
966 * have a large reply, but no NFSv2 or NFSv3 procedure has argument and
967 * reply that can both be larger than a page. The xdr code has taken
968 * advantage of this assumption to be a sloppy about bounds checking in
969 * some cases. Pending a rewrite of the NFSv2/v3 xdr code to fix that
970 * problem, we enforce these assumptions here:
972 static bool nfs_request_too_big(struct svc_rqst
*rqstp
,
973 const struct svc_procedure
*proc
)
976 * The ACL code has more careful bounds-checking and is not
977 * susceptible to this problem:
979 if (rqstp
->rq_prog
!= NFS_PROGRAM
)
982 * Ditto NFSv4 (which can in theory have argument and reply both
985 if (rqstp
->rq_vers
>= 4)
987 /* The reply will be small, we're OK: */
988 if (proc
->pc_xdrressize
> 0 &&
989 proc
->pc_xdrressize
< XDR_QUADLEN(PAGE_SIZE
))
992 return rqstp
->rq_arg
.len
> PAGE_SIZE
;
996 * nfsd_dispatch - Process an NFS or NFSACL Request
997 * @rqstp: incoming request
998 * @statp: pointer to location of accept_stat field in RPC Reply buffer
1000 * This RPC dispatcher integrates the NFS server's duplicate reply cache.
1003 * %0: Processing complete; do not send a Reply
1004 * %1: Processing complete; send Reply in rqstp->rq_res
1006 int nfsd_dispatch(struct svc_rqst
*rqstp
, __be32
*statp
)
1008 const struct svc_procedure
*proc
= rqstp
->rq_procinfo
;
1009 struct kvec
*argv
= &rqstp
->rq_arg
.head
[0];
1010 struct kvec
*resv
= &rqstp
->rq_res
.head
[0];
1013 if (nfs_request_too_big(rqstp
, proc
))
1014 goto out_decode_err
;
1017 * Give the xdr decoder a chance to change this if it wants
1018 * (necessary in the NFSv4.0 compound case)
1020 rqstp
->rq_cachetype
= proc
->pc_cachetype
;
1022 svcxdr_init_decode(rqstp
);
1023 if (!proc
->pc_decode(rqstp
, argv
->iov_base
))
1024 goto out_decode_err
;
1026 switch (nfsd_cache_lookup(rqstp
)) {
1030 goto out_cached_reply
;
1036 * Need to grab the location to store the status, as
1037 * NFSv4 does some encoding while processing
1039 p
= resv
->iov_base
+ resv
->iov_len
;
1040 resv
->iov_len
+= sizeof(__be32
);
1042 *statp
= proc
->pc_func(rqstp
);
1043 if (*statp
== rpc_drop_reply
|| test_bit(RQ_DROPME
, &rqstp
->rq_flags
))
1044 goto out_update_drop
;
1046 if (!proc
->pc_encode(rqstp
, p
))
1047 goto out_encode_err
;
1049 nfsd_cache_update(rqstp
, rqstp
->rq_cachetype
, statp
+ 1);
1054 trace_nfsd_garbage_args_err(rqstp
);
1055 *statp
= rpc_garbage_args
;
1059 nfsd_cache_update(rqstp
, RC_NOCACHE
, NULL
);
1064 trace_nfsd_cant_encode_err(rqstp
);
1065 nfsd_cache_update(rqstp
, RC_NOCACHE
, NULL
);
1066 *statp
= rpc_system_err
;
1071 * nfssvc_decode_voidarg - Decode void arguments
1072 * @rqstp: Server RPC transaction context
1073 * @p: buffer containing arguments to decode
1076 * %0: Arguments were not valid
1077 * %1: Decoding was successful
1079 int nfssvc_decode_voidarg(struct svc_rqst
*rqstp
, __be32
*p
)
1085 * nfssvc_encode_voidres - Encode void results
1086 * @rqstp: Server RPC transaction context
1087 * @p: buffer in which to encode results
1090 * %0: Local error while encoding
1091 * %1: Encoding was successful
1093 int nfssvc_encode_voidres(struct svc_rqst
*rqstp
, __be32
*p
)
1095 return xdr_ressize_check(rqstp
, p
);
1098 int nfsd_pool_stats_open(struct inode
*inode
, struct file
*file
)
1101 struct nfsd_net
*nn
= net_generic(inode
->i_sb
->s_fs_info
, nfsd_net_id
);
1103 mutex_lock(&nfsd_mutex
);
1104 if (nn
->nfsd_serv
== NULL
) {
1105 mutex_unlock(&nfsd_mutex
);
1108 /* bump up the psudo refcount while traversing */
1109 svc_get(nn
->nfsd_serv
);
1110 ret
= svc_pool_stats_open(nn
->nfsd_serv
, file
);
1111 mutex_unlock(&nfsd_mutex
);
1115 int nfsd_pool_stats_release(struct inode
*inode
, struct file
*file
)
1117 int ret
= seq_release(inode
, file
);
1118 struct net
*net
= inode
->i_sb
->s_fs_info
;
1120 mutex_lock(&nfsd_mutex
);
1121 /* this function really, really should have been called svc_put() */
1123 mutex_unlock(&nfsd_mutex
);