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