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