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