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 * nfs_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_inplace_delegation(struct nfs_delegation *delegation, 382 const struct nfs_delegation *update) 383 { 384 if (nfs4_stateid_is_newer(&update->stateid, &delegation->stateid)) { 385 delegation->stateid.seqid = update->stateid.seqid; 386 smp_wmb(); 387 delegation->type = update->type; 388 if (test_and_clear_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) 389 atomic_long_inc(&nfs_active_delegations); 390 } 391 } 392 393 /** 394 * nfs_inode_set_delegation - set up a delegation on an inode 395 * @inode: inode to which delegation applies 396 * @cred: cred to use for subsequent delegation processing 397 * @type: delegation type 398 * @stateid: delegation stateid 399 * @pagemod_limit: write delegation "space_limit" 400 * 401 * Returns zero on success, or a negative errno value. 402 */ 403 int nfs_inode_set_delegation(struct inode *inode, const struct cred *cred, 404 fmode_t type, 405 const nfs4_stateid *stateid, 406 unsigned long pagemod_limit) 407 { 408 struct nfs_server *server = NFS_SERVER(inode); 409 struct nfs_client *clp = server->nfs_client; 410 struct nfs_inode *nfsi = NFS_I(inode); 411 struct nfs_delegation *delegation, *old_delegation; 412 struct nfs_delegation *freeme = NULL; 413 int status = 0; 414 415 delegation = kmalloc(sizeof(*delegation), GFP_NOFS); 416 if (delegation == NULL) 417 return -ENOMEM; 418 nfs4_stateid_copy(&delegation->stateid, stateid); 419 refcount_set(&delegation->refcount, 1); 420 delegation->type = type; 421 delegation->pagemod_limit = pagemod_limit; 422 delegation->change_attr = inode_peek_iversion_raw(inode); 423 delegation->cred = get_cred(cred); 424 delegation->inode = inode; 425 delegation->flags = 1<<NFS_DELEGATION_REFERENCED; 426 spin_lock_init(&delegation->lock); 427 428 spin_lock(&clp->cl_lock); 429 old_delegation = rcu_dereference_protected(nfsi->delegation, 430 lockdep_is_held(&clp->cl_lock)); 431 if (old_delegation == NULL) 432 goto add_new; 433 /* Is this an update of the existing delegation? */ 434 if (nfs4_stateid_match_other(&old_delegation->stateid, 435 &delegation->stateid)) { 436 spin_lock(&old_delegation->lock); 437 nfs_update_inplace_delegation(old_delegation, 438 delegation); 439 spin_unlock(&old_delegation->lock); 440 goto out; 441 } 442 if (!test_bit(NFS_DELEGATION_REVOKED, &old_delegation->flags)) { 443 /* 444 * Deal with broken servers that hand out two 445 * delegations for the same file. 446 * Allow for upgrades to a WRITE delegation, but 447 * nothing else. 448 */ 449 dfprintk(FILE, "%s: server %s handed out " 450 "a duplicate delegation!\n", 451 __func__, clp->cl_hostname); 452 if (delegation->type == old_delegation->type || 453 !(delegation->type & FMODE_WRITE)) { 454 freeme = delegation; 455 delegation = NULL; 456 goto out; 457 } 458 if (test_and_set_bit(NFS_DELEGATION_RETURNING, 459 &old_delegation->flags)) 460 goto out; 461 } 462 freeme = nfs_detach_delegation_locked(nfsi, old_delegation, clp); 463 if (freeme == NULL) 464 goto out; 465 add_new: 466 list_add_tail_rcu(&delegation->super_list, &server->delegations); 467 rcu_assign_pointer(nfsi->delegation, delegation); 468 delegation = NULL; 469 470 atomic_long_inc(&nfs_active_delegations); 471 472 trace_nfs4_set_delegation(inode, type); 473 474 spin_lock(&inode->i_lock); 475 if (NFS_I(inode)->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ATIME)) 476 NFS_I(inode)->cache_validity |= NFS_INO_REVAL_FORCED; 477 spin_unlock(&inode->i_lock); 478 out: 479 spin_unlock(&clp->cl_lock); 480 if (delegation != NULL) 481 __nfs_free_delegation(delegation); 482 if (freeme != NULL) { 483 nfs_do_return_delegation(inode, freeme, 0); 484 nfs_free_delegation(freeme); 485 } 486 return status; 487 } 488 489 /* 490 * Basic procedure for returning a delegation to the server 491 */ 492 static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync) 493 { 494 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; 495 int err = 0; 496 497 if (delegation == NULL) 498 return 0; 499 do { 500 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) 501 break; 502 err = nfs_delegation_claim_opens(inode, &delegation->stateid, 503 delegation->type); 504 if (!issync || err != -EAGAIN) 505 break; 506 /* 507 * Guard against state recovery 508 */ 509 err = nfs4_wait_clnt_recover(clp); 510 } while (err == 0); 511 512 if (err) { 513 nfs_abort_delegation_return(delegation, clp); 514 goto out; 515 } 516 517 err = nfs_do_return_delegation(inode, delegation, issync); 518 out: 519 /* Refcount matched in nfs_start_delegation_return_locked() */ 520 nfs_put_delegation(delegation); 521 return err; 522 } 523 524 static bool nfs_delegation_need_return(struct nfs_delegation *delegation) 525 { 526 bool ret = false; 527 528 if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags)) 529 ret = true; 530 else if (test_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags)) { 531 struct inode *inode; 532 533 spin_lock(&delegation->lock); 534 inode = delegation->inode; 535 if (inode && list_empty(&NFS_I(inode)->open_files)) 536 ret = true; 537 spin_unlock(&delegation->lock); 538 } 539 if (ret) 540 clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags); 541 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags) || 542 test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) 543 ret = false; 544 545 return ret; 546 } 547 548 /** 549 * nfs_client_return_marked_delegations - return previously marked delegations 550 * @clp: nfs_client to process 551 * 552 * Note that this function is designed to be called by the state 553 * manager thread. For this reason, it cannot flush the dirty data, 554 * since that could deadlock in case of a state recovery error. 555 * 556 * Returns zero on success, or a negative errno value. 557 */ 558 int nfs_client_return_marked_delegations(struct nfs_client *clp) 559 { 560 struct nfs_delegation *delegation; 561 struct nfs_delegation *prev; 562 struct nfs_server *server; 563 struct inode *inode; 564 struct inode *place_holder = NULL; 565 struct nfs_delegation *place_holder_deleg = NULL; 566 int err = 0; 567 568 restart: 569 /* 570 * To avoid quadratic looping we hold a reference 571 * to an inode place_holder. Each time we restart, we 572 * list nfs_servers from the server of that inode, and 573 * delegation in the server from the delegations of that 574 * inode. 575 * prev is an RCU-protected pointer to a delegation which 576 * wasn't marked for return and might be a good choice for 577 * the next place_holder. 578 */ 579 rcu_read_lock(); 580 prev = NULL; 581 if (place_holder) 582 server = NFS_SERVER(place_holder); 583 else 584 server = list_entry_rcu(clp->cl_superblocks.next, 585 struct nfs_server, client_link); 586 list_for_each_entry_from_rcu(server, &clp->cl_superblocks, client_link) { 587 delegation = NULL; 588 if (place_holder && server == NFS_SERVER(place_holder)) 589 delegation = rcu_dereference(NFS_I(place_holder)->delegation); 590 if (!delegation || delegation != place_holder_deleg) 591 delegation = list_entry_rcu(server->delegations.next, 592 struct nfs_delegation, super_list); 593 list_for_each_entry_from_rcu(delegation, &server->delegations, super_list) { 594 struct inode *to_put = NULL; 595 596 if (!nfs_delegation_need_return(delegation)) { 597 prev = delegation; 598 continue; 599 } 600 if (!nfs_sb_active(server->super)) 601 break; /* continue in outer loop */ 602 603 if (prev) { 604 struct inode *tmp; 605 606 tmp = nfs_delegation_grab_inode(prev); 607 if (tmp) { 608 to_put = place_holder; 609 place_holder = tmp; 610 place_holder_deleg = prev; 611 } 612 } 613 614 inode = nfs_delegation_grab_inode(delegation); 615 if (inode == NULL) { 616 rcu_read_unlock(); 617 if (to_put) 618 iput(to_put); 619 nfs_sb_deactive(server->super); 620 goto restart; 621 } 622 delegation = nfs_start_delegation_return_locked(NFS_I(inode)); 623 rcu_read_unlock(); 624 625 if (to_put) 626 iput(to_put); 627 628 err = nfs_end_delegation_return(inode, delegation, 0); 629 iput(inode); 630 nfs_sb_deactive(server->super); 631 cond_resched(); 632 if (!err) 633 goto restart; 634 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state); 635 if (place_holder) 636 iput(place_holder); 637 return err; 638 } 639 } 640 rcu_read_unlock(); 641 if (place_holder) 642 iput(place_holder); 643 return 0; 644 } 645 646 /** 647 * nfs_inode_evict_delegation - return delegation, don't reclaim opens 648 * @inode: inode to process 649 * 650 * Does not protect against delegation reclaims, therefore really only safe 651 * to be called from nfs4_clear_inode(). Guaranteed to always free 652 * the delegation structure. 653 */ 654 void nfs_inode_evict_delegation(struct inode *inode) 655 { 656 struct nfs_delegation *delegation; 657 658 delegation = nfs_inode_detach_delegation(inode); 659 if (delegation != NULL) { 660 set_bit(NFS_DELEGATION_RETURNING, &delegation->flags); 661 set_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags); 662 nfs_do_return_delegation(inode, delegation, 1); 663 nfs_free_delegation(delegation); 664 } 665 } 666 667 /** 668 * nfs_inode_return_delegation - synchronously return a delegation 669 * @inode: inode to process 670 * 671 * This routine will always flush any dirty data to disk on the 672 * assumption that if we need to return the delegation, then 673 * we should stop caching. 674 * 675 * Returns zero on success, or a negative errno value. 676 */ 677 int nfs4_inode_return_delegation(struct inode *inode) 678 { 679 struct nfs_inode *nfsi = NFS_I(inode); 680 struct nfs_delegation *delegation; 681 int err = 0; 682 683 nfs_wb_all(inode); 684 delegation = nfs_start_delegation_return(nfsi); 685 if (delegation != NULL) 686 err = nfs_end_delegation_return(inode, delegation, 1); 687 return err; 688 } 689 690 /** 691 * nfs_inode_return_delegation_on_close - asynchronously return a delegation 692 * @inode: inode to process 693 * 694 * This routine is called on file close in order to determine if the 695 * inode delegation needs to be returned immediately. 696 */ 697 void nfs4_inode_return_delegation_on_close(struct inode *inode) 698 { 699 struct nfs_delegation *delegation; 700 struct nfs_delegation *ret = NULL; 701 702 if (!inode) 703 return; 704 rcu_read_lock(); 705 delegation = nfs4_get_valid_delegation(inode); 706 if (!delegation) 707 goto out; 708 if (test_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags) || 709 atomic_long_read(&nfs_active_delegations) >= nfs_delegation_watermark) { 710 spin_lock(&delegation->lock); 711 if (delegation->inode && 712 list_empty(&NFS_I(inode)->open_files) && 713 !test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) { 714 clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags); 715 /* Refcount matched in nfs_end_delegation_return() */ 716 ret = nfs_get_delegation(delegation); 717 } 718 spin_unlock(&delegation->lock); 719 if (ret) 720 nfs_clear_verifier_delegated(inode); 721 } 722 out: 723 rcu_read_unlock(); 724 nfs_end_delegation_return(inode, ret, 0); 725 } 726 727 /** 728 * nfs4_inode_make_writeable 729 * @inode: pointer to inode 730 * 731 * Make the inode writeable by returning the delegation if necessary 732 * 733 * Returns zero on success, or a negative errno value. 734 */ 735 int nfs4_inode_make_writeable(struct inode *inode) 736 { 737 struct nfs_delegation *delegation; 738 739 rcu_read_lock(); 740 delegation = nfs4_get_valid_delegation(inode); 741 if (delegation == NULL || 742 (nfs4_has_session(NFS_SERVER(inode)->nfs_client) && 743 (delegation->type & FMODE_WRITE))) { 744 rcu_read_unlock(); 745 return 0; 746 } 747 rcu_read_unlock(); 748 return nfs4_inode_return_delegation(inode); 749 } 750 751 static void nfs_mark_return_if_closed_delegation(struct nfs_server *server, 752 struct nfs_delegation *delegation) 753 { 754 set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags); 755 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state); 756 } 757 758 static void nfs_mark_return_delegation(struct nfs_server *server, 759 struct nfs_delegation *delegation) 760 { 761 set_bit(NFS_DELEGATION_RETURN, &delegation->flags); 762 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state); 763 } 764 765 static bool nfs_server_mark_return_all_delegations(struct nfs_server *server) 766 { 767 struct nfs_delegation *delegation; 768 bool ret = false; 769 770 list_for_each_entry_rcu(delegation, &server->delegations, super_list) { 771 nfs_mark_return_delegation(server, delegation); 772 ret = true; 773 } 774 return ret; 775 } 776 777 static void nfs_client_mark_return_all_delegations(struct nfs_client *clp) 778 { 779 struct nfs_server *server; 780 781 rcu_read_lock(); 782 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) 783 nfs_server_mark_return_all_delegations(server); 784 rcu_read_unlock(); 785 } 786 787 static void nfs_delegation_run_state_manager(struct nfs_client *clp) 788 { 789 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) 790 nfs4_schedule_state_manager(clp); 791 } 792 793 /** 794 * nfs_expire_all_delegations 795 * @clp: client to process 796 * 797 */ 798 void nfs_expire_all_delegations(struct nfs_client *clp) 799 { 800 nfs_client_mark_return_all_delegations(clp); 801 nfs_delegation_run_state_manager(clp); 802 } 803 804 /** 805 * nfs_super_return_all_delegations - return delegations for one superblock 806 * @server: pointer to nfs_server to process 807 * 808 */ 809 void nfs_server_return_all_delegations(struct nfs_server *server) 810 { 811 struct nfs_client *clp = server->nfs_client; 812 bool need_wait; 813 814 if (clp == NULL) 815 return; 816 817 rcu_read_lock(); 818 need_wait = nfs_server_mark_return_all_delegations(server); 819 rcu_read_unlock(); 820 821 if (need_wait) { 822 nfs4_schedule_state_manager(clp); 823 nfs4_wait_clnt_recover(clp); 824 } 825 } 826 827 static void nfs_mark_return_unused_delegation_types(struct nfs_server *server, 828 fmode_t flags) 829 { 830 struct nfs_delegation *delegation; 831 832 list_for_each_entry_rcu(delegation, &server->delegations, super_list) { 833 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE)) 834 continue; 835 if (delegation->type & flags) 836 nfs_mark_return_if_closed_delegation(server, delegation); 837 } 838 } 839 840 static void nfs_client_mark_return_unused_delegation_types(struct nfs_client *clp, 841 fmode_t flags) 842 { 843 struct nfs_server *server; 844 845 rcu_read_lock(); 846 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) 847 nfs_mark_return_unused_delegation_types(server, flags); 848 rcu_read_unlock(); 849 } 850 851 static void nfs_revoke_delegation(struct inode *inode, 852 const nfs4_stateid *stateid) 853 { 854 struct nfs_delegation *delegation; 855 nfs4_stateid tmp; 856 bool ret = false; 857 858 rcu_read_lock(); 859 delegation = rcu_dereference(NFS_I(inode)->delegation); 860 if (delegation == NULL) 861 goto out; 862 if (stateid == NULL) { 863 nfs4_stateid_copy(&tmp, &delegation->stateid); 864 stateid = &tmp; 865 } else { 866 if (!nfs4_stateid_match_other(stateid, &delegation->stateid)) 867 goto out; 868 spin_lock(&delegation->lock); 869 if (stateid->seqid) { 870 if (nfs4_stateid_is_newer(&delegation->stateid, stateid)) { 871 spin_unlock(&delegation->lock); 872 goto out; 873 } 874 delegation->stateid.seqid = stateid->seqid; 875 } 876 spin_unlock(&delegation->lock); 877 } 878 nfs_mark_delegation_revoked(delegation); 879 ret = true; 880 out: 881 rcu_read_unlock(); 882 if (ret) 883 nfs_inode_find_state_and_recover(inode, stateid); 884 } 885 886 void nfs_remove_bad_delegation(struct inode *inode, 887 const nfs4_stateid *stateid) 888 { 889 nfs_revoke_delegation(inode, stateid); 890 } 891 EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation); 892 893 void nfs_delegation_mark_returned(struct inode *inode, 894 const nfs4_stateid *stateid) 895 { 896 struct nfs_delegation *delegation; 897 898 if (!inode) 899 return; 900 901 rcu_read_lock(); 902 delegation = rcu_dereference(NFS_I(inode)->delegation); 903 if (!delegation) 904 goto out_rcu_unlock; 905 906 spin_lock(&delegation->lock); 907 if (!nfs4_stateid_match_other(stateid, &delegation->stateid)) 908 goto out_spin_unlock; 909 if (stateid->seqid) { 910 /* If delegation->stateid is newer, dont mark as returned */ 911 if (nfs4_stateid_is_newer(&delegation->stateid, stateid)) 912 goto out_clear_returning; 913 if (delegation->stateid.seqid != stateid->seqid) 914 delegation->stateid.seqid = stateid->seqid; 915 } 916 917 nfs_mark_delegation_revoked(delegation); 918 919 out_clear_returning: 920 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags); 921 out_spin_unlock: 922 spin_unlock(&delegation->lock); 923 out_rcu_unlock: 924 rcu_read_unlock(); 925 926 nfs_inode_find_state_and_recover(inode, stateid); 927 } 928 929 /** 930 * nfs_expire_unused_delegation_types 931 * @clp: client to process 932 * @flags: delegation types to expire 933 * 934 */ 935 void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags) 936 { 937 nfs_client_mark_return_unused_delegation_types(clp, flags); 938 nfs_delegation_run_state_manager(clp); 939 } 940 941 static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server) 942 { 943 struct nfs_delegation *delegation; 944 945 list_for_each_entry_rcu(delegation, &server->delegations, super_list) { 946 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags)) 947 continue; 948 nfs_mark_return_if_closed_delegation(server, delegation); 949 } 950 } 951 952 /** 953 * nfs_expire_unreferenced_delegations - Eliminate unused delegations 954 * @clp: nfs_client to process 955 * 956 */ 957 void nfs_expire_unreferenced_delegations(struct nfs_client *clp) 958 { 959 struct nfs_server *server; 960 961 rcu_read_lock(); 962 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) 963 nfs_mark_return_unreferenced_delegations(server); 964 rcu_read_unlock(); 965 966 nfs_delegation_run_state_manager(clp); 967 } 968 969 /** 970 * nfs_async_inode_return_delegation - asynchronously return a delegation 971 * @inode: inode to process 972 * @stateid: state ID information 973 * 974 * Returns zero on success, or a negative errno value. 975 */ 976 int nfs_async_inode_return_delegation(struct inode *inode, 977 const nfs4_stateid *stateid) 978 { 979 struct nfs_server *server = NFS_SERVER(inode); 980 struct nfs_client *clp = server->nfs_client; 981 struct nfs_delegation *delegation; 982 983 rcu_read_lock(); 984 delegation = nfs4_get_valid_delegation(inode); 985 if (delegation == NULL) 986 goto out_enoent; 987 if (stateid != NULL && 988 !clp->cl_mvops->match_stateid(&delegation->stateid, stateid)) 989 goto out_enoent; 990 nfs_mark_return_delegation(server, delegation); 991 rcu_read_unlock(); 992 993 nfs_delegation_run_state_manager(clp); 994 return 0; 995 out_enoent: 996 rcu_read_unlock(); 997 return -ENOENT; 998 } 999 1000 static struct inode * 1001 nfs_delegation_find_inode_server(struct nfs_server *server, 1002 const struct nfs_fh *fhandle) 1003 { 1004 struct nfs_delegation *delegation; 1005 struct inode *freeme, *res = NULL; 1006 1007 list_for_each_entry_rcu(delegation, &server->delegations, super_list) { 1008 spin_lock(&delegation->lock); 1009 if (delegation->inode != NULL && 1010 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) && 1011 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) { 1012 freeme = igrab(delegation->inode); 1013 if (freeme && nfs_sb_active(freeme->i_sb)) 1014 res = freeme; 1015 spin_unlock(&delegation->lock); 1016 if (res != NULL) 1017 return res; 1018 if (freeme) { 1019 rcu_read_unlock(); 1020 iput(freeme); 1021 rcu_read_lock(); 1022 } 1023 return ERR_PTR(-EAGAIN); 1024 } 1025 spin_unlock(&delegation->lock); 1026 } 1027 return ERR_PTR(-ENOENT); 1028 } 1029 1030 /** 1031 * nfs_delegation_find_inode - retrieve the inode associated with a delegation 1032 * @clp: client state handle 1033 * @fhandle: filehandle from a delegation recall 1034 * 1035 * Returns pointer to inode matching "fhandle," or NULL if a matching inode 1036 * cannot be found. 1037 */ 1038 struct inode *nfs_delegation_find_inode(struct nfs_client *clp, 1039 const struct nfs_fh *fhandle) 1040 { 1041 struct nfs_server *server; 1042 struct inode *res; 1043 1044 rcu_read_lock(); 1045 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) { 1046 res = nfs_delegation_find_inode_server(server, fhandle); 1047 if (res != ERR_PTR(-ENOENT)) { 1048 rcu_read_unlock(); 1049 return res; 1050 } 1051 } 1052 rcu_read_unlock(); 1053 return ERR_PTR(-ENOENT); 1054 } 1055 1056 static void nfs_delegation_mark_reclaim_server(struct nfs_server *server) 1057 { 1058 struct nfs_delegation *delegation; 1059 1060 list_for_each_entry_rcu(delegation, &server->delegations, super_list) { 1061 /* 1062 * If the delegation may have been admin revoked, then we 1063 * cannot reclaim it. 1064 */ 1065 if (test_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags)) 1066 continue; 1067 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags); 1068 } 1069 } 1070 1071 /** 1072 * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed 1073 * @clp: nfs_client to process 1074 * 1075 */ 1076 void nfs_delegation_mark_reclaim(struct nfs_client *clp) 1077 { 1078 struct nfs_server *server; 1079 1080 rcu_read_lock(); 1081 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) 1082 nfs_delegation_mark_reclaim_server(server); 1083 rcu_read_unlock(); 1084 } 1085 1086 /** 1087 * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done 1088 * @clp: nfs_client to process 1089 * 1090 */ 1091 void nfs_delegation_reap_unclaimed(struct nfs_client *clp) 1092 { 1093 struct nfs_delegation *delegation; 1094 struct nfs_server *server; 1095 struct inode *inode; 1096 1097 restart: 1098 rcu_read_lock(); 1099 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) { 1100 list_for_each_entry_rcu(delegation, &server->delegations, 1101 super_list) { 1102 if (test_bit(NFS_DELEGATION_INODE_FREEING, 1103 &delegation->flags) || 1104 test_bit(NFS_DELEGATION_RETURNING, 1105 &delegation->flags) || 1106 test_bit(NFS_DELEGATION_NEED_RECLAIM, 1107 &delegation->flags) == 0) 1108 continue; 1109 if (!nfs_sb_active(server->super)) 1110 break; /* continue in outer loop */ 1111 inode = nfs_delegation_grab_inode(delegation); 1112 if (inode == NULL) { 1113 rcu_read_unlock(); 1114 nfs_sb_deactive(server->super); 1115 goto restart; 1116 } 1117 delegation = nfs_start_delegation_return_locked(NFS_I(inode)); 1118 rcu_read_unlock(); 1119 if (delegation != NULL) { 1120 if (nfs_detach_delegation(NFS_I(inode), delegation, 1121 server) != NULL) 1122 nfs_free_delegation(delegation); 1123 /* Match nfs_start_delegation_return_locked */ 1124 nfs_put_delegation(delegation); 1125 } 1126 iput(inode); 1127 nfs_sb_deactive(server->super); 1128 cond_resched(); 1129 goto restart; 1130 } 1131 } 1132 rcu_read_unlock(); 1133 } 1134 1135 static inline bool nfs4_server_rebooted(const struct nfs_client *clp) 1136 { 1137 return (clp->cl_state & (BIT(NFS4CLNT_CHECK_LEASE) | 1138 BIT(NFS4CLNT_LEASE_EXPIRED) | 1139 BIT(NFS4CLNT_SESSION_RESET))) != 0; 1140 } 1141 1142 static void nfs_mark_test_expired_delegation(struct nfs_server *server, 1143 struct nfs_delegation *delegation) 1144 { 1145 if (delegation->stateid.type == NFS4_INVALID_STATEID_TYPE) 1146 return; 1147 clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags); 1148 set_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags); 1149 set_bit(NFS4CLNT_DELEGATION_EXPIRED, &server->nfs_client->cl_state); 1150 } 1151 1152 static void nfs_inode_mark_test_expired_delegation(struct nfs_server *server, 1153 struct inode *inode) 1154 { 1155 struct nfs_delegation *delegation; 1156 1157 rcu_read_lock(); 1158 delegation = rcu_dereference(NFS_I(inode)->delegation); 1159 if (delegation) 1160 nfs_mark_test_expired_delegation(server, delegation); 1161 rcu_read_unlock(); 1162 1163 } 1164 1165 static void nfs_delegation_mark_test_expired_server(struct nfs_server *server) 1166 { 1167 struct nfs_delegation *delegation; 1168 1169 list_for_each_entry_rcu(delegation, &server->delegations, super_list) 1170 nfs_mark_test_expired_delegation(server, delegation); 1171 } 1172 1173 /** 1174 * nfs_mark_test_expired_all_delegations - mark all delegations for testing 1175 * @clp: nfs_client to process 1176 * 1177 * Iterates through all the delegations associated with this server and 1178 * marks them as needing to be checked for validity. 1179 */ 1180 void nfs_mark_test_expired_all_delegations(struct nfs_client *clp) 1181 { 1182 struct nfs_server *server; 1183 1184 rcu_read_lock(); 1185 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) 1186 nfs_delegation_mark_test_expired_server(server); 1187 rcu_read_unlock(); 1188 } 1189 1190 /** 1191 * nfs_test_expired_all_delegations - test all delegations for a client 1192 * @clp: nfs_client to process 1193 * 1194 * Helper for handling "recallable state revoked" status from server. 1195 */ 1196 void nfs_test_expired_all_delegations(struct nfs_client *clp) 1197 { 1198 nfs_mark_test_expired_all_delegations(clp); 1199 nfs4_schedule_state_manager(clp); 1200 } 1201 1202 static void 1203 nfs_delegation_test_free_expired(struct inode *inode, 1204 nfs4_stateid *stateid, 1205 const struct cred *cred) 1206 { 1207 struct nfs_server *server = NFS_SERVER(inode); 1208 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops; 1209 int status; 1210 1211 if (!cred) 1212 return; 1213 status = ops->test_and_free_expired(server, stateid, cred); 1214 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) 1215 nfs_remove_bad_delegation(inode, stateid); 1216 } 1217 1218 /** 1219 * nfs_reap_expired_delegations - reap expired delegations 1220 * @clp: nfs_client to process 1221 * 1222 * Iterates through all the delegations associated with this server and 1223 * checks if they have may have been revoked. This function is usually 1224 * expected to be called in cases where the server may have lost its 1225 * lease. 1226 */ 1227 void nfs_reap_expired_delegations(struct nfs_client *clp) 1228 { 1229 struct nfs_delegation *delegation; 1230 struct nfs_server *server; 1231 struct inode *inode; 1232 const struct cred *cred; 1233 nfs4_stateid stateid; 1234 1235 restart: 1236 rcu_read_lock(); 1237 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) { 1238 list_for_each_entry_rcu(delegation, &server->delegations, 1239 super_list) { 1240 if (test_bit(NFS_DELEGATION_INODE_FREEING, 1241 &delegation->flags) || 1242 test_bit(NFS_DELEGATION_RETURNING, 1243 &delegation->flags) || 1244 test_bit(NFS_DELEGATION_TEST_EXPIRED, 1245 &delegation->flags) == 0) 1246 continue; 1247 if (!nfs_sb_active(server->super)) 1248 break; /* continue in outer loop */ 1249 inode = nfs_delegation_grab_inode(delegation); 1250 if (inode == NULL) { 1251 rcu_read_unlock(); 1252 nfs_sb_deactive(server->super); 1253 goto restart; 1254 } 1255 cred = get_cred_rcu(delegation->cred); 1256 nfs4_stateid_copy(&stateid, &delegation->stateid); 1257 clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags); 1258 rcu_read_unlock(); 1259 nfs_delegation_test_free_expired(inode, &stateid, cred); 1260 put_cred(cred); 1261 if (nfs4_server_rebooted(clp)) { 1262 nfs_inode_mark_test_expired_delegation(server,inode); 1263 iput(inode); 1264 nfs_sb_deactive(server->super); 1265 return; 1266 } 1267 iput(inode); 1268 nfs_sb_deactive(server->super); 1269 cond_resched(); 1270 goto restart; 1271 } 1272 } 1273 rcu_read_unlock(); 1274 } 1275 1276 void nfs_inode_find_delegation_state_and_recover(struct inode *inode, 1277 const nfs4_stateid *stateid) 1278 { 1279 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; 1280 struct nfs_delegation *delegation; 1281 bool found = false; 1282 1283 rcu_read_lock(); 1284 delegation = rcu_dereference(NFS_I(inode)->delegation); 1285 if (delegation && 1286 nfs4_stateid_match_or_older(&delegation->stateid, stateid) && 1287 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) { 1288 nfs_mark_test_expired_delegation(NFS_SERVER(inode), delegation); 1289 found = true; 1290 } 1291 rcu_read_unlock(); 1292 if (found) 1293 nfs4_schedule_state_manager(clp); 1294 } 1295 1296 /** 1297 * nfs_delegations_present - check for existence of delegations 1298 * @clp: client state handle 1299 * 1300 * Returns one if there are any nfs_delegation structures attached 1301 * to this nfs_client. 1302 */ 1303 int nfs_delegations_present(struct nfs_client *clp) 1304 { 1305 struct nfs_server *server; 1306 int ret = 0; 1307 1308 rcu_read_lock(); 1309 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) 1310 if (!list_empty(&server->delegations)) { 1311 ret = 1; 1312 break; 1313 } 1314 rcu_read_unlock(); 1315 return ret; 1316 } 1317 1318 /** 1319 * nfs4_refresh_delegation_stateid - Update delegation stateid seqid 1320 * @dst: stateid to refresh 1321 * @inode: inode to check 1322 * 1323 * Returns "true" and updates "dst->seqid" * if inode had a delegation 1324 * that matches our delegation stateid. Otherwise "false" is returned. 1325 */ 1326 bool nfs4_refresh_delegation_stateid(nfs4_stateid *dst, struct inode *inode) 1327 { 1328 struct nfs_delegation *delegation; 1329 bool ret = false; 1330 if (!inode) 1331 goto out; 1332 1333 rcu_read_lock(); 1334 delegation = rcu_dereference(NFS_I(inode)->delegation); 1335 if (delegation != NULL && 1336 nfs4_stateid_match_other(dst, &delegation->stateid) && 1337 nfs4_stateid_is_newer(&delegation->stateid, dst) && 1338 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) { 1339 dst->seqid = delegation->stateid.seqid; 1340 ret = true; 1341 } 1342 rcu_read_unlock(); 1343 out: 1344 return ret; 1345 } 1346 1347 /** 1348 * nfs4_copy_delegation_stateid - Copy inode's state ID information 1349 * @inode: inode to check 1350 * @flags: delegation type requirement 1351 * @dst: stateid data structure to fill in 1352 * @cred: optional argument to retrieve credential 1353 * 1354 * Returns "true" and fills in "dst->data" * if inode had a delegation, 1355 * otherwise "false" is returned. 1356 */ 1357 bool nfs4_copy_delegation_stateid(struct inode *inode, fmode_t flags, 1358 nfs4_stateid *dst, const struct cred **cred) 1359 { 1360 struct nfs_inode *nfsi = NFS_I(inode); 1361 struct nfs_delegation *delegation; 1362 bool ret; 1363 1364 flags &= FMODE_READ|FMODE_WRITE; 1365 rcu_read_lock(); 1366 delegation = rcu_dereference(nfsi->delegation); 1367 ret = nfs4_is_valid_delegation(delegation, flags); 1368 if (ret) { 1369 nfs4_stateid_copy(dst, &delegation->stateid); 1370 nfs_mark_delegation_referenced(delegation); 1371 if (cred) 1372 *cred = get_cred(delegation->cred); 1373 } 1374 rcu_read_unlock(); 1375 return ret; 1376 } 1377 1378 /** 1379 * nfs4_delegation_flush_on_close - Check if we must flush file on close 1380 * @inode: inode to check 1381 * 1382 * This function checks the number of outstanding writes to the file 1383 * against the delegation 'space_limit' field to see if 1384 * the spec requires us to flush the file on close. 1385 */ 1386 bool nfs4_delegation_flush_on_close(const struct inode *inode) 1387 { 1388 struct nfs_inode *nfsi = NFS_I(inode); 1389 struct nfs_delegation *delegation; 1390 bool ret = true; 1391 1392 rcu_read_lock(); 1393 delegation = rcu_dereference(nfsi->delegation); 1394 if (delegation == NULL || !(delegation->type & FMODE_WRITE)) 1395 goto out; 1396 if (atomic_long_read(&nfsi->nrequests) < delegation->pagemod_limit) 1397 ret = false; 1398 out: 1399 rcu_read_unlock(); 1400 return ret; 1401 } 1402 1403 module_param_named(delegation_watermark, nfs_delegation_watermark, uint, 0644); 1404