1 /* 2 * linux/fs/nfs/delegation.c 3 * 4 * Copyright (C) 2004 Trond Myklebust 5 * 6 * NFS file delegation management 7 * 8 */ 9 #include <linux/completion.h> 10 #include <linux/kthread.h> 11 #include <linux/module.h> 12 #include <linux/sched.h> 13 #include <linux/slab.h> 14 #include <linux/spinlock.h> 15 #include <linux/iversion.h> 16 17 #include <linux/nfs4.h> 18 #include <linux/nfs_fs.h> 19 #include <linux/nfs_xdr.h> 20 21 #include "nfs4_fs.h" 22 #include "delegation.h" 23 #include "internal.h" 24 #include "nfs4trace.h" 25 26 static void nfs_free_delegation(struct nfs_delegation *delegation) 27 { 28 if (delegation->cred) { 29 put_rpccred(delegation->cred); 30 delegation->cred = NULL; 31 } 32 kfree_rcu(delegation, rcu); 33 } 34 35 /** 36 * nfs_mark_delegation_referenced - set delegation's REFERENCED flag 37 * @delegation: delegation to process 38 * 39 */ 40 void nfs_mark_delegation_referenced(struct nfs_delegation *delegation) 41 { 42 set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags); 43 } 44 45 static bool 46 nfs4_is_valid_delegation(const struct nfs_delegation *delegation, 47 fmode_t flags) 48 { 49 if (delegation != NULL && (delegation->type & flags) == flags && 50 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) && 51 !test_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) 52 return true; 53 return false; 54 } 55 56 static int 57 nfs4_do_check_delegation(struct inode *inode, fmode_t flags, bool mark) 58 { 59 struct nfs_delegation *delegation; 60 int ret = 0; 61 62 flags &= FMODE_READ|FMODE_WRITE; 63 rcu_read_lock(); 64 delegation = rcu_dereference(NFS_I(inode)->delegation); 65 if (nfs4_is_valid_delegation(delegation, flags)) { 66 if (mark) 67 nfs_mark_delegation_referenced(delegation); 68 ret = 1; 69 } 70 rcu_read_unlock(); 71 return ret; 72 } 73 /** 74 * nfs_have_delegation - check if inode has a delegation, mark it 75 * NFS_DELEGATION_REFERENCED if there is one. 76 * @inode: inode to check 77 * @flags: delegation types to check for 78 * 79 * Returns one if inode has the indicated delegation, otherwise zero. 80 */ 81 int nfs4_have_delegation(struct inode *inode, fmode_t flags) 82 { 83 return nfs4_do_check_delegation(inode, flags, true); 84 } 85 86 /* 87 * nfs4_check_delegation - check if inode has a delegation, do not mark 88 * NFS_DELEGATION_REFERENCED if it has one. 89 */ 90 int nfs4_check_delegation(struct inode *inode, fmode_t flags) 91 { 92 return nfs4_do_check_delegation(inode, flags, false); 93 } 94 95 static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid) 96 { 97 struct inode *inode = state->inode; 98 struct file_lock *fl; 99 struct file_lock_context *flctx = inode->i_flctx; 100 struct list_head *list; 101 int status = 0; 102 103 if (flctx == NULL) 104 goto out; 105 106 list = &flctx->flc_posix; 107 spin_lock(&flctx->flc_lock); 108 restart: 109 list_for_each_entry(fl, list, fl_list) { 110 if (nfs_file_open_context(fl->fl_file) != ctx) 111 continue; 112 spin_unlock(&flctx->flc_lock); 113 status = nfs4_lock_delegation_recall(fl, state, stateid); 114 if (status < 0) 115 goto out; 116 spin_lock(&flctx->flc_lock); 117 } 118 if (list == &flctx->flc_posix) { 119 list = &flctx->flc_flock; 120 goto restart; 121 } 122 spin_unlock(&flctx->flc_lock); 123 out: 124 return status; 125 } 126 127 static int nfs_delegation_claim_opens(struct inode *inode, 128 const nfs4_stateid *stateid, fmode_t type) 129 { 130 struct nfs_inode *nfsi = NFS_I(inode); 131 struct nfs_open_context *ctx; 132 struct nfs4_state_owner *sp; 133 struct nfs4_state *state; 134 unsigned int seq; 135 int err; 136 137 again: 138 spin_lock(&inode->i_lock); 139 list_for_each_entry(ctx, &nfsi->open_files, list) { 140 state = ctx->state; 141 if (state == NULL) 142 continue; 143 if (!test_bit(NFS_DELEGATED_STATE, &state->flags)) 144 continue; 145 if (!nfs4_valid_open_stateid(state)) 146 continue; 147 if (!nfs4_stateid_match(&state->stateid, stateid)) 148 continue; 149 get_nfs_open_context(ctx); 150 spin_unlock(&inode->i_lock); 151 sp = state->owner; 152 /* Block nfs4_proc_unlck */ 153 mutex_lock(&sp->so_delegreturn_mutex); 154 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount); 155 err = nfs4_open_delegation_recall(ctx, state, stateid, type); 156 if (!err) 157 err = nfs_delegation_claim_locks(ctx, state, stateid); 158 if (!err && read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) 159 err = -EAGAIN; 160 mutex_unlock(&sp->so_delegreturn_mutex); 161 put_nfs_open_context(ctx); 162 if (err != 0) 163 return err; 164 goto again; 165 } 166 spin_unlock(&inode->i_lock); 167 return 0; 168 } 169 170 /** 171 * nfs_inode_reclaim_delegation - process a delegation reclaim request 172 * @inode: inode to process 173 * @cred: credential to use for request 174 * @res: new delegation state from server 175 * 176 */ 177 void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred, 178 struct nfs_openres *res) 179 { 180 struct nfs_delegation *delegation; 181 struct rpc_cred *oldcred = NULL; 182 183 rcu_read_lock(); 184 delegation = rcu_dereference(NFS_I(inode)->delegation); 185 if (delegation != NULL) { 186 spin_lock(&delegation->lock); 187 if (delegation->inode != NULL) { 188 nfs4_stateid_copy(&delegation->stateid, &res->delegation); 189 delegation->type = res->delegation_type; 190 delegation->pagemod_limit = res->pagemod_limit; 191 oldcred = delegation->cred; 192 delegation->cred = get_rpccred(cred); 193 clear_bit(NFS_DELEGATION_NEED_RECLAIM, 194 &delegation->flags); 195 spin_unlock(&delegation->lock); 196 rcu_read_unlock(); 197 put_rpccred(oldcred); 198 trace_nfs4_reclaim_delegation(inode, res->delegation_type); 199 return; 200 } 201 /* We appear to have raced with a delegation return. */ 202 spin_unlock(&delegation->lock); 203 } 204 rcu_read_unlock(); 205 nfs_inode_set_delegation(inode, cred, res); 206 } 207 208 static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync) 209 { 210 int res = 0; 211 212 if (!test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) 213 res = nfs4_proc_delegreturn(inode, 214 delegation->cred, 215 &delegation->stateid, 216 issync); 217 nfs_free_delegation(delegation); 218 return res; 219 } 220 221 static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation) 222 { 223 struct inode *inode = NULL; 224 225 spin_lock(&delegation->lock); 226 if (delegation->inode != NULL) 227 inode = igrab(delegation->inode); 228 spin_unlock(&delegation->lock); 229 return inode; 230 } 231 232 static struct nfs_delegation * 233 nfs_start_delegation_return_locked(struct nfs_inode *nfsi) 234 { 235 struct nfs_delegation *ret = NULL; 236 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation); 237 238 if (delegation == NULL) 239 goto out; 240 spin_lock(&delegation->lock); 241 if (!test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) 242 ret = delegation; 243 spin_unlock(&delegation->lock); 244 out: 245 return ret; 246 } 247 248 static struct nfs_delegation * 249 nfs_start_delegation_return(struct nfs_inode *nfsi) 250 { 251 struct nfs_delegation *delegation; 252 253 rcu_read_lock(); 254 delegation = nfs_start_delegation_return_locked(nfsi); 255 rcu_read_unlock(); 256 return delegation; 257 } 258 259 static void 260 nfs_abort_delegation_return(struct nfs_delegation *delegation, 261 struct nfs_client *clp) 262 { 263 264 spin_lock(&delegation->lock); 265 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags); 266 set_bit(NFS_DELEGATION_RETURN, &delegation->flags); 267 spin_unlock(&delegation->lock); 268 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state); 269 } 270 271 static struct nfs_delegation * 272 nfs_detach_delegation_locked(struct nfs_inode *nfsi, 273 struct nfs_delegation *delegation, 274 struct nfs_client *clp) 275 { 276 struct nfs_delegation *deleg_cur = 277 rcu_dereference_protected(nfsi->delegation, 278 lockdep_is_held(&clp->cl_lock)); 279 280 if (deleg_cur == NULL || delegation != deleg_cur) 281 return NULL; 282 283 spin_lock(&delegation->lock); 284 set_bit(NFS_DELEGATION_RETURNING, &delegation->flags); 285 list_del_rcu(&delegation->super_list); 286 delegation->inode = NULL; 287 rcu_assign_pointer(nfsi->delegation, NULL); 288 spin_unlock(&delegation->lock); 289 return delegation; 290 } 291 292 static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi, 293 struct nfs_delegation *delegation, 294 struct nfs_server *server) 295 { 296 struct nfs_client *clp = server->nfs_client; 297 298 spin_lock(&clp->cl_lock); 299 delegation = nfs_detach_delegation_locked(nfsi, delegation, clp); 300 spin_unlock(&clp->cl_lock); 301 return delegation; 302 } 303 304 static struct nfs_delegation * 305 nfs_inode_detach_delegation(struct inode *inode) 306 { 307 struct nfs_inode *nfsi = NFS_I(inode); 308 struct nfs_server *server = NFS_SERVER(inode); 309 struct nfs_delegation *delegation; 310 311 delegation = nfs_start_delegation_return(nfsi); 312 if (delegation == NULL) 313 return NULL; 314 return nfs_detach_delegation(nfsi, delegation, server); 315 } 316 317 static void 318 nfs_update_inplace_delegation(struct nfs_delegation *delegation, 319 const struct nfs_delegation *update) 320 { 321 if (nfs4_stateid_is_newer(&update->stateid, &delegation->stateid)) { 322 delegation->stateid.seqid = update->stateid.seqid; 323 smp_wmb(); 324 delegation->type = update->type; 325 } 326 } 327 328 /** 329 * nfs_inode_set_delegation - set up a delegation on an inode 330 * @inode: inode to which delegation applies 331 * @cred: cred to use for subsequent delegation processing 332 * @res: new delegation state from server 333 * 334 * Returns zero on success, or a negative errno value. 335 */ 336 int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res) 337 { 338 struct nfs_server *server = NFS_SERVER(inode); 339 struct nfs_client *clp = server->nfs_client; 340 struct nfs_inode *nfsi = NFS_I(inode); 341 struct nfs_delegation *delegation, *old_delegation; 342 struct nfs_delegation *freeme = NULL; 343 int status = 0; 344 345 delegation = kmalloc(sizeof(*delegation), GFP_NOFS); 346 if (delegation == NULL) 347 return -ENOMEM; 348 nfs4_stateid_copy(&delegation->stateid, &res->delegation); 349 delegation->type = res->delegation_type; 350 delegation->pagemod_limit = res->pagemod_limit; 351 delegation->change_attr = inode_peek_iversion_raw(inode); 352 delegation->cred = get_rpccred(cred); 353 delegation->inode = inode; 354 delegation->flags = 1<<NFS_DELEGATION_REFERENCED; 355 spin_lock_init(&delegation->lock); 356 357 spin_lock(&clp->cl_lock); 358 old_delegation = rcu_dereference_protected(nfsi->delegation, 359 lockdep_is_held(&clp->cl_lock)); 360 if (old_delegation != NULL) { 361 /* Is this an update of the existing delegation? */ 362 if (nfs4_stateid_match_other(&old_delegation->stateid, 363 &delegation->stateid)) { 364 nfs_update_inplace_delegation(old_delegation, 365 delegation); 366 goto out; 367 } 368 /* 369 * Deal with broken servers that hand out two 370 * delegations for the same file. 371 * Allow for upgrades to a WRITE delegation, but 372 * nothing else. 373 */ 374 dfprintk(FILE, "%s: server %s handed out " 375 "a duplicate delegation!\n", 376 __func__, clp->cl_hostname); 377 if (delegation->type == old_delegation->type || 378 !(delegation->type & FMODE_WRITE)) { 379 freeme = delegation; 380 delegation = NULL; 381 goto out; 382 } 383 if (test_and_set_bit(NFS_DELEGATION_RETURNING, 384 &old_delegation->flags)) 385 goto out; 386 freeme = nfs_detach_delegation_locked(nfsi, 387 old_delegation, clp); 388 if (freeme == NULL) 389 goto out; 390 } 391 list_add_tail_rcu(&delegation->super_list, &server->delegations); 392 rcu_assign_pointer(nfsi->delegation, delegation); 393 delegation = NULL; 394 395 trace_nfs4_set_delegation(inode, res->delegation_type); 396 397 out: 398 spin_unlock(&clp->cl_lock); 399 if (delegation != NULL) 400 nfs_free_delegation(delegation); 401 if (freeme != NULL) 402 nfs_do_return_delegation(inode, freeme, 0); 403 return status; 404 } 405 406 /* 407 * Basic procedure for returning a delegation to the server 408 */ 409 static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync) 410 { 411 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; 412 struct nfs_inode *nfsi = NFS_I(inode); 413 int err = 0; 414 415 if (delegation == NULL) 416 return 0; 417 do { 418 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) 419 break; 420 err = nfs_delegation_claim_opens(inode, &delegation->stateid, 421 delegation->type); 422 if (!issync || err != -EAGAIN) 423 break; 424 /* 425 * Guard against state recovery 426 */ 427 err = nfs4_wait_clnt_recover(clp); 428 } while (err == 0); 429 430 if (err) { 431 nfs_abort_delegation_return(delegation, clp); 432 goto out; 433 } 434 if (!nfs_detach_delegation(nfsi, delegation, NFS_SERVER(inode))) 435 goto out; 436 437 err = nfs_do_return_delegation(inode, delegation, issync); 438 out: 439 return err; 440 } 441 442 static bool nfs_delegation_need_return(struct nfs_delegation *delegation) 443 { 444 bool ret = false; 445 446 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) 447 goto out; 448 if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags)) 449 ret = true; 450 if (test_and_clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags) && !ret) { 451 struct inode *inode; 452 453 spin_lock(&delegation->lock); 454 inode = delegation->inode; 455 if (inode && list_empty(&NFS_I(inode)->open_files)) 456 ret = true; 457 spin_unlock(&delegation->lock); 458 } 459 out: 460 return ret; 461 } 462 463 /** 464 * nfs_client_return_marked_delegations - return previously marked delegations 465 * @clp: nfs_client to process 466 * 467 * Note that this function is designed to be called by the state 468 * manager thread. For this reason, it cannot flush the dirty data, 469 * since that could deadlock in case of a state recovery error. 470 * 471 * Returns zero on success, or a negative errno value. 472 */ 473 int nfs_client_return_marked_delegations(struct nfs_client *clp) 474 { 475 struct nfs_delegation *delegation; 476 struct nfs_server *server; 477 struct inode *inode; 478 int err = 0; 479 480 restart: 481 rcu_read_lock(); 482 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) { 483 list_for_each_entry_rcu(delegation, &server->delegations, 484 super_list) { 485 if (!nfs_delegation_need_return(delegation)) 486 continue; 487 if (!nfs_sb_active(server->super)) 488 continue; 489 inode = nfs_delegation_grab_inode(delegation); 490 if (inode == NULL) { 491 rcu_read_unlock(); 492 nfs_sb_deactive(server->super); 493 goto restart; 494 } 495 delegation = nfs_start_delegation_return_locked(NFS_I(inode)); 496 rcu_read_unlock(); 497 498 err = nfs_end_delegation_return(inode, delegation, 0); 499 iput(inode); 500 nfs_sb_deactive(server->super); 501 if (!err) 502 goto restart; 503 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state); 504 return err; 505 } 506 } 507 rcu_read_unlock(); 508 return 0; 509 } 510 511 /** 512 * nfs_inode_return_delegation_noreclaim - return delegation, don't reclaim opens 513 * @inode: inode to process 514 * 515 * Does not protect against delegation reclaims, therefore really only safe 516 * to be called from nfs4_clear_inode(). 517 */ 518 void nfs_inode_return_delegation_noreclaim(struct inode *inode) 519 { 520 struct nfs_delegation *delegation; 521 522 delegation = nfs_inode_detach_delegation(inode); 523 if (delegation != NULL) 524 nfs_do_return_delegation(inode, delegation, 1); 525 } 526 527 /** 528 * nfs_inode_return_delegation - synchronously return a delegation 529 * @inode: inode to process 530 * 531 * This routine will always flush any dirty data to disk on the 532 * assumption that if we need to return the delegation, then 533 * we should stop caching. 534 * 535 * Returns zero on success, or a negative errno value. 536 */ 537 int nfs4_inode_return_delegation(struct inode *inode) 538 { 539 struct nfs_inode *nfsi = NFS_I(inode); 540 struct nfs_delegation *delegation; 541 int err = 0; 542 543 nfs_wb_all(inode); 544 delegation = nfs_start_delegation_return(nfsi); 545 if (delegation != NULL) 546 err = nfs_end_delegation_return(inode, delegation, 1); 547 return err; 548 } 549 550 static void nfs_mark_return_if_closed_delegation(struct nfs_server *server, 551 struct nfs_delegation *delegation) 552 { 553 set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags); 554 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state); 555 } 556 557 static void nfs_mark_return_delegation(struct nfs_server *server, 558 struct nfs_delegation *delegation) 559 { 560 set_bit(NFS_DELEGATION_RETURN, &delegation->flags); 561 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state); 562 } 563 564 static bool nfs_server_mark_return_all_delegations(struct nfs_server *server) 565 { 566 struct nfs_delegation *delegation; 567 bool ret = false; 568 569 list_for_each_entry_rcu(delegation, &server->delegations, super_list) { 570 nfs_mark_return_delegation(server, delegation); 571 ret = true; 572 } 573 return ret; 574 } 575 576 static void nfs_client_mark_return_all_delegations(struct nfs_client *clp) 577 { 578 struct nfs_server *server; 579 580 rcu_read_lock(); 581 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) 582 nfs_server_mark_return_all_delegations(server); 583 rcu_read_unlock(); 584 } 585 586 static void nfs_delegation_run_state_manager(struct nfs_client *clp) 587 { 588 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) 589 nfs4_schedule_state_manager(clp); 590 } 591 592 /** 593 * nfs_expire_all_delegations 594 * @clp: client to process 595 * 596 */ 597 void nfs_expire_all_delegations(struct nfs_client *clp) 598 { 599 nfs_client_mark_return_all_delegations(clp); 600 nfs_delegation_run_state_manager(clp); 601 } 602 603 /** 604 * nfs_super_return_all_delegations - return delegations for one superblock 605 * @sb: sb to process 606 * 607 */ 608 void nfs_server_return_all_delegations(struct nfs_server *server) 609 { 610 struct nfs_client *clp = server->nfs_client; 611 bool need_wait; 612 613 if (clp == NULL) 614 return; 615 616 rcu_read_lock(); 617 need_wait = nfs_server_mark_return_all_delegations(server); 618 rcu_read_unlock(); 619 620 if (need_wait) { 621 nfs4_schedule_state_manager(clp); 622 nfs4_wait_clnt_recover(clp); 623 } 624 } 625 626 static void nfs_mark_return_unused_delegation_types(struct nfs_server *server, 627 fmode_t flags) 628 { 629 struct nfs_delegation *delegation; 630 631 list_for_each_entry_rcu(delegation, &server->delegations, super_list) { 632 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE)) 633 continue; 634 if (delegation->type & flags) 635 nfs_mark_return_if_closed_delegation(server, delegation); 636 } 637 } 638 639 static void nfs_client_mark_return_unused_delegation_types(struct nfs_client *clp, 640 fmode_t flags) 641 { 642 struct nfs_server *server; 643 644 rcu_read_lock(); 645 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) 646 nfs_mark_return_unused_delegation_types(server, flags); 647 rcu_read_unlock(); 648 } 649 650 static void nfs_mark_delegation_revoked(struct nfs_server *server, 651 struct nfs_delegation *delegation) 652 { 653 set_bit(NFS_DELEGATION_REVOKED, &delegation->flags); 654 delegation->stateid.type = NFS4_INVALID_STATEID_TYPE; 655 nfs_mark_return_delegation(server, delegation); 656 } 657 658 static bool nfs_revoke_delegation(struct inode *inode, 659 const nfs4_stateid *stateid) 660 { 661 struct nfs_delegation *delegation; 662 nfs4_stateid tmp; 663 bool ret = false; 664 665 rcu_read_lock(); 666 delegation = rcu_dereference(NFS_I(inode)->delegation); 667 if (delegation == NULL) 668 goto out; 669 if (stateid == NULL) { 670 nfs4_stateid_copy(&tmp, &delegation->stateid); 671 stateid = &tmp; 672 } else if (!nfs4_stateid_match(stateid, &delegation->stateid)) 673 goto out; 674 nfs_mark_delegation_revoked(NFS_SERVER(inode), delegation); 675 ret = true; 676 out: 677 rcu_read_unlock(); 678 if (ret) 679 nfs_inode_find_state_and_recover(inode, stateid); 680 return ret; 681 } 682 683 void nfs_remove_bad_delegation(struct inode *inode, 684 const nfs4_stateid *stateid) 685 { 686 struct nfs_delegation *delegation; 687 688 if (!nfs_revoke_delegation(inode, stateid)) 689 return; 690 delegation = nfs_inode_detach_delegation(inode); 691 if (delegation) 692 nfs_free_delegation(delegation); 693 } 694 EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation); 695 696 /** 697 * nfs_expire_unused_delegation_types 698 * @clp: client to process 699 * @flags: delegation types to expire 700 * 701 */ 702 void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags) 703 { 704 nfs_client_mark_return_unused_delegation_types(clp, flags); 705 nfs_delegation_run_state_manager(clp); 706 } 707 708 static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server) 709 { 710 struct nfs_delegation *delegation; 711 712 list_for_each_entry_rcu(delegation, &server->delegations, super_list) { 713 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags)) 714 continue; 715 nfs_mark_return_if_closed_delegation(server, delegation); 716 } 717 } 718 719 /** 720 * nfs_expire_unreferenced_delegations - Eliminate unused delegations 721 * @clp: nfs_client to process 722 * 723 */ 724 void nfs_expire_unreferenced_delegations(struct nfs_client *clp) 725 { 726 struct nfs_server *server; 727 728 rcu_read_lock(); 729 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) 730 nfs_mark_return_unreferenced_delegations(server); 731 rcu_read_unlock(); 732 733 nfs_delegation_run_state_manager(clp); 734 } 735 736 /** 737 * nfs_async_inode_return_delegation - asynchronously return a delegation 738 * @inode: inode to process 739 * @stateid: state ID information 740 * 741 * Returns zero on success, or a negative errno value. 742 */ 743 int nfs_async_inode_return_delegation(struct inode *inode, 744 const nfs4_stateid *stateid) 745 { 746 struct nfs_server *server = NFS_SERVER(inode); 747 struct nfs_client *clp = server->nfs_client; 748 struct nfs_delegation *delegation; 749 750 rcu_read_lock(); 751 delegation = rcu_dereference(NFS_I(inode)->delegation); 752 if (delegation == NULL) 753 goto out_enoent; 754 if (stateid != NULL && 755 !clp->cl_mvops->match_stateid(&delegation->stateid, stateid)) 756 goto out_enoent; 757 nfs_mark_return_delegation(server, delegation); 758 rcu_read_unlock(); 759 760 nfs_delegation_run_state_manager(clp); 761 return 0; 762 out_enoent: 763 rcu_read_unlock(); 764 return -ENOENT; 765 } 766 767 static struct inode * 768 nfs_delegation_find_inode_server(struct nfs_server *server, 769 const struct nfs_fh *fhandle) 770 { 771 struct nfs_delegation *delegation; 772 struct inode *res = NULL; 773 774 list_for_each_entry_rcu(delegation, &server->delegations, super_list) { 775 spin_lock(&delegation->lock); 776 if (delegation->inode != NULL && 777 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) { 778 res = igrab(delegation->inode); 779 } 780 spin_unlock(&delegation->lock); 781 if (res != NULL) 782 break; 783 } 784 return res; 785 } 786 787 /** 788 * nfs_delegation_find_inode - retrieve the inode associated with a delegation 789 * @clp: client state handle 790 * @fhandle: filehandle from a delegation recall 791 * 792 * Returns pointer to inode matching "fhandle," or NULL if a matching inode 793 * cannot be found. 794 */ 795 struct inode *nfs_delegation_find_inode(struct nfs_client *clp, 796 const struct nfs_fh *fhandle) 797 { 798 struct nfs_server *server; 799 struct inode *res = NULL; 800 801 rcu_read_lock(); 802 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) { 803 res = nfs_delegation_find_inode_server(server, fhandle); 804 if (res != NULL) 805 break; 806 } 807 rcu_read_unlock(); 808 return res; 809 } 810 811 static void nfs_delegation_mark_reclaim_server(struct nfs_server *server) 812 { 813 struct nfs_delegation *delegation; 814 815 list_for_each_entry_rcu(delegation, &server->delegations, super_list) { 816 /* 817 * If the delegation may have been admin revoked, then we 818 * cannot reclaim it. 819 */ 820 if (test_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags)) 821 continue; 822 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags); 823 } 824 } 825 826 /** 827 * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed 828 * @clp: nfs_client to process 829 * 830 */ 831 void nfs_delegation_mark_reclaim(struct nfs_client *clp) 832 { 833 struct nfs_server *server; 834 835 rcu_read_lock(); 836 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) 837 nfs_delegation_mark_reclaim_server(server); 838 rcu_read_unlock(); 839 } 840 841 /** 842 * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done 843 * @clp: nfs_client to process 844 * 845 */ 846 void nfs_delegation_reap_unclaimed(struct nfs_client *clp) 847 { 848 struct nfs_delegation *delegation; 849 struct nfs_server *server; 850 struct inode *inode; 851 852 restart: 853 rcu_read_lock(); 854 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) { 855 list_for_each_entry_rcu(delegation, &server->delegations, 856 super_list) { 857 if (test_bit(NFS_DELEGATION_RETURNING, 858 &delegation->flags)) 859 continue; 860 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, 861 &delegation->flags) == 0) 862 continue; 863 if (!nfs_sb_active(server->super)) 864 continue; 865 inode = nfs_delegation_grab_inode(delegation); 866 if (inode == NULL) { 867 rcu_read_unlock(); 868 nfs_sb_deactive(server->super); 869 goto restart; 870 } 871 delegation = nfs_start_delegation_return_locked(NFS_I(inode)); 872 rcu_read_unlock(); 873 if (delegation != NULL) { 874 delegation = nfs_detach_delegation(NFS_I(inode), 875 delegation, server); 876 if (delegation != NULL) 877 nfs_free_delegation(delegation); 878 } 879 iput(inode); 880 nfs_sb_deactive(server->super); 881 goto restart; 882 } 883 } 884 rcu_read_unlock(); 885 } 886 887 static inline bool nfs4_server_rebooted(const struct nfs_client *clp) 888 { 889 return (clp->cl_state & (BIT(NFS4CLNT_CHECK_LEASE) | 890 BIT(NFS4CLNT_LEASE_EXPIRED) | 891 BIT(NFS4CLNT_SESSION_RESET))) != 0; 892 } 893 894 static void nfs_mark_test_expired_delegation(struct nfs_server *server, 895 struct nfs_delegation *delegation) 896 { 897 if (delegation->stateid.type == NFS4_INVALID_STATEID_TYPE) 898 return; 899 clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags); 900 set_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags); 901 set_bit(NFS4CLNT_DELEGATION_EXPIRED, &server->nfs_client->cl_state); 902 } 903 904 static void nfs_inode_mark_test_expired_delegation(struct nfs_server *server, 905 struct inode *inode) 906 { 907 struct nfs_delegation *delegation; 908 909 rcu_read_lock(); 910 delegation = rcu_dereference(NFS_I(inode)->delegation); 911 if (delegation) 912 nfs_mark_test_expired_delegation(server, delegation); 913 rcu_read_unlock(); 914 915 } 916 917 static void nfs_delegation_mark_test_expired_server(struct nfs_server *server) 918 { 919 struct nfs_delegation *delegation; 920 921 list_for_each_entry_rcu(delegation, &server->delegations, super_list) 922 nfs_mark_test_expired_delegation(server, delegation); 923 } 924 925 /** 926 * nfs_mark_test_expired_all_delegations - mark all delegations for testing 927 * @clp: nfs_client to process 928 * 929 * Iterates through all the delegations associated with this server and 930 * marks them as needing to be checked for validity. 931 */ 932 void nfs_mark_test_expired_all_delegations(struct nfs_client *clp) 933 { 934 struct nfs_server *server; 935 936 rcu_read_lock(); 937 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) 938 nfs_delegation_mark_test_expired_server(server); 939 rcu_read_unlock(); 940 } 941 942 /** 943 * nfs_reap_expired_delegations - reap expired delegations 944 * @clp: nfs_client to process 945 * 946 * Iterates through all the delegations associated with this server and 947 * checks if they have may have been revoked. This function is usually 948 * expected to be called in cases where the server may have lost its 949 * lease. 950 */ 951 void nfs_reap_expired_delegations(struct nfs_client *clp) 952 { 953 const struct nfs4_minor_version_ops *ops = clp->cl_mvops; 954 struct nfs_delegation *delegation; 955 struct nfs_server *server; 956 struct inode *inode; 957 struct rpc_cred *cred; 958 nfs4_stateid stateid; 959 960 restart: 961 rcu_read_lock(); 962 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) { 963 list_for_each_entry_rcu(delegation, &server->delegations, 964 super_list) { 965 if (test_bit(NFS_DELEGATION_RETURNING, 966 &delegation->flags)) 967 continue; 968 if (test_bit(NFS_DELEGATION_TEST_EXPIRED, 969 &delegation->flags) == 0) 970 continue; 971 if (!nfs_sb_active(server->super)) 972 continue; 973 inode = nfs_delegation_grab_inode(delegation); 974 if (inode == NULL) { 975 rcu_read_unlock(); 976 nfs_sb_deactive(server->super); 977 goto restart; 978 } 979 cred = get_rpccred_rcu(delegation->cred); 980 nfs4_stateid_copy(&stateid, &delegation->stateid); 981 clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags); 982 rcu_read_unlock(); 983 if (cred != NULL && 984 ops->test_and_free_expired(server, &stateid, cred) < 0) { 985 nfs_revoke_delegation(inode, &stateid); 986 nfs_inode_find_state_and_recover(inode, &stateid); 987 } 988 put_rpccred(cred); 989 if (nfs4_server_rebooted(clp)) { 990 nfs_inode_mark_test_expired_delegation(server,inode); 991 iput(inode); 992 nfs_sb_deactive(server->super); 993 return; 994 } 995 iput(inode); 996 nfs_sb_deactive(server->super); 997 goto restart; 998 } 999 } 1000 rcu_read_unlock(); 1001 } 1002 1003 void nfs_inode_find_delegation_state_and_recover(struct inode *inode, 1004 const nfs4_stateid *stateid) 1005 { 1006 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; 1007 struct nfs_delegation *delegation; 1008 bool found = false; 1009 1010 rcu_read_lock(); 1011 delegation = rcu_dereference(NFS_I(inode)->delegation); 1012 if (delegation && 1013 nfs4_stateid_match_other(&delegation->stateid, stateid)) { 1014 nfs_mark_test_expired_delegation(NFS_SERVER(inode), delegation); 1015 found = true; 1016 } 1017 rcu_read_unlock(); 1018 if (found) 1019 nfs4_schedule_state_manager(clp); 1020 } 1021 1022 /** 1023 * nfs_delegations_present - check for existence of delegations 1024 * @clp: client state handle 1025 * 1026 * Returns one if there are any nfs_delegation structures attached 1027 * to this nfs_client. 1028 */ 1029 int nfs_delegations_present(struct nfs_client *clp) 1030 { 1031 struct nfs_server *server; 1032 int ret = 0; 1033 1034 rcu_read_lock(); 1035 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) 1036 if (!list_empty(&server->delegations)) { 1037 ret = 1; 1038 break; 1039 } 1040 rcu_read_unlock(); 1041 return ret; 1042 } 1043 1044 /** 1045 * nfs4_refresh_delegation_stateid - Update delegation stateid seqid 1046 * @dst: stateid to refresh 1047 * @inode: inode to check 1048 * 1049 * Returns "true" and updates "dst->seqid" * if inode had a delegation 1050 * that matches our delegation stateid. Otherwise "false" is returned. 1051 */ 1052 bool nfs4_refresh_delegation_stateid(nfs4_stateid *dst, struct inode *inode) 1053 { 1054 struct nfs_delegation *delegation; 1055 bool ret = false; 1056 if (!inode) 1057 goto out; 1058 1059 rcu_read_lock(); 1060 delegation = rcu_dereference(NFS_I(inode)->delegation); 1061 if (delegation != NULL && 1062 nfs4_stateid_match_other(dst, &delegation->stateid)) { 1063 dst->seqid = delegation->stateid.seqid; 1064 return ret; 1065 } 1066 rcu_read_unlock(); 1067 out: 1068 return ret; 1069 } 1070 1071 /** 1072 * nfs4_copy_delegation_stateid - Copy inode's state ID information 1073 * @inode: inode to check 1074 * @flags: delegation type requirement 1075 * @dst: stateid data structure to fill in 1076 * @cred: optional argument to retrieve credential 1077 * 1078 * Returns "true" and fills in "dst->data" * if inode had a delegation, 1079 * otherwise "false" is returned. 1080 */ 1081 bool nfs4_copy_delegation_stateid(struct inode *inode, fmode_t flags, 1082 nfs4_stateid *dst, struct rpc_cred **cred) 1083 { 1084 struct nfs_inode *nfsi = NFS_I(inode); 1085 struct nfs_delegation *delegation; 1086 bool ret; 1087 1088 flags &= FMODE_READ|FMODE_WRITE; 1089 rcu_read_lock(); 1090 delegation = rcu_dereference(nfsi->delegation); 1091 ret = nfs4_is_valid_delegation(delegation, flags); 1092 if (ret) { 1093 nfs4_stateid_copy(dst, &delegation->stateid); 1094 nfs_mark_delegation_referenced(delegation); 1095 if (cred) 1096 *cred = get_rpccred(delegation->cred); 1097 } 1098 rcu_read_unlock(); 1099 return ret; 1100 } 1101 1102 /** 1103 * nfs4_delegation_flush_on_close - Check if we must flush file on close 1104 * @inode: inode to check 1105 * 1106 * This function checks the number of outstanding writes to the file 1107 * against the delegation 'space_limit' field to see if 1108 * the spec requires us to flush the file on close. 1109 */ 1110 bool nfs4_delegation_flush_on_close(const struct inode *inode) 1111 { 1112 struct nfs_inode *nfsi = NFS_I(inode); 1113 struct nfs_delegation *delegation; 1114 bool ret = true; 1115 1116 rcu_read_lock(); 1117 delegation = rcu_dereference(nfsi->delegation); 1118 if (delegation == NULL || !(delegation->type & FMODE_WRITE)) 1119 goto out; 1120 if (atomic_long_read(&nfsi->nrequests) < delegation->pagemod_limit) 1121 ret = false; 1122 out: 1123 rcu_read_unlock(); 1124 return ret; 1125 } 1126