blob: 969a227186fa8b3821995f611bc93a5e14b4e29f [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// 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
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>
24#include <net/ipv6.h>
25#include <net/net_namespace.h>
26#include "nfsd.h"
27#include "cache.h"
28#include "vfs.h"
29#include "netns.h"
30#include "filecache.h"
31
32#define NFSDDBG_FACILITY NFSDDBG_SVC
33
34extern struct svc_program nfsd_program;
35static int nfsd(void *vrqstp);
36#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
37static int nfsd_acl_rpcbind_set(struct net *,
38 const struct svc_program *,
39 u32, int,
40 unsigned short,
41 unsigned short);
42static __be32 nfsd_acl_init_request(struct svc_rqst *,
43 const struct svc_program *,
44 struct svc_process_info *);
45#endif
46static int nfsd_rpcbind_set(struct net *,
47 const struct svc_program *,
48 u32, int,
49 unsigned short,
50 unsigned short);
51static __be32 nfsd_init_request(struct svc_rqst *,
52 const struct svc_program *,
53 struct svc_process_info *);
54
55/*
56 * nfsd_mutex protects nn->nfsd_serv -- both the pointer itself and the members
57 * of the svc_serv struct. In particular, ->sv_nrthreads but also to some
58 * extent ->sv_temp_socks and ->sv_permsocks. It also protects nfsdstats.th_cnt
59 *
60 * If (out side the lock) nn->nfsd_serv is non-NULL, then it must point to a
61 * properly initialised 'struct svc_serv' with ->sv_nrthreads > 0. That number
62 * of nfsd threads must exist and each must listed in ->sp_all_threads in each
63 * entry of ->sv_pools[].
64 *
65 * Transitions of the thread count between zero and non-zero are of particular
66 * interest since the svc_serv needs to be created and initialized at that
67 * point, or freed.
68 *
69 * Finally, the nfsd_mutex also protects some of the global variables that are
70 * accessed when nfsd starts and that are settable via the write_* routines in
71 * nfsctl.c. In particular:
72 *
73 * user_recovery_dirname
74 * user_lease_time
75 * nfsd_versions
76 */
77DEFINE_MUTEX(nfsd_mutex);
78
79/*
80 * nfsd_drc_lock protects nfsd_drc_max_pages and nfsd_drc_pages_used.
81 * nfsd_drc_max_pages limits the total amount of memory available for
82 * version 4.1 DRC caches.
83 * nfsd_drc_pages_used tracks the current version 4.1 DRC memory usage.
84 */
85spinlock_t nfsd_drc_lock;
86unsigned long nfsd_drc_max_mem;
87unsigned long nfsd_drc_mem_used;
88
89#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
90static struct svc_stat nfsd_acl_svcstats;
91static const struct svc_version *nfsd_acl_version[] = {
92 [2] = &nfsd_acl_version2,
93 [3] = &nfsd_acl_version3,
94};
95
96#define NFSD_ACL_MINVERS 2
97#define NFSD_ACL_NRVERS ARRAY_SIZE(nfsd_acl_version)
98
99static struct svc_program nfsd_acl_program = {
100 .pg_prog = NFS_ACL_PROGRAM,
101 .pg_nvers = NFSD_ACL_NRVERS,
102 .pg_vers = nfsd_acl_version,
103 .pg_name = "nfsacl",
104 .pg_class = "nfsd",
105 .pg_stats = &nfsd_acl_svcstats,
106 .pg_authenticate = &svc_set_client,
107 .pg_init_request = nfsd_acl_init_request,
108 .pg_rpcbind_set = nfsd_acl_rpcbind_set,
109};
110
111static struct svc_stat nfsd_acl_svcstats = {
112 .program = &nfsd_acl_program,
113};
114#endif /* defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) */
115
116static const struct svc_version *nfsd_version[] = {
117 [2] = &nfsd_version2,
118#if defined(CONFIG_NFSD_V3)
119 [3] = &nfsd_version3,
120#endif
121#if defined(CONFIG_NFSD_V4)
122 [4] = &nfsd_version4,
123#endif
124};
125
126#define NFSD_MINVERS 2
127#define NFSD_NRVERS ARRAY_SIZE(nfsd_version)
128
129struct svc_program nfsd_program = {
130#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
131 .pg_next = &nfsd_acl_program,
132#endif
133 .pg_prog = NFS_PROGRAM, /* program number */
134 .pg_nvers = NFSD_NRVERS, /* nr of entries in nfsd_version */
135 .pg_vers = nfsd_version, /* version table */
136 .pg_name = "nfsd", /* program name */
137 .pg_class = "nfsd", /* authentication class */
138 .pg_stats = &nfsd_svcstats, /* version table */
139 .pg_authenticate = &svc_set_client, /* export authentication */
140 .pg_init_request = nfsd_init_request,
141 .pg_rpcbind_set = nfsd_rpcbind_set,
142};
143
144static bool
145nfsd_support_version(int vers)
146{
147 if (vers >= NFSD_MINVERS && vers < NFSD_NRVERS)
148 return nfsd_version[vers] != NULL;
149 return false;
150}
151
152static bool *
153nfsd_alloc_versions(void)
154{
155 bool *vers = kmalloc_array(NFSD_NRVERS, sizeof(bool), GFP_KERNEL);
156 unsigned i;
157
158 if (vers) {
159 /* All compiled versions are enabled by default */
160 for (i = 0; i < NFSD_NRVERS; i++)
161 vers[i] = nfsd_support_version(i);
162 }
163 return vers;
164}
165
166static bool *
167nfsd_alloc_minorversions(void)
168{
169 bool *vers = kmalloc_array(NFSD_SUPPORTED_MINOR_VERSION + 1,
170 sizeof(bool), GFP_KERNEL);
171 unsigned i;
172
173 if (vers) {
174 /* All minor versions are enabled by default */
175 for (i = 0; i <= NFSD_SUPPORTED_MINOR_VERSION; i++)
176 vers[i] = nfsd_support_version(4);
177 }
178 return vers;
179}
180
181void
182nfsd_netns_free_versions(struct nfsd_net *nn)
183{
184 kfree(nn->nfsd_versions);
185 kfree(nn->nfsd4_minorversions);
186 nn->nfsd_versions = NULL;
187 nn->nfsd4_minorversions = NULL;
188}
189
190static void
191nfsd_netns_init_versions(struct nfsd_net *nn)
192{
193 if (!nn->nfsd_versions) {
194 nn->nfsd_versions = nfsd_alloc_versions();
195 nn->nfsd4_minorversions = nfsd_alloc_minorversions();
196 if (!nn->nfsd_versions || !nn->nfsd4_minorversions)
197 nfsd_netns_free_versions(nn);
198 }
199}
200
201int nfsd_vers(struct nfsd_net *nn, int vers, enum vers_op change)
202{
203 if (vers < NFSD_MINVERS || vers >= NFSD_NRVERS)
204 return 0;
205 switch(change) {
206 case NFSD_SET:
207 if (nn->nfsd_versions)
208 nn->nfsd_versions[vers] = nfsd_support_version(vers);
209 break;
210 case NFSD_CLEAR:
211 nfsd_netns_init_versions(nn);
212 if (nn->nfsd_versions)
213 nn->nfsd_versions[vers] = false;
214 break;
215 case NFSD_TEST:
216 if (nn->nfsd_versions)
217 return nn->nfsd_versions[vers];
218 /* Fallthrough */
219 case NFSD_AVAIL:
220 return nfsd_support_version(vers);
221 }
222 return 0;
223}
224
225static void
226nfsd_adjust_nfsd_versions4(struct nfsd_net *nn)
227{
228 unsigned i;
229
230 for (i = 0; i <= NFSD_SUPPORTED_MINOR_VERSION; i++) {
231 if (nn->nfsd4_minorversions[i])
232 return;
233 }
234 nfsd_vers(nn, 4, NFSD_CLEAR);
235}
236
237int nfsd_minorversion(struct nfsd_net *nn, u32 minorversion, enum vers_op change)
238{
239 if (minorversion > NFSD_SUPPORTED_MINOR_VERSION &&
240 change != NFSD_AVAIL)
241 return -1;
242
243 switch(change) {
244 case NFSD_SET:
245 if (nn->nfsd4_minorversions) {
246 nfsd_vers(nn, 4, NFSD_SET);
247 nn->nfsd4_minorversions[minorversion] =
248 nfsd_vers(nn, 4, NFSD_TEST);
249 }
250 break;
251 case NFSD_CLEAR:
252 nfsd_netns_init_versions(nn);
253 if (nn->nfsd4_minorversions) {
254 nn->nfsd4_minorversions[minorversion] = false;
255 nfsd_adjust_nfsd_versions4(nn);
256 }
257 break;
258 case NFSD_TEST:
259 if (nn->nfsd4_minorversions)
260 return nn->nfsd4_minorversions[minorversion];
261 return nfsd_vers(nn, 4, NFSD_TEST);
262 case NFSD_AVAIL:
263 return minorversion <= NFSD_SUPPORTED_MINOR_VERSION &&
264 nfsd_vers(nn, 4, NFSD_AVAIL);
265 }
266 return 0;
267}
268
269/*
270 * Maximum number of nfsd processes
271 */
272#define NFSD_MAXSERVS 8192
273
274int nfsd_nrthreads(struct net *net)
275{
276 int rv = 0;
277 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
278
279 mutex_lock(&nfsd_mutex);
280 if (nn->nfsd_serv)
281 rv = nn->nfsd_serv->sv_nrthreads;
282 mutex_unlock(&nfsd_mutex);
283 return rv;
284}
285
286static int nfsd_init_socks(struct net *net, const struct cred *cred)
287{
288 int error;
289 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
290
291 if (!list_empty(&nn->nfsd_serv->sv_permsocks))
292 return 0;
293
294 error = svc_create_xprt(nn->nfsd_serv, "udp", net, PF_INET, NFS_PORT,
295 SVC_SOCK_DEFAULTS, cred);
296 if (error < 0)
297 return error;
298
299 error = svc_create_xprt(nn->nfsd_serv, "tcp", net, PF_INET, NFS_PORT,
300 SVC_SOCK_DEFAULTS, cred);
301 if (error < 0)
302 return error;
303
304 return 0;
305}
306
307static int nfsd_users = 0;
308
309static int nfsd_startup_generic(int nrservs)
310{
311 int ret;
312
313 if (nfsd_users++)
314 return 0;
315
316 ret = nfsd_file_cache_init();
317 if (ret)
318 goto dec_users;
319
320 ret = nfs4_state_start();
321 if (ret)
322 goto out_file_cache;
323 return 0;
324
325out_file_cache:
326 nfsd_file_cache_shutdown();
327dec_users:
328 nfsd_users--;
329 return ret;
330}
331
332static void nfsd_shutdown_generic(void)
333{
334 if (--nfsd_users)
335 return;
336
337 nfs4_state_shutdown();
338 nfsd_file_cache_shutdown();
339}
340
341static bool nfsd_needs_lockd(struct nfsd_net *nn)
342{
343 return nfsd_vers(nn, 2, NFSD_TEST) || nfsd_vers(nn, 3, NFSD_TEST);
344}
345
346void nfsd_copy_boot_verifier(__be32 verf[2], struct nfsd_net *nn)
347{
348 int seq = 0;
349
350 do {
351 read_seqbegin_or_lock(&nn->boot_lock, &seq);
352 /*
353 * This is opaque to client, so no need to byte-swap. Use
354 * __force to keep sparse happy. y2038 time_t overflow is
355 * irrelevant in this usage
356 */
357 verf[0] = (__force __be32)nn->nfssvc_boot.tv_sec;
358 verf[1] = (__force __be32)nn->nfssvc_boot.tv_nsec;
359 } while (need_seqretry(&nn->boot_lock, seq));
360 done_seqretry(&nn->boot_lock, seq);
361}
362
363static void nfsd_reset_boot_verifier_locked(struct nfsd_net *nn)
364{
365 ktime_get_real_ts64(&nn->nfssvc_boot);
366}
367
368void nfsd_reset_boot_verifier(struct nfsd_net *nn)
369{
370 write_seqlock(&nn->boot_lock);
371 nfsd_reset_boot_verifier_locked(nn);
372 write_sequnlock(&nn->boot_lock);
373}
374
375static int nfsd_startup_net(int nrservs, struct net *net, const struct cred *cred)
376{
377 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
378 int ret;
379
380 if (nn->nfsd_net_up)
381 return 0;
382
383 ret = nfsd_startup_generic(nrservs);
384 if (ret)
385 return ret;
386 ret = nfsd_init_socks(net, cred);
387 if (ret)
388 goto out_socks;
389
390 if (nfsd_needs_lockd(nn) && !nn->lockd_up) {
391 ret = lockd_up(net, cred);
392 if (ret)
393 goto out_socks;
394 nn->lockd_up = 1;
395 }
396
397 ret = nfsd_file_cache_start_net(net);
398 if (ret)
399 goto out_lockd;
400 ret = nfs4_state_start_net(net);
401 if (ret)
402 goto out_filecache;
403
404 nn->nfsd_net_up = true;
405 return 0;
406
407out_filecache:
408 nfsd_file_cache_shutdown_net(net);
409out_lockd:
410 if (nn->lockd_up) {
411 lockd_down(net);
412 nn->lockd_up = 0;
413 }
414out_socks:
415 nfsd_shutdown_generic();
416 return ret;
417}
418
419static void nfsd_shutdown_net(struct net *net)
420{
421 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
422
423 nfs4_state_shutdown_net(net);
424 nfsd_file_cache_shutdown_net(net);
425 if (nn->lockd_up) {
426 lockd_down(net);
427 nn->lockd_up = 0;
428 }
429 nn->nfsd_net_up = false;
430 nfsd_shutdown_generic();
431}
432
433static int nfsd_inetaddr_event(struct notifier_block *this, unsigned long event,
434 void *ptr)
435{
436 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
437 struct net_device *dev = ifa->ifa_dev->dev;
438 struct net *net = dev_net(dev);
439 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
440 struct sockaddr_in sin;
441
442 if ((event != NETDEV_DOWN) ||
443 !atomic_inc_not_zero(&nn->ntf_refcnt))
444 goto out;
445
446 if (nn->nfsd_serv) {
447 dprintk("nfsd_inetaddr_event: removed %pI4\n", &ifa->ifa_local);
448 sin.sin_family = AF_INET;
449 sin.sin_addr.s_addr = ifa->ifa_local;
450 svc_age_temp_xprts_now(nn->nfsd_serv, (struct sockaddr *)&sin);
451 }
452 atomic_dec(&nn->ntf_refcnt);
453 wake_up(&nn->ntf_wq);
454
455out:
456 return NOTIFY_DONE;
457}
458
459static struct notifier_block nfsd_inetaddr_notifier = {
460 .notifier_call = nfsd_inetaddr_event,
461};
462
463#if IS_ENABLED(CONFIG_IPV6)
464static int nfsd_inet6addr_event(struct notifier_block *this,
465 unsigned long event, void *ptr)
466{
467 struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
468 struct net_device *dev = ifa->idev->dev;
469 struct net *net = dev_net(dev);
470 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
471 struct sockaddr_in6 sin6;
472
473 if ((event != NETDEV_DOWN) ||
474 !atomic_inc_not_zero(&nn->ntf_refcnt))
475 goto out;
476
477 if (nn->nfsd_serv) {
478 dprintk("nfsd_inet6addr_event: removed %pI6\n", &ifa->addr);
479 sin6.sin6_family = AF_INET6;
480 sin6.sin6_addr = ifa->addr;
481 if (ipv6_addr_type(&sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
482 sin6.sin6_scope_id = ifa->idev->dev->ifindex;
483 svc_age_temp_xprts_now(nn->nfsd_serv, (struct sockaddr *)&sin6);
484 }
485 atomic_dec(&nn->ntf_refcnt);
486 wake_up(&nn->ntf_wq);
487out:
488 return NOTIFY_DONE;
489}
490
491static struct notifier_block nfsd_inet6addr_notifier = {
492 .notifier_call = nfsd_inet6addr_event,
493};
494#endif
495
496/* Only used under nfsd_mutex, so this atomic may be overkill: */
497static atomic_t nfsd_notifier_refcount = ATOMIC_INIT(0);
498
499static void nfsd_last_thread(struct svc_serv *serv, struct net *net)
500{
501 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
502
503 atomic_dec(&nn->ntf_refcnt);
504 /* check if the notifier still has clients */
505 if (atomic_dec_return(&nfsd_notifier_refcount) == 0) {
506 unregister_inetaddr_notifier(&nfsd_inetaddr_notifier);
507#if IS_ENABLED(CONFIG_IPV6)
508 unregister_inet6addr_notifier(&nfsd_inet6addr_notifier);
509#endif
510 }
511 wait_event(nn->ntf_wq, atomic_read(&nn->ntf_refcnt) == 0);
512
513 /*
514 * write_ports can create the server without actually starting
515 * any threads--if we get shut down before any threads are
516 * started, then nfsd_last_thread will be run before any of this
517 * other initialization has been done except the rpcb information.
518 */
519 svc_rpcb_cleanup(serv, net);
520 if (!nn->nfsd_net_up)
521 return;
522
523 nfsd_shutdown_net(net);
524 pr_info("nfsd: last server has exited, flushing export cache\n");
525 nfsd_export_flush(net);
526}
527
528void nfsd_reset_versions(struct nfsd_net *nn)
529{
530 int i;
531
532 for (i = 0; i < NFSD_NRVERS; i++)
533 if (nfsd_vers(nn, i, NFSD_TEST))
534 return;
535
536 for (i = 0; i < NFSD_NRVERS; i++)
537 if (i != 4)
538 nfsd_vers(nn, i, NFSD_SET);
539 else {
540 int minor = 0;
541 while (nfsd_minorversion(nn, minor, NFSD_SET) >= 0)
542 minor++;
543 }
544}
545
546/*
547 * Each session guarantees a negotiated per slot memory cache for replies
548 * which in turn consumes memory beyond the v2/v3/v4.0 server. A dedicated
549 * NFSv4.1 server might want to use more memory for a DRC than a machine
550 * with mutiple services.
551 *
552 * Impose a hard limit on the number of pages for the DRC which varies
553 * according to the machines free pages. This is of course only a default.
554 *
555 * For now this is a #defined shift which could be under admin control
556 * in the future.
557 */
558static void set_max_drc(void)
559{
560 #define NFSD_DRC_SIZE_SHIFT 7
561 nfsd_drc_max_mem = (nr_free_buffer_pages()
562 >> NFSD_DRC_SIZE_SHIFT) * PAGE_SIZE;
563 nfsd_drc_mem_used = 0;
564 spin_lock_init(&nfsd_drc_lock);
565 dprintk("%s nfsd_drc_max_mem %lu \n", __func__, nfsd_drc_max_mem);
566}
567
568static int nfsd_get_default_max_blksize(void)
569{
570 struct sysinfo i;
571 unsigned long long target;
572 unsigned long ret;
573
574 si_meminfo(&i);
575 target = (i.totalram - i.totalhigh) << PAGE_SHIFT;
576 /*
577 * Aim for 1/4096 of memory per thread This gives 1MB on 4Gig
578 * machines, but only uses 32K on 128M machines. Bottom out at
579 * 8K on 32M and smaller. Of course, this is only a default.
580 */
581 target >>= 12;
582
583 ret = NFSSVC_MAXBLKSIZE;
584 while (ret > target && ret >= 8*1024*2)
585 ret /= 2;
586 return ret;
587}
588
589static const struct svc_serv_ops nfsd_thread_sv_ops = {
590 .svo_shutdown = nfsd_last_thread,
591 .svo_function = nfsd,
592 .svo_enqueue_xprt = svc_xprt_do_enqueue,
593 .svo_setup = svc_set_num_threads,
594 .svo_module = THIS_MODULE,
595};
596
597static void nfsd_complete_shutdown(struct net *net)
598{
599 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
600
601 WARN_ON(!mutex_is_locked(&nfsd_mutex));
602
603 nn->nfsd_serv = NULL;
604 complete(&nn->nfsd_shutdown_complete);
605}
606
607void nfsd_shutdown_threads(struct net *net)
608{
609 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
610 struct svc_serv *serv;
611
612 mutex_lock(&nfsd_mutex);
613 serv = nn->nfsd_serv;
614 if (serv == NULL) {
615 mutex_unlock(&nfsd_mutex);
616 return;
617 }
618
619 svc_get(serv);
620 /* Kill outstanding nfsd threads */
621 serv->sv_ops->svo_setup(serv, NULL, 0);
622 nfsd_destroy(net);
623 mutex_unlock(&nfsd_mutex);
624 /* Wait for shutdown of nfsd_serv to complete */
625 wait_for_completion(&nn->nfsd_shutdown_complete);
626}
627
628int nfsd_create_serv(struct net *net)
629{
630 int error;
631 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
632
633 WARN_ON(!mutex_is_locked(&nfsd_mutex));
634 if (nn->nfsd_serv) {
635 svc_get(nn->nfsd_serv);
636 return 0;
637 }
638 if (nfsd_max_blksize == 0)
639 nfsd_max_blksize = nfsd_get_default_max_blksize();
640 nfsd_reset_versions(nn);
641 nn->nfsd_serv = svc_create_pooled(&nfsd_program, nfsd_max_blksize,
642 &nfsd_thread_sv_ops);
643 if (nn->nfsd_serv == NULL)
644 return -ENOMEM;
645 init_completion(&nn->nfsd_shutdown_complete);
646
647 nn->nfsd_serv->sv_maxconn = nn->max_connections;
648 error = svc_bind(nn->nfsd_serv, net);
649 if (error < 0) {
650 svc_destroy(nn->nfsd_serv);
651 nfsd_complete_shutdown(net);
652 return error;
653 }
654
655 set_max_drc();
656 /* check if the notifier is already set */
657 if (atomic_inc_return(&nfsd_notifier_refcount) == 1) {
658 register_inetaddr_notifier(&nfsd_inetaddr_notifier);
659#if IS_ENABLED(CONFIG_IPV6)
660 register_inet6addr_notifier(&nfsd_inet6addr_notifier);
661#endif
662 }
663 atomic_inc(&nn->ntf_refcnt);
664 nfsd_reset_boot_verifier(nn);
665 return 0;
666}
667
668int nfsd_nrpools(struct net *net)
669{
670 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
671
672 if (nn->nfsd_serv == NULL)
673 return 0;
674 else
675 return nn->nfsd_serv->sv_nrpools;
676}
677
678int nfsd_get_nrthreads(int n, int *nthreads, struct net *net)
679{
680 int i = 0;
681 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
682
683 if (nn->nfsd_serv != NULL) {
684 for (i = 0; i < nn->nfsd_serv->sv_nrpools && i < n; i++)
685 nthreads[i] = nn->nfsd_serv->sv_pools[i].sp_nrthreads;
686 }
687
688 return 0;
689}
690
691void nfsd_destroy(struct net *net)
692{
693 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
694 int destroy = (nn->nfsd_serv->sv_nrthreads == 1);
695
696 if (destroy)
697 svc_shutdown_net(nn->nfsd_serv, net);
698 svc_destroy(nn->nfsd_serv);
699 if (destroy)
700 nfsd_complete_shutdown(net);
701}
702
703int nfsd_set_nrthreads(int n, int *nthreads, struct net *net)
704{
705 int i = 0;
706 int tot = 0;
707 int err = 0;
708 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
709
710 WARN_ON(!mutex_is_locked(&nfsd_mutex));
711
712 if (nn->nfsd_serv == NULL || n <= 0)
713 return 0;
714
715 if (n > nn->nfsd_serv->sv_nrpools)
716 n = nn->nfsd_serv->sv_nrpools;
717
718 /* enforce a global maximum number of threads */
719 tot = 0;
720 for (i = 0; i < n; i++) {
721 nthreads[i] = min(nthreads[i], NFSD_MAXSERVS);
722 tot += nthreads[i];
723 }
724 if (tot > NFSD_MAXSERVS) {
725 /* total too large: scale down requested numbers */
726 for (i = 0; i < n && tot > 0; i++) {
727 int new = nthreads[i] * NFSD_MAXSERVS / tot;
728 tot -= (nthreads[i] - new);
729 nthreads[i] = new;
730 }
731 for (i = 0; i < n && tot > 0; i++) {
732 nthreads[i]--;
733 tot--;
734 }
735 }
736
737 /*
738 * There must always be a thread in pool 0; the admin
739 * can't shut down NFS completely using pool_threads.
740 */
741 if (nthreads[0] == 0)
742 nthreads[0] = 1;
743
744 /* apply the new numbers */
745 svc_get(nn->nfsd_serv);
746 for (i = 0; i < n; i++) {
747 err = nn->nfsd_serv->sv_ops->svo_setup(nn->nfsd_serv,
748 &nn->nfsd_serv->sv_pools[i], nthreads[i]);
749 if (err)
750 break;
751 }
752 nfsd_destroy(net);
753 return err;
754}
755
756/*
757 * Adjust the number of threads and return the new number of threads.
758 * This is also the function that starts the server if necessary, if
759 * this is the first time nrservs is nonzero.
760 */
761int
762nfsd_svc(int nrservs, struct net *net, const struct cred *cred)
763{
764 int error;
765 bool nfsd_up_before;
766 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
767
768 mutex_lock(&nfsd_mutex);
769 dprintk("nfsd: creating service\n");
770
771 nrservs = max(nrservs, 0);
772 nrservs = min(nrservs, NFSD_MAXSERVS);
773 error = 0;
774
775 if (nrservs == 0 && nn->nfsd_serv == NULL)
776 goto out;
777
778 error = nfsd_create_serv(net);
779 if (error)
780 goto out;
781
782 nfsd_up_before = nn->nfsd_net_up;
783
784 error = nfsd_startup_net(nrservs, net, cred);
785 if (error)
786 goto out_destroy;
787 error = nn->nfsd_serv->sv_ops->svo_setup(nn->nfsd_serv,
788 NULL, nrservs);
789 if (error)
790 goto out_shutdown;
791 /* We are holding a reference to nn->nfsd_serv which
792 * we don't want to count in the return value,
793 * so subtract 1
794 */
795 error = nn->nfsd_serv->sv_nrthreads - 1;
796out_shutdown:
797 if (error < 0 && !nfsd_up_before)
798 nfsd_shutdown_net(net);
799out_destroy:
800 nfsd_destroy(net); /* Release server */
801out:
802 mutex_unlock(&nfsd_mutex);
803 return error;
804}
805
806#if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
807static bool
808nfsd_support_acl_version(int vers)
809{
810 if (vers >= NFSD_ACL_MINVERS && vers < NFSD_ACL_NRVERS)
811 return nfsd_acl_version[vers] != NULL;
812 return false;
813}
814
815static int
816nfsd_acl_rpcbind_set(struct net *net, const struct svc_program *progp,
817 u32 version, int family, unsigned short proto,
818 unsigned short port)
819{
820 if (!nfsd_support_acl_version(version) ||
821 !nfsd_vers(net_generic(net, nfsd_net_id), version, NFSD_TEST))
822 return 0;
823 return svc_generic_rpcbind_set(net, progp, version, family,
824 proto, port);
825}
826
827static __be32
828nfsd_acl_init_request(struct svc_rqst *rqstp,
829 const struct svc_program *progp,
830 struct svc_process_info *ret)
831{
832 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
833 int i;
834
835 if (likely(nfsd_support_acl_version(rqstp->rq_vers) &&
836 nfsd_vers(nn, rqstp->rq_vers, NFSD_TEST)))
837 return svc_generic_init_request(rqstp, progp, ret);
838
839 ret->mismatch.lovers = NFSD_ACL_NRVERS;
840 for (i = NFSD_ACL_MINVERS; i < NFSD_ACL_NRVERS; i++) {
841 if (nfsd_support_acl_version(rqstp->rq_vers) &&
842 nfsd_vers(nn, i, NFSD_TEST)) {
843 ret->mismatch.lovers = i;
844 break;
845 }
846 }
847 if (ret->mismatch.lovers == NFSD_ACL_NRVERS)
848 return rpc_prog_unavail;
849 ret->mismatch.hivers = NFSD_ACL_MINVERS;
850 for (i = NFSD_ACL_NRVERS - 1; i >= NFSD_ACL_MINVERS; i--) {
851 if (nfsd_support_acl_version(rqstp->rq_vers) &&
852 nfsd_vers(nn, i, NFSD_TEST)) {
853 ret->mismatch.hivers = i;
854 break;
855 }
856 }
857 return rpc_prog_mismatch;
858}
859#endif
860
861static int
862nfsd_rpcbind_set(struct net *net, const struct svc_program *progp,
863 u32 version, int family, unsigned short proto,
864 unsigned short port)
865{
866 if (!nfsd_vers(net_generic(net, nfsd_net_id), version, NFSD_TEST))
867 return 0;
868 return svc_generic_rpcbind_set(net, progp, version, family,
869 proto, port);
870}
871
872static __be32
873nfsd_init_request(struct svc_rqst *rqstp,
874 const struct svc_program *progp,
875 struct svc_process_info *ret)
876{
877 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
878 int i;
879
880 if (likely(nfsd_vers(nn, rqstp->rq_vers, NFSD_TEST)))
881 return svc_generic_init_request(rqstp, progp, ret);
882
883 ret->mismatch.lovers = NFSD_NRVERS;
884 for (i = NFSD_MINVERS; i < NFSD_NRVERS; i++) {
885 if (nfsd_vers(nn, i, NFSD_TEST)) {
886 ret->mismatch.lovers = i;
887 break;
888 }
889 }
890 if (ret->mismatch.lovers == NFSD_NRVERS)
891 return rpc_prog_unavail;
892 ret->mismatch.hivers = NFSD_MINVERS;
893 for (i = NFSD_NRVERS - 1; i >= NFSD_MINVERS; i--) {
894 if (nfsd_vers(nn, i, NFSD_TEST)) {
895 ret->mismatch.hivers = i;
896 break;
897 }
898 }
899 return rpc_prog_mismatch;
900}
901
902/*
903 * This is the NFS server kernel thread
904 */
905static int
906nfsd(void *vrqstp)
907{
908 struct svc_rqst *rqstp = (struct svc_rqst *) vrqstp;
909 struct svc_xprt *perm_sock = list_entry(rqstp->rq_server->sv_permsocks.next, typeof(struct svc_xprt), xpt_list);
910 struct net *net = perm_sock->xpt_net;
911 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
912 int err;
913
914 /* Lock module and set up kernel thread */
915 mutex_lock(&nfsd_mutex);
916
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");
922 goto out;
923 }
924
925 current->fs->umask = 0;
926
927 /*
928 * thread is spawned with all signals set to SIG_IGN, re-enable
929 * the ones that will bring down the thread
930 */
931 allow_signal(SIGKILL);
932 allow_signal(SIGHUP);
933 allow_signal(SIGINT);
934 allow_signal(SIGQUIT);
935
936 nfsdstats.th_cnt++;
937 mutex_unlock(&nfsd_mutex);
938
939 set_freezable();
940
941 /*
942 * The main request loop
943 */
944 for (;;) {
945 /* Update sv_maxconn if it has changed */
946 rqstp->rq_server->sv_maxconn = nn->max_connections;
947
948 /*
949 * Find a socket with data available and call its
950 * recvfrom routine.
951 */
952 while ((err = svc_recv(rqstp, 60*60*HZ)) == -EAGAIN)
953 ;
954 if (err == -EINTR)
955 break;
956 validate_process_creds();
957 svc_process(rqstp);
958 validate_process_creds();
959 }
960
961 /* Clear signals before calling svc_exit_thread() */
962 flush_signals(current);
963
964 mutex_lock(&nfsd_mutex);
965 nfsdstats.th_cnt --;
966
967out:
968 rqstp->rq_server = NULL;
969
970 /* Release the thread */
971 svc_exit_thread(rqstp);
972
973 nfsd_destroy(net);
974
975 /* Release module */
976 mutex_unlock(&nfsd_mutex);
977 module_put_and_exit(0);
978 return 0;
979}
980
981static __be32 map_new_errors(u32 vers, __be32 nfserr)
982{
983 if (nfserr == nfserr_jukebox && vers == 2)
984 return nfserr_dropit;
985 if (nfserr == nfserr_wrongsec && vers < 4)
986 return nfserr_acces;
987 return nfserr;
988}
989
990/*
991 * A write procedure can have a large argument, and a read procedure can
992 * have a large reply, but no NFSv2 or NFSv3 procedure has argument and
993 * reply that can both be larger than a page. The xdr code has taken
994 * advantage of this assumption to be a sloppy about bounds checking in
995 * some cases. Pending a rewrite of the NFSv2/v3 xdr code to fix that
996 * problem, we enforce these assumptions here:
997 */
998static bool nfs_request_too_big(struct svc_rqst *rqstp,
999 const struct svc_procedure *proc)
1000{
1001 /*
1002 * The ACL code has more careful bounds-checking and is not
1003 * susceptible to this problem:
1004 */
1005 if (rqstp->rq_prog != NFS_PROGRAM)
1006 return false;
1007 /*
1008 * Ditto NFSv4 (which can in theory have argument and reply both
1009 * more than a page):
1010 */
1011 if (rqstp->rq_vers >= 4)
1012 return false;
1013 /* The reply will be small, we're OK: */
1014 if (proc->pc_xdrressize > 0 &&
1015 proc->pc_xdrressize < XDR_QUADLEN(PAGE_SIZE))
1016 return false;
1017
1018 return rqstp->rq_arg.len > PAGE_SIZE;
1019}
1020
1021int
1022nfsd_dispatch(struct svc_rqst *rqstp, __be32 *statp)
1023{
1024 const struct svc_procedure *proc;
1025 __be32 nfserr;
1026 __be32 *nfserrp;
1027
1028 dprintk("nfsd_dispatch: vers %d proc %d\n",
1029 rqstp->rq_vers, rqstp->rq_proc);
1030 proc = rqstp->rq_procinfo;
1031
1032 if (nfs_request_too_big(rqstp, proc)) {
1033 dprintk("nfsd: NFSv%d argument too large\n", rqstp->rq_vers);
1034 *statp = rpc_garbage_args;
1035 return 1;
1036 }
1037 /*
1038 * Give the xdr decoder a chance to change this if it wants
1039 * (necessary in the NFSv4.0 compound case)
1040 */
1041 rqstp->rq_cachetype = proc->pc_cachetype;
1042 /* Decode arguments */
1043 if (proc->pc_decode &&
1044 !proc->pc_decode(rqstp, (__be32*)rqstp->rq_arg.head[0].iov_base)) {
1045 dprintk("nfsd: failed to decode arguments!\n");
1046 *statp = rpc_garbage_args;
1047 return 1;
1048 }
1049
1050 /* Check whether we have this call in the cache. */
1051 switch (nfsd_cache_lookup(rqstp)) {
1052 case RC_DROPIT:
1053 return 0;
1054 case RC_REPLY:
1055 return 1;
1056 case RC_DOIT:;
1057 /* do it */
1058 }
1059
1060 /* need to grab the location to store the status, as
1061 * nfsv4 does some encoding while processing
1062 */
1063 nfserrp = rqstp->rq_res.head[0].iov_base
1064 + rqstp->rq_res.head[0].iov_len;
1065 rqstp->rq_res.head[0].iov_len += sizeof(__be32);
1066
1067 /* Now call the procedure handler, and encode NFS status. */
1068 nfserr = proc->pc_func(rqstp);
1069 nfserr = map_new_errors(rqstp->rq_vers, nfserr);
1070 if (nfserr == nfserr_dropit || test_bit(RQ_DROPME, &rqstp->rq_flags)) {
1071 dprintk("nfsd: Dropping request; may be revisited later\n");
1072 nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
1073 return 0;
1074 }
1075
1076 if (rqstp->rq_proc != 0)
1077 *nfserrp++ = nfserr;
1078
1079 /* Encode result.
1080 * For NFSv2, additional info is never returned in case of an error.
1081 */
1082 if (!(nfserr && rqstp->rq_vers == 2)) {
1083 if (proc->pc_encode && !proc->pc_encode(rqstp, nfserrp)) {
1084 /* Failed to encode result. Release cache entry */
1085 dprintk("nfsd: failed to encode result!\n");
1086 nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
1087 *statp = rpc_system_err;
1088 return 1;
1089 }
1090 }
1091
1092 /* Store reply in cache. */
1093 nfsd_cache_update(rqstp, rqstp->rq_cachetype, statp + 1);
1094 return 1;
1095}
1096
1097int nfsd_pool_stats_open(struct inode *inode, struct file *file)
1098{
1099 int ret;
1100 struct nfsd_net *nn = net_generic(inode->i_sb->s_fs_info, nfsd_net_id);
1101
1102 mutex_lock(&nfsd_mutex);
1103 if (nn->nfsd_serv == NULL) {
1104 mutex_unlock(&nfsd_mutex);
1105 return -ENODEV;
1106 }
1107 /* bump up the psudo refcount while traversing */
1108 svc_get(nn->nfsd_serv);
1109 ret = svc_pool_stats_open(nn->nfsd_serv, file);
1110 mutex_unlock(&nfsd_mutex);
1111 return ret;
1112}
1113
1114int nfsd_pool_stats_release(struct inode *inode, struct file *file)
1115{
1116 int ret = seq_release(inode, file);
1117 struct net *net = inode->i_sb->s_fs_info;
1118
1119 mutex_lock(&nfsd_mutex);
1120 /* this function really, really should have been called svc_put() */
1121 nfsd_destroy(net);
1122 mutex_unlock(&nfsd_mutex);
1123 return ret;
1124}