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