1 /* 2 * Copyright (c) 2004 The Regents of the University of Michigan. 3 * Copyright (c) 2012 Jeff Layton <jlayton@redhat.com> 4 * All rights reserved. 5 * 6 * Andy Adamson <andros@citi.umich.edu> 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. Neither the name of the University nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED 22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 24 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 28 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 * 33 */ 34 35 #include <crypto/hash.h> 36 #include <crypto/sha2.h> 37 #include <linux/file.h> 38 #include <linux/slab.h> 39 #include <linux/namei.h> 40 #include <linux/sched.h> 41 #include <linux/fs.h> 42 #include <linux/module.h> 43 #include <net/net_namespace.h> 44 #include <linux/sunrpc/rpc_pipe_fs.h> 45 #include <linux/sunrpc/clnt.h> 46 #include <linux/nfsd/cld.h> 47 48 #include "nfsd.h" 49 #include "state.h" 50 #include "vfs.h" 51 #include "netns.h" 52 53 #define NFSDDBG_FACILITY NFSDDBG_PROC 54 55 /* Declarations */ 56 struct nfsd4_client_tracking_ops { 57 int (*init)(struct net *); 58 void (*exit)(struct net *); 59 void (*create)(struct nfs4_client *); 60 void (*remove)(struct nfs4_client *); 61 int (*check)(struct nfs4_client *); 62 void (*grace_done)(struct nfsd_net *); 63 uint8_t version; 64 size_t msglen; 65 }; 66 67 static const struct nfsd4_client_tracking_ops nfsd4_cld_tracking_ops; 68 static const struct nfsd4_client_tracking_ops nfsd4_cld_tracking_ops_v2; 69 70 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING 71 /* Globals */ 72 static char user_recovery_dirname[PATH_MAX] = "/var/lib/nfs/v4recovery"; 73 74 static int 75 nfs4_save_creds(const struct cred **original_creds) 76 { 77 struct cred *new; 78 79 new = prepare_creds(); 80 if (!new) 81 return -ENOMEM; 82 83 new->fsuid = GLOBAL_ROOT_UID; 84 new->fsgid = GLOBAL_ROOT_GID; 85 *original_creds = override_creds(new); 86 return 0; 87 } 88 89 static void 90 nfs4_reset_creds(const struct cred *original) 91 { 92 put_cred(revert_creds(original)); 93 } 94 95 static void 96 md5_to_hex(char *out, char *md5) 97 { 98 int i; 99 100 for (i=0; i<16; i++) { 101 unsigned char c = md5[i]; 102 103 *out++ = '0' + ((c&0xf0)>>4) + (c>=0xa0)*('a'-'9'-1); 104 *out++ = '0' + (c&0x0f) + ((c&0x0f)>=0x0a)*('a'-'9'-1); 105 } 106 *out = '\0'; 107 } 108 109 static int 110 nfs4_make_rec_clidname(char *dname, const struct xdr_netobj *clname) 111 { 112 struct xdr_netobj cksum; 113 struct crypto_shash *tfm; 114 int status; 115 116 dprintk("NFSD: nfs4_make_rec_clidname for %.*s\n", 117 clname->len, clname->data); 118 tfm = crypto_alloc_shash("md5", 0, 0); 119 if (IS_ERR(tfm)) { 120 status = PTR_ERR(tfm); 121 goto out_no_tfm; 122 } 123 124 cksum.len = crypto_shash_digestsize(tfm); 125 cksum.data = kmalloc(cksum.len, GFP_KERNEL); 126 if (cksum.data == NULL) { 127 status = -ENOMEM; 128 goto out; 129 } 130 131 status = crypto_shash_tfm_digest(tfm, clname->data, clname->len, 132 cksum.data); 133 if (status) 134 goto out; 135 136 md5_to_hex(dname, cksum.data); 137 138 status = 0; 139 out: 140 kfree(cksum.data); 141 crypto_free_shash(tfm); 142 out_no_tfm: 143 return status; 144 } 145 146 /* 147 * If we had an error generating the recdir name for the legacy tracker 148 * then warn the admin. If the error doesn't appear to be transient, 149 * then disable recovery tracking. 150 */ 151 static void 152 legacy_recdir_name_error(struct nfs4_client *clp, int error) 153 { 154 printk(KERN_ERR "NFSD: unable to generate recoverydir " 155 "name (%d).\n", error); 156 157 /* 158 * if the algorithm just doesn't exist, then disable the recovery 159 * tracker altogether. The crypto libs will generally return this if 160 * FIPS is enabled as well. 161 */ 162 if (error == -ENOENT) { 163 printk(KERN_ERR "NFSD: disabling legacy clientid tracking. " 164 "Reboot recovery will not function correctly!\n"); 165 nfsd4_client_tracking_exit(clp->net); 166 } 167 } 168 169 static void 170 __nfsd4_create_reclaim_record_grace(struct nfs4_client *clp, 171 const char *dname, int len, struct nfsd_net *nn) 172 { 173 struct xdr_netobj name; 174 struct xdr_netobj princhash = { .len = 0, .data = NULL }; 175 struct nfs4_client_reclaim *crp; 176 177 name.data = kmemdup(dname, len, GFP_KERNEL); 178 if (!name.data) { 179 dprintk("%s: failed to allocate memory for name.data!\n", 180 __func__); 181 return; 182 } 183 name.len = len; 184 crp = nfs4_client_to_reclaim(name, princhash, nn); 185 if (!crp) { 186 kfree(name.data); 187 return; 188 } 189 crp->cr_clp = clp; 190 } 191 192 static void 193 nfsd4_create_clid_dir(struct nfs4_client *clp) 194 { 195 const struct cred *original_cred; 196 char dname[HEXDIR_LEN]; 197 struct dentry *dir, *dentry; 198 int status; 199 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 200 201 if (test_and_set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags)) 202 return; 203 if (!nn->rec_file) 204 return; 205 206 status = nfs4_make_rec_clidname(dname, &clp->cl_name); 207 if (status) 208 return legacy_recdir_name_error(clp, status); 209 210 status = nfs4_save_creds(&original_cred); 211 if (status < 0) 212 return; 213 214 status = mnt_want_write_file(nn->rec_file); 215 if (status) 216 goto out_creds; 217 218 dir = nn->rec_file->f_path.dentry; 219 /* lock the parent */ 220 inode_lock(d_inode(dir)); 221 222 dentry = lookup_one(&nop_mnt_idmap, &QSTR(dname), dir); 223 if (IS_ERR(dentry)) { 224 status = PTR_ERR(dentry); 225 goto out_unlock; 226 } 227 if (d_really_is_positive(dentry)) 228 /* 229 * In the 4.1 case, where we're called from 230 * reclaim_complete(), records from the previous reboot 231 * may still be left, so this is OK. 232 * 233 * In the 4.0 case, we should never get here; but we may 234 * as well be forgiving and just succeed silently. 235 */ 236 goto out_put; 237 dentry = vfs_mkdir(&nop_mnt_idmap, d_inode(dir), dentry, S_IRWXU); 238 if (IS_ERR(dentry)) 239 status = PTR_ERR(dentry); 240 out_put: 241 if (!status) 242 dput(dentry); 243 out_unlock: 244 inode_unlock(d_inode(dir)); 245 if (status == 0) { 246 if (nn->in_grace) 247 __nfsd4_create_reclaim_record_grace(clp, dname, 248 HEXDIR_LEN, nn); 249 vfs_fsync(nn->rec_file, 0); 250 } else { 251 printk(KERN_ERR "NFSD: failed to write recovery record" 252 " (err %d); please check that %s exists" 253 " and is writeable", status, 254 user_recovery_dirname); 255 } 256 mnt_drop_write_file(nn->rec_file); 257 out_creds: 258 nfs4_reset_creds(original_cred); 259 } 260 261 typedef int (recdir_func)(struct dentry *, struct dentry *, struct nfsd_net *); 262 263 struct name_list { 264 char name[HEXDIR_LEN]; 265 struct list_head list; 266 }; 267 268 struct nfs4_dir_ctx { 269 struct dir_context ctx; 270 struct list_head names; 271 }; 272 273 static bool 274 nfsd4_build_namelist(struct dir_context *__ctx, const char *name, int namlen, 275 loff_t offset, u64 ino, unsigned int d_type) 276 { 277 struct nfs4_dir_ctx *ctx = 278 container_of(__ctx, struct nfs4_dir_ctx, ctx); 279 struct name_list *entry; 280 281 if (namlen != HEXDIR_LEN - 1) 282 return true; 283 entry = kmalloc(sizeof(struct name_list), GFP_KERNEL); 284 if (entry == NULL) 285 return false; 286 memcpy(entry->name, name, HEXDIR_LEN - 1); 287 entry->name[HEXDIR_LEN - 1] = '\0'; 288 list_add(&entry->list, &ctx->names); 289 return true; 290 } 291 292 static int 293 nfsd4_list_rec_dir(recdir_func *f, struct nfsd_net *nn) 294 { 295 const struct cred *original_cred; 296 struct dentry *dir = nn->rec_file->f_path.dentry; 297 struct nfs4_dir_ctx ctx = { 298 .ctx.actor = nfsd4_build_namelist, 299 .names = LIST_HEAD_INIT(ctx.names) 300 }; 301 struct name_list *entry, *tmp; 302 int status; 303 304 status = nfs4_save_creds(&original_cred); 305 if (status < 0) 306 return status; 307 308 status = vfs_llseek(nn->rec_file, 0, SEEK_SET); 309 if (status < 0) { 310 nfs4_reset_creds(original_cred); 311 return status; 312 } 313 314 status = iterate_dir(nn->rec_file, &ctx.ctx); 315 inode_lock_nested(d_inode(dir), I_MUTEX_PARENT); 316 317 list_for_each_entry_safe(entry, tmp, &ctx.names, list) { 318 if (!status) { 319 struct dentry *dentry; 320 dentry = lookup_one(&nop_mnt_idmap, 321 &QSTR(entry->name), dir); 322 if (IS_ERR(dentry)) { 323 status = PTR_ERR(dentry); 324 break; 325 } 326 status = f(dir, dentry, nn); 327 dput(dentry); 328 } 329 list_del(&entry->list); 330 kfree(entry); 331 } 332 inode_unlock(d_inode(dir)); 333 nfs4_reset_creds(original_cred); 334 335 list_for_each_entry_safe(entry, tmp, &ctx.names, list) { 336 dprintk("NFSD: %s. Left entry %s\n", __func__, entry->name); 337 list_del(&entry->list); 338 kfree(entry); 339 } 340 return status; 341 } 342 343 static int 344 nfsd4_unlink_clid_dir(char *name, struct nfsd_net *nn) 345 { 346 struct dentry *dir, *dentry; 347 int status; 348 349 dprintk("NFSD: nfsd4_unlink_clid_dir. name %s\n", name); 350 351 dir = nn->rec_file->f_path.dentry; 352 inode_lock_nested(d_inode(dir), I_MUTEX_PARENT); 353 dentry = lookup_one(&nop_mnt_idmap, &QSTR(name), dir); 354 if (IS_ERR(dentry)) { 355 status = PTR_ERR(dentry); 356 goto out_unlock; 357 } 358 status = -ENOENT; 359 if (d_really_is_negative(dentry)) 360 goto out; 361 status = vfs_rmdir(&nop_mnt_idmap, d_inode(dir), dentry); 362 out: 363 dput(dentry); 364 out_unlock: 365 inode_unlock(d_inode(dir)); 366 return status; 367 } 368 369 static void 370 __nfsd4_remove_reclaim_record_grace(const char *dname, int len, 371 struct nfsd_net *nn) 372 { 373 struct xdr_netobj name; 374 struct nfs4_client_reclaim *crp; 375 376 name.data = kmemdup(dname, len, GFP_KERNEL); 377 if (!name.data) { 378 dprintk("%s: failed to allocate memory for name.data!\n", 379 __func__); 380 return; 381 } 382 name.len = len; 383 crp = nfsd4_find_reclaim_client(name, nn); 384 kfree(name.data); 385 if (crp) 386 nfs4_remove_reclaim_record(crp, nn); 387 } 388 389 static void 390 nfsd4_remove_clid_dir(struct nfs4_client *clp) 391 { 392 const struct cred *original_cred; 393 char dname[HEXDIR_LEN]; 394 int status; 395 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 396 397 if (!nn->rec_file || !test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags)) 398 return; 399 400 status = nfs4_make_rec_clidname(dname, &clp->cl_name); 401 if (status) 402 return legacy_recdir_name_error(clp, status); 403 404 status = mnt_want_write_file(nn->rec_file); 405 if (status) 406 goto out; 407 clear_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags); 408 409 status = nfs4_save_creds(&original_cred); 410 if (status < 0) 411 goto out_drop_write; 412 413 status = nfsd4_unlink_clid_dir(dname, nn); 414 nfs4_reset_creds(original_cred); 415 if (status == 0) { 416 vfs_fsync(nn->rec_file, 0); 417 if (nn->in_grace) 418 __nfsd4_remove_reclaim_record_grace(dname, 419 HEXDIR_LEN, nn); 420 } 421 out_drop_write: 422 mnt_drop_write_file(nn->rec_file); 423 out: 424 if (status) 425 printk("NFSD: Failed to remove expired client state directory" 426 " %.*s\n", HEXDIR_LEN, dname); 427 } 428 429 static int 430 purge_old(struct dentry *parent, struct dentry *child, struct nfsd_net *nn) 431 { 432 int status; 433 struct xdr_netobj name; 434 435 if (child->d_name.len != HEXDIR_LEN - 1) { 436 printk("%s: illegal name %pd in recovery directory\n", 437 __func__, child); 438 /* Keep trying; maybe the others are OK: */ 439 return 0; 440 } 441 name.data = kmemdup_nul(child->d_name.name, child->d_name.len, GFP_KERNEL); 442 if (!name.data) { 443 dprintk("%s: failed to allocate memory for name.data!\n", 444 __func__); 445 goto out; 446 } 447 name.len = HEXDIR_LEN; 448 if (nfs4_has_reclaimed_state(name, nn)) 449 goto out_free; 450 451 status = vfs_rmdir(&nop_mnt_idmap, d_inode(parent), child); 452 if (status) 453 printk("failed to remove client recovery directory %pd\n", 454 child); 455 out_free: 456 kfree(name.data); 457 out: 458 /* Keep trying, success or failure: */ 459 return 0; 460 } 461 462 static void 463 nfsd4_recdir_purge_old(struct nfsd_net *nn) 464 { 465 int status; 466 467 nn->in_grace = false; 468 if (!nn->rec_file) 469 return; 470 status = mnt_want_write_file(nn->rec_file); 471 if (status) 472 goto out; 473 status = nfsd4_list_rec_dir(purge_old, nn); 474 if (status == 0) 475 vfs_fsync(nn->rec_file, 0); 476 mnt_drop_write_file(nn->rec_file); 477 out: 478 nfs4_release_reclaim(nn); 479 if (status) 480 printk("nfsd4: failed to purge old clients from recovery" 481 " directory %pD\n", nn->rec_file); 482 } 483 484 static int 485 load_recdir(struct dentry *parent, struct dentry *child, struct nfsd_net *nn) 486 { 487 struct xdr_netobj name; 488 struct xdr_netobj princhash = { .len = 0, .data = NULL }; 489 490 if (child->d_name.len != HEXDIR_LEN - 1) { 491 printk("%s: illegal name %pd in recovery directory\n", 492 __func__, child); 493 /* Keep trying; maybe the others are OK: */ 494 return 0; 495 } 496 name.data = kmemdup_nul(child->d_name.name, child->d_name.len, GFP_KERNEL); 497 if (!name.data) { 498 dprintk("%s: failed to allocate memory for name.data!\n", 499 __func__); 500 goto out; 501 } 502 name.len = HEXDIR_LEN; 503 if (!nfs4_client_to_reclaim(name, princhash, nn)) 504 kfree(name.data); 505 out: 506 return 0; 507 } 508 509 static int 510 nfsd4_recdir_load(struct net *net) { 511 int status; 512 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 513 514 if (!nn->rec_file) 515 return 0; 516 517 status = nfsd4_list_rec_dir(load_recdir, nn); 518 if (status) 519 printk("nfsd4: failed loading clients from recovery" 520 " directory %pD\n", nn->rec_file); 521 return status; 522 } 523 524 /* 525 * Hold reference to the recovery directory. 526 */ 527 528 static int 529 nfsd4_init_recdir(struct net *net) 530 { 531 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 532 const struct cred *original_cred; 533 int status; 534 535 printk("NFSD: Using %s as the NFSv4 state recovery directory\n", 536 user_recovery_dirname); 537 538 BUG_ON(nn->rec_file); 539 540 status = nfs4_save_creds(&original_cred); 541 if (status < 0) { 542 printk("NFSD: Unable to change credentials to find recovery" 543 " directory: error %d\n", 544 status); 545 return status; 546 } 547 548 nn->rec_file = filp_open(user_recovery_dirname, O_RDONLY | O_DIRECTORY, 0); 549 if (IS_ERR(nn->rec_file)) { 550 printk("NFSD: unable to find recovery directory %s\n", 551 user_recovery_dirname); 552 status = PTR_ERR(nn->rec_file); 553 nn->rec_file = NULL; 554 } 555 556 nfs4_reset_creds(original_cred); 557 if (!status) 558 nn->in_grace = true; 559 return status; 560 } 561 562 static void 563 nfsd4_shutdown_recdir(struct net *net) 564 { 565 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 566 567 if (!nn->rec_file) 568 return; 569 fput(nn->rec_file); 570 nn->rec_file = NULL; 571 } 572 573 static int 574 nfs4_legacy_state_init(struct net *net) 575 { 576 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 577 int i; 578 579 nn->reclaim_str_hashtbl = kmalloc_array(CLIENT_HASH_SIZE, 580 sizeof(struct list_head), 581 GFP_KERNEL); 582 if (!nn->reclaim_str_hashtbl) 583 return -ENOMEM; 584 585 for (i = 0; i < CLIENT_HASH_SIZE; i++) 586 INIT_LIST_HEAD(&nn->reclaim_str_hashtbl[i]); 587 nn->reclaim_str_hashtbl_size = 0; 588 589 return 0; 590 } 591 592 static void 593 nfs4_legacy_state_shutdown(struct net *net) 594 { 595 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 596 597 kfree(nn->reclaim_str_hashtbl); 598 } 599 600 static int 601 nfsd4_load_reboot_recovery_data(struct net *net) 602 { 603 int status; 604 605 status = nfsd4_init_recdir(net); 606 if (status) 607 return status; 608 609 status = nfsd4_recdir_load(net); 610 if (status) 611 nfsd4_shutdown_recdir(net); 612 613 return status; 614 } 615 616 static int 617 nfsd4_legacy_tracking_init(struct net *net) 618 { 619 int status; 620 621 /* XXX: The legacy code won't work in a container */ 622 if (net != &init_net) { 623 pr_warn("NFSD: attempt to initialize legacy client tracking in a container ignored.\n"); 624 return -EINVAL; 625 } 626 627 status = nfs4_legacy_state_init(net); 628 if (status) 629 return status; 630 631 status = nfsd4_load_reboot_recovery_data(net); 632 if (status) 633 goto err; 634 pr_info("NFSD: Using legacy client tracking operations.\n"); 635 return 0; 636 637 err: 638 nfs4_legacy_state_shutdown(net); 639 return status; 640 } 641 642 static void 643 nfsd4_legacy_tracking_exit(struct net *net) 644 { 645 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 646 647 nfs4_release_reclaim(nn); 648 nfsd4_shutdown_recdir(net); 649 nfs4_legacy_state_shutdown(net); 650 } 651 652 /* 653 * Change the NFSv4 recovery directory to recdir. 654 */ 655 int 656 nfs4_reset_recoverydir(char *recdir) 657 { 658 int status; 659 struct path path; 660 661 status = kern_path(recdir, LOOKUP_FOLLOW, &path); 662 if (status) 663 return status; 664 status = -ENOTDIR; 665 if (d_is_dir(path.dentry)) { 666 strscpy(user_recovery_dirname, recdir, 667 sizeof(user_recovery_dirname)); 668 status = 0; 669 } 670 path_put(&path); 671 return status; 672 } 673 674 char * 675 nfs4_recoverydir(void) 676 { 677 return user_recovery_dirname; 678 } 679 680 static int 681 nfsd4_check_legacy_client(struct nfs4_client *clp) 682 { 683 int status; 684 char dname[HEXDIR_LEN]; 685 struct nfs4_client_reclaim *crp; 686 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 687 struct xdr_netobj name; 688 689 /* did we already find that this client is stable? */ 690 if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags)) 691 return 0; 692 693 status = nfs4_make_rec_clidname(dname, &clp->cl_name); 694 if (status) { 695 legacy_recdir_name_error(clp, status); 696 return status; 697 } 698 699 /* look for it in the reclaim hashtable otherwise */ 700 name.data = kmemdup(dname, HEXDIR_LEN, GFP_KERNEL); 701 if (!name.data) { 702 dprintk("%s: failed to allocate memory for name.data!\n", 703 __func__); 704 goto out_enoent; 705 } 706 name.len = HEXDIR_LEN; 707 crp = nfsd4_find_reclaim_client(name, nn); 708 kfree(name.data); 709 if (crp) { 710 set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags); 711 crp->cr_clp = clp; 712 return 0; 713 } 714 715 out_enoent: 716 return -ENOENT; 717 } 718 719 static const struct nfsd4_client_tracking_ops nfsd4_legacy_tracking_ops = { 720 .init = nfsd4_legacy_tracking_init, 721 .exit = nfsd4_legacy_tracking_exit, 722 .create = nfsd4_create_clid_dir, 723 .remove = nfsd4_remove_clid_dir, 724 .check = nfsd4_check_legacy_client, 725 .grace_done = nfsd4_recdir_purge_old, 726 .version = 1, 727 .msglen = 0, 728 }; 729 #endif /* CONFIG_NFSD_LEGACY_CLIENT_TRACKING */ 730 731 /* Globals */ 732 #define NFSD_PIPE_DIR "nfsd" 733 #define NFSD_CLD_PIPE "cld" 734 735 /* per-net-ns structure for holding cld upcall info */ 736 struct cld_net { 737 struct rpc_pipe *cn_pipe; 738 spinlock_t cn_lock; 739 struct list_head cn_list; 740 unsigned int cn_xid; 741 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING 742 bool cn_has_legacy; 743 #endif 744 }; 745 746 struct cld_upcall { 747 struct list_head cu_list; 748 struct cld_net *cu_net; 749 struct completion cu_done; 750 union { 751 struct cld_msg_hdr cu_hdr; 752 struct cld_msg cu_msg; 753 struct cld_msg_v2 cu_msg_v2; 754 } cu_u; 755 }; 756 757 static int 758 __cld_pipe_upcall(struct rpc_pipe *pipe, void *cmsg, struct nfsd_net *nn) 759 { 760 int ret; 761 struct rpc_pipe_msg msg; 762 struct cld_upcall *cup = container_of(cmsg, struct cld_upcall, cu_u); 763 764 memset(&msg, 0, sizeof(msg)); 765 msg.data = cmsg; 766 msg.len = nn->client_tracking_ops->msglen; 767 768 ret = rpc_queue_upcall(pipe, &msg); 769 if (ret < 0) { 770 goto out; 771 } 772 773 wait_for_completion(&cup->cu_done); 774 775 if (msg.errno < 0) 776 ret = msg.errno; 777 out: 778 return ret; 779 } 780 781 static int 782 cld_pipe_upcall(struct rpc_pipe *pipe, void *cmsg, struct nfsd_net *nn) 783 { 784 int ret; 785 786 /* 787 * -EAGAIN occurs when pipe is closed and reopened while there are 788 * upcalls queued. 789 */ 790 do { 791 ret = __cld_pipe_upcall(pipe, cmsg, nn); 792 } while (ret == -EAGAIN); 793 794 return ret; 795 } 796 797 static ssize_t 798 __cld_pipe_inprogress_downcall(const struct cld_msg_v2 __user *cmsg, 799 struct nfsd_net *nn) 800 { 801 uint8_t cmd, princhashlen; 802 struct xdr_netobj name, princhash = { .len = 0, .data = NULL }; 803 uint16_t namelen; 804 805 if (get_user(cmd, &cmsg->cm_cmd)) { 806 dprintk("%s: error when copying cmd from userspace", __func__); 807 return -EFAULT; 808 } 809 if (cmd == Cld_GraceStart) { 810 if (nn->client_tracking_ops->version >= 2) { 811 const struct cld_clntinfo __user *ci; 812 813 ci = &cmsg->cm_u.cm_clntinfo; 814 if (get_user(namelen, &ci->cc_name.cn_len)) 815 return -EFAULT; 816 if (namelen == 0 || namelen > NFS4_OPAQUE_LIMIT) { 817 dprintk("%s: invalid namelen (%u)", __func__, namelen); 818 return -EINVAL; 819 } 820 name.data = memdup_user(&ci->cc_name.cn_id, namelen); 821 if (IS_ERR(name.data)) 822 return PTR_ERR(name.data); 823 name.len = namelen; 824 get_user(princhashlen, &ci->cc_princhash.cp_len); 825 if (princhashlen > 0) { 826 princhash.data = memdup_user( 827 &ci->cc_princhash.cp_data, 828 princhashlen); 829 if (IS_ERR(princhash.data)) { 830 kfree(name.data); 831 return PTR_ERR(princhash.data); 832 } 833 princhash.len = princhashlen; 834 } else 835 princhash.len = 0; 836 } else { 837 const struct cld_name __user *cnm; 838 839 cnm = &cmsg->cm_u.cm_name; 840 if (get_user(namelen, &cnm->cn_len)) 841 return -EFAULT; 842 if (namelen == 0 || namelen > NFS4_OPAQUE_LIMIT) { 843 dprintk("%s: invalid namelen (%u)", __func__, namelen); 844 return -EINVAL; 845 } 846 name.data = memdup_user(&cnm->cn_id, namelen); 847 if (IS_ERR(name.data)) 848 return PTR_ERR(name.data); 849 name.len = namelen; 850 } 851 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING 852 if (name.len > 5 && memcmp(name.data, "hash:", 5) == 0) { 853 struct cld_net *cn = nn->cld_net; 854 855 name.len = name.len - 5; 856 memmove(name.data, name.data + 5, name.len); 857 cn->cn_has_legacy = true; 858 } 859 #endif 860 if (!nfs4_client_to_reclaim(name, princhash, nn)) { 861 kfree(name.data); 862 kfree(princhash.data); 863 return -EFAULT; 864 } 865 return nn->client_tracking_ops->msglen; 866 } 867 return -EFAULT; 868 } 869 870 static ssize_t 871 cld_pipe_downcall(struct file *filp, const char __user *src, size_t mlen) 872 { 873 struct cld_upcall *tmp, *cup; 874 struct cld_msg_hdr __user *hdr = (struct cld_msg_hdr __user *)src; 875 struct cld_msg_v2 __user *cmsg = (struct cld_msg_v2 __user *)src; 876 uint32_t xid; 877 struct nfsd_net *nn = net_generic(file_inode(filp)->i_sb->s_fs_info, 878 nfsd_net_id); 879 struct cld_net *cn = nn->cld_net; 880 int16_t status; 881 882 if (mlen != nn->client_tracking_ops->msglen) { 883 dprintk("%s: got %zu bytes, expected %zu\n", __func__, mlen, 884 nn->client_tracking_ops->msglen); 885 return -EINVAL; 886 } 887 888 /* copy just the xid so we can try to find that */ 889 if (copy_from_user(&xid, &hdr->cm_xid, sizeof(xid)) != 0) { 890 dprintk("%s: error when copying xid from userspace", __func__); 891 return -EFAULT; 892 } 893 894 /* 895 * copy the status so we know whether to remove the upcall from the 896 * list (for -EINPROGRESS, we just want to make sure the xid is 897 * valid, not remove the upcall from the list) 898 */ 899 if (get_user(status, &hdr->cm_status)) { 900 dprintk("%s: error when copying status from userspace", __func__); 901 return -EFAULT; 902 } 903 904 /* walk the list and find corresponding xid */ 905 cup = NULL; 906 spin_lock(&cn->cn_lock); 907 list_for_each_entry(tmp, &cn->cn_list, cu_list) { 908 if (get_unaligned(&tmp->cu_u.cu_hdr.cm_xid) == xid) { 909 cup = tmp; 910 if (status != -EINPROGRESS) 911 list_del_init(&cup->cu_list); 912 break; 913 } 914 } 915 spin_unlock(&cn->cn_lock); 916 917 /* couldn't find upcall? */ 918 if (!cup) { 919 dprintk("%s: couldn't find upcall -- xid=%u\n", __func__, xid); 920 return -EINVAL; 921 } 922 923 if (status == -EINPROGRESS) 924 return __cld_pipe_inprogress_downcall(cmsg, nn); 925 926 if (copy_from_user(&cup->cu_u.cu_msg_v2, src, mlen) != 0) 927 return -EFAULT; 928 929 complete(&cup->cu_done); 930 return mlen; 931 } 932 933 static void 934 cld_pipe_destroy_msg(struct rpc_pipe_msg *msg) 935 { 936 struct cld_msg *cmsg = msg->data; 937 struct cld_upcall *cup = container_of(cmsg, struct cld_upcall, 938 cu_u.cu_msg); 939 940 /* errno >= 0 means we got a downcall */ 941 if (msg->errno >= 0) 942 return; 943 944 complete(&cup->cu_done); 945 } 946 947 static const struct rpc_pipe_ops cld_upcall_ops = { 948 .upcall = rpc_pipe_generic_upcall, 949 .downcall = cld_pipe_downcall, 950 .destroy_msg = cld_pipe_destroy_msg, 951 }; 952 953 static struct dentry * 954 nfsd4_cld_register_sb(struct super_block *sb, struct rpc_pipe *pipe) 955 { 956 struct dentry *dir, *dentry; 957 958 dir = rpc_d_lookup_sb(sb, NFSD_PIPE_DIR); 959 if (dir == NULL) 960 return ERR_PTR(-ENOENT); 961 dentry = rpc_mkpipe_dentry(dir, NFSD_CLD_PIPE, NULL, pipe); 962 dput(dir); 963 return dentry; 964 } 965 966 static void 967 nfsd4_cld_unregister_sb(struct rpc_pipe *pipe) 968 { 969 if (pipe->dentry) 970 rpc_unlink(pipe->dentry); 971 } 972 973 static struct dentry * 974 nfsd4_cld_register_net(struct net *net, struct rpc_pipe *pipe) 975 { 976 struct super_block *sb; 977 struct dentry *dentry; 978 979 sb = rpc_get_sb_net(net); 980 if (!sb) 981 return NULL; 982 dentry = nfsd4_cld_register_sb(sb, pipe); 983 rpc_put_sb_net(net); 984 return dentry; 985 } 986 987 static void 988 nfsd4_cld_unregister_net(struct net *net, struct rpc_pipe *pipe) 989 { 990 struct super_block *sb; 991 992 sb = rpc_get_sb_net(net); 993 if (sb) { 994 nfsd4_cld_unregister_sb(pipe); 995 rpc_put_sb_net(net); 996 } 997 } 998 999 /* Initialize rpc_pipefs pipe for communication with client tracking daemon */ 1000 static int 1001 __nfsd4_init_cld_pipe(struct net *net) 1002 { 1003 int ret; 1004 struct dentry *dentry; 1005 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 1006 struct cld_net *cn; 1007 1008 if (nn->cld_net) 1009 return 0; 1010 1011 cn = kzalloc(sizeof(*cn), GFP_KERNEL); 1012 if (!cn) { 1013 ret = -ENOMEM; 1014 goto err; 1015 } 1016 1017 cn->cn_pipe = rpc_mkpipe_data(&cld_upcall_ops, RPC_PIPE_WAIT_FOR_OPEN); 1018 if (IS_ERR(cn->cn_pipe)) { 1019 ret = PTR_ERR(cn->cn_pipe); 1020 goto err; 1021 } 1022 spin_lock_init(&cn->cn_lock); 1023 INIT_LIST_HEAD(&cn->cn_list); 1024 1025 dentry = nfsd4_cld_register_net(net, cn->cn_pipe); 1026 if (IS_ERR(dentry)) { 1027 ret = PTR_ERR(dentry); 1028 goto err_destroy_data; 1029 } 1030 1031 cn->cn_pipe->dentry = dentry; 1032 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING 1033 cn->cn_has_legacy = false; 1034 #endif 1035 nn->cld_net = cn; 1036 return 0; 1037 1038 err_destroy_data: 1039 rpc_destroy_pipe_data(cn->cn_pipe); 1040 err: 1041 kfree(cn); 1042 printk(KERN_ERR "NFSD: unable to create nfsdcld upcall pipe (%d)\n", 1043 ret); 1044 return ret; 1045 } 1046 1047 static int 1048 nfsd4_init_cld_pipe(struct net *net) 1049 { 1050 int status; 1051 1052 status = __nfsd4_init_cld_pipe(net); 1053 if (!status) 1054 pr_info("NFSD: Using old nfsdcld client tracking operations.\n"); 1055 return status; 1056 } 1057 1058 static void 1059 nfsd4_remove_cld_pipe(struct net *net) 1060 { 1061 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 1062 struct cld_net *cn = nn->cld_net; 1063 1064 nfsd4_cld_unregister_net(net, cn->cn_pipe); 1065 rpc_destroy_pipe_data(cn->cn_pipe); 1066 kfree(nn->cld_net); 1067 nn->cld_net = NULL; 1068 } 1069 1070 static struct cld_upcall * 1071 alloc_cld_upcall(struct nfsd_net *nn) 1072 { 1073 struct cld_upcall *new, *tmp; 1074 struct cld_net *cn = nn->cld_net; 1075 1076 new = kzalloc(sizeof(*new), GFP_KERNEL); 1077 if (!new) 1078 return new; 1079 1080 /* FIXME: hard cap on number in flight? */ 1081 restart_search: 1082 spin_lock(&cn->cn_lock); 1083 list_for_each_entry(tmp, &cn->cn_list, cu_list) { 1084 if (tmp->cu_u.cu_msg.cm_xid == cn->cn_xid) { 1085 cn->cn_xid++; 1086 spin_unlock(&cn->cn_lock); 1087 goto restart_search; 1088 } 1089 } 1090 init_completion(&new->cu_done); 1091 new->cu_u.cu_msg.cm_vers = nn->client_tracking_ops->version; 1092 put_unaligned(cn->cn_xid++, &new->cu_u.cu_msg.cm_xid); 1093 new->cu_net = cn; 1094 list_add(&new->cu_list, &cn->cn_list); 1095 spin_unlock(&cn->cn_lock); 1096 1097 dprintk("%s: allocated xid %u\n", __func__, new->cu_u.cu_msg.cm_xid); 1098 1099 return new; 1100 } 1101 1102 static void 1103 free_cld_upcall(struct cld_upcall *victim) 1104 { 1105 struct cld_net *cn = victim->cu_net; 1106 1107 spin_lock(&cn->cn_lock); 1108 list_del(&victim->cu_list); 1109 spin_unlock(&cn->cn_lock); 1110 kfree(victim); 1111 } 1112 1113 /* Ask daemon to create a new record */ 1114 static void 1115 nfsd4_cld_create(struct nfs4_client *clp) 1116 { 1117 int ret; 1118 struct cld_upcall *cup; 1119 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 1120 struct cld_net *cn = nn->cld_net; 1121 1122 /* Don't upcall if it's already stored */ 1123 if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags)) 1124 return; 1125 1126 cup = alloc_cld_upcall(nn); 1127 if (!cup) { 1128 ret = -ENOMEM; 1129 goto out_err; 1130 } 1131 1132 cup->cu_u.cu_msg.cm_cmd = Cld_Create; 1133 cup->cu_u.cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len; 1134 memcpy(cup->cu_u.cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data, 1135 clp->cl_name.len); 1136 1137 ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_u.cu_msg, nn); 1138 if (!ret) { 1139 ret = cup->cu_u.cu_msg.cm_status; 1140 set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags); 1141 } 1142 1143 free_cld_upcall(cup); 1144 out_err: 1145 if (ret) 1146 printk(KERN_ERR "NFSD: Unable to create client " 1147 "record on stable storage: %d\n", ret); 1148 } 1149 1150 /* Ask daemon to create a new record */ 1151 static void 1152 nfsd4_cld_create_v2(struct nfs4_client *clp) 1153 { 1154 int ret; 1155 struct cld_upcall *cup; 1156 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 1157 struct cld_net *cn = nn->cld_net; 1158 struct cld_msg_v2 *cmsg; 1159 char *principal = NULL; 1160 1161 /* Don't upcall if it's already stored */ 1162 if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags)) 1163 return; 1164 1165 cup = alloc_cld_upcall(nn); 1166 if (!cup) { 1167 ret = -ENOMEM; 1168 goto out_err; 1169 } 1170 1171 cmsg = &cup->cu_u.cu_msg_v2; 1172 cmsg->cm_cmd = Cld_Create; 1173 cmsg->cm_u.cm_clntinfo.cc_name.cn_len = clp->cl_name.len; 1174 memcpy(cmsg->cm_u.cm_clntinfo.cc_name.cn_id, clp->cl_name.data, 1175 clp->cl_name.len); 1176 if (clp->cl_cred.cr_raw_principal) 1177 principal = clp->cl_cred.cr_raw_principal; 1178 else if (clp->cl_cred.cr_principal) 1179 principal = clp->cl_cred.cr_principal; 1180 if (principal) { 1181 sha256(principal, strlen(principal), 1182 cmsg->cm_u.cm_clntinfo.cc_princhash.cp_data); 1183 cmsg->cm_u.cm_clntinfo.cc_princhash.cp_len = SHA256_DIGEST_SIZE; 1184 } else 1185 cmsg->cm_u.cm_clntinfo.cc_princhash.cp_len = 0; 1186 1187 ret = cld_pipe_upcall(cn->cn_pipe, cmsg, nn); 1188 if (!ret) { 1189 ret = cmsg->cm_status; 1190 set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags); 1191 } 1192 1193 free_cld_upcall(cup); 1194 out_err: 1195 if (ret) 1196 pr_err("NFSD: Unable to create client record on stable storage: %d\n", 1197 ret); 1198 } 1199 1200 /* Ask daemon to create a new record */ 1201 static void 1202 nfsd4_cld_remove(struct nfs4_client *clp) 1203 { 1204 int ret; 1205 struct cld_upcall *cup; 1206 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 1207 struct cld_net *cn = nn->cld_net; 1208 1209 /* Don't upcall if it's already removed */ 1210 if (!test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags)) 1211 return; 1212 1213 cup = alloc_cld_upcall(nn); 1214 if (!cup) { 1215 ret = -ENOMEM; 1216 goto out_err; 1217 } 1218 1219 cup->cu_u.cu_msg.cm_cmd = Cld_Remove; 1220 cup->cu_u.cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len; 1221 memcpy(cup->cu_u.cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data, 1222 clp->cl_name.len); 1223 1224 ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_u.cu_msg, nn); 1225 if (!ret) { 1226 ret = cup->cu_u.cu_msg.cm_status; 1227 clear_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags); 1228 } 1229 1230 free_cld_upcall(cup); 1231 out_err: 1232 if (ret) 1233 printk(KERN_ERR "NFSD: Unable to remove client " 1234 "record from stable storage: %d\n", ret); 1235 } 1236 1237 /* 1238 * For older nfsdcld's that do not allow us to "slurp" the clients 1239 * from the tracking database during startup. 1240 * 1241 * Check for presence of a record, and update its timestamp 1242 */ 1243 static int 1244 nfsd4_cld_check_v0(struct nfs4_client *clp) 1245 { 1246 int ret; 1247 struct cld_upcall *cup; 1248 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 1249 struct cld_net *cn = nn->cld_net; 1250 1251 /* Don't upcall if one was already stored during this grace pd */ 1252 if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags)) 1253 return 0; 1254 1255 cup = alloc_cld_upcall(nn); 1256 if (!cup) { 1257 printk(KERN_ERR "NFSD: Unable to check client record on " 1258 "stable storage: %d\n", -ENOMEM); 1259 return -ENOMEM; 1260 } 1261 1262 cup->cu_u.cu_msg.cm_cmd = Cld_Check; 1263 cup->cu_u.cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len; 1264 memcpy(cup->cu_u.cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data, 1265 clp->cl_name.len); 1266 1267 ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_u.cu_msg, nn); 1268 if (!ret) { 1269 ret = cup->cu_u.cu_msg.cm_status; 1270 set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags); 1271 } 1272 1273 free_cld_upcall(cup); 1274 return ret; 1275 } 1276 1277 /* 1278 * For newer nfsdcld's that allow us to "slurp" the clients 1279 * from the tracking database during startup. 1280 * 1281 * Check for presence of a record in the reclaim_str_hashtbl 1282 */ 1283 static int 1284 nfsd4_cld_check(struct nfs4_client *clp) 1285 { 1286 struct nfs4_client_reclaim *crp; 1287 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 1288 1289 /* did we already find that this client is stable? */ 1290 if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags)) 1291 return 0; 1292 1293 /* look for it in the reclaim hashtable otherwise */ 1294 crp = nfsd4_find_reclaim_client(clp->cl_name, nn); 1295 if (crp) 1296 goto found; 1297 1298 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING 1299 if (nn->cld_net->cn_has_legacy) { 1300 int status; 1301 char dname[HEXDIR_LEN]; 1302 struct xdr_netobj name; 1303 1304 status = nfs4_make_rec_clidname(dname, &clp->cl_name); 1305 if (status) 1306 return -ENOENT; 1307 1308 name.data = kmemdup(dname, HEXDIR_LEN, GFP_KERNEL); 1309 if (!name.data) { 1310 dprintk("%s: failed to allocate memory for name.data!\n", 1311 __func__); 1312 return -ENOENT; 1313 } 1314 name.len = HEXDIR_LEN; 1315 crp = nfsd4_find_reclaim_client(name, nn); 1316 kfree(name.data); 1317 if (crp) 1318 goto found; 1319 1320 } 1321 #endif 1322 return -ENOENT; 1323 found: 1324 crp->cr_clp = clp; 1325 return 0; 1326 } 1327 1328 static int 1329 nfsd4_cld_check_v2(struct nfs4_client *clp) 1330 { 1331 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 1332 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING 1333 struct cld_net *cn = nn->cld_net; 1334 #endif 1335 struct nfs4_client_reclaim *crp; 1336 char *principal = NULL; 1337 1338 /* did we already find that this client is stable? */ 1339 if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags)) 1340 return 0; 1341 1342 /* look for it in the reclaim hashtable otherwise */ 1343 crp = nfsd4_find_reclaim_client(clp->cl_name, nn); 1344 if (crp) 1345 goto found; 1346 1347 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING 1348 if (cn->cn_has_legacy) { 1349 struct xdr_netobj name; 1350 char dname[HEXDIR_LEN]; 1351 int status; 1352 1353 status = nfs4_make_rec_clidname(dname, &clp->cl_name); 1354 if (status) 1355 return -ENOENT; 1356 1357 name.data = kmemdup(dname, HEXDIR_LEN, GFP_KERNEL); 1358 if (!name.data) { 1359 dprintk("%s: failed to allocate memory for name.data\n", 1360 __func__); 1361 return -ENOENT; 1362 } 1363 name.len = HEXDIR_LEN; 1364 crp = nfsd4_find_reclaim_client(name, nn); 1365 kfree(name.data); 1366 if (crp) 1367 goto found; 1368 1369 } 1370 #endif 1371 return -ENOENT; 1372 found: 1373 if (crp->cr_princhash.len) { 1374 u8 digest[SHA256_DIGEST_SIZE]; 1375 1376 if (clp->cl_cred.cr_raw_principal) 1377 principal = clp->cl_cred.cr_raw_principal; 1378 else if (clp->cl_cred.cr_principal) 1379 principal = clp->cl_cred.cr_principal; 1380 if (principal == NULL) 1381 return -ENOENT; 1382 sha256(principal, strlen(principal), digest); 1383 if (memcmp(crp->cr_princhash.data, digest, 1384 crp->cr_princhash.len)) 1385 return -ENOENT; 1386 } 1387 crp->cr_clp = clp; 1388 return 0; 1389 } 1390 1391 static int 1392 nfsd4_cld_grace_start(struct nfsd_net *nn) 1393 { 1394 int ret; 1395 struct cld_upcall *cup; 1396 struct cld_net *cn = nn->cld_net; 1397 1398 cup = alloc_cld_upcall(nn); 1399 if (!cup) { 1400 ret = -ENOMEM; 1401 goto out_err; 1402 } 1403 1404 cup->cu_u.cu_msg.cm_cmd = Cld_GraceStart; 1405 ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_u.cu_msg, nn); 1406 if (!ret) 1407 ret = cup->cu_u.cu_msg.cm_status; 1408 1409 free_cld_upcall(cup); 1410 out_err: 1411 if (ret) 1412 dprintk("%s: Unable to get clients from userspace: %d\n", 1413 __func__, ret); 1414 return ret; 1415 } 1416 1417 /* For older nfsdcld's that need cm_gracetime */ 1418 static void 1419 nfsd4_cld_grace_done_v0(struct nfsd_net *nn) 1420 { 1421 int ret; 1422 struct cld_upcall *cup; 1423 struct cld_net *cn = nn->cld_net; 1424 1425 cup = alloc_cld_upcall(nn); 1426 if (!cup) { 1427 ret = -ENOMEM; 1428 goto out_err; 1429 } 1430 1431 cup->cu_u.cu_msg.cm_cmd = Cld_GraceDone; 1432 cup->cu_u.cu_msg.cm_u.cm_gracetime = nn->boot_time; 1433 ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_u.cu_msg, nn); 1434 if (!ret) 1435 ret = cup->cu_u.cu_msg.cm_status; 1436 1437 free_cld_upcall(cup); 1438 out_err: 1439 if (ret) 1440 printk(KERN_ERR "NFSD: Unable to end grace period: %d\n", ret); 1441 } 1442 1443 /* 1444 * For newer nfsdcld's that do not need cm_gracetime. We also need to call 1445 * nfs4_release_reclaim() to clear out the reclaim_str_hashtbl. 1446 */ 1447 static void 1448 nfsd4_cld_grace_done(struct nfsd_net *nn) 1449 { 1450 int ret; 1451 struct cld_upcall *cup; 1452 struct cld_net *cn = nn->cld_net; 1453 1454 cup = alloc_cld_upcall(nn); 1455 if (!cup) { 1456 ret = -ENOMEM; 1457 goto out_err; 1458 } 1459 1460 cup->cu_u.cu_msg.cm_cmd = Cld_GraceDone; 1461 ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_u.cu_msg, nn); 1462 if (!ret) 1463 ret = cup->cu_u.cu_msg.cm_status; 1464 1465 free_cld_upcall(cup); 1466 out_err: 1467 nfs4_release_reclaim(nn); 1468 if (ret) 1469 printk(KERN_ERR "NFSD: Unable to end grace period: %d\n", ret); 1470 } 1471 1472 static int 1473 nfs4_cld_state_init(struct net *net) 1474 { 1475 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 1476 int i; 1477 1478 nn->reclaim_str_hashtbl = kmalloc_array(CLIENT_HASH_SIZE, 1479 sizeof(struct list_head), 1480 GFP_KERNEL); 1481 if (!nn->reclaim_str_hashtbl) 1482 return -ENOMEM; 1483 1484 for (i = 0; i < CLIENT_HASH_SIZE; i++) 1485 INIT_LIST_HEAD(&nn->reclaim_str_hashtbl[i]); 1486 nn->reclaim_str_hashtbl_size = 0; 1487 nn->track_reclaim_completes = true; 1488 atomic_set(&nn->nr_reclaim_complete, 0); 1489 1490 return 0; 1491 } 1492 1493 static void 1494 nfs4_cld_state_shutdown(struct net *net) 1495 { 1496 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 1497 1498 nn->track_reclaim_completes = false; 1499 kfree(nn->reclaim_str_hashtbl); 1500 } 1501 1502 static bool 1503 cld_running(struct nfsd_net *nn) 1504 { 1505 struct cld_net *cn = nn->cld_net; 1506 struct rpc_pipe *pipe = cn->cn_pipe; 1507 1508 return pipe->nreaders || pipe->nwriters; 1509 } 1510 1511 static int 1512 nfsd4_cld_get_version(struct nfsd_net *nn) 1513 { 1514 int ret = 0; 1515 struct cld_upcall *cup; 1516 struct cld_net *cn = nn->cld_net; 1517 uint8_t version; 1518 1519 cup = alloc_cld_upcall(nn); 1520 if (!cup) { 1521 ret = -ENOMEM; 1522 goto out_err; 1523 } 1524 cup->cu_u.cu_msg.cm_cmd = Cld_GetVersion; 1525 ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_u.cu_msg, nn); 1526 if (!ret) { 1527 ret = cup->cu_u.cu_msg.cm_status; 1528 if (ret) 1529 goto out_free; 1530 version = cup->cu_u.cu_msg.cm_u.cm_version; 1531 dprintk("%s: userspace returned version %u\n", 1532 __func__, version); 1533 if (version < 1) 1534 version = 1; 1535 else if (version > CLD_UPCALL_VERSION) 1536 version = CLD_UPCALL_VERSION; 1537 1538 switch (version) { 1539 case 1: 1540 nn->client_tracking_ops = &nfsd4_cld_tracking_ops; 1541 break; 1542 case 2: 1543 nn->client_tracking_ops = &nfsd4_cld_tracking_ops_v2; 1544 break; 1545 default: 1546 break; 1547 } 1548 } 1549 out_free: 1550 free_cld_upcall(cup); 1551 out_err: 1552 if (ret) 1553 dprintk("%s: Unable to get version from userspace: %d\n", 1554 __func__, ret); 1555 return ret; 1556 } 1557 1558 static int 1559 nfsd4_cld_tracking_init(struct net *net) 1560 { 1561 int status; 1562 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 1563 bool running; 1564 int retries = 10; 1565 1566 status = nfs4_cld_state_init(net); 1567 if (status) 1568 return status; 1569 1570 status = __nfsd4_init_cld_pipe(net); 1571 if (status) 1572 goto err_shutdown; 1573 1574 /* 1575 * rpc pipe upcalls take 30 seconds to time out, so we don't want to 1576 * queue an upcall unless we know that nfsdcld is running (because we 1577 * want this to fail fast so that nfsd4_client_tracking_init() can try 1578 * the next client tracking method). nfsdcld should already be running 1579 * before nfsd is started, so the wait here is for nfsdcld to open the 1580 * pipefs file we just created. 1581 */ 1582 while (!(running = cld_running(nn)) && retries--) 1583 msleep(100); 1584 1585 if (!running) { 1586 status = -ETIMEDOUT; 1587 goto err_remove; 1588 } 1589 1590 status = nfsd4_cld_get_version(nn); 1591 if (status == -EOPNOTSUPP) 1592 pr_warn("NFSD: nfsdcld GetVersion upcall failed. Please upgrade nfsdcld.\n"); 1593 1594 status = nfsd4_cld_grace_start(nn); 1595 if (status) { 1596 if (status == -EOPNOTSUPP) 1597 pr_warn("NFSD: nfsdcld GraceStart upcall failed. Please upgrade nfsdcld.\n"); 1598 nfs4_release_reclaim(nn); 1599 goto err_remove; 1600 } else 1601 pr_info("NFSD: Using nfsdcld client tracking operations.\n"); 1602 return 0; 1603 1604 err_remove: 1605 nfsd4_remove_cld_pipe(net); 1606 err_shutdown: 1607 nfs4_cld_state_shutdown(net); 1608 return status; 1609 } 1610 1611 static void 1612 nfsd4_cld_tracking_exit(struct net *net) 1613 { 1614 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 1615 1616 nfs4_release_reclaim(nn); 1617 nfsd4_remove_cld_pipe(net); 1618 nfs4_cld_state_shutdown(net); 1619 } 1620 1621 /* For older nfsdcld's */ 1622 static const struct nfsd4_client_tracking_ops nfsd4_cld_tracking_ops_v0 = { 1623 .init = nfsd4_init_cld_pipe, 1624 .exit = nfsd4_remove_cld_pipe, 1625 .create = nfsd4_cld_create, 1626 .remove = nfsd4_cld_remove, 1627 .check = nfsd4_cld_check_v0, 1628 .grace_done = nfsd4_cld_grace_done_v0, 1629 .version = 1, 1630 .msglen = sizeof(struct cld_msg), 1631 }; 1632 1633 /* For newer nfsdcld's */ 1634 static const struct nfsd4_client_tracking_ops nfsd4_cld_tracking_ops = { 1635 .init = nfsd4_cld_tracking_init, 1636 .exit = nfsd4_cld_tracking_exit, 1637 .create = nfsd4_cld_create, 1638 .remove = nfsd4_cld_remove, 1639 .check = nfsd4_cld_check, 1640 .grace_done = nfsd4_cld_grace_done, 1641 .version = 1, 1642 .msglen = sizeof(struct cld_msg), 1643 }; 1644 1645 /* v2 create/check ops include the principal, if available */ 1646 static const struct nfsd4_client_tracking_ops nfsd4_cld_tracking_ops_v2 = { 1647 .init = nfsd4_cld_tracking_init, 1648 .exit = nfsd4_cld_tracking_exit, 1649 .create = nfsd4_cld_create_v2, 1650 .remove = nfsd4_cld_remove, 1651 .check = nfsd4_cld_check_v2, 1652 .grace_done = nfsd4_cld_grace_done, 1653 .version = 2, 1654 .msglen = sizeof(struct cld_msg_v2), 1655 }; 1656 1657 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING 1658 /* upcall via usermodehelper */ 1659 static char cltrack_prog[PATH_MAX] = "/sbin/nfsdcltrack"; 1660 module_param_string(cltrack_prog, cltrack_prog, sizeof(cltrack_prog), 1661 S_IRUGO|S_IWUSR); 1662 MODULE_PARM_DESC(cltrack_prog, "Path to the nfsdcltrack upcall program"); 1663 1664 static bool cltrack_legacy_disable; 1665 module_param(cltrack_legacy_disable, bool, S_IRUGO|S_IWUSR); 1666 MODULE_PARM_DESC(cltrack_legacy_disable, 1667 "Disable legacy recoverydir conversion. Default: false"); 1668 1669 #define LEGACY_TOPDIR_ENV_PREFIX "NFSDCLTRACK_LEGACY_TOPDIR=" 1670 #define LEGACY_RECDIR_ENV_PREFIX "NFSDCLTRACK_LEGACY_RECDIR=" 1671 #define HAS_SESSION_ENV_PREFIX "NFSDCLTRACK_CLIENT_HAS_SESSION=" 1672 #define GRACE_START_ENV_PREFIX "NFSDCLTRACK_GRACE_START=" 1673 1674 static char * 1675 nfsd4_cltrack_legacy_topdir(void) 1676 { 1677 int copied; 1678 size_t len; 1679 char *result; 1680 1681 if (cltrack_legacy_disable) 1682 return NULL; 1683 1684 len = strlen(LEGACY_TOPDIR_ENV_PREFIX) + 1685 strlen(nfs4_recoverydir()) + 1; 1686 1687 result = kmalloc(len, GFP_KERNEL); 1688 if (!result) 1689 return result; 1690 1691 copied = snprintf(result, len, LEGACY_TOPDIR_ENV_PREFIX "%s", 1692 nfs4_recoverydir()); 1693 if (copied >= len) { 1694 /* just return nothing if output was truncated */ 1695 kfree(result); 1696 return NULL; 1697 } 1698 1699 return result; 1700 } 1701 1702 static char * 1703 nfsd4_cltrack_legacy_recdir(const struct xdr_netobj *name) 1704 { 1705 int copied; 1706 size_t len; 1707 char *result; 1708 1709 if (cltrack_legacy_disable) 1710 return NULL; 1711 1712 /* +1 is for '/' between "topdir" and "recdir" */ 1713 len = strlen(LEGACY_RECDIR_ENV_PREFIX) + 1714 strlen(nfs4_recoverydir()) + 1 + HEXDIR_LEN; 1715 1716 result = kmalloc(len, GFP_KERNEL); 1717 if (!result) 1718 return result; 1719 1720 copied = snprintf(result, len, LEGACY_RECDIR_ENV_PREFIX "%s/", 1721 nfs4_recoverydir()); 1722 if (copied > (len - HEXDIR_LEN)) { 1723 /* just return nothing if output will be truncated */ 1724 kfree(result); 1725 return NULL; 1726 } 1727 1728 copied = nfs4_make_rec_clidname(result + copied, name); 1729 if (copied) { 1730 kfree(result); 1731 return NULL; 1732 } 1733 1734 return result; 1735 } 1736 1737 static char * 1738 nfsd4_cltrack_client_has_session(struct nfs4_client *clp) 1739 { 1740 int copied; 1741 size_t len; 1742 char *result; 1743 1744 /* prefix + Y/N character + terminating NULL */ 1745 len = strlen(HAS_SESSION_ENV_PREFIX) + 1 + 1; 1746 1747 result = kmalloc(len, GFP_KERNEL); 1748 if (!result) 1749 return result; 1750 1751 copied = snprintf(result, len, HAS_SESSION_ENV_PREFIX "%c", 1752 clp->cl_minorversion ? 'Y' : 'N'); 1753 if (copied >= len) { 1754 /* just return nothing if output was truncated */ 1755 kfree(result); 1756 return NULL; 1757 } 1758 1759 return result; 1760 } 1761 1762 static char * 1763 nfsd4_cltrack_grace_start(time64_t grace_start) 1764 { 1765 int copied; 1766 size_t len; 1767 char *result; 1768 1769 /* prefix + max width of int64_t string + terminating NULL */ 1770 len = strlen(GRACE_START_ENV_PREFIX) + 22 + 1; 1771 1772 result = kmalloc(len, GFP_KERNEL); 1773 if (!result) 1774 return result; 1775 1776 copied = snprintf(result, len, GRACE_START_ENV_PREFIX "%lld", 1777 grace_start); 1778 if (copied >= len) { 1779 /* just return nothing if output was truncated */ 1780 kfree(result); 1781 return NULL; 1782 } 1783 1784 return result; 1785 } 1786 1787 static int 1788 nfsd4_umh_cltrack_upcall(char *cmd, char *arg, char *env0, char *env1) 1789 { 1790 char *envp[3]; 1791 char *argv[4]; 1792 int ret; 1793 1794 if (unlikely(!cltrack_prog[0])) { 1795 dprintk("%s: cltrack_prog is disabled\n", __func__); 1796 return -EACCES; 1797 } 1798 1799 dprintk("%s: cmd: %s\n", __func__, cmd); 1800 dprintk("%s: arg: %s\n", __func__, arg ? arg : "(null)"); 1801 dprintk("%s: env0: %s\n", __func__, env0 ? env0 : "(null)"); 1802 dprintk("%s: env1: %s\n", __func__, env1 ? env1 : "(null)"); 1803 1804 envp[0] = env0; 1805 envp[1] = env1; 1806 envp[2] = NULL; 1807 1808 argv[0] = (char *)cltrack_prog; 1809 argv[1] = cmd; 1810 argv[2] = arg; 1811 argv[3] = NULL; 1812 1813 ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC); 1814 /* 1815 * Disable the upcall mechanism if we're getting an ENOENT or EACCES 1816 * error. The admin can re-enable it on the fly by using sysfs 1817 * once the problem has been fixed. 1818 */ 1819 if (ret == -ENOENT || ret == -EACCES) { 1820 dprintk("NFSD: %s was not found or isn't executable (%d). " 1821 "Setting cltrack_prog to blank string!", 1822 cltrack_prog, ret); 1823 cltrack_prog[0] = '\0'; 1824 } 1825 dprintk("%s: %s return value: %d\n", __func__, cltrack_prog, ret); 1826 1827 return ret; 1828 } 1829 1830 static char * 1831 bin_to_hex_dup(const unsigned char *src, int srclen) 1832 { 1833 char *buf; 1834 1835 /* +1 for terminating NULL */ 1836 buf = kzalloc((srclen * 2) + 1, GFP_KERNEL); 1837 if (!buf) 1838 return buf; 1839 1840 bin2hex(buf, src, srclen); 1841 return buf; 1842 } 1843 1844 static int 1845 nfsd4_umh_cltrack_init(struct net *net) 1846 { 1847 int ret; 1848 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 1849 char *grace_start = nfsd4_cltrack_grace_start(nn->boot_time); 1850 1851 /* XXX: The usermode helper s not working in container yet. */ 1852 if (net != &init_net) { 1853 pr_warn("NFSD: attempt to initialize umh client tracking in a container ignored.\n"); 1854 kfree(grace_start); 1855 return -EINVAL; 1856 } 1857 1858 ret = nfsd4_umh_cltrack_upcall("init", NULL, grace_start, NULL); 1859 kfree(grace_start); 1860 if (!ret) 1861 pr_info("NFSD: Using UMH upcall client tracking operations.\n"); 1862 return ret; 1863 } 1864 1865 static void 1866 nfsd4_cltrack_upcall_lock(struct nfs4_client *clp) 1867 { 1868 wait_on_bit_lock(&clp->cl_flags, NFSD4_CLIENT_UPCALL_LOCK, 1869 TASK_UNINTERRUPTIBLE); 1870 } 1871 1872 static void 1873 nfsd4_cltrack_upcall_unlock(struct nfs4_client *clp) 1874 { 1875 clear_and_wake_up_bit(NFSD4_CLIENT_UPCALL_LOCK, &clp->cl_flags); 1876 } 1877 1878 static void 1879 nfsd4_umh_cltrack_create(struct nfs4_client *clp) 1880 { 1881 char *hexid, *has_session, *grace_start; 1882 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 1883 1884 /* 1885 * With v4.0 clients, there's little difference in outcome between a 1886 * create and check operation, and we can end up calling into this 1887 * function multiple times per client (once for each openowner). So, 1888 * for v4.0 clients skip upcalling once the client has been recorded 1889 * on stable storage. 1890 * 1891 * For v4.1+ clients, the outcome of the two operations is different, 1892 * so we must ensure that we upcall for the create operation. v4.1+ 1893 * clients call this on RECLAIM_COMPLETE though, so we should only end 1894 * up doing a single create upcall per client. 1895 */ 1896 if (clp->cl_minorversion == 0 && 1897 test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags)) 1898 return; 1899 1900 hexid = bin_to_hex_dup(clp->cl_name.data, clp->cl_name.len); 1901 if (!hexid) { 1902 dprintk("%s: can't allocate memory for upcall!\n", __func__); 1903 return; 1904 } 1905 1906 has_session = nfsd4_cltrack_client_has_session(clp); 1907 grace_start = nfsd4_cltrack_grace_start(nn->boot_time); 1908 1909 nfsd4_cltrack_upcall_lock(clp); 1910 if (!nfsd4_umh_cltrack_upcall("create", hexid, has_session, grace_start)) 1911 set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags); 1912 nfsd4_cltrack_upcall_unlock(clp); 1913 1914 kfree(has_session); 1915 kfree(grace_start); 1916 kfree(hexid); 1917 } 1918 1919 static void 1920 nfsd4_umh_cltrack_remove(struct nfs4_client *clp) 1921 { 1922 char *hexid; 1923 1924 if (!test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags)) 1925 return; 1926 1927 hexid = bin_to_hex_dup(clp->cl_name.data, clp->cl_name.len); 1928 if (!hexid) { 1929 dprintk("%s: can't allocate memory for upcall!\n", __func__); 1930 return; 1931 } 1932 1933 nfsd4_cltrack_upcall_lock(clp); 1934 if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags) && 1935 nfsd4_umh_cltrack_upcall("remove", hexid, NULL, NULL) == 0) 1936 clear_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags); 1937 nfsd4_cltrack_upcall_unlock(clp); 1938 1939 kfree(hexid); 1940 } 1941 1942 static int 1943 nfsd4_umh_cltrack_check(struct nfs4_client *clp) 1944 { 1945 int ret; 1946 char *hexid, *has_session, *legacy; 1947 1948 if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags)) 1949 return 0; 1950 1951 hexid = bin_to_hex_dup(clp->cl_name.data, clp->cl_name.len); 1952 if (!hexid) { 1953 dprintk("%s: can't allocate memory for upcall!\n", __func__); 1954 return -ENOMEM; 1955 } 1956 1957 has_session = nfsd4_cltrack_client_has_session(clp); 1958 legacy = nfsd4_cltrack_legacy_recdir(&clp->cl_name); 1959 1960 nfsd4_cltrack_upcall_lock(clp); 1961 if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags)) { 1962 ret = 0; 1963 } else { 1964 ret = nfsd4_umh_cltrack_upcall("check", hexid, has_session, legacy); 1965 if (ret == 0) 1966 set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags); 1967 } 1968 nfsd4_cltrack_upcall_unlock(clp); 1969 kfree(has_session); 1970 kfree(legacy); 1971 kfree(hexid); 1972 1973 return ret; 1974 } 1975 1976 static void 1977 nfsd4_umh_cltrack_grace_done(struct nfsd_net *nn) 1978 { 1979 char *legacy; 1980 char timestr[22]; /* FIXME: better way to determine max size? */ 1981 1982 sprintf(timestr, "%lld", nn->boot_time); 1983 legacy = nfsd4_cltrack_legacy_topdir(); 1984 nfsd4_umh_cltrack_upcall("gracedone", timestr, legacy, NULL); 1985 kfree(legacy); 1986 } 1987 1988 static const struct nfsd4_client_tracking_ops nfsd4_umh_tracking_ops = { 1989 .init = nfsd4_umh_cltrack_init, 1990 .exit = NULL, 1991 .create = nfsd4_umh_cltrack_create, 1992 .remove = nfsd4_umh_cltrack_remove, 1993 .check = nfsd4_umh_cltrack_check, 1994 .grace_done = nfsd4_umh_cltrack_grace_done, 1995 .version = 1, 1996 .msglen = 0, 1997 }; 1998 1999 static inline int check_for_legacy_methods(int status, struct net *net) 2000 { 2001 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 2002 struct path path; 2003 2004 /* 2005 * Next, try the UMH upcall. 2006 */ 2007 nn->client_tracking_ops = &nfsd4_umh_tracking_ops; 2008 status = nn->client_tracking_ops->init(net); 2009 if (!status) 2010 return status; 2011 2012 /* 2013 * Finally, See if the recoverydir exists and is a directory. 2014 * If it is, then use the legacy ops. 2015 */ 2016 nn->client_tracking_ops = &nfsd4_legacy_tracking_ops; 2017 status = kern_path(nfs4_recoverydir(), LOOKUP_FOLLOW, &path); 2018 if (!status) { 2019 status = !d_is_dir(path.dentry); 2020 path_put(&path); 2021 if (status) 2022 return -ENOTDIR; 2023 } 2024 return status; 2025 } 2026 #else 2027 static inline int check_for_legacy_methods(int status, struct net *net) 2028 { 2029 return status; 2030 } 2031 #endif /* CONFIG_LEGACY_NFSD_CLIENT_TRACKING */ 2032 2033 int 2034 nfsd4_client_tracking_init(struct net *net) 2035 { 2036 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 2037 int status; 2038 2039 /* just run the init if it the method is already decided */ 2040 if (nn->client_tracking_ops) 2041 goto do_init; 2042 2043 /* First, try to use nfsdcld */ 2044 nn->client_tracking_ops = &nfsd4_cld_tracking_ops; 2045 status = nn->client_tracking_ops->init(net); 2046 if (!status) 2047 return status; 2048 if (status != -ETIMEDOUT) { 2049 nn->client_tracking_ops = &nfsd4_cld_tracking_ops_v0; 2050 status = nn->client_tracking_ops->init(net); 2051 if (!status) 2052 return status; 2053 } 2054 2055 status = check_for_legacy_methods(status, net); 2056 if (status) 2057 goto out; 2058 do_init: 2059 status = nn->client_tracking_ops->init(net); 2060 out: 2061 if (status) { 2062 pr_warn("NFSD: Unable to initialize client recovery tracking! (%d)\n", status); 2063 pr_warn("NFSD: Is nfsdcld running? If not, enable CONFIG_NFSD_LEGACY_CLIENT_TRACKING.\n"); 2064 nn->client_tracking_ops = NULL; 2065 } 2066 return status; 2067 } 2068 2069 void 2070 nfsd4_client_tracking_exit(struct net *net) 2071 { 2072 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 2073 2074 if (nn->client_tracking_ops) { 2075 if (nn->client_tracking_ops->exit) 2076 nn->client_tracking_ops->exit(net); 2077 nn->client_tracking_ops = NULL; 2078 } 2079 } 2080 2081 void 2082 nfsd4_client_record_create(struct nfs4_client *clp) 2083 { 2084 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 2085 2086 if (nn->client_tracking_ops) 2087 nn->client_tracking_ops->create(clp); 2088 } 2089 2090 void 2091 nfsd4_client_record_remove(struct nfs4_client *clp) 2092 { 2093 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 2094 2095 if (nn->client_tracking_ops) 2096 nn->client_tracking_ops->remove(clp); 2097 } 2098 2099 int 2100 nfsd4_client_record_check(struct nfs4_client *clp) 2101 { 2102 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 2103 2104 if (nn->client_tracking_ops) 2105 return nn->client_tracking_ops->check(clp); 2106 2107 return -EOPNOTSUPP; 2108 } 2109 2110 void 2111 nfsd4_record_grace_done(struct nfsd_net *nn) 2112 { 2113 if (nn->client_tracking_ops) 2114 nn->client_tracking_ops->grace_done(nn); 2115 } 2116 2117 static int 2118 rpc_pipefs_event(struct notifier_block *nb, unsigned long event, void *ptr) 2119 { 2120 struct super_block *sb = ptr; 2121 struct net *net = sb->s_fs_info; 2122 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 2123 struct cld_net *cn = nn->cld_net; 2124 struct dentry *dentry; 2125 int ret = 0; 2126 2127 if (!try_module_get(THIS_MODULE)) 2128 return 0; 2129 2130 if (!cn) { 2131 module_put(THIS_MODULE); 2132 return 0; 2133 } 2134 2135 switch (event) { 2136 case RPC_PIPEFS_MOUNT: 2137 dentry = nfsd4_cld_register_sb(sb, cn->cn_pipe); 2138 if (IS_ERR(dentry)) { 2139 ret = PTR_ERR(dentry); 2140 break; 2141 } 2142 cn->cn_pipe->dentry = dentry; 2143 break; 2144 case RPC_PIPEFS_UMOUNT: 2145 if (cn->cn_pipe->dentry) 2146 nfsd4_cld_unregister_sb(cn->cn_pipe); 2147 break; 2148 default: 2149 ret = -ENOTSUPP; 2150 break; 2151 } 2152 module_put(THIS_MODULE); 2153 return ret; 2154 } 2155 2156 static struct notifier_block nfsd4_cld_block = { 2157 .notifier_call = rpc_pipefs_event, 2158 }; 2159 2160 int 2161 register_cld_notifier(void) 2162 { 2163 WARN_ON(!nfsd_net_id); 2164 return rpc_pipefs_notifier_register(&nfsd4_cld_block); 2165 } 2166 2167 void 2168 unregister_cld_notifier(void) 2169 { 2170 rpc_pipefs_notifier_unregister(&nfsd4_cld_block); 2171 } 2172