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