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/svcsock.h> 14 #include <linux/lockd/lockd.h> 15 #include <linux/sunrpc/addr.h> 16 #include <linux/sunrpc/gss_api.h> 17 #include <linux/sunrpc/rpc_pipe_fs.h> 18 #include <linux/module.h> 19 #include <linux/fsnotify.h> 20 21 #include "idmap.h" 22 #include "nfsd.h" 23 #include "cache.h" 24 #include "state.h" 25 #include "netns.h" 26 #include "pnfs.h" 27 #include "filecache.h" 28 #include "trace.h" 29 #include "netlink.h" 30 31 /* 32 * We have a single directory with several nodes in it. 33 */ 34 enum { 35 NFSD_Root = 1, 36 NFSD_List, 37 NFSD_Export_Stats, 38 NFSD_Export_features, 39 NFSD_Fh, 40 NFSD_FO_UnlockIP, 41 NFSD_FO_UnlockFS, 42 NFSD_Threads, 43 NFSD_Pool_Threads, 44 NFSD_Pool_Stats, 45 NFSD_Reply_Cache_Stats, 46 NFSD_Versions, 47 NFSD_Ports, 48 NFSD_MaxBlkSize, 49 NFSD_MaxConnections, 50 NFSD_Filecache, 51 /* 52 * The below MUST come last. Otherwise we leave a hole in nfsd_files[] 53 * with !CONFIG_NFSD_V4 and simple_fill_super() goes oops 54 */ 55 #ifdef CONFIG_NFSD_V4 56 NFSD_Leasetime, 57 NFSD_Gracetime, 58 NFSD_RecoveryDir, 59 NFSD_V4EndGrace, 60 #endif 61 NFSD_MaxReserved 62 }; 63 64 /* 65 * write() for these nodes. 66 */ 67 static ssize_t write_filehandle(struct file *file, char *buf, size_t size); 68 static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size); 69 static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size); 70 static ssize_t write_threads(struct file *file, char *buf, size_t size); 71 static ssize_t write_pool_threads(struct file *file, char *buf, size_t size); 72 static ssize_t write_versions(struct file *file, char *buf, size_t size); 73 static ssize_t write_ports(struct file *file, char *buf, size_t size); 74 static ssize_t write_maxblksize(struct file *file, char *buf, size_t size); 75 static ssize_t write_maxconn(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 [NFSD_MaxConnections] = write_maxconn, 95 #ifdef CONFIG_NFSD_V4 96 [NFSD_Leasetime] = write_leasetime, 97 [NFSD_Gracetime] = write_gracetime, 98 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING 99 [NFSD_RecoveryDir] = write_recoverydir, 100 #endif 101 [NFSD_V4EndGrace] = write_v4_end_grace, 102 #endif 103 }; 104 105 static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos) 106 { 107 ino_t ino = file_inode(file)->i_ino; 108 char *data; 109 ssize_t rv; 110 111 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino]) 112 return -EINVAL; 113 114 data = simple_transaction_get(file, buf, size); 115 if (IS_ERR(data)) 116 return PTR_ERR(data); 117 118 rv = write_op[ino](file, data, size); 119 if (rv < 0) 120 return rv; 121 122 simple_transaction_set(file, rv); 123 return size; 124 } 125 126 static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos) 127 { 128 if (! file->private_data) { 129 /* An attempt to read a transaction file without writing 130 * causes a 0-byte write so that the file can return 131 * state information 132 */ 133 ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos); 134 if (rv < 0) 135 return rv; 136 } 137 return simple_transaction_read(file, buf, size, pos); 138 } 139 140 static const struct file_operations transaction_ops = { 141 .write = nfsctl_transaction_write, 142 .read = nfsctl_transaction_read, 143 .release = simple_transaction_release, 144 .llseek = default_llseek, 145 }; 146 147 static int exports_net_open(struct net *net, struct file *file) 148 { 149 int err; 150 struct seq_file *seq; 151 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 152 153 err = seq_open(file, &nfs_exports_op); 154 if (err) 155 return err; 156 157 seq = file->private_data; 158 seq->private = nn->svc_export_cache; 159 return 0; 160 } 161 162 static int exports_nfsd_open(struct inode *inode, struct file *file) 163 { 164 return exports_net_open(inode->i_sb->s_fs_info, file); 165 } 166 167 static const struct file_operations exports_nfsd_operations = { 168 .open = exports_nfsd_open, 169 .read = seq_read, 170 .llseek = seq_lseek, 171 .release = seq_release, 172 }; 173 174 static int export_features_show(struct seq_file *m, void *v) 175 { 176 seq_printf(m, "0x%x 0x%x\n", NFSEXP_ALLFLAGS, NFSEXP_SECINFO_FLAGS); 177 return 0; 178 } 179 180 DEFINE_SHOW_ATTRIBUTE(export_features); 181 182 static const struct file_operations pool_stats_operations = { 183 .open = nfsd_pool_stats_open, 184 .read = seq_read, 185 .llseek = seq_lseek, 186 .release = seq_release, 187 }; 188 189 DEFINE_SHOW_ATTRIBUTE(nfsd_reply_cache_stats); 190 191 DEFINE_SHOW_ATTRIBUTE(nfsd_file_cache_stats); 192 193 /*----------------------------------------------------------------------------*/ 194 /* 195 * payload - write methods 196 */ 197 198 static inline struct net *netns(struct file *file) 199 { 200 return file_inode(file)->i_sb->s_fs_info; 201 } 202 203 /* 204 * write_unlock_ip - Release all locks used by a client 205 * 206 * Experimental. 207 * 208 * Input: 209 * buf: '\n'-terminated C string containing a 210 * presentation format IP address 211 * size: length of C string in @buf 212 * Output: 213 * On success: returns zero if all specified locks were released; 214 * returns one if one or more locks were not released 215 * On error: return code is negative errno value 216 */ 217 static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size) 218 { 219 struct sockaddr_storage address; 220 struct sockaddr *sap = (struct sockaddr *)&address; 221 size_t salen = sizeof(address); 222 char *fo_path; 223 struct net *net = netns(file); 224 225 /* sanity check */ 226 if (size == 0) 227 return -EINVAL; 228 229 if (buf[size-1] != '\n') 230 return -EINVAL; 231 232 fo_path = buf; 233 if (qword_get(&buf, fo_path, size) < 0) 234 return -EINVAL; 235 236 if (rpc_pton(net, fo_path, size, sap, salen) == 0) 237 return -EINVAL; 238 239 trace_nfsd_ctl_unlock_ip(net, buf); 240 return nlmsvc_unlock_all_by_ip(sap); 241 } 242 243 /* 244 * write_unlock_fs - Release all locks on a local file system 245 * 246 * Experimental. 247 * 248 * Input: 249 * buf: '\n'-terminated C string containing the 250 * absolute pathname of a local file system 251 * size: length of C string in @buf 252 * Output: 253 * On success: returns zero if all specified locks were released; 254 * returns one if one or more locks were not released 255 * On error: return code is negative errno value 256 */ 257 static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size) 258 { 259 struct path path; 260 char *fo_path; 261 int error; 262 263 /* sanity check */ 264 if (size == 0) 265 return -EINVAL; 266 267 if (buf[size-1] != '\n') 268 return -EINVAL; 269 270 fo_path = buf; 271 if (qword_get(&buf, fo_path, size) < 0) 272 return -EINVAL; 273 trace_nfsd_ctl_unlock_fs(netns(file), fo_path); 274 error = kern_path(fo_path, 0, &path); 275 if (error) 276 return error; 277 278 /* 279 * XXX: Needs better sanity checking. Otherwise we could end up 280 * releasing locks on the wrong file system. 281 * 282 * For example: 283 * 1. Does the path refer to a directory? 284 * 2. Is that directory a mount point, or 285 * 3. Is that directory the root of an exported file system? 286 */ 287 error = nlmsvc_unlock_all_by_sb(path.dentry->d_sb); 288 289 path_put(&path); 290 return error; 291 } 292 293 /* 294 * write_filehandle - Get a variable-length NFS file handle by path 295 * 296 * On input, the buffer contains a '\n'-terminated C string comprised of 297 * three alphanumeric words separated by whitespace. The string may 298 * contain escape sequences. 299 * 300 * Input: 301 * buf: 302 * domain: client domain name 303 * path: export pathname 304 * maxsize: numeric maximum size of 305 * @buf 306 * size: length of C string in @buf 307 * Output: 308 * On success: passed-in buffer filled with '\n'-terminated C 309 * string containing a ASCII hex text version 310 * of the NFS file handle; 311 * return code is the size in bytes of the string 312 * On error: return code is negative errno value 313 */ 314 static ssize_t write_filehandle(struct file *file, char *buf, size_t size) 315 { 316 char *dname, *path; 317 int maxsize; 318 char *mesg = buf; 319 int len; 320 struct auth_domain *dom; 321 struct knfsd_fh fh; 322 323 if (size == 0) 324 return -EINVAL; 325 326 if (buf[size-1] != '\n') 327 return -EINVAL; 328 buf[size-1] = 0; 329 330 dname = mesg; 331 len = qword_get(&mesg, dname, size); 332 if (len <= 0) 333 return -EINVAL; 334 335 path = dname+len+1; 336 len = qword_get(&mesg, path, size); 337 if (len <= 0) 338 return -EINVAL; 339 340 len = get_int(&mesg, &maxsize); 341 if (len) 342 return len; 343 344 if (maxsize < NFS_FHSIZE) 345 return -EINVAL; 346 maxsize = min(maxsize, NFS3_FHSIZE); 347 348 if (qword_get(&mesg, mesg, size) > 0) 349 return -EINVAL; 350 351 trace_nfsd_ctl_filehandle(netns(file), dname, path, maxsize); 352 353 /* we have all the words, they are in buf.. */ 354 dom = unix_domain_find(dname); 355 if (!dom) 356 return -ENOMEM; 357 358 len = exp_rootfh(netns(file), dom, path, &fh, maxsize); 359 auth_domain_put(dom); 360 if (len) 361 return len; 362 363 mesg = buf; 364 len = SIMPLE_TRANSACTION_LIMIT; 365 qword_addhex(&mesg, &len, fh.fh_raw, fh.fh_size); 366 mesg[-1] = '\n'; 367 return mesg - buf; 368 } 369 370 /* 371 * write_threads - Start NFSD, or report the current number of running threads 372 * 373 * Input: 374 * buf: ignored 375 * size: zero 376 * Output: 377 * On success: passed-in buffer filled with '\n'-terminated C 378 * string numeric value representing the number of 379 * running NFSD threads; 380 * return code is the size in bytes of the string 381 * On error: return code is zero 382 * 383 * OR 384 * 385 * Input: 386 * buf: C string containing an unsigned 387 * integer value representing the 388 * number of NFSD threads to start 389 * size: non-zero length of C string in @buf 390 * Output: 391 * On success: NFS service is started; 392 * passed-in buffer filled with '\n'-terminated C 393 * string numeric value representing the number of 394 * running NFSD threads; 395 * return code is the size in bytes of the string 396 * On error: return code is zero or a negative errno value 397 */ 398 static ssize_t write_threads(struct file *file, char *buf, size_t size) 399 { 400 char *mesg = buf; 401 int rv; 402 struct net *net = netns(file); 403 404 if (size > 0) { 405 int newthreads; 406 rv = get_int(&mesg, &newthreads); 407 if (rv) 408 return rv; 409 if (newthreads < 0) 410 return -EINVAL; 411 trace_nfsd_ctl_threads(net, newthreads); 412 rv = nfsd_svc(newthreads, net, file->f_cred); 413 if (rv < 0) 414 return rv; 415 } else 416 rv = nfsd_nrthreads(net); 417 418 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", rv); 419 } 420 421 /* 422 * write_pool_threads - Set or report the current number of threads per pool 423 * 424 * Input: 425 * buf: ignored 426 * size: zero 427 * 428 * OR 429 * 430 * Input: 431 * buf: C string containing whitespace- 432 * separated unsigned integer values 433 * representing the number of NFSD 434 * threads to start in each pool 435 * size: non-zero length of C string in @buf 436 * Output: 437 * On success: passed-in buffer filled with '\n'-terminated C 438 * string containing integer values representing the 439 * number of NFSD threads in each pool; 440 * return code is the size in bytes of the string 441 * On error: return code is zero or a negative errno value 442 */ 443 static ssize_t write_pool_threads(struct file *file, char *buf, size_t size) 444 { 445 /* if size > 0, look for an array of number of threads per node 446 * and apply them then write out number of threads per node as reply 447 */ 448 char *mesg = buf; 449 int i; 450 int rv; 451 int len; 452 int npools; 453 int *nthreads; 454 struct net *net = netns(file); 455 456 mutex_lock(&nfsd_mutex); 457 npools = nfsd_nrpools(net); 458 if (npools == 0) { 459 /* 460 * NFS is shut down. The admin can start it by 461 * writing to the threads file but NOT the pool_threads 462 * file, sorry. Report zero threads. 463 */ 464 mutex_unlock(&nfsd_mutex); 465 strcpy(buf, "0\n"); 466 return strlen(buf); 467 } 468 469 nthreads = kcalloc(npools, sizeof(int), GFP_KERNEL); 470 rv = -ENOMEM; 471 if (nthreads == NULL) 472 goto out_free; 473 474 if (size > 0) { 475 for (i = 0; i < npools; i++) { 476 rv = get_int(&mesg, &nthreads[i]); 477 if (rv == -ENOENT) 478 break; /* fewer numbers than pools */ 479 if (rv) 480 goto out_free; /* syntax error */ 481 rv = -EINVAL; 482 if (nthreads[i] < 0) 483 goto out_free; 484 trace_nfsd_ctl_pool_threads(net, i, nthreads[i]); 485 } 486 rv = nfsd_set_nrthreads(i, nthreads, net); 487 if (rv) 488 goto out_free; 489 } 490 491 rv = nfsd_get_nrthreads(npools, nthreads, net); 492 if (rv) 493 goto out_free; 494 495 mesg = buf; 496 size = SIMPLE_TRANSACTION_LIMIT; 497 for (i = 0; i < npools && size > 0; i++) { 498 snprintf(mesg, size, "%d%c", nthreads[i], (i == npools-1 ? '\n' : ' ')); 499 len = strlen(mesg); 500 size -= len; 501 mesg += len; 502 } 503 rv = mesg - buf; 504 out_free: 505 kfree(nthreads); 506 mutex_unlock(&nfsd_mutex); 507 return rv; 508 } 509 510 static ssize_t 511 nfsd_print_version_support(struct nfsd_net *nn, char *buf, int remaining, 512 const char *sep, unsigned vers, int minor) 513 { 514 const char *format = minor < 0 ? "%s%c%u" : "%s%c%u.%u"; 515 bool supported = !!nfsd_vers(nn, vers, NFSD_TEST); 516 517 if (vers == 4 && minor >= 0 && 518 !nfsd_minorversion(nn, minor, NFSD_TEST)) 519 supported = false; 520 if (minor == 0 && supported) 521 /* 522 * special case for backward compatability. 523 * +4.0 is never reported, it is implied by 524 * +4, unless -4.0 is present. 525 */ 526 return 0; 527 return snprintf(buf, remaining, format, sep, 528 supported ? '+' : '-', vers, minor); 529 } 530 531 static ssize_t __write_versions(struct file *file, char *buf, size_t size) 532 { 533 char *mesg = buf; 534 char *vers, *minorp, sign; 535 int len, num, remaining; 536 ssize_t tlen = 0; 537 char *sep; 538 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id); 539 540 if (size > 0) { 541 if (nn->nfsd_serv) 542 /* Cannot change versions without updating 543 * nn->nfsd_serv->sv_xdrsize, and reallocing 544 * rq_argp and rq_resp 545 */ 546 return -EBUSY; 547 if (buf[size-1] != '\n') 548 return -EINVAL; 549 buf[size-1] = 0; 550 trace_nfsd_ctl_version(netns(file), buf); 551 552 vers = mesg; 553 len = qword_get(&mesg, vers, size); 554 if (len <= 0) return -EINVAL; 555 do { 556 enum vers_op cmd; 557 unsigned minor; 558 sign = *vers; 559 if (sign == '+' || sign == '-') 560 num = simple_strtol((vers+1), &minorp, 0); 561 else 562 num = simple_strtol(vers, &minorp, 0); 563 if (*minorp == '.') { 564 if (num != 4) 565 return -EINVAL; 566 if (kstrtouint(minorp+1, 0, &minor) < 0) 567 return -EINVAL; 568 } 569 570 cmd = sign == '-' ? NFSD_CLEAR : NFSD_SET; 571 switch(num) { 572 #ifdef CONFIG_NFSD_V2 573 case 2: 574 #endif 575 case 3: 576 nfsd_vers(nn, num, cmd); 577 break; 578 case 4: 579 if (*minorp == '.') { 580 if (nfsd_minorversion(nn, minor, cmd) < 0) 581 return -EINVAL; 582 } else if ((cmd == NFSD_SET) != nfsd_vers(nn, num, NFSD_TEST)) { 583 /* 584 * Either we have +4 and no minors are enabled, 585 * or we have -4 and at least one minor is enabled. 586 * In either case, propagate 'cmd' to all minors. 587 */ 588 minor = 0; 589 while (nfsd_minorversion(nn, minor, cmd) >= 0) 590 minor++; 591 } 592 break; 593 default: 594 /* Ignore requests to disable non-existent versions */ 595 if (cmd == NFSD_SET) 596 return -EINVAL; 597 } 598 vers += len + 1; 599 } while ((len = qword_get(&mesg, vers, size)) > 0); 600 /* If all get turned off, turn them back on, as 601 * having no versions is BAD 602 */ 603 nfsd_reset_versions(nn); 604 } 605 606 /* Now write current state into reply buffer */ 607 sep = ""; 608 remaining = SIMPLE_TRANSACTION_LIMIT; 609 for (num=2 ; num <= 4 ; num++) { 610 int minor; 611 if (!nfsd_vers(nn, num, NFSD_AVAIL)) 612 continue; 613 614 minor = -1; 615 do { 616 len = nfsd_print_version_support(nn, buf, remaining, 617 sep, num, minor); 618 if (len >= remaining) 619 goto out; 620 remaining -= len; 621 buf += len; 622 tlen += len; 623 minor++; 624 if (len) 625 sep = " "; 626 } while (num == 4 && minor <= NFSD_SUPPORTED_MINOR_VERSION); 627 } 628 out: 629 len = snprintf(buf, remaining, "\n"); 630 if (len >= remaining) 631 return -EINVAL; 632 return tlen + len; 633 } 634 635 /* 636 * write_versions - Set or report the available NFS protocol versions 637 * 638 * Input: 639 * buf: ignored 640 * size: zero 641 * Output: 642 * On success: passed-in buffer filled with '\n'-terminated C 643 * string containing positive or negative integer 644 * values representing the current status of each 645 * protocol version; 646 * return code is the size in bytes of the string 647 * On error: return code is zero or a negative errno value 648 * 649 * OR 650 * 651 * Input: 652 * buf: C string containing whitespace- 653 * separated positive or negative 654 * integer values representing NFS 655 * protocol versions to enable ("+n") 656 * or disable ("-n") 657 * size: non-zero length of C string in @buf 658 * Output: 659 * On success: status of zero or more protocol versions has 660 * been updated; passed-in buffer filled with 661 * '\n'-terminated C string containing positive 662 * or negative integer values representing the 663 * current status of each protocol version; 664 * return code is the size in bytes of the string 665 * On error: return code is zero or a negative errno value 666 */ 667 static ssize_t write_versions(struct file *file, char *buf, size_t size) 668 { 669 ssize_t rv; 670 671 mutex_lock(&nfsd_mutex); 672 rv = __write_versions(file, buf, size); 673 mutex_unlock(&nfsd_mutex); 674 return rv; 675 } 676 677 /* 678 * Zero-length write. Return a list of NFSD's current listener 679 * transports. 680 */ 681 static ssize_t __write_ports_names(char *buf, struct net *net) 682 { 683 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 684 685 if (nn->nfsd_serv == NULL) 686 return 0; 687 return svc_xprt_names(nn->nfsd_serv, buf, SIMPLE_TRANSACTION_LIMIT); 688 } 689 690 /* 691 * A single 'fd' number was written, in which case it must be for 692 * a socket of a supported family/protocol, and we use it as an 693 * nfsd listener. 694 */ 695 static ssize_t __write_ports_addfd(char *buf, struct net *net, const struct cred *cred) 696 { 697 char *mesg = buf; 698 int fd, err; 699 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 700 struct svc_serv *serv; 701 702 err = get_int(&mesg, &fd); 703 if (err != 0 || fd < 0) 704 return -EINVAL; 705 trace_nfsd_ctl_ports_addfd(net, fd); 706 707 err = nfsd_create_serv(net); 708 if (err != 0) 709 return err; 710 711 serv = nn->nfsd_serv; 712 err = svc_addsock(serv, net, fd, buf, SIMPLE_TRANSACTION_LIMIT, cred); 713 714 if (!serv->sv_nrthreads && list_empty(&nn->nfsd_serv->sv_permsocks)) 715 nfsd_destroy_serv(net); 716 717 return err; 718 } 719 720 /* 721 * A transport listener is added by writing its transport name and 722 * a port number. 723 */ 724 static ssize_t __write_ports_addxprt(char *buf, struct net *net, const struct cred *cred) 725 { 726 char transport[16]; 727 struct svc_xprt *xprt; 728 int port, err; 729 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 730 struct svc_serv *serv; 731 732 if (sscanf(buf, "%15s %5u", transport, &port) != 2) 733 return -EINVAL; 734 735 if (port < 1 || port > USHRT_MAX) 736 return -EINVAL; 737 trace_nfsd_ctl_ports_addxprt(net, transport, port); 738 739 err = nfsd_create_serv(net); 740 if (err != 0) 741 return err; 742 743 serv = nn->nfsd_serv; 744 err = svc_xprt_create(serv, transport, net, 745 PF_INET, port, SVC_SOCK_ANONYMOUS, cred); 746 if (err < 0) 747 goto out_err; 748 749 err = svc_xprt_create(serv, transport, net, 750 PF_INET6, port, SVC_SOCK_ANONYMOUS, cred); 751 if (err < 0 && err != -EAFNOSUPPORT) 752 goto out_close; 753 754 return 0; 755 out_close: 756 xprt = svc_find_xprt(serv, transport, net, PF_INET, port); 757 if (xprt != NULL) { 758 svc_xprt_close(xprt); 759 svc_xprt_put(xprt); 760 } 761 out_err: 762 if (!serv->sv_nrthreads && list_empty(&nn->nfsd_serv->sv_permsocks)) 763 nfsd_destroy_serv(net); 764 765 return err; 766 } 767 768 static ssize_t __write_ports(struct file *file, char *buf, size_t size, 769 struct net *net) 770 { 771 if (size == 0) 772 return __write_ports_names(buf, net); 773 774 if (isdigit(buf[0])) 775 return __write_ports_addfd(buf, net, file->f_cred); 776 777 if (isalpha(buf[0])) 778 return __write_ports_addxprt(buf, net, file->f_cred); 779 780 return -EINVAL; 781 } 782 783 /* 784 * write_ports - Pass a socket file descriptor or transport name to listen on 785 * 786 * Input: 787 * buf: ignored 788 * size: zero 789 * Output: 790 * On success: passed-in buffer filled with a '\n'-terminated C 791 * string containing a whitespace-separated list of 792 * named NFSD listeners; 793 * return code is the size in bytes of the string 794 * On error: return code is zero or a negative errno value 795 * 796 * OR 797 * 798 * Input: 799 * buf: C string containing an unsigned 800 * integer value representing a bound 801 * but unconnected socket that is to be 802 * used as an NFSD listener; listen(3) 803 * must be called for a SOCK_STREAM 804 * socket, otherwise it is ignored 805 * size: non-zero length of C string in @buf 806 * Output: 807 * On success: NFS service is started; 808 * passed-in buffer filled with a '\n'-terminated C 809 * string containing a unique alphanumeric name of 810 * the listener; 811 * return code is the size in bytes of the string 812 * On error: return code is a negative errno value 813 * 814 * OR 815 * 816 * Input: 817 * buf: C string containing a transport 818 * name and an unsigned integer value 819 * representing the port to listen on, 820 * separated by whitespace 821 * size: non-zero length of C string in @buf 822 * Output: 823 * On success: returns zero; NFS service is started 824 * On error: return code is a negative errno value 825 */ 826 static ssize_t write_ports(struct file *file, char *buf, size_t size) 827 { 828 ssize_t rv; 829 830 mutex_lock(&nfsd_mutex); 831 rv = __write_ports(file, buf, size, netns(file)); 832 mutex_unlock(&nfsd_mutex); 833 return rv; 834 } 835 836 837 int nfsd_max_blksize; 838 839 /* 840 * write_maxblksize - Set or report the current NFS blksize 841 * 842 * Input: 843 * buf: ignored 844 * size: zero 845 * 846 * OR 847 * 848 * Input: 849 * buf: C string containing an unsigned 850 * integer value representing the new 851 * NFS blksize 852 * size: non-zero length of C string in @buf 853 * Output: 854 * On success: passed-in buffer filled with '\n'-terminated C string 855 * containing numeric value of the current NFS blksize 856 * setting; 857 * return code is the size in bytes of the string 858 * On error: return code is zero or a negative errno value 859 */ 860 static ssize_t write_maxblksize(struct file *file, char *buf, size_t size) 861 { 862 char *mesg = buf; 863 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id); 864 865 if (size > 0) { 866 int bsize; 867 int rv = get_int(&mesg, &bsize); 868 if (rv) 869 return rv; 870 trace_nfsd_ctl_maxblksize(netns(file), bsize); 871 872 /* force bsize into allowed range and 873 * required alignment. 874 */ 875 bsize = max_t(int, bsize, 1024); 876 bsize = min_t(int, bsize, NFSSVC_MAXBLKSIZE); 877 bsize &= ~(1024-1); 878 mutex_lock(&nfsd_mutex); 879 if (nn->nfsd_serv) { 880 mutex_unlock(&nfsd_mutex); 881 return -EBUSY; 882 } 883 nfsd_max_blksize = bsize; 884 mutex_unlock(&nfsd_mutex); 885 } 886 887 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", 888 nfsd_max_blksize); 889 } 890 891 /* 892 * write_maxconn - Set or report the current max number of connections 893 * 894 * Input: 895 * buf: ignored 896 * size: zero 897 * OR 898 * 899 * Input: 900 * buf: C string containing an unsigned 901 * integer value representing the new 902 * number of max connections 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 max_connections setting 907 * for this net namespace; 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_maxconn(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 unsigned int maxconn = nn->max_connections; 916 917 if (size > 0) { 918 int rv = get_uint(&mesg, &maxconn); 919 920 if (rv) 921 return rv; 922 trace_nfsd_ctl_maxconn(netns(file), maxconn); 923 nn->max_connections = maxconn; 924 } 925 926 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%u\n", maxconn); 927 } 928 929 #ifdef CONFIG_NFSD_V4 930 static ssize_t __nfsd4_write_time(struct file *file, char *buf, size_t size, 931 time64_t *time, struct nfsd_net *nn) 932 { 933 struct dentry *dentry = file_dentry(file); 934 char *mesg = buf; 935 int rv, i; 936 937 if (size > 0) { 938 if (nn->nfsd_serv) 939 return -EBUSY; 940 rv = get_int(&mesg, &i); 941 if (rv) 942 return rv; 943 trace_nfsd_ctl_time(netns(file), dentry->d_name.name, 944 dentry->d_name.len, i); 945 946 /* 947 * Some sanity checking. We don't have a reason for 948 * these particular numbers, but problems with the 949 * extremes are: 950 * - Too short: the briefest network outage may 951 * cause clients to lose all their locks. Also, 952 * the frequent polling may be wasteful. 953 * - Too long: do you really want reboot recovery 954 * to take more than an hour? Or to make other 955 * clients wait an hour before being able to 956 * revoke a dead client's locks? 957 */ 958 if (i < 10 || i > 3600) 959 return -EINVAL; 960 *time = i; 961 } 962 963 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%lld\n", *time); 964 } 965 966 static ssize_t nfsd4_write_time(struct file *file, char *buf, size_t size, 967 time64_t *time, struct nfsd_net *nn) 968 { 969 ssize_t rv; 970 971 mutex_lock(&nfsd_mutex); 972 rv = __nfsd4_write_time(file, buf, size, time, nn); 973 mutex_unlock(&nfsd_mutex); 974 return rv; 975 } 976 977 /* 978 * write_leasetime - Set or report the current NFSv4 lease time 979 * 980 * Input: 981 * buf: ignored 982 * size: zero 983 * 984 * OR 985 * 986 * Input: 987 * buf: C string containing an unsigned 988 * integer value representing the new 989 * NFSv4 lease expiry time 990 * size: non-zero length of C string in @buf 991 * Output: 992 * On success: passed-in buffer filled with '\n'-terminated C 993 * string containing unsigned integer value of the 994 * current lease expiry time; 995 * return code is the size in bytes of the string 996 * On error: return code is zero or a negative errno value 997 */ 998 static ssize_t write_leasetime(struct file *file, char *buf, size_t size) 999 { 1000 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id); 1001 return nfsd4_write_time(file, buf, size, &nn->nfsd4_lease, nn); 1002 } 1003 1004 /* 1005 * write_gracetime - Set or report current NFSv4 grace period time 1006 * 1007 * As above, but sets the time of the NFSv4 grace period. 1008 * 1009 * Note this should never be set to less than the *previous* 1010 * lease-period time, but we don't try to enforce this. (In the common 1011 * case (a new boot), we don't know what the previous lease time was 1012 * anyway.) 1013 */ 1014 static ssize_t write_gracetime(struct file *file, char *buf, size_t size) 1015 { 1016 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id); 1017 return nfsd4_write_time(file, buf, size, &nn->nfsd4_grace, nn); 1018 } 1019 1020 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING 1021 static ssize_t __write_recoverydir(struct file *file, char *buf, size_t size, 1022 struct nfsd_net *nn) 1023 { 1024 char *mesg = buf; 1025 char *recdir; 1026 int len, status; 1027 1028 if (size > 0) { 1029 if (nn->nfsd_serv) 1030 return -EBUSY; 1031 if (size > PATH_MAX || buf[size-1] != '\n') 1032 return -EINVAL; 1033 buf[size-1] = 0; 1034 1035 recdir = mesg; 1036 len = qword_get(&mesg, recdir, size); 1037 if (len <= 0) 1038 return -EINVAL; 1039 trace_nfsd_ctl_recoverydir(netns(file), recdir); 1040 1041 status = nfs4_reset_recoverydir(recdir); 1042 if (status) 1043 return status; 1044 } 1045 1046 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%s\n", 1047 nfs4_recoverydir()); 1048 } 1049 1050 /* 1051 * write_recoverydir - Set or report the pathname of the recovery directory 1052 * 1053 * Input: 1054 * buf: ignored 1055 * size: zero 1056 * 1057 * OR 1058 * 1059 * Input: 1060 * buf: C string containing the pathname 1061 * of the directory on a local file 1062 * system containing permanent NFSv4 1063 * recovery data 1064 * size: non-zero length of C string in @buf 1065 * Output: 1066 * On success: passed-in buffer filled with '\n'-terminated C string 1067 * containing the current recovery pathname setting; 1068 * return code is the size in bytes of the string 1069 * On error: return code is zero or a negative errno value 1070 */ 1071 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size) 1072 { 1073 ssize_t rv; 1074 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id); 1075 1076 mutex_lock(&nfsd_mutex); 1077 rv = __write_recoverydir(file, buf, size, nn); 1078 mutex_unlock(&nfsd_mutex); 1079 return rv; 1080 } 1081 #endif 1082 1083 /* 1084 * write_v4_end_grace - release grace period for nfsd's v4.x lock manager 1085 * 1086 * Input: 1087 * buf: ignored 1088 * size: zero 1089 * OR 1090 * 1091 * Input: 1092 * buf: any value 1093 * size: non-zero length of C string in @buf 1094 * Output: 1095 * passed-in buffer filled with "Y" or "N" with a newline 1096 * and NULL-terminated C string. This indicates whether 1097 * the grace period has ended in the current net 1098 * namespace. Return code is the size in bytes of the 1099 * string. Writing a string that starts with 'Y', 'y', or 1100 * '1' to the file will end the grace period for nfsd's v4 1101 * lock manager. 1102 */ 1103 static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size) 1104 { 1105 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id); 1106 1107 if (size > 0) { 1108 switch(buf[0]) { 1109 case 'Y': 1110 case 'y': 1111 case '1': 1112 if (!nn->nfsd_serv) 1113 return -EBUSY; 1114 trace_nfsd_end_grace(netns(file)); 1115 nfsd4_end_grace(nn); 1116 break; 1117 default: 1118 return -EINVAL; 1119 } 1120 } 1121 1122 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%c\n", 1123 nn->grace_ended ? 'Y' : 'N'); 1124 } 1125 1126 #endif 1127 1128 /*----------------------------------------------------------------------------*/ 1129 /* 1130 * populating the filesystem. 1131 */ 1132 1133 /* Basically copying rpc_get_inode. */ 1134 static struct inode *nfsd_get_inode(struct super_block *sb, umode_t mode) 1135 { 1136 struct inode *inode = new_inode(sb); 1137 if (!inode) 1138 return NULL; 1139 /* Following advice from simple_fill_super documentation: */ 1140 inode->i_ino = iunique(sb, NFSD_MaxReserved); 1141 inode->i_mode = mode; 1142 simple_inode_init_ts(inode); 1143 switch (mode & S_IFMT) { 1144 case S_IFDIR: 1145 inode->i_fop = &simple_dir_operations; 1146 inode->i_op = &simple_dir_inode_operations; 1147 inc_nlink(inode); 1148 break; 1149 case S_IFLNK: 1150 inode->i_op = &simple_symlink_inode_operations; 1151 break; 1152 default: 1153 break; 1154 } 1155 return inode; 1156 } 1157 1158 static int __nfsd_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode, struct nfsdfs_client *ncl) 1159 { 1160 struct inode *inode; 1161 1162 inode = nfsd_get_inode(dir->i_sb, mode); 1163 if (!inode) 1164 return -ENOMEM; 1165 if (ncl) { 1166 inode->i_private = ncl; 1167 kref_get(&ncl->cl_ref); 1168 } 1169 d_add(dentry, inode); 1170 inc_nlink(dir); 1171 fsnotify_mkdir(dir, dentry); 1172 return 0; 1173 } 1174 1175 static struct dentry *nfsd_mkdir(struct dentry *parent, struct nfsdfs_client *ncl, char *name) 1176 { 1177 struct inode *dir = parent->d_inode; 1178 struct dentry *dentry; 1179 int ret = -ENOMEM; 1180 1181 inode_lock(dir); 1182 dentry = d_alloc_name(parent, name); 1183 if (!dentry) 1184 goto out_err; 1185 ret = __nfsd_mkdir(d_inode(parent), dentry, S_IFDIR | 0600, ncl); 1186 if (ret) 1187 goto out_err; 1188 out: 1189 inode_unlock(dir); 1190 return dentry; 1191 out_err: 1192 dput(dentry); 1193 dentry = ERR_PTR(ret); 1194 goto out; 1195 } 1196 1197 #if IS_ENABLED(CONFIG_SUNRPC_GSS) 1198 static int __nfsd_symlink(struct inode *dir, struct dentry *dentry, 1199 umode_t mode, const char *content) 1200 { 1201 struct inode *inode; 1202 1203 inode = nfsd_get_inode(dir->i_sb, mode); 1204 if (!inode) 1205 return -ENOMEM; 1206 1207 inode->i_link = (char *)content; 1208 inode->i_size = strlen(content); 1209 1210 d_add(dentry, inode); 1211 inc_nlink(dir); 1212 fsnotify_create(dir, dentry); 1213 return 0; 1214 } 1215 1216 /* 1217 * @content is assumed to be a NUL-terminated string that lives 1218 * longer than the symlink itself. 1219 */ 1220 static void _nfsd_symlink(struct dentry *parent, const char *name, 1221 const char *content) 1222 { 1223 struct inode *dir = parent->d_inode; 1224 struct dentry *dentry; 1225 int ret; 1226 1227 inode_lock(dir); 1228 dentry = d_alloc_name(parent, name); 1229 if (!dentry) 1230 goto out; 1231 ret = __nfsd_symlink(d_inode(parent), dentry, S_IFLNK | 0777, content); 1232 if (ret) 1233 dput(dentry); 1234 out: 1235 inode_unlock(dir); 1236 } 1237 #else 1238 static inline void _nfsd_symlink(struct dentry *parent, const char *name, 1239 const char *content) 1240 { 1241 } 1242 1243 #endif 1244 1245 static void clear_ncl(struct inode *inode) 1246 { 1247 struct nfsdfs_client *ncl = inode->i_private; 1248 1249 inode->i_private = NULL; 1250 kref_put(&ncl->cl_ref, ncl->cl_release); 1251 } 1252 1253 static struct nfsdfs_client *__get_nfsdfs_client(struct inode *inode) 1254 { 1255 struct nfsdfs_client *nc = inode->i_private; 1256 1257 if (nc) 1258 kref_get(&nc->cl_ref); 1259 return nc; 1260 } 1261 1262 struct nfsdfs_client *get_nfsdfs_client(struct inode *inode) 1263 { 1264 struct nfsdfs_client *nc; 1265 1266 inode_lock_shared(inode); 1267 nc = __get_nfsdfs_client(inode); 1268 inode_unlock_shared(inode); 1269 return nc; 1270 } 1271 /* from __rpc_unlink */ 1272 static void nfsdfs_remove_file(struct inode *dir, struct dentry *dentry) 1273 { 1274 int ret; 1275 1276 clear_ncl(d_inode(dentry)); 1277 dget(dentry); 1278 ret = simple_unlink(dir, dentry); 1279 d_drop(dentry); 1280 fsnotify_unlink(dir, dentry); 1281 dput(dentry); 1282 WARN_ON_ONCE(ret); 1283 } 1284 1285 static void nfsdfs_remove_files(struct dentry *root) 1286 { 1287 struct dentry *dentry, *tmp; 1288 1289 list_for_each_entry_safe(dentry, tmp, &root->d_subdirs, d_child) { 1290 if (!simple_positive(dentry)) { 1291 WARN_ON_ONCE(1); /* I think this can't happen? */ 1292 continue; 1293 } 1294 nfsdfs_remove_file(d_inode(root), dentry); 1295 } 1296 } 1297 1298 /* XXX: cut'n'paste from simple_fill_super; figure out if we could share 1299 * code instead. */ 1300 static int nfsdfs_create_files(struct dentry *root, 1301 const struct tree_descr *files, 1302 struct dentry **fdentries) 1303 { 1304 struct inode *dir = d_inode(root); 1305 struct inode *inode; 1306 struct dentry *dentry; 1307 int i; 1308 1309 inode_lock(dir); 1310 for (i = 0; files->name && files->name[0]; i++, files++) { 1311 dentry = d_alloc_name(root, files->name); 1312 if (!dentry) 1313 goto out; 1314 inode = nfsd_get_inode(d_inode(root)->i_sb, 1315 S_IFREG | files->mode); 1316 if (!inode) { 1317 dput(dentry); 1318 goto out; 1319 } 1320 inode->i_fop = files->ops; 1321 inode->i_private = __get_nfsdfs_client(dir); 1322 d_add(dentry, inode); 1323 fsnotify_create(dir, dentry); 1324 if (fdentries) 1325 fdentries[i] = dentry; 1326 } 1327 inode_unlock(dir); 1328 return 0; 1329 out: 1330 nfsdfs_remove_files(root); 1331 inode_unlock(dir); 1332 return -ENOMEM; 1333 } 1334 1335 /* on success, returns positive number unique to that client. */ 1336 struct dentry *nfsd_client_mkdir(struct nfsd_net *nn, 1337 struct nfsdfs_client *ncl, u32 id, 1338 const struct tree_descr *files, 1339 struct dentry **fdentries) 1340 { 1341 struct dentry *dentry; 1342 char name[11]; 1343 int ret; 1344 1345 sprintf(name, "%u", id); 1346 1347 dentry = nfsd_mkdir(nn->nfsd_client_dir, ncl, name); 1348 if (IS_ERR(dentry)) /* XXX: tossing errors? */ 1349 return NULL; 1350 ret = nfsdfs_create_files(dentry, files, fdentries); 1351 if (ret) { 1352 nfsd_client_rmdir(dentry); 1353 return NULL; 1354 } 1355 return dentry; 1356 } 1357 1358 /* Taken from __rpc_rmdir: */ 1359 void nfsd_client_rmdir(struct dentry *dentry) 1360 { 1361 struct inode *dir = d_inode(dentry->d_parent); 1362 struct inode *inode = d_inode(dentry); 1363 int ret; 1364 1365 inode_lock(dir); 1366 nfsdfs_remove_files(dentry); 1367 clear_ncl(inode); 1368 dget(dentry); 1369 ret = simple_rmdir(dir, dentry); 1370 WARN_ON_ONCE(ret); 1371 d_drop(dentry); 1372 fsnotify_rmdir(dir, dentry); 1373 dput(dentry); 1374 inode_unlock(dir); 1375 } 1376 1377 static int nfsd_fill_super(struct super_block *sb, struct fs_context *fc) 1378 { 1379 struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, 1380 nfsd_net_id); 1381 struct dentry *dentry; 1382 int ret; 1383 1384 static const struct tree_descr nfsd_files[] = { 1385 [NFSD_List] = {"exports", &exports_nfsd_operations, S_IRUGO}, 1386 /* Per-export io stats use same ops as exports file */ 1387 [NFSD_Export_Stats] = {"export_stats", &exports_nfsd_operations, S_IRUGO}, 1388 [NFSD_Export_features] = {"export_features", 1389 &export_features_fops, S_IRUGO}, 1390 [NFSD_FO_UnlockIP] = {"unlock_ip", 1391 &transaction_ops, S_IWUSR|S_IRUSR}, 1392 [NFSD_FO_UnlockFS] = {"unlock_filesystem", 1393 &transaction_ops, S_IWUSR|S_IRUSR}, 1394 [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR}, 1395 [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR}, 1396 [NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR}, 1397 [NFSD_Pool_Stats] = {"pool_stats", &pool_stats_operations, S_IRUGO}, 1398 [NFSD_Reply_Cache_Stats] = {"reply_cache_stats", 1399 &nfsd_reply_cache_stats_fops, S_IRUGO}, 1400 [NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR}, 1401 [NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO}, 1402 [NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO}, 1403 [NFSD_MaxConnections] = {"max_connections", &transaction_ops, S_IWUSR|S_IRUGO}, 1404 [NFSD_Filecache] = {"filecache", &nfsd_file_cache_stats_fops, S_IRUGO}, 1405 #ifdef CONFIG_NFSD_V4 1406 [NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR}, 1407 [NFSD_Gracetime] = {"nfsv4gracetime", &transaction_ops, S_IWUSR|S_IRUSR}, 1408 [NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR}, 1409 [NFSD_V4EndGrace] = {"v4_end_grace", &transaction_ops, S_IWUSR|S_IRUGO}, 1410 #endif 1411 /* last one */ {""} 1412 }; 1413 1414 ret = simple_fill_super(sb, 0x6e667364, nfsd_files); 1415 if (ret) 1416 return ret; 1417 _nfsd_symlink(sb->s_root, "supported_krb5_enctypes", 1418 "/proc/net/rpc/gss_krb5_enctypes"); 1419 dentry = nfsd_mkdir(sb->s_root, NULL, "clients"); 1420 if (IS_ERR(dentry)) 1421 return PTR_ERR(dentry); 1422 nn->nfsd_client_dir = dentry; 1423 return 0; 1424 } 1425 1426 static int nfsd_fs_get_tree(struct fs_context *fc) 1427 { 1428 return get_tree_keyed(fc, nfsd_fill_super, get_net(fc->net_ns)); 1429 } 1430 1431 static void nfsd_fs_free_fc(struct fs_context *fc) 1432 { 1433 if (fc->s_fs_info) 1434 put_net(fc->s_fs_info); 1435 } 1436 1437 static const struct fs_context_operations nfsd_fs_context_ops = { 1438 .free = nfsd_fs_free_fc, 1439 .get_tree = nfsd_fs_get_tree, 1440 }; 1441 1442 static int nfsd_init_fs_context(struct fs_context *fc) 1443 { 1444 put_user_ns(fc->user_ns); 1445 fc->user_ns = get_user_ns(fc->net_ns->user_ns); 1446 fc->ops = &nfsd_fs_context_ops; 1447 return 0; 1448 } 1449 1450 static void nfsd_umount(struct super_block *sb) 1451 { 1452 struct net *net = sb->s_fs_info; 1453 1454 nfsd_shutdown_threads(net); 1455 1456 kill_litter_super(sb); 1457 put_net(net); 1458 } 1459 1460 static struct file_system_type nfsd_fs_type = { 1461 .owner = THIS_MODULE, 1462 .name = "nfsd", 1463 .init_fs_context = nfsd_init_fs_context, 1464 .kill_sb = nfsd_umount, 1465 }; 1466 MODULE_ALIAS_FS("nfsd"); 1467 1468 #ifdef CONFIG_PROC_FS 1469 1470 static int exports_proc_open(struct inode *inode, struct file *file) 1471 { 1472 return exports_net_open(current->nsproxy->net_ns, file); 1473 } 1474 1475 static const struct proc_ops exports_proc_ops = { 1476 .proc_open = exports_proc_open, 1477 .proc_read = seq_read, 1478 .proc_lseek = seq_lseek, 1479 .proc_release = seq_release, 1480 }; 1481 1482 static int create_proc_exports_entry(void) 1483 { 1484 struct proc_dir_entry *entry; 1485 1486 entry = proc_mkdir("fs/nfs", NULL); 1487 if (!entry) 1488 return -ENOMEM; 1489 entry = proc_create("exports", 0, entry, &exports_proc_ops); 1490 if (!entry) { 1491 remove_proc_entry("fs/nfs", NULL); 1492 return -ENOMEM; 1493 } 1494 return 0; 1495 } 1496 #else /* CONFIG_PROC_FS */ 1497 static int create_proc_exports_entry(void) 1498 { 1499 return 0; 1500 } 1501 #endif 1502 1503 unsigned int nfsd_net_id; 1504 1505 /** 1506 * nfsd_nl_rpc_status_get_start - Prepare rpc_status_get dumpit 1507 * @cb: netlink metadata and command arguments 1508 * 1509 * Return values: 1510 * %0: The rpc_status_get command may proceed 1511 * %-ENODEV: There is no NFSD running in this namespace 1512 */ 1513 int nfsd_nl_rpc_status_get_start(struct netlink_callback *cb) 1514 { 1515 struct nfsd_net *nn = net_generic(sock_net(cb->skb->sk), nfsd_net_id); 1516 int ret = -ENODEV; 1517 1518 mutex_lock(&nfsd_mutex); 1519 if (nn->nfsd_serv) 1520 ret = 0; 1521 else 1522 mutex_unlock(&nfsd_mutex); 1523 1524 return ret; 1525 } 1526 1527 static int nfsd_genl_rpc_status_compose_msg(struct sk_buff *skb, 1528 struct netlink_callback *cb, 1529 struct nfsd_genl_rqstp *rqstp) 1530 { 1531 void *hdr; 1532 u32 i; 1533 1534 hdr = genlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq, 1535 &nfsd_nl_family, 0, NFSD_CMD_RPC_STATUS_GET); 1536 if (!hdr) 1537 return -ENOBUFS; 1538 1539 if (nla_put_be32(skb, NFSD_A_RPC_STATUS_XID, rqstp->rq_xid) || 1540 nla_put_u32(skb, NFSD_A_RPC_STATUS_FLAGS, rqstp->rq_flags) || 1541 nla_put_u32(skb, NFSD_A_RPC_STATUS_PROG, rqstp->rq_prog) || 1542 nla_put_u32(skb, NFSD_A_RPC_STATUS_PROC, rqstp->rq_proc) || 1543 nla_put_u8(skb, NFSD_A_RPC_STATUS_VERSION, rqstp->rq_vers) || 1544 nla_put_s64(skb, NFSD_A_RPC_STATUS_SERVICE_TIME, 1545 ktime_to_us(rqstp->rq_stime), 1546 NFSD_A_RPC_STATUS_PAD)) 1547 return -ENOBUFS; 1548 1549 switch (rqstp->rq_saddr.sa_family) { 1550 case AF_INET: { 1551 const struct sockaddr_in *s_in, *d_in; 1552 1553 s_in = (const struct sockaddr_in *)&rqstp->rq_saddr; 1554 d_in = (const struct sockaddr_in *)&rqstp->rq_daddr; 1555 if (nla_put_in_addr(skb, NFSD_A_RPC_STATUS_SADDR4, 1556 s_in->sin_addr.s_addr) || 1557 nla_put_in_addr(skb, NFSD_A_RPC_STATUS_DADDR4, 1558 d_in->sin_addr.s_addr) || 1559 nla_put_be16(skb, NFSD_A_RPC_STATUS_SPORT, 1560 s_in->sin_port) || 1561 nla_put_be16(skb, NFSD_A_RPC_STATUS_DPORT, 1562 d_in->sin_port)) 1563 return -ENOBUFS; 1564 break; 1565 } 1566 case AF_INET6: { 1567 const struct sockaddr_in6 *s_in, *d_in; 1568 1569 s_in = (const struct sockaddr_in6 *)&rqstp->rq_saddr; 1570 d_in = (const struct sockaddr_in6 *)&rqstp->rq_daddr; 1571 if (nla_put_in6_addr(skb, NFSD_A_RPC_STATUS_SADDR6, 1572 &s_in->sin6_addr) || 1573 nla_put_in6_addr(skb, NFSD_A_RPC_STATUS_DADDR6, 1574 &d_in->sin6_addr) || 1575 nla_put_be16(skb, NFSD_A_RPC_STATUS_SPORT, 1576 s_in->sin6_port) || 1577 nla_put_be16(skb, NFSD_A_RPC_STATUS_DPORT, 1578 d_in->sin6_port)) 1579 return -ENOBUFS; 1580 break; 1581 } 1582 } 1583 1584 for (i = 0; i < rqstp->rq_opcnt; i++) 1585 if (nla_put_u32(skb, NFSD_A_RPC_STATUS_COMPOUND_OPS, 1586 rqstp->rq_opnum[i])) 1587 return -ENOBUFS; 1588 1589 genlmsg_end(skb, hdr); 1590 return 0; 1591 } 1592 1593 /** 1594 * nfsd_nl_rpc_status_get_dumpit - Handle rpc_status_get dumpit 1595 * @skb: reply buffer 1596 * @cb: netlink metadata and command arguments 1597 * 1598 * Returns the size of the reply or a negative errno. 1599 */ 1600 int nfsd_nl_rpc_status_get_dumpit(struct sk_buff *skb, 1601 struct netlink_callback *cb) 1602 { 1603 struct nfsd_net *nn = net_generic(sock_net(skb->sk), nfsd_net_id); 1604 int i, ret, rqstp_index = 0; 1605 1606 rcu_read_lock(); 1607 1608 for (i = 0; i < nn->nfsd_serv->sv_nrpools; i++) { 1609 struct svc_rqst *rqstp; 1610 1611 if (i < cb->args[0]) /* already consumed */ 1612 continue; 1613 1614 rqstp_index = 0; 1615 list_for_each_entry_rcu(rqstp, 1616 &nn->nfsd_serv->sv_pools[i].sp_all_threads, 1617 rq_all) { 1618 struct nfsd_genl_rqstp genl_rqstp; 1619 unsigned int status_counter; 1620 1621 if (rqstp_index++ < cb->args[1]) /* already consumed */ 1622 continue; 1623 /* 1624 * Acquire rq_status_counter before parsing the rqst 1625 * fields. rq_status_counter is set to an odd value in 1626 * order to notify the consumers the rqstp fields are 1627 * meaningful. 1628 */ 1629 status_counter = 1630 smp_load_acquire(&rqstp->rq_status_counter); 1631 if (!(status_counter & 1)) 1632 continue; 1633 1634 genl_rqstp.rq_xid = rqstp->rq_xid; 1635 genl_rqstp.rq_flags = rqstp->rq_flags; 1636 genl_rqstp.rq_vers = rqstp->rq_vers; 1637 genl_rqstp.rq_prog = rqstp->rq_prog; 1638 genl_rqstp.rq_proc = rqstp->rq_proc; 1639 genl_rqstp.rq_stime = rqstp->rq_stime; 1640 genl_rqstp.rq_opcnt = 0; 1641 memcpy(&genl_rqstp.rq_daddr, svc_daddr(rqstp), 1642 sizeof(struct sockaddr)); 1643 memcpy(&genl_rqstp.rq_saddr, svc_addr(rqstp), 1644 sizeof(struct sockaddr)); 1645 1646 #ifdef CONFIG_NFSD_V4 1647 if (rqstp->rq_vers == NFS4_VERSION && 1648 rqstp->rq_proc == NFSPROC4_COMPOUND) { 1649 /* NFSv4 compound */ 1650 struct nfsd4_compoundargs *args; 1651 int j; 1652 1653 args = rqstp->rq_argp; 1654 genl_rqstp.rq_opcnt = args->opcnt; 1655 for (j = 0; j < genl_rqstp.rq_opcnt; j++) 1656 genl_rqstp.rq_opnum[j] = 1657 args->ops[j].opnum; 1658 } 1659 #endif /* CONFIG_NFSD_V4 */ 1660 1661 /* 1662 * Acquire rq_status_counter before reporting the rqst 1663 * fields to the user. 1664 */ 1665 if (smp_load_acquire(&rqstp->rq_status_counter) != 1666 status_counter) 1667 continue; 1668 1669 ret = nfsd_genl_rpc_status_compose_msg(skb, cb, 1670 &genl_rqstp); 1671 if (ret) 1672 goto out; 1673 } 1674 } 1675 1676 cb->args[0] = i; 1677 cb->args[1] = rqstp_index; 1678 ret = skb->len; 1679 out: 1680 rcu_read_unlock(); 1681 1682 return ret; 1683 } 1684 1685 /** 1686 * nfsd_nl_rpc_status_get_done - rpc_status_get dumpit post-processing 1687 * @cb: netlink metadata and command arguments 1688 * 1689 * Return values: 1690 * %0: Success 1691 */ 1692 int nfsd_nl_rpc_status_get_done(struct netlink_callback *cb) 1693 { 1694 mutex_unlock(&nfsd_mutex); 1695 1696 return 0; 1697 } 1698 1699 /** 1700 * nfsd_net_init - Prepare the nfsd_net portion of a new net namespace 1701 * @net: a freshly-created network namespace 1702 * 1703 * This information stays around as long as the network namespace is 1704 * alive whether or not there is an NFSD instance running in the 1705 * namespace. 1706 * 1707 * Returns zero on success, or a negative errno otherwise. 1708 */ 1709 static __net_init int nfsd_net_init(struct net *net) 1710 { 1711 int retval; 1712 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 1713 1714 retval = nfsd_export_init(net); 1715 if (retval) 1716 goto out_export_error; 1717 retval = nfsd_idmap_init(net); 1718 if (retval) 1719 goto out_idmap_error; 1720 retval = nfsd_net_reply_cache_init(nn); 1721 if (retval) 1722 goto out_repcache_error; 1723 nn->nfsd_versions = NULL; 1724 nn->nfsd4_minorversions = NULL; 1725 nfsd4_init_leases_net(nn); 1726 get_random_bytes(&nn->siphash_key, sizeof(nn->siphash_key)); 1727 seqlock_init(&nn->writeverf_lock); 1728 1729 return 0; 1730 1731 out_repcache_error: 1732 nfsd_idmap_shutdown(net); 1733 out_idmap_error: 1734 nfsd_export_shutdown(net); 1735 out_export_error: 1736 return retval; 1737 } 1738 1739 /** 1740 * nfsd_net_exit - Release the nfsd_net portion of a net namespace 1741 * @net: a network namespace that is about to be destroyed 1742 * 1743 */ 1744 static __net_exit void nfsd_net_exit(struct net *net) 1745 { 1746 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 1747 1748 nfsd_net_reply_cache_destroy(nn); 1749 nfsd_idmap_shutdown(net); 1750 nfsd_export_shutdown(net); 1751 nfsd_netns_free_versions(nn); 1752 } 1753 1754 static struct pernet_operations nfsd_net_ops = { 1755 .init = nfsd_net_init, 1756 .exit = nfsd_net_exit, 1757 .id = &nfsd_net_id, 1758 .size = sizeof(struct nfsd_net), 1759 }; 1760 1761 static int __init init_nfsd(void) 1762 { 1763 int retval; 1764 1765 retval = nfsd4_init_slabs(); 1766 if (retval) 1767 return retval; 1768 retval = nfsd4_init_pnfs(); 1769 if (retval) 1770 goto out_free_slabs; 1771 retval = nfsd_stat_init(); /* Statistics */ 1772 if (retval) 1773 goto out_free_pnfs; 1774 retval = nfsd_drc_slab_create(); 1775 if (retval) 1776 goto out_free_stat; 1777 nfsd_lockd_init(); /* lockd->nfsd callbacks */ 1778 retval = create_proc_exports_entry(); 1779 if (retval) 1780 goto out_free_lockd; 1781 retval = register_pernet_subsys(&nfsd_net_ops); 1782 if (retval < 0) 1783 goto out_free_exports; 1784 retval = register_cld_notifier(); 1785 if (retval) 1786 goto out_free_subsys; 1787 retval = nfsd4_create_laundry_wq(); 1788 if (retval) 1789 goto out_free_cld; 1790 retval = register_filesystem(&nfsd_fs_type); 1791 if (retval) 1792 goto out_free_all; 1793 retval = genl_register_family(&nfsd_nl_family); 1794 if (retval) 1795 goto out_free_all; 1796 1797 return 0; 1798 out_free_all: 1799 nfsd4_destroy_laundry_wq(); 1800 out_free_cld: 1801 unregister_cld_notifier(); 1802 out_free_subsys: 1803 unregister_pernet_subsys(&nfsd_net_ops); 1804 out_free_exports: 1805 remove_proc_entry("fs/nfs/exports", NULL); 1806 remove_proc_entry("fs/nfs", NULL); 1807 out_free_lockd: 1808 nfsd_lockd_shutdown(); 1809 nfsd_drc_slab_free(); 1810 out_free_stat: 1811 nfsd_stat_shutdown(); 1812 out_free_pnfs: 1813 nfsd4_exit_pnfs(); 1814 out_free_slabs: 1815 nfsd4_free_slabs(); 1816 return retval; 1817 } 1818 1819 static void __exit exit_nfsd(void) 1820 { 1821 genl_unregister_family(&nfsd_nl_family); 1822 unregister_filesystem(&nfsd_fs_type); 1823 nfsd4_destroy_laundry_wq(); 1824 unregister_cld_notifier(); 1825 unregister_pernet_subsys(&nfsd_net_ops); 1826 nfsd_drc_slab_free(); 1827 remove_proc_entry("fs/nfs/exports", NULL); 1828 remove_proc_entry("fs/nfs", NULL); 1829 nfsd_stat_shutdown(); 1830 nfsd_lockd_shutdown(); 1831 nfsd4_free_slabs(); 1832 nfsd4_exit_pnfs(); 1833 } 1834 1835 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>"); 1836 MODULE_DESCRIPTION("In-kernel NFS server"); 1837 MODULE_LICENSE("GPL"); 1838 module_init(init_nfsd) 1839 module_exit(exit_nfsd) 1840