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