1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Syscall interface to knfsd. 4 * 5 * Copyright (C) 1995, 1996 Olaf Kirch <okir@monad.swb.de> 6 */ 7 8 #include <linux/slab.h> 9 #include <linux/namei.h> 10 #include <linux/ctype.h> 11 #include <linux/fs_context.h> 12 13 #include <linux/sunrpc/cache.h> 14 #include <linux/sunrpc/svcsock.h> 15 #include <linux/lockd/bind.h> 16 #include <linux/sunrpc/addr.h> 17 #include <linux/sunrpc/gss_api.h> 18 #include <linux/sunrpc/rpc_pipe_fs.h> 19 #include <linux/sunrpc/svc.h> 20 #include <linux/module.h> 21 #include <linux/fsnotify.h> 22 #include <linux/nfslocalio.h> 23 #include <linux/nfs3.h> 24 25 #include "idmap.h" 26 #include "nfsd.h" 27 #include "nfserr.h" 28 #include "nfs4ctl.h" 29 #include "netns.h" 30 #include "stats.h" 31 #include "cache.h" 32 #include "state.h" 33 #include "netns.h" 34 #include "pnfs.h" 35 #include "filecache.h" 36 #include "trace.h" 37 #include "netlink.h" 38 39 /* 40 * We have a single directory with several nodes in it. 41 */ 42 enum { 43 NFSD_Root = 1, 44 NFSD_List, 45 NFSD_Export_Stats, 46 NFSD_Export_features, 47 NFSD_Fh, 48 NFSD_FO_UnlockIP, 49 NFSD_FO_UnlockFS, 50 NFSD_Threads, 51 NFSD_Pool_Threads, 52 NFSD_Pool_Stats, 53 NFSD_Reply_Cache_Stats, 54 NFSD_Versions, 55 NFSD_Ports, 56 NFSD_MaxBlkSize, 57 NFSD_Filecache, 58 NFSD_Leasetime, 59 NFSD_Gracetime, 60 NFSD_RecoveryDir, 61 NFSD_V4EndGrace, 62 NFSD_MaxReserved 63 }; 64 65 /* 66 * write() for these nodes. 67 */ 68 static ssize_t write_filehandle(struct file *file, char *buf, size_t size); 69 static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size); 70 static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size); 71 static ssize_t write_threads(struct file *file, char *buf, size_t size); 72 static ssize_t write_pool_threads(struct file *file, char *buf, size_t size); 73 static ssize_t write_versions(struct file *file, char *buf, size_t size); 74 static ssize_t write_ports(struct file *file, char *buf, size_t size); 75 static ssize_t write_maxblksize(struct file *file, char *buf, size_t size); 76 #ifdef CONFIG_NFSD_V4 77 static ssize_t write_leasetime(struct file *file, char *buf, size_t size); 78 static ssize_t write_gracetime(struct file *file, char *buf, size_t size); 79 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING 80 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size); 81 #endif 82 static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size); 83 #endif 84 85 static ssize_t (*const write_op[])(struct file *, char *, size_t) = { 86 [NFSD_Fh] = write_filehandle, 87 [NFSD_FO_UnlockIP] = write_unlock_ip, 88 [NFSD_FO_UnlockFS] = write_unlock_fs, 89 [NFSD_Threads] = write_threads, 90 [NFSD_Pool_Threads] = write_pool_threads, 91 [NFSD_Versions] = write_versions, 92 [NFSD_Ports] = write_ports, 93 [NFSD_MaxBlkSize] = write_maxblksize, 94 #ifdef CONFIG_NFSD_V4 95 [NFSD_Leasetime] = write_leasetime, 96 [NFSD_Gracetime] = write_gracetime, 97 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING 98 [NFSD_RecoveryDir] = write_recoverydir, 99 #endif 100 [NFSD_V4EndGrace] = write_v4_end_grace, 101 #endif 102 }; 103 104 static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos) 105 { 106 ino_t ino = file_inode(file)->i_ino; 107 char *data; 108 ssize_t rv; 109 110 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino]) 111 return -EINVAL; 112 113 data = simple_transaction_get(file, buf, size); 114 if (IS_ERR(data)) 115 return PTR_ERR(data); 116 117 rv = write_op[ino](file, data, size); 118 if (rv < 0) 119 return rv; 120 121 simple_transaction_set(file, rv); 122 return size; 123 } 124 125 static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos) 126 { 127 if (! file->private_data) { 128 /* An attempt to read a transaction file without writing 129 * causes a 0-byte write so that the file can return 130 * state information 131 */ 132 ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos); 133 if (rv < 0) 134 return rv; 135 } 136 return simple_transaction_read(file, buf, size, pos); 137 } 138 139 static const struct file_operations transaction_ops = { 140 .write = nfsctl_transaction_write, 141 .read = nfsctl_transaction_read, 142 .release = simple_transaction_release, 143 .llseek = default_llseek, 144 }; 145 146 static int exports_net_open(struct net *net, struct file *file) 147 { 148 int err; 149 struct seq_file *seq; 150 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 151 152 err = seq_open(file, &nfs_exports_op); 153 if (err) 154 return err; 155 156 seq = file->private_data; 157 seq->private = nn->svc_export_cache; 158 get_net(net); 159 return 0; 160 } 161 162 static int exports_release(struct inode *inode, struct file *file) 163 { 164 struct seq_file *seq = file->private_data; 165 struct cache_detail *cd = seq->private; 166 167 put_net(cd->net); 168 return seq_release(inode, file); 169 } 170 171 static int exports_nfsd_open(struct inode *inode, struct file *file) 172 { 173 return exports_net_open(inode->i_sb->s_fs_info, file); 174 } 175 176 static const struct file_operations exports_nfsd_operations = { 177 .open = exports_nfsd_open, 178 .read = seq_read, 179 .llseek = seq_lseek, 180 .release = exports_release, 181 }; 182 183 static int export_features_show(struct seq_file *m, void *v) 184 { 185 seq_printf(m, "0x%x 0x%x\n", NFSEXP_ALLFLAGS, NFSEXP_SECINFO_FLAGS); 186 return 0; 187 } 188 189 DEFINE_SHOW_ATTRIBUTE(export_features); 190 191 static int nfsd_pool_stats_open(struct inode *inode, struct file *file) 192 { 193 struct nfsd_net *nn = net_generic(inode->i_sb->s_fs_info, nfsd_net_id); 194 195 return svc_pool_stats_open(&nn->nfsd_info, file); 196 } 197 198 static const struct file_operations pool_stats_operations = { 199 .open = nfsd_pool_stats_open, 200 .read = seq_read, 201 .llseek = seq_lseek, 202 .release = seq_release, 203 }; 204 205 DEFINE_SHOW_ATTRIBUTE(nfsd_reply_cache_stats); 206 207 DEFINE_SHOW_ATTRIBUTE(nfsd_file_cache_stats); 208 209 /*----------------------------------------------------------------------------*/ 210 /* 211 * payload - write methods 212 */ 213 214 static inline struct net *netns(struct file *file) 215 { 216 return file_inode(file)->i_sb->s_fs_info; 217 } 218 219 /* 220 * write_unlock_ip - Release all locks used by a client 221 * 222 * Experimental. 223 * 224 * Input: 225 * buf: '\n'-terminated C string containing a 226 * presentation format IP address 227 * size: length of C string in @buf 228 * Output: 229 * On success: returns zero if all specified locks were released; 230 * returns one if one or more locks were not released 231 * On error: return code is negative errno value 232 */ 233 static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size) 234 { 235 struct sockaddr_storage address; 236 struct sockaddr *sap = (struct sockaddr *)&address; 237 size_t salen = sizeof(address); 238 char *fo_path; 239 struct net *net = netns(file); 240 241 /* sanity check */ 242 if (size == 0) 243 return -EINVAL; 244 245 if (buf[size-1] != '\n') 246 return -EINVAL; 247 248 fo_path = buf; 249 if (qword_get(&buf, fo_path, size) < 0) 250 return -EINVAL; 251 252 if (rpc_pton(net, fo_path, size, sap, salen) == 0) 253 return -EINVAL; 254 255 trace_nfsd_ctl_unlock_ip(net, sap, svc_addr_len(sap)); 256 return nlmsvc_unlock_all_by_ip(sap); 257 } 258 259 /* 260 * write_unlock_fs - Release all locks on a local file system 261 * 262 * Experimental. 263 * 264 * Input: 265 * buf: '\n'-terminated C string containing the 266 * absolute pathname of a local file system 267 * size: length of C string in @buf 268 * Output: 269 * On success: returns zero if all specified locks were released; 270 * returns one if one or more locks were not released 271 * On error: return code is negative errno value 272 */ 273 static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size) 274 { 275 struct path path; 276 char *fo_path; 277 int error; 278 struct nfsd_net *nn; 279 280 /* sanity check */ 281 if (size == 0) 282 return -EINVAL; 283 284 if (buf[size-1] != '\n') 285 return -EINVAL; 286 287 fo_path = buf; 288 if (qword_get(&buf, fo_path, size) < 0) 289 return -EINVAL; 290 trace_nfsd_ctl_unlock_fs(netns(file), fo_path); 291 error = kern_path(fo_path, 0, &path); 292 if (error) 293 return error; 294 295 /* 296 * XXX: Needs better sanity checking. Otherwise we could end up 297 * releasing locks on the wrong file system. 298 * 299 * For example: 300 * 1. Does the path refer to a directory? 301 * 2. Is that directory a mount point, or 302 * 3. Is that directory the root of an exported file system? 303 */ 304 error = nlmsvc_unlock_all_by_sb(path.dentry->d_sb); 305 mutex_lock(&nfsd_mutex); 306 nn = net_generic(netns(file), nfsd_net_id); 307 if (test_bit(NFSD_NET_UP, &nn->flags)) { 308 nfsd4_cancel_copy_by_sb(netns(file), path.dentry->d_sb); 309 nfsd4_revoke_states(nn, path.dentry->d_sb); 310 } else { 311 error = -EINVAL; 312 } 313 mutex_unlock(&nfsd_mutex); 314 315 path_put(&path); 316 return error; 317 } 318 319 /* 320 * write_filehandle - Get a variable-length NFS file handle by path 321 * 322 * On input, the buffer contains a '\n'-terminated C string comprised of 323 * three alphanumeric words separated by whitespace. The string may 324 * contain escape sequences. 325 * 326 * Input: 327 * buf: 328 * domain: client domain name 329 * path: export pathname 330 * maxsize: numeric maximum size of 331 * @buf 332 * size: length of C string in @buf 333 * Output: 334 * On success: passed-in buffer filled with '\n'-terminated C 335 * string containing a ASCII hex text version 336 * of the NFS file handle; 337 * return code is the size in bytes of the string 338 * On error: return code is negative errno value 339 */ 340 static ssize_t write_filehandle(struct file *file, char *buf, size_t size) 341 { 342 char *dname, *path; 343 int maxsize; 344 char *mesg = buf; 345 int len; 346 struct auth_domain *dom; 347 struct knfsd_fh fh; 348 349 if (size == 0) 350 return -EINVAL; 351 352 if (buf[size-1] != '\n') 353 return -EINVAL; 354 buf[size-1] = 0; 355 356 dname = mesg; 357 len = qword_get(&mesg, dname, size); 358 if (len <= 0) 359 return -EINVAL; 360 361 path = dname+len+1; 362 len = qword_get(&mesg, path, size); 363 if (len <= 0) 364 return -EINVAL; 365 366 len = get_int(&mesg, &maxsize); 367 if (len) 368 return len; 369 370 if (maxsize < NFS_FHSIZE) 371 return -EINVAL; 372 maxsize = min(maxsize, NFS3_FHSIZE); 373 374 if (qword_get(&mesg, mesg, size) > 0) 375 return -EINVAL; 376 377 trace_nfsd_ctl_filehandle(netns(file), dname, path, maxsize); 378 379 /* we have all the words, they are in buf.. */ 380 dom = unix_domain_find(dname); 381 if (!dom) 382 return -ENOMEM; 383 384 len = exp_rootfh(netns(file), dom, path, &fh, maxsize); 385 auth_domain_put(dom); 386 if (len) 387 return len; 388 389 mesg = buf; 390 len = SIMPLE_TRANSACTION_LIMIT; 391 qword_addhex(&mesg, &len, fh.fh_raw, fh.fh_size); 392 mesg[-1] = '\n'; 393 return mesg - buf; 394 } 395 396 /* 397 * write_threads - Start NFSD, or report the configured number of threads 398 * 399 * Input: 400 * buf: ignored 401 * size: zero 402 * Output: 403 * On success: passed-in buffer filled with '\n'-terminated C 404 * string numeric value representing the configured 405 * number of NFSD threads; 406 * return code is the size in bytes of the string 407 * On error: return code is zero 408 * 409 * OR 410 * 411 * Input: 412 * buf: C string containing an unsigned 413 * integer value representing the 414 * number of NFSD threads to start 415 * size: non-zero length of C string in @buf 416 * Output: 417 * On success: NFS service is started; 418 * passed-in buffer filled with '\n'-terminated C 419 * string numeric value representing the configured 420 * number of NFSD threads; 421 * return code is the size in bytes of the string 422 * On error: return code is zero or a negative errno value 423 */ 424 static ssize_t write_threads(struct file *file, char *buf, size_t size) 425 { 426 char *mesg = buf; 427 int rv; 428 struct net *net = netns(file); 429 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 430 431 if (size > 0) { 432 int newthreads; 433 rv = get_int(&mesg, &newthreads); 434 if (rv) 435 return rv; 436 if (newthreads < 0) 437 return -EINVAL; 438 trace_nfsd_ctl_threads(net, newthreads); 439 mutex_lock(&nfsd_mutex); 440 if (newthreads > 0 || nn->nfsd_serv != NULL) 441 rv = nfsd_svc(1, &newthreads, net, file->f_cred, NULL); 442 else 443 rv = 0; 444 mutex_unlock(&nfsd_mutex); 445 if (rv < 0) 446 return rv; 447 } else 448 rv = nfsd_nrthreads(net); 449 450 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", rv); 451 } 452 453 /* 454 * write_pool_threads - Set or report the configured number of threads per pool 455 * 456 * Input: 457 * buf: ignored 458 * size: zero 459 * 460 * OR 461 * 462 * Input: 463 * buf: C string containing whitespace- 464 * separated unsigned integer values 465 * representing the number of NFSD 466 * threads to start in each pool 467 * size: non-zero length of C string in @buf 468 * Output: 469 * On success: passed-in buffer filled with '\n'-terminated C 470 * string containing integer values representing the 471 * configured number of NFSD threads in each pool; 472 * return code is the size in bytes of the string 473 * On error: return code is zero or a negative errno value 474 */ 475 static ssize_t write_pool_threads(struct file *file, char *buf, size_t size) 476 { 477 /* if size > 0, look for an array of number of threads per node 478 * and apply them then write out number of threads per node as reply 479 */ 480 char *mesg = buf; 481 int i; 482 int rv; 483 size_t len; 484 int npools; 485 int *nthreads; 486 struct net *net = netns(file); 487 488 mutex_lock(&nfsd_mutex); 489 npools = nfsd_nrpools(net); 490 if (npools == 0) { 491 /* 492 * NFS is shut down. The admin can start it by 493 * writing to the threads file but NOT the pool_threads 494 * file, sorry. Report zero threads. 495 */ 496 mutex_unlock(&nfsd_mutex); 497 strcpy(buf, "0\n"); 498 return strlen(buf); 499 } 500 501 nthreads = kzalloc_objs(int, npools); 502 rv = -ENOMEM; 503 if (nthreads == NULL) 504 goto out_free; 505 506 if (size > 0) { 507 for (i = 0; i < npools; i++) { 508 rv = get_int(&mesg, &nthreads[i]); 509 if (rv == -ENOENT) 510 break; /* fewer numbers than pools */ 511 if (rv) 512 goto out_free; /* syntax error */ 513 rv = -EINVAL; 514 if (nthreads[i] < 0) 515 goto out_free; 516 trace_nfsd_ctl_pool_threads(net, i, nthreads[i]); 517 } 518 519 /* 520 * There must always be a thread in pool 0; the admin 521 * can't shut down NFS completely using pool_threads. 522 */ 523 if (nthreads[0] == 0) 524 nthreads[0] = 1; 525 526 rv = nfsd_set_nrthreads(i, nthreads, net); 527 if (rv) 528 goto out_free; 529 } 530 531 rv = nfsd_get_nrthreads(npools, nthreads, net); 532 if (rv) 533 goto out_free; 534 535 mesg = buf; 536 size = SIMPLE_TRANSACTION_LIMIT; 537 for (i = 0; i < npools; i++) { 538 len = snprintf(mesg, size, "%d%c", nthreads[i], 539 (i == npools - 1 ? '\n' : ' ')); 540 if (len >= size) { 541 rv = -ENAMETOOLONG; 542 goto out_free; 543 } 544 size -= len; 545 mesg += len; 546 } 547 rv = mesg - buf; 548 out_free: 549 kfree(nthreads); 550 mutex_unlock(&nfsd_mutex); 551 return rv; 552 } 553 554 static ssize_t 555 nfsd_print_version_support(struct nfsd_net *nn, char *buf, int remaining, 556 const char *sep, unsigned vers, int minor) 557 { 558 const char *format = minor < 0 ? "%s%c%u" : "%s%c%u.%u"; 559 bool supported = !!nfsd_vers(nn, vers, NFSD_TEST); 560 561 if (vers == 4 && minor >= 0 && 562 !nfsd_minorversion(nn, minor, NFSD_TEST)) 563 supported = false; 564 if (minor == 0 && supported) 565 /* 566 * special case for backward compatability. 567 * +4.0 is never reported, it is implied by 568 * +4, unless -4.0 is present. 569 */ 570 return 0; 571 return snprintf(buf, remaining, format, sep, 572 supported ? '+' : '-', vers, minor); 573 } 574 575 static ssize_t __write_versions(struct file *file, char *buf, size_t size) 576 { 577 char *mesg = buf; 578 char *vers, *minorp, sign; 579 int len, num, remaining; 580 ssize_t tlen = 0; 581 char *sep; 582 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id); 583 584 if (size > 0) { 585 if (nn->nfsd_serv) 586 /* Cannot change versions without updating 587 * nn->nfsd_serv->sv_xdrsize, and reallocing 588 * rq_argp and rq_resp 589 */ 590 return -EBUSY; 591 if (buf[size-1] != '\n') 592 return -EINVAL; 593 buf[size-1] = 0; 594 trace_nfsd_ctl_version(netns(file), buf); 595 596 vers = mesg; 597 len = qword_get(&mesg, vers, size); 598 if (len <= 0) return -EINVAL; 599 do { 600 enum vers_op cmd; 601 unsigned minor; 602 sign = *vers; 603 if (sign == '+' || sign == '-') 604 num = simple_strtol((vers+1), &minorp, 0); 605 else 606 num = simple_strtol(vers, &minorp, 0); 607 if (*minorp == '.') { 608 if (num != 4) 609 return -EINVAL; 610 if (kstrtouint(minorp+1, 0, &minor) < 0) 611 return -EINVAL; 612 } 613 614 cmd = sign == '-' ? NFSD_CLEAR : NFSD_SET; 615 switch(num) { 616 #ifdef CONFIG_NFSD_V2 617 case 2: 618 #endif 619 case 3: 620 nfsd_vers(nn, num, cmd); 621 break; 622 case 4: 623 if (*minorp == '.') { 624 if (nfsd_minorversion(nn, minor, cmd) < 0) 625 return -EINVAL; 626 } else if ((cmd == NFSD_SET) != nfsd_vers(nn, num, NFSD_TEST)) { 627 /* 628 * Either we have +4 and no minors are enabled, 629 * or we have -4 and at least one minor is enabled. 630 * In either case, propagate 'cmd' to all minors. 631 */ 632 minor = 0; 633 while (nfsd_minorversion(nn, minor, cmd) >= 0) 634 minor++; 635 } 636 break; 637 default: 638 /* Ignore requests to disable non-existent versions */ 639 if (cmd == NFSD_SET) 640 return -EINVAL; 641 } 642 vers += len + 1; 643 } while ((len = qword_get(&mesg, vers, size)) > 0); 644 /* If all get turned off, turn them back on, as 645 * having no versions is BAD 646 */ 647 nfsd_reset_versions(nn); 648 } 649 650 /* Now write current state into reply buffer */ 651 sep = ""; 652 remaining = SIMPLE_TRANSACTION_LIMIT; 653 for (num=2 ; num <= 4 ; num++) { 654 int minor; 655 if (!nfsd_vers(nn, num, NFSD_AVAIL)) 656 continue; 657 658 minor = -1; 659 do { 660 len = nfsd_print_version_support(nn, buf, remaining, 661 sep, num, minor); 662 if (len >= remaining) 663 goto out; 664 remaining -= len; 665 buf += len; 666 tlen += len; 667 minor++; 668 if (len) 669 sep = " "; 670 } while (num == 4 && minor <= NFSD_SUPPORTED_MINOR_VERSION); 671 } 672 out: 673 len = snprintf(buf, remaining, "\n"); 674 if (len >= remaining) 675 return -EINVAL; 676 return tlen + len; 677 } 678 679 /* 680 * write_versions - Set or report the available NFS protocol versions 681 * 682 * Input: 683 * buf: ignored 684 * size: zero 685 * Output: 686 * On success: passed-in buffer filled with '\n'-terminated C 687 * string containing positive or negative integer 688 * values representing the current status of each 689 * protocol version; 690 * return code is the size in bytes of the string 691 * On error: return code is zero or a negative errno value 692 * 693 * OR 694 * 695 * Input: 696 * buf: C string containing whitespace- 697 * separated positive or negative 698 * integer values representing NFS 699 * protocol versions to enable ("+n") 700 * or disable ("-n") 701 * size: non-zero length of C string in @buf 702 * Output: 703 * On success: status of zero or more protocol versions has 704 * been updated; passed-in buffer filled with 705 * '\n'-terminated C string containing positive 706 * or negative integer values representing the 707 * current status of each protocol version; 708 * return code is the size in bytes of the string 709 * On error: return code is zero or a negative errno value 710 */ 711 static ssize_t write_versions(struct file *file, char *buf, size_t size) 712 { 713 ssize_t rv; 714 715 mutex_lock(&nfsd_mutex); 716 rv = __write_versions(file, buf, size); 717 mutex_unlock(&nfsd_mutex); 718 return rv; 719 } 720 721 /* 722 * Zero-length write. Return a list of NFSD's current listener 723 * transports. 724 */ 725 static ssize_t __write_ports_names(char *buf, struct net *net) 726 { 727 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 728 729 if (nn->nfsd_serv == NULL) 730 return 0; 731 return svc_xprt_names(nn->nfsd_serv, buf, SIMPLE_TRANSACTION_LIMIT); 732 } 733 734 /* 735 * A single 'fd' number was written, in which case it must be for 736 * a socket of a supported family/protocol, and we use it as an 737 * nfsd listener. 738 */ 739 static ssize_t __write_ports_addfd(char *buf, struct net *net, const struct cred *cred) 740 { 741 char *mesg = buf; 742 int fd, err; 743 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 744 struct svc_serv *serv; 745 746 err = get_int(&mesg, &fd); 747 if (err != 0 || fd < 0) 748 return -EINVAL; 749 trace_nfsd_ctl_ports_addfd(net, fd); 750 751 /* 752 * svc_register() is a no-op once userland owns rpcbind, and this 753 * interface has no way to hand the new listener to that owner. 754 */ 755 if (nn->nfsd_serv && nn->nfsd_serv->sv_no_rpcbind) 756 return -EBUSY; 757 758 err = nfsd_create_serv(net, false); 759 if (err != 0) 760 return err; 761 762 serv = nn->nfsd_serv; 763 err = svc_addsock(serv, net, fd, buf, SIMPLE_TRANSACTION_LIMIT, cred); 764 765 if (!serv->sv_nrthreads && list_empty(&nn->nfsd_serv->sv_permsocks)) 766 nfsd_destroy_serv(net); 767 768 return err; 769 } 770 771 /* 772 * A transport listener is added by writing its transport name and 773 * a port number. 774 */ 775 static ssize_t __write_ports_addxprt(char *buf, struct net *net, const struct cred *cred) 776 { 777 char transport[16]; 778 struct svc_xprt *xprt; 779 int port, err; 780 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 781 struct svc_serv *serv; 782 783 if (sscanf(buf, "%15s %5u", transport, &port) != 2) 784 return -EINVAL; 785 786 if (port < 1 || port > USHRT_MAX) 787 return -EINVAL; 788 trace_nfsd_ctl_ports_addxprt(net, transport, port); 789 790 err = nfsd_create_serv(net, false); 791 if (err != 0) 792 return err; 793 794 serv = nn->nfsd_serv; 795 err = svc_xprt_create(serv, transport, net, 796 PF_INET, port, SVC_SOCK_ANONYMOUS, cred); 797 if (err < 0) 798 goto out_err; 799 800 err = svc_xprt_create(serv, transport, net, 801 PF_INET6, port, SVC_SOCK_ANONYMOUS, cred); 802 if (err < 0 && err != -EAFNOSUPPORT) 803 goto out_close; 804 805 return 0; 806 out_close: 807 xprt = svc_find_xprt(serv, transport, net, PF_INET, port); 808 if (xprt != NULL) { 809 svc_xprt_close(xprt); 810 svc_xprt_put(xprt); 811 } 812 out_err: 813 if (!serv->sv_nrthreads && list_empty(&nn->nfsd_serv->sv_permsocks)) 814 nfsd_destroy_serv(net); 815 816 return err; 817 } 818 819 static ssize_t __write_ports(struct file *file, char *buf, size_t size, 820 struct net *net) 821 { 822 if (size == 0) 823 return __write_ports_names(buf, net); 824 825 if (isdigit(buf[0])) 826 return __write_ports_addfd(buf, net, file->f_cred); 827 828 if (isalpha(buf[0])) 829 return __write_ports_addxprt(buf, net, file->f_cred); 830 831 return -EINVAL; 832 } 833 834 /* 835 * write_ports - Pass a socket file descriptor or transport name to listen on 836 * 837 * Input: 838 * buf: ignored 839 * size: zero 840 * Output: 841 * On success: passed-in buffer filled with a '\n'-terminated C 842 * string containing a whitespace-separated list of 843 * named NFSD listeners; 844 * return code is the size in bytes of the string 845 * On error: return code is zero or a negative errno value 846 * 847 * OR 848 * 849 * Input: 850 * buf: C string containing an unsigned 851 * integer value representing a bound 852 * but unconnected socket that is to be 853 * used as an NFSD listener; listen(3) 854 * must be called for a SOCK_STREAM 855 * socket, otherwise it is ignored 856 * size: non-zero length of C string in @buf 857 * Output: 858 * On success: NFS service is started; 859 * passed-in buffer filled with a '\n'-terminated C 860 * string containing a unique alphanumeric name of 861 * the listener; 862 * return code is the size in bytes of the string 863 * On error: return code is a negative errno value 864 * 865 * OR 866 * 867 * Input: 868 * buf: C string containing a transport 869 * name and an unsigned integer value 870 * representing the port to listen on, 871 * separated by whitespace 872 * size: non-zero length of C string in @buf 873 * Output: 874 * On success: returns zero; NFS service is started 875 * On error: return code is a negative errno value 876 */ 877 static ssize_t write_ports(struct file *file, char *buf, size_t size) 878 { 879 ssize_t rv; 880 881 mutex_lock(&nfsd_mutex); 882 rv = __write_ports(file, buf, size, netns(file)); 883 mutex_unlock(&nfsd_mutex); 884 return rv; 885 } 886 887 888 int nfsd_max_blksize; 889 890 /* 891 * write_maxblksize - Set or report the current NFS blksize 892 * 893 * Input: 894 * buf: ignored 895 * size: zero 896 * 897 * OR 898 * 899 * Input: 900 * buf: C string containing an unsigned 901 * integer value representing the new 902 * NFS blksize 903 * size: non-zero length of C string in @buf 904 * Output: 905 * On success: passed-in buffer filled with '\n'-terminated C string 906 * containing numeric value of the current NFS blksize 907 * setting; 908 * return code is the size in bytes of the string 909 * On error: return code is zero or a negative errno value 910 */ 911 static ssize_t write_maxblksize(struct file *file, char *buf, size_t size) 912 { 913 char *mesg = buf; 914 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id); 915 916 if (size > 0) { 917 int bsize; 918 int rv = get_int(&mesg, &bsize); 919 if (rv) 920 return rv; 921 trace_nfsd_ctl_maxblksize(netns(file), bsize); 922 923 /* force bsize into allowed range and 924 * required alignment. 925 */ 926 bsize = max_t(int, bsize, 1024); 927 bsize = min_t(int, bsize, NFSSVC_MAXBLKSIZE); 928 bsize &= ~(1024-1); 929 mutex_lock(&nfsd_mutex); 930 if (nn->nfsd_serv) { 931 mutex_unlock(&nfsd_mutex); 932 return -EBUSY; 933 } 934 nfsd_max_blksize = bsize; 935 mutex_unlock(&nfsd_mutex); 936 } 937 938 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", 939 nfsd_max_blksize); 940 } 941 942 #ifdef CONFIG_NFSD_V4 943 static ssize_t __nfsd4_write_time(struct file *file, char *buf, size_t size, 944 time64_t *time, struct nfsd_net *nn) 945 { 946 struct dentry *dentry = file_dentry(file); 947 char *mesg = buf; 948 int rv, i; 949 950 if (size > 0) { 951 if (nn->nfsd_serv) 952 return -EBUSY; 953 rv = get_int(&mesg, &i); 954 if (rv) 955 return rv; 956 trace_nfsd_ctl_time(netns(file), dentry->d_name.name, 957 dentry->d_name.len, i); 958 959 /* 960 * Some sanity checking. We don't have a reason for 961 * these particular numbers, but problems with the 962 * extremes are: 963 * - Too short: the briefest network outage may 964 * cause clients to lose all their locks. Also, 965 * the frequent polling may be wasteful. 966 * - Too long: do you really want reboot recovery 967 * to take more than an hour? Or to make other 968 * clients wait an hour before being able to 969 * revoke a dead client's locks? 970 */ 971 if (i < 10 || i > 3600) 972 return -EINVAL; 973 *time = i; 974 } 975 976 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%lld\n", *time); 977 } 978 979 static ssize_t nfsd4_write_time(struct file *file, char *buf, size_t size, 980 time64_t *time, struct nfsd_net *nn) 981 { 982 ssize_t rv; 983 984 mutex_lock(&nfsd_mutex); 985 rv = __nfsd4_write_time(file, buf, size, time, nn); 986 mutex_unlock(&nfsd_mutex); 987 return rv; 988 } 989 990 /* 991 * write_leasetime - Set or report the current NFSv4 lease time 992 * 993 * Input: 994 * buf: ignored 995 * size: zero 996 * 997 * OR 998 * 999 * Input: 1000 * buf: C string containing an unsigned 1001 * integer value representing the new 1002 * NFSv4 lease expiry time 1003 * size: non-zero length of C string in @buf 1004 * Output: 1005 * On success: passed-in buffer filled with '\n'-terminated C 1006 * string containing unsigned integer value of the 1007 * current lease expiry time; 1008 * return code is the size in bytes of the string 1009 * On error: return code is zero or a negative errno value 1010 */ 1011 static ssize_t write_leasetime(struct file *file, char *buf, size_t size) 1012 { 1013 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id); 1014 return nfsd4_write_time(file, buf, size, &nn->nfsd4_lease, nn); 1015 } 1016 1017 /* 1018 * write_gracetime - Set or report current NFSv4 grace period time 1019 * 1020 * As above, but sets the time of the NFSv4 grace period. 1021 * 1022 * Note this should never be set to less than the *previous* 1023 * lease-period time, but we don't try to enforce this. (In the common 1024 * case (a new boot), we don't know what the previous lease time was 1025 * anyway.) 1026 */ 1027 static ssize_t write_gracetime(struct file *file, char *buf, size_t size) 1028 { 1029 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id); 1030 return nfsd4_write_time(file, buf, size, &nn->nfsd4_grace, nn); 1031 } 1032 1033 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING 1034 static ssize_t __write_recoverydir(struct file *file, char *buf, size_t size, 1035 struct nfsd_net *nn) 1036 { 1037 char *mesg = buf; 1038 char *recdir; 1039 int len, status; 1040 1041 if (size > 0) { 1042 if (nn->nfsd_serv) 1043 return -EBUSY; 1044 if (size > PATH_MAX || buf[size-1] != '\n') 1045 return -EINVAL; 1046 buf[size-1] = 0; 1047 1048 recdir = mesg; 1049 len = qword_get(&mesg, recdir, size); 1050 if (len <= 0) 1051 return -EINVAL; 1052 trace_nfsd_ctl_recoverydir(netns(file), recdir); 1053 1054 status = nfs4_reset_recoverydir(recdir); 1055 if (status) 1056 return status; 1057 } 1058 1059 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%s\n", 1060 nfs4_recoverydir()); 1061 } 1062 1063 /* 1064 * write_recoverydir - Set or report the pathname of the recovery directory 1065 * 1066 * Input: 1067 * buf: ignored 1068 * size: zero 1069 * 1070 * OR 1071 * 1072 * Input: 1073 * buf: C string containing the pathname 1074 * of the directory on a local file 1075 * system containing permanent NFSv4 1076 * recovery data 1077 * size: non-zero length of C string in @buf 1078 * Output: 1079 * On success: passed-in buffer filled with '\n'-terminated C string 1080 * containing the current recovery pathname setting; 1081 * return code is the size in bytes of the string 1082 * On error: return code is zero or a negative errno value 1083 */ 1084 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size) 1085 { 1086 ssize_t rv; 1087 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id); 1088 1089 mutex_lock(&nfsd_mutex); 1090 rv = __write_recoverydir(file, buf, size, nn); 1091 mutex_unlock(&nfsd_mutex); 1092 return rv; 1093 } 1094 #endif 1095 1096 /* 1097 * write_v4_end_grace - release grace period for nfsd's v4.x lock manager 1098 * 1099 * Input: 1100 * buf: ignored 1101 * size: zero 1102 * OR 1103 * 1104 * Input: 1105 * buf: any value 1106 * size: non-zero length of C string in @buf 1107 * Output: 1108 * passed-in buffer filled with "Y" or "N" with a newline 1109 * and NULL-terminated C string. This indicates whether 1110 * the grace period has ended in the current net 1111 * namespace. Return code is the size in bytes of the 1112 * string. Writing a string that starts with 'Y', 'y', or 1113 * '1' to the file will end the grace period for nfsd's v4 1114 * lock manager. 1115 */ 1116 static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size) 1117 { 1118 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id); 1119 1120 if (size > 0) { 1121 switch(buf[0]) { 1122 case 'Y': 1123 case 'y': 1124 case '1': 1125 if (!nfsd4_force_end_grace(nn)) 1126 return -EBUSY; 1127 trace_nfsd_end_grace(netns(file)); 1128 break; 1129 default: 1130 return -EINVAL; 1131 } 1132 } 1133 1134 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%c\n", 1135 test_bit(NFSD_NET_GRACE_ENDED, &nn->flags) ? 'Y' : 'N'); 1136 } 1137 1138 #endif 1139 1140 /*----------------------------------------------------------------------------*/ 1141 /* 1142 * populating the filesystem. 1143 */ 1144 1145 static struct inode *nfsd_get_inode(struct super_block *sb, umode_t mode) 1146 { 1147 struct inode *inode = new_inode(sb); 1148 if (inode) { 1149 /* Following advice from simple_fill_super documentation: */ 1150 inode->i_ino = iunique(sb, NFSD_MaxReserved); 1151 inode->i_mode = mode; 1152 simple_inode_init_ts(inode); 1153 } 1154 return inode; 1155 } 1156 1157 static struct dentry *nfsd_mkdir(struct dentry *parent, struct nfsdfs_client *ncl, char *name) 1158 { 1159 struct inode *dir = parent->d_inode; 1160 struct dentry *dentry; 1161 struct inode *inode; 1162 1163 inode = nfsd_get_inode(parent->d_sb, S_IFDIR | 0600); 1164 if (!inode) 1165 return ERR_PTR(-ENOMEM); 1166 1167 dentry = simple_start_creating(parent, name); 1168 if (IS_ERR(dentry)) { 1169 iput(inode); 1170 return dentry; 1171 } 1172 inode->i_fop = &simple_dir_operations; 1173 inode->i_op = &simple_dir_inode_operations; 1174 inc_nlink(inode); 1175 if (ncl) { 1176 inode->i_private = ncl; 1177 kref_get(&ncl->cl_ref); 1178 } 1179 d_make_persistent(dentry, inode); 1180 inc_nlink(dir); 1181 fsnotify_mkdir(dir, dentry); 1182 simple_done_creating(dentry); 1183 return dentry; // borrowed 1184 } 1185 1186 #if IS_ENABLED(CONFIG_SUNRPC_GSS) 1187 /* 1188 * @content is assumed to be a NUL-terminated string that lives 1189 * longer than the symlink itself. 1190 */ 1191 static void _nfsd_symlink(struct dentry *parent, const char *name, 1192 const char *content) 1193 { 1194 struct inode *dir = parent->d_inode; 1195 struct inode *inode; 1196 struct dentry *dentry; 1197 1198 inode = nfsd_get_inode(dir->i_sb, S_IFLNK | 0777); 1199 if (!inode) 1200 return; 1201 1202 dentry = simple_start_creating(parent, name); 1203 if (IS_ERR(dentry)) { 1204 iput(inode); 1205 return; 1206 } 1207 1208 inode->i_op = &simple_symlink_inode_operations; 1209 inode->i_link = (char *)content; 1210 inode->i_size = strlen(content); 1211 1212 d_make_persistent(dentry, inode); 1213 fsnotify_create(dir, dentry); 1214 simple_done_creating(dentry); 1215 } 1216 #else 1217 static inline void _nfsd_symlink(struct dentry *parent, const char *name, 1218 const char *content) 1219 { 1220 } 1221 1222 #endif 1223 1224 static void clear_ncl(struct dentry *dentry) 1225 { 1226 struct inode *inode = d_inode(dentry); 1227 struct nfsdfs_client *ncl = inode->i_private; 1228 1229 spin_lock(&inode->i_lock); 1230 inode->i_private = NULL; 1231 spin_unlock(&inode->i_lock); 1232 kref_put(&ncl->cl_ref, ncl->cl_release); 1233 } 1234 1235 struct nfsdfs_client *get_nfsdfs_client(struct inode *inode) 1236 { 1237 struct nfsdfs_client *nc; 1238 1239 spin_lock(&inode->i_lock); 1240 nc = inode->i_private; 1241 if (nc) 1242 kref_get(&nc->cl_ref); 1243 spin_unlock(&inode->i_lock); 1244 return nc; 1245 } 1246 1247 /* XXX: cut'n'paste from simple_fill_super; figure out if we could share 1248 * code instead. */ 1249 static int nfsdfs_create_files(struct dentry *root, 1250 const struct tree_descr *files, 1251 struct nfsdfs_client *ncl, 1252 struct dentry **fdentries) 1253 { 1254 struct inode *dir = d_inode(root); 1255 struct dentry *dentry; 1256 1257 for (int i = 0; files->name && files->name[0]; i++, files++) { 1258 struct inode *inode = nfsd_get_inode(root->d_sb, 1259 S_IFREG | files->mode); 1260 if (!inode) 1261 return -ENOMEM; 1262 dentry = simple_start_creating(root, files->name); 1263 if (IS_ERR(dentry)) { 1264 iput(inode); 1265 return PTR_ERR(dentry); 1266 } 1267 kref_get(&ncl->cl_ref); 1268 inode->i_fop = files->ops; 1269 inode->i_private = ncl; 1270 d_make_persistent(dentry, inode); 1271 fsnotify_create(dir, dentry); 1272 if (fdentries) 1273 fdentries[i] = dentry; // borrowed 1274 simple_done_creating(dentry); 1275 } 1276 return 0; 1277 } 1278 1279 /* on success, returns positive number unique to that client. */ 1280 struct dentry *nfsd_client_mkdir(struct nfsd_net *nn, 1281 struct nfsdfs_client *ncl, u32 id, 1282 const struct tree_descr *files, 1283 struct dentry **fdentries) 1284 { 1285 struct dentry *dentry; 1286 char name[11]; 1287 int ret; 1288 1289 sprintf(name, "%u", id); 1290 1291 dentry = nfsd_mkdir(nn->nfsd_client_dir, ncl, name); 1292 if (IS_ERR(dentry)) /* XXX: tossing errors? */ 1293 return NULL; 1294 ret = nfsdfs_create_files(dentry, files, ncl, fdentries); 1295 if (ret) { 1296 nfsd_client_rmdir(dentry); 1297 return NULL; 1298 } 1299 return dentry; 1300 } 1301 1302 /* Taken from __rpc_rmdir: */ 1303 void nfsd_client_rmdir(struct dentry *dentry) 1304 { 1305 simple_recursive_removal(dentry, clear_ncl); 1306 } 1307 1308 static int nfsd_fill_super(struct super_block *sb, struct fs_context *fc) 1309 { 1310 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, 1311 nfsd_net_id); 1312 struct dentry *dentry; 1313 int ret; 1314 1315 static const struct tree_descr nfsd_files[] = { 1316 [NFSD_List] = {"exports", &exports_nfsd_operations, S_IRUGO}, 1317 /* Per-export io stats use same ops as exports file */ 1318 [NFSD_Export_Stats] = {"export_stats", &exports_nfsd_operations, S_IRUGO}, 1319 [NFSD_Export_features] = {"export_features", 1320 &export_features_fops, S_IRUGO}, 1321 [NFSD_FO_UnlockIP] = {"unlock_ip", 1322 &transaction_ops, S_IWUSR|S_IRUSR}, 1323 [NFSD_FO_UnlockFS] = {"unlock_filesystem", 1324 &transaction_ops, S_IWUSR|S_IRUSR}, 1325 [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR}, 1326 [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR}, 1327 [NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR}, 1328 [NFSD_Pool_Stats] = {"pool_stats", &pool_stats_operations, S_IRUGO}, 1329 [NFSD_Reply_Cache_Stats] = {"reply_cache_stats", 1330 &nfsd_reply_cache_stats_fops, S_IRUGO}, 1331 [NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR}, 1332 [NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO}, 1333 [NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO}, 1334 [NFSD_Filecache] = {"filecache", &nfsd_file_cache_stats_fops, S_IRUGO}, 1335 #ifdef CONFIG_NFSD_V4 1336 [NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR}, 1337 [NFSD_Gracetime] = {"nfsv4gracetime", &transaction_ops, S_IWUSR|S_IRUSR}, 1338 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING 1339 [NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR}, 1340 #endif 1341 [NFSD_V4EndGrace] = {"v4_end_grace", &transaction_ops, S_IWUSR|S_IRUGO}, 1342 #endif 1343 /* last one */ {""} 1344 }; 1345 1346 ret = simple_fill_super(sb, 0x6e667364, nfsd_files); 1347 if (ret) 1348 return ret; 1349 _nfsd_symlink(sb->s_root, "supported_krb5_enctypes", 1350 "/proc/net/rpc/gss_krb5_enctypes"); 1351 dentry = nfsd_mkdir(sb->s_root, NULL, "clients"); 1352 if (IS_ERR(dentry)) 1353 return PTR_ERR(dentry); 1354 nn->nfsd_client_dir = dentry; 1355 return 0; 1356 } 1357 1358 static int nfsd_fs_get_tree(struct fs_context *fc) 1359 { 1360 return get_tree_keyed(fc, nfsd_fill_super, get_net(fc->net_ns)); 1361 } 1362 1363 static void nfsd_fs_free_fc(struct fs_context *fc) 1364 { 1365 if (fc->s_fs_info) 1366 put_net(fc->s_fs_info); 1367 } 1368 1369 static const struct fs_context_operations nfsd_fs_context_ops = { 1370 .free = nfsd_fs_free_fc, 1371 .get_tree = nfsd_fs_get_tree, 1372 }; 1373 1374 static int nfsd_init_fs_context(struct fs_context *fc) 1375 { 1376 put_user_ns(fc->user_ns); 1377 fc->user_ns = get_user_ns(fc->net_ns->user_ns); 1378 fc->ops = &nfsd_fs_context_ops; 1379 return 0; 1380 } 1381 1382 static void nfsd_umount(struct super_block *sb) 1383 { 1384 struct net *net = sb->s_fs_info; 1385 1386 nfsd_shutdown_threads(net); 1387 1388 kill_anon_super(sb); 1389 put_net(net); 1390 } 1391 1392 static struct file_system_type nfsd_fs_type = { 1393 .owner = THIS_MODULE, 1394 .name = "nfsd", 1395 .init_fs_context = nfsd_init_fs_context, 1396 .kill_sb = nfsd_umount, 1397 }; 1398 MODULE_ALIAS_FS("nfsd"); 1399 1400 #ifdef CONFIG_PROC_FS 1401 1402 static int exports_proc_open(struct inode *inode, struct file *file) 1403 { 1404 return exports_net_open(current->nsproxy->net_ns, file); 1405 } 1406 1407 static const struct proc_ops exports_proc_ops = { 1408 .proc_open = exports_proc_open, 1409 .proc_read = seq_read, 1410 .proc_lseek = seq_lseek, 1411 .proc_release = exports_release, 1412 }; 1413 1414 static int create_proc_exports_entry(void) 1415 { 1416 struct proc_dir_entry *entry; 1417 1418 entry = proc_mkdir("fs/nfs", NULL); 1419 if (!entry) 1420 return -ENOMEM; 1421 entry = proc_create("exports", 0, entry, &exports_proc_ops); 1422 if (!entry) { 1423 remove_proc_entry("fs/nfs", NULL); 1424 return -ENOMEM; 1425 } 1426 return 0; 1427 } 1428 #else /* CONFIG_PROC_FS */ 1429 static int create_proc_exports_entry(void) 1430 { 1431 return 0; 1432 } 1433 #endif 1434 1435 unsigned int nfsd_net_id; 1436 1437 struct nfsd_genl_rqstp { 1438 struct sockaddr_storage rq_daddr; 1439 struct sockaddr_storage rq_saddr; 1440 unsigned long rq_flags; 1441 ktime_t rq_stime; 1442 __be32 rq_xid; 1443 u32 rq_vers; 1444 u32 rq_prog; 1445 u32 rq_proc; 1446 1447 /* NFSv4 compound */ 1448 u32 rq_opcnt; 1449 u32 rq_opnum[16]; 1450 }; 1451 1452 static int nfsd_genl_rpc_status_compose_msg(struct sk_buff *skb, 1453 struct netlink_callback *cb, 1454 struct nfsd_genl_rqstp *genl_rqstp) 1455 { 1456 void *hdr; 1457 u32 i; 1458 1459 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, 1460 &nfsd_nl_family, 0, NFSD_CMD_RPC_STATUS_GET); 1461 if (!hdr) 1462 return -ENOBUFS; 1463 1464 if (nla_put_be32(skb, NFSD_A_RPC_STATUS_XID, genl_rqstp->rq_xid) || 1465 nla_put_u32(skb, NFSD_A_RPC_STATUS_FLAGS, genl_rqstp->rq_flags) || 1466 nla_put_u32(skb, NFSD_A_RPC_STATUS_PROG, genl_rqstp->rq_prog) || 1467 nla_put_u32(skb, NFSD_A_RPC_STATUS_PROC, genl_rqstp->rq_proc) || 1468 nla_put_u8(skb, NFSD_A_RPC_STATUS_VERSION, genl_rqstp->rq_vers) || 1469 nla_put_s64(skb, NFSD_A_RPC_STATUS_SERVICE_TIME, 1470 ktime_to_us(genl_rqstp->rq_stime), 1471 NFSD_A_RPC_STATUS_PAD)) 1472 goto out_cancel; 1473 1474 switch (genl_rqstp->rq_saddr.ss_family) { 1475 case AF_INET: { 1476 const struct sockaddr_in *s_in, *d_in; 1477 1478 s_in = (const struct sockaddr_in *)&genl_rqstp->rq_saddr; 1479 d_in = (const struct sockaddr_in *)&genl_rqstp->rq_daddr; 1480 if (nla_put_in_addr(skb, NFSD_A_RPC_STATUS_SADDR4, 1481 s_in->sin_addr.s_addr) || 1482 nla_put_in_addr(skb, NFSD_A_RPC_STATUS_DADDR4, 1483 d_in->sin_addr.s_addr) || 1484 nla_put_be16(skb, NFSD_A_RPC_STATUS_SPORT, 1485 s_in->sin_port) || 1486 nla_put_be16(skb, NFSD_A_RPC_STATUS_DPORT, 1487 d_in->sin_port)) 1488 goto out_cancel; 1489 break; 1490 } 1491 case AF_INET6: { 1492 const struct sockaddr_in6 *s_in, *d_in; 1493 1494 s_in = (const struct sockaddr_in6 *)&genl_rqstp->rq_saddr; 1495 d_in = (const struct sockaddr_in6 *)&genl_rqstp->rq_daddr; 1496 if (nla_put_in6_addr(skb, NFSD_A_RPC_STATUS_SADDR6, 1497 &s_in->sin6_addr) || 1498 nla_put_in6_addr(skb, NFSD_A_RPC_STATUS_DADDR6, 1499 &d_in->sin6_addr) || 1500 nla_put_be16(skb, NFSD_A_RPC_STATUS_SPORT, 1501 s_in->sin6_port) || 1502 nla_put_be16(skb, NFSD_A_RPC_STATUS_DPORT, 1503 d_in->sin6_port)) 1504 goto out_cancel; 1505 break; 1506 } 1507 } 1508 1509 for (i = 0; i < genl_rqstp->rq_opcnt; i++) 1510 if (nla_put_u32(skb, NFSD_A_RPC_STATUS_COMPOUND_OPS, 1511 genl_rqstp->rq_opnum[i])) 1512 goto out_cancel; 1513 1514 genlmsg_end(skb, hdr); 1515 return 0; 1516 1517 out_cancel: 1518 genlmsg_cancel(skb, hdr); 1519 return -ENOBUFS; 1520 } 1521 1522 /** 1523 * nfsd_nl_rpc_status_get_dumpit - Handle rpc_status_get dumpit 1524 * @skb: reply buffer 1525 * @cb: netlink metadata and command arguments 1526 * 1527 * Returns the size of the reply or a negative errno. 1528 */ 1529 int nfsd_nl_rpc_status_get_dumpit(struct sk_buff *skb, 1530 struct netlink_callback *cb) 1531 { 1532 int i, ret, rqstp_index = 0; 1533 struct nfsd_net *nn; 1534 1535 mutex_lock(&nfsd_mutex); 1536 1537 nn = net_generic(sock_net(skb->sk), nfsd_net_id); 1538 if (!nn->nfsd_serv) { 1539 ret = -ENODEV; 1540 goto out_unlock; 1541 } 1542 1543 rcu_read_lock(); 1544 1545 for (i = 0; i < svc_serv_nrpools(nn->nfsd_serv); i++) { 1546 struct svc_rqst *rqstp; 1547 long thread_skip = 0; 1548 1549 if (i < cb->args[0]) /* already consumed */ 1550 continue; 1551 1552 /* 1553 * The saved thread index only applies to the pool the dump 1554 * was resumed in. Subsequent pools must start from thread 0, 1555 * otherwise their first cb->args[1] threads are silently 1556 * skipped. 1557 */ 1558 if (i == cb->args[0]) 1559 thread_skip = cb->args[1]; 1560 1561 rqstp_index = 0; 1562 list_for_each_entry_rcu(rqstp, 1563 &nn->nfsd_serv->sv_pools[i].sp_all_threads, 1564 rq_all) { 1565 struct nfsd_genl_rqstp genl_rqstp = {}; 1566 unsigned int status_counter; 1567 1568 if (rqstp_index++ < thread_skip) /* already consumed */ 1569 continue; 1570 /* 1571 * Acquire rq_status_counter before parsing the rqst 1572 * fields. rq_status_counter is set to an odd value in 1573 * order to notify the consumers the rqstp fields are 1574 * meaningful. 1575 */ 1576 status_counter = 1577 smp_load_acquire(&rqstp->rq_status_counter); 1578 if (!(status_counter & 1)) 1579 continue; 1580 1581 genl_rqstp.rq_xid = rqstp->rq_xid; 1582 genl_rqstp.rq_flags = rqstp->rq_flags; 1583 genl_rqstp.rq_vers = rqstp->rq_vers; 1584 genl_rqstp.rq_prog = rqstp->rq_prog; 1585 genl_rqstp.rq_proc = rqstp->rq_proc; 1586 genl_rqstp.rq_stime = rqstp->rq_stime; 1587 genl_rqstp.rq_opcnt = 0; 1588 memcpy(&genl_rqstp.rq_daddr, svc_daddr(rqstp), 1589 sizeof(struct sockaddr_storage)); 1590 memcpy(&genl_rqstp.rq_saddr, svc_addr(rqstp), 1591 sizeof(struct sockaddr_storage)); 1592 1593 #ifdef CONFIG_NFSD_V4 1594 if (rqstp->rq_vers == NFS4_VERSION && 1595 rqstp->rq_proc == NFSPROC4_COMPOUND) { 1596 /* NFSv4 compound */ 1597 struct nfsd4_compoundargs *args; 1598 struct nfsd4_op *ops; 1599 u32 opcnt; 1600 int j; 1601 1602 args = rqstp->rq_argp; 1603 opcnt = READ_ONCE(args->opcnt); 1604 ops = READ_ONCE(args->ops); 1605 1606 /* 1607 * Finish the seqcount retry before 1608 * dereferencing ops. An unchanged counter means 1609 * opcnt and ops came from the same COMPOUND, 1610 * where opcnt cannot exceed what ops holds. 1611 */ 1612 smp_rmb(); 1613 if (READ_ONCE(rqstp->rq_status_counter) != 1614 status_counter) 1615 continue; 1616 1617 genl_rqstp.rq_opcnt = min_t(u32, opcnt, 1618 ARRAY_SIZE(genl_rqstp.rq_opnum)); 1619 for (j = 0; j < genl_rqstp.rq_opcnt; j++) 1620 genl_rqstp.rq_opnum[j] = ops[j].opnum; 1621 } 1622 #endif /* CONFIG_NFSD_V4 */ 1623 1624 /* 1625 * Read-side load-load fence: order the field reads 1626 * above before the counter re-read below, mirroring 1627 * the smp_rmb() in the standard seqcount retry. The 1628 * begin-side smp_load_acquire() above pairs with the 1629 * smp_store_release() in nfsd_dispatch(). 1630 */ 1631 smp_rmb(); 1632 if (READ_ONCE(rqstp->rq_status_counter) != status_counter) 1633 continue; 1634 1635 ret = nfsd_genl_rpc_status_compose_msg(skb, cb, 1636 &genl_rqstp); 1637 if (ret) { 1638 if (skb->len) { 1639 cb->args[0] = i; 1640 cb->args[1] = rqstp_index - 1; 1641 ret = skb->len; 1642 } 1643 goto out; 1644 } 1645 } 1646 } 1647 1648 cb->args[0] = i; 1649 cb->args[1] = rqstp_index; 1650 ret = skb->len; 1651 out: 1652 rcu_read_unlock(); 1653 out_unlock: 1654 mutex_unlock(&nfsd_mutex); 1655 1656 return ret; 1657 } 1658 1659 /** 1660 * nfsd_nl_fh_key_set - helper to copy fh_key from userspace 1661 * @attr: nlattr NFSD_A_SERVER_FH_KEY 1662 * @nn: nfsd_net 1663 * 1664 * Callers should hold nfsd_mutex, returns 0 on success or negative errno. 1665 * Callers must ensure the server is shut down (sv_nrthreads == 0), 1666 * userspace documentation asserts the key may only be set when the server 1667 * is not running. 1668 */ 1669 static int nfsd_nl_fh_key_set(const struct nlattr *attr, struct nfsd_net *nn) 1670 { 1671 siphash_key_t *fh_key = nn->fh_key; 1672 u64 k0, k1; 1673 bool changed; 1674 1675 k0 = get_unaligned_le64(nla_data(attr)); 1676 k1 = get_unaligned_le64(nla_data(attr) + 8); 1677 1678 if (!fh_key) { 1679 fh_key = kmalloc_obj(siphash_key_t); 1680 if (!fh_key) { 1681 trace_nfsd_ctl_fh_key_set(false, -ENOMEM); 1682 return -ENOMEM; 1683 } 1684 nn->fh_key = fh_key; 1685 changed = true; 1686 } else { 1687 changed = fh_key->key[0] != k0 || fh_key->key[1] != k1; 1688 } 1689 1690 fh_key->key[0] = k0; 1691 fh_key->key[1] = k1; 1692 trace_nfsd_ctl_fh_key_set(changed, 0); 1693 return 0; 1694 } 1695 1696 /** 1697 * nfsd_nl_threads_set_doit - set the number of running threads 1698 * @skb: reply buffer 1699 * @info: netlink metadata and command arguments 1700 * 1701 * Return 0 on success or a negative errno. 1702 */ 1703 int nfsd_nl_threads_set_doit(struct sk_buff *skb, struct genl_info *info) 1704 { 1705 int *nthreads, nrpools = 0, i, ret = -EOPNOTSUPP, rem; 1706 struct net *net = genl_info_net(info); 1707 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 1708 const struct nlattr *attr; 1709 const char *scope = NULL; 1710 1711 if (GENL_REQ_ATTR_CHECK(info, NFSD_A_SERVER_THREADS)) 1712 return -EINVAL; 1713 1714 /* count number of SERVER_THREADS values */ 1715 nlmsg_for_each_attr_type(attr, NFSD_A_SERVER_THREADS, info->nlhdr, 1716 GENL_HDRLEN, rem) 1717 nrpools++; 1718 1719 mutex_lock(&nfsd_mutex); 1720 1721 nthreads = kzalloc_objs(int, nrpools); 1722 if (!nthreads) { 1723 ret = -ENOMEM; 1724 goto out_unlock; 1725 } 1726 1727 i = 0; 1728 nlmsg_for_each_attr_type(attr, NFSD_A_SERVER_THREADS, info->nlhdr, 1729 GENL_HDRLEN, rem) { 1730 nthreads[i++] = nla_get_u32(attr); 1731 if (i >= nrpools) 1732 break; 1733 } 1734 1735 if (info->attrs[NFSD_A_SERVER_GRACETIME] || 1736 info->attrs[NFSD_A_SERVER_LEASETIME] || 1737 info->attrs[NFSD_A_SERVER_SCOPE] || 1738 info->attrs[NFSD_A_SERVER_FH_KEY]) { 1739 ret = -EBUSY; 1740 if (nn->nfsd_serv && nn->nfsd_serv->sv_nrthreads) 1741 goto out_unlock; 1742 1743 ret = -EINVAL; 1744 attr = info->attrs[NFSD_A_SERVER_GRACETIME]; 1745 if (attr) { 1746 u32 gracetime = nla_get_u32(attr); 1747 1748 if (gracetime < 10 || gracetime > 3600) 1749 goto out_unlock; 1750 1751 nn->nfsd4_grace = gracetime; 1752 } 1753 1754 attr = info->attrs[NFSD_A_SERVER_LEASETIME]; 1755 if (attr) { 1756 u32 leasetime = nla_get_u32(attr); 1757 1758 if (leasetime < 10 || leasetime > 3600) 1759 goto out_unlock; 1760 1761 nn->nfsd4_lease = leasetime; 1762 } 1763 1764 attr = info->attrs[NFSD_A_SERVER_SCOPE]; 1765 if (attr) 1766 scope = nla_data(attr); 1767 1768 attr = info->attrs[NFSD_A_SERVER_FH_KEY]; 1769 if (attr) { 1770 ret = nfsd_nl_fh_key_set(attr, nn); 1771 if (ret) 1772 goto out_unlock; 1773 } 1774 } 1775 1776 attr = info->attrs[NFSD_A_SERVER_MIN_THREADS]; 1777 if (attr) 1778 nn->min_threads = nla_get_u32(attr); 1779 1780 ret = nfsd_svc(nrpools, nthreads, net, current_cred(), scope); 1781 if (ret > 0) 1782 ret = 0; 1783 out_unlock: 1784 mutex_unlock(&nfsd_mutex); 1785 kfree(nthreads); 1786 return ret; 1787 } 1788 1789 /** 1790 * nfsd_nl_threads_get_doit - get the maximum number of running threads 1791 * @skb: reply buffer 1792 * @info: netlink metadata and command arguments 1793 * 1794 * Return 0 on success or a negative errno. 1795 */ 1796 int nfsd_nl_threads_get_doit(struct sk_buff *skb, struct genl_info *info) 1797 { 1798 struct net *net = genl_info_net(info); 1799 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 1800 void *hdr; 1801 int err; 1802 1803 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); 1804 if (!skb) 1805 return -ENOMEM; 1806 1807 hdr = genlmsg_iput(skb, info); 1808 if (!hdr) { 1809 err = -EMSGSIZE; 1810 goto err_free_msg; 1811 } 1812 1813 mutex_lock(&nfsd_mutex); 1814 1815 err = nla_put_u32(skb, NFSD_A_SERVER_GRACETIME, 1816 nn->nfsd4_grace) || 1817 nla_put_u32(skb, NFSD_A_SERVER_LEASETIME, 1818 nn->nfsd4_lease) || 1819 nla_put_u32(skb, NFSD_A_SERVER_MIN_THREADS, 1820 nn->min_threads) || 1821 nla_put_string(skb, NFSD_A_SERVER_SCOPE, 1822 nn->nfsd_name); 1823 if (err) 1824 goto err_unlock; 1825 1826 if (nn->nfsd_serv) { 1827 int i; 1828 1829 for (i = 0; i < nfsd_nrpools(net); ++i) { 1830 struct svc_pool *sp = &nn->nfsd_serv->sv_pools[i]; 1831 1832 err = nla_put_u32(skb, NFSD_A_SERVER_THREADS, 1833 sp->sp_nrthrmax); 1834 if (err) 1835 goto err_unlock; 1836 } 1837 } else { 1838 err = nla_put_u32(skb, NFSD_A_SERVER_THREADS, 0); 1839 if (err) 1840 goto err_unlock; 1841 } 1842 1843 mutex_unlock(&nfsd_mutex); 1844 1845 genlmsg_end(skb, hdr); 1846 1847 return genlmsg_reply(skb, info); 1848 1849 err_unlock: 1850 mutex_unlock(&nfsd_mutex); 1851 err_free_msg: 1852 nlmsg_free(skb); 1853 1854 return err; 1855 } 1856 1857 /** 1858 * nfsd_nl_version_set_doit - set the nfs enabled versions 1859 * @skb: reply buffer 1860 * @info: netlink metadata and command arguments 1861 * 1862 * Return 0 on success or a negative errno. 1863 */ 1864 int nfsd_nl_version_set_doit(struct sk_buff *skb, struct genl_info *info) 1865 { 1866 const struct nlattr *attr; 1867 struct nfsd_net *nn; 1868 int i, rem; 1869 1870 if (GENL_REQ_ATTR_CHECK(info, NFSD_A_SERVER_PROTO_VERSION)) 1871 return -EINVAL; 1872 1873 mutex_lock(&nfsd_mutex); 1874 1875 nn = net_generic(genl_info_net(info), nfsd_net_id); 1876 if (nn->nfsd_serv) { 1877 mutex_unlock(&nfsd_mutex); 1878 return -EBUSY; 1879 } 1880 1881 /* clear current supported versions. */ 1882 nfsd_vers(nn, 2, NFSD_CLEAR); 1883 nfsd_vers(nn, 3, NFSD_CLEAR); 1884 for (i = 0; i <= NFSD_SUPPORTED_MINOR_VERSION; i++) 1885 nfsd_minorversion(nn, i, NFSD_CLEAR); 1886 1887 nlmsg_for_each_attr_type(attr, NFSD_A_SERVER_PROTO_VERSION, info->nlhdr, 1888 GENL_HDRLEN, rem) { 1889 struct nlattr *tb[NFSD_A_VERSION_MAX + 1]; 1890 u32 major, minor = 0; 1891 bool enabled; 1892 1893 if (nla_parse_nested(tb, NFSD_A_VERSION_MAX, attr, 1894 nfsd_version_nl_policy, info->extack) < 0) 1895 continue; 1896 1897 if (!tb[NFSD_A_VERSION_MAJOR]) 1898 continue; 1899 1900 major = nla_get_u32(tb[NFSD_A_VERSION_MAJOR]); 1901 if (tb[NFSD_A_VERSION_MINOR]) 1902 minor = nla_get_u32(tb[NFSD_A_VERSION_MINOR]); 1903 1904 enabled = nla_get_flag(tb[NFSD_A_VERSION_ENABLED]); 1905 1906 switch (major) { 1907 case 4: 1908 nfsd_minorversion(nn, minor, enabled ? NFSD_SET : NFSD_CLEAR); 1909 break; 1910 case 3: 1911 case 2: 1912 if (!minor) 1913 nfsd_vers(nn, major, enabled ? NFSD_SET : NFSD_CLEAR); 1914 break; 1915 default: 1916 break; 1917 } 1918 } 1919 1920 mutex_unlock(&nfsd_mutex); 1921 1922 return 0; 1923 } 1924 1925 /** 1926 * nfsd_nl_version_get_doit - get the enabled status for all supported nfs versions 1927 * @skb: reply buffer 1928 * @info: netlink metadata and command arguments 1929 * 1930 * Return 0 on success or a negative errno. 1931 */ 1932 int nfsd_nl_version_get_doit(struct sk_buff *skb, struct genl_info *info) 1933 { 1934 struct nfsd_net *nn; 1935 int i, err; 1936 void *hdr; 1937 1938 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); 1939 if (!skb) 1940 return -ENOMEM; 1941 1942 hdr = genlmsg_iput(skb, info); 1943 if (!hdr) { 1944 err = -EMSGSIZE; 1945 goto err_free_msg; 1946 } 1947 1948 mutex_lock(&nfsd_mutex); 1949 nn = net_generic(genl_info_net(info), nfsd_net_id); 1950 1951 for (i = 2; i <= 4; i++) { 1952 int j; 1953 1954 for (j = 0; j <= NFSD_SUPPORTED_MINOR_VERSION; j++) { 1955 struct nlattr *attr; 1956 1957 /* Don't record any versions the kernel doesn't have 1958 * compiled in 1959 */ 1960 if (!nfsd_support_version(i)) 1961 continue; 1962 1963 /* NFSv{2,3} does not support minor numbers */ 1964 if (i < 4 && j) 1965 continue; 1966 1967 attr = nla_nest_start(skb, 1968 NFSD_A_SERVER_PROTO_VERSION); 1969 if (!attr) { 1970 err = -EINVAL; 1971 goto err_nfsd_unlock; 1972 } 1973 1974 if (nla_put_u32(skb, NFSD_A_VERSION_MAJOR, i) || 1975 nla_put_u32(skb, NFSD_A_VERSION_MINOR, j)) { 1976 err = -EINVAL; 1977 goto err_nfsd_unlock; 1978 } 1979 1980 /* Set the enabled flag if the version is enabled */ 1981 if (nfsd_vers(nn, i, NFSD_TEST) && 1982 (i < 4 || nfsd_minorversion(nn, j, NFSD_TEST)) && 1983 nla_put_flag(skb, NFSD_A_VERSION_ENABLED)) { 1984 err = -EINVAL; 1985 goto err_nfsd_unlock; 1986 } 1987 1988 nla_nest_end(skb, attr); 1989 } 1990 } 1991 1992 mutex_unlock(&nfsd_mutex); 1993 genlmsg_end(skb, hdr); 1994 1995 return genlmsg_reply(skb, info); 1996 1997 err_nfsd_unlock: 1998 mutex_unlock(&nfsd_mutex); 1999 err_free_msg: 2000 nlmsg_free(skb); 2001 2002 return err; 2003 } 2004 2005 /* 2006 * Transport classes NFSD knows how to instantiate. Vetting the name here 2007 * keeps a bogus string from reaching svc_xprt_create_from_sa(), where an 2008 * unknown name triggers a request_module("svc%s", name) upcall under 2009 * nfsd_mutex. 2010 */ 2011 static bool nfsd_nl_transport_supported(const char *name) 2012 { 2013 static const char * const supported[] = { "tcp", "udp", "rdma" }; 2014 int i; 2015 2016 for (i = 0; i < ARRAY_SIZE(supported); i++) 2017 if (!strcmp(name, supported[i])) 2018 return true; 2019 return false; 2020 } 2021 2022 /* Upper bound on the number of listeners a single request may carry. */ 2023 #define NFSD_NL_LISTENER_MAX 1024 2024 2025 /** 2026 * nfsd_nl_validate_listeners - sanity-check the listener list from userland 2027 * @info: netlink metadata and command arguments 2028 * 2029 * Walk every NFSD_A_SERVER_SOCK_ADDR attribute and confirm that the list is 2030 * not oversized and that each entry is well-formed. 2031 * 2032 * Return: the number of entries if every entry is valid, or a negative 2033 * errno otherwise. 2034 */ 2035 static int nfsd_nl_validate_listeners(struct genl_info *info) 2036 { 2037 const struct nlattr *attr; 2038 unsigned int count = 0; 2039 int rem; 2040 2041 nlmsg_for_each_attr_type(attr, NFSD_A_SERVER_SOCK_ADDR, info->nlhdr, 2042 GENL_HDRLEN, rem) { 2043 struct nlattr *tb[NFSD_A_SOCK_MAX + 1]; 2044 struct sockaddr *sa; 2045 int err; 2046 2047 if (++count > NFSD_NL_LISTENER_MAX) { 2048 NL_SET_ERR_MSG(info->extack, "too many listeners"); 2049 return -E2BIG; 2050 } 2051 2052 err = nla_parse_nested(tb, NFSD_A_SOCK_MAX, attr, 2053 nfsd_sock_nl_policy, info->extack); 2054 if (err < 0) 2055 return err; 2056 2057 if (!tb[NFSD_A_SOCK_ADDR] || !tb[NFSD_A_SOCK_TRANSPORT_NAME]) 2058 return -EINVAL; 2059 2060 if (!nfsd_nl_transport_supported(nla_data(tb[NFSD_A_SOCK_TRANSPORT_NAME]))) { 2061 NL_SET_ERR_MSG_ATTR(info->extack, 2062 tb[NFSD_A_SOCK_TRANSPORT_NAME], 2063 "unsupported transport name"); 2064 return -EPROTONOSUPPORT; 2065 } 2066 2067 sa = nla_data(tb[NFSD_A_SOCK_ADDR]); 2068 if (nla_len(tb[NFSD_A_SOCK_ADDR]) < sizeof(sa->sa_family)) 2069 return -EINVAL; 2070 2071 switch (sa->sa_family) { 2072 case AF_INET: 2073 if (nla_len(tb[NFSD_A_SOCK_ADDR]) < 2074 sizeof(struct sockaddr_in)) 2075 return -EINVAL; 2076 break; 2077 case AF_INET6: 2078 if (nla_len(tb[NFSD_A_SOCK_ADDR]) < 2079 sizeof(struct sockaddr_in6)) 2080 return -EINVAL; 2081 break; 2082 default: 2083 return -EAFNOSUPPORT; 2084 } 2085 } 2086 2087 return count; 2088 } 2089 2090 static size_t nfsd_nl_listener_set_msgsize(struct svc_serv *serv) 2091 { 2092 size_t size = GENL_HDRLEN + /* genlmsg_iput() */ 2093 nla_total_size(0); /* userspace-rpcbind */ 2094 struct svc_xprt *xprt; 2095 unsigned int p; 2096 2097 lockdep_assert_held(&nfsd_mutex); 2098 2099 for (p = 0; p < serv->sv_nprogs; p++) 2100 size += serv->sv_programs[p].pg_nvers * 2101 (nla_total_size(0) + /* rpcbind nest */ 2102 nla_total_size(sizeof(u32)) + /* program */ 2103 nla_total_size(sizeof(u32)) + /* version */ 2104 nla_total_size(sizeof(u32))); /* flags */ 2105 2106 spin_lock_bh(&serv->sv_lock); 2107 list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list) { 2108 if (!test_bit(XPT_RPCB_UNREG, &xprt->xpt_flags)) 2109 continue; 2110 size += nla_total_size(0) + /* addr nest */ 2111 nla_total_size(strlen(xprt->xpt_class->xcl_name) + 1) + 2112 nla_total_size(sizeof(struct sockaddr_storage)); 2113 } 2114 spin_unlock_bh(&serv->sv_lock); 2115 2116 return size; 2117 } 2118 2119 static struct sk_buff * 2120 nfsd_nl_listener_set_msg(struct genl_info *info, struct net *net, 2121 struct svc_serv *serv) 2122 { 2123 struct svc_xprt *xprt; 2124 struct sk_buff *skb; 2125 unsigned int p, i; 2126 void *hdr; 2127 int err; 2128 2129 lockdep_assert_held(&nfsd_mutex); 2130 2131 skb = genlmsg_new(nfsd_nl_listener_set_msgsize(serv), GFP_KERNEL); 2132 if (!skb) 2133 return ERR_PTR(-ENOMEM); 2134 2135 hdr = genlmsg_iput(skb, info); 2136 if (!hdr) { 2137 err = -EMSGSIZE; 2138 goto err_free_msg; 2139 } 2140 2141 if (nla_put_flag(skb, NFSD_A_SERVER_SOCK_USERSPACE_RPCBIND)) { 2142 err = -EMSGSIZE; 2143 goto err_free_msg; 2144 } 2145 2146 for (p = 0; p < serv->sv_nprogs; p++) { 2147 const struct svc_program *progp = &serv->sv_programs[p]; 2148 2149 for (i = 0; i < progp->pg_nvers; i++) { 2150 struct nlattr *attr; 2151 u32 flags = 0; 2152 2153 if (!nfsd_version_registerable(net, progp, i)) 2154 continue; 2155 2156 if (progp->pg_vers[i]->vs_need_cong_ctrl) 2157 flags |= NFSD_RPCBIND_FLAGS_NO_UDP; 2158 2159 attr = nla_nest_start(skb, NFSD_A_SERVER_SOCK_RPCBIND); 2160 if (!attr) { 2161 err = -EMSGSIZE; 2162 goto err_free_msg; 2163 } 2164 if (nla_put_u32(skb, NFSD_A_RPCBIND_PROGRAM, 2165 progp->pg_prog) || 2166 nla_put_u32(skb, NFSD_A_RPCBIND_VERSION, i) || 2167 (flags && nla_put_u32(skb, NFSD_A_RPCBIND_FLAGS, 2168 flags))) { 2169 err = -EMSGSIZE; 2170 goto err_free_msg; 2171 } 2172 nla_nest_end(skb, attr); 2173 } 2174 } 2175 2176 spin_lock_bh(&serv->sv_lock); 2177 list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list) { 2178 struct nlattr *attr; 2179 2180 if (!test_bit(XPT_RPCB_UNREG, &xprt->xpt_flags)) 2181 continue; 2182 2183 attr = nla_nest_start(skb, NFSD_A_SERVER_SOCK_ADDR); 2184 if (!attr) { 2185 err = -EMSGSIZE; 2186 goto err_serv_unlock; 2187 } 2188 2189 if (nla_put_string(skb, NFSD_A_SOCK_TRANSPORT_NAME, 2190 xprt->xpt_class->xcl_name) || 2191 nla_put(skb, NFSD_A_SOCK_ADDR, 2192 sizeof(struct sockaddr_storage), 2193 &xprt->xpt_local)) { 2194 err = -EMSGSIZE; 2195 goto err_serv_unlock; 2196 } 2197 2198 nla_nest_end(skb, attr); 2199 } 2200 spin_unlock_bh(&serv->sv_lock); 2201 2202 genlmsg_end(skb, hdr); 2203 return skb; 2204 2205 err_serv_unlock: 2206 spin_unlock_bh(&serv->sv_lock); 2207 err_free_msg: 2208 nlmsg_free(skb); 2209 return ERR_PTR(err); 2210 } 2211 2212 /** 2213 * nfsd_nl_listener_set_doit - set the nfs running sockets 2214 * @skb: reply buffer 2215 * @info: netlink metadata and command arguments 2216 * 2217 * Return 0 on success or a negative errno. 2218 */ 2219 int nfsd_nl_listener_set_doit(struct sk_buff *skb, struct genl_info *info) 2220 { 2221 struct net *net = genl_info_net(info); 2222 const struct nlattr *bad_attr = NULL; 2223 struct svc_xprt *xprt, *tmp; 2224 const char *bad_xprt = NULL; 2225 struct sk_buff *rskb = NULL; 2226 unsigned int rpcb_failures; 2227 const struct nlattr *attr; 2228 bool skipped_rpcb = false; 2229 bool userspace_rpcbind; 2230 bool bad_rpcb = false; 2231 struct svc_serv *serv; 2232 LIST_HEAD(permsocks); 2233 struct nfsd_net *nn; 2234 bool delete = false; 2235 int nlisteners; 2236 int err, rem; 2237 2238 /* 2239 * Validate the entire listener list before making any changes, so a 2240 * malformed request fails cleanly without creating a serv or touching 2241 * the existing listeners. 2242 */ 2243 nlisteners = nfsd_nl_validate_listeners(info); 2244 if (nlisteners < 0) 2245 return nlisteners; 2246 2247 userspace_rpcbind = nla_get_flag(info->attrs[NFSD_A_SERVER_SOCK_USERSPACE_RPCBIND]); 2248 2249 mutex_lock(&nfsd_mutex); 2250 2251 nn = net_generic(net, nfsd_net_id); 2252 2253 /* 2254 * An empty list destroys the serv, and nfsd_destroy_serv() drops 2255 * whatever svc_bind() took either way, so teardown is not an 2256 * ownership change. Only a request that leaves a listener standing 2257 * has to agree with the serv it found. 2258 */ 2259 if (nlisteners && nn->nfsd_serv && 2260 nn->nfsd_serv->sv_no_rpcbind != userspace_rpcbind) { 2261 NL_SET_ERR_MSG(info->extack, 2262 "cannot change rpcbind ownership while a server exists"); 2263 mutex_unlock(&nfsd_mutex); 2264 return -EBUSY; 2265 } 2266 2267 err = nfsd_create_serv(net, userspace_rpcbind); 2268 if (err) { 2269 mutex_unlock(&nfsd_mutex); 2270 return err; 2271 } 2272 2273 serv = nn->nfsd_serv; 2274 2275 spin_lock_bh(&serv->sv_lock); 2276 2277 /* Move all of the old listener sockets to a temp list */ 2278 list_splice_init(&serv->sv_permsocks, &permsocks); 2279 2280 /* 2281 * Walk the list of server_socks from userland and move any that match 2282 * back to sv_permsocks 2283 */ 2284 nlmsg_for_each_attr_type(attr, NFSD_A_SERVER_SOCK_ADDR, info->nlhdr, 2285 GENL_HDRLEN, rem) { 2286 struct nlattr *tb[NFSD_A_SOCK_MAX + 1]; 2287 const char *xcl_name; 2288 struct sockaddr *sa; 2289 2290 /* validated up front in nfsd_nl_validate_listeners() */ 2291 if (nla_parse_nested(tb, NFSD_A_SOCK_MAX, attr, 2292 nfsd_sock_nl_policy, info->extack) < 0) 2293 continue; 2294 2295 xcl_name = nla_data(tb[NFSD_A_SOCK_TRANSPORT_NAME]); 2296 sa = nla_data(tb[NFSD_A_SOCK_ADDR]); 2297 2298 /* Put back any matching sockets */ 2299 list_for_each_entry_safe(xprt, tmp, &permsocks, xpt_list) { 2300 /* This shouldn't be possible */ 2301 if (WARN_ON_ONCE(xprt->xpt_net != net)) { 2302 list_move(&xprt->xpt_list, &serv->sv_permsocks); 2303 continue; 2304 } 2305 2306 /* If everything matches, put it back */ 2307 if (!strcmp(xprt->xpt_class->xcl_name, xcl_name) && 2308 rpc_cmp_addr_port(sa, (struct sockaddr *)&xprt->xpt_local)) { 2309 list_move(&xprt->xpt_list, &serv->sv_permsocks); 2310 break; 2311 } 2312 } 2313 } 2314 2315 /* 2316 * If there are listener transports remaining on the permsocks list, 2317 * it means we were asked to remove a listener. 2318 */ 2319 if (!list_empty(&permsocks)) { 2320 list_splice_init(&permsocks, &serv->sv_permsocks); 2321 delete = true; 2322 } 2323 spin_unlock_bh(&serv->sv_lock); 2324 2325 /* Do not remove listeners while there are active threads. */ 2326 if (serv->sv_nrthreads) { 2327 err = -EBUSY; 2328 goto out_unlock_mtx; 2329 } 2330 2331 /* 2332 * Since we can't delete an arbitrary llist entry, destroy the 2333 * remaining listeners and recreate the list. 2334 */ 2335 if (delete) 2336 svc_xprt_destroy_all(serv, net, false); 2337 2338 rpcb_failures = svc_rpcb_failure_count(serv); 2339 2340 /* walk list of addrs again, open any that still don't exist */ 2341 nlmsg_for_each_attr_type(attr, NFSD_A_SERVER_SOCK_ADDR, info->nlhdr, 2342 GENL_HDRLEN, rem) { 2343 struct nlattr *tb[NFSD_A_SOCK_MAX + 1]; 2344 const char *xcl_name; 2345 struct sockaddr *sa; 2346 bool hit_rpcb; 2347 int flags, ret; 2348 2349 /* validated up front in nfsd_nl_validate_listeners() */ 2350 if (nla_parse_nested(tb, NFSD_A_SOCK_MAX, attr, 2351 nfsd_sock_nl_policy, info->extack) < 0) 2352 continue; 2353 2354 xcl_name = nla_data(tb[NFSD_A_SOCK_TRANSPORT_NAME]); 2355 sa = nla_data(tb[NFSD_A_SOCK_ADDR]); 2356 2357 xprt = svc_find_listener(serv, xcl_name, net, sa); 2358 if (xprt) { 2359 if (delete) 2360 WARN_ONCE(1, "Transport type=%s already exists\n", 2361 xcl_name); 2362 svc_xprt_put(xprt); 2363 continue; 2364 } 2365 2366 flags = (userspace_rpcbind || skipped_rpcb) ? SVC_SOCK_ANONYMOUS : 0; 2367 ret = svc_xprt_create_from_sa(serv, xcl_name, net, sa, flags, 2368 current_cred()); 2369 2370 hit_rpcb = false; 2371 if (!userspace_rpcbind && !skipped_rpcb && 2372 svc_rpcb_failure_count(serv) != rpcb_failures) { 2373 skipped_rpcb = true; 2374 hit_rpcb = true; 2375 if (ret < 0) 2376 ret = svc_xprt_create_from_sa(serv, xcl_name, 2377 net, sa, 2378 SVC_SOCK_ANONYMOUS, 2379 current_cred()); 2380 } 2381 2382 /* always save the latest error */ 2383 if (ret < 0) { 2384 bad_attr = attr; 2385 bad_xprt = xcl_name; 2386 bad_rpcb = hit_rpcb; 2387 err = ret; 2388 } 2389 } 2390 2391 /* 2392 * The ack carries the errno of the last entry that failed. Point at 2393 * that entry as well, since several entries can share a transport 2394 * name and the errno alone cannot tell them apart. 2395 */ 2396 if (err) { 2397 NL_SET_BAD_ATTR(info->extack, bad_attr); 2398 if (bad_rpcb) 2399 NL_SET_ERR_MSG_FMT(info->extack, 2400 "cannot create %s listener; rpcbind did not answer", 2401 bad_xprt); 2402 else if (skipped_rpcb) 2403 NL_SET_ERR_MSG_FMT(info->extack, 2404 "cannot create %s listener; rpcbind did not answer earlier", 2405 bad_xprt); 2406 else 2407 NL_SET_ERR_MSG_FMT(info->extack, 2408 "cannot create %s listener", 2409 bad_xprt); 2410 } else if (skipped_rpcb) { 2411 NL_SET_ERR_MSG(info->extack, 2412 "rpcbind did not answer, some listeners are not registered"); 2413 } 2414 2415 /* 2416 * Build the reply before the serv can go away, and only on success. 2417 * A caller that got an errno has nothing to register. 2418 */ 2419 if (!err && userspace_rpcbind) { 2420 rskb = nfsd_nl_listener_set_msg(info, net, serv); 2421 if (IS_ERR(rskb)) { 2422 err = PTR_ERR(rskb); 2423 rskb = NULL; 2424 /* 2425 * The listeners are up and the errno alone reads as 2426 * if nothing happened. Retrying is safe: a request 2427 * that matches the running set recreates nothing. 2428 */ 2429 NL_SET_ERR_MSG(info->extack, 2430 "listeners are up but the reply could not be built; retry to fetch it"); 2431 } 2432 } 2433 2434 if (!serv->sv_nrthreads && list_empty(&nn->nfsd_serv->sv_permsocks)) 2435 nfsd_destroy_serv(net); 2436 2437 out_unlock_mtx: 2438 mutex_unlock(&nfsd_mutex); 2439 2440 /* rskb is only built once err is known to be zero. */ 2441 if (rskb) 2442 return genlmsg_reply(rskb, info); 2443 2444 return err; 2445 } 2446 2447 /** 2448 * nfsd_nl_listener_get_doit - get the nfs running listeners 2449 * @skb: reply buffer 2450 * @info: netlink metadata and command arguments 2451 * 2452 * Return 0 on success or a negative errno. 2453 */ 2454 int nfsd_nl_listener_get_doit(struct sk_buff *skb, struct genl_info *info) 2455 { 2456 struct svc_xprt *xprt; 2457 struct svc_serv *serv; 2458 struct nfsd_net *nn; 2459 void *hdr; 2460 int err; 2461 2462 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); 2463 if (!skb) 2464 return -ENOMEM; 2465 2466 hdr = genlmsg_iput(skb, info); 2467 if (!hdr) { 2468 err = -EMSGSIZE; 2469 goto err_free_msg; 2470 } 2471 2472 mutex_lock(&nfsd_mutex); 2473 nn = net_generic(genl_info_net(info), nfsd_net_id); 2474 2475 /* no nfs server? Just send empty socket list */ 2476 if (!nn->nfsd_serv) 2477 goto out_unlock_mtx; 2478 2479 serv = nn->nfsd_serv; 2480 spin_lock_bh(&serv->sv_lock); 2481 list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list) { 2482 struct nlattr *attr; 2483 2484 attr = nla_nest_start(skb, NFSD_A_SERVER_SOCK_ADDR); 2485 if (!attr) { 2486 err = -EINVAL; 2487 goto err_serv_unlock; 2488 } 2489 2490 if (nla_put_string(skb, NFSD_A_SOCK_TRANSPORT_NAME, 2491 xprt->xpt_class->xcl_name) || 2492 nla_put(skb, NFSD_A_SOCK_ADDR, 2493 sizeof(struct sockaddr_storage), 2494 &xprt->xpt_local)) { 2495 err = -EINVAL; 2496 goto err_serv_unlock; 2497 } 2498 2499 nla_nest_end(skb, attr); 2500 } 2501 spin_unlock_bh(&serv->sv_lock); 2502 out_unlock_mtx: 2503 mutex_unlock(&nfsd_mutex); 2504 genlmsg_end(skb, hdr); 2505 2506 return genlmsg_reply(skb, info); 2507 2508 err_serv_unlock: 2509 spin_unlock_bh(&serv->sv_lock); 2510 mutex_unlock(&nfsd_mutex); 2511 err_free_msg: 2512 nlmsg_free(skb); 2513 2514 return err; 2515 } 2516 2517 /** 2518 * nfsd_nl_pool_mode_set_doit - set the number of running threads 2519 * @skb: reply buffer 2520 * @info: netlink metadata and command arguments 2521 * 2522 * Return 0 on success or a negative errno. 2523 */ 2524 int nfsd_nl_pool_mode_set_doit(struct sk_buff *skb, struct genl_info *info) 2525 { 2526 const struct nlattr *attr; 2527 2528 if (GENL_REQ_ATTR_CHECK(info, NFSD_A_POOL_MODE_MODE)) 2529 return -EINVAL; 2530 2531 attr = info->attrs[NFSD_A_POOL_MODE_MODE]; 2532 return sunrpc_set_pool_mode(nla_data(attr)); 2533 } 2534 2535 /** 2536 * nfsd_nl_pool_mode_get_doit - get info about pool_mode 2537 * @skb: reply buffer 2538 * @info: netlink metadata and command arguments 2539 * 2540 * Return 0 on success or a negative errno. 2541 */ 2542 int nfsd_nl_pool_mode_get_doit(struct sk_buff *skb, struct genl_info *info) 2543 { 2544 struct net *net = genl_info_net(info); 2545 char buf[16]; 2546 void *hdr; 2547 int err; 2548 2549 if (sunrpc_get_pool_mode(buf, ARRAY_SIZE(buf)) >= ARRAY_SIZE(buf)) 2550 return -ERANGE; 2551 2552 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); 2553 if (!skb) 2554 return -ENOMEM; 2555 2556 err = -EMSGSIZE; 2557 hdr = genlmsg_iput(skb, info); 2558 if (!hdr) 2559 goto err_free_msg; 2560 2561 err = nla_put_string(skb, NFSD_A_POOL_MODE_MODE, buf) | 2562 nla_put_u32(skb, NFSD_A_POOL_MODE_NPOOLS, nfsd_nrpools(net)); 2563 if (err) 2564 goto err_free_msg; 2565 2566 genlmsg_end(skb, hdr); 2567 return genlmsg_reply(skb, info); 2568 2569 err_free_msg: 2570 nlmsg_free(skb); 2571 return err; 2572 } 2573 2574 /** 2575 * nfsd_nl_cache_flush_doit - flush nfsd caches via netlink 2576 * @skb: reply buffer 2577 * @info: netlink metadata and command arguments 2578 * 2579 * Flush the svc_export and/or expkey caches. If NFSD_A_CACHE_FLUSH_MASK 2580 * is provided, only flush the caches indicated by the bitmask (bit 0 = 2581 * svc_export, bit 1 = expkey). If omitted, flush both. 2582 * 2583 * Return 0 on success or a negative errno. 2584 */ 2585 int nfsd_nl_cache_flush_doit(struct sk_buff *skb, struct genl_info *info) 2586 { 2587 struct net *net = genl_info_net(info); 2588 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 2589 u32 mask = ~0U; 2590 2591 if (info->attrs[NFSD_A_CACHE_FLUSH_MASK]) 2592 mask = nla_get_u32(info->attrs[NFSD_A_CACHE_FLUSH_MASK]); 2593 2594 mutex_lock(&nfsd_mutex); 2595 2596 if ((mask & NFSD_CACHE_TYPE_SVC_EXPORT) && 2597 nn->svc_export_cache) 2598 cache_purge(nn->svc_export_cache); 2599 2600 if ((mask & NFSD_CACHE_TYPE_EXPKEY) && 2601 nn->svc_expkey_cache) 2602 cache_purge(nn->svc_expkey_cache); 2603 2604 mutex_unlock(&nfsd_mutex); 2605 2606 return 0; 2607 } 2608 2609 /* Emit a single server-proc-entry nest: { op, count }. */ 2610 static int nfsd_nl_put_proc_entry(struct sk_buff *skb, int attr, 2611 u32 op, u64 count) 2612 { 2613 struct nlattr *nest; 2614 2615 nest = nla_nest_start(skb, attr); 2616 if (!nest) 2617 return -EMSGSIZE; 2618 if (nla_put_u32(skb, NFSD_A_SERVER_PROC_ENTRY_OP, op) || 2619 nla_put_u64_64bit(skb, NFSD_A_SERVER_PROC_ENTRY_COUNT, 2620 count, NFSD_A_SERVER_PROC_ENTRY_PAD)) { 2621 nla_nest_cancel(skb, nest); 2622 return -EMSGSIZE; 2623 } 2624 nla_nest_end(skb, nest); 2625 return 0; 2626 } 2627 2628 /* Emit the scalar server-stats counters. Only ever called on a fresh skb. */ 2629 static int nfsd_nl_server_stats_scalars(struct sk_buff *skb, 2630 struct nfsd_net *nn, 2631 struct svc_stat *statp) 2632 { 2633 if (nla_put_u64_64bit(skb, NFSD_A_SERVER_STATS_RC_HITS, 2634 percpu_counter_sum_positive(&nn->counter[NFSD_STATS_RC_HITS]), 2635 NFSD_A_SERVER_STATS_PAD) || 2636 nla_put_u64_64bit(skb, NFSD_A_SERVER_STATS_RC_MISSES, 2637 percpu_counter_sum_positive(&nn->counter[NFSD_STATS_RC_MISSES]), 2638 NFSD_A_SERVER_STATS_PAD) || 2639 nla_put_u64_64bit(skb, NFSD_A_SERVER_STATS_RC_NOCACHE, 2640 percpu_counter_sum_positive(&nn->counter[NFSD_STATS_RC_NOCACHE]), 2641 NFSD_A_SERVER_STATS_PAD)) 2642 return -EMSGSIZE; 2643 2644 if (nla_put_u64_64bit(skb, NFSD_A_SERVER_STATS_FH_STALE, 2645 percpu_counter_sum_positive(&nn->counter[NFSD_STATS_FH_STALE]), 2646 NFSD_A_SERVER_STATS_PAD)) 2647 return -EMSGSIZE; 2648 2649 if (nla_put_u64_64bit(skb, NFSD_A_SERVER_STATS_IO_READ, 2650 percpu_counter_sum_positive(&nn->counter[NFSD_STATS_IO_READ]), 2651 NFSD_A_SERVER_STATS_PAD) || 2652 nla_put_u64_64bit(skb, NFSD_A_SERVER_STATS_IO_WRITE, 2653 percpu_counter_sum_positive(&nn->counter[NFSD_STATS_IO_WRITE]), 2654 NFSD_A_SERVER_STATS_PAD)) 2655 return -EMSGSIZE; 2656 2657 if (nla_put_u32(skb, NFSD_A_SERVER_STATS_NETCNT, statp->netcnt) || 2658 nla_put_u32(skb, NFSD_A_SERVER_STATS_NETUDPCNT, statp->netudpcnt) || 2659 nla_put_u32(skb, NFSD_A_SERVER_STATS_NETTCPCNT, statp->nettcpcnt) || 2660 nla_put_u32(skb, NFSD_A_SERVER_STATS_NETTCPCONN, statp->nettcpconn)) 2661 return -EMSGSIZE; 2662 2663 if (nla_put_u32(skb, NFSD_A_SERVER_STATS_RPCCNT, statp->rpccnt) || 2664 nla_put_u32(skb, NFSD_A_SERVER_STATS_RPCBADFMT, statp->rpcbadfmt) || 2665 nla_put_u32(skb, NFSD_A_SERVER_STATS_RPCBADAUTH, statp->rpcbadauth) || 2666 nla_put_u32(skb, NFSD_A_SERVER_STATS_RPCBADCLNT, statp->rpcbadclnt)) 2667 return -EMSGSIZE; 2668 2669 return 0; 2670 } 2671 2672 /* 2673 * Emit per-version procedure counts for one NFS version, resuming at *idx. 2674 * Returns 0 when the version has been fully emitted (or is not present), or 2675 * -EMSGSIZE when @skb filled up, leaving *idx at the entry still to emit. 2676 */ 2677 static int nfsd_nl_server_stats_proc(struct sk_buff *skb, 2678 struct svc_stat *statp, 2679 struct svc_program *prog, 2680 unsigned int ver, int attr, int *idx) 2681 { 2682 unsigned long __percpu *counts; 2683 unsigned int nproc; 2684 2685 if (!statp->vs_count || ver >= prog->pg_nvers || 2686 !prog->pg_vers[ver] || !statp->vs_count[ver]) 2687 return 0; 2688 2689 counts = statp->vs_count[ver]; 2690 nproc = prog->pg_vers[ver]->vs_nproc; 2691 2692 for (; *idx < nproc; (*idx)++) { 2693 unsigned long count = 0; 2694 int cpu; 2695 2696 for_each_possible_cpu(cpu) 2697 count += per_cpu(counts[*idx], cpu); 2698 2699 if (!count) 2700 continue; 2701 if (nfsd_nl_put_proc_entry(skb, attr, *idx, count)) 2702 return -EMSGSIZE; 2703 } 2704 2705 return 0; 2706 } 2707 2708 #ifdef CONFIG_NFSD_V4 2709 /* 2710 * Emit NFSv4 per-operation counts, resuming at *idx. Same return convention 2711 * as nfsd_nl_server_stats_proc(). 2712 */ 2713 static int nfsd_nl_server_stats_nfs4ops(struct sk_buff *skb, 2714 struct nfsd_net *nn, int *idx) 2715 { 2716 for (; *idx <= LAST_NFS4_OP; (*idx)++) { 2717 u64 cnt = percpu_counter_sum_positive( 2718 &nn->counter[NFSD_STATS_NFS4_OP(*idx)]); 2719 2720 if (!cnt) 2721 continue; 2722 if (nfsd_nl_put_proc_entry(skb, NFSD_A_SERVER_STATS_PROC4OPS_OPS, 2723 *idx, cnt)) 2724 return -EMSGSIZE; 2725 } 2726 2727 return 0; 2728 } 2729 2730 /* 2731 * Emit NFSv4 callback (backchannel) per-operation counts, resuming at *idx, 2732 * which counts from OP_CB_GETATTR. Same return convention as 2733 * nfsd_nl_server_stats_proc(). 2734 */ 2735 static int nfsd_nl_server_stats_cbops(struct sk_buff *skb, 2736 struct nfsd_net *nn, int *idx) 2737 { 2738 int op; 2739 2740 for (op = OP_CB_GETATTR + *idx; op <= OP_CB_OFFLOAD; op++, (*idx)++) { 2741 u64 cnt = percpu_counter_sum_positive(&nn->cb_counter[op]); 2742 2743 if (!cnt) 2744 continue; 2745 if (nfsd_nl_put_proc_entry(skb, NFSD_A_SERVER_STATS_PROC4CB_OPS, 2746 op, cnt)) 2747 return -EMSGSIZE; 2748 } 2749 2750 return 0; 2751 } 2752 #endif 2753 2754 /* Sections of the server-stats dump, emitted in order across messages. */ 2755 enum { 2756 NFSD_SERVER_STATS_SCALARS = 0, 2757 NFSD_SERVER_STATS_PROC2, 2758 NFSD_SERVER_STATS_PROC3, 2759 NFSD_SERVER_STATS_PROC4, 2760 NFSD_SERVER_STATS_PROC4CB, 2761 NFSD_SERVER_STATS_PROC4OPS, 2762 NFSD_SERVER_STATS_DONE, 2763 }; 2764 2765 /** 2766 * nfsd_nl_server_stats_get_dumpit - dump NFS server statistics 2767 * @skb: reply buffer 2768 * @cb: netlink metadata and command arguments 2769 * 2770 * The server-stats object is emitted across one or more netlink messages. 2771 * cb->args[0] tracks the current section and cb->args[1] the entry index 2772 * within it, so a section that does not fit in the current message is resumed 2773 * in the next one. The scalar counters are small and emitted once, in the 2774 * first message; userspace merges the attributes from every message. 2775 * 2776 * Returns the size of the reply or a negative errno. 2777 */ 2778 int nfsd_nl_server_stats_get_dumpit(struct sk_buff *skb, 2779 struct netlink_callback *cb) 2780 { 2781 struct net *net = sock_net(skb->sk); 2782 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 2783 struct svc_stat *statp = &nn->nfsd_svcstats; 2784 struct svc_program *prog = statp->program; 2785 int section = cb->args[0]; 2786 int idx = cb->args[1]; 2787 void *hdr; 2788 2789 if (section >= NFSD_SERVER_STATS_DONE) 2790 return 0; 2791 2792 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, 2793 cb->nlh->nlmsg_seq, &nfsd_nl_family, 2794 NLM_F_MULTI, NFSD_CMD_SERVER_STATS_GET); 2795 if (!hdr) 2796 return -ENOBUFS; 2797 2798 /* Scalar stats fit easily and are emitted in the first message. */ 2799 if (section == NFSD_SERVER_STATS_SCALARS) { 2800 if (nfsd_nl_server_stats_scalars(skb, nn, statp)) 2801 goto err_cancel; 2802 section = NFSD_SERVER_STATS_PROC2; 2803 idx = 0; 2804 } 2805 2806 /* 2807 * Emit as many of the remaining sections as fit. A section returning 2808 * -EMSGSIZE means the message is full: close it and resume from the 2809 * same section/index on the next call with a fresh skb. Each entry is 2810 * small enough to fit in a fresh skb, so forward progress is assured. 2811 */ 2812 while (section < NFSD_SERVER_STATS_DONE) { 2813 int ret = 0; 2814 2815 switch (section) { 2816 case NFSD_SERVER_STATS_PROC2: 2817 ret = nfsd_nl_server_stats_proc(skb, statp, prog, 2, 2818 NFSD_A_SERVER_STATS_PROC2_OPS, &idx); 2819 break; 2820 case NFSD_SERVER_STATS_PROC3: 2821 ret = nfsd_nl_server_stats_proc(skb, statp, prog, 3, 2822 NFSD_A_SERVER_STATS_PROC3_OPS, &idx); 2823 break; 2824 case NFSD_SERVER_STATS_PROC4: 2825 ret = nfsd_nl_server_stats_proc(skb, statp, prog, 4, 2826 NFSD_A_SERVER_STATS_PROC4_OPS, &idx); 2827 break; 2828 #ifdef CONFIG_NFSD_V4 2829 case NFSD_SERVER_STATS_PROC4CB: 2830 ret = nfsd_nl_server_stats_cbops(skb, nn, &idx); 2831 break; 2832 case NFSD_SERVER_STATS_PROC4OPS: 2833 ret = nfsd_nl_server_stats_nfs4ops(skb, nn, &idx); 2834 break; 2835 #endif 2836 } 2837 2838 if (ret == -EMSGSIZE) 2839 goto out; 2840 if (ret) 2841 goto err_cancel; 2842 2843 section++; 2844 idx = 0; 2845 } 2846 2847 out: 2848 genlmsg_end(skb, hdr); 2849 cb->args[0] = section; 2850 cb->args[1] = idx; 2851 return skb->len; 2852 2853 err_cancel: 2854 genlmsg_cancel(skb, hdr); 2855 return -EMSGSIZE; 2856 } 2857 2858 int nfsd_cache_notify(struct cache_detail *cd, struct cache_head *h, u32 cache_type) 2859 { 2860 struct genlmsghdr *hdr; 2861 struct sk_buff *msg; 2862 2863 if (!genl_has_listeners(&nfsd_nl_family, cd->net, NFSD_NLGRP_EXPORTD)) 2864 return -ENOLINK; 2865 2866 msg = genlmsg_new(nla_total_size(sizeof(u32)), GFP_KERNEL); 2867 if (!msg) 2868 return -ENOMEM; 2869 2870 hdr = genlmsg_put(msg, 0, 0, &nfsd_nl_family, 0, NFSD_CMD_CACHE_NOTIFY); 2871 if (!hdr) { 2872 nlmsg_free(msg); 2873 return -ENOMEM; 2874 } 2875 2876 if (nla_put_u32(msg, NFSD_A_CACHE_NOTIFY_CACHE_TYPE, cache_type)) { 2877 nlmsg_free(msg); 2878 return -ENOMEM; 2879 } 2880 2881 genlmsg_end(msg, hdr); 2882 return genlmsg_multicast_netns(&nfsd_nl_family, cd->net, msg, 0, 2883 NFSD_NLGRP_EXPORTD, GFP_KERNEL); 2884 } 2885 2886 /** 2887 * nfsd_nl_unlock_ip_doit - release NLM locks held by an IP address 2888 * @skb: reply buffer 2889 * @info: netlink metadata and command arguments 2890 * 2891 * Return: 0 on success or a negative errno. 2892 */ 2893 int nfsd_nl_unlock_ip_doit(struct sk_buff *skb, struct genl_info *info) 2894 { 2895 struct sockaddr *sap; 2896 2897 if (GENL_REQ_ATTR_CHECK(info, NFSD_A_UNLOCK_IP_ADDRESS)) 2898 return -EINVAL; 2899 sap = nla_data(info->attrs[NFSD_A_UNLOCK_IP_ADDRESS]); 2900 switch (sap->sa_family) { 2901 case AF_INET: 2902 if (nla_len(info->attrs[NFSD_A_UNLOCK_IP_ADDRESS]) < 2903 sizeof(struct sockaddr_in)) 2904 return -EINVAL; 2905 break; 2906 case AF_INET6: 2907 if (nla_len(info->attrs[NFSD_A_UNLOCK_IP_ADDRESS]) < 2908 sizeof(struct sockaddr_in6)) 2909 return -EINVAL; 2910 break; 2911 default: 2912 return -EAFNOSUPPORT; 2913 } 2914 /* 2915 * nlmsvc_unlock_all_by_ip() releases matching locks 2916 * across all network namespaces because lockd operates 2917 * a single global instance. 2918 */ 2919 trace_nfsd_ctl_unlock_ip(genl_info_net(info), sap, 2920 svc_addr_len(sap)); 2921 return nlmsvc_unlock_all_by_ip(sap); 2922 } 2923 2924 /** 2925 * nfsd_nl_unlock_filesystem_doit - revoke NFS state under a filesystem path 2926 * @skb: reply buffer 2927 * @info: netlink metadata and command arguments 2928 * 2929 * Return: 0 on success or a negative errno. 2930 */ 2931 int nfsd_nl_unlock_filesystem_doit(struct sk_buff *skb, 2932 struct genl_info *info) 2933 { 2934 struct net *net = genl_info_net(info); 2935 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 2936 struct path path; 2937 int error; 2938 2939 if (GENL_REQ_ATTR_CHECK(info, NFSD_A_UNLOCK_FILESYSTEM_PATH)) 2940 return -EINVAL; 2941 2942 trace_nfsd_ctl_unlock_fs(net, 2943 nla_data(info->attrs[NFSD_A_UNLOCK_FILESYSTEM_PATH])); 2944 error = kern_path( 2945 nla_data(info->attrs[NFSD_A_UNLOCK_FILESYSTEM_PATH]), 2946 0, &path); 2947 if (error) 2948 return error; 2949 2950 error = nlmsvc_unlock_all_by_sb(path.dentry->d_sb); 2951 2952 mutex_lock(&nfsd_mutex); 2953 if (test_bit(NFSD_NET_UP, &nn->flags)) { 2954 nfsd4_cancel_copy_by_sb(net, path.dentry->d_sb); 2955 nfsd4_revoke_states(nn, path.dentry->d_sb); 2956 } else { 2957 error = -EINVAL; 2958 } 2959 mutex_unlock(&nfsd_mutex); 2960 2961 path_put(&path); 2962 return error; 2963 } 2964 2965 /** 2966 * nfsd_nl_unlock_export_doit - revoke NFSv4 state for an export path 2967 * @skb: reply buffer 2968 * @info: netlink metadata and command arguments 2969 * 2970 * Revokes all NFSv4 state (opens, locks, delegations, layouts) acquired 2971 * through any export of the given path, regardless of which client holds 2972 * the state. Userspace (exportfs -u) sends this after removing the last 2973 * client for a path so the underlying filesystem can be unmounted. 2974 * 2975 * Unlike NFSD_CMD_UNLOCK_FILESYSTEM, which operates at superblock 2976 * granularity, this command revokes only the state associated with 2977 * exports of a specific path. 2978 * 2979 * Return: 0 on success or a negative errno. 2980 */ 2981 int nfsd_nl_unlock_export_doit(struct sk_buff *skb, struct genl_info *info) 2982 { 2983 struct net *net = genl_info_net(info); 2984 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 2985 struct path path; 2986 int error; 2987 2988 if (GENL_REQ_ATTR_CHECK(info, NFSD_A_UNLOCK_EXPORT_PATH)) 2989 return -EINVAL; 2990 2991 trace_nfsd_ctl_unlock_export(net, 2992 nla_data(info->attrs[NFSD_A_UNLOCK_EXPORT_PATH])); 2993 error = kern_path( 2994 nla_data(info->attrs[NFSD_A_UNLOCK_EXPORT_PATH]), 2995 0, &path); 2996 if (error) 2997 return error; 2998 2999 mutex_lock(&nfsd_mutex); 3000 if (test_bit(NFSD_NET_UP, &nn->flags)) { 3001 nfsd_file_close_export(net, &path); 3002 nfsd4_revoke_export_states(nn, &path); 3003 } else 3004 error = -EINVAL; 3005 mutex_unlock(&nfsd_mutex); 3006 3007 path_put(&path); 3008 return error; 3009 } 3010 3011 /** 3012 * nfsd_net_init - Prepare the nfsd_net portion of a new net namespace 3013 * @net: a freshly-created network namespace 3014 * 3015 * This information stays around as long as the network namespace is 3016 * alive whether or not there is an NFSD instance running in the 3017 * namespace. 3018 * 3019 * Returns zero on success, or a negative errno otherwise. 3020 */ 3021 static __net_init int nfsd_net_init(struct net *net) 3022 { 3023 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 3024 int retval; 3025 int i; 3026 3027 retval = nfsd_net_cb_init(nn); 3028 if (retval) 3029 return retval; 3030 retval = nfsd_export_init(net); 3031 if (retval) 3032 goto out_export_error; 3033 retval = nfsd_idmap_init(net); 3034 if (retval) 3035 goto out_idmap_error; 3036 retval = percpu_counter_init_many(nn->counter, 0, GFP_KERNEL, 3037 NFSD_STATS_COUNTERS_NUM); 3038 if (retval) 3039 goto out_repcache_error; 3040 3041 #ifdef CONFIG_NFSD_V4 3042 retval = percpu_counter_init_many(nn->cb_counter, 0, GFP_KERNEL, 3043 NFSD_STATS_CB_OPS_NUM); 3044 if (retval) 3045 goto out_cb_counter_error; 3046 #endif 3047 3048 memset(&nn->nfsd_svcstats, 0, sizeof(nn->nfsd_svcstats)); 3049 nn->nfsd_svcstats.program = &nfsd_programs[0]; 3050 retval = svc_stat_alloc_counts(&nn->nfsd_svcstats); 3051 if (retval) 3052 goto out_proc_error; 3053 if (!nfsd_proc_stat_init(net)) { 3054 retval = -ENOMEM; 3055 goto out_svcstats_error; 3056 } 3057 3058 for (i = 0; i < sizeof(nn->nfsd_versions); i++) 3059 nn->nfsd_versions[i] = nfsd_support_version(i); 3060 for (i = 0; i < sizeof(nn->nfsd4_minorversions); i++) 3061 nn->nfsd4_minorversions[i] = nfsd_support_version(4); 3062 nn->nfsd_info.mutex = &nfsd_mutex; 3063 nn->nfsd_serv = NULL; 3064 nfsd4_init_leases_net(nn); 3065 get_random_bytes(&nn->siphash_key, sizeof(nn->siphash_key)); 3066 seqlock_init(&nn->writeverf_lock); 3067 #if IS_ENABLED(CONFIG_NFS_LOCALIO) 3068 spin_lock_init(&nn->local_clients_lock); 3069 INIT_LIST_HEAD(&nn->local_clients); 3070 #endif 3071 return 0; 3072 3073 out_svcstats_error: 3074 svc_stat_free_counts(&nn->nfsd_svcstats); 3075 out_proc_error: 3076 #ifdef CONFIG_NFSD_V4 3077 percpu_counter_destroy_many(nn->cb_counter, NFSD_STATS_CB_OPS_NUM); 3078 out_cb_counter_error: 3079 #endif 3080 percpu_counter_destroy_many(nn->counter, NFSD_STATS_COUNTERS_NUM); 3081 out_repcache_error: 3082 nfsd_idmap_shutdown(net); 3083 out_idmap_error: 3084 nfsd_export_shutdown(net); 3085 out_export_error: 3086 nfsd_net_cb_shutdown(nn); 3087 return retval; 3088 } 3089 3090 #if IS_ENABLED(CONFIG_NFS_LOCALIO) 3091 /** 3092 * nfsd_net_pre_exit - Disconnect localio clients from net namespace 3093 * @net: a network namespace that is about to be destroyed 3094 * 3095 * This invalidates ->net pointers held by localio clients 3096 * while they can still safely access nn->counter. 3097 */ 3098 static __net_exit void nfsd_net_pre_exit(struct net *net) 3099 { 3100 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 3101 3102 nfs_localio_invalidate_clients(&nn->local_clients, 3103 &nn->local_clients_lock); 3104 } 3105 #endif 3106 3107 /** 3108 * nfsd_net_exit - Release the nfsd_net portion of a net namespace 3109 * @net: a network namespace that is about to be destroyed 3110 * 3111 */ 3112 static __net_exit void nfsd_net_exit(struct net *net) 3113 { 3114 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 3115 3116 kfree_sensitive(nn->fh_key); 3117 nfsd_net_cb_shutdown(nn); 3118 nfsd_proc_stat_shutdown(net); 3119 svc_stat_free_counts(&nn->nfsd_svcstats); 3120 #ifdef CONFIG_NFSD_V4 3121 percpu_counter_destroy_many(nn->cb_counter, NFSD_STATS_CB_OPS_NUM); 3122 #endif 3123 percpu_counter_destroy_many(nn->counter, NFSD_STATS_COUNTERS_NUM); 3124 nfsd_idmap_shutdown(net); 3125 nfsd_export_shutdown(net); 3126 } 3127 3128 static struct pernet_operations nfsd_net_ops = { 3129 .init = nfsd_net_init, 3130 #if IS_ENABLED(CONFIG_NFS_LOCALIO) 3131 .pre_exit = nfsd_net_pre_exit, 3132 #endif 3133 .exit = nfsd_net_exit, 3134 .id = &nfsd_net_id, 3135 .size = sizeof(struct nfsd_net), 3136 }; 3137 3138 static int __init init_nfsd(void) 3139 { 3140 int retval; 3141 3142 retval = nfsd4_init_slabs(); 3143 if (retval) 3144 return retval; 3145 3146 nfsd_debugfs_init(); 3147 3148 retval = nfsd4_init_pnfs(); 3149 if (retval) 3150 goto out_free_slabs; 3151 retval = nfsd_drc_slab_create(); 3152 if (retval) 3153 goto out_free_pnfs; 3154 nfsd_lockd_init(); /* lockd->nfsd callbacks */ 3155 retval = register_pernet_subsys(&nfsd_net_ops); 3156 if (retval < 0) 3157 goto out_free_lockd; 3158 retval = register_cld_notifier(); 3159 if (retval) 3160 goto out_free_subsys; 3161 retval = nfsd4_create_laundry_wq(); 3162 if (retval) 3163 goto out_free_cld; 3164 retval = register_filesystem(&nfsd_fs_type); 3165 if (retval) 3166 goto out_free_nfsd4; 3167 retval = genl_register_family(&nfsd_nl_family); 3168 if (retval) 3169 goto out_free_filesystem; 3170 retval = create_proc_exports_entry(); 3171 if (retval) 3172 goto out_free_all; 3173 nfsd_localio_ops_init(); 3174 3175 return 0; 3176 out_free_all: 3177 genl_unregister_family(&nfsd_nl_family); 3178 out_free_filesystem: 3179 unregister_filesystem(&nfsd_fs_type); 3180 out_free_nfsd4: 3181 nfsd4_destroy_laundry_wq(); 3182 out_free_cld: 3183 unregister_cld_notifier(); 3184 out_free_subsys: 3185 unregister_pernet_subsys(&nfsd_net_ops); 3186 out_free_lockd: 3187 nfsd_lockd_shutdown(); 3188 nfsd_drc_slab_free(); 3189 out_free_pnfs: 3190 nfsd4_exit_pnfs(); 3191 out_free_slabs: 3192 nfsd_debugfs_exit(); 3193 nfsd4_free_slabs(); 3194 return retval; 3195 } 3196 3197 static void __exit exit_nfsd(void) 3198 { 3199 remove_proc_entry("fs/nfs/exports", NULL); 3200 remove_proc_entry("fs/nfs", NULL); 3201 genl_unregister_family(&nfsd_nl_family); 3202 unregister_filesystem(&nfsd_fs_type); 3203 nfsd4_destroy_laundry_wq(); 3204 unregister_cld_notifier(); 3205 unregister_pernet_subsys(&nfsd_net_ops); 3206 nfsd_drc_slab_free(); 3207 nfsd_lockd_shutdown(); 3208 nfsd4_exit_pnfs(); 3209 nfsd_debugfs_exit(); 3210 nfsd4_free_slabs(); 3211 } 3212 3213 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>"); 3214 MODULE_DESCRIPTION("In-kernel NFS server"); 3215 MODULE_LICENSE("GPL"); 3216 module_init(init_nfsd) 3217 module_exit(exit_nfsd) 3218