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