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 24 #include "idmap.h" 25 #include "nfsd.h" 26 #include "cache.h" 27 #include "state.h" 28 #include "netns.h" 29 #include "pnfs.h" 30 #include "filecache.h" 31 #include "trace.h" 32 #include "netlink.h" 33 34 /* 35 * We have a single directory with several nodes in it. 36 */ 37 enum { 38 NFSD_Root = 1, 39 NFSD_List, 40 NFSD_Export_Stats, 41 NFSD_Export_features, 42 NFSD_Fh, 43 NFSD_FO_UnlockIP, 44 NFSD_FO_UnlockFS, 45 NFSD_Threads, 46 NFSD_Pool_Threads, 47 NFSD_Pool_Stats, 48 NFSD_Reply_Cache_Stats, 49 NFSD_Versions, 50 NFSD_Ports, 51 NFSD_MaxBlkSize, 52 NFSD_Filecache, 53 NFSD_Leasetime, 54 NFSD_Gracetime, 55 NFSD_RecoveryDir, 56 NFSD_V4EndGrace, 57 NFSD_MaxReserved 58 }; 59 60 /* 61 * write() for these nodes. 62 */ 63 static ssize_t write_filehandle(struct file *file, char *buf, size_t size); 64 static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size); 65 static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size); 66 static ssize_t write_threads(struct file *file, char *buf, size_t size); 67 static ssize_t write_pool_threads(struct file *file, char *buf, size_t size); 68 static ssize_t write_versions(struct file *file, char *buf, size_t size); 69 static ssize_t write_ports(struct file *file, char *buf, size_t size); 70 static ssize_t write_maxblksize(struct file *file, char *buf, size_t size); 71 #ifdef CONFIG_NFSD_V4 72 static ssize_t write_leasetime(struct file *file, char *buf, size_t size); 73 static ssize_t write_gracetime(struct file *file, char *buf, size_t size); 74 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING 75 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size); 76 #endif 77 static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size); 78 #endif 79 80 static ssize_t (*const write_op[])(struct file *, char *, size_t) = { 81 [NFSD_Fh] = write_filehandle, 82 [NFSD_FO_UnlockIP] = write_unlock_ip, 83 [NFSD_FO_UnlockFS] = write_unlock_fs, 84 [NFSD_Threads] = write_threads, 85 [NFSD_Pool_Threads] = write_pool_threads, 86 [NFSD_Versions] = write_versions, 87 [NFSD_Ports] = write_ports, 88 [NFSD_MaxBlkSize] = write_maxblksize, 89 #ifdef CONFIG_NFSD_V4 90 [NFSD_Leasetime] = write_leasetime, 91 [NFSD_Gracetime] = write_gracetime, 92 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING 93 [NFSD_RecoveryDir] = write_recoverydir, 94 #endif 95 [NFSD_V4EndGrace] = write_v4_end_grace, 96 #endif 97 }; 98 99 static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos) 100 { 101 ino_t ino = file_inode(file)->i_ino; 102 char *data; 103 ssize_t rv; 104 105 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino]) 106 return -EINVAL; 107 108 data = simple_transaction_get(file, buf, size); 109 if (IS_ERR(data)) 110 return PTR_ERR(data); 111 112 rv = write_op[ino](file, data, size); 113 if (rv < 0) 114 return rv; 115 116 simple_transaction_set(file, rv); 117 return size; 118 } 119 120 static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos) 121 { 122 if (! file->private_data) { 123 /* An attempt to read a transaction file without writing 124 * causes a 0-byte write so that the file can return 125 * state information 126 */ 127 ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos); 128 if (rv < 0) 129 return rv; 130 } 131 return simple_transaction_read(file, buf, size, pos); 132 } 133 134 static const struct file_operations transaction_ops = { 135 .write = nfsctl_transaction_write, 136 .read = nfsctl_transaction_read, 137 .release = simple_transaction_release, 138 .llseek = default_llseek, 139 }; 140 141 static int exports_net_open(struct net *net, struct file *file) 142 { 143 int err; 144 struct seq_file *seq; 145 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 146 147 err = seq_open(file, &nfs_exports_op); 148 if (err) 149 return err; 150 151 seq = file->private_data; 152 seq->private = nn->svc_export_cache; 153 get_net(net); 154 return 0; 155 } 156 157 static int exports_release(struct inode *inode, struct file *file) 158 { 159 struct seq_file *seq = file->private_data; 160 struct cache_detail *cd = seq->private; 161 162 put_net(cd->net); 163 return seq_release(inode, file); 164 } 165 166 static int exports_nfsd_open(struct inode *inode, struct file *file) 167 { 168 return exports_net_open(inode->i_sb->s_fs_info, file); 169 } 170 171 static const struct file_operations exports_nfsd_operations = { 172 .open = exports_nfsd_open, 173 .read = seq_read, 174 .llseek = seq_lseek, 175 .release = exports_release, 176 }; 177 178 static int export_features_show(struct seq_file *m, void *v) 179 { 180 seq_printf(m, "0x%x 0x%x\n", NFSEXP_ALLFLAGS, NFSEXP_SECINFO_FLAGS); 181 return 0; 182 } 183 184 DEFINE_SHOW_ATTRIBUTE(export_features); 185 186 static int nfsd_pool_stats_open(struct inode *inode, struct file *file) 187 { 188 struct nfsd_net *nn = net_generic(inode->i_sb->s_fs_info, nfsd_net_id); 189 190 return svc_pool_stats_open(&nn->nfsd_info, file); 191 } 192 193 static const struct file_operations pool_stats_operations = { 194 .open = nfsd_pool_stats_open, 195 .read = seq_read, 196 .llseek = seq_lseek, 197 .release = seq_release, 198 }; 199 200 DEFINE_SHOW_ATTRIBUTE(nfsd_reply_cache_stats); 201 202 DEFINE_SHOW_ATTRIBUTE(nfsd_file_cache_stats); 203 204 /*----------------------------------------------------------------------------*/ 205 /* 206 * payload - write methods 207 */ 208 209 static inline struct net *netns(struct file *file) 210 { 211 return file_inode(file)->i_sb->s_fs_info; 212 } 213 214 /* 215 * write_unlock_ip - Release all locks used by a client 216 * 217 * Experimental. 218 * 219 * Input: 220 * buf: '\n'-terminated C string containing a 221 * presentation format IP address 222 * size: length of C string in @buf 223 * Output: 224 * On success: returns zero if all specified locks were released; 225 * returns one if one or more locks were not released 226 * On error: return code is negative errno value 227 */ 228 static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size) 229 { 230 struct sockaddr_storage address; 231 struct sockaddr *sap = (struct sockaddr *)&address; 232 size_t salen = sizeof(address); 233 char *fo_path; 234 struct net *net = netns(file); 235 236 /* sanity check */ 237 if (size == 0) 238 return -EINVAL; 239 240 if (buf[size-1] != '\n') 241 return -EINVAL; 242 243 fo_path = buf; 244 if (qword_get(&buf, fo_path, size) < 0) 245 return -EINVAL; 246 247 if (rpc_pton(net, fo_path, size, sap, salen) == 0) 248 return -EINVAL; 249 250 trace_nfsd_ctl_unlock_ip(net, sap, svc_addr_len(sap)); 251 return nlmsvc_unlock_all_by_ip(sap); 252 } 253 254 /* 255 * write_unlock_fs - Release all locks on a local file system 256 * 257 * Experimental. 258 * 259 * Input: 260 * buf: '\n'-terminated C string containing the 261 * absolute pathname of a local file system 262 * size: length of C string in @buf 263 * Output: 264 * On success: returns zero if all specified locks were released; 265 * returns one if one or more locks were not released 266 * On error: return code is negative errno value 267 */ 268 static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size) 269 { 270 struct path path; 271 char *fo_path; 272 int error; 273 struct nfsd_net *nn; 274 275 /* sanity check */ 276 if (size == 0) 277 return -EINVAL; 278 279 if (buf[size-1] != '\n') 280 return -EINVAL; 281 282 fo_path = buf; 283 if (qword_get(&buf, fo_path, size) < 0) 284 return -EINVAL; 285 trace_nfsd_ctl_unlock_fs(netns(file), fo_path); 286 error = kern_path(fo_path, 0, &path); 287 if (error) 288 return error; 289 290 /* 291 * XXX: Needs better sanity checking. Otherwise we could end up 292 * releasing locks on the wrong file system. 293 * 294 * For example: 295 * 1. Does the path refer to a directory? 296 * 2. Is that directory a mount point, or 297 * 3. Is that directory the root of an exported file system? 298 */ 299 nfsd4_cancel_copy_by_sb(netns(file), path.dentry->d_sb); 300 error = nlmsvc_unlock_all_by_sb(path.dentry->d_sb); 301 mutex_lock(&nfsd_mutex); 302 nn = net_generic(netns(file), nfsd_net_id); 303 if (nn->nfsd_serv) 304 nfsd4_revoke_states(nn, path.dentry->d_sb); 305 else 306 error = -EINVAL; 307 mutex_unlock(&nfsd_mutex); 308 309 path_put(&path); 310 return error; 311 } 312 313 /* 314 * write_filehandle - Get a variable-length NFS file handle by path 315 * 316 * On input, the buffer contains a '\n'-terminated C string comprised of 317 * three alphanumeric words separated by whitespace. The string may 318 * contain escape sequences. 319 * 320 * Input: 321 * buf: 322 * domain: client domain name 323 * path: export pathname 324 * maxsize: numeric maximum size of 325 * @buf 326 * size: length of C string in @buf 327 * Output: 328 * On success: passed-in buffer filled with '\n'-terminated C 329 * string containing a ASCII hex text version 330 * of the NFS file handle; 331 * return code is the size in bytes of the string 332 * On error: return code is negative errno value 333 */ 334 static ssize_t write_filehandle(struct file *file, char *buf, size_t size) 335 { 336 char *dname, *path; 337 int maxsize; 338 char *mesg = buf; 339 int len; 340 struct auth_domain *dom; 341 struct knfsd_fh fh; 342 343 if (size == 0) 344 return -EINVAL; 345 346 if (buf[size-1] != '\n') 347 return -EINVAL; 348 buf[size-1] = 0; 349 350 dname = mesg; 351 len = qword_get(&mesg, dname, size); 352 if (len <= 0) 353 return -EINVAL; 354 355 path = dname+len+1; 356 len = qword_get(&mesg, path, size); 357 if (len <= 0) 358 return -EINVAL; 359 360 len = get_int(&mesg, &maxsize); 361 if (len) 362 return len; 363 364 if (maxsize < NFS_FHSIZE) 365 return -EINVAL; 366 maxsize = min(maxsize, NFS3_FHSIZE); 367 368 if (qword_get(&mesg, mesg, size) > 0) 369 return -EINVAL; 370 371 trace_nfsd_ctl_filehandle(netns(file), dname, path, maxsize); 372 373 /* we have all the words, they are in buf.. */ 374 dom = unix_domain_find(dname); 375 if (!dom) 376 return -ENOMEM; 377 378 len = exp_rootfh(netns(file), dom, path, &fh, maxsize); 379 auth_domain_put(dom); 380 if (len) 381 return len; 382 383 mesg = buf; 384 len = SIMPLE_TRANSACTION_LIMIT; 385 qword_addhex(&mesg, &len, fh.fh_raw, fh.fh_size); 386 mesg[-1] = '\n'; 387 return mesg - buf; 388 } 389 390 /* 391 * write_threads - Start NFSD, or report the configured number of threads 392 * 393 * Input: 394 * buf: ignored 395 * size: zero 396 * Output: 397 * On success: passed-in buffer filled with '\n'-terminated C 398 * string numeric value representing the configured 399 * number of NFSD threads; 400 * return code is the size in bytes of the string 401 * On error: return code is zero 402 * 403 * OR 404 * 405 * Input: 406 * buf: C string containing an unsigned 407 * integer value representing the 408 * number of NFSD threads to start 409 * size: non-zero length of C string in @buf 410 * Output: 411 * On success: NFS service is started; 412 * passed-in buffer filled with '\n'-terminated C 413 * string numeric value representing the configured 414 * number of NFSD threads; 415 * return code is the size in bytes of the string 416 * On error: return code is zero or a negative errno value 417 */ 418 static ssize_t write_threads(struct file *file, char *buf, size_t size) 419 { 420 char *mesg = buf; 421 int rv; 422 struct net *net = netns(file); 423 424 if (size > 0) { 425 int newthreads; 426 rv = get_int(&mesg, &newthreads); 427 if (rv) 428 return rv; 429 if (newthreads < 0) 430 return -EINVAL; 431 trace_nfsd_ctl_threads(net, newthreads); 432 mutex_lock(&nfsd_mutex); 433 rv = nfsd_svc(1, &newthreads, net, file->f_cred, NULL); 434 mutex_unlock(&nfsd_mutex); 435 if (rv < 0) 436 return rv; 437 } else 438 rv = nfsd_nrthreads(net); 439 440 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", rv); 441 } 442 443 /* 444 * write_pool_threads - Set or report the configured number of threads per pool 445 * 446 * Input: 447 * buf: ignored 448 * size: zero 449 * 450 * OR 451 * 452 * Input: 453 * buf: C string containing whitespace- 454 * separated unsigned integer values 455 * representing the number of NFSD 456 * threads to start in each pool 457 * size: non-zero length of C string in @buf 458 * Output: 459 * On success: passed-in buffer filled with '\n'-terminated C 460 * string containing integer values representing the 461 * configured number of NFSD threads in each pool; 462 * return code is the size in bytes of the string 463 * On error: return code is zero or a negative errno value 464 */ 465 static ssize_t write_pool_threads(struct file *file, char *buf, size_t size) 466 { 467 /* if size > 0, look for an array of number of threads per node 468 * and apply them then write out number of threads per node as reply 469 */ 470 char *mesg = buf; 471 int i; 472 int rv; 473 int len; 474 int npools; 475 int *nthreads; 476 struct net *net = netns(file); 477 478 mutex_lock(&nfsd_mutex); 479 npools = nfsd_nrpools(net); 480 if (npools == 0) { 481 /* 482 * NFS is shut down. The admin can start it by 483 * writing to the threads file but NOT the pool_threads 484 * file, sorry. Report zero threads. 485 */ 486 mutex_unlock(&nfsd_mutex); 487 strcpy(buf, "0\n"); 488 return strlen(buf); 489 } 490 491 nthreads = kzalloc_objs(int, npools); 492 rv = -ENOMEM; 493 if (nthreads == NULL) 494 goto out_free; 495 496 if (size > 0) { 497 for (i = 0; i < npools; i++) { 498 rv = get_int(&mesg, &nthreads[i]); 499 if (rv == -ENOENT) 500 break; /* fewer numbers than pools */ 501 if (rv) 502 goto out_free; /* syntax error */ 503 rv = -EINVAL; 504 if (nthreads[i] < 0) 505 goto out_free; 506 trace_nfsd_ctl_pool_threads(net, i, nthreads[i]); 507 } 508 509 /* 510 * There must always be a thread in pool 0; the admin 511 * can't shut down NFS completely using pool_threads. 512 */ 513 if (nthreads[0] == 0) 514 nthreads[0] = 1; 515 516 rv = nfsd_set_nrthreads(i, nthreads, net); 517 if (rv) 518 goto out_free; 519 } 520 521 rv = nfsd_get_nrthreads(npools, nthreads, net); 522 if (rv) 523 goto out_free; 524 525 mesg = buf; 526 size = SIMPLE_TRANSACTION_LIMIT; 527 for (i = 0; i < npools && size > 0; i++) { 528 snprintf(mesg, size, "%d%c", nthreads[i], (i == npools-1 ? '\n' : ' ')); 529 len = strlen(mesg); 530 size -= len; 531 mesg += len; 532 } 533 rv = mesg - buf; 534 out_free: 535 kfree(nthreads); 536 mutex_unlock(&nfsd_mutex); 537 return rv; 538 } 539 540 static ssize_t 541 nfsd_print_version_support(struct nfsd_net *nn, char *buf, int remaining, 542 const char *sep, unsigned vers, int minor) 543 { 544 const char *format = minor < 0 ? "%s%c%u" : "%s%c%u.%u"; 545 bool supported = !!nfsd_vers(nn, vers, NFSD_TEST); 546 547 if (vers == 4 && minor >= 0 && 548 !nfsd_minorversion(nn, minor, NFSD_TEST)) 549 supported = false; 550 if (minor == 0 && supported) 551 /* 552 * special case for backward compatability. 553 * +4.0 is never reported, it is implied by 554 * +4, unless -4.0 is present. 555 */ 556 return 0; 557 return snprintf(buf, remaining, format, sep, 558 supported ? '+' : '-', vers, minor); 559 } 560 561 static ssize_t __write_versions(struct file *file, char *buf, size_t size) 562 { 563 char *mesg = buf; 564 char *vers, *minorp, sign; 565 int len, num, remaining; 566 ssize_t tlen = 0; 567 char *sep; 568 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id); 569 570 if (size > 0) { 571 if (nn->nfsd_serv) 572 /* Cannot change versions without updating 573 * nn->nfsd_serv->sv_xdrsize, and reallocing 574 * rq_argp and rq_resp 575 */ 576 return -EBUSY; 577 if (buf[size-1] != '\n') 578 return -EINVAL; 579 buf[size-1] = 0; 580 trace_nfsd_ctl_version(netns(file), buf); 581 582 vers = mesg; 583 len = qword_get(&mesg, vers, size); 584 if (len <= 0) return -EINVAL; 585 do { 586 enum vers_op cmd; 587 unsigned minor; 588 sign = *vers; 589 if (sign == '+' || sign == '-') 590 num = simple_strtol((vers+1), &minorp, 0); 591 else 592 num = simple_strtol(vers, &minorp, 0); 593 if (*minorp == '.') { 594 if (num != 4) 595 return -EINVAL; 596 if (kstrtouint(minorp+1, 0, &minor) < 0) 597 return -EINVAL; 598 } 599 600 cmd = sign == '-' ? NFSD_CLEAR : NFSD_SET; 601 switch(num) { 602 #ifdef CONFIG_NFSD_V2 603 case 2: 604 #endif 605 case 3: 606 nfsd_vers(nn, num, cmd); 607 break; 608 case 4: 609 if (*minorp == '.') { 610 if (nfsd_minorversion(nn, minor, cmd) < 0) 611 return -EINVAL; 612 } else if ((cmd == NFSD_SET) != nfsd_vers(nn, num, NFSD_TEST)) { 613 /* 614 * Either we have +4 and no minors are enabled, 615 * or we have -4 and at least one minor is enabled. 616 * In either case, propagate 'cmd' to all minors. 617 */ 618 minor = 0; 619 while (nfsd_minorversion(nn, minor, cmd) >= 0) 620 minor++; 621 } 622 break; 623 default: 624 /* Ignore requests to disable non-existent versions */ 625 if (cmd == NFSD_SET) 626 return -EINVAL; 627 } 628 vers += len + 1; 629 } while ((len = qword_get(&mesg, vers, size)) > 0); 630 /* If all get turned off, turn them back on, as 631 * having no versions is BAD 632 */ 633 nfsd_reset_versions(nn); 634 } 635 636 /* Now write current state into reply buffer */ 637 sep = ""; 638 remaining = SIMPLE_TRANSACTION_LIMIT; 639 for (num=2 ; num <= 4 ; num++) { 640 int minor; 641 if (!nfsd_vers(nn, num, NFSD_AVAIL)) 642 continue; 643 644 minor = -1; 645 do { 646 len = nfsd_print_version_support(nn, buf, remaining, 647 sep, num, minor); 648 if (len >= remaining) 649 goto out; 650 remaining -= len; 651 buf += len; 652 tlen += len; 653 minor++; 654 if (len) 655 sep = " "; 656 } while (num == 4 && minor <= NFSD_SUPPORTED_MINOR_VERSION); 657 } 658 out: 659 len = snprintf(buf, remaining, "\n"); 660 if (len >= remaining) 661 return -EINVAL; 662 return tlen + len; 663 } 664 665 /* 666 * write_versions - Set or report the available NFS protocol versions 667 * 668 * Input: 669 * buf: ignored 670 * size: zero 671 * Output: 672 * On success: passed-in buffer filled with '\n'-terminated C 673 * string containing positive or negative integer 674 * values representing the current status of each 675 * protocol version; 676 * return code is the size in bytes of the string 677 * On error: return code is zero or a negative errno value 678 * 679 * OR 680 * 681 * Input: 682 * buf: C string containing whitespace- 683 * separated positive or negative 684 * integer values representing NFS 685 * protocol versions to enable ("+n") 686 * or disable ("-n") 687 * size: non-zero length of C string in @buf 688 * Output: 689 * On success: status of zero or more protocol versions has 690 * been updated; passed-in buffer filled with 691 * '\n'-terminated C string containing positive 692 * or negative integer values representing the 693 * current status of each protocol version; 694 * return code is the size in bytes of the string 695 * On error: return code is zero or a negative errno value 696 */ 697 static ssize_t write_versions(struct file *file, char *buf, size_t size) 698 { 699 ssize_t rv; 700 701 mutex_lock(&nfsd_mutex); 702 rv = __write_versions(file, buf, size); 703 mutex_unlock(&nfsd_mutex); 704 return rv; 705 } 706 707 /* 708 * Zero-length write. Return a list of NFSD's current listener 709 * transports. 710 */ 711 static ssize_t __write_ports_names(char *buf, struct net *net) 712 { 713 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 714 715 if (nn->nfsd_serv == NULL) 716 return 0; 717 return svc_xprt_names(nn->nfsd_serv, buf, SIMPLE_TRANSACTION_LIMIT); 718 } 719 720 /* 721 * A single 'fd' number was written, in which case it must be for 722 * a socket of a supported family/protocol, and we use it as an 723 * nfsd listener. 724 */ 725 static ssize_t __write_ports_addfd(char *buf, struct net *net, const struct cred *cred) 726 { 727 char *mesg = buf; 728 int fd, err; 729 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 730 struct svc_serv *serv; 731 732 err = get_int(&mesg, &fd); 733 if (err != 0 || fd < 0) 734 return -EINVAL; 735 trace_nfsd_ctl_ports_addfd(net, fd); 736 737 err = nfsd_create_serv(net); 738 if (err != 0) 739 return err; 740 741 serv = nn->nfsd_serv; 742 err = svc_addsock(serv, net, fd, buf, SIMPLE_TRANSACTION_LIMIT, cred); 743 744 if (!serv->sv_nrthreads && list_empty(&nn->nfsd_serv->sv_permsocks)) 745 nfsd_destroy_serv(net); 746 747 return err; 748 } 749 750 /* 751 * A transport listener is added by writing its transport name and 752 * a port number. 753 */ 754 static ssize_t __write_ports_addxprt(char *buf, struct net *net, const struct cred *cred) 755 { 756 char transport[16]; 757 struct svc_xprt *xprt; 758 int port, err; 759 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 760 struct svc_serv *serv; 761 762 if (sscanf(buf, "%15s %5u", transport, &port) != 2) 763 return -EINVAL; 764 765 if (port < 1 || port > USHRT_MAX) 766 return -EINVAL; 767 trace_nfsd_ctl_ports_addxprt(net, transport, port); 768 769 err = nfsd_create_serv(net); 770 if (err != 0) 771 return err; 772 773 serv = nn->nfsd_serv; 774 err = svc_xprt_create(serv, transport, net, 775 PF_INET, port, SVC_SOCK_ANONYMOUS, cred); 776 if (err < 0) 777 goto out_err; 778 779 err = svc_xprt_create(serv, transport, net, 780 PF_INET6, port, SVC_SOCK_ANONYMOUS, cred); 781 if (err < 0 && err != -EAFNOSUPPORT) 782 goto out_close; 783 784 return 0; 785 out_close: 786 xprt = svc_find_xprt(serv, transport, net, PF_INET, port); 787 if (xprt != NULL) { 788 svc_xprt_close(xprt); 789 svc_xprt_put(xprt); 790 } 791 out_err: 792 if (!serv->sv_nrthreads && list_empty(&nn->nfsd_serv->sv_permsocks)) 793 nfsd_destroy_serv(net); 794 795 return err; 796 } 797 798 static ssize_t __write_ports(struct file *file, char *buf, size_t size, 799 struct net *net) 800 { 801 if (size == 0) 802 return __write_ports_names(buf, net); 803 804 if (isdigit(buf[0])) 805 return __write_ports_addfd(buf, net, file->f_cred); 806 807 if (isalpha(buf[0])) 808 return __write_ports_addxprt(buf, net, file->f_cred); 809 810 return -EINVAL; 811 } 812 813 /* 814 * write_ports - Pass a socket file descriptor or transport name to listen on 815 * 816 * Input: 817 * buf: ignored 818 * size: zero 819 * Output: 820 * On success: passed-in buffer filled with a '\n'-terminated C 821 * string containing a whitespace-separated list of 822 * named NFSD listeners; 823 * return code is the size in bytes of the string 824 * On error: return code is zero or a negative errno value 825 * 826 * OR 827 * 828 * Input: 829 * buf: C string containing an unsigned 830 * integer value representing a bound 831 * but unconnected socket that is to be 832 * used as an NFSD listener; listen(3) 833 * must be called for a SOCK_STREAM 834 * socket, otherwise it is ignored 835 * size: non-zero length of C string in @buf 836 * Output: 837 * On success: NFS service is started; 838 * passed-in buffer filled with a '\n'-terminated C 839 * string containing a unique alphanumeric name of 840 * the listener; 841 * return code is the size in bytes of the string 842 * On error: return code is a negative errno value 843 * 844 * OR 845 * 846 * Input: 847 * buf: C string containing a transport 848 * name and an unsigned integer value 849 * representing the port to listen on, 850 * separated by whitespace 851 * size: non-zero length of C string in @buf 852 * Output: 853 * On success: returns zero; NFS service is started 854 * On error: return code is a negative errno value 855 */ 856 static ssize_t write_ports(struct file *file, char *buf, size_t size) 857 { 858 ssize_t rv; 859 860 mutex_lock(&nfsd_mutex); 861 rv = __write_ports(file, buf, size, netns(file)); 862 mutex_unlock(&nfsd_mutex); 863 return rv; 864 } 865 866 867 int nfsd_max_blksize; 868 869 /* 870 * write_maxblksize - Set or report the current NFS blksize 871 * 872 * Input: 873 * buf: ignored 874 * size: zero 875 * 876 * OR 877 * 878 * Input: 879 * buf: C string containing an unsigned 880 * integer value representing the new 881 * NFS blksize 882 * size: non-zero length of C string in @buf 883 * Output: 884 * On success: passed-in buffer filled with '\n'-terminated C string 885 * containing numeric value of the current NFS blksize 886 * setting; 887 * return code is the size in bytes of the string 888 * On error: return code is zero or a negative errno value 889 */ 890 static ssize_t write_maxblksize(struct file *file, char *buf, size_t size) 891 { 892 char *mesg = buf; 893 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id); 894 895 if (size > 0) { 896 int bsize; 897 int rv = get_int(&mesg, &bsize); 898 if (rv) 899 return rv; 900 trace_nfsd_ctl_maxblksize(netns(file), bsize); 901 902 /* force bsize into allowed range and 903 * required alignment. 904 */ 905 bsize = max_t(int, bsize, 1024); 906 bsize = min_t(int, bsize, NFSSVC_MAXBLKSIZE); 907 bsize &= ~(1024-1); 908 mutex_lock(&nfsd_mutex); 909 if (nn->nfsd_serv) { 910 mutex_unlock(&nfsd_mutex); 911 return -EBUSY; 912 } 913 nfsd_max_blksize = bsize; 914 mutex_unlock(&nfsd_mutex); 915 } 916 917 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", 918 nfsd_max_blksize); 919 } 920 921 #ifdef CONFIG_NFSD_V4 922 static ssize_t __nfsd4_write_time(struct file *file, char *buf, size_t size, 923 time64_t *time, struct nfsd_net *nn) 924 { 925 struct dentry *dentry = file_dentry(file); 926 char *mesg = buf; 927 int rv, i; 928 929 if (size > 0) { 930 if (nn->nfsd_serv) 931 return -EBUSY; 932 rv = get_int(&mesg, &i); 933 if (rv) 934 return rv; 935 trace_nfsd_ctl_time(netns(file), dentry->d_name.name, 936 dentry->d_name.len, i); 937 938 /* 939 * Some sanity checking. We don't have a reason for 940 * these particular numbers, but problems with the 941 * extremes are: 942 * - Too short: the briefest network outage may 943 * cause clients to lose all their locks. Also, 944 * the frequent polling may be wasteful. 945 * - Too long: do you really want reboot recovery 946 * to take more than an hour? Or to make other 947 * clients wait an hour before being able to 948 * revoke a dead client's locks? 949 */ 950 if (i < 10 || i > 3600) 951 return -EINVAL; 952 *time = i; 953 } 954 955 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%lld\n", *time); 956 } 957 958 static ssize_t nfsd4_write_time(struct file *file, char *buf, size_t size, 959 time64_t *time, struct nfsd_net *nn) 960 { 961 ssize_t rv; 962 963 mutex_lock(&nfsd_mutex); 964 rv = __nfsd4_write_time(file, buf, size, time, nn); 965 mutex_unlock(&nfsd_mutex); 966 return rv; 967 } 968 969 /* 970 * write_leasetime - Set or report the current NFSv4 lease time 971 * 972 * Input: 973 * buf: ignored 974 * size: zero 975 * 976 * OR 977 * 978 * Input: 979 * buf: C string containing an unsigned 980 * integer value representing the new 981 * NFSv4 lease expiry time 982 * size: non-zero length of C string in @buf 983 * Output: 984 * On success: passed-in buffer filled with '\n'-terminated C 985 * string containing unsigned integer value of the 986 * current lease expiry time; 987 * return code is the size in bytes of the string 988 * On error: return code is zero or a negative errno value 989 */ 990 static ssize_t write_leasetime(struct file *file, char *buf, size_t size) 991 { 992 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id); 993 return nfsd4_write_time(file, buf, size, &nn->nfsd4_lease, nn); 994 } 995 996 /* 997 * write_gracetime - Set or report current NFSv4 grace period time 998 * 999 * As above, but sets the time of the NFSv4 grace period. 1000 * 1001 * Note this should never be set to less than the *previous* 1002 * lease-period time, but we don't try to enforce this. (In the common 1003 * case (a new boot), we don't know what the previous lease time was 1004 * anyway.) 1005 */ 1006 static ssize_t write_gracetime(struct file *file, char *buf, size_t size) 1007 { 1008 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id); 1009 return nfsd4_write_time(file, buf, size, &nn->nfsd4_grace, nn); 1010 } 1011 1012 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING 1013 static ssize_t __write_recoverydir(struct file *file, char *buf, size_t size, 1014 struct nfsd_net *nn) 1015 { 1016 char *mesg = buf; 1017 char *recdir; 1018 int len, status; 1019 1020 if (size > 0) { 1021 if (nn->nfsd_serv) 1022 return -EBUSY; 1023 if (size > PATH_MAX || buf[size-1] != '\n') 1024 return -EINVAL; 1025 buf[size-1] = 0; 1026 1027 recdir = mesg; 1028 len = qword_get(&mesg, recdir, size); 1029 if (len <= 0) 1030 return -EINVAL; 1031 trace_nfsd_ctl_recoverydir(netns(file), recdir); 1032 1033 status = nfs4_reset_recoverydir(recdir); 1034 if (status) 1035 return status; 1036 } 1037 1038 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%s\n", 1039 nfs4_recoverydir()); 1040 } 1041 1042 /* 1043 * write_recoverydir - Set or report the pathname of the recovery directory 1044 * 1045 * Input: 1046 * buf: ignored 1047 * size: zero 1048 * 1049 * OR 1050 * 1051 * Input: 1052 * buf: C string containing the pathname 1053 * of the directory on a local file 1054 * system containing permanent NFSv4 1055 * recovery data 1056 * size: non-zero length of C string in @buf 1057 * Output: 1058 * On success: passed-in buffer filled with '\n'-terminated C string 1059 * containing the current recovery pathname setting; 1060 * return code is the size in bytes of the string 1061 * On error: return code is zero or a negative errno value 1062 */ 1063 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size) 1064 { 1065 ssize_t rv; 1066 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id); 1067 1068 mutex_lock(&nfsd_mutex); 1069 rv = __write_recoverydir(file, buf, size, nn); 1070 mutex_unlock(&nfsd_mutex); 1071 return rv; 1072 } 1073 #endif 1074 1075 /* 1076 * write_v4_end_grace - release grace period for nfsd's v4.x lock manager 1077 * 1078 * Input: 1079 * buf: ignored 1080 * size: zero 1081 * OR 1082 * 1083 * Input: 1084 * buf: any value 1085 * size: non-zero length of C string in @buf 1086 * Output: 1087 * passed-in buffer filled with "Y" or "N" with a newline 1088 * and NULL-terminated C string. This indicates whether 1089 * the grace period has ended in the current net 1090 * namespace. Return code is the size in bytes of the 1091 * string. Writing a string that starts with 'Y', 'y', or 1092 * '1' to the file will end the grace period for nfsd's v4 1093 * lock manager. 1094 */ 1095 static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size) 1096 { 1097 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id); 1098 1099 if (size > 0) { 1100 switch(buf[0]) { 1101 case 'Y': 1102 case 'y': 1103 case '1': 1104 if (!nfsd4_force_end_grace(nn)) 1105 return -EBUSY; 1106 trace_nfsd_end_grace(netns(file)); 1107 break; 1108 default: 1109 return -EINVAL; 1110 } 1111 } 1112 1113 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%c\n", 1114 nn->grace_ended ? 'Y' : 'N'); 1115 } 1116 1117 #endif 1118 1119 /*----------------------------------------------------------------------------*/ 1120 /* 1121 * populating the filesystem. 1122 */ 1123 1124 static struct inode *nfsd_get_inode(struct super_block *sb, umode_t mode) 1125 { 1126 struct inode *inode = new_inode(sb); 1127 if (inode) { 1128 /* Following advice from simple_fill_super documentation: */ 1129 inode->i_ino = iunique(sb, NFSD_MaxReserved); 1130 inode->i_mode = mode; 1131 simple_inode_init_ts(inode); 1132 } 1133 return inode; 1134 } 1135 1136 static struct dentry *nfsd_mkdir(struct dentry *parent, struct nfsdfs_client *ncl, char *name) 1137 { 1138 struct inode *dir = parent->d_inode; 1139 struct dentry *dentry; 1140 struct inode *inode; 1141 1142 inode = nfsd_get_inode(parent->d_sb, S_IFDIR | 0600); 1143 if (!inode) 1144 return ERR_PTR(-ENOMEM); 1145 1146 dentry = simple_start_creating(parent, name); 1147 if (IS_ERR(dentry)) { 1148 iput(inode); 1149 return dentry; 1150 } 1151 inode->i_fop = &simple_dir_operations; 1152 inode->i_op = &simple_dir_inode_operations; 1153 inc_nlink(inode); 1154 if (ncl) { 1155 inode->i_private = ncl; 1156 kref_get(&ncl->cl_ref); 1157 } 1158 d_make_persistent(dentry, inode); 1159 inc_nlink(dir); 1160 fsnotify_mkdir(dir, dentry); 1161 simple_done_creating(dentry); 1162 return dentry; // borrowed 1163 } 1164 1165 #if IS_ENABLED(CONFIG_SUNRPC_GSS) 1166 /* 1167 * @content is assumed to be a NUL-terminated string that lives 1168 * longer than the symlink itself. 1169 */ 1170 static void _nfsd_symlink(struct dentry *parent, const char *name, 1171 const char *content) 1172 { 1173 struct inode *dir = parent->d_inode; 1174 struct inode *inode; 1175 struct dentry *dentry; 1176 1177 inode = nfsd_get_inode(dir->i_sb, S_IFLNK | 0777); 1178 if (!inode) 1179 return; 1180 1181 dentry = simple_start_creating(parent, name); 1182 if (IS_ERR(dentry)) { 1183 iput(inode); 1184 return; 1185 } 1186 1187 inode->i_op = &simple_symlink_inode_operations; 1188 inode->i_link = (char *)content; 1189 inode->i_size = strlen(content); 1190 1191 d_make_persistent(dentry, inode); 1192 fsnotify_create(dir, dentry); 1193 simple_done_creating(dentry); 1194 } 1195 #else 1196 static inline void _nfsd_symlink(struct dentry *parent, const char *name, 1197 const char *content) 1198 { 1199 } 1200 1201 #endif 1202 1203 static void clear_ncl(struct dentry *dentry) 1204 { 1205 struct inode *inode = d_inode(dentry); 1206 struct nfsdfs_client *ncl = inode->i_private; 1207 1208 spin_lock(&inode->i_lock); 1209 inode->i_private = NULL; 1210 spin_unlock(&inode->i_lock); 1211 kref_put(&ncl->cl_ref, ncl->cl_release); 1212 } 1213 1214 struct nfsdfs_client *get_nfsdfs_client(struct inode *inode) 1215 { 1216 struct nfsdfs_client *nc; 1217 1218 spin_lock(&inode->i_lock); 1219 nc = inode->i_private; 1220 if (nc) 1221 kref_get(&nc->cl_ref); 1222 spin_unlock(&inode->i_lock); 1223 return nc; 1224 } 1225 1226 /* XXX: cut'n'paste from simple_fill_super; figure out if we could share 1227 * code instead. */ 1228 static int nfsdfs_create_files(struct dentry *root, 1229 const struct tree_descr *files, 1230 struct nfsdfs_client *ncl, 1231 struct dentry **fdentries) 1232 { 1233 struct inode *dir = d_inode(root); 1234 struct dentry *dentry; 1235 1236 for (int i = 0; files->name && files->name[0]; i++, files++) { 1237 struct inode *inode = nfsd_get_inode(root->d_sb, 1238 S_IFREG | files->mode); 1239 if (!inode) 1240 return -ENOMEM; 1241 dentry = simple_start_creating(root, files->name); 1242 if (IS_ERR(dentry)) { 1243 iput(inode); 1244 return PTR_ERR(dentry); 1245 } 1246 kref_get(&ncl->cl_ref); 1247 inode->i_fop = files->ops; 1248 inode->i_private = ncl; 1249 d_make_persistent(dentry, inode); 1250 fsnotify_create(dir, dentry); 1251 if (fdentries) 1252 fdentries[i] = dentry; // borrowed 1253 simple_done_creating(dentry); 1254 } 1255 return 0; 1256 } 1257 1258 /* on success, returns positive number unique to that client. */ 1259 struct dentry *nfsd_client_mkdir(struct nfsd_net *nn, 1260 struct nfsdfs_client *ncl, u32 id, 1261 const struct tree_descr *files, 1262 struct dentry **fdentries) 1263 { 1264 struct dentry *dentry; 1265 char name[11]; 1266 int ret; 1267 1268 sprintf(name, "%u", id); 1269 1270 dentry = nfsd_mkdir(nn->nfsd_client_dir, ncl, name); 1271 if (IS_ERR(dentry)) /* XXX: tossing errors? */ 1272 return NULL; 1273 ret = nfsdfs_create_files(dentry, files, ncl, fdentries); 1274 if (ret) { 1275 nfsd_client_rmdir(dentry); 1276 return NULL; 1277 } 1278 return dentry; 1279 } 1280 1281 /* Taken from __rpc_rmdir: */ 1282 void nfsd_client_rmdir(struct dentry *dentry) 1283 { 1284 simple_recursive_removal(dentry, clear_ncl); 1285 } 1286 1287 static int nfsd_fill_super(struct super_block *sb, struct fs_context *fc) 1288 { 1289 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, 1290 nfsd_net_id); 1291 struct dentry *dentry; 1292 int ret; 1293 1294 static const struct tree_descr nfsd_files[] = { 1295 [NFSD_List] = {"exports", &exports_nfsd_operations, S_IRUGO}, 1296 /* Per-export io stats use same ops as exports file */ 1297 [NFSD_Export_Stats] = {"export_stats", &exports_nfsd_operations, S_IRUGO}, 1298 [NFSD_Export_features] = {"export_features", 1299 &export_features_fops, S_IRUGO}, 1300 [NFSD_FO_UnlockIP] = {"unlock_ip", 1301 &transaction_ops, S_IWUSR|S_IRUSR}, 1302 [NFSD_FO_UnlockFS] = {"unlock_filesystem", 1303 &transaction_ops, S_IWUSR|S_IRUSR}, 1304 [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR}, 1305 [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR}, 1306 [NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR}, 1307 [NFSD_Pool_Stats] = {"pool_stats", &pool_stats_operations, S_IRUGO}, 1308 [NFSD_Reply_Cache_Stats] = {"reply_cache_stats", 1309 &nfsd_reply_cache_stats_fops, S_IRUGO}, 1310 [NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR}, 1311 [NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO}, 1312 [NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO}, 1313 [NFSD_Filecache] = {"filecache", &nfsd_file_cache_stats_fops, S_IRUGO}, 1314 #ifdef CONFIG_NFSD_V4 1315 [NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR}, 1316 [NFSD_Gracetime] = {"nfsv4gracetime", &transaction_ops, S_IWUSR|S_IRUSR}, 1317 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING 1318 [NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR}, 1319 #endif 1320 [NFSD_V4EndGrace] = {"v4_end_grace", &transaction_ops, S_IWUSR|S_IRUGO}, 1321 #endif 1322 /* last one */ {""} 1323 }; 1324 1325 ret = simple_fill_super(sb, 0x6e667364, nfsd_files); 1326 if (ret) 1327 return ret; 1328 _nfsd_symlink(sb->s_root, "supported_krb5_enctypes", 1329 "/proc/net/rpc/gss_krb5_enctypes"); 1330 dentry = nfsd_mkdir(sb->s_root, NULL, "clients"); 1331 if (IS_ERR(dentry)) 1332 return PTR_ERR(dentry); 1333 nn->nfsd_client_dir = dentry; 1334 return 0; 1335 } 1336 1337 static int nfsd_fs_get_tree(struct fs_context *fc) 1338 { 1339 return get_tree_keyed(fc, nfsd_fill_super, get_net(fc->net_ns)); 1340 } 1341 1342 static void nfsd_fs_free_fc(struct fs_context *fc) 1343 { 1344 if (fc->s_fs_info) 1345 put_net(fc->s_fs_info); 1346 } 1347 1348 static const struct fs_context_operations nfsd_fs_context_ops = { 1349 .free = nfsd_fs_free_fc, 1350 .get_tree = nfsd_fs_get_tree, 1351 }; 1352 1353 static int nfsd_init_fs_context(struct fs_context *fc) 1354 { 1355 put_user_ns(fc->user_ns); 1356 fc->user_ns = get_user_ns(fc->net_ns->user_ns); 1357 fc->ops = &nfsd_fs_context_ops; 1358 return 0; 1359 } 1360 1361 static void nfsd_umount(struct super_block *sb) 1362 { 1363 struct net *net = sb->s_fs_info; 1364 1365 nfsd_shutdown_threads(net); 1366 1367 kill_anon_super(sb); 1368 put_net(net); 1369 } 1370 1371 static struct file_system_type nfsd_fs_type = { 1372 .owner = THIS_MODULE, 1373 .name = "nfsd", 1374 .init_fs_context = nfsd_init_fs_context, 1375 .kill_sb = nfsd_umount, 1376 }; 1377 MODULE_ALIAS_FS("nfsd"); 1378 1379 #ifdef CONFIG_PROC_FS 1380 1381 static int exports_proc_open(struct inode *inode, struct file *file) 1382 { 1383 return exports_net_open(current->nsproxy->net_ns, file); 1384 } 1385 1386 static const struct proc_ops exports_proc_ops = { 1387 .proc_open = exports_proc_open, 1388 .proc_read = seq_read, 1389 .proc_lseek = seq_lseek, 1390 .proc_release = exports_release, 1391 }; 1392 1393 static int create_proc_exports_entry(void) 1394 { 1395 struct proc_dir_entry *entry; 1396 1397 entry = proc_mkdir("fs/nfs", NULL); 1398 if (!entry) 1399 return -ENOMEM; 1400 entry = proc_create("exports", 0, entry, &exports_proc_ops); 1401 if (!entry) { 1402 remove_proc_entry("fs/nfs", NULL); 1403 return -ENOMEM; 1404 } 1405 return 0; 1406 } 1407 #else /* CONFIG_PROC_FS */ 1408 static int create_proc_exports_entry(void) 1409 { 1410 return 0; 1411 } 1412 #endif 1413 1414 unsigned int nfsd_net_id; 1415 1416 struct nfsd_genl_rqstp { 1417 struct sockaddr rq_daddr; 1418 struct sockaddr rq_saddr; 1419 unsigned long rq_flags; 1420 ktime_t rq_stime; 1421 __be32 rq_xid; 1422 u32 rq_vers; 1423 u32 rq_prog; 1424 u32 rq_proc; 1425 1426 /* NFSv4 compound */ 1427 u32 rq_opcnt; 1428 u32 rq_opnum[16]; 1429 }; 1430 1431 static int nfsd_genl_rpc_status_compose_msg(struct sk_buff *skb, 1432 struct netlink_callback *cb, 1433 struct nfsd_genl_rqstp *genl_rqstp) 1434 { 1435 void *hdr; 1436 u32 i; 1437 1438 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, 1439 &nfsd_nl_family, 0, NFSD_CMD_RPC_STATUS_GET); 1440 if (!hdr) 1441 return -ENOBUFS; 1442 1443 if (nla_put_be32(skb, NFSD_A_RPC_STATUS_XID, genl_rqstp->rq_xid) || 1444 nla_put_u32(skb, NFSD_A_RPC_STATUS_FLAGS, genl_rqstp->rq_flags) || 1445 nla_put_u32(skb, NFSD_A_RPC_STATUS_PROG, genl_rqstp->rq_prog) || 1446 nla_put_u32(skb, NFSD_A_RPC_STATUS_PROC, genl_rqstp->rq_proc) || 1447 nla_put_u8(skb, NFSD_A_RPC_STATUS_VERSION, genl_rqstp->rq_vers) || 1448 nla_put_s64(skb, NFSD_A_RPC_STATUS_SERVICE_TIME, 1449 ktime_to_us(genl_rqstp->rq_stime), 1450 NFSD_A_RPC_STATUS_PAD)) 1451 return -ENOBUFS; 1452 1453 switch (genl_rqstp->rq_saddr.sa_family) { 1454 case AF_INET: { 1455 const struct sockaddr_in *s_in, *d_in; 1456 1457 s_in = (const struct sockaddr_in *)&genl_rqstp->rq_saddr; 1458 d_in = (const struct sockaddr_in *)&genl_rqstp->rq_daddr; 1459 if (nla_put_in_addr(skb, NFSD_A_RPC_STATUS_SADDR4, 1460 s_in->sin_addr.s_addr) || 1461 nla_put_in_addr(skb, NFSD_A_RPC_STATUS_DADDR4, 1462 d_in->sin_addr.s_addr) || 1463 nla_put_be16(skb, NFSD_A_RPC_STATUS_SPORT, 1464 s_in->sin_port) || 1465 nla_put_be16(skb, NFSD_A_RPC_STATUS_DPORT, 1466 d_in->sin_port)) 1467 return -ENOBUFS; 1468 break; 1469 } 1470 case AF_INET6: { 1471 const struct sockaddr_in6 *s_in, *d_in; 1472 1473 s_in = (const struct sockaddr_in6 *)&genl_rqstp->rq_saddr; 1474 d_in = (const struct sockaddr_in6 *)&genl_rqstp->rq_daddr; 1475 if (nla_put_in6_addr(skb, NFSD_A_RPC_STATUS_SADDR6, 1476 &s_in->sin6_addr) || 1477 nla_put_in6_addr(skb, NFSD_A_RPC_STATUS_DADDR6, 1478 &d_in->sin6_addr) || 1479 nla_put_be16(skb, NFSD_A_RPC_STATUS_SPORT, 1480 s_in->sin6_port) || 1481 nla_put_be16(skb, NFSD_A_RPC_STATUS_DPORT, 1482 d_in->sin6_port)) 1483 return -ENOBUFS; 1484 break; 1485 } 1486 } 1487 1488 for (i = 0; i < genl_rqstp->rq_opcnt; i++) 1489 if (nla_put_u32(skb, NFSD_A_RPC_STATUS_COMPOUND_OPS, 1490 genl_rqstp->rq_opnum[i])) 1491 return -ENOBUFS; 1492 1493 genlmsg_end(skb, hdr); 1494 return 0; 1495 } 1496 1497 /** 1498 * nfsd_nl_rpc_status_get_dumpit - Handle rpc_status_get dumpit 1499 * @skb: reply buffer 1500 * @cb: netlink metadata and command arguments 1501 * 1502 * Returns the size of the reply or a negative errno. 1503 */ 1504 int nfsd_nl_rpc_status_get_dumpit(struct sk_buff *skb, 1505 struct netlink_callback *cb) 1506 { 1507 int i, ret, rqstp_index = 0; 1508 struct nfsd_net *nn; 1509 1510 mutex_lock(&nfsd_mutex); 1511 1512 nn = net_generic(sock_net(skb->sk), nfsd_net_id); 1513 if (!nn->nfsd_serv) { 1514 ret = -ENODEV; 1515 goto out_unlock; 1516 } 1517 1518 rcu_read_lock(); 1519 1520 for (i = 0; i < nn->nfsd_serv->sv_nrpools; i++) { 1521 struct svc_rqst *rqstp; 1522 1523 if (i < cb->args[0]) /* already consumed */ 1524 continue; 1525 1526 rqstp_index = 0; 1527 list_for_each_entry_rcu(rqstp, 1528 &nn->nfsd_serv->sv_pools[i].sp_all_threads, 1529 rq_all) { 1530 struct nfsd_genl_rqstp genl_rqstp; 1531 unsigned int status_counter; 1532 1533 if (rqstp_index++ < cb->args[1]) /* already consumed */ 1534 continue; 1535 /* 1536 * Acquire rq_status_counter before parsing the rqst 1537 * fields. rq_status_counter is set to an odd value in 1538 * order to notify the consumers the rqstp fields are 1539 * meaningful. 1540 */ 1541 status_counter = 1542 smp_load_acquire(&rqstp->rq_status_counter); 1543 if (!(status_counter & 1)) 1544 continue; 1545 1546 genl_rqstp.rq_xid = rqstp->rq_xid; 1547 genl_rqstp.rq_flags = rqstp->rq_flags; 1548 genl_rqstp.rq_vers = rqstp->rq_vers; 1549 genl_rqstp.rq_prog = rqstp->rq_prog; 1550 genl_rqstp.rq_proc = rqstp->rq_proc; 1551 genl_rqstp.rq_stime = rqstp->rq_stime; 1552 genl_rqstp.rq_opcnt = 0; 1553 memcpy(&genl_rqstp.rq_daddr, svc_daddr(rqstp), 1554 sizeof(struct sockaddr)); 1555 memcpy(&genl_rqstp.rq_saddr, svc_addr(rqstp), 1556 sizeof(struct sockaddr)); 1557 1558 #ifdef CONFIG_NFSD_V4 1559 if (rqstp->rq_vers == NFS4_VERSION && 1560 rqstp->rq_proc == NFSPROC4_COMPOUND) { 1561 /* NFSv4 compound */ 1562 struct nfsd4_compoundargs *args; 1563 int j; 1564 1565 args = rqstp->rq_argp; 1566 genl_rqstp.rq_opcnt = min_t(u32, args->opcnt, 1567 ARRAY_SIZE(genl_rqstp.rq_opnum)); 1568 for (j = 0; j < genl_rqstp.rq_opcnt; j++) 1569 genl_rqstp.rq_opnum[j] = 1570 args->ops[j].opnum; 1571 } 1572 #endif /* CONFIG_NFSD_V4 */ 1573 1574 /* 1575 * Acquire rq_status_counter before reporting the rqst 1576 * fields to the user. 1577 */ 1578 if (smp_load_acquire(&rqstp->rq_status_counter) != 1579 status_counter) 1580 continue; 1581 1582 ret = nfsd_genl_rpc_status_compose_msg(skb, cb, 1583 &genl_rqstp); 1584 if (ret) 1585 goto out; 1586 } 1587 } 1588 1589 cb->args[0] = i; 1590 cb->args[1] = rqstp_index; 1591 ret = skb->len; 1592 out: 1593 rcu_read_unlock(); 1594 out_unlock: 1595 mutex_unlock(&nfsd_mutex); 1596 1597 return ret; 1598 } 1599 1600 /** 1601 * nfsd_nl_fh_key_set - helper to copy fh_key from userspace 1602 * @attr: nlattr NFSD_A_SERVER_FH_KEY 1603 * @nn: nfsd_net 1604 * 1605 * Callers should hold nfsd_mutex, returns 0 on success or negative errno. 1606 * Callers must ensure the server is shut down (sv_nrthreads == 0), 1607 * userspace documentation asserts the key may only be set when the server 1608 * is not running. 1609 */ 1610 static int nfsd_nl_fh_key_set(const struct nlattr *attr, struct nfsd_net *nn) 1611 { 1612 siphash_key_t *fh_key = nn->fh_key; 1613 u64 k0, k1; 1614 bool changed; 1615 1616 k0 = get_unaligned_le64(nla_data(attr)); 1617 k1 = get_unaligned_le64(nla_data(attr) + 8); 1618 1619 if (!fh_key) { 1620 fh_key = kmalloc(sizeof(siphash_key_t), GFP_KERNEL); 1621 if (!fh_key) { 1622 trace_nfsd_ctl_fh_key_set(false, -ENOMEM); 1623 return -ENOMEM; 1624 } 1625 nn->fh_key = fh_key; 1626 changed = true; 1627 } else { 1628 changed = fh_key->key[0] != k0 || fh_key->key[1] != k1; 1629 } 1630 1631 fh_key->key[0] = k0; 1632 fh_key->key[1] = k1; 1633 trace_nfsd_ctl_fh_key_set(changed, 0); 1634 return 0; 1635 } 1636 1637 /** 1638 * nfsd_nl_threads_set_doit - set the number of running threads 1639 * @skb: reply buffer 1640 * @info: netlink metadata and command arguments 1641 * 1642 * Return 0 on success or a negative errno. 1643 */ 1644 int nfsd_nl_threads_set_doit(struct sk_buff *skb, struct genl_info *info) 1645 { 1646 int *nthreads, nrpools = 0, i, ret = -EOPNOTSUPP, rem; 1647 struct net *net = genl_info_net(info); 1648 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 1649 const struct nlattr *attr; 1650 const char *scope = NULL; 1651 1652 if (GENL_REQ_ATTR_CHECK(info, NFSD_A_SERVER_THREADS)) 1653 return -EINVAL; 1654 1655 /* count number of SERVER_THREADS values */ 1656 nlmsg_for_each_attr_type(attr, NFSD_A_SERVER_THREADS, info->nlhdr, 1657 GENL_HDRLEN, rem) 1658 nrpools++; 1659 1660 mutex_lock(&nfsd_mutex); 1661 1662 nthreads = kzalloc_objs(int, nrpools); 1663 if (!nthreads) { 1664 ret = -ENOMEM; 1665 goto out_unlock; 1666 } 1667 1668 i = 0; 1669 nlmsg_for_each_attr_type(attr, NFSD_A_SERVER_THREADS, info->nlhdr, 1670 GENL_HDRLEN, rem) { 1671 nthreads[i++] = nla_get_u32(attr); 1672 if (i >= nrpools) 1673 break; 1674 } 1675 1676 if (info->attrs[NFSD_A_SERVER_GRACETIME] || 1677 info->attrs[NFSD_A_SERVER_LEASETIME] || 1678 info->attrs[NFSD_A_SERVER_SCOPE] || 1679 info->attrs[NFSD_A_SERVER_FH_KEY]) { 1680 ret = -EBUSY; 1681 if (nn->nfsd_serv && nn->nfsd_serv->sv_nrthreads) 1682 goto out_unlock; 1683 1684 ret = -EINVAL; 1685 attr = info->attrs[NFSD_A_SERVER_GRACETIME]; 1686 if (attr) { 1687 u32 gracetime = nla_get_u32(attr); 1688 1689 if (gracetime < 10 || gracetime > 3600) 1690 goto out_unlock; 1691 1692 nn->nfsd4_grace = gracetime; 1693 } 1694 1695 attr = info->attrs[NFSD_A_SERVER_LEASETIME]; 1696 if (attr) { 1697 u32 leasetime = nla_get_u32(attr); 1698 1699 if (leasetime < 10 || leasetime > 3600) 1700 goto out_unlock; 1701 1702 nn->nfsd4_lease = leasetime; 1703 } 1704 1705 attr = info->attrs[NFSD_A_SERVER_SCOPE]; 1706 if (attr) 1707 scope = nla_data(attr); 1708 1709 attr = info->attrs[NFSD_A_SERVER_FH_KEY]; 1710 if (attr) { 1711 ret = nfsd_nl_fh_key_set(attr, nn); 1712 if (ret) 1713 goto out_unlock; 1714 } 1715 } 1716 1717 attr = info->attrs[NFSD_A_SERVER_MIN_THREADS]; 1718 if (attr) 1719 nn->min_threads = nla_get_u32(attr); 1720 1721 ret = nfsd_svc(nrpools, nthreads, net, current_cred(), scope); 1722 if (ret > 0) 1723 ret = 0; 1724 out_unlock: 1725 mutex_unlock(&nfsd_mutex); 1726 kfree(nthreads); 1727 return ret; 1728 } 1729 1730 /** 1731 * nfsd_nl_threads_get_doit - get the maximum number of running threads 1732 * @skb: reply buffer 1733 * @info: netlink metadata and command arguments 1734 * 1735 * Return 0 on success or a negative errno. 1736 */ 1737 int nfsd_nl_threads_get_doit(struct sk_buff *skb, struct genl_info *info) 1738 { 1739 struct net *net = genl_info_net(info); 1740 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 1741 void *hdr; 1742 int err; 1743 1744 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); 1745 if (!skb) 1746 return -ENOMEM; 1747 1748 hdr = genlmsg_iput(skb, info); 1749 if (!hdr) { 1750 err = -EMSGSIZE; 1751 goto err_free_msg; 1752 } 1753 1754 mutex_lock(&nfsd_mutex); 1755 1756 err = nla_put_u32(skb, NFSD_A_SERVER_GRACETIME, 1757 nn->nfsd4_grace) || 1758 nla_put_u32(skb, NFSD_A_SERVER_LEASETIME, 1759 nn->nfsd4_lease) || 1760 nla_put_u32(skb, NFSD_A_SERVER_MIN_THREADS, 1761 nn->min_threads) || 1762 nla_put_string(skb, NFSD_A_SERVER_SCOPE, 1763 nn->nfsd_name); 1764 if (err) 1765 goto err_unlock; 1766 1767 if (nn->nfsd_serv) { 1768 int i; 1769 1770 for (i = 0; i < nfsd_nrpools(net); ++i) { 1771 struct svc_pool *sp = &nn->nfsd_serv->sv_pools[i]; 1772 1773 err = nla_put_u32(skb, NFSD_A_SERVER_THREADS, 1774 sp->sp_nrthrmax); 1775 if (err) 1776 goto err_unlock; 1777 } 1778 } else { 1779 err = nla_put_u32(skb, NFSD_A_SERVER_THREADS, 0); 1780 if (err) 1781 goto err_unlock; 1782 } 1783 1784 mutex_unlock(&nfsd_mutex); 1785 1786 genlmsg_end(skb, hdr); 1787 1788 return genlmsg_reply(skb, info); 1789 1790 err_unlock: 1791 mutex_unlock(&nfsd_mutex); 1792 err_free_msg: 1793 nlmsg_free(skb); 1794 1795 return err; 1796 } 1797 1798 /** 1799 * nfsd_nl_version_set_doit - set the nfs enabled versions 1800 * @skb: reply buffer 1801 * @info: netlink metadata and command arguments 1802 * 1803 * Return 0 on success or a negative errno. 1804 */ 1805 int nfsd_nl_version_set_doit(struct sk_buff *skb, struct genl_info *info) 1806 { 1807 const struct nlattr *attr; 1808 struct nfsd_net *nn; 1809 int i, rem; 1810 1811 if (GENL_REQ_ATTR_CHECK(info, NFSD_A_SERVER_PROTO_VERSION)) 1812 return -EINVAL; 1813 1814 mutex_lock(&nfsd_mutex); 1815 1816 nn = net_generic(genl_info_net(info), nfsd_net_id); 1817 if (nn->nfsd_serv) { 1818 mutex_unlock(&nfsd_mutex); 1819 return -EBUSY; 1820 } 1821 1822 /* clear current supported versions. */ 1823 nfsd_vers(nn, 2, NFSD_CLEAR); 1824 nfsd_vers(nn, 3, NFSD_CLEAR); 1825 for (i = 0; i <= NFSD_SUPPORTED_MINOR_VERSION; i++) 1826 nfsd_minorversion(nn, i, NFSD_CLEAR); 1827 1828 nlmsg_for_each_attr_type(attr, NFSD_A_SERVER_PROTO_VERSION, info->nlhdr, 1829 GENL_HDRLEN, rem) { 1830 struct nlattr *tb[NFSD_A_VERSION_MAX + 1]; 1831 u32 major, minor = 0; 1832 bool enabled; 1833 1834 if (nla_parse_nested(tb, NFSD_A_VERSION_MAX, attr, 1835 nfsd_version_nl_policy, info->extack) < 0) 1836 continue; 1837 1838 if (!tb[NFSD_A_VERSION_MAJOR]) 1839 continue; 1840 1841 major = nla_get_u32(tb[NFSD_A_VERSION_MAJOR]); 1842 if (tb[NFSD_A_VERSION_MINOR]) 1843 minor = nla_get_u32(tb[NFSD_A_VERSION_MINOR]); 1844 1845 enabled = nla_get_flag(tb[NFSD_A_VERSION_ENABLED]); 1846 1847 switch (major) { 1848 case 4: 1849 nfsd_minorversion(nn, minor, enabled ? NFSD_SET : NFSD_CLEAR); 1850 break; 1851 case 3: 1852 case 2: 1853 if (!minor) 1854 nfsd_vers(nn, major, enabled ? NFSD_SET : NFSD_CLEAR); 1855 break; 1856 default: 1857 break; 1858 } 1859 } 1860 1861 mutex_unlock(&nfsd_mutex); 1862 1863 return 0; 1864 } 1865 1866 /** 1867 * nfsd_nl_version_get_doit - get the enabled status for all supported nfs versions 1868 * @skb: reply buffer 1869 * @info: netlink metadata and command arguments 1870 * 1871 * Return 0 on success or a negative errno. 1872 */ 1873 int nfsd_nl_version_get_doit(struct sk_buff *skb, struct genl_info *info) 1874 { 1875 struct nfsd_net *nn; 1876 int i, err; 1877 void *hdr; 1878 1879 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); 1880 if (!skb) 1881 return -ENOMEM; 1882 1883 hdr = genlmsg_iput(skb, info); 1884 if (!hdr) { 1885 err = -EMSGSIZE; 1886 goto err_free_msg; 1887 } 1888 1889 mutex_lock(&nfsd_mutex); 1890 nn = net_generic(genl_info_net(info), nfsd_net_id); 1891 1892 for (i = 2; i <= 4; i++) { 1893 int j; 1894 1895 for (j = 0; j <= NFSD_SUPPORTED_MINOR_VERSION; j++) { 1896 struct nlattr *attr; 1897 1898 /* Don't record any versions the kernel doesn't have 1899 * compiled in 1900 */ 1901 if (!nfsd_support_version(i)) 1902 continue; 1903 1904 /* NFSv{2,3} does not support minor numbers */ 1905 if (i < 4 && j) 1906 continue; 1907 1908 attr = nla_nest_start(skb, 1909 NFSD_A_SERVER_PROTO_VERSION); 1910 if (!attr) { 1911 err = -EINVAL; 1912 goto err_nfsd_unlock; 1913 } 1914 1915 if (nla_put_u32(skb, NFSD_A_VERSION_MAJOR, i) || 1916 nla_put_u32(skb, NFSD_A_VERSION_MINOR, j)) { 1917 err = -EINVAL; 1918 goto err_nfsd_unlock; 1919 } 1920 1921 /* Set the enabled flag if the version is enabled */ 1922 if (nfsd_vers(nn, i, NFSD_TEST) && 1923 (i < 4 || nfsd_minorversion(nn, j, NFSD_TEST)) && 1924 nla_put_flag(skb, NFSD_A_VERSION_ENABLED)) { 1925 err = -EINVAL; 1926 goto err_nfsd_unlock; 1927 } 1928 1929 nla_nest_end(skb, attr); 1930 } 1931 } 1932 1933 mutex_unlock(&nfsd_mutex); 1934 genlmsg_end(skb, hdr); 1935 1936 return genlmsg_reply(skb, info); 1937 1938 err_nfsd_unlock: 1939 mutex_unlock(&nfsd_mutex); 1940 err_free_msg: 1941 nlmsg_free(skb); 1942 1943 return err; 1944 } 1945 1946 /** 1947 * nfsd_nl_listener_set_doit - set the nfs running sockets 1948 * @skb: reply buffer 1949 * @info: netlink metadata and command arguments 1950 * 1951 * Return 0 on success or a negative errno. 1952 */ 1953 int nfsd_nl_listener_set_doit(struct sk_buff *skb, struct genl_info *info) 1954 { 1955 struct net *net = genl_info_net(info); 1956 struct svc_xprt *xprt, *tmp; 1957 const struct nlattr *attr; 1958 struct svc_serv *serv; 1959 LIST_HEAD(permsocks); 1960 struct nfsd_net *nn; 1961 bool delete = false; 1962 int err, rem; 1963 1964 mutex_lock(&nfsd_mutex); 1965 1966 err = nfsd_create_serv(net); 1967 if (err) { 1968 mutex_unlock(&nfsd_mutex); 1969 return err; 1970 } 1971 1972 nn = net_generic(net, nfsd_net_id); 1973 serv = nn->nfsd_serv; 1974 1975 spin_lock_bh(&serv->sv_lock); 1976 1977 /* Move all of the old listener sockets to a temp list */ 1978 list_splice_init(&serv->sv_permsocks, &permsocks); 1979 1980 /* 1981 * Walk the list of server_socks from userland and move any that match 1982 * back to sv_permsocks 1983 */ 1984 nlmsg_for_each_attr_type(attr, NFSD_A_SERVER_SOCK_ADDR, info->nlhdr, 1985 GENL_HDRLEN, rem) { 1986 struct nlattr *tb[NFSD_A_SOCK_MAX + 1]; 1987 const char *xcl_name; 1988 struct sockaddr *sa; 1989 1990 if (nla_parse_nested(tb, NFSD_A_SOCK_MAX, attr, 1991 nfsd_sock_nl_policy, info->extack) < 0) 1992 continue; 1993 1994 if (!tb[NFSD_A_SOCK_ADDR] || !tb[NFSD_A_SOCK_TRANSPORT_NAME]) 1995 continue; 1996 1997 if (nla_len(tb[NFSD_A_SOCK_ADDR]) < sizeof(*sa)) 1998 continue; 1999 2000 xcl_name = nla_data(tb[NFSD_A_SOCK_TRANSPORT_NAME]); 2001 sa = nla_data(tb[NFSD_A_SOCK_ADDR]); 2002 2003 /* Put back any matching sockets */ 2004 list_for_each_entry_safe(xprt, tmp, &permsocks, xpt_list) { 2005 /* This shouldn't be possible */ 2006 if (WARN_ON_ONCE(xprt->xpt_net != net)) { 2007 list_move(&xprt->xpt_list, &serv->sv_permsocks); 2008 continue; 2009 } 2010 2011 /* If everything matches, put it back */ 2012 if (!strcmp(xprt->xpt_class->xcl_name, xcl_name) && 2013 rpc_cmp_addr_port(sa, (struct sockaddr *)&xprt->xpt_local)) { 2014 list_move(&xprt->xpt_list, &serv->sv_permsocks); 2015 break; 2016 } 2017 } 2018 } 2019 2020 /* 2021 * If there are listener transports remaining on the permsocks list, 2022 * it means we were asked to remove a listener. 2023 */ 2024 if (!list_empty(&permsocks)) { 2025 list_splice_init(&permsocks, &serv->sv_permsocks); 2026 delete = true; 2027 } 2028 spin_unlock_bh(&serv->sv_lock); 2029 2030 /* Do not remove listeners while there are active threads. */ 2031 if (serv->sv_nrthreads) { 2032 err = -EBUSY; 2033 goto out_unlock_mtx; 2034 } 2035 2036 /* 2037 * Since we can't delete an arbitrary llist entry, destroy the 2038 * remaining listeners and recreate the list. 2039 */ 2040 if (delete) 2041 svc_xprt_destroy_all(serv, net, false); 2042 2043 /* walk list of addrs again, open any that still don't exist */ 2044 nlmsg_for_each_attr_type(attr, NFSD_A_SERVER_SOCK_ADDR, info->nlhdr, 2045 GENL_HDRLEN, rem) { 2046 struct nlattr *tb[NFSD_A_SOCK_MAX + 1]; 2047 const char *xcl_name; 2048 struct sockaddr *sa; 2049 int ret; 2050 2051 if (nla_parse_nested(tb, NFSD_A_SOCK_MAX, attr, 2052 nfsd_sock_nl_policy, info->extack) < 0) 2053 continue; 2054 2055 if (!tb[NFSD_A_SOCK_ADDR] || !tb[NFSD_A_SOCK_TRANSPORT_NAME]) 2056 continue; 2057 2058 if (nla_len(tb[NFSD_A_SOCK_ADDR]) < sizeof(*sa)) 2059 continue; 2060 2061 xcl_name = nla_data(tb[NFSD_A_SOCK_TRANSPORT_NAME]); 2062 sa = nla_data(tb[NFSD_A_SOCK_ADDR]); 2063 2064 xprt = svc_find_listener(serv, xcl_name, net, sa); 2065 if (xprt) { 2066 if (delete) 2067 WARN_ONCE(1, "Transport type=%s already exists\n", 2068 xcl_name); 2069 svc_xprt_put(xprt); 2070 continue; 2071 } 2072 2073 ret = svc_xprt_create_from_sa(serv, xcl_name, net, sa, 0, 2074 current_cred()); 2075 /* always save the latest error */ 2076 if (ret < 0) 2077 err = ret; 2078 } 2079 2080 if (!serv->sv_nrthreads && list_empty(&nn->nfsd_serv->sv_permsocks)) 2081 nfsd_destroy_serv(net); 2082 2083 out_unlock_mtx: 2084 mutex_unlock(&nfsd_mutex); 2085 2086 return err; 2087 } 2088 2089 /** 2090 * nfsd_nl_listener_get_doit - get the nfs running listeners 2091 * @skb: reply buffer 2092 * @info: netlink metadata and command arguments 2093 * 2094 * Return 0 on success or a negative errno. 2095 */ 2096 int nfsd_nl_listener_get_doit(struct sk_buff *skb, struct genl_info *info) 2097 { 2098 struct svc_xprt *xprt; 2099 struct svc_serv *serv; 2100 struct nfsd_net *nn; 2101 void *hdr; 2102 int err; 2103 2104 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); 2105 if (!skb) 2106 return -ENOMEM; 2107 2108 hdr = genlmsg_iput(skb, info); 2109 if (!hdr) { 2110 err = -EMSGSIZE; 2111 goto err_free_msg; 2112 } 2113 2114 mutex_lock(&nfsd_mutex); 2115 nn = net_generic(genl_info_net(info), nfsd_net_id); 2116 2117 /* no nfs server? Just send empty socket list */ 2118 if (!nn->nfsd_serv) 2119 goto out_unlock_mtx; 2120 2121 serv = nn->nfsd_serv; 2122 spin_lock_bh(&serv->sv_lock); 2123 list_for_each_entry(xprt, &serv->sv_permsocks, xpt_list) { 2124 struct nlattr *attr; 2125 2126 attr = nla_nest_start(skb, NFSD_A_SERVER_SOCK_ADDR); 2127 if (!attr) { 2128 err = -EINVAL; 2129 goto err_serv_unlock; 2130 } 2131 2132 if (nla_put_string(skb, NFSD_A_SOCK_TRANSPORT_NAME, 2133 xprt->xpt_class->xcl_name) || 2134 nla_put(skb, NFSD_A_SOCK_ADDR, 2135 sizeof(struct sockaddr_storage), 2136 &xprt->xpt_local)) { 2137 err = -EINVAL; 2138 goto err_serv_unlock; 2139 } 2140 2141 nla_nest_end(skb, attr); 2142 } 2143 spin_unlock_bh(&serv->sv_lock); 2144 out_unlock_mtx: 2145 mutex_unlock(&nfsd_mutex); 2146 genlmsg_end(skb, hdr); 2147 2148 return genlmsg_reply(skb, info); 2149 2150 err_serv_unlock: 2151 spin_unlock_bh(&serv->sv_lock); 2152 mutex_unlock(&nfsd_mutex); 2153 err_free_msg: 2154 nlmsg_free(skb); 2155 2156 return err; 2157 } 2158 2159 /** 2160 * nfsd_nl_pool_mode_set_doit - set the number of running threads 2161 * @skb: reply buffer 2162 * @info: netlink metadata and command arguments 2163 * 2164 * Return 0 on success or a negative errno. 2165 */ 2166 int nfsd_nl_pool_mode_set_doit(struct sk_buff *skb, struct genl_info *info) 2167 { 2168 const struct nlattr *attr; 2169 2170 if (GENL_REQ_ATTR_CHECK(info, NFSD_A_POOL_MODE_MODE)) 2171 return -EINVAL; 2172 2173 attr = info->attrs[NFSD_A_POOL_MODE_MODE]; 2174 return sunrpc_set_pool_mode(nla_data(attr)); 2175 } 2176 2177 /** 2178 * nfsd_nl_pool_mode_get_doit - get info about pool_mode 2179 * @skb: reply buffer 2180 * @info: netlink metadata and command arguments 2181 * 2182 * Return 0 on success or a negative errno. 2183 */ 2184 int nfsd_nl_pool_mode_get_doit(struct sk_buff *skb, struct genl_info *info) 2185 { 2186 struct net *net = genl_info_net(info); 2187 char buf[16]; 2188 void *hdr; 2189 int err; 2190 2191 if (sunrpc_get_pool_mode(buf, ARRAY_SIZE(buf)) >= ARRAY_SIZE(buf)) 2192 return -ERANGE; 2193 2194 skb = genlmsg_new(GENLMSG_DEFAULT_SIZE, GFP_KERNEL); 2195 if (!skb) 2196 return -ENOMEM; 2197 2198 err = -EMSGSIZE; 2199 hdr = genlmsg_iput(skb, info); 2200 if (!hdr) 2201 goto err_free_msg; 2202 2203 err = nla_put_string(skb, NFSD_A_POOL_MODE_MODE, buf) | 2204 nla_put_u32(skb, NFSD_A_POOL_MODE_NPOOLS, nfsd_nrpools(net)); 2205 if (err) 2206 goto err_free_msg; 2207 2208 genlmsg_end(skb, hdr); 2209 return genlmsg_reply(skb, info); 2210 2211 err_free_msg: 2212 nlmsg_free(skb); 2213 return err; 2214 } 2215 2216 /** 2217 * nfsd_nl_cache_flush_doit - flush nfsd caches via netlink 2218 * @skb: reply buffer 2219 * @info: netlink metadata and command arguments 2220 * 2221 * Flush the svc_export and/or expkey caches. If NFSD_A_CACHE_FLUSH_MASK 2222 * is provided, only flush the caches indicated by the bitmask (bit 0 = 2223 * svc_export, bit 1 = expkey). If omitted, flush both. 2224 * 2225 * Return 0 on success or a negative errno. 2226 */ 2227 int nfsd_nl_cache_flush_doit(struct sk_buff *skb, struct genl_info *info) 2228 { 2229 struct net *net = genl_info_net(info); 2230 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 2231 u32 mask = ~0U; 2232 2233 if (info->attrs[NFSD_A_CACHE_FLUSH_MASK]) 2234 mask = nla_get_u32(info->attrs[NFSD_A_CACHE_FLUSH_MASK]); 2235 2236 mutex_lock(&nfsd_mutex); 2237 2238 if ((mask & NFSD_CACHE_TYPE_SVC_EXPORT) && 2239 nn->svc_export_cache) 2240 cache_purge(nn->svc_export_cache); 2241 2242 if ((mask & NFSD_CACHE_TYPE_EXPKEY) && 2243 nn->svc_expkey_cache) 2244 cache_purge(nn->svc_expkey_cache); 2245 2246 mutex_unlock(&nfsd_mutex); 2247 2248 return 0; 2249 } 2250 2251 int nfsd_cache_notify(struct cache_detail *cd, struct cache_head *h, u32 cache_type) 2252 { 2253 struct genlmsghdr *hdr; 2254 struct sk_buff *msg; 2255 2256 if (!genl_has_listeners(&nfsd_nl_family, cd->net, NFSD_NLGRP_EXPORTD)) 2257 return -ENOLINK; 2258 2259 msg = genlmsg_new(nla_total_size(sizeof(u32)), GFP_KERNEL); 2260 if (!msg) 2261 return -ENOMEM; 2262 2263 hdr = genlmsg_put(msg, 0, 0, &nfsd_nl_family, 0, NFSD_CMD_CACHE_NOTIFY); 2264 if (!hdr) { 2265 nlmsg_free(msg); 2266 return -ENOMEM; 2267 } 2268 2269 if (nla_put_u32(msg, NFSD_A_CACHE_NOTIFY_CACHE_TYPE, cache_type)) { 2270 nlmsg_free(msg); 2271 return -ENOMEM; 2272 } 2273 2274 genlmsg_end(msg, hdr); 2275 return genlmsg_multicast_netns(&nfsd_nl_family, cd->net, msg, 0, 2276 NFSD_NLGRP_EXPORTD, GFP_KERNEL); 2277 } 2278 2279 /** 2280 * nfsd_nl_unlock_ip_doit - release NLM locks held by an IP address 2281 * @skb: reply buffer 2282 * @info: netlink metadata and command arguments 2283 * 2284 * Return: 0 on success or a negative errno. 2285 */ 2286 int nfsd_nl_unlock_ip_doit(struct sk_buff *skb, struct genl_info *info) 2287 { 2288 struct sockaddr *sap; 2289 2290 if (GENL_REQ_ATTR_CHECK(info, NFSD_A_UNLOCK_IP_ADDRESS)) 2291 return -EINVAL; 2292 sap = nla_data(info->attrs[NFSD_A_UNLOCK_IP_ADDRESS]); 2293 switch (sap->sa_family) { 2294 case AF_INET: 2295 if (nla_len(info->attrs[NFSD_A_UNLOCK_IP_ADDRESS]) < 2296 sizeof(struct sockaddr_in)) 2297 return -EINVAL; 2298 break; 2299 case AF_INET6: 2300 if (nla_len(info->attrs[NFSD_A_UNLOCK_IP_ADDRESS]) < 2301 sizeof(struct sockaddr_in6)) 2302 return -EINVAL; 2303 break; 2304 default: 2305 return -EAFNOSUPPORT; 2306 } 2307 /* 2308 * nlmsvc_unlock_all_by_ip() releases matching locks 2309 * across all network namespaces because lockd operates 2310 * a single global instance. 2311 */ 2312 trace_nfsd_ctl_unlock_ip(genl_info_net(info), sap, 2313 svc_addr_len(sap)); 2314 return nlmsvc_unlock_all_by_ip(sap); 2315 } 2316 2317 /** 2318 * nfsd_nl_unlock_filesystem_doit - revoke NFS state under a filesystem path 2319 * @skb: reply buffer 2320 * @info: netlink metadata and command arguments 2321 * 2322 * Return: 0 on success or a negative errno. 2323 */ 2324 int nfsd_nl_unlock_filesystem_doit(struct sk_buff *skb, 2325 struct genl_info *info) 2326 { 2327 struct net *net = genl_info_net(info); 2328 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 2329 struct path path; 2330 int error; 2331 2332 if (GENL_REQ_ATTR_CHECK(info, NFSD_A_UNLOCK_FILESYSTEM_PATH)) 2333 return -EINVAL; 2334 2335 trace_nfsd_ctl_unlock_fs(net, 2336 nla_data(info->attrs[NFSD_A_UNLOCK_FILESYSTEM_PATH])); 2337 error = kern_path( 2338 nla_data(info->attrs[NFSD_A_UNLOCK_FILESYSTEM_PATH]), 2339 0, &path); 2340 if (error) 2341 return error; 2342 2343 nfsd4_cancel_copy_by_sb(net, path.dentry->d_sb); 2344 error = nlmsvc_unlock_all_by_sb(path.dentry->d_sb); 2345 2346 mutex_lock(&nfsd_mutex); 2347 if (nn->nfsd_serv) 2348 nfsd4_revoke_states(nn, path.dentry->d_sb); 2349 else 2350 error = -EINVAL; 2351 mutex_unlock(&nfsd_mutex); 2352 2353 path_put(&path); 2354 return error; 2355 } 2356 2357 /** 2358 * nfsd_nl_unlock_export_doit - revoke NFSv4 state for an export path 2359 * @skb: reply buffer 2360 * @info: netlink metadata and command arguments 2361 * 2362 * Revokes all NFSv4 state (opens, locks, delegations, layouts) acquired 2363 * through any export of the given path, regardless of which client holds 2364 * the state. Userspace (exportfs -u) sends this after removing the last 2365 * client for a path so the underlying filesystem can be unmounted. 2366 * 2367 * Unlike NFSD_CMD_UNLOCK_FILESYSTEM, which operates at superblock 2368 * granularity, this command revokes only the state associated with 2369 * exports of a specific path. 2370 * 2371 * Return: 0 on success or a negative errno. 2372 */ 2373 int nfsd_nl_unlock_export_doit(struct sk_buff *skb, struct genl_info *info) 2374 { 2375 struct net *net = genl_info_net(info); 2376 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 2377 struct path path; 2378 int error; 2379 2380 if (GENL_REQ_ATTR_CHECK(info, NFSD_A_UNLOCK_EXPORT_PATH)) 2381 return -EINVAL; 2382 2383 trace_nfsd_ctl_unlock_export(net, 2384 nla_data(info->attrs[NFSD_A_UNLOCK_EXPORT_PATH])); 2385 error = kern_path( 2386 nla_data(info->attrs[NFSD_A_UNLOCK_EXPORT_PATH]), 2387 0, &path); 2388 if (error) 2389 return error; 2390 2391 mutex_lock(&nfsd_mutex); 2392 if (nn->nfsd_serv) { 2393 nfsd_file_close_export(net, &path); 2394 nfsd4_revoke_export_states(nn, &path); 2395 } else 2396 error = -EINVAL; 2397 mutex_unlock(&nfsd_mutex); 2398 2399 path_put(&path); 2400 return error; 2401 } 2402 2403 /** 2404 * nfsd_net_init - Prepare the nfsd_net portion of a new net namespace 2405 * @net: a freshly-created network namespace 2406 * 2407 * This information stays around as long as the network namespace is 2408 * alive whether or not there is an NFSD instance running in the 2409 * namespace. 2410 * 2411 * Returns zero on success, or a negative errno otherwise. 2412 */ 2413 static __net_init int nfsd_net_init(struct net *net) 2414 { 2415 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 2416 int retval; 2417 int i; 2418 2419 retval = nfsd_net_cb_init(nn); 2420 if (retval) 2421 return retval; 2422 retval = nfsd_export_init(net); 2423 if (retval) 2424 goto out_export_error; 2425 retval = nfsd_idmap_init(net); 2426 if (retval) 2427 goto out_idmap_error; 2428 retval = percpu_counter_init_many(nn->counter, 0, GFP_KERNEL, 2429 NFSD_STATS_COUNTERS_NUM); 2430 if (retval) 2431 goto out_repcache_error; 2432 2433 memset(&nn->nfsd_svcstats, 0, sizeof(nn->nfsd_svcstats)); 2434 nn->nfsd_svcstats.program = &nfsd_programs[0]; 2435 if (!nfsd_proc_stat_init(net)) { 2436 retval = -ENOMEM; 2437 goto out_proc_error; 2438 } 2439 2440 for (i = 0; i < sizeof(nn->nfsd_versions); i++) 2441 nn->nfsd_versions[i] = nfsd_support_version(i); 2442 for (i = 0; i < sizeof(nn->nfsd4_minorversions); i++) 2443 nn->nfsd4_minorversions[i] = nfsd_support_version(4); 2444 nn->nfsd_info.mutex = &nfsd_mutex; 2445 nn->nfsd_serv = NULL; 2446 nfsd4_init_leases_net(nn); 2447 get_random_bytes(&nn->siphash_key, sizeof(nn->siphash_key)); 2448 seqlock_init(&nn->writeverf_lock); 2449 #if IS_ENABLED(CONFIG_NFS_LOCALIO) 2450 spin_lock_init(&nn->local_clients_lock); 2451 INIT_LIST_HEAD(&nn->local_clients); 2452 #endif 2453 return 0; 2454 2455 out_proc_error: 2456 percpu_counter_destroy_many(nn->counter, NFSD_STATS_COUNTERS_NUM); 2457 out_repcache_error: 2458 nfsd_idmap_shutdown(net); 2459 out_idmap_error: 2460 nfsd_export_shutdown(net); 2461 out_export_error: 2462 nfsd_net_cb_shutdown(nn); 2463 return retval; 2464 } 2465 2466 #if IS_ENABLED(CONFIG_NFS_LOCALIO) 2467 /** 2468 * nfsd_net_pre_exit - Disconnect localio clients from net namespace 2469 * @net: a network namespace that is about to be destroyed 2470 * 2471 * This invalidates ->net pointers held by localio clients 2472 * while they can still safely access nn->counter. 2473 */ 2474 static __net_exit void nfsd_net_pre_exit(struct net *net) 2475 { 2476 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 2477 2478 nfs_localio_invalidate_clients(&nn->local_clients, 2479 &nn->local_clients_lock); 2480 } 2481 #endif 2482 2483 /** 2484 * nfsd_net_exit - Release the nfsd_net portion of a net namespace 2485 * @net: a network namespace that is about to be destroyed 2486 * 2487 */ 2488 static __net_exit void nfsd_net_exit(struct net *net) 2489 { 2490 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 2491 2492 kfree_sensitive(nn->fh_key); 2493 nfsd_net_cb_shutdown(nn); 2494 nfsd_proc_stat_shutdown(net); 2495 percpu_counter_destroy_many(nn->counter, NFSD_STATS_COUNTERS_NUM); 2496 nfsd_idmap_shutdown(net); 2497 nfsd_export_shutdown(net); 2498 } 2499 2500 static struct pernet_operations nfsd_net_ops = { 2501 .init = nfsd_net_init, 2502 #if IS_ENABLED(CONFIG_NFS_LOCALIO) 2503 .pre_exit = nfsd_net_pre_exit, 2504 #endif 2505 .exit = nfsd_net_exit, 2506 .id = &nfsd_net_id, 2507 .size = sizeof(struct nfsd_net), 2508 }; 2509 2510 static int __init init_nfsd(void) 2511 { 2512 int retval; 2513 2514 nfsd_debugfs_init(); 2515 2516 retval = nfsd4_init_slabs(); 2517 if (retval) 2518 return retval; 2519 retval = nfsd4_init_pnfs(); 2520 if (retval) 2521 goto out_free_slabs; 2522 retval = nfsd_drc_slab_create(); 2523 if (retval) 2524 goto out_free_pnfs; 2525 nfsd_lockd_init(); /* lockd->nfsd callbacks */ 2526 retval = register_pernet_subsys(&nfsd_net_ops); 2527 if (retval < 0) 2528 goto out_free_lockd; 2529 retval = register_cld_notifier(); 2530 if (retval) 2531 goto out_free_subsys; 2532 retval = nfsd4_create_laundry_wq(); 2533 if (retval) 2534 goto out_free_cld; 2535 retval = register_filesystem(&nfsd_fs_type); 2536 if (retval) 2537 goto out_free_nfsd4; 2538 retval = genl_register_family(&nfsd_nl_family); 2539 if (retval) 2540 goto out_free_filesystem; 2541 retval = create_proc_exports_entry(); 2542 if (retval) 2543 goto out_free_all; 2544 nfsd_localio_ops_init(); 2545 2546 return 0; 2547 out_free_all: 2548 genl_unregister_family(&nfsd_nl_family); 2549 out_free_filesystem: 2550 unregister_filesystem(&nfsd_fs_type); 2551 out_free_nfsd4: 2552 nfsd4_destroy_laundry_wq(); 2553 out_free_cld: 2554 unregister_cld_notifier(); 2555 out_free_subsys: 2556 unregister_pernet_subsys(&nfsd_net_ops); 2557 out_free_lockd: 2558 nfsd_lockd_shutdown(); 2559 nfsd_drc_slab_free(); 2560 out_free_pnfs: 2561 nfsd4_exit_pnfs(); 2562 out_free_slabs: 2563 nfsd4_free_slabs(); 2564 nfsd_debugfs_exit(); 2565 return retval; 2566 } 2567 2568 static void __exit exit_nfsd(void) 2569 { 2570 remove_proc_entry("fs/nfs/exports", NULL); 2571 remove_proc_entry("fs/nfs", NULL); 2572 genl_unregister_family(&nfsd_nl_family); 2573 unregister_filesystem(&nfsd_fs_type); 2574 nfsd4_destroy_laundry_wq(); 2575 unregister_cld_notifier(); 2576 unregister_pernet_subsys(&nfsd_net_ops); 2577 nfsd_drc_slab_free(); 2578 nfsd_lockd_shutdown(); 2579 nfsd4_free_slabs(); 2580 nfsd4_exit_pnfs(); 2581 nfsd_debugfs_exit(); 2582 } 2583 2584 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>"); 2585 MODULE_DESCRIPTION("In-kernel NFS server"); 2586 MODULE_LICENSE("GPL"); 2587 module_init(init_nfsd) 2588 module_exit(exit_nfsd) 2589