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 int 954 nfsd4_cld_register_sb(struct super_block *sb, struct rpc_pipe *pipe) 955 { 956 struct dentry *dir; 957 int err; 958 959 dir = rpc_d_lookup_sb(sb, NFSD_PIPE_DIR); 960 if (dir == NULL) 961 return -ENOENT; 962 err = rpc_mkpipe_dentry(dir, NFSD_CLD_PIPE, NULL, pipe); 963 dput(dir); 964 return err; 965 } 966 967 static int 968 nfsd4_cld_register_net(struct net *net, struct rpc_pipe *pipe) 969 { 970 struct super_block *sb; 971 int err; 972 973 sb = rpc_get_sb_net(net); 974 if (!sb) 975 return 0; 976 err = nfsd4_cld_register_sb(sb, pipe); 977 rpc_put_sb_net(net); 978 return err; 979 } 980 981 static void 982 nfsd4_cld_unregister_net(struct net *net, struct rpc_pipe *pipe) 983 { 984 struct super_block *sb; 985 986 sb = rpc_get_sb_net(net); 987 if (sb) { 988 rpc_unlink(pipe); 989 rpc_put_sb_net(net); 990 } 991 } 992 993 /* Initialize rpc_pipefs pipe for communication with client tracking daemon */ 994 static int 995 __nfsd4_init_cld_pipe(struct net *net) 996 { 997 int ret; 998 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 999 struct cld_net *cn; 1000 1001 if (nn->cld_net) 1002 return 0; 1003 1004 cn = kzalloc(sizeof(*cn), GFP_KERNEL); 1005 if (!cn) { 1006 ret = -ENOMEM; 1007 goto err; 1008 } 1009 1010 cn->cn_pipe = rpc_mkpipe_data(&cld_upcall_ops, RPC_PIPE_WAIT_FOR_OPEN); 1011 if (IS_ERR(cn->cn_pipe)) { 1012 ret = PTR_ERR(cn->cn_pipe); 1013 goto err; 1014 } 1015 spin_lock_init(&cn->cn_lock); 1016 INIT_LIST_HEAD(&cn->cn_list); 1017 1018 ret = nfsd4_cld_register_net(net, cn->cn_pipe); 1019 if (unlikely(ret)) 1020 goto err_destroy_data; 1021 1022 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING 1023 cn->cn_has_legacy = false; 1024 #endif 1025 nn->cld_net = cn; 1026 return 0; 1027 1028 err_destroy_data: 1029 rpc_destroy_pipe_data(cn->cn_pipe); 1030 err: 1031 kfree(cn); 1032 printk(KERN_ERR "NFSD: unable to create nfsdcld upcall pipe (%d)\n", 1033 ret); 1034 return ret; 1035 } 1036 1037 static int 1038 nfsd4_init_cld_pipe(struct net *net) 1039 { 1040 int status; 1041 1042 status = __nfsd4_init_cld_pipe(net); 1043 if (!status) 1044 pr_info("NFSD: Using old nfsdcld client tracking operations.\n"); 1045 return status; 1046 } 1047 1048 static void 1049 nfsd4_remove_cld_pipe(struct net *net) 1050 { 1051 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 1052 struct cld_net *cn = nn->cld_net; 1053 1054 nfsd4_cld_unregister_net(net, cn->cn_pipe); 1055 rpc_destroy_pipe_data(cn->cn_pipe); 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 char *principal = NULL; 1150 1151 /* Don't upcall if it's already stored */ 1152 if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags)) 1153 return; 1154 1155 cup = alloc_cld_upcall(nn); 1156 if (!cup) { 1157 ret = -ENOMEM; 1158 goto out_err; 1159 } 1160 1161 cmsg = &cup->cu_u.cu_msg_v2; 1162 cmsg->cm_cmd = Cld_Create; 1163 cmsg->cm_u.cm_clntinfo.cc_name.cn_len = clp->cl_name.len; 1164 memcpy(cmsg->cm_u.cm_clntinfo.cc_name.cn_id, clp->cl_name.data, 1165 clp->cl_name.len); 1166 if (clp->cl_cred.cr_raw_principal) 1167 principal = clp->cl_cred.cr_raw_principal; 1168 else if (clp->cl_cred.cr_principal) 1169 principal = clp->cl_cred.cr_principal; 1170 if (principal) { 1171 sha256(principal, strlen(principal), 1172 cmsg->cm_u.cm_clntinfo.cc_princhash.cp_data); 1173 cmsg->cm_u.cm_clntinfo.cc_princhash.cp_len = SHA256_DIGEST_SIZE; 1174 } else 1175 cmsg->cm_u.cm_clntinfo.cc_princhash.cp_len = 0; 1176 1177 ret = cld_pipe_upcall(cn->cn_pipe, cmsg, nn); 1178 if (!ret) { 1179 ret = cmsg->cm_status; 1180 set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags); 1181 } 1182 1183 free_cld_upcall(cup); 1184 out_err: 1185 if (ret) 1186 pr_err("NFSD: Unable to create client record on stable storage: %d\n", 1187 ret); 1188 } 1189 1190 /* Ask daemon to create a new record */ 1191 static void 1192 nfsd4_cld_remove(struct nfs4_client *clp) 1193 { 1194 int ret; 1195 struct cld_upcall *cup; 1196 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 1197 struct cld_net *cn = nn->cld_net; 1198 1199 /* Don't upcall if it's already removed */ 1200 if (!test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags)) 1201 return; 1202 1203 cup = alloc_cld_upcall(nn); 1204 if (!cup) { 1205 ret = -ENOMEM; 1206 goto out_err; 1207 } 1208 1209 cup->cu_u.cu_msg.cm_cmd = Cld_Remove; 1210 cup->cu_u.cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len; 1211 memcpy(cup->cu_u.cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data, 1212 clp->cl_name.len); 1213 1214 ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_u.cu_msg, nn); 1215 if (!ret) { 1216 ret = cup->cu_u.cu_msg.cm_status; 1217 clear_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags); 1218 } 1219 1220 free_cld_upcall(cup); 1221 out_err: 1222 if (ret) 1223 printk(KERN_ERR "NFSD: Unable to remove client " 1224 "record from stable storage: %d\n", ret); 1225 } 1226 1227 /* 1228 * For older nfsdcld's that do not allow us to "slurp" the clients 1229 * from the tracking database during startup. 1230 * 1231 * Check for presence of a record, and update its timestamp 1232 */ 1233 static int 1234 nfsd4_cld_check_v0(struct nfs4_client *clp) 1235 { 1236 int ret; 1237 struct cld_upcall *cup; 1238 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 1239 struct cld_net *cn = nn->cld_net; 1240 1241 /* Don't upcall if one was already stored during this grace pd */ 1242 if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags)) 1243 return 0; 1244 1245 cup = alloc_cld_upcall(nn); 1246 if (!cup) { 1247 printk(KERN_ERR "NFSD: Unable to check client record on " 1248 "stable storage: %d\n", -ENOMEM); 1249 return -ENOMEM; 1250 } 1251 1252 cup->cu_u.cu_msg.cm_cmd = Cld_Check; 1253 cup->cu_u.cu_msg.cm_u.cm_name.cn_len = clp->cl_name.len; 1254 memcpy(cup->cu_u.cu_msg.cm_u.cm_name.cn_id, clp->cl_name.data, 1255 clp->cl_name.len); 1256 1257 ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_u.cu_msg, nn); 1258 if (!ret) { 1259 ret = cup->cu_u.cu_msg.cm_status; 1260 set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags); 1261 } 1262 1263 free_cld_upcall(cup); 1264 return ret; 1265 } 1266 1267 /* 1268 * For newer nfsdcld's that allow us to "slurp" the clients 1269 * from the tracking database during startup. 1270 * 1271 * Check for presence of a record in the reclaim_str_hashtbl 1272 */ 1273 static int 1274 nfsd4_cld_check(struct nfs4_client *clp) 1275 { 1276 struct nfs4_client_reclaim *crp; 1277 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 1278 1279 /* did we already find that this client is stable? */ 1280 if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags)) 1281 return 0; 1282 1283 /* look for it in the reclaim hashtable otherwise */ 1284 crp = nfsd4_find_reclaim_client(clp->cl_name, nn); 1285 if (crp) 1286 goto found; 1287 1288 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING 1289 if (nn->cld_net->cn_has_legacy) { 1290 int status; 1291 char dname[HEXDIR_LEN]; 1292 struct xdr_netobj name; 1293 1294 status = nfs4_make_rec_clidname(dname, &clp->cl_name); 1295 if (status) 1296 return -ENOENT; 1297 1298 name.data = kmemdup(dname, HEXDIR_LEN, GFP_KERNEL); 1299 if (!name.data) { 1300 dprintk("%s: failed to allocate memory for name.data!\n", 1301 __func__); 1302 return -ENOENT; 1303 } 1304 name.len = HEXDIR_LEN; 1305 crp = nfsd4_find_reclaim_client(name, nn); 1306 kfree(name.data); 1307 if (crp) 1308 goto found; 1309 1310 } 1311 #endif 1312 return -ENOENT; 1313 found: 1314 crp->cr_clp = clp; 1315 return 0; 1316 } 1317 1318 static int 1319 nfsd4_cld_check_v2(struct nfs4_client *clp) 1320 { 1321 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 1322 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING 1323 struct cld_net *cn = nn->cld_net; 1324 #endif 1325 struct nfs4_client_reclaim *crp; 1326 char *principal = NULL; 1327 1328 /* did we already find that this client is stable? */ 1329 if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags)) 1330 return 0; 1331 1332 /* look for it in the reclaim hashtable otherwise */ 1333 crp = nfsd4_find_reclaim_client(clp->cl_name, nn); 1334 if (crp) 1335 goto found; 1336 1337 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING 1338 if (cn->cn_has_legacy) { 1339 struct xdr_netobj name; 1340 char dname[HEXDIR_LEN]; 1341 int status; 1342 1343 status = nfs4_make_rec_clidname(dname, &clp->cl_name); 1344 if (status) 1345 return -ENOENT; 1346 1347 name.data = kmemdup(dname, HEXDIR_LEN, GFP_KERNEL); 1348 if (!name.data) { 1349 dprintk("%s: failed to allocate memory for name.data\n", 1350 __func__); 1351 return -ENOENT; 1352 } 1353 name.len = HEXDIR_LEN; 1354 crp = nfsd4_find_reclaim_client(name, nn); 1355 kfree(name.data); 1356 if (crp) 1357 goto found; 1358 1359 } 1360 #endif 1361 return -ENOENT; 1362 found: 1363 if (crp->cr_princhash.len) { 1364 u8 digest[SHA256_DIGEST_SIZE]; 1365 1366 if (clp->cl_cred.cr_raw_principal) 1367 principal = clp->cl_cred.cr_raw_principal; 1368 else if (clp->cl_cred.cr_principal) 1369 principal = clp->cl_cred.cr_principal; 1370 if (principal == NULL) 1371 return -ENOENT; 1372 sha256(principal, strlen(principal), digest); 1373 if (memcmp(crp->cr_princhash.data, digest, 1374 crp->cr_princhash.len)) 1375 return -ENOENT; 1376 } 1377 crp->cr_clp = clp; 1378 return 0; 1379 } 1380 1381 static int 1382 nfsd4_cld_grace_start(struct nfsd_net *nn) 1383 { 1384 int ret; 1385 struct cld_upcall *cup; 1386 struct cld_net *cn = nn->cld_net; 1387 1388 cup = alloc_cld_upcall(nn); 1389 if (!cup) { 1390 ret = -ENOMEM; 1391 goto out_err; 1392 } 1393 1394 cup->cu_u.cu_msg.cm_cmd = Cld_GraceStart; 1395 ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_u.cu_msg, nn); 1396 if (!ret) 1397 ret = cup->cu_u.cu_msg.cm_status; 1398 1399 free_cld_upcall(cup); 1400 out_err: 1401 if (ret) 1402 dprintk("%s: Unable to get clients from userspace: %d\n", 1403 __func__, ret); 1404 return ret; 1405 } 1406 1407 /* For older nfsdcld's that need cm_gracetime */ 1408 static void 1409 nfsd4_cld_grace_done_v0(struct nfsd_net *nn) 1410 { 1411 int ret; 1412 struct cld_upcall *cup; 1413 struct cld_net *cn = nn->cld_net; 1414 1415 cup = alloc_cld_upcall(nn); 1416 if (!cup) { 1417 ret = -ENOMEM; 1418 goto out_err; 1419 } 1420 1421 cup->cu_u.cu_msg.cm_cmd = Cld_GraceDone; 1422 cup->cu_u.cu_msg.cm_u.cm_gracetime = nn->boot_time; 1423 ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_u.cu_msg, nn); 1424 if (!ret) 1425 ret = cup->cu_u.cu_msg.cm_status; 1426 1427 free_cld_upcall(cup); 1428 out_err: 1429 if (ret) 1430 printk(KERN_ERR "NFSD: Unable to end grace period: %d\n", ret); 1431 } 1432 1433 /* 1434 * For newer nfsdcld's that do not need cm_gracetime. We also need to call 1435 * nfs4_release_reclaim() to clear out the reclaim_str_hashtbl. 1436 */ 1437 static void 1438 nfsd4_cld_grace_done(struct nfsd_net *nn) 1439 { 1440 int ret; 1441 struct cld_upcall *cup; 1442 struct cld_net *cn = nn->cld_net; 1443 1444 cup = alloc_cld_upcall(nn); 1445 if (!cup) { 1446 ret = -ENOMEM; 1447 goto out_err; 1448 } 1449 1450 cup->cu_u.cu_msg.cm_cmd = Cld_GraceDone; 1451 ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_u.cu_msg, nn); 1452 if (!ret) 1453 ret = cup->cu_u.cu_msg.cm_status; 1454 1455 free_cld_upcall(cup); 1456 out_err: 1457 nfs4_release_reclaim(nn); 1458 if (ret) 1459 printk(KERN_ERR "NFSD: Unable to end grace period: %d\n", ret); 1460 } 1461 1462 static int 1463 nfs4_cld_state_init(struct net *net) 1464 { 1465 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 1466 int i; 1467 1468 nn->reclaim_str_hashtbl = kmalloc_array(CLIENT_HASH_SIZE, 1469 sizeof(struct list_head), 1470 GFP_KERNEL); 1471 if (!nn->reclaim_str_hashtbl) 1472 return -ENOMEM; 1473 1474 for (i = 0; i < CLIENT_HASH_SIZE; i++) 1475 INIT_LIST_HEAD(&nn->reclaim_str_hashtbl[i]); 1476 nn->reclaim_str_hashtbl_size = 0; 1477 nn->track_reclaim_completes = true; 1478 atomic_set(&nn->nr_reclaim_complete, 0); 1479 1480 return 0; 1481 } 1482 1483 static void 1484 nfs4_cld_state_shutdown(struct net *net) 1485 { 1486 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 1487 1488 nn->track_reclaim_completes = false; 1489 kfree(nn->reclaim_str_hashtbl); 1490 } 1491 1492 static bool 1493 cld_running(struct nfsd_net *nn) 1494 { 1495 struct cld_net *cn = nn->cld_net; 1496 struct rpc_pipe *pipe = cn->cn_pipe; 1497 1498 return pipe->nreaders || pipe->nwriters; 1499 } 1500 1501 static int 1502 nfsd4_cld_get_version(struct nfsd_net *nn) 1503 { 1504 int ret = 0; 1505 struct cld_upcall *cup; 1506 struct cld_net *cn = nn->cld_net; 1507 uint8_t version; 1508 1509 cup = alloc_cld_upcall(nn); 1510 if (!cup) { 1511 ret = -ENOMEM; 1512 goto out_err; 1513 } 1514 cup->cu_u.cu_msg.cm_cmd = Cld_GetVersion; 1515 ret = cld_pipe_upcall(cn->cn_pipe, &cup->cu_u.cu_msg, nn); 1516 if (!ret) { 1517 ret = cup->cu_u.cu_msg.cm_status; 1518 if (ret) 1519 goto out_free; 1520 version = cup->cu_u.cu_msg.cm_u.cm_version; 1521 dprintk("%s: userspace returned version %u\n", 1522 __func__, version); 1523 if (version < 1) 1524 version = 1; 1525 else if (version > CLD_UPCALL_VERSION) 1526 version = CLD_UPCALL_VERSION; 1527 1528 switch (version) { 1529 case 1: 1530 nn->client_tracking_ops = &nfsd4_cld_tracking_ops; 1531 break; 1532 case 2: 1533 nn->client_tracking_ops = &nfsd4_cld_tracking_ops_v2; 1534 break; 1535 default: 1536 break; 1537 } 1538 } 1539 out_free: 1540 free_cld_upcall(cup); 1541 out_err: 1542 if (ret) 1543 dprintk("%s: Unable to get version from userspace: %d\n", 1544 __func__, ret); 1545 return ret; 1546 } 1547 1548 static int 1549 nfsd4_cld_tracking_init(struct net *net) 1550 { 1551 int status; 1552 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 1553 bool running; 1554 int retries = 10; 1555 1556 status = nfs4_cld_state_init(net); 1557 if (status) 1558 return status; 1559 1560 status = __nfsd4_init_cld_pipe(net); 1561 if (status) 1562 goto err_shutdown; 1563 1564 /* 1565 * rpc pipe upcalls take 30 seconds to time out, so we don't want to 1566 * queue an upcall unless we know that nfsdcld is running (because we 1567 * want this to fail fast so that nfsd4_client_tracking_init() can try 1568 * the next client tracking method). nfsdcld should already be running 1569 * before nfsd is started, so the wait here is for nfsdcld to open the 1570 * pipefs file we just created. 1571 */ 1572 while (!(running = cld_running(nn)) && retries--) 1573 msleep(100); 1574 1575 if (!running) { 1576 status = -ETIMEDOUT; 1577 goto err_remove; 1578 } 1579 1580 status = nfsd4_cld_get_version(nn); 1581 if (status == -EOPNOTSUPP) 1582 pr_warn("NFSD: nfsdcld GetVersion upcall failed. Please upgrade nfsdcld.\n"); 1583 1584 status = nfsd4_cld_grace_start(nn); 1585 if (status) { 1586 if (status == -EOPNOTSUPP) 1587 pr_warn("NFSD: nfsdcld GraceStart upcall failed. Please upgrade nfsdcld.\n"); 1588 nfs4_release_reclaim(nn); 1589 goto err_remove; 1590 } else 1591 pr_info("NFSD: Using nfsdcld client tracking operations.\n"); 1592 return 0; 1593 1594 err_remove: 1595 nfsd4_remove_cld_pipe(net); 1596 err_shutdown: 1597 nfs4_cld_state_shutdown(net); 1598 return status; 1599 } 1600 1601 static void 1602 nfsd4_cld_tracking_exit(struct net *net) 1603 { 1604 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 1605 1606 nfs4_release_reclaim(nn); 1607 nfsd4_remove_cld_pipe(net); 1608 nfs4_cld_state_shutdown(net); 1609 } 1610 1611 /* For older nfsdcld's */ 1612 static const struct nfsd4_client_tracking_ops nfsd4_cld_tracking_ops_v0 = { 1613 .init = nfsd4_init_cld_pipe, 1614 .exit = nfsd4_remove_cld_pipe, 1615 .create = nfsd4_cld_create, 1616 .remove = nfsd4_cld_remove, 1617 .check = nfsd4_cld_check_v0, 1618 .grace_done = nfsd4_cld_grace_done_v0, 1619 .version = 1, 1620 .msglen = sizeof(struct cld_msg), 1621 }; 1622 1623 /* For newer nfsdcld's */ 1624 static const struct nfsd4_client_tracking_ops nfsd4_cld_tracking_ops = { 1625 .init = nfsd4_cld_tracking_init, 1626 .exit = nfsd4_cld_tracking_exit, 1627 .create = nfsd4_cld_create, 1628 .remove = nfsd4_cld_remove, 1629 .check = nfsd4_cld_check, 1630 .grace_done = nfsd4_cld_grace_done, 1631 .version = 1, 1632 .msglen = sizeof(struct cld_msg), 1633 }; 1634 1635 /* v2 create/check ops include the principal, if available */ 1636 static const struct nfsd4_client_tracking_ops nfsd4_cld_tracking_ops_v2 = { 1637 .init = nfsd4_cld_tracking_init, 1638 .exit = nfsd4_cld_tracking_exit, 1639 .create = nfsd4_cld_create_v2, 1640 .remove = nfsd4_cld_remove, 1641 .check = nfsd4_cld_check_v2, 1642 .grace_done = nfsd4_cld_grace_done, 1643 .version = 2, 1644 .msglen = sizeof(struct cld_msg_v2), 1645 }; 1646 1647 #ifdef CONFIG_NFSD_LEGACY_CLIENT_TRACKING 1648 /* upcall via usermodehelper */ 1649 static char cltrack_prog[PATH_MAX] = "/sbin/nfsdcltrack"; 1650 module_param_string(cltrack_prog, cltrack_prog, sizeof(cltrack_prog), 1651 S_IRUGO|S_IWUSR); 1652 MODULE_PARM_DESC(cltrack_prog, "Path to the nfsdcltrack upcall program"); 1653 1654 static bool cltrack_legacy_disable; 1655 module_param(cltrack_legacy_disable, bool, S_IRUGO|S_IWUSR); 1656 MODULE_PARM_DESC(cltrack_legacy_disable, 1657 "Disable legacy recoverydir conversion. Default: false"); 1658 1659 #define LEGACY_TOPDIR_ENV_PREFIX "NFSDCLTRACK_LEGACY_TOPDIR=" 1660 #define LEGACY_RECDIR_ENV_PREFIX "NFSDCLTRACK_LEGACY_RECDIR=" 1661 #define HAS_SESSION_ENV_PREFIX "NFSDCLTRACK_CLIENT_HAS_SESSION=" 1662 #define GRACE_START_ENV_PREFIX "NFSDCLTRACK_GRACE_START=" 1663 1664 static char * 1665 nfsd4_cltrack_legacy_topdir(void) 1666 { 1667 int copied; 1668 size_t len; 1669 char *result; 1670 1671 if (cltrack_legacy_disable) 1672 return NULL; 1673 1674 len = strlen(LEGACY_TOPDIR_ENV_PREFIX) + 1675 strlen(nfs4_recoverydir()) + 1; 1676 1677 result = kmalloc(len, GFP_KERNEL); 1678 if (!result) 1679 return result; 1680 1681 copied = snprintf(result, len, LEGACY_TOPDIR_ENV_PREFIX "%s", 1682 nfs4_recoverydir()); 1683 if (copied >= len) { 1684 /* just return nothing if output was truncated */ 1685 kfree(result); 1686 return NULL; 1687 } 1688 1689 return result; 1690 } 1691 1692 static char * 1693 nfsd4_cltrack_legacy_recdir(const struct xdr_netobj *name) 1694 { 1695 int copied; 1696 size_t len; 1697 char *result; 1698 1699 if (cltrack_legacy_disable) 1700 return NULL; 1701 1702 /* +1 is for '/' between "topdir" and "recdir" */ 1703 len = strlen(LEGACY_RECDIR_ENV_PREFIX) + 1704 strlen(nfs4_recoverydir()) + 1 + HEXDIR_LEN; 1705 1706 result = kmalloc(len, GFP_KERNEL); 1707 if (!result) 1708 return result; 1709 1710 copied = snprintf(result, len, LEGACY_RECDIR_ENV_PREFIX "%s/", 1711 nfs4_recoverydir()); 1712 if (copied > (len - HEXDIR_LEN)) { 1713 /* just return nothing if output will be truncated */ 1714 kfree(result); 1715 return NULL; 1716 } 1717 1718 copied = nfs4_make_rec_clidname(result + copied, name); 1719 if (copied) { 1720 kfree(result); 1721 return NULL; 1722 } 1723 1724 return result; 1725 } 1726 1727 static char * 1728 nfsd4_cltrack_client_has_session(struct nfs4_client *clp) 1729 { 1730 int copied; 1731 size_t len; 1732 char *result; 1733 1734 /* prefix + Y/N character + terminating NULL */ 1735 len = strlen(HAS_SESSION_ENV_PREFIX) + 1 + 1; 1736 1737 result = kmalloc(len, GFP_KERNEL); 1738 if (!result) 1739 return result; 1740 1741 copied = snprintf(result, len, HAS_SESSION_ENV_PREFIX "%c", 1742 clp->cl_minorversion ? 'Y' : 'N'); 1743 if (copied >= len) { 1744 /* just return nothing if output was truncated */ 1745 kfree(result); 1746 return NULL; 1747 } 1748 1749 return result; 1750 } 1751 1752 static char * 1753 nfsd4_cltrack_grace_start(time64_t grace_start) 1754 { 1755 int copied; 1756 size_t len; 1757 char *result; 1758 1759 /* prefix + max width of int64_t string + terminating NULL */ 1760 len = strlen(GRACE_START_ENV_PREFIX) + 22 + 1; 1761 1762 result = kmalloc(len, GFP_KERNEL); 1763 if (!result) 1764 return result; 1765 1766 copied = snprintf(result, len, GRACE_START_ENV_PREFIX "%lld", 1767 grace_start); 1768 if (copied >= len) { 1769 /* just return nothing if output was truncated */ 1770 kfree(result); 1771 return NULL; 1772 } 1773 1774 return result; 1775 } 1776 1777 static int 1778 nfsd4_umh_cltrack_upcall(char *cmd, char *arg, char *env0, char *env1) 1779 { 1780 char *envp[3]; 1781 char *argv[4]; 1782 int ret; 1783 1784 if (unlikely(!cltrack_prog[0])) { 1785 dprintk("%s: cltrack_prog is disabled\n", __func__); 1786 return -EACCES; 1787 } 1788 1789 dprintk("%s: cmd: %s\n", __func__, cmd); 1790 dprintk("%s: arg: %s\n", __func__, arg ? arg : "(null)"); 1791 dprintk("%s: env0: %s\n", __func__, env0 ? env0 : "(null)"); 1792 dprintk("%s: env1: %s\n", __func__, env1 ? env1 : "(null)"); 1793 1794 envp[0] = env0; 1795 envp[1] = env1; 1796 envp[2] = NULL; 1797 1798 argv[0] = (char *)cltrack_prog; 1799 argv[1] = cmd; 1800 argv[2] = arg; 1801 argv[3] = NULL; 1802 1803 ret = call_usermodehelper(argv[0], argv, envp, UMH_WAIT_PROC); 1804 /* 1805 * Disable the upcall mechanism if we're getting an ENOENT or EACCES 1806 * error. The admin can re-enable it on the fly by using sysfs 1807 * once the problem has been fixed. 1808 */ 1809 if (ret == -ENOENT || ret == -EACCES) { 1810 dprintk("NFSD: %s was not found or isn't executable (%d). " 1811 "Setting cltrack_prog to blank string!", 1812 cltrack_prog, ret); 1813 cltrack_prog[0] = '\0'; 1814 } 1815 dprintk("%s: %s return value: %d\n", __func__, cltrack_prog, ret); 1816 1817 return ret; 1818 } 1819 1820 static char * 1821 bin_to_hex_dup(const unsigned char *src, int srclen) 1822 { 1823 char *buf; 1824 1825 /* +1 for terminating NULL */ 1826 buf = kzalloc((srclen * 2) + 1, GFP_KERNEL); 1827 if (!buf) 1828 return buf; 1829 1830 bin2hex(buf, src, srclen); 1831 return buf; 1832 } 1833 1834 static int 1835 nfsd4_umh_cltrack_init(struct net *net) 1836 { 1837 int ret; 1838 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 1839 char *grace_start = nfsd4_cltrack_grace_start(nn->boot_time); 1840 1841 /* XXX: The usermode helper s not working in container yet. */ 1842 if (net != &init_net) { 1843 pr_warn("NFSD: attempt to initialize umh client tracking in a container ignored.\n"); 1844 kfree(grace_start); 1845 return -EINVAL; 1846 } 1847 1848 ret = nfsd4_umh_cltrack_upcall("init", NULL, grace_start, NULL); 1849 kfree(grace_start); 1850 if (!ret) 1851 pr_info("NFSD: Using UMH upcall client tracking operations.\n"); 1852 return ret; 1853 } 1854 1855 static void 1856 nfsd4_cltrack_upcall_lock(struct nfs4_client *clp) 1857 { 1858 wait_on_bit_lock(&clp->cl_flags, NFSD4_CLIENT_UPCALL_LOCK, 1859 TASK_UNINTERRUPTIBLE); 1860 } 1861 1862 static void 1863 nfsd4_cltrack_upcall_unlock(struct nfs4_client *clp) 1864 { 1865 clear_and_wake_up_bit(NFSD4_CLIENT_UPCALL_LOCK, &clp->cl_flags); 1866 } 1867 1868 static void 1869 nfsd4_umh_cltrack_create(struct nfs4_client *clp) 1870 { 1871 char *hexid, *has_session, *grace_start; 1872 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 1873 1874 /* 1875 * With v4.0 clients, there's little difference in outcome between a 1876 * create and check operation, and we can end up calling into this 1877 * function multiple times per client (once for each openowner). So, 1878 * for v4.0 clients skip upcalling once the client has been recorded 1879 * on stable storage. 1880 * 1881 * For v4.1+ clients, the outcome of the two operations is different, 1882 * so we must ensure that we upcall for the create operation. v4.1+ 1883 * clients call this on RECLAIM_COMPLETE though, so we should only end 1884 * up doing a single create upcall per client. 1885 */ 1886 if (clp->cl_minorversion == 0 && 1887 test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags)) 1888 return; 1889 1890 hexid = bin_to_hex_dup(clp->cl_name.data, clp->cl_name.len); 1891 if (!hexid) { 1892 dprintk("%s: can't allocate memory for upcall!\n", __func__); 1893 return; 1894 } 1895 1896 has_session = nfsd4_cltrack_client_has_session(clp); 1897 grace_start = nfsd4_cltrack_grace_start(nn->boot_time); 1898 1899 nfsd4_cltrack_upcall_lock(clp); 1900 if (!nfsd4_umh_cltrack_upcall("create", hexid, has_session, grace_start)) 1901 set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags); 1902 nfsd4_cltrack_upcall_unlock(clp); 1903 1904 kfree(has_session); 1905 kfree(grace_start); 1906 kfree(hexid); 1907 } 1908 1909 static void 1910 nfsd4_umh_cltrack_remove(struct nfs4_client *clp) 1911 { 1912 char *hexid; 1913 1914 if (!test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags)) 1915 return; 1916 1917 hexid = bin_to_hex_dup(clp->cl_name.data, clp->cl_name.len); 1918 if (!hexid) { 1919 dprintk("%s: can't allocate memory for upcall!\n", __func__); 1920 return; 1921 } 1922 1923 nfsd4_cltrack_upcall_lock(clp); 1924 if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags) && 1925 nfsd4_umh_cltrack_upcall("remove", hexid, NULL, NULL) == 0) 1926 clear_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags); 1927 nfsd4_cltrack_upcall_unlock(clp); 1928 1929 kfree(hexid); 1930 } 1931 1932 static int 1933 nfsd4_umh_cltrack_check(struct nfs4_client *clp) 1934 { 1935 int ret; 1936 char *hexid, *has_session, *legacy; 1937 1938 if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags)) 1939 return 0; 1940 1941 hexid = bin_to_hex_dup(clp->cl_name.data, clp->cl_name.len); 1942 if (!hexid) { 1943 dprintk("%s: can't allocate memory for upcall!\n", __func__); 1944 return -ENOMEM; 1945 } 1946 1947 has_session = nfsd4_cltrack_client_has_session(clp); 1948 legacy = nfsd4_cltrack_legacy_recdir(&clp->cl_name); 1949 1950 nfsd4_cltrack_upcall_lock(clp); 1951 if (test_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags)) { 1952 ret = 0; 1953 } else { 1954 ret = nfsd4_umh_cltrack_upcall("check", hexid, has_session, legacy); 1955 if (ret == 0) 1956 set_bit(NFSD4_CLIENT_STABLE, &clp->cl_flags); 1957 } 1958 nfsd4_cltrack_upcall_unlock(clp); 1959 kfree(has_session); 1960 kfree(legacy); 1961 kfree(hexid); 1962 1963 return ret; 1964 } 1965 1966 static void 1967 nfsd4_umh_cltrack_grace_done(struct nfsd_net *nn) 1968 { 1969 char *legacy; 1970 char timestr[22]; /* FIXME: better way to determine max size? */ 1971 1972 sprintf(timestr, "%lld", nn->boot_time); 1973 legacy = nfsd4_cltrack_legacy_topdir(); 1974 nfsd4_umh_cltrack_upcall("gracedone", timestr, legacy, NULL); 1975 kfree(legacy); 1976 } 1977 1978 static const struct nfsd4_client_tracking_ops nfsd4_umh_tracking_ops = { 1979 .init = nfsd4_umh_cltrack_init, 1980 .exit = NULL, 1981 .create = nfsd4_umh_cltrack_create, 1982 .remove = nfsd4_umh_cltrack_remove, 1983 .check = nfsd4_umh_cltrack_check, 1984 .grace_done = nfsd4_umh_cltrack_grace_done, 1985 .version = 1, 1986 .msglen = 0, 1987 }; 1988 1989 static inline int check_for_legacy_methods(int status, struct net *net) 1990 { 1991 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 1992 struct path path; 1993 1994 /* 1995 * Next, try the UMH upcall. 1996 */ 1997 nn->client_tracking_ops = &nfsd4_umh_tracking_ops; 1998 status = nn->client_tracking_ops->init(net); 1999 if (!status) 2000 return status; 2001 2002 /* 2003 * Finally, See if the recoverydir exists and is a directory. 2004 * If it is, then use the legacy ops. 2005 */ 2006 nn->client_tracking_ops = &nfsd4_legacy_tracking_ops; 2007 status = kern_path(nfs4_recoverydir(), LOOKUP_FOLLOW, &path); 2008 if (!status) { 2009 status = !d_is_dir(path.dentry); 2010 path_put(&path); 2011 if (status) 2012 return -ENOTDIR; 2013 } 2014 return status; 2015 } 2016 #else 2017 static inline int check_for_legacy_methods(int status, struct net *net) 2018 { 2019 return status; 2020 } 2021 #endif /* CONFIG_LEGACY_NFSD_CLIENT_TRACKING */ 2022 2023 int 2024 nfsd4_client_tracking_init(struct net *net) 2025 { 2026 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 2027 int status; 2028 2029 /* just run the init if it the method is already decided */ 2030 if (nn->client_tracking_ops) 2031 goto do_init; 2032 2033 /* First, try to use nfsdcld */ 2034 nn->client_tracking_ops = &nfsd4_cld_tracking_ops; 2035 status = nn->client_tracking_ops->init(net); 2036 if (!status) 2037 return status; 2038 if (status != -ETIMEDOUT) { 2039 nn->client_tracking_ops = &nfsd4_cld_tracking_ops_v0; 2040 status = nn->client_tracking_ops->init(net); 2041 if (!status) 2042 return status; 2043 } 2044 2045 status = check_for_legacy_methods(status, net); 2046 if (status) 2047 goto out; 2048 do_init: 2049 status = nn->client_tracking_ops->init(net); 2050 out: 2051 if (status) { 2052 pr_warn("NFSD: Unable to initialize client recovery tracking! (%d)\n", status); 2053 pr_warn("NFSD: Is nfsdcld running? If not, enable CONFIG_NFSD_LEGACY_CLIENT_TRACKING.\n"); 2054 nn->client_tracking_ops = NULL; 2055 } 2056 return status; 2057 } 2058 2059 void 2060 nfsd4_client_tracking_exit(struct net *net) 2061 { 2062 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 2063 2064 if (nn->client_tracking_ops) { 2065 if (nn->client_tracking_ops->exit) 2066 nn->client_tracking_ops->exit(net); 2067 nn->client_tracking_ops = NULL; 2068 } 2069 } 2070 2071 void 2072 nfsd4_client_record_create(struct nfs4_client *clp) 2073 { 2074 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 2075 2076 if (nn->client_tracking_ops) 2077 nn->client_tracking_ops->create(clp); 2078 } 2079 2080 void 2081 nfsd4_client_record_remove(struct nfs4_client *clp) 2082 { 2083 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 2084 2085 if (nn->client_tracking_ops) 2086 nn->client_tracking_ops->remove(clp); 2087 } 2088 2089 int 2090 nfsd4_client_record_check(struct nfs4_client *clp) 2091 { 2092 struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); 2093 2094 if (nn->client_tracking_ops) 2095 return nn->client_tracking_ops->check(clp); 2096 2097 return -EOPNOTSUPP; 2098 } 2099 2100 void 2101 nfsd4_record_grace_done(struct nfsd_net *nn) 2102 { 2103 if (nn->client_tracking_ops) 2104 nn->client_tracking_ops->grace_done(nn); 2105 } 2106 2107 static int 2108 rpc_pipefs_event(struct notifier_block *nb, unsigned long event, void *ptr) 2109 { 2110 struct super_block *sb = ptr; 2111 struct net *net = sb->s_fs_info; 2112 struct nfsd_net *nn = net_generic(net, nfsd_net_id); 2113 struct cld_net *cn = nn->cld_net; 2114 int ret = 0; 2115 2116 if (!try_module_get(THIS_MODULE)) 2117 return 0; 2118 2119 if (!cn) { 2120 module_put(THIS_MODULE); 2121 return 0; 2122 } 2123 2124 switch (event) { 2125 case RPC_PIPEFS_MOUNT: 2126 ret = nfsd4_cld_register_sb(sb, cn->cn_pipe); 2127 break; 2128 case RPC_PIPEFS_UMOUNT: 2129 rpc_unlink(cn->cn_pipe); 2130 break; 2131 default: 2132 ret = -ENOTSUPP; 2133 break; 2134 } 2135 module_put(THIS_MODULE); 2136 return ret; 2137 } 2138 2139 static struct notifier_block nfsd4_cld_block = { 2140 .notifier_call = rpc_pipefs_event, 2141 }; 2142 2143 int 2144 register_cld_notifier(void) 2145 { 2146 WARN_ON(!nfsd_net_id); 2147 return rpc_pipefs_notifier_register(&nfsd4_cld_block); 2148 } 2149 2150 void 2151 unregister_cld_notifier(void) 2152 { 2153 rpc_pipefs_notifier_unregister(&nfsd4_cld_block); 2154 } 2155