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