1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * linux/fs/nfs/delegation.c 4 * 5 * Copyright (C) 2004 Trond Myklebust 6 * 7 * NFS file delegation management 8 * 9 */ 10 #include <linux/completion.h> 11 #include <linux/kthread.h> 12 #include <linux/module.h> 13 #include <linux/sched.h> 14 #include <linux/slab.h> 15 #include <linux/spinlock.h> 16 #include <linux/iversion.h> 17 18 #include <linux/nfs4.h> 19 #include <linux/nfs_fs.h> 20 #include <linux/nfs_xdr.h> 21 22 #include "nfs4_fs.h" 23 #include "nfs4session.h" 24 #include "delegation.h" 25 #include "internal.h" 26 #include "nfs4trace.h" 27 28 #define NFS_DEFAULT_DELEGATION_WATERMARK (5000U) 29 30 static atomic_long_t nfs_active_delegations; 31 static unsigned nfs_delegation_watermark = NFS_DEFAULT_DELEGATION_WATERMARK; 32 33 static void __nfs_free_delegation(struct nfs_delegation *delegation) 34 { 35 put_cred(delegation->cred); 36 delegation->cred = NULL; 37 kfree_rcu(delegation, rcu); 38 } 39 40 static void nfs_mark_delegation_revoked(struct nfs_delegation *delegation) 41 { 42 if (!test_and_set_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) { 43 delegation->stateid.type = NFS4_INVALID_STATEID_TYPE; 44 atomic_long_dec(&nfs_active_delegations); 45 if (!test_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) 46 nfs_clear_verifier_delegated(delegation->inode); 47 } 48 } 49 50 static struct nfs_delegation *nfs_get_delegation(struct nfs_delegation *delegation) 51 { 52 refcount_inc(&delegation->refcount); 53 return delegation; 54 } 55 56 static void nfs_put_delegation(struct nfs_delegation *delegation) 57 { 58 if (refcount_dec_and_test(&delegation->refcount)) 59 __nfs_free_delegation(delegation); 60 } 61 62 static void nfs_free_delegation(struct nfs_delegation *delegation) 63 { 64 nfs_mark_delegation_revoked(delegation); 65 nfs_put_delegation(delegation); 66 } 67 68 /** 69 * nfs_mark_delegation_referenced - set delegation's REFERENCED flag 70 * @delegation: delegation to process 71 * 72 */ 73 void nfs_mark_delegation_referenced(struct nfs_delegation *delegation) 74 { 75 set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags); 76 } 77 78 static bool 79 nfs4_is_valid_delegation(const struct nfs_delegation *delegation, 80 fmode_t flags) 81 { 82 if (delegation != NULL && (delegation->type & flags) == flags && 83 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) && 84 !test_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) 85 return true; 86 return false; 87 } 88 89 struct nfs_delegation *nfs4_get_valid_delegation(const struct inode *inode) 90 { 91 struct nfs_delegation *delegation; 92 93 delegation = rcu_dereference(NFS_I(inode)->delegation); 94 if (nfs4_is_valid_delegation(delegation, 0)) 95 return delegation; 96 return NULL; 97 } 98 99 static int 100 nfs4_do_check_delegation(struct inode *inode, fmode_t flags, bool mark) 101 { 102 struct nfs_delegation *delegation; 103 int ret = 0; 104 105 flags &= FMODE_READ|FMODE_WRITE; 106 rcu_read_lock(); 107 delegation = rcu_dereference(NFS_I(inode)->delegation); 108 if (nfs4_is_valid_delegation(delegation, flags)) { 109 if (mark) 110 nfs_mark_delegation_referenced(delegation); 111 ret = 1; 112 } 113 rcu_read_unlock(); 114 return ret; 115 } 116 /** 117 * nfs4_have_delegation - check if inode has a delegation, mark it 118 * NFS_DELEGATION_REFERENCED if there is one. 119 * @inode: inode to check 120 * @flags: delegation types to check for 121 * 122 * Returns one if inode has the indicated delegation, otherwise zero. 123 */ 124 int nfs4_have_delegation(struct inode *inode, fmode_t flags) 125 { 126 return nfs4_do_check_delegation(inode, flags, true); 127 } 128 129 /* 130 * nfs4_check_delegation - check if inode has a delegation, do not mark 131 * NFS_DELEGATION_REFERENCED if it has one. 132 */ 133 int nfs4_check_delegation(struct inode *inode, fmode_t flags) 134 { 135 return nfs4_do_check_delegation(inode, flags, false); 136 } 137 138 static int nfs_delegation_claim_locks(struct nfs4_state *state, const nfs4_stateid *stateid) 139 { 140 struct inode *inode = state->inode; 141 struct file_lock *fl; 142 struct file_lock_context *flctx = inode->i_flctx; 143 struct list_head *list; 144 int status = 0; 145 146 if (flctx == NULL) 147 goto out; 148 149 list = &flctx->flc_posix; 150 spin_lock(&flctx->flc_lock); 151 restart: 152 list_for_each_entry(fl, list, fl_list) { 153 if (nfs_file_open_context(fl->fl_file)->state != state) 154 continue; 155 spin_unlock(&flctx->flc_lock); 156 status = nfs4_lock_delegation_recall(fl, state, stateid); 157 if (status < 0) 158 goto out; 159 spin_lock(&flctx->flc_lock); 160 } 161 if (list == &flctx->flc_posix) { 162 list = &flctx->flc_flock; 163 goto restart; 164 } 165 spin_unlock(&flctx->flc_lock); 166 out: 167 return status; 168 } 169 170 static int nfs_delegation_claim_opens(struct inode *inode, 171 const nfs4_stateid *stateid, fmode_t type) 172 { 173 struct nfs_inode *nfsi = NFS_I(inode); 174 struct nfs_open_context *ctx; 175 struct nfs4_state_owner *sp; 176 struct nfs4_state *state; 177 unsigned int seq; 178 int err; 179 180 again: 181 rcu_read_lock(); 182 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) { 183 state = ctx->state; 184 if (state == NULL) 185 continue; 186 if (!test_bit(NFS_DELEGATED_STATE, &state->flags)) 187 continue; 188 if (!nfs4_valid_open_stateid(state)) 189 continue; 190 if (!nfs4_stateid_match(&state->stateid, stateid)) 191 continue; 192 if (!get_nfs_open_context(ctx)) 193 continue; 194 rcu_read_unlock(); 195 sp = state->owner; 196 /* Block nfs4_proc_unlck */ 197 mutex_lock(&sp->so_delegreturn_mutex); 198 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount); 199 err = nfs4_open_delegation_recall(ctx, state, stateid); 200 if (!err) 201 err = nfs_delegation_claim_locks(state, stateid); 202 if (!err && read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) 203 err = -EAGAIN; 204 mutex_unlock(&sp->so_delegreturn_mutex); 205 put_nfs_open_context(ctx); 206 if (err != 0) 207 return err; 208 goto again; 209 } 210 rcu_read_unlock(); 211 return 0; 212 } 213 214 /** 215 * nfs_inode_reclaim_delegation - process a delegation reclaim request 216 * @inode: inode to process 217 * @cred: credential to use for request 218 * @type: delegation type 219 * @stateid: delegation stateid 220 * @pagemod_limit: write delegation "space_limit" 221 * 222 */ 223 void nfs_inode_reclaim_delegation(struct inode *inode, const struct cred *cred, 224 fmode_t type, 225 const nfs4_stateid *stateid, 226 unsigned long pagemod_limit) 227 { 228 struct nfs_delegation *delegation; 229 const struct cred *oldcred = NULL; 230 231 rcu_read_lock(); 232 delegation = rcu_dereference(NFS_I(inode)->delegation); 233 if (delegation != NULL) { 234 spin_lock(&delegation->lock); 235 if (nfs4_is_valid_delegation(delegation, 0)) { 236 nfs4_stateid_copy(&delegation->stateid, stateid); 237 delegation->type = type; 238 delegation->pagemod_limit = pagemod_limit; 239 oldcred = delegation->cred; 240 delegation->cred = get_cred(cred); 241 clear_bit(NFS_DELEGATION_NEED_RECLAIM, 242 &delegation->flags); 243 spin_unlock(&delegation->lock); 244 rcu_read_unlock(); 245 put_cred(oldcred); 246 trace_nfs4_reclaim_delegation(inode, type); 247 return; 248 } 249 /* We appear to have raced with a delegation return. */ 250 spin_unlock(&delegation->lock); 251 } 252 rcu_read_unlock(); 253 nfs_inode_set_delegation(inode, cred, type, stateid, pagemod_limit); 254 } 255 256 static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync) 257 { 258 const struct cred *cred; 259 int res = 0; 260 261 if (!test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) { 262 spin_lock(&delegation->lock); 263 cred = get_cred(delegation->cred); 264 spin_unlock(&delegation->lock); 265 res = nfs4_proc_delegreturn(inode, cred, 266 &delegation->stateid, 267 issync); 268 put_cred(cred); 269 } 270 return res; 271 } 272 273 static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation) 274 { 275 struct inode *inode = NULL; 276 277 spin_lock(&delegation->lock); 278 if (delegation->inode != NULL) 279 inode = igrab(delegation->inode); 280 if (!inode) 281 set_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags); 282 spin_unlock(&delegation->lock); 283 return inode; 284 } 285 286 static struct nfs_delegation * 287 nfs_start_delegation_return_locked(struct nfs_inode *nfsi) 288 { 289 struct nfs_delegation *ret = NULL; 290 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation); 291 292 if (delegation == NULL) 293 goto out; 294 spin_lock(&delegation->lock); 295 if (!test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) { 296 /* Refcount matched in nfs_end_delegation_return() */ 297 ret = nfs_get_delegation(delegation); 298 } 299 spin_unlock(&delegation->lock); 300 if (ret) 301 nfs_clear_verifier_delegated(&nfsi->vfs_inode); 302 out: 303 return ret; 304 } 305 306 static struct nfs_delegation * 307 nfs_start_delegation_return(struct nfs_inode *nfsi) 308 { 309 struct nfs_delegation *delegation; 310 311 rcu_read_lock(); 312 delegation = nfs_start_delegation_return_locked(nfsi); 313 rcu_read_unlock(); 314 return delegation; 315 } 316 317 static void 318 nfs_abort_delegation_return(struct nfs_delegation *delegation, 319 struct nfs_client *clp) 320 { 321 322 spin_lock(&delegation->lock); 323 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags); 324 set_bit(NFS_DELEGATION_RETURN, &delegation->flags); 325 spin_unlock(&delegation->lock); 326 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state); 327 } 328 329 static struct nfs_delegation * 330 nfs_detach_delegation_locked(struct nfs_inode *nfsi, 331 struct nfs_delegation *delegation, 332 struct nfs_client *clp) 333 { 334 struct nfs_delegation *deleg_cur = 335 rcu_dereference_protected(nfsi->delegation, 336 lockdep_is_held(&clp->cl_lock)); 337 338 if (deleg_cur == NULL || delegation != deleg_cur) 339 return NULL; 340 341 spin_lock(&delegation->lock); 342 if (!delegation->inode) { 343 spin_unlock(&delegation->lock); 344 return NULL; 345 } 346 list_del_rcu(&delegation->super_list); 347 delegation->inode = NULL; 348 rcu_assign_pointer(nfsi->delegation, NULL); 349 spin_unlock(&delegation->lock); 350 return delegation; 351 } 352 353 static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi, 354 struct nfs_delegation *delegation, 355 struct nfs_server *server) 356 { 357 struct nfs_client *clp = server->nfs_client; 358 359 spin_lock(&clp->cl_lock); 360 delegation = nfs_detach_delegation_locked(nfsi, delegation, clp); 361 spin_unlock(&clp->cl_lock); 362 return delegation; 363 } 364 365 static struct nfs_delegation * 366 nfs_inode_detach_delegation(struct inode *inode) 367 { 368 struct nfs_inode *nfsi = NFS_I(inode); 369 struct nfs_server *server = NFS_SERVER(inode); 370 struct nfs_delegation *delegation; 371 372 rcu_read_lock(); 373 delegation = rcu_dereference(nfsi->delegation); 374 if (delegation != NULL) 375 delegation = nfs_detach_delegation(nfsi, delegation, server); 376 rcu_read_unlock(); 377 return delegation; 378 } 379 380 static void 381 nfs_update_delegation_cred(struct nfs_delegation *delegation, 382 const struct cred *cred) 383 { 384 const struct cred *old; 385 386 if (cred_fscmp(delegation->cred, cred) != 0) { 387 old = xchg(&delegation->cred, get_cred(cred)); 388 put_cred(old); 389 } 390 } 391 392 static void 393 nfs_update_inplace_delegation(struct nfs_delegation *delegation, 394 const struct nfs_delegation *update) 395 { 396 if (nfs4_stateid_is_newer(&update->stateid, &delegation->stateid)) { 397 delegation->stateid.seqid = update->stateid.seqid; 398 smp_wmb(); 399 delegation->type = update->type; 400 delegation->pagemod_limit = update->pagemod_limit; 401 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) { 402 delegation->change_attr = update->change_attr; 403 nfs_update_delegation_cred(delegation, update->cred); 404 /* smp_mb__before_atomic() is implicit due to xchg() */ 405 clear_bit(NFS_DELEGATION_REVOKED, &delegation->flags); 406 atomic_long_inc(&nfs_active_delegations); 407 } 408 } 409 } 410 411 /** 412 * nfs_inode_set_delegation - set up a delegation on an inode 413 * @inode: inode to which delegation applies 414 * @cred: cred to use for subsequent delegation processing 415 * @type: delegation type 416 * @stateid: delegation stateid 417 * @pagemod_limit: write delegation "space_limit" 418 * 419 * Returns zero on success, or a negative errno value. 420 */ 421 int nfs_inode_set_delegation(struct inode *inode, const struct cred *cred, 422 fmode_t type, 423 const nfs4_stateid *stateid, 424 unsigned long pagemod_limit) 425 { 426 struct nfs_server *server = NFS_SERVER(inode); 427 struct nfs_client *clp = server->nfs_client; 428 struct nfs_inode *nfsi = NFS_I(inode); 429 struct nfs_delegation *delegation, *old_delegation; 430 struct nfs_delegation *freeme = NULL; 431 int status = 0; 432 433 delegation = kmalloc(sizeof(*delegation), GFP_NOFS); 434 if (delegation == NULL) 435 return -ENOMEM; 436 nfs4_stateid_copy(&delegation->stateid, stateid); 437 refcount_set(&delegation->refcount, 1); 438 delegation->type = type; 439 delegation->pagemod_limit = pagemod_limit; 440 delegation->change_attr = inode_peek_iversion_raw(inode); 441 delegation->cred = get_cred(cred); 442 delegation->inode = inode; 443 delegation->flags = 1<<NFS_DELEGATION_REFERENCED; 444 spin_lock_init(&delegation->lock); 445 446 spin_lock(&clp->cl_lock); 447 old_delegation = rcu_dereference_protected(nfsi->delegation, 448 lockdep_is_held(&clp->cl_lock)); 449 if (old_delegation == NULL) 450 goto add_new; 451 /* Is this an update of the existing delegation? */ 452 if (nfs4_stateid_match_other(&old_delegation->stateid, 453 &delegation->stateid)) { 454 spin_lock(&old_delegation->lock); 455 nfs_update_inplace_delegation(old_delegation, 456 delegation); 457 spin_unlock(&old_delegation->lock); 458 goto out; 459 } 460 if (!test_bit(NFS_DELEGATION_REVOKED, &old_delegation->flags)) { 461 /* 462 * Deal with broken servers that hand out two 463 * delegations for the same file. 464 * Allow for upgrades to a WRITE delegation, but 465 * nothing else. 466 */ 467 dfprintk(FILE, "%s: server %s handed out " 468 "a duplicate delegation!\n", 469 __func__, clp->cl_hostname); 470 if (delegation->type == old_delegation->type || 471 !(delegation->type & FMODE_WRITE)) { 472 freeme = delegation; 473 delegation = NULL; 474 goto out; 475 } 476 if (test_and_set_bit(NFS_DELEGATION_RETURNING, 477 &old_delegation->flags)) 478 goto out; 479 } 480 freeme = nfs_detach_delegation_locked(nfsi, old_delegation, clp); 481 if (freeme == NULL) 482 goto out; 483 add_new: 484 /* 485 * If we didn't revalidate the change attribute before setting 486 * the delegation, then pre-emptively ask for a full attribute 487 * cache revalidation. 488 */ 489 spin_lock(&inode->i_lock); 490 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_CHANGE) 491 nfs_set_cache_invalid(inode, 492 NFS_INO_INVALID_ATIME | NFS_INO_INVALID_CTIME | 493 NFS_INO_INVALID_MTIME | NFS_INO_INVALID_SIZE | 494 NFS_INO_INVALID_BLOCKS | NFS_INO_INVALID_NLINK | 495 NFS_INO_INVALID_OTHER | NFS_INO_INVALID_DATA | 496 NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL | 497 NFS_INO_INVALID_XATTR); 498 spin_unlock(&inode->i_lock); 499 500 list_add_tail_rcu(&delegation->super_list, &server->delegations); 501 rcu_assign_pointer(nfsi->delegation, delegation); 502 delegation = NULL; 503 504 atomic_long_inc(&nfs_active_delegations); 505 506 trace_nfs4_set_delegation(inode, type); 507 out: 508 spin_unlock(&clp->cl_lock); 509 if (delegation != NULL) 510 __nfs_free_delegation(delegation); 511 if (freeme != NULL) { 512 nfs_do_return_delegation(inode, freeme, 0); 513 nfs_free_delegation(freeme); 514 } 515 return status; 516 } 517 518 /* 519 * Basic procedure for returning a delegation to the server 520 */ 521 static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync) 522 { 523 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; 524 int err = 0; 525 526 if (delegation == NULL) 527 return 0; 528 do { 529 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) 530 break; 531 err = nfs_delegation_claim_opens(inode, &delegation->stateid, 532 delegation->type); 533 if (!issync || err != -EAGAIN) 534 break; 535 /* 536 * Guard against state recovery 537 */ 538 err = nfs4_wait_clnt_recover(clp); 539 } while (err == 0); 540 541 if (err) { 542 nfs_abort_delegation_return(delegation, clp); 543 goto out; 544 } 545 546 err = nfs_do_return_delegation(inode, delegation, issync); 547 out: 548 /* Refcount matched in nfs_start_delegation_return_locked() */ 549 nfs_put_delegation(delegation); 550 return err; 551 } 552 553 static bool nfs_delegation_need_return(struct nfs_delegation *delegation) 554 { 555 bool ret = false; 556 557 if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags)) 558 ret = true; 559 else if (test_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags)) { 560 struct inode *inode; 561 562 spin_lock(&delegation->lock); 563 inode = delegation->inode; 564 if (inode && list_empty(&NFS_I(inode)->open_files)) 565 ret = true; 566 spin_unlock(&delegation->lock); 567 } 568 if (ret) 569 clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags); 570 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags) || 571 test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) 572 ret = false; 573 574 return ret; 575 } 576 577 static int nfs_server_return_marked_delegations(struct nfs_server *server, 578 void __always_unused *data) 579 { 580 struct nfs_delegation *delegation; 581 struct nfs_delegation *prev; 582 struct inode *inode; 583 struct inode *place_holder = NULL; 584 struct nfs_delegation *place_holder_deleg = NULL; 585 int err = 0; 586 587 restart: 588 /* 589 * To avoid quadratic looping we hold a reference 590 * to an inode place_holder. Each time we restart, we 591 * list delegation in the server from the delegations 592 * of that inode. 593 * prev is an RCU-protected pointer to a delegation which 594 * wasn't marked for return and might be a good choice for 595 * the next place_holder. 596 */ 597 prev = NULL; 598 delegation = NULL; 599 rcu_read_lock(); 600 if (place_holder) 601 delegation = rcu_dereference(NFS_I(place_holder)->delegation); 602 if (!delegation || delegation != place_holder_deleg) 603 delegation = list_entry_rcu(server->delegations.next, 604 struct nfs_delegation, super_list); 605 list_for_each_entry_from_rcu(delegation, &server->delegations, super_list) { 606 struct inode *to_put = NULL; 607 608 if (test_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags)) 609 continue; 610 if (!nfs_delegation_need_return(delegation)) { 611 if (nfs4_is_valid_delegation(delegation, 0)) 612 prev = delegation; 613 continue; 614 } 615 616 if (prev) { 617 struct inode *tmp = nfs_delegation_grab_inode(prev); 618 if (tmp) { 619 to_put = place_holder; 620 place_holder = tmp; 621 place_holder_deleg = prev; 622 } 623 } 624 625 inode = nfs_delegation_grab_inode(delegation); 626 if (inode == NULL) { 627 rcu_read_unlock(); 628 iput(to_put); 629 goto restart; 630 } 631 delegation = nfs_start_delegation_return_locked(NFS_I(inode)); 632 rcu_read_unlock(); 633 634 iput(to_put); 635 636 err = nfs_end_delegation_return(inode, delegation, 0); 637 iput(inode); 638 cond_resched(); 639 if (!err) 640 goto restart; 641 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state); 642 goto out; 643 } 644 rcu_read_unlock(); 645 out: 646 iput(place_holder); 647 return err; 648 } 649 650 /** 651 * nfs_client_return_marked_delegations - return previously marked delegations 652 * @clp: nfs_client to process 653 * 654 * Note that this function is designed to be called by the state 655 * manager thread. For this reason, it cannot flush the dirty data, 656 * since that could deadlock in case of a state recovery error. 657 * 658 * Returns zero on success, or a negative errno value. 659 */ 660 int nfs_client_return_marked_delegations(struct nfs_client *clp) 661 { 662 return nfs_client_for_each_server(clp, 663 nfs_server_return_marked_delegations, NULL); 664 } 665 666 /** 667 * nfs_inode_evict_delegation - return delegation, don't reclaim opens 668 * @inode: inode to process 669 * 670 * Does not protect against delegation reclaims, therefore really only safe 671 * to be called from nfs4_clear_inode(). Guaranteed to always free 672 * the delegation structure. 673 */ 674 void nfs_inode_evict_delegation(struct inode *inode) 675 { 676 struct nfs_delegation *delegation; 677 678 delegation = nfs_inode_detach_delegation(inode); 679 if (delegation != NULL) { 680 set_bit(NFS_DELEGATION_RETURNING, &delegation->flags); 681 set_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags); 682 nfs_do_return_delegation(inode, delegation, 1); 683 nfs_free_delegation(delegation); 684 } 685 } 686 687 /** 688 * nfs4_inode_return_delegation - synchronously return a delegation 689 * @inode: inode to process 690 * 691 * This routine will always flush any dirty data to disk on the 692 * assumption that if we need to return the delegation, then 693 * we should stop caching. 694 * 695 * Returns zero on success, or a negative errno value. 696 */ 697 int nfs4_inode_return_delegation(struct inode *inode) 698 { 699 struct nfs_inode *nfsi = NFS_I(inode); 700 struct nfs_delegation *delegation; 701 int err = 0; 702 703 nfs_wb_all(inode); 704 delegation = nfs_start_delegation_return(nfsi); 705 if (delegation != NULL) 706 err = nfs_end_delegation_return(inode, delegation, 1); 707 return err; 708 } 709 710 /** 711 * nfs4_inode_return_delegation_on_close - asynchronously return a delegation 712 * @inode: inode to process 713 * 714 * This routine is called on file close in order to determine if the 715 * inode delegation needs to be returned immediately. 716 */ 717 void nfs4_inode_return_delegation_on_close(struct inode *inode) 718 { 719 struct nfs_delegation *delegation; 720 struct nfs_delegation *ret = NULL; 721 722 if (!inode) 723 return; 724 rcu_read_lock(); 725 delegation = nfs4_get_valid_delegation(inode); 726 if (!delegation) 727 goto out; 728 if (test_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags) || 729 atomic_long_read(&nfs_active_delegations) >= nfs_delegation_watermark) { 730 spin_lock(&delegation->lock); 731 if (delegation->inode && 732 list_empty(&NFS_I(inode)->open_files) && 733 !test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) { 734 clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags); 735 /* Refcount matched in nfs_end_delegation_return() */ 736 ret = nfs_get_delegation(delegation); 737 } 738 spin_unlock(&delegation->lock); 739 if (ret) 740 nfs_clear_verifier_delegated(inode); 741 } 742 out: 743 rcu_read_unlock(); 744 nfs_end_delegation_return(inode, ret, 0); 745 } 746 747 /** 748 * nfs4_inode_make_writeable 749 * @inode: pointer to inode 750 * 751 * Make the inode writeable by returning the delegation if necessary 752 * 753 * Returns zero on success, or a negative errno value. 754 */ 755 int nfs4_inode_make_writeable(struct inode *inode) 756 { 757 struct nfs_delegation *delegation; 758 759 rcu_read_lock(); 760 delegation = nfs4_get_valid_delegation(inode); 761 if (delegation == NULL || 762 (nfs4_has_session(NFS_SERVER(inode)->nfs_client) && 763 (delegation->type & FMODE_WRITE))) { 764 rcu_read_unlock(); 765 return 0; 766 } 767 rcu_read_unlock(); 768 return nfs4_inode_return_delegation(inode); 769 } 770 771 static void nfs_mark_return_if_closed_delegation(struct nfs_server *server, 772 struct nfs_delegation *delegation) 773 { 774 set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags); 775 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state); 776 } 777 778 static void nfs_mark_return_delegation(struct nfs_server *server, 779 struct nfs_delegation *delegation) 780 { 781 set_bit(NFS_DELEGATION_RETURN, &delegation->flags); 782 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state); 783 } 784 785 static bool nfs_server_mark_return_all_delegations(struct nfs_server *server) 786 { 787 struct nfs_delegation *delegation; 788 bool ret = false; 789 790 list_for_each_entry_rcu(delegation, &server->delegations, super_list) { 791 nfs_mark_return_delegation(server, delegation); 792 ret = true; 793 } 794 return ret; 795 } 796 797 static void nfs_client_mark_return_all_delegations(struct nfs_client *clp) 798 { 799 struct nfs_server *server; 800 801 rcu_read_lock(); 802 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) 803 nfs_server_mark_return_all_delegations(server); 804 rcu_read_unlock(); 805 } 806 807 static void nfs_delegation_run_state_manager(struct nfs_client *clp) 808 { 809 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) 810 nfs4_schedule_state_manager(clp); 811 } 812 813 /** 814 * nfs_expire_all_delegations 815 * @clp: client to process 816 * 817 */ 818 void nfs_expire_all_delegations(struct nfs_client *clp) 819 { 820 nfs_client_mark_return_all_delegations(clp); 821 nfs_delegation_run_state_manager(clp); 822 } 823 824 /** 825 * nfs_server_return_all_delegations - return delegations for one superblock 826 * @server: pointer to nfs_server to process 827 * 828 */ 829 void nfs_server_return_all_delegations(struct nfs_server *server) 830 { 831 struct nfs_client *clp = server->nfs_client; 832 bool need_wait; 833 834 if (clp == NULL) 835 return; 836 837 rcu_read_lock(); 838 need_wait = nfs_server_mark_return_all_delegations(server); 839 rcu_read_unlock(); 840 841 if (need_wait) { 842 nfs4_schedule_state_manager(clp); 843 nfs4_wait_clnt_recover(clp); 844 } 845 } 846 847 static void nfs_mark_return_unused_delegation_types(struct nfs_server *server, 848 fmode_t flags) 849 { 850 struct nfs_delegation *delegation; 851 852 list_for_each_entry_rcu(delegation, &server->delegations, super_list) { 853 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE)) 854 continue; 855 if (delegation->type & flags) 856 nfs_mark_return_if_closed_delegation(server, delegation); 857 } 858 } 859 860 static void nfs_client_mark_return_unused_delegation_types(struct nfs_client *clp, 861 fmode_t flags) 862 { 863 struct nfs_server *server; 864 865 rcu_read_lock(); 866 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) 867 nfs_mark_return_unused_delegation_types(server, flags); 868 rcu_read_unlock(); 869 } 870 871 static void nfs_revoke_delegation(struct inode *inode, 872 const nfs4_stateid *stateid) 873 { 874 struct nfs_delegation *delegation; 875 nfs4_stateid tmp; 876 bool ret = false; 877 878 rcu_read_lock(); 879 delegation = rcu_dereference(NFS_I(inode)->delegation); 880 if (delegation == NULL) 881 goto out; 882 if (stateid == NULL) { 883 nfs4_stateid_copy(&tmp, &delegation->stateid); 884 stateid = &tmp; 885 } else { 886 if (!nfs4_stateid_match_other(stateid, &delegation->stateid)) 887 goto out; 888 spin_lock(&delegation->lock); 889 if (stateid->seqid) { 890 if (nfs4_stateid_is_newer(&delegation->stateid, stateid)) { 891 spin_unlock(&delegation->lock); 892 goto out; 893 } 894 delegation->stateid.seqid = stateid->seqid; 895 } 896 spin_unlock(&delegation->lock); 897 } 898 nfs_mark_delegation_revoked(delegation); 899 ret = true; 900 out: 901 rcu_read_unlock(); 902 if (ret) 903 nfs_inode_find_state_and_recover(inode, stateid); 904 } 905 906 void nfs_remove_bad_delegation(struct inode *inode, 907 const nfs4_stateid *stateid) 908 { 909 nfs_revoke_delegation(inode, stateid); 910 } 911 EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation); 912 913 void nfs_delegation_mark_returned(struct inode *inode, 914 const nfs4_stateid *stateid) 915 { 916 struct nfs_delegation *delegation; 917 918 if (!inode) 919 return; 920 921 rcu_read_lock(); 922 delegation = rcu_dereference(NFS_I(inode)->delegation); 923 if (!delegation) 924 goto out_rcu_unlock; 925 926 spin_lock(&delegation->lock); 927 if (!nfs4_stateid_match_other(stateid, &delegation->stateid)) 928 goto out_spin_unlock; 929 if (stateid->seqid) { 930 /* If delegation->stateid is newer, dont mark as returned */ 931 if (nfs4_stateid_is_newer(&delegation->stateid, stateid)) 932 goto out_clear_returning; 933 if (delegation->stateid.seqid != stateid->seqid) 934 delegation->stateid.seqid = stateid->seqid; 935 } 936 937 nfs_mark_delegation_revoked(delegation); 938 939 out_clear_returning: 940 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags); 941 out_spin_unlock: 942 spin_unlock(&delegation->lock); 943 out_rcu_unlock: 944 rcu_read_unlock(); 945 946 nfs_inode_find_state_and_recover(inode, stateid); 947 } 948 949 /** 950 * nfs_expire_unused_delegation_types 951 * @clp: client to process 952 * @flags: delegation types to expire 953 * 954 */ 955 void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags) 956 { 957 nfs_client_mark_return_unused_delegation_types(clp, flags); 958 nfs_delegation_run_state_manager(clp); 959 } 960 961 static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server) 962 { 963 struct nfs_delegation *delegation; 964 965 list_for_each_entry_rcu(delegation, &server->delegations, super_list) { 966 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags)) 967 continue; 968 nfs_mark_return_if_closed_delegation(server, delegation); 969 } 970 } 971 972 /** 973 * nfs_expire_unreferenced_delegations - Eliminate unused delegations 974 * @clp: nfs_client to process 975 * 976 */ 977 void nfs_expire_unreferenced_delegations(struct nfs_client *clp) 978 { 979 struct nfs_server *server; 980 981 rcu_read_lock(); 982 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) 983 nfs_mark_return_unreferenced_delegations(server); 984 rcu_read_unlock(); 985 986 nfs_delegation_run_state_manager(clp); 987 } 988 989 /** 990 * nfs_async_inode_return_delegation - asynchronously return a delegation 991 * @inode: inode to process 992 * @stateid: state ID information 993 * 994 * Returns zero on success, or a negative errno value. 995 */ 996 int nfs_async_inode_return_delegation(struct inode *inode, 997 const nfs4_stateid *stateid) 998 { 999 struct nfs_server *server = NFS_SERVER(inode); 1000 struct nfs_client *clp = server->nfs_client; 1001 struct nfs_delegation *delegation; 1002 1003 rcu_read_lock(); 1004 delegation = nfs4_get_valid_delegation(inode); 1005 if (delegation == NULL) 1006 goto out_enoent; 1007 if (stateid != NULL && 1008 !clp->cl_mvops->match_stateid(&delegation->stateid, stateid)) 1009 goto out_enoent; 1010 nfs_mark_return_delegation(server, delegation); 1011 rcu_read_unlock(); 1012 1013 nfs_delegation_run_state_manager(clp); 1014 return 0; 1015 out_enoent: 1016 rcu_read_unlock(); 1017 return -ENOENT; 1018 } 1019 1020 static struct inode * 1021 nfs_delegation_find_inode_server(struct nfs_server *server, 1022 const struct nfs_fh *fhandle) 1023 { 1024 struct nfs_delegation *delegation; 1025 struct super_block *freeme = NULL; 1026 struct inode *res = NULL; 1027 1028 list_for_each_entry_rcu(delegation, &server->delegations, super_list) { 1029 spin_lock(&delegation->lock); 1030 if (delegation->inode != NULL && 1031 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) && 1032 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) { 1033 if (nfs_sb_active(server->super)) { 1034 freeme = server->super; 1035 res = igrab(delegation->inode); 1036 } 1037 spin_unlock(&delegation->lock); 1038 if (res != NULL) 1039 return res; 1040 if (freeme) { 1041 rcu_read_unlock(); 1042 nfs_sb_deactive(freeme); 1043 rcu_read_lock(); 1044 } 1045 return ERR_PTR(-EAGAIN); 1046 } 1047 spin_unlock(&delegation->lock); 1048 } 1049 return ERR_PTR(-ENOENT); 1050 } 1051 1052 /** 1053 * nfs_delegation_find_inode - retrieve the inode associated with a delegation 1054 * @clp: client state handle 1055 * @fhandle: filehandle from a delegation recall 1056 * 1057 * Returns pointer to inode matching "fhandle," or NULL if a matching inode 1058 * cannot be found. 1059 */ 1060 struct inode *nfs_delegation_find_inode(struct nfs_client *clp, 1061 const struct nfs_fh *fhandle) 1062 { 1063 struct nfs_server *server; 1064 struct inode *res; 1065 1066 rcu_read_lock(); 1067 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) { 1068 res = nfs_delegation_find_inode_server(server, fhandle); 1069 if (res != ERR_PTR(-ENOENT)) { 1070 rcu_read_unlock(); 1071 return res; 1072 } 1073 } 1074 rcu_read_unlock(); 1075 return ERR_PTR(-ENOENT); 1076 } 1077 1078 static void nfs_delegation_mark_reclaim_server(struct nfs_server *server) 1079 { 1080 struct nfs_delegation *delegation; 1081 1082 list_for_each_entry_rcu(delegation, &server->delegations, super_list) { 1083 /* 1084 * If the delegation may have been admin revoked, then we 1085 * cannot reclaim it. 1086 */ 1087 if (test_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags)) 1088 continue; 1089 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags); 1090 } 1091 } 1092 1093 /** 1094 * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed 1095 * @clp: nfs_client to process 1096 * 1097 */ 1098 void nfs_delegation_mark_reclaim(struct nfs_client *clp) 1099 { 1100 struct nfs_server *server; 1101 1102 rcu_read_lock(); 1103 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) 1104 nfs_delegation_mark_reclaim_server(server); 1105 rcu_read_unlock(); 1106 } 1107 1108 static int nfs_server_reap_unclaimed_delegations(struct nfs_server *server, 1109 void __always_unused *data) 1110 { 1111 struct nfs_delegation *delegation; 1112 struct inode *inode; 1113 restart: 1114 rcu_read_lock(); 1115 restart_locked: 1116 list_for_each_entry_rcu(delegation, &server->delegations, super_list) { 1117 if (test_bit(NFS_DELEGATION_INODE_FREEING, 1118 &delegation->flags) || 1119 test_bit(NFS_DELEGATION_RETURNING, 1120 &delegation->flags) || 1121 test_bit(NFS_DELEGATION_NEED_RECLAIM, 1122 &delegation->flags) == 0) 1123 continue; 1124 inode = nfs_delegation_grab_inode(delegation); 1125 if (inode == NULL) 1126 goto restart_locked; 1127 delegation = nfs_start_delegation_return_locked(NFS_I(inode)); 1128 rcu_read_unlock(); 1129 if (delegation != NULL) { 1130 if (nfs_detach_delegation(NFS_I(inode), delegation, 1131 server) != NULL) 1132 nfs_free_delegation(delegation); 1133 /* Match nfs_start_delegation_return_locked */ 1134 nfs_put_delegation(delegation); 1135 } 1136 iput(inode); 1137 cond_resched(); 1138 goto restart; 1139 } 1140 rcu_read_unlock(); 1141 return 0; 1142 } 1143 1144 /** 1145 * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done 1146 * @clp: nfs_client to process 1147 * 1148 */ 1149 void nfs_delegation_reap_unclaimed(struct nfs_client *clp) 1150 { 1151 nfs_client_for_each_server(clp, nfs_server_reap_unclaimed_delegations, 1152 NULL); 1153 } 1154 1155 static inline bool nfs4_server_rebooted(const struct nfs_client *clp) 1156 { 1157 return (clp->cl_state & (BIT(NFS4CLNT_CHECK_LEASE) | 1158 BIT(NFS4CLNT_LEASE_EXPIRED) | 1159 BIT(NFS4CLNT_SESSION_RESET))) != 0; 1160 } 1161 1162 static void nfs_mark_test_expired_delegation(struct nfs_server *server, 1163 struct nfs_delegation *delegation) 1164 { 1165 if (delegation->stateid.type == NFS4_INVALID_STATEID_TYPE) 1166 return; 1167 clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags); 1168 set_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags); 1169 set_bit(NFS4CLNT_DELEGATION_EXPIRED, &server->nfs_client->cl_state); 1170 } 1171 1172 static void nfs_inode_mark_test_expired_delegation(struct nfs_server *server, 1173 struct inode *inode) 1174 { 1175 struct nfs_delegation *delegation; 1176 1177 rcu_read_lock(); 1178 delegation = rcu_dereference(NFS_I(inode)->delegation); 1179 if (delegation) 1180 nfs_mark_test_expired_delegation(server, delegation); 1181 rcu_read_unlock(); 1182 1183 } 1184 1185 static void nfs_delegation_mark_test_expired_server(struct nfs_server *server) 1186 { 1187 struct nfs_delegation *delegation; 1188 1189 list_for_each_entry_rcu(delegation, &server->delegations, super_list) 1190 nfs_mark_test_expired_delegation(server, delegation); 1191 } 1192 1193 /** 1194 * nfs_mark_test_expired_all_delegations - mark all delegations for testing 1195 * @clp: nfs_client to process 1196 * 1197 * Iterates through all the delegations associated with this server and 1198 * marks them as needing to be checked for validity. 1199 */ 1200 void nfs_mark_test_expired_all_delegations(struct nfs_client *clp) 1201 { 1202 struct nfs_server *server; 1203 1204 rcu_read_lock(); 1205 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) 1206 nfs_delegation_mark_test_expired_server(server); 1207 rcu_read_unlock(); 1208 } 1209 1210 /** 1211 * nfs_test_expired_all_delegations - test all delegations for a client 1212 * @clp: nfs_client to process 1213 * 1214 * Helper for handling "recallable state revoked" status from server. 1215 */ 1216 void nfs_test_expired_all_delegations(struct nfs_client *clp) 1217 { 1218 nfs_mark_test_expired_all_delegations(clp); 1219 nfs4_schedule_state_manager(clp); 1220 } 1221 1222 static void 1223 nfs_delegation_test_free_expired(struct inode *inode, 1224 nfs4_stateid *stateid, 1225 const struct cred *cred) 1226 { 1227 struct nfs_server *server = NFS_SERVER(inode); 1228 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops; 1229 int status; 1230 1231 if (!cred) 1232 return; 1233 status = ops->test_and_free_expired(server, stateid, cred); 1234 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) 1235 nfs_remove_bad_delegation(inode, stateid); 1236 } 1237 1238 static int nfs_server_reap_expired_delegations(struct nfs_server *server, 1239 void __always_unused *data) 1240 { 1241 struct nfs_delegation *delegation; 1242 struct inode *inode; 1243 const struct cred *cred; 1244 nfs4_stateid stateid; 1245 restart: 1246 rcu_read_lock(); 1247 restart_locked: 1248 list_for_each_entry_rcu(delegation, &server->delegations, super_list) { 1249 if (test_bit(NFS_DELEGATION_INODE_FREEING, 1250 &delegation->flags) || 1251 test_bit(NFS_DELEGATION_RETURNING, 1252 &delegation->flags) || 1253 test_bit(NFS_DELEGATION_TEST_EXPIRED, 1254 &delegation->flags) == 0) 1255 continue; 1256 inode = nfs_delegation_grab_inode(delegation); 1257 if (inode == NULL) 1258 goto restart_locked; 1259 spin_lock(&delegation->lock); 1260 cred = get_cred_rcu(delegation->cred); 1261 nfs4_stateid_copy(&stateid, &delegation->stateid); 1262 spin_unlock(&delegation->lock); 1263 clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags); 1264 rcu_read_unlock(); 1265 nfs_delegation_test_free_expired(inode, &stateid, cred); 1266 put_cred(cred); 1267 if (!nfs4_server_rebooted(server->nfs_client)) { 1268 iput(inode); 1269 cond_resched(); 1270 goto restart; 1271 } 1272 nfs_inode_mark_test_expired_delegation(server,inode); 1273 iput(inode); 1274 return -EAGAIN; 1275 } 1276 rcu_read_unlock(); 1277 return 0; 1278 } 1279 1280 /** 1281 * nfs_reap_expired_delegations - reap expired delegations 1282 * @clp: nfs_client to process 1283 * 1284 * Iterates through all the delegations associated with this server and 1285 * checks if they have may have been revoked. This function is usually 1286 * expected to be called in cases where the server may have lost its 1287 * lease. 1288 */ 1289 void nfs_reap_expired_delegations(struct nfs_client *clp) 1290 { 1291 nfs_client_for_each_server(clp, nfs_server_reap_expired_delegations, 1292 NULL); 1293 } 1294 1295 void nfs_inode_find_delegation_state_and_recover(struct inode *inode, 1296 const nfs4_stateid *stateid) 1297 { 1298 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; 1299 struct nfs_delegation *delegation; 1300 bool found = false; 1301 1302 rcu_read_lock(); 1303 delegation = rcu_dereference(NFS_I(inode)->delegation); 1304 if (delegation && 1305 nfs4_stateid_match_or_older(&delegation->stateid, stateid) && 1306 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) { 1307 nfs_mark_test_expired_delegation(NFS_SERVER(inode), delegation); 1308 found = true; 1309 } 1310 rcu_read_unlock(); 1311 if (found) 1312 nfs4_schedule_state_manager(clp); 1313 } 1314 1315 /** 1316 * nfs_delegations_present - check for existence of delegations 1317 * @clp: client state handle 1318 * 1319 * Returns one if there are any nfs_delegation structures attached 1320 * to this nfs_client. 1321 */ 1322 int nfs_delegations_present(struct nfs_client *clp) 1323 { 1324 struct nfs_server *server; 1325 int ret = 0; 1326 1327 rcu_read_lock(); 1328 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) 1329 if (!list_empty(&server->delegations)) { 1330 ret = 1; 1331 break; 1332 } 1333 rcu_read_unlock(); 1334 return ret; 1335 } 1336 1337 /** 1338 * nfs4_refresh_delegation_stateid - Update delegation stateid seqid 1339 * @dst: stateid to refresh 1340 * @inode: inode to check 1341 * 1342 * Returns "true" and updates "dst->seqid" * if inode had a delegation 1343 * that matches our delegation stateid. Otherwise "false" is returned. 1344 */ 1345 bool nfs4_refresh_delegation_stateid(nfs4_stateid *dst, struct inode *inode) 1346 { 1347 struct nfs_delegation *delegation; 1348 bool ret = false; 1349 if (!inode) 1350 goto out; 1351 1352 rcu_read_lock(); 1353 delegation = rcu_dereference(NFS_I(inode)->delegation); 1354 if (delegation != NULL && 1355 nfs4_stateid_match_other(dst, &delegation->stateid) && 1356 nfs4_stateid_is_newer(&delegation->stateid, dst) && 1357 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) { 1358 dst->seqid = delegation->stateid.seqid; 1359 ret = true; 1360 } 1361 rcu_read_unlock(); 1362 out: 1363 return ret; 1364 } 1365 1366 /** 1367 * nfs4_copy_delegation_stateid - Copy inode's state ID information 1368 * @inode: inode to check 1369 * @flags: delegation type requirement 1370 * @dst: stateid data structure to fill in 1371 * @cred: optional argument to retrieve credential 1372 * 1373 * Returns "true" and fills in "dst->data" * if inode had a delegation, 1374 * otherwise "false" is returned. 1375 */ 1376 bool nfs4_copy_delegation_stateid(struct inode *inode, fmode_t flags, 1377 nfs4_stateid *dst, const struct cred **cred) 1378 { 1379 struct nfs_inode *nfsi = NFS_I(inode); 1380 struct nfs_delegation *delegation; 1381 bool ret = false; 1382 1383 flags &= FMODE_READ|FMODE_WRITE; 1384 rcu_read_lock(); 1385 delegation = rcu_dereference(nfsi->delegation); 1386 if (!delegation) 1387 goto out; 1388 spin_lock(&delegation->lock); 1389 ret = nfs4_is_valid_delegation(delegation, flags); 1390 if (ret) { 1391 nfs4_stateid_copy(dst, &delegation->stateid); 1392 nfs_mark_delegation_referenced(delegation); 1393 if (cred) 1394 *cred = get_cred(delegation->cred); 1395 } 1396 spin_unlock(&delegation->lock); 1397 out: 1398 rcu_read_unlock(); 1399 return ret; 1400 } 1401 1402 /** 1403 * nfs4_delegation_flush_on_close - Check if we must flush file on close 1404 * @inode: inode to check 1405 * 1406 * This function checks the number of outstanding writes to the file 1407 * against the delegation 'space_limit' field to see if 1408 * the spec requires us to flush the file on close. 1409 */ 1410 bool nfs4_delegation_flush_on_close(const struct inode *inode) 1411 { 1412 struct nfs_inode *nfsi = NFS_I(inode); 1413 struct nfs_delegation *delegation; 1414 bool ret = true; 1415 1416 rcu_read_lock(); 1417 delegation = rcu_dereference(nfsi->delegation); 1418 if (delegation == NULL || !(delegation->type & FMODE_WRITE)) 1419 goto out; 1420 if (atomic_long_read(&nfsi->nrequests) < delegation->pagemod_limit) 1421 ret = false; 1422 out: 1423 rcu_read_unlock(); 1424 return ret; 1425 } 1426 1427 module_param_named(delegation_watermark, nfs_delegation_watermark, uint, 0644); 1428