]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blob - fs/nfsd/nfssvc.c
nfsd: Containerise filecache laundrette
[mirror_ubuntu-focal-kernel.git] / fs / nfsd / nfssvc.c
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
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
34 extern struct svc_program nfsd_program;
35 static int nfsd(void *vrqstp);
36 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
37 static int nfsd_acl_rpcbind_set(struct net *,
38 const struct svc_program *,
39 u32, int,
40 unsigned short,
41 unsigned short);
42 static __be32 nfsd_acl_init_request(struct svc_rqst *,
43 const struct svc_program *,
44 struct svc_process_info *);
45 #endif
46 static int nfsd_rpcbind_set(struct net *,
47 const struct svc_program *,
48 u32, int,
49 unsigned short,
50 unsigned short);
51 static __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 */
77 DEFINE_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 */
85 spinlock_t nfsd_drc_lock;
86 unsigned long nfsd_drc_max_mem;
87 unsigned long nfsd_drc_mem_used;
88
89 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
90 static struct svc_stat nfsd_acl_svcstats;
91 static 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
99 static 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
111 static 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
116 static 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
129 struct 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
144 static bool
145 nfsd_support_version(int vers)
146 {
147 if (vers >= NFSD_MINVERS && vers < NFSD_NRVERS)
148 return nfsd_version[vers] != NULL;
149 return false;
150 }
151
152 static bool *
153 nfsd_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
166 static bool *
167 nfsd_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
181 void
182 nfsd_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
190 static void
191 nfsd_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
201 int 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
225 static void
226 nfsd_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
237 int 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
274 int 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
286 static 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
307 static int nfsd_users = 0;
308
309 static 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
325 out_file_cache:
326 nfsd_file_cache_shutdown();
327 dec_users:
328 nfsd_users--;
329 return ret;
330 }
331
332 static void nfsd_shutdown_generic(void)
333 {
334 if (--nfsd_users)
335 return;
336
337 nfs4_state_shutdown();
338 nfsd_file_cache_shutdown();
339 }
340
341 static 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
346 void 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
363 static void nfsd_reset_boot_verifier_locked(struct nfsd_net *nn)
364 {
365 ktime_get_real_ts64(&nn->nfssvc_boot);
366 }
367
368 void 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
375 static 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
407 out_filecache:
408 nfsd_file_cache_shutdown_net(net);
409 out_lockd:
410 if (nn->lockd_up) {
411 lockd_down(net);
412 nn->lockd_up = 0;
413 }
414 out_socks:
415 nfsd_shutdown_generic();
416 return ret;
417 }
418
419 static void nfsd_shutdown_net(struct net *net)
420 {
421 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
422
423 nfsd_file_cache_shutdown_net(net);
424 nfs4_state_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
433 static 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
455 out:
456 return NOTIFY_DONE;
457 }
458
459 static struct notifier_block nfsd_inetaddr_notifier = {
460 .notifier_call = nfsd_inetaddr_event,
461 };
462
463 #if IS_ENABLED(CONFIG_IPV6)
464 static 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);
487 out:
488 return NOTIFY_DONE;
489 }
490
491 static 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: */
497 static atomic_t nfsd_notifier_refcount = ATOMIC_INIT(0);
498
499 static 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
528 void 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 */
558 static 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
568 static 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
589 static 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
597 int nfsd_create_serv(struct net *net)
598 {
599 int error;
600 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
601
602 WARN_ON(!mutex_is_locked(&nfsd_mutex));
603 if (nn->nfsd_serv) {
604 svc_get(nn->nfsd_serv);
605 return 0;
606 }
607 if (nfsd_max_blksize == 0)
608 nfsd_max_blksize = nfsd_get_default_max_blksize();
609 nfsd_reset_versions(nn);
610 nn->nfsd_serv = svc_create_pooled(&nfsd_program, nfsd_max_blksize,
611 &nfsd_thread_sv_ops);
612 if (nn->nfsd_serv == NULL)
613 return -ENOMEM;
614
615 nn->nfsd_serv->sv_maxconn = nn->max_connections;
616 error = svc_bind(nn->nfsd_serv, net);
617 if (error < 0) {
618 svc_destroy(nn->nfsd_serv);
619 return error;
620 }
621
622 set_max_drc();
623 /* check if the notifier is already set */
624 if (atomic_inc_return(&nfsd_notifier_refcount) == 1) {
625 register_inetaddr_notifier(&nfsd_inetaddr_notifier);
626 #if IS_ENABLED(CONFIG_IPV6)
627 register_inet6addr_notifier(&nfsd_inet6addr_notifier);
628 #endif
629 }
630 atomic_inc(&nn->ntf_refcnt);
631 nfsd_reset_boot_verifier(nn);
632 return 0;
633 }
634
635 int nfsd_nrpools(struct net *net)
636 {
637 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
638
639 if (nn->nfsd_serv == NULL)
640 return 0;
641 else
642 return nn->nfsd_serv->sv_nrpools;
643 }
644
645 int nfsd_get_nrthreads(int n, int *nthreads, struct net *net)
646 {
647 int i = 0;
648 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
649
650 if (nn->nfsd_serv != NULL) {
651 for (i = 0; i < nn->nfsd_serv->sv_nrpools && i < n; i++)
652 nthreads[i] = nn->nfsd_serv->sv_pools[i].sp_nrthreads;
653 }
654
655 return 0;
656 }
657
658 void nfsd_destroy(struct net *net)
659 {
660 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
661 int destroy = (nn->nfsd_serv->sv_nrthreads == 1);
662
663 if (destroy)
664 svc_shutdown_net(nn->nfsd_serv, net);
665 svc_destroy(nn->nfsd_serv);
666 if (destroy)
667 nn->nfsd_serv = NULL;
668 }
669
670 int nfsd_set_nrthreads(int n, int *nthreads, struct net *net)
671 {
672 int i = 0;
673 int tot = 0;
674 int err = 0;
675 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
676
677 WARN_ON(!mutex_is_locked(&nfsd_mutex));
678
679 if (nn->nfsd_serv == NULL || n <= 0)
680 return 0;
681
682 if (n > nn->nfsd_serv->sv_nrpools)
683 n = nn->nfsd_serv->sv_nrpools;
684
685 /* enforce a global maximum number of threads */
686 tot = 0;
687 for (i = 0; i < n; i++) {
688 nthreads[i] = min(nthreads[i], NFSD_MAXSERVS);
689 tot += nthreads[i];
690 }
691 if (tot > NFSD_MAXSERVS) {
692 /* total too large: scale down requested numbers */
693 for (i = 0; i < n && tot > 0; i++) {
694 int new = nthreads[i] * NFSD_MAXSERVS / tot;
695 tot -= (nthreads[i] - new);
696 nthreads[i] = new;
697 }
698 for (i = 0; i < n && tot > 0; i++) {
699 nthreads[i]--;
700 tot--;
701 }
702 }
703
704 /*
705 * There must always be a thread in pool 0; the admin
706 * can't shut down NFS completely using pool_threads.
707 */
708 if (nthreads[0] == 0)
709 nthreads[0] = 1;
710
711 /* apply the new numbers */
712 svc_get(nn->nfsd_serv);
713 for (i = 0; i < n; i++) {
714 err = nn->nfsd_serv->sv_ops->svo_setup(nn->nfsd_serv,
715 &nn->nfsd_serv->sv_pools[i], nthreads[i]);
716 if (err)
717 break;
718 }
719 nfsd_destroy(net);
720 return err;
721 }
722
723 /*
724 * Adjust the number of threads and return the new number of threads.
725 * This is also the function that starts the server if necessary, if
726 * this is the first time nrservs is nonzero.
727 */
728 int
729 nfsd_svc(int nrservs, struct net *net, const struct cred *cred)
730 {
731 int error;
732 bool nfsd_up_before;
733 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
734
735 mutex_lock(&nfsd_mutex);
736 dprintk("nfsd: creating service\n");
737
738 nrservs = max(nrservs, 0);
739 nrservs = min(nrservs, NFSD_MAXSERVS);
740 error = 0;
741
742 if (nrservs == 0 && nn->nfsd_serv == NULL)
743 goto out;
744
745 error = nfsd_create_serv(net);
746 if (error)
747 goto out;
748
749 nfsd_up_before = nn->nfsd_net_up;
750
751 error = nfsd_startup_net(nrservs, net, cred);
752 if (error)
753 goto out_destroy;
754 error = nn->nfsd_serv->sv_ops->svo_setup(nn->nfsd_serv,
755 NULL, nrservs);
756 if (error)
757 goto out_shutdown;
758 /* We are holding a reference to nn->nfsd_serv which
759 * we don't want to count in the return value,
760 * so subtract 1
761 */
762 error = nn->nfsd_serv->sv_nrthreads - 1;
763 out_shutdown:
764 if (error < 0 && !nfsd_up_before)
765 nfsd_shutdown_net(net);
766 out_destroy:
767 nfsd_destroy(net); /* Release server */
768 out:
769 mutex_unlock(&nfsd_mutex);
770 return error;
771 }
772
773 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL)
774 static bool
775 nfsd_support_acl_version(int vers)
776 {
777 if (vers >= NFSD_ACL_MINVERS && vers < NFSD_ACL_NRVERS)
778 return nfsd_acl_version[vers] != NULL;
779 return false;
780 }
781
782 static int
783 nfsd_acl_rpcbind_set(struct net *net, const struct svc_program *progp,
784 u32 version, int family, unsigned short proto,
785 unsigned short port)
786 {
787 if (!nfsd_support_acl_version(version) ||
788 !nfsd_vers(net_generic(net, nfsd_net_id), version, NFSD_TEST))
789 return 0;
790 return svc_generic_rpcbind_set(net, progp, version, family,
791 proto, port);
792 }
793
794 static __be32
795 nfsd_acl_init_request(struct svc_rqst *rqstp,
796 const struct svc_program *progp,
797 struct svc_process_info *ret)
798 {
799 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
800 int i;
801
802 if (likely(nfsd_support_acl_version(rqstp->rq_vers) &&
803 nfsd_vers(nn, rqstp->rq_vers, NFSD_TEST)))
804 return svc_generic_init_request(rqstp, progp, ret);
805
806 ret->mismatch.lovers = NFSD_ACL_NRVERS;
807 for (i = NFSD_ACL_MINVERS; i < NFSD_ACL_NRVERS; i++) {
808 if (nfsd_support_acl_version(rqstp->rq_vers) &&
809 nfsd_vers(nn, i, NFSD_TEST)) {
810 ret->mismatch.lovers = i;
811 break;
812 }
813 }
814 if (ret->mismatch.lovers == NFSD_ACL_NRVERS)
815 return rpc_prog_unavail;
816 ret->mismatch.hivers = NFSD_ACL_MINVERS;
817 for (i = NFSD_ACL_NRVERS - 1; i >= NFSD_ACL_MINVERS; i--) {
818 if (nfsd_support_acl_version(rqstp->rq_vers) &&
819 nfsd_vers(nn, i, NFSD_TEST)) {
820 ret->mismatch.hivers = i;
821 break;
822 }
823 }
824 return rpc_prog_mismatch;
825 }
826 #endif
827
828 static int
829 nfsd_rpcbind_set(struct net *net, const struct svc_program *progp,
830 u32 version, int family, unsigned short proto,
831 unsigned short port)
832 {
833 if (!nfsd_vers(net_generic(net, nfsd_net_id), version, NFSD_TEST))
834 return 0;
835 return svc_generic_rpcbind_set(net, progp, version, family,
836 proto, port);
837 }
838
839 static __be32
840 nfsd_init_request(struct svc_rqst *rqstp,
841 const struct svc_program *progp,
842 struct svc_process_info *ret)
843 {
844 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id);
845 int i;
846
847 if (likely(nfsd_vers(nn, rqstp->rq_vers, NFSD_TEST)))
848 return svc_generic_init_request(rqstp, progp, ret);
849
850 ret->mismatch.lovers = NFSD_NRVERS;
851 for (i = NFSD_MINVERS; i < NFSD_NRVERS; i++) {
852 if (nfsd_vers(nn, i, NFSD_TEST)) {
853 ret->mismatch.lovers = i;
854 break;
855 }
856 }
857 if (ret->mismatch.lovers == NFSD_NRVERS)
858 return rpc_prog_unavail;
859 ret->mismatch.hivers = NFSD_MINVERS;
860 for (i = NFSD_NRVERS - 1; i >= NFSD_MINVERS; i--) {
861 if (nfsd_vers(nn, i, NFSD_TEST)) {
862 ret->mismatch.hivers = i;
863 break;
864 }
865 }
866 return rpc_prog_mismatch;
867 }
868
869 /*
870 * This is the NFS server kernel thread
871 */
872 static int
873 nfsd(void *vrqstp)
874 {
875 struct svc_rqst *rqstp = (struct svc_rqst *) vrqstp;
876 struct svc_xprt *perm_sock = list_entry(rqstp->rq_server->sv_permsocks.next, typeof(struct svc_xprt), xpt_list);
877 struct net *net = perm_sock->xpt_net;
878 struct nfsd_net *nn = net_generic(net, nfsd_net_id);
879 int err;
880
881 /* Lock module and set up kernel thread */
882 mutex_lock(&nfsd_mutex);
883
884 /* At this point, the thread shares current->fs
885 * with the init process. We need to create files with the
886 * umask as defined by the client instead of init's umask. */
887 if (unshare_fs_struct() < 0) {
888 printk("Unable to start nfsd thread: out of memory\n");
889 goto out;
890 }
891
892 current->fs->umask = 0;
893
894 /*
895 * thread is spawned with all signals set to SIG_IGN, re-enable
896 * the ones that will bring down the thread
897 */
898 allow_signal(SIGKILL);
899 allow_signal(SIGHUP);
900 allow_signal(SIGINT);
901 allow_signal(SIGQUIT);
902
903 nfsdstats.th_cnt++;
904 mutex_unlock(&nfsd_mutex);
905
906 set_freezable();
907
908 /*
909 * The main request loop
910 */
911 for (;;) {
912 /* Update sv_maxconn if it has changed */
913 rqstp->rq_server->sv_maxconn = nn->max_connections;
914
915 /*
916 * Find a socket with data available and call its
917 * recvfrom routine.
918 */
919 while ((err = svc_recv(rqstp, 60*60*HZ)) == -EAGAIN)
920 ;
921 if (err == -EINTR)
922 break;
923 validate_process_creds();
924 svc_process(rqstp);
925 validate_process_creds();
926 }
927
928 /* Clear signals before calling svc_exit_thread() */
929 flush_signals(current);
930
931 mutex_lock(&nfsd_mutex);
932 nfsdstats.th_cnt --;
933
934 out:
935 rqstp->rq_server = NULL;
936
937 /* Release the thread */
938 svc_exit_thread(rqstp);
939
940 nfsd_destroy(net);
941
942 /* Release module */
943 mutex_unlock(&nfsd_mutex);
944 module_put_and_exit(0);
945 return 0;
946 }
947
948 static __be32 map_new_errors(u32 vers, __be32 nfserr)
949 {
950 if (nfserr == nfserr_jukebox && vers == 2)
951 return nfserr_dropit;
952 if (nfserr == nfserr_wrongsec && vers < 4)
953 return nfserr_acces;
954 return nfserr;
955 }
956
957 /*
958 * A write procedure can have a large argument, and a read procedure can
959 * have a large reply, but no NFSv2 or NFSv3 procedure has argument and
960 * reply that can both be larger than a page. The xdr code has taken
961 * advantage of this assumption to be a sloppy about bounds checking in
962 * some cases. Pending a rewrite of the NFSv2/v3 xdr code to fix that
963 * problem, we enforce these assumptions here:
964 */
965 static bool nfs_request_too_big(struct svc_rqst *rqstp,
966 const struct svc_procedure *proc)
967 {
968 /*
969 * The ACL code has more careful bounds-checking and is not
970 * susceptible to this problem:
971 */
972 if (rqstp->rq_prog != NFS_PROGRAM)
973 return false;
974 /*
975 * Ditto NFSv4 (which can in theory have argument and reply both
976 * more than a page):
977 */
978 if (rqstp->rq_vers >= 4)
979 return false;
980 /* The reply will be small, we're OK: */
981 if (proc->pc_xdrressize > 0 &&
982 proc->pc_xdrressize < XDR_QUADLEN(PAGE_SIZE))
983 return false;
984
985 return rqstp->rq_arg.len > PAGE_SIZE;
986 }
987
988 int
989 nfsd_dispatch(struct svc_rqst *rqstp, __be32 *statp)
990 {
991 const struct svc_procedure *proc;
992 __be32 nfserr;
993 __be32 *nfserrp;
994
995 dprintk("nfsd_dispatch: vers %d proc %d\n",
996 rqstp->rq_vers, rqstp->rq_proc);
997 proc = rqstp->rq_procinfo;
998
999 if (nfs_request_too_big(rqstp, proc)) {
1000 dprintk("nfsd: NFSv%d argument too large\n", rqstp->rq_vers);
1001 *statp = rpc_garbage_args;
1002 return 1;
1003 }
1004 /*
1005 * Give the xdr decoder a chance to change this if it wants
1006 * (necessary in the NFSv4.0 compound case)
1007 */
1008 rqstp->rq_cachetype = proc->pc_cachetype;
1009 /* Decode arguments */
1010 if (proc->pc_decode &&
1011 !proc->pc_decode(rqstp, (__be32*)rqstp->rq_arg.head[0].iov_base)) {
1012 dprintk("nfsd: failed to decode arguments!\n");
1013 *statp = rpc_garbage_args;
1014 return 1;
1015 }
1016
1017 /* Check whether we have this call in the cache. */
1018 switch (nfsd_cache_lookup(rqstp)) {
1019 case RC_DROPIT:
1020 return 0;
1021 case RC_REPLY:
1022 return 1;
1023 case RC_DOIT:;
1024 /* do it */
1025 }
1026
1027 /* need to grab the location to store the status, as
1028 * nfsv4 does some encoding while processing
1029 */
1030 nfserrp = rqstp->rq_res.head[0].iov_base
1031 + rqstp->rq_res.head[0].iov_len;
1032 rqstp->rq_res.head[0].iov_len += sizeof(__be32);
1033
1034 /* Now call the procedure handler, and encode NFS status. */
1035 nfserr = proc->pc_func(rqstp);
1036 nfserr = map_new_errors(rqstp->rq_vers, nfserr);
1037 if (nfserr == nfserr_dropit || test_bit(RQ_DROPME, &rqstp->rq_flags)) {
1038 dprintk("nfsd: Dropping request; may be revisited later\n");
1039 nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
1040 return 0;
1041 }
1042
1043 if (rqstp->rq_proc != 0)
1044 *nfserrp++ = nfserr;
1045
1046 /* Encode result.
1047 * For NFSv2, additional info is never returned in case of an error.
1048 */
1049 if (!(nfserr && rqstp->rq_vers == 2)) {
1050 if (proc->pc_encode && !proc->pc_encode(rqstp, nfserrp)) {
1051 /* Failed to encode result. Release cache entry */
1052 dprintk("nfsd: failed to encode result!\n");
1053 nfsd_cache_update(rqstp, RC_NOCACHE, NULL);
1054 *statp = rpc_system_err;
1055 return 1;
1056 }
1057 }
1058
1059 /* Store reply in cache. */
1060 nfsd_cache_update(rqstp, rqstp->rq_cachetype, statp + 1);
1061 return 1;
1062 }
1063
1064 int nfsd_pool_stats_open(struct inode *inode, struct file *file)
1065 {
1066 int ret;
1067 struct nfsd_net *nn = net_generic(inode->i_sb->s_fs_info, nfsd_net_id);
1068
1069 mutex_lock(&nfsd_mutex);
1070 if (nn->nfsd_serv == NULL) {
1071 mutex_unlock(&nfsd_mutex);
1072 return -ENODEV;
1073 }
1074 /* bump up the psudo refcount while traversing */
1075 svc_get(nn->nfsd_serv);
1076 ret = svc_pool_stats_open(nn->nfsd_serv, file);
1077 mutex_unlock(&nfsd_mutex);
1078 return ret;
1079 }
1080
1081 int nfsd_pool_stats_release(struct inode *inode, struct file *file)
1082 {
1083 int ret = seq_release(inode, file);
1084 struct net *net = inode->i_sb->s_fs_info;
1085
1086 mutex_lock(&nfsd_mutex);
1087 /* this function really, really should have been called svc_put() */
1088 nfsd_destroy(net);
1089 mutex_unlock(&nfsd_mutex);
1090 return ret;
1091 }