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 = nfs4_state_start_net(net); 398 if (ret) 399 goto out_lockd; 400 401 nn->nfsd_net_up = true; 402 return 0; 403 404 out_lockd: 405 if (nn->lockd_up) { 406 lockd_down(net); 407 nn->lockd_up = 0; 408 } 409 out_socks: 410 nfsd_shutdown_generic(); 411 return ret; 412 } 413 414 static void nfsd_shutdown_net(struct net *net) 415 { 416 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 417 418 nfsd_file_cache_purge(net); 419 nfs4_state_shutdown_net(net); 420 if (nn->lockd_up) { 421 lockd_down(net); 422 nn->lockd_up = 0; 423 } 424 nn->nfsd_net_up = false; 425 nfsd_shutdown_generic(); 426 } 427 428 static int nfsd_inetaddr_event(struct notifier_block *this, unsigned long event, 429 void *ptr) 430 { 431 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr; 432 struct net_device *dev = ifa->ifa_dev->dev; 433 struct net *net = dev_net(dev); 434 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 435 struct sockaddr_in sin; 436 437 if ((event != NETDEV_DOWN) || 438 !atomic_inc_not_zero(&nn->ntf_refcnt)) 439 goto out; 440 441 if (nn->nfsd_serv) { 442 dprintk("nfsd_inetaddr_event: removed %pI4\n", &ifa->ifa_local); 443 sin.sin_family = AF_INET; 444 sin.sin_addr.s_addr = ifa->ifa_local; 445 svc_age_temp_xprts_now(nn->nfsd_serv, (struct sockaddr *)&sin); 446 } 447 atomic_dec(&nn->ntf_refcnt); 448 wake_up(&nn->ntf_wq); 449 450 out: 451 return NOTIFY_DONE; 452 } 453 454 static struct notifier_block nfsd_inetaddr_notifier = { 455 .notifier_call = nfsd_inetaddr_event, 456 }; 457 458 #if IS_ENABLED(CONFIG_IPV6) 459 static int nfsd_inet6addr_event(struct notifier_block *this, 460 unsigned long event, void *ptr) 461 { 462 struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr; 463 struct net_device *dev = ifa->idev->dev; 464 struct net *net = dev_net(dev); 465 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 466 struct sockaddr_in6 sin6; 467 468 if ((event != NETDEV_DOWN) || 469 !atomic_inc_not_zero(&nn->ntf_refcnt)) 470 goto out; 471 472 if (nn->nfsd_serv) { 473 dprintk("nfsd_inet6addr_event: removed %pI6\n", &ifa->addr); 474 sin6.sin6_family = AF_INET6; 475 sin6.sin6_addr = ifa->addr; 476 if (ipv6_addr_type(&sin6.sin6_addr) & IPV6_ADDR_LINKLOCAL) 477 sin6.sin6_scope_id = ifa->idev->dev->ifindex; 478 svc_age_temp_xprts_now(nn->nfsd_serv, (struct sockaddr *)&sin6); 479 } 480 atomic_dec(&nn->ntf_refcnt); 481 wake_up(&nn->ntf_wq); 482 out: 483 return NOTIFY_DONE; 484 } 485 486 static struct notifier_block nfsd_inet6addr_notifier = { 487 .notifier_call = nfsd_inet6addr_event, 488 }; 489 #endif 490 491 /* Only used under nfsd_mutex, so this atomic may be overkill: */ 492 static atomic_t nfsd_notifier_refcount = ATOMIC_INIT(0); 493 494 static void nfsd_last_thread(struct svc_serv *serv, struct net *net) 495 { 496 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 497 498 atomic_dec(&nn->ntf_refcnt); 499 /* check if the notifier still has clients */ 500 if (atomic_dec_return(&nfsd_notifier_refcount) == 0) { 501 unregister_inetaddr_notifier(&nfsd_inetaddr_notifier); 502 #if IS_ENABLED(CONFIG_IPV6) 503 unregister_inet6addr_notifier(&nfsd_inet6addr_notifier); 504 #endif 505 } 506 wait_event(nn->ntf_wq, atomic_read(&nn->ntf_refcnt) == 0); 507 508 /* 509 * write_ports can create the server without actually starting 510 * any threads--if we get shut down before any threads are 511 * started, then nfsd_last_thread will be run before any of this 512 * other initialization has been done except the rpcb information. 513 */ 514 svc_rpcb_cleanup(serv, net); 515 if (!nn->nfsd_net_up) 516 return; 517 518 nfsd_shutdown_net(net); 519 printk(KERN_WARNING "nfsd: last server has exited, flushing export " 520 "cache\n"); 521 nfsd_export_flush(net); 522 } 523 524 void nfsd_reset_versions(struct nfsd_net *nn) 525 { 526 int i; 527 528 for (i = 0; i < NFSD_NRVERS; i++) 529 if (nfsd_vers(nn, i, NFSD_TEST)) 530 return; 531 532 for (i = 0; i < NFSD_NRVERS; i++) 533 if (i != 4) 534 nfsd_vers(nn, i, NFSD_SET); 535 else { 536 int minor = 0; 537 while (nfsd_minorversion(nn, minor, NFSD_SET) >= 0) 538 minor++; 539 } 540 } 541 542 /* 543 * Each session guarantees a negotiated per slot memory cache for replies 544 * which in turn consumes memory beyond the v2/v3/v4.0 server. A dedicated 545 * NFSv4.1 server might want to use more memory for a DRC than a machine 546 * with mutiple services. 547 * 548 * Impose a hard limit on the number of pages for the DRC which varies 549 * according to the machines free pages. This is of course only a default. 550 * 551 * For now this is a #defined shift which could be under admin control 552 * in the future. 553 */ 554 static void set_max_drc(void) 555 { 556 #define NFSD_DRC_SIZE_SHIFT 7 557 nfsd_drc_max_mem = (nr_free_buffer_pages() 558 >> NFSD_DRC_SIZE_SHIFT) * PAGE_SIZE; 559 nfsd_drc_mem_used = 0; 560 spin_lock_init(&nfsd_drc_lock); 561 dprintk("%s nfsd_drc_max_mem %lu \n", __func__, nfsd_drc_max_mem); 562 } 563 564 static int nfsd_get_default_max_blksize(void) 565 { 566 struct sysinfo i; 567 unsigned long long target; 568 unsigned long ret; 569 570 si_meminfo(&i); 571 target = (i.totalram - i.totalhigh) << PAGE_SHIFT; 572 /* 573 * Aim for 1/4096 of memory per thread This gives 1MB on 4Gig 574 * machines, but only uses 32K on 128M machines. Bottom out at 575 * 8K on 32M and smaller. Of course, this is only a default. 576 */ 577 target >>= 12; 578 579 ret = NFSSVC_MAXBLKSIZE; 580 while (ret > target && ret >= 8*1024*2) 581 ret /= 2; 582 return ret; 583 } 584 585 static const struct svc_serv_ops nfsd_thread_sv_ops = { 586 .svo_shutdown = nfsd_last_thread, 587 .svo_function = nfsd, 588 .svo_enqueue_xprt = svc_xprt_do_enqueue, 589 .svo_setup = svc_set_num_threads, 590 .svo_module = THIS_MODULE, 591 }; 592 593 int nfsd_create_serv(struct net *net) 594 { 595 int error; 596 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 597 598 WARN_ON(!mutex_is_locked(&nfsd_mutex)); 599 if (nn->nfsd_serv) { 600 svc_get(nn->nfsd_serv); 601 return 0; 602 } 603 if (nfsd_max_blksize == 0) 604 nfsd_max_blksize = nfsd_get_default_max_blksize(); 605 nfsd_reset_versions(nn); 606 nn->nfsd_serv = svc_create_pooled(&nfsd_program, nfsd_max_blksize, 607 &nfsd_thread_sv_ops); 608 if (nn->nfsd_serv == NULL) 609 return -ENOMEM; 610 611 nn->nfsd_serv->sv_maxconn = nn->max_connections; 612 error = svc_bind(nn->nfsd_serv, net); 613 if (error < 0) { 614 svc_destroy(nn->nfsd_serv); 615 return error; 616 } 617 618 set_max_drc(); 619 /* check if the notifier is already set */ 620 if (atomic_inc_return(&nfsd_notifier_refcount) == 1) { 621 register_inetaddr_notifier(&nfsd_inetaddr_notifier); 622 #if IS_ENABLED(CONFIG_IPV6) 623 register_inet6addr_notifier(&nfsd_inet6addr_notifier); 624 #endif 625 } 626 atomic_inc(&nn->ntf_refcnt); 627 nfsd_reset_boot_verifier(nn); 628 return 0; 629 } 630 631 int nfsd_nrpools(struct net *net) 632 { 633 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 634 635 if (nn->nfsd_serv == NULL) 636 return 0; 637 else 638 return nn->nfsd_serv->sv_nrpools; 639 } 640 641 int nfsd_get_nrthreads(int n, int *nthreads, struct net *net) 642 { 643 int i = 0; 644 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 645 646 if (nn->nfsd_serv != NULL) { 647 for (i = 0; i < nn->nfsd_serv->sv_nrpools && i < n; i++) 648 nthreads[i] = nn->nfsd_serv->sv_pools[i].sp_nrthreads; 649 } 650 651 return 0; 652 } 653 654 void nfsd_destroy(struct net *net) 655 { 656 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 657 int destroy = (nn->nfsd_serv->sv_nrthreads == 1); 658 659 if (destroy) 660 svc_shutdown_net(nn->nfsd_serv, net); 661 svc_destroy(nn->nfsd_serv); 662 if (destroy) 663 nn->nfsd_serv = NULL; 664 } 665 666 int nfsd_set_nrthreads(int n, int *nthreads, struct net *net) 667 { 668 int i = 0; 669 int tot = 0; 670 int err = 0; 671 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 672 673 WARN_ON(!mutex_is_locked(&nfsd_mutex)); 674 675 if (nn->nfsd_serv == NULL || n <= 0) 676 return 0; 677 678 if (n > nn->nfsd_serv->sv_nrpools) 679 n = nn->nfsd_serv->sv_nrpools; 680 681 /* enforce a global maximum number of threads */ 682 tot = 0; 683 for (i = 0; i < n; i++) { 684 nthreads[i] = min(nthreads[i], NFSD_MAXSERVS); 685 tot += nthreads[i]; 686 } 687 if (tot > NFSD_MAXSERVS) { 688 /* total too large: scale down requested numbers */ 689 for (i = 0; i < n && tot > 0; i++) { 690 int new = nthreads[i] * NFSD_MAXSERVS / tot; 691 tot -= (nthreads[i] - new); 692 nthreads[i] = new; 693 } 694 for (i = 0; i < n && tot > 0; i++) { 695 nthreads[i]--; 696 tot--; 697 } 698 } 699 700 /* 701 * There must always be a thread in pool 0; the admin 702 * can't shut down NFS completely using pool_threads. 703 */ 704 if (nthreads[0] == 0) 705 nthreads[0] = 1; 706 707 /* apply the new numbers */ 708 svc_get(nn->nfsd_serv); 709 for (i = 0; i < n; i++) { 710 err = nn->nfsd_serv->sv_ops->svo_setup(nn->nfsd_serv, 711 &nn->nfsd_serv->sv_pools[i], nthreads[i]); 712 if (err) 713 break; 714 } 715 nfsd_destroy(net); 716 return err; 717 } 718 719 /* 720 * Adjust the number of threads and return the new number of threads. 721 * This is also the function that starts the server if necessary, if 722 * this is the first time nrservs is nonzero. 723 */ 724 int 725 nfsd_svc(int nrservs, struct net *net, const struct cred *cred) 726 { 727 int error; 728 bool nfsd_up_before; 729 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 730 731 mutex_lock(&nfsd_mutex); 732 dprintk("nfsd: creating service\n"); 733 734 nrservs = max(nrservs, 0); 735 nrservs = min(nrservs, NFSD_MAXSERVS); 736 error = 0; 737 738 if (nrservs == 0 && nn->nfsd_serv == NULL) 739 goto out; 740 741 error = nfsd_create_serv(net); 742 if (error) 743 goto out; 744 745 nfsd_up_before = nn->nfsd_net_up; 746 747 error = nfsd_startup_net(nrservs, net, cred); 748 if (error) 749 goto out_destroy; 750 error = nn->nfsd_serv->sv_ops->svo_setup(nn->nfsd_serv, 751 NULL, nrservs); 752 if (error) 753 goto out_shutdown; 754 /* We are holding a reference to nn->nfsd_serv which 755 * we don't want to count in the return value, 756 * so subtract 1 757 */ 758 error = nn->nfsd_serv->sv_nrthreads - 1; 759 out_shutdown: 760 if (error < 0 && !nfsd_up_before) 761 nfsd_shutdown_net(net); 762 out_destroy: 763 nfsd_destroy(net); /* Release server */ 764 out: 765 mutex_unlock(&nfsd_mutex); 766 return error; 767 } 768 769 #if defined(CONFIG_NFSD_V2_ACL) || defined(CONFIG_NFSD_V3_ACL) 770 static bool 771 nfsd_support_acl_version(int vers) 772 { 773 if (vers >= NFSD_ACL_MINVERS && vers < NFSD_ACL_NRVERS) 774 return nfsd_acl_version[vers] != NULL; 775 return false; 776 } 777 778 static int 779 nfsd_acl_rpcbind_set(struct net *net, const struct svc_program *progp, 780 u32 version, int family, unsigned short proto, 781 unsigned short port) 782 { 783 if (!nfsd_support_acl_version(version) || 784 !nfsd_vers(net_generic(net, nfsd_net_id), version, NFSD_TEST)) 785 return 0; 786 return svc_generic_rpcbind_set(net, progp, version, family, 787 proto, port); 788 } 789 790 static __be32 791 nfsd_acl_init_request(struct svc_rqst *rqstp, 792 const struct svc_program *progp, 793 struct svc_process_info *ret) 794 { 795 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 796 int i; 797 798 if (likely(nfsd_support_acl_version(rqstp->rq_vers) && 799 nfsd_vers(nn, rqstp->rq_vers, NFSD_TEST))) 800 return svc_generic_init_request(rqstp, progp, ret); 801 802 ret->mismatch.lovers = NFSD_ACL_NRVERS; 803 for (i = NFSD_ACL_MINVERS; i < NFSD_ACL_NRVERS; i++) { 804 if (nfsd_support_acl_version(rqstp->rq_vers) && 805 nfsd_vers(nn, i, NFSD_TEST)) { 806 ret->mismatch.lovers = i; 807 break; 808 } 809 } 810 if (ret->mismatch.lovers == NFSD_ACL_NRVERS) 811 return rpc_prog_unavail; 812 ret->mismatch.hivers = NFSD_ACL_MINVERS; 813 for (i = NFSD_ACL_NRVERS - 1; i >= NFSD_ACL_MINVERS; i--) { 814 if (nfsd_support_acl_version(rqstp->rq_vers) && 815 nfsd_vers(nn, i, NFSD_TEST)) { 816 ret->mismatch.hivers = i; 817 break; 818 } 819 } 820 return rpc_prog_mismatch; 821 } 822 #endif 823 824 static int 825 nfsd_rpcbind_set(struct net *net, const struct svc_program *progp, 826 u32 version, int family, unsigned short proto, 827 unsigned short port) 828 { 829 if (!nfsd_vers(net_generic(net, nfsd_net_id), version, NFSD_TEST)) 830 return 0; 831 return svc_generic_rpcbind_set(net, progp, version, family, 832 proto, port); 833 } 834 835 static __be32 836 nfsd_init_request(struct svc_rqst *rqstp, 837 const struct svc_program *progp, 838 struct svc_process_info *ret) 839 { 840 struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); 841 int i; 842 843 if (likely(nfsd_vers(nn, rqstp->rq_vers, NFSD_TEST))) 844 return svc_generic_init_request(rqstp, progp, ret); 845 846 ret->mismatch.lovers = NFSD_NRVERS; 847 for (i = NFSD_MINVERS; i < NFSD_NRVERS; i++) { 848 if (nfsd_vers(nn, i, NFSD_TEST)) { 849 ret->mismatch.lovers = i; 850 break; 851 } 852 } 853 if (ret->mismatch.lovers == NFSD_NRVERS) 854 return rpc_prog_unavail; 855 ret->mismatch.hivers = NFSD_MINVERS; 856 for (i = NFSD_NRVERS - 1; i >= NFSD_MINVERS; i--) { 857 if (nfsd_vers(nn, i, NFSD_TEST)) { 858 ret->mismatch.hivers = i; 859 break; 860 } 861 } 862 return rpc_prog_mismatch; 863 } 864 865 /* 866 * This is the NFS server kernel thread 867 */ 868 static int 869 nfsd(void *vrqstp) 870 { 871 struct svc_rqst *rqstp = (struct svc_rqst *) vrqstp; 872 struct svc_xprt *perm_sock = list_entry(rqstp->rq_server->sv_permsocks.next, typeof(struct svc_xprt), xpt_list); 873 struct net *net = perm_sock->xpt_net; 874 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 875 int err; 876 877 /* Lock module and set up kernel thread */ 878 mutex_lock(&nfsd_mutex); 879 880 /* At this point, the thread shares current->fs 881 * with the init process. We need to create files with the 882 * umask as defined by the client instead of init's umask. */ 883 if (unshare_fs_struct() < 0) { 884 printk("Unable to start nfsd thread: out of memory\n"); 885 goto out; 886 } 887 888 current->fs->umask = 0; 889 890 /* 891 * thread is spawned with all signals set to SIG_IGN, re-enable 892 * the ones that will bring down the thread 893 */ 894 allow_signal(SIGKILL); 895 allow_signal(SIGHUP); 896 allow_signal(SIGINT); 897 allow_signal(SIGQUIT); 898 899 nfsdstats.th_cnt++; 900 mutex_unlock(&nfsd_mutex); 901 902 set_freezable(); 903 904 /* 905 * The main request loop 906 */ 907 for (;;) { 908 /* Update sv_maxconn if it has changed */ 909 rqstp->rq_server->sv_maxconn = nn->max_connections; 910 911 /* 912 * Find a socket with data available and call its 913 * recvfrom routine. 914 */ 915 while ((err = svc_recv(rqstp, 60*60*HZ)) == -EAGAIN) 916 ; 917 if (err == -EINTR) 918 break; 919 validate_process_creds(); 920 svc_process(rqstp); 921 validate_process_creds(); 922 } 923 924 /* Clear signals before calling svc_exit_thread() */ 925 flush_signals(current); 926 927 mutex_lock(&nfsd_mutex); 928 nfsdstats.th_cnt --; 929 930 out: 931 rqstp->rq_server = NULL; 932 933 /* Release the thread */ 934 svc_exit_thread(rqstp); 935 936 nfsd_destroy(net); 937 938 /* Release module */ 939 mutex_unlock(&nfsd_mutex); 940 module_put_and_exit(0); 941 return 0; 942 } 943 944 static __be32 map_new_errors(u32 vers, __be32 nfserr) 945 { 946 if (nfserr == nfserr_jukebox && vers == 2) 947 return nfserr_dropit; 948 if (nfserr == nfserr_wrongsec && vers < 4) 949 return nfserr_acces; 950 return nfserr; 951 } 952 953 /* 954 * A write procedure can have a large argument, and a read procedure can 955 * have a large reply, but no NFSv2 or NFSv3 procedure has argument and 956 * reply that can both be larger than a page. The xdr code has taken 957 * advantage of this assumption to be a sloppy about bounds checking in 958 * some cases. Pending a rewrite of the NFSv2/v3 xdr code to fix that 959 * problem, we enforce these assumptions here: 960 */ 961 static bool nfs_request_too_big(struct svc_rqst *rqstp, 962 const struct svc_procedure *proc) 963 { 964 /* 965 * The ACL code has more careful bounds-checking and is not 966 * susceptible to this problem: 967 */ 968 if (rqstp->rq_prog != NFS_PROGRAM) 969 return false; 970 /* 971 * Ditto NFSv4 (which can in theory have argument and reply both 972 * more than a page): 973 */ 974 if (rqstp->rq_vers >= 4) 975 return false; 976 /* The reply will be small, we're OK: */ 977 if (proc->pc_xdrressize > 0 && 978 proc->pc_xdrressize < XDR_QUADLEN(PAGE_SIZE)) 979 return false; 980 981 return rqstp->rq_arg.len > PAGE_SIZE; 982 } 983 984 int 985 nfsd_dispatch(struct svc_rqst *rqstp, __be32 *statp) 986 { 987 const struct svc_procedure *proc; 988 __be32 nfserr; 989 __be32 *nfserrp; 990 991 dprintk("nfsd_dispatch: vers %d proc %d\n", 992 rqstp->rq_vers, rqstp->rq_proc); 993 proc = rqstp->rq_procinfo; 994 995 if (nfs_request_too_big(rqstp, proc)) { 996 dprintk("nfsd: NFSv%d argument too large\n", rqstp->rq_vers); 997 *statp = rpc_garbage_args; 998 return 1; 999 } 1000 /* 1001 * Give the xdr decoder a chance to change this if it wants 1002 * (necessary in the NFSv4.0 compound case) 1003 */ 1004 rqstp->rq_cachetype = proc->pc_cachetype; 1005 /* Decode arguments */ 1006 if (proc->pc_decode && 1007 !proc->pc_decode(rqstp, (__be32*)rqstp->rq_arg.head[0].iov_base)) { 1008 dprintk("nfsd: failed to decode arguments!\n"); 1009 *statp = rpc_garbage_args; 1010 return 1; 1011 } 1012 1013 /* Check whether we have this call in the cache. */ 1014 switch (nfsd_cache_lookup(rqstp)) { 1015 case RC_DROPIT: 1016 return 0; 1017 case RC_REPLY: 1018 return 1; 1019 case RC_DOIT:; 1020 /* do it */ 1021 } 1022 1023 /* need to grab the location to store the status, as 1024 * nfsv4 does some encoding while processing 1025 */ 1026 nfserrp = rqstp->rq_res.head[0].iov_base 1027 + rqstp->rq_res.head[0].iov_len; 1028 rqstp->rq_res.head[0].iov_len += sizeof(__be32); 1029 1030 /* Now call the procedure handler, and encode NFS status. */ 1031 nfserr = proc->pc_func(rqstp); 1032 nfserr = map_new_errors(rqstp->rq_vers, nfserr); 1033 if (nfserr == nfserr_dropit || test_bit(RQ_DROPME, &rqstp->rq_flags)) { 1034 dprintk("nfsd: Dropping request; may be revisited later\n"); 1035 nfsd_cache_update(rqstp, RC_NOCACHE, NULL); 1036 return 0; 1037 } 1038 1039 if (rqstp->rq_proc != 0) 1040 *nfserrp++ = nfserr; 1041 1042 /* Encode result. 1043 * For NFSv2, additional info is never returned in case of an error. 1044 */ 1045 if (!(nfserr && rqstp->rq_vers == 2)) { 1046 if (proc->pc_encode && !proc->pc_encode(rqstp, nfserrp)) { 1047 /* Failed to encode result. Release cache entry */ 1048 dprintk("nfsd: failed to encode result!\n"); 1049 nfsd_cache_update(rqstp, RC_NOCACHE, NULL); 1050 *statp = rpc_system_err; 1051 return 1; 1052 } 1053 } 1054 1055 /* Store reply in cache. */ 1056 nfsd_cache_update(rqstp, rqstp->rq_cachetype, statp + 1); 1057 return 1; 1058 } 1059 1060 int nfsd_pool_stats_open(struct inode *inode, struct file *file) 1061 { 1062 int ret; 1063 struct nfsd_net *nn = net_generic(inode->i_sb->s_fs_info, nfsd_net_id); 1064 1065 mutex_lock(&nfsd_mutex); 1066 if (nn->nfsd_serv == NULL) { 1067 mutex_unlock(&nfsd_mutex); 1068 return -ENODEV; 1069 } 1070 /* bump up the psudo refcount while traversing */ 1071 svc_get(nn->nfsd_serv); 1072 ret = svc_pool_stats_open(nn->nfsd_serv, file); 1073 mutex_unlock(&nfsd_mutex); 1074 return ret; 1075 } 1076 1077 int nfsd_pool_stats_release(struct inode *inode, struct file *file) 1078 { 1079 int ret = seq_release(inode, file); 1080 struct net *net = inode->i_sb->s_fs_info; 1081 1082 mutex_lock(&nfsd_mutex); 1083 /* this function really, really should have been called svc_put() */ 1084 nfsd_destroy(net); 1085 mutex_unlock(&nfsd_mutex); 1086 return ret; 1087 } 1088