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