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 12 #include <linux/sunrpc/svcsock.h> 13 #include <linux/lockd/lockd.h> 14 #include <linux/sunrpc/addr.h> 15 #include <linux/sunrpc/gss_api.h> 16 #include <linux/sunrpc/gss_krb5_enctypes.h> 17 #include <linux/sunrpc/rpc_pipe_fs.h> 18 #include <linux/module.h> 19 20 #include "idmap.h" 21 #include "nfsd.h" 22 #include "cache.h" 23 #include "state.h" 24 #include "netns.h" 25 #include "pnfs.h" 26 27 /* 28 * We have a single directory with several nodes in it. 29 */ 30 enum { 31 NFSD_Root = 1, 32 NFSD_List, 33 NFSD_Export_features, 34 NFSD_Fh, 35 NFSD_FO_UnlockIP, 36 NFSD_FO_UnlockFS, 37 NFSD_Threads, 38 NFSD_Pool_Threads, 39 NFSD_Pool_Stats, 40 NFSD_Reply_Cache_Stats, 41 NFSD_Versions, 42 NFSD_Ports, 43 NFSD_MaxBlkSize, 44 NFSD_MaxConnections, 45 NFSD_SupportedEnctypes, 46 /* 47 * The below MUST come last. Otherwise we leave a hole in nfsd_files[] 48 * with !CONFIG_NFSD_V4 and simple_fill_super() goes oops 49 */ 50 #ifdef CONFIG_NFSD_V4 51 NFSD_Leasetime, 52 NFSD_Gracetime, 53 NFSD_RecoveryDir, 54 NFSD_V4EndGrace, 55 #endif 56 }; 57 58 /* 59 * write() for these nodes. 60 */ 61 static ssize_t write_filehandle(struct file *file, char *buf, size_t size); 62 static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size); 63 static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size); 64 static ssize_t write_threads(struct file *file, char *buf, size_t size); 65 static ssize_t write_pool_threads(struct file *file, char *buf, size_t size); 66 static ssize_t write_versions(struct file *file, char *buf, size_t size); 67 static ssize_t write_ports(struct file *file, char *buf, size_t size); 68 static ssize_t write_maxblksize(struct file *file, char *buf, size_t size); 69 static ssize_t write_maxconn(struct file *file, char *buf, size_t size); 70 #ifdef CONFIG_NFSD_V4 71 static ssize_t write_leasetime(struct file *file, char *buf, size_t size); 72 static ssize_t write_gracetime(struct file *file, char *buf, size_t size); 73 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size); 74 static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size); 75 #endif 76 77 static ssize_t (*const write_op[])(struct file *, char *, size_t) = { 78 [NFSD_Fh] = write_filehandle, 79 [NFSD_FO_UnlockIP] = write_unlock_ip, 80 [NFSD_FO_UnlockFS] = write_unlock_fs, 81 [NFSD_Threads] = write_threads, 82 [NFSD_Pool_Threads] = write_pool_threads, 83 [NFSD_Versions] = write_versions, 84 [NFSD_Ports] = write_ports, 85 [NFSD_MaxBlkSize] = write_maxblksize, 86 [NFSD_MaxConnections] = write_maxconn, 87 #ifdef CONFIG_NFSD_V4 88 [NFSD_Leasetime] = write_leasetime, 89 [NFSD_Gracetime] = write_gracetime, 90 [NFSD_RecoveryDir] = write_recoverydir, 91 [NFSD_V4EndGrace] = write_v4_end_grace, 92 #endif 93 }; 94 95 static ssize_t nfsctl_transaction_write(struct file *file, const char __user *buf, size_t size, loff_t *pos) 96 { 97 ino_t ino = file_inode(file)->i_ino; 98 char *data; 99 ssize_t rv; 100 101 if (ino >= ARRAY_SIZE(write_op) || !write_op[ino]) 102 return -EINVAL; 103 104 data = simple_transaction_get(file, buf, size); 105 if (IS_ERR(data)) 106 return PTR_ERR(data); 107 108 rv = write_op[ino](file, data, size); 109 if (rv >= 0) { 110 simple_transaction_set(file, rv); 111 rv = size; 112 } 113 return rv; 114 } 115 116 static ssize_t nfsctl_transaction_read(struct file *file, char __user *buf, size_t size, loff_t *pos) 117 { 118 if (! file->private_data) { 119 /* An attempt to read a transaction file without writing 120 * causes a 0-byte write so that the file can return 121 * state information 122 */ 123 ssize_t rv = nfsctl_transaction_write(file, buf, 0, pos); 124 if (rv < 0) 125 return rv; 126 } 127 return simple_transaction_read(file, buf, size, pos); 128 } 129 130 static const struct file_operations transaction_ops = { 131 .write = nfsctl_transaction_write, 132 .read = nfsctl_transaction_read, 133 .release = simple_transaction_release, 134 .llseek = default_llseek, 135 }; 136 137 static int exports_net_open(struct net *net, struct file *file) 138 { 139 int err; 140 struct seq_file *seq; 141 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 142 143 err = seq_open(file, &nfs_exports_op); 144 if (err) 145 return err; 146 147 seq = file->private_data; 148 seq->private = nn->svc_export_cache; 149 return 0; 150 } 151 152 static int exports_proc_open(struct inode *inode, struct file *file) 153 { 154 return exports_net_open(current->nsproxy->net_ns, file); 155 } 156 157 static const struct file_operations exports_proc_operations = { 158 .open = exports_proc_open, 159 .read = seq_read, 160 .llseek = seq_lseek, 161 .release = seq_release, 162 }; 163 164 static int exports_nfsd_open(struct inode *inode, struct file *file) 165 { 166 return exports_net_open(inode->i_sb->s_fs_info, file); 167 } 168 169 static const struct file_operations exports_nfsd_operations = { 170 .open = exports_nfsd_open, 171 .read = seq_read, 172 .llseek = seq_lseek, 173 .release = seq_release, 174 }; 175 176 static int export_features_show(struct seq_file *m, void *v) 177 { 178 seq_printf(m, "0x%x 0x%x\n", NFSEXP_ALLFLAGS, NFSEXP_SECINFO_FLAGS); 179 return 0; 180 } 181 182 static int export_features_open(struct inode *inode, struct file *file) 183 { 184 return single_open(file, export_features_show, NULL); 185 } 186 187 static const struct file_operations export_features_operations = { 188 .open = export_features_open, 189 .read = seq_read, 190 .llseek = seq_lseek, 191 .release = single_release, 192 }; 193 194 #if defined(CONFIG_SUNRPC_GSS) || defined(CONFIG_SUNRPC_GSS_MODULE) 195 static int supported_enctypes_show(struct seq_file *m, void *v) 196 { 197 seq_printf(m, KRB5_SUPPORTED_ENCTYPES); 198 return 0; 199 } 200 201 static int supported_enctypes_open(struct inode *inode, struct file *file) 202 { 203 return single_open(file, supported_enctypes_show, NULL); 204 } 205 206 static const struct file_operations supported_enctypes_ops = { 207 .open = supported_enctypes_open, 208 .read = seq_read, 209 .llseek = seq_lseek, 210 .release = single_release, 211 }; 212 #endif /* CONFIG_SUNRPC_GSS or CONFIG_SUNRPC_GSS_MODULE */ 213 214 static const struct file_operations pool_stats_operations = { 215 .open = nfsd_pool_stats_open, 216 .read = seq_read, 217 .llseek = seq_lseek, 218 .release = nfsd_pool_stats_release, 219 }; 220 221 static const struct file_operations reply_cache_stats_operations = { 222 .open = nfsd_reply_cache_stats_open, 223 .read = seq_read, 224 .llseek = seq_lseek, 225 .release = single_release, 226 }; 227 228 /*----------------------------------------------------------------------------*/ 229 /* 230 * payload - write methods 231 */ 232 233 static inline struct net *netns(struct file *file) 234 { 235 return file_inode(file)->i_sb->s_fs_info; 236 } 237 238 /** 239 * write_unlock_ip - Release all locks used by a client 240 * 241 * Experimental. 242 * 243 * Input: 244 * buf: '\n'-terminated C string containing a 245 * presentation format IP address 246 * size: length of C string in @buf 247 * Output: 248 * On success: returns zero if all specified locks were released; 249 * returns one if one or more locks were not released 250 * On error: return code is negative errno value 251 */ 252 static ssize_t write_unlock_ip(struct file *file, char *buf, size_t size) 253 { 254 struct sockaddr_storage address; 255 struct sockaddr *sap = (struct sockaddr *)&address; 256 size_t salen = sizeof(address); 257 char *fo_path; 258 struct net *net = netns(file); 259 260 /* sanity check */ 261 if (size == 0) 262 return -EINVAL; 263 264 if (buf[size-1] != '\n') 265 return -EINVAL; 266 267 fo_path = buf; 268 if (qword_get(&buf, fo_path, size) < 0) 269 return -EINVAL; 270 271 if (rpc_pton(net, fo_path, size, sap, salen) == 0) 272 return -EINVAL; 273 274 return nlmsvc_unlock_all_by_ip(sap); 275 } 276 277 /** 278 * write_unlock_fs - Release all locks on a local file system 279 * 280 * Experimental. 281 * 282 * Input: 283 * buf: '\n'-terminated C string containing the 284 * absolute pathname of a local file system 285 * size: length of C string in @buf 286 * Output: 287 * On success: returns zero if all specified locks were released; 288 * returns one if one or more locks were not released 289 * On error: return code is negative errno value 290 */ 291 static ssize_t write_unlock_fs(struct file *file, char *buf, size_t size) 292 { 293 struct path path; 294 char *fo_path; 295 int error; 296 297 /* sanity check */ 298 if (size == 0) 299 return -EINVAL; 300 301 if (buf[size-1] != '\n') 302 return -EINVAL; 303 304 fo_path = buf; 305 if (qword_get(&buf, fo_path, size) < 0) 306 return -EINVAL; 307 308 error = kern_path(fo_path, 0, &path); 309 if (error) 310 return error; 311 312 /* 313 * XXX: Needs better sanity checking. Otherwise we could end up 314 * releasing locks on the wrong file system. 315 * 316 * For example: 317 * 1. Does the path refer to a directory? 318 * 2. Is that directory a mount point, or 319 * 3. Is that directory the root of an exported file system? 320 */ 321 error = nlmsvc_unlock_all_by_sb(path.dentry->d_sb); 322 323 path_put(&path); 324 return error; 325 } 326 327 /** 328 * write_filehandle - Get a variable-length NFS file handle by path 329 * 330 * On input, the buffer contains a '\n'-terminated C string comprised of 331 * three alphanumeric words separated by whitespace. The string may 332 * contain escape sequences. 333 * 334 * Input: 335 * buf: 336 * domain: client domain name 337 * path: export pathname 338 * maxsize: numeric maximum size of 339 * @buf 340 * size: length of C string in @buf 341 * Output: 342 * On success: passed-in buffer filled with '\n'-terminated C 343 * string containing a ASCII hex text version 344 * of the NFS file handle; 345 * return code is the size in bytes of the string 346 * On error: return code is negative errno value 347 */ 348 static ssize_t write_filehandle(struct file *file, char *buf, size_t size) 349 { 350 char *dname, *path; 351 int uninitialized_var(maxsize); 352 char *mesg = buf; 353 int len; 354 struct auth_domain *dom; 355 struct knfsd_fh fh; 356 357 if (size == 0) 358 return -EINVAL; 359 360 if (buf[size-1] != '\n') 361 return -EINVAL; 362 buf[size-1] = 0; 363 364 dname = mesg; 365 len = qword_get(&mesg, dname, size); 366 if (len <= 0) 367 return -EINVAL; 368 369 path = dname+len+1; 370 len = qword_get(&mesg, path, size); 371 if (len <= 0) 372 return -EINVAL; 373 374 len = get_int(&mesg, &maxsize); 375 if (len) 376 return len; 377 378 if (maxsize < NFS_FHSIZE) 379 return -EINVAL; 380 maxsize = min(maxsize, NFS3_FHSIZE); 381 382 if (qword_get(&mesg, mesg, size)>0) 383 return -EINVAL; 384 385 /* we have all the words, they are in buf.. */ 386 dom = unix_domain_find(dname); 387 if (!dom) 388 return -ENOMEM; 389 390 len = exp_rootfh(netns(file), dom, path, &fh, maxsize); 391 auth_domain_put(dom); 392 if (len) 393 return len; 394 395 mesg = buf; 396 len = SIMPLE_TRANSACTION_LIMIT; 397 qword_addhex(&mesg, &len, (char*)&fh.fh_base, fh.fh_size); 398 mesg[-1] = '\n'; 399 return mesg - buf; 400 } 401 402 /** 403 * write_threads - Start NFSD, or report the current number of running threads 404 * 405 * Input: 406 * buf: ignored 407 * size: zero 408 * Output: 409 * On success: passed-in buffer filled with '\n'-terminated C 410 * string numeric value representing the number of 411 * running NFSD threads; 412 * return code is the size in bytes of the string 413 * On error: return code is zero 414 * 415 * OR 416 * 417 * Input: 418 * buf: C string containing an unsigned 419 * integer value representing the 420 * number of NFSD threads to start 421 * size: non-zero length of C string in @buf 422 * Output: 423 * On success: NFS service is started; 424 * passed-in buffer filled with '\n'-terminated C 425 * string numeric value representing the number of 426 * running NFSD threads; 427 * return code is the size in bytes of the string 428 * On error: return code is zero or a negative errno value 429 */ 430 static ssize_t write_threads(struct file *file, char *buf, size_t size) 431 { 432 char *mesg = buf; 433 int rv; 434 struct net *net = netns(file); 435 436 if (size > 0) { 437 int newthreads; 438 rv = get_int(&mesg, &newthreads); 439 if (rv) 440 return rv; 441 if (newthreads < 0) 442 return -EINVAL; 443 rv = nfsd_svc(newthreads, net, file->f_cred); 444 if (rv < 0) 445 return rv; 446 } else 447 rv = nfsd_nrthreads(net); 448 449 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", rv); 450 } 451 452 /** 453 * write_pool_threads - Set or report the current number of threads per pool 454 * 455 * Input: 456 * buf: ignored 457 * size: zero 458 * 459 * OR 460 * 461 * Input: 462 * buf: C string containing whitespace- 463 * separated unsigned integer values 464 * representing the number of NFSD 465 * threads to start in each pool 466 * size: non-zero length of C string in @buf 467 * Output: 468 * On success: passed-in buffer filled with '\n'-terminated C 469 * string containing integer values representing the 470 * number of NFSD threads in each pool; 471 * return code is the size in bytes of the string 472 * On error: return code is zero or a negative errno value 473 */ 474 static ssize_t write_pool_threads(struct file *file, char *buf, size_t size) 475 { 476 /* if size > 0, look for an array of number of threads per node 477 * and apply them then write out number of threads per node as reply 478 */ 479 char *mesg = buf; 480 int i; 481 int rv; 482 int len; 483 int npools; 484 int *nthreads; 485 struct net *net = netns(file); 486 487 mutex_lock(&nfsd_mutex); 488 npools = nfsd_nrpools(net); 489 if (npools == 0) { 490 /* 491 * NFS is shut down. The admin can start it by 492 * writing to the threads file but NOT the pool_threads 493 * file, sorry. Report zero threads. 494 */ 495 mutex_unlock(&nfsd_mutex); 496 strcpy(buf, "0\n"); 497 return strlen(buf); 498 } 499 500 nthreads = kcalloc(npools, sizeof(int), GFP_KERNEL); 501 rv = -ENOMEM; 502 if (nthreads == NULL) 503 goto out_free; 504 505 if (size > 0) { 506 for (i = 0; i < npools; i++) { 507 rv = get_int(&mesg, &nthreads[i]); 508 if (rv == -ENOENT) 509 break; /* fewer numbers than pools */ 510 if (rv) 511 goto out_free; /* syntax error */ 512 rv = -EINVAL; 513 if (nthreads[i] < 0) 514 goto out_free; 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 581 vers = mesg; 582 len = qword_get(&mesg, vers, size); 583 if (len <= 0) return -EINVAL; 584 do { 585 enum vers_op cmd; 586 unsigned minor; 587 sign = *vers; 588 if (sign == '+' || sign == '-') 589 num = simple_strtol((vers+1), &minorp, 0); 590 else 591 num = simple_strtol(vers, &minorp, 0); 592 if (*minorp == '.') { 593 if (num != 4) 594 return -EINVAL; 595 if (kstrtouint(minorp+1, 0, &minor) < 0) 596 return -EINVAL; 597 } 598 599 cmd = sign == '-' ? NFSD_CLEAR : NFSD_SET; 600 switch(num) { 601 case 2: 602 case 3: 603 nfsd_vers(nn, num, cmd); 604 break; 605 case 4: 606 if (*minorp == '.') { 607 if (nfsd_minorversion(nn, minor, cmd) < 0) 608 return -EINVAL; 609 } else if ((cmd == NFSD_SET) != nfsd_vers(nn, num, NFSD_TEST)) { 610 /* 611 * Either we have +4 and no minors are enabled, 612 * or we have -4 and at least one minor is enabled. 613 * In either case, propagate 'cmd' to all minors. 614 */ 615 minor = 0; 616 while (nfsd_minorversion(nn, minor, cmd) >= 0) 617 minor++; 618 } 619 break; 620 default: 621 return -EINVAL; 622 } 623 vers += len + 1; 624 } while ((len = qword_get(&mesg, vers, size)) > 0); 625 /* If all get turned off, turn them back on, as 626 * having no versions is BAD 627 */ 628 nfsd_reset_versions(nn); 629 } 630 631 /* Now write current state into reply buffer */ 632 len = 0; 633 sep = ""; 634 remaining = SIMPLE_TRANSACTION_LIMIT; 635 for (num=2 ; num <= 4 ; num++) { 636 int minor; 637 if (!nfsd_vers(nn, num, NFSD_AVAIL)) 638 continue; 639 640 minor = -1; 641 do { 642 len = nfsd_print_version_support(nn, buf, remaining, 643 sep, num, minor); 644 if (len >= remaining) 645 goto out; 646 remaining -= len; 647 buf += len; 648 tlen += len; 649 minor++; 650 if (len) 651 sep = " "; 652 } while (num == 4 && minor <= NFSD_SUPPORTED_MINOR_VERSION); 653 } 654 out: 655 len = snprintf(buf, remaining, "\n"); 656 if (len >= remaining) 657 return -EINVAL; 658 return tlen + len; 659 } 660 661 /** 662 * write_versions - Set or report the available NFS protocol versions 663 * 664 * Input: 665 * buf: ignored 666 * size: zero 667 * Output: 668 * On success: passed-in buffer filled with '\n'-terminated C 669 * string containing positive or negative integer 670 * values representing the current status of each 671 * protocol version; 672 * return code is the size in bytes of the string 673 * On error: return code is zero or a negative errno value 674 * 675 * OR 676 * 677 * Input: 678 * buf: C string containing whitespace- 679 * separated positive or negative 680 * integer values representing NFS 681 * protocol versions to enable ("+n") 682 * or disable ("-n") 683 * size: non-zero length of C string in @buf 684 * Output: 685 * On success: status of zero or more protocol versions has 686 * been updated; passed-in buffer filled with 687 * '\n'-terminated C string containing positive 688 * or negative integer values representing the 689 * current status of each protocol version; 690 * return code is the size in bytes of the string 691 * On error: return code is zero or a negative errno value 692 */ 693 static ssize_t write_versions(struct file *file, char *buf, size_t size) 694 { 695 ssize_t rv; 696 697 mutex_lock(&nfsd_mutex); 698 rv = __write_versions(file, buf, size); 699 mutex_unlock(&nfsd_mutex); 700 return rv; 701 } 702 703 /* 704 * Zero-length write. Return a list of NFSD's current listener 705 * transports. 706 */ 707 static ssize_t __write_ports_names(char *buf, struct net *net) 708 { 709 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 710 711 if (nn->nfsd_serv == NULL) 712 return 0; 713 return svc_xprt_names(nn->nfsd_serv, buf, SIMPLE_TRANSACTION_LIMIT); 714 } 715 716 /* 717 * A single 'fd' number was written, in which case it must be for 718 * a socket of a supported family/protocol, and we use it as an 719 * nfsd listener. 720 */ 721 static ssize_t __write_ports_addfd(char *buf, struct net *net, const struct cred *cred) 722 { 723 char *mesg = buf; 724 int fd, err; 725 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 726 727 err = get_int(&mesg, &fd); 728 if (err != 0 || fd < 0) 729 return -EINVAL; 730 731 if (svc_alien_sock(net, fd)) { 732 printk(KERN_ERR "%s: socket net is different to NFSd's one\n", __func__); 733 return -EINVAL; 734 } 735 736 err = nfsd_create_serv(net); 737 if (err != 0) 738 return err; 739 740 err = svc_addsock(nn->nfsd_serv, fd, buf, SIMPLE_TRANSACTION_LIMIT, cred); 741 if (err < 0) { 742 nfsd_destroy(net); 743 return err; 744 } 745 746 /* Decrease the count, but don't shut down the service */ 747 nn->nfsd_serv->sv_nrthreads--; 748 return err; 749 } 750 751 /* 752 * A transport listener is added by writing it's transport name and 753 * a port number. 754 */ 755 static ssize_t __write_ports_addxprt(char *buf, struct net *net, const struct cred *cred) 756 { 757 char transport[16]; 758 struct svc_xprt *xprt; 759 int port, err; 760 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 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 768 err = nfsd_create_serv(net); 769 if (err != 0) 770 return err; 771 772 err = svc_create_xprt(nn->nfsd_serv, transport, net, 773 PF_INET, port, SVC_SOCK_ANONYMOUS, cred); 774 if (err < 0) 775 goto out_err; 776 777 err = svc_create_xprt(nn->nfsd_serv, transport, net, 778 PF_INET6, port, SVC_SOCK_ANONYMOUS, cred); 779 if (err < 0 && err != -EAFNOSUPPORT) 780 goto out_close; 781 782 /* Decrease the count, but don't shut down the service */ 783 nn->nfsd_serv->sv_nrthreads--; 784 return 0; 785 out_close: 786 xprt = svc_find_xprt(nn->nfsd_serv, transport, net, PF_INET, port); 787 if (xprt != NULL) { 788 svc_close_xprt(xprt); 789 svc_xprt_put(xprt); 790 } 791 out_err: 792 nfsd_destroy(net); 793 return err; 794 } 795 796 static ssize_t __write_ports(struct file *file, char *buf, size_t size, 797 struct net *net) 798 { 799 if (size == 0) 800 return __write_ports_names(buf, net); 801 802 if (isdigit(buf[0])) 803 return __write_ports_addfd(buf, net, file->f_cred); 804 805 if (isalpha(buf[0])) 806 return __write_ports_addxprt(buf, net, file->f_cred); 807 808 return -EINVAL; 809 } 810 811 /** 812 * write_ports - Pass a socket file descriptor or transport name to listen on 813 * 814 * Input: 815 * buf: ignored 816 * size: zero 817 * Output: 818 * On success: passed-in buffer filled with a '\n'-terminated C 819 * string containing a whitespace-separated list of 820 * named NFSD listeners; 821 * return code is the size in bytes of the string 822 * On error: return code is zero or a negative errno value 823 * 824 * OR 825 * 826 * Input: 827 * buf: C string containing an unsigned 828 * integer value representing a bound 829 * but unconnected socket that is to be 830 * used as an NFSD listener; listen(3) 831 * must be called for a SOCK_STREAM 832 * socket, otherwise it is ignored 833 * size: non-zero length of C string in @buf 834 * Output: 835 * On success: NFS service is started; 836 * passed-in buffer filled with a '\n'-terminated C 837 * string containing a unique alphanumeric name of 838 * the listener; 839 * return code is the size in bytes of the string 840 * On error: return code is a negative errno value 841 * 842 * OR 843 * 844 * Input: 845 * buf: C string containing a transport 846 * name and an unsigned integer value 847 * representing the port to listen on, 848 * separated by whitespace 849 * size: non-zero length of C string in @buf 850 * Output: 851 * On success: returns zero; NFS service is started 852 * On error: return code is a negative errno value 853 */ 854 static ssize_t write_ports(struct file *file, char *buf, size_t size) 855 { 856 ssize_t rv; 857 858 mutex_lock(&nfsd_mutex); 859 rv = __write_ports(file, buf, size, netns(file)); 860 mutex_unlock(&nfsd_mutex); 861 return rv; 862 } 863 864 865 int nfsd_max_blksize; 866 867 /** 868 * write_maxblksize - Set or report the current NFS blksize 869 * 870 * Input: 871 * buf: ignored 872 * size: zero 873 * 874 * OR 875 * 876 * Input: 877 * buf: C string containing an unsigned 878 * integer value representing the new 879 * NFS blksize 880 * size: non-zero length of C string in @buf 881 * Output: 882 * On success: passed-in buffer filled with '\n'-terminated C string 883 * containing numeric value of the current NFS blksize 884 * setting; 885 * return code is the size in bytes of the string 886 * On error: return code is zero or a negative errno value 887 */ 888 static ssize_t write_maxblksize(struct file *file, char *buf, size_t size) 889 { 890 char *mesg = buf; 891 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id); 892 893 if (size > 0) { 894 int bsize; 895 int rv = get_int(&mesg, &bsize); 896 if (rv) 897 return rv; 898 /* force bsize into allowed range and 899 * required alignment. 900 */ 901 bsize = max_t(int, bsize, 1024); 902 bsize = min_t(int, bsize, NFSSVC_MAXBLKSIZE); 903 bsize &= ~(1024-1); 904 mutex_lock(&nfsd_mutex); 905 if (nn->nfsd_serv) { 906 mutex_unlock(&nfsd_mutex); 907 return -EBUSY; 908 } 909 nfsd_max_blksize = bsize; 910 mutex_unlock(&nfsd_mutex); 911 } 912 913 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%d\n", 914 nfsd_max_blksize); 915 } 916 917 /** 918 * write_maxconn - Set or report the current max number of connections 919 * 920 * Input: 921 * buf: ignored 922 * size: zero 923 * OR 924 * 925 * Input: 926 * buf: C string containing an unsigned 927 * integer value representing the new 928 * number of max connections 929 * size: non-zero length of C string in @buf 930 * Output: 931 * On success: passed-in buffer filled with '\n'-terminated C string 932 * containing numeric value of max_connections setting 933 * for this net namespace; 934 * return code is the size in bytes of the string 935 * On error: return code is zero or a negative errno value 936 */ 937 static ssize_t write_maxconn(struct file *file, char *buf, size_t size) 938 { 939 char *mesg = buf; 940 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id); 941 unsigned int maxconn = nn->max_connections; 942 943 if (size > 0) { 944 int rv = get_uint(&mesg, &maxconn); 945 946 if (rv) 947 return rv; 948 nn->max_connections = maxconn; 949 } 950 951 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%u\n", maxconn); 952 } 953 954 #ifdef CONFIG_NFSD_V4 955 static ssize_t __nfsd4_write_time(struct file *file, char *buf, size_t size, 956 time_t *time, struct nfsd_net *nn) 957 { 958 char *mesg = buf; 959 int rv, i; 960 961 if (size > 0) { 962 if (nn->nfsd_serv) 963 return -EBUSY; 964 rv = get_int(&mesg, &i); 965 if (rv) 966 return rv; 967 /* 968 * Some sanity checking. We don't have a reason for 969 * these particular numbers, but problems with the 970 * extremes are: 971 * - Too short: the briefest network outage may 972 * cause clients to lose all their locks. Also, 973 * the frequent polling may be wasteful. 974 * - Too long: do you really want reboot recovery 975 * to take more than an hour? Or to make other 976 * clients wait an hour before being able to 977 * revoke a dead client's locks? 978 */ 979 if (i < 10 || i > 3600) 980 return -EINVAL; 981 *time = i; 982 } 983 984 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%ld\n", *time); 985 } 986 987 static ssize_t nfsd4_write_time(struct file *file, char *buf, size_t size, 988 time_t *time, struct nfsd_net *nn) 989 { 990 ssize_t rv; 991 992 mutex_lock(&nfsd_mutex); 993 rv = __nfsd4_write_time(file, buf, size, time, nn); 994 mutex_unlock(&nfsd_mutex); 995 return rv; 996 } 997 998 /** 999 * write_leasetime - Set or report the current NFSv4 lease time 1000 * 1001 * Input: 1002 * buf: ignored 1003 * size: zero 1004 * 1005 * OR 1006 * 1007 * Input: 1008 * buf: C string containing an unsigned 1009 * integer value representing the new 1010 * NFSv4 lease expiry time 1011 * size: non-zero length of C string in @buf 1012 * Output: 1013 * On success: passed-in buffer filled with '\n'-terminated C 1014 * string containing unsigned integer value of the 1015 * current lease expiry time; 1016 * return code is the size in bytes of the string 1017 * On error: return code is zero or a negative errno value 1018 */ 1019 static ssize_t write_leasetime(struct file *file, char *buf, size_t size) 1020 { 1021 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id); 1022 return nfsd4_write_time(file, buf, size, &nn->nfsd4_lease, nn); 1023 } 1024 1025 /** 1026 * write_gracetime - Set or report current NFSv4 grace period time 1027 * 1028 * As above, but sets the time of the NFSv4 grace period. 1029 * 1030 * Note this should never be set to less than the *previous* 1031 * lease-period time, but we don't try to enforce this. (In the common 1032 * case (a new boot), we don't know what the previous lease time was 1033 * anyway.) 1034 */ 1035 static ssize_t write_gracetime(struct file *file, char *buf, size_t size) 1036 { 1037 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id); 1038 return nfsd4_write_time(file, buf, size, &nn->nfsd4_grace, nn); 1039 } 1040 1041 static ssize_t __write_recoverydir(struct file *file, char *buf, size_t size, 1042 struct nfsd_net *nn) 1043 { 1044 char *mesg = buf; 1045 char *recdir; 1046 int len, status; 1047 1048 if (size > 0) { 1049 if (nn->nfsd_serv) 1050 return -EBUSY; 1051 if (size > PATH_MAX || buf[size-1] != '\n') 1052 return -EINVAL; 1053 buf[size-1] = 0; 1054 1055 recdir = mesg; 1056 len = qword_get(&mesg, recdir, size); 1057 if (len <= 0) 1058 return -EINVAL; 1059 1060 status = nfs4_reset_recoverydir(recdir); 1061 if (status) 1062 return status; 1063 } 1064 1065 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%s\n", 1066 nfs4_recoverydir()); 1067 } 1068 1069 /** 1070 * write_recoverydir - Set or report the pathname of the recovery directory 1071 * 1072 * Input: 1073 * buf: ignored 1074 * size: zero 1075 * 1076 * OR 1077 * 1078 * Input: 1079 * buf: C string containing the pathname 1080 * of the directory on a local file 1081 * system containing permanent NFSv4 1082 * recovery data 1083 * size: non-zero length of C string in @buf 1084 * Output: 1085 * On success: passed-in buffer filled with '\n'-terminated C string 1086 * containing the current recovery pathname setting; 1087 * return code is the size in bytes of the string 1088 * On error: return code is zero or a negative errno value 1089 */ 1090 static ssize_t write_recoverydir(struct file *file, char *buf, size_t size) 1091 { 1092 ssize_t rv; 1093 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id); 1094 1095 mutex_lock(&nfsd_mutex); 1096 rv = __write_recoverydir(file, buf, size, nn); 1097 mutex_unlock(&nfsd_mutex); 1098 return rv; 1099 } 1100 1101 /** 1102 * write_v4_end_grace - release grace period for nfsd's v4.x lock manager 1103 * 1104 * Input: 1105 * buf: ignored 1106 * size: zero 1107 * OR 1108 * 1109 * Input: 1110 * buf: any value 1111 * size: non-zero length of C string in @buf 1112 * Output: 1113 * passed-in buffer filled with "Y" or "N" with a newline 1114 * and NULL-terminated C string. This indicates whether 1115 * the grace period has ended in the current net 1116 * namespace. Return code is the size in bytes of the 1117 * string. Writing a string that starts with 'Y', 'y', or 1118 * '1' to the file will end the grace period for nfsd's v4 1119 * lock manager. 1120 */ 1121 static ssize_t write_v4_end_grace(struct file *file, char *buf, size_t size) 1122 { 1123 struct nfsd_net *nn = net_generic(netns(file), nfsd_net_id); 1124 1125 if (size > 0) { 1126 switch(buf[0]) { 1127 case 'Y': 1128 case 'y': 1129 case '1': 1130 if (!nn->nfsd_serv) 1131 return -EBUSY; 1132 nfsd4_end_grace(nn); 1133 break; 1134 default: 1135 return -EINVAL; 1136 } 1137 } 1138 1139 return scnprintf(buf, SIMPLE_TRANSACTION_LIMIT, "%c\n", 1140 nn->grace_ended ? 'Y' : 'N'); 1141 } 1142 1143 #endif 1144 1145 /*----------------------------------------------------------------------------*/ 1146 /* 1147 * populating the filesystem. 1148 */ 1149 1150 static int nfsd_fill_super(struct super_block * sb, void * data, int silent) 1151 { 1152 static const struct tree_descr nfsd_files[] = { 1153 [NFSD_List] = {"exports", &exports_nfsd_operations, S_IRUGO}, 1154 [NFSD_Export_features] = {"export_features", 1155 &export_features_operations, S_IRUGO}, 1156 [NFSD_FO_UnlockIP] = {"unlock_ip", 1157 &transaction_ops, S_IWUSR|S_IRUSR}, 1158 [NFSD_FO_UnlockFS] = {"unlock_filesystem", 1159 &transaction_ops, S_IWUSR|S_IRUSR}, 1160 [NFSD_Fh] = {"filehandle", &transaction_ops, S_IWUSR|S_IRUSR}, 1161 [NFSD_Threads] = {"threads", &transaction_ops, S_IWUSR|S_IRUSR}, 1162 [NFSD_Pool_Threads] = {"pool_threads", &transaction_ops, S_IWUSR|S_IRUSR}, 1163 [NFSD_Pool_Stats] = {"pool_stats", &pool_stats_operations, S_IRUGO}, 1164 [NFSD_Reply_Cache_Stats] = {"reply_cache_stats", &reply_cache_stats_operations, S_IRUGO}, 1165 [NFSD_Versions] = {"versions", &transaction_ops, S_IWUSR|S_IRUSR}, 1166 [NFSD_Ports] = {"portlist", &transaction_ops, S_IWUSR|S_IRUGO}, 1167 [NFSD_MaxBlkSize] = {"max_block_size", &transaction_ops, S_IWUSR|S_IRUGO}, 1168 [NFSD_MaxConnections] = {"max_connections", &transaction_ops, S_IWUSR|S_IRUGO}, 1169 #if defined(CONFIG_SUNRPC_GSS) || defined(CONFIG_SUNRPC_GSS_MODULE) 1170 [NFSD_SupportedEnctypes] = {"supported_krb5_enctypes", &supported_enctypes_ops, S_IRUGO}, 1171 #endif /* CONFIG_SUNRPC_GSS or CONFIG_SUNRPC_GSS_MODULE */ 1172 #ifdef CONFIG_NFSD_V4 1173 [NFSD_Leasetime] = {"nfsv4leasetime", &transaction_ops, S_IWUSR|S_IRUSR}, 1174 [NFSD_Gracetime] = {"nfsv4gracetime", &transaction_ops, S_IWUSR|S_IRUSR}, 1175 [NFSD_RecoveryDir] = {"nfsv4recoverydir", &transaction_ops, S_IWUSR|S_IRUSR}, 1176 [NFSD_V4EndGrace] = {"v4_end_grace", &transaction_ops, S_IWUSR|S_IRUGO}, 1177 #endif 1178 /* last one */ {""} 1179 }; 1180 get_net(sb->s_fs_info); 1181 return simple_fill_super(sb, 0x6e667364, nfsd_files); 1182 } 1183 1184 static struct dentry *nfsd_mount(struct file_system_type *fs_type, 1185 int flags, const char *dev_name, void *data) 1186 { 1187 struct net *net = current->nsproxy->net_ns; 1188 return mount_ns(fs_type, flags, data, net, net->user_ns, nfsd_fill_super); 1189 } 1190 1191 static void nfsd_umount(struct super_block *sb) 1192 { 1193 struct net *net = sb->s_fs_info; 1194 1195 kill_litter_super(sb); 1196 put_net(net); 1197 } 1198 1199 static struct file_system_type nfsd_fs_type = { 1200 .owner = THIS_MODULE, 1201 .name = "nfsd", 1202 .mount = nfsd_mount, 1203 .kill_sb = nfsd_umount, 1204 }; 1205 MODULE_ALIAS_FS("nfsd"); 1206 1207 #ifdef CONFIG_PROC_FS 1208 static int create_proc_exports_entry(void) 1209 { 1210 struct proc_dir_entry *entry; 1211 1212 entry = proc_mkdir("fs/nfs", NULL); 1213 if (!entry) 1214 return -ENOMEM; 1215 entry = proc_create("exports", 0, entry, 1216 &exports_proc_operations); 1217 if (!entry) { 1218 remove_proc_entry("fs/nfs", NULL); 1219 return -ENOMEM; 1220 } 1221 return 0; 1222 } 1223 #else /* CONFIG_PROC_FS */ 1224 static int create_proc_exports_entry(void) 1225 { 1226 return 0; 1227 } 1228 #endif 1229 1230 unsigned int nfsd_net_id; 1231 1232 static __net_init int nfsd_init_net(struct net *net) 1233 { 1234 int retval; 1235 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 1236 1237 retval = nfsd_export_init(net); 1238 if (retval) 1239 goto out_export_error; 1240 retval = nfsd_idmap_init(net); 1241 if (retval) 1242 goto out_idmap_error; 1243 nn->nfsd_versions = NULL; 1244 nn->nfsd4_minorversions = NULL; 1245 nn->nfsd4_lease = 90; /* default lease time */ 1246 nn->nfsd4_grace = 90; 1247 nn->somebody_reclaimed = false; 1248 nn->track_reclaim_completes = false; 1249 nn->clverifier_counter = prandom_u32(); 1250 nn->clientid_counter = prandom_u32(); 1251 nn->s2s_cp_cl_id = nn->clientid_counter++; 1252 1253 atomic_set(&nn->ntf_refcnt, 0); 1254 init_waitqueue_head(&nn->ntf_wq); 1255 return 0; 1256 1257 out_idmap_error: 1258 nfsd_export_shutdown(net); 1259 out_export_error: 1260 return retval; 1261 } 1262 1263 static __net_exit void nfsd_exit_net(struct net *net) 1264 { 1265 nfsd_idmap_shutdown(net); 1266 nfsd_export_shutdown(net); 1267 nfsd_netns_free_versions(net_generic(net, nfsd_net_id)); 1268 } 1269 1270 static struct pernet_operations nfsd_net_ops = { 1271 .init = nfsd_init_net, 1272 .exit = nfsd_exit_net, 1273 .id = &nfsd_net_id, 1274 .size = sizeof(struct nfsd_net), 1275 }; 1276 1277 static int __init init_nfsd(void) 1278 { 1279 int retval; 1280 printk(KERN_INFO "Installing knfsd (copyright (C) 1996 okir@monad.swb.de).\n"); 1281 1282 retval = register_pernet_subsys(&nfsd_net_ops); 1283 if (retval < 0) 1284 return retval; 1285 retval = register_cld_notifier(); 1286 if (retval) 1287 goto out_unregister_pernet; 1288 retval = nfsd4_init_slabs(); 1289 if (retval) 1290 goto out_unregister_notifier; 1291 retval = nfsd4_init_pnfs(); 1292 if (retval) 1293 goto out_free_slabs; 1294 retval = nfsd_fault_inject_init(); /* nfsd fault injection controls */ 1295 if (retval) 1296 goto out_exit_pnfs; 1297 nfsd_stat_init(); /* Statistics */ 1298 retval = nfsd_reply_cache_init(); 1299 if (retval) 1300 goto out_free_stat; 1301 nfsd_lockd_init(); /* lockd->nfsd callbacks */ 1302 retval = create_proc_exports_entry(); 1303 if (retval) 1304 goto out_free_lockd; 1305 retval = register_filesystem(&nfsd_fs_type); 1306 if (retval) 1307 goto out_free_all; 1308 return 0; 1309 out_free_all: 1310 remove_proc_entry("fs/nfs/exports", NULL); 1311 remove_proc_entry("fs/nfs", NULL); 1312 out_free_lockd: 1313 nfsd_lockd_shutdown(); 1314 nfsd_reply_cache_shutdown(); 1315 out_free_stat: 1316 nfsd_stat_shutdown(); 1317 nfsd_fault_inject_cleanup(); 1318 out_exit_pnfs: 1319 nfsd4_exit_pnfs(); 1320 out_free_slabs: 1321 nfsd4_free_slabs(); 1322 out_unregister_notifier: 1323 unregister_cld_notifier(); 1324 out_unregister_pernet: 1325 unregister_pernet_subsys(&nfsd_net_ops); 1326 return retval; 1327 } 1328 1329 static void __exit exit_nfsd(void) 1330 { 1331 nfsd_reply_cache_shutdown(); 1332 remove_proc_entry("fs/nfs/exports", NULL); 1333 remove_proc_entry("fs/nfs", NULL); 1334 nfsd_stat_shutdown(); 1335 nfsd_lockd_shutdown(); 1336 nfsd4_free_slabs(); 1337 nfsd4_exit_pnfs(); 1338 nfsd_fault_inject_cleanup(); 1339 unregister_filesystem(&nfsd_fs_type); 1340 unregister_cld_notifier(); 1341 unregister_pernet_subsys(&nfsd_net_ops); 1342 } 1343 1344 MODULE_AUTHOR("Olaf Kirch <okir@monad.swb.de>"); 1345 MODULE_LICENSE("GPL"); 1346 module_init(init_nfsd) 1347 module_exit(exit_nfsd) 1348