1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/fs/nfs/delegation.c
5 * Copyright (C) 2004 Trond Myklebust
7 * NFS file delegation management
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>
18 #include <linux/nfs4.h>
19 #include <linux/nfs_fs.h>
20 #include <linux/nfs_xdr.h>
23 #include "nfs4session.h"
24 #include "delegation.h"
26 #include "nfs4trace.h"
28 #define NFS_DEFAULT_DELEGATION_WATERMARK (5000U)
30 static atomic_long_t nfs_active_delegations;
31 static unsigned nfs_delegation_watermark = NFS_DEFAULT_DELEGATION_WATERMARK;
33 static void __nfs_free_delegation(struct nfs_delegation *delegation)
35 put_cred(delegation->cred);
36 delegation->cred = NULL;
37 kfree_rcu(delegation, rcu);
40 static void nfs_mark_delegation_revoked(struct nfs_delegation *delegation)
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);
50 static struct nfs_delegation *nfs_get_delegation(struct nfs_delegation *delegation)
52 refcount_inc(&delegation->refcount);
56 static void nfs_put_delegation(struct nfs_delegation *delegation)
58 if (refcount_dec_and_test(&delegation->refcount))
59 __nfs_free_delegation(delegation);
62 static void nfs_free_delegation(struct nfs_delegation *delegation)
64 nfs_mark_delegation_revoked(delegation);
65 nfs_put_delegation(delegation);
69 * nfs_mark_delegation_referenced - set delegation's REFERENCED flag
70 * @delegation: delegation to process
73 void nfs_mark_delegation_referenced(struct nfs_delegation *delegation)
75 set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags);
79 nfs4_is_valid_delegation(const struct nfs_delegation *delegation,
82 if (delegation != NULL && (delegation->type & flags) == flags &&
83 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) &&
84 !test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
89 struct nfs_delegation *nfs4_get_valid_delegation(const struct inode *inode)
91 struct nfs_delegation *delegation;
93 delegation = rcu_dereference(NFS_I(inode)->delegation);
94 if (nfs4_is_valid_delegation(delegation, 0))
100 nfs4_do_check_delegation(struct inode *inode, fmode_t flags, bool mark)
102 struct nfs_delegation *delegation;
105 flags &= FMODE_READ|FMODE_WRITE;
107 delegation = rcu_dereference(NFS_I(inode)->delegation);
108 if (nfs4_is_valid_delegation(delegation, flags)) {
110 nfs_mark_delegation_referenced(delegation);
117 * nfs4_have_delegation - check if inode has a delegation, mark it
118 * NFS_DELEGATION_REFERENCED if there is one.
119 * @inode: inode to check
120 * @flags: delegation types to check for
122 * Returns one if inode has the indicated delegation, otherwise zero.
124 int nfs4_have_delegation(struct inode *inode, fmode_t flags)
126 return nfs4_do_check_delegation(inode, flags, true);
130 * nfs4_check_delegation - check if inode has a delegation, do not mark
131 * NFS_DELEGATION_REFERENCED if it has one.
133 int nfs4_check_delegation(struct inode *inode, fmode_t flags)
135 return nfs4_do_check_delegation(inode, flags, false);
138 static int nfs_delegation_claim_locks(struct nfs4_state *state, const nfs4_stateid *stateid)
140 struct inode *inode = state->inode;
141 struct file_lock *fl;
142 struct file_lock_context *flctx = inode->i_flctx;
143 struct list_head *list;
149 list = &flctx->flc_posix;
150 spin_lock(&flctx->flc_lock);
152 list_for_each_entry(fl, list, fl_list) {
153 if (nfs_file_open_context(fl->fl_file)->state != state)
155 spin_unlock(&flctx->flc_lock);
156 status = nfs4_lock_delegation_recall(fl, state, stateid);
159 spin_lock(&flctx->flc_lock);
161 if (list == &flctx->flc_posix) {
162 list = &flctx->flc_flock;
165 spin_unlock(&flctx->flc_lock);
170 static int nfs_delegation_claim_opens(struct inode *inode,
171 const nfs4_stateid *stateid, fmode_t type)
173 struct nfs_inode *nfsi = NFS_I(inode);
174 struct nfs_open_context *ctx;
175 struct nfs4_state_owner *sp;
176 struct nfs4_state *state;
182 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
186 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
188 if (!nfs4_valid_open_stateid(state))
190 if (!nfs4_stateid_match(&state->stateid, stateid))
192 if (!get_nfs_open_context(ctx))
196 /* Block nfs4_proc_unlck */
197 mutex_lock(&sp->so_delegreturn_mutex);
198 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
199 err = nfs4_open_delegation_recall(ctx, state, stateid);
201 err = nfs_delegation_claim_locks(state, stateid);
202 if (!err && read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
204 mutex_unlock(&sp->so_delegreturn_mutex);
205 put_nfs_open_context(ctx);
215 * nfs_inode_reclaim_delegation - process a delegation reclaim request
216 * @inode: inode to process
217 * @cred: credential to use for request
218 * @type: delegation type
219 * @stateid: delegation stateid
220 * @pagemod_limit: write delegation "space_limit"
223 void nfs_inode_reclaim_delegation(struct inode *inode, const struct cred *cred,
225 const nfs4_stateid *stateid,
226 unsigned long pagemod_limit)
228 struct nfs_delegation *delegation;
229 const struct cred *oldcred = NULL;
232 delegation = rcu_dereference(NFS_I(inode)->delegation);
233 if (delegation != NULL) {
234 spin_lock(&delegation->lock);
235 if (nfs4_is_valid_delegation(delegation, 0)) {
236 nfs4_stateid_copy(&delegation->stateid, stateid);
237 delegation->type = type;
238 delegation->pagemod_limit = pagemod_limit;
239 oldcred = delegation->cred;
240 delegation->cred = get_cred(cred);
241 clear_bit(NFS_DELEGATION_NEED_RECLAIM,
243 spin_unlock(&delegation->lock);
246 trace_nfs4_reclaim_delegation(inode, type);
249 /* We appear to have raced with a delegation return. */
250 spin_unlock(&delegation->lock);
253 nfs_inode_set_delegation(inode, cred, type, stateid, pagemod_limit);
256 static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
258 const struct cred *cred;
261 if (!test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
262 spin_lock(&delegation->lock);
263 cred = get_cred(delegation->cred);
264 spin_unlock(&delegation->lock);
265 res = nfs4_proc_delegreturn(inode, cred,
266 &delegation->stateid,
273 static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
275 struct inode *inode = NULL;
277 spin_lock(&delegation->lock);
278 if (delegation->inode != NULL)
279 inode = igrab(delegation->inode);
281 set_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags);
282 spin_unlock(&delegation->lock);
286 static struct nfs_delegation *
287 nfs_start_delegation_return_locked(struct nfs_inode *nfsi)
289 struct nfs_delegation *ret = NULL;
290 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
292 if (delegation == NULL)
294 spin_lock(&delegation->lock);
295 if (!test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) {
296 /* Refcount matched in nfs_end_delegation_return() */
297 ret = nfs_get_delegation(delegation);
299 spin_unlock(&delegation->lock);
301 nfs_clear_verifier_delegated(&nfsi->vfs_inode);
306 static struct nfs_delegation *
307 nfs_start_delegation_return(struct nfs_inode *nfsi)
309 struct nfs_delegation *delegation;
312 delegation = nfs_start_delegation_return_locked(nfsi);
318 nfs_abort_delegation_return(struct nfs_delegation *delegation,
319 struct nfs_client *clp)
322 spin_lock(&delegation->lock);
323 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
324 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
325 spin_unlock(&delegation->lock);
326 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
329 static struct nfs_delegation *
330 nfs_detach_delegation_locked(struct nfs_inode *nfsi,
331 struct nfs_delegation *delegation,
332 struct nfs_client *clp)
334 struct nfs_delegation *deleg_cur =
335 rcu_dereference_protected(nfsi->delegation,
336 lockdep_is_held(&clp->cl_lock));
338 if (deleg_cur == NULL || delegation != deleg_cur)
341 spin_lock(&delegation->lock);
342 if (!delegation->inode) {
343 spin_unlock(&delegation->lock);
346 list_del_rcu(&delegation->super_list);
347 delegation->inode = NULL;
348 rcu_assign_pointer(nfsi->delegation, NULL);
349 spin_unlock(&delegation->lock);
353 static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi,
354 struct nfs_delegation *delegation,
355 struct nfs_server *server)
357 struct nfs_client *clp = server->nfs_client;
359 spin_lock(&clp->cl_lock);
360 delegation = nfs_detach_delegation_locked(nfsi, delegation, clp);
361 spin_unlock(&clp->cl_lock);
365 static struct nfs_delegation *
366 nfs_inode_detach_delegation(struct inode *inode)
368 struct nfs_inode *nfsi = NFS_I(inode);
369 struct nfs_server *server = NFS_SERVER(inode);
370 struct nfs_delegation *delegation;
373 delegation = rcu_dereference(nfsi->delegation);
374 if (delegation != NULL)
375 delegation = nfs_detach_delegation(nfsi, delegation, server);
381 nfs_update_delegation_cred(struct nfs_delegation *delegation,
382 const struct cred *cred)
384 const struct cred *old;
386 if (cred_fscmp(delegation->cred, cred) != 0) {
387 old = xchg(&delegation->cred, get_cred(cred));
393 nfs_update_inplace_delegation(struct nfs_delegation *delegation,
394 const struct nfs_delegation *update)
396 if (nfs4_stateid_is_newer(&update->stateid, &delegation->stateid)) {
397 delegation->stateid.seqid = update->stateid.seqid;
399 delegation->type = update->type;
400 delegation->pagemod_limit = update->pagemod_limit;
401 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
402 delegation->change_attr = update->change_attr;
403 nfs_update_delegation_cred(delegation, update->cred);
404 /* smp_mb__before_atomic() is implicit due to xchg() */
405 clear_bit(NFS_DELEGATION_REVOKED, &delegation->flags);
406 atomic_long_inc(&nfs_active_delegations);
412 * nfs_inode_set_delegation - set up a delegation on an inode
413 * @inode: inode to which delegation applies
414 * @cred: cred to use for subsequent delegation processing
415 * @type: delegation type
416 * @stateid: delegation stateid
417 * @pagemod_limit: write delegation "space_limit"
419 * Returns zero on success, or a negative errno value.
421 int nfs_inode_set_delegation(struct inode *inode, const struct cred *cred,
423 const nfs4_stateid *stateid,
424 unsigned long pagemod_limit)
426 struct nfs_server *server = NFS_SERVER(inode);
427 struct nfs_client *clp = server->nfs_client;
428 struct nfs_inode *nfsi = NFS_I(inode);
429 struct nfs_delegation *delegation, *old_delegation;
430 struct nfs_delegation *freeme = NULL;
433 delegation = kmalloc(sizeof(*delegation), GFP_NOFS);
434 if (delegation == NULL)
436 nfs4_stateid_copy(&delegation->stateid, stateid);
437 refcount_set(&delegation->refcount, 1);
438 delegation->type = type;
439 delegation->pagemod_limit = pagemod_limit;
440 delegation->change_attr = inode_peek_iversion_raw(inode);
441 delegation->cred = get_cred(cred);
442 delegation->inode = inode;
443 delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
444 spin_lock_init(&delegation->lock);
446 spin_lock(&clp->cl_lock);
447 old_delegation = rcu_dereference_protected(nfsi->delegation,
448 lockdep_is_held(&clp->cl_lock));
449 if (old_delegation == NULL)
451 /* Is this an update of the existing delegation? */
452 if (nfs4_stateid_match_other(&old_delegation->stateid,
453 &delegation->stateid)) {
454 spin_lock(&old_delegation->lock);
455 nfs_update_inplace_delegation(old_delegation,
457 spin_unlock(&old_delegation->lock);
460 if (!test_bit(NFS_DELEGATION_REVOKED, &old_delegation->flags)) {
462 * Deal with broken servers that hand out two
463 * delegations for the same file.
464 * Allow for upgrades to a WRITE delegation, but
467 dfprintk(FILE, "%s: server %s handed out "
468 "a duplicate delegation!\n",
469 __func__, clp->cl_hostname);
470 if (delegation->type == old_delegation->type ||
471 !(delegation->type & FMODE_WRITE)) {
476 if (test_and_set_bit(NFS_DELEGATION_RETURNING,
477 &old_delegation->flags))
480 freeme = nfs_detach_delegation_locked(nfsi, old_delegation, clp);
485 * If we didn't revalidate the change attribute before setting
486 * the delegation, then pre-emptively ask for a full attribute
487 * cache revalidation.
489 spin_lock(&inode->i_lock);
490 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_CHANGE)
491 nfs_set_cache_invalid(inode,
492 NFS_INO_INVALID_ATIME | NFS_INO_INVALID_CTIME |
493 NFS_INO_INVALID_MTIME | NFS_INO_INVALID_SIZE |
494 NFS_INO_INVALID_BLOCKS | NFS_INO_INVALID_NLINK |
495 NFS_INO_INVALID_OTHER | NFS_INO_INVALID_DATA |
496 NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL |
497 NFS_INO_INVALID_XATTR);
498 spin_unlock(&inode->i_lock);
500 list_add_tail_rcu(&delegation->super_list, &server->delegations);
501 rcu_assign_pointer(nfsi->delegation, delegation);
504 atomic_long_inc(&nfs_active_delegations);
506 trace_nfs4_set_delegation(inode, type);
508 spin_unlock(&clp->cl_lock);
509 if (delegation != NULL)
510 __nfs_free_delegation(delegation);
511 if (freeme != NULL) {
512 nfs_do_return_delegation(inode, freeme, 0);
513 nfs_free_delegation(freeme);
519 * Basic procedure for returning a delegation to the server
521 static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync)
523 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
526 if (delegation == NULL)
529 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
531 err = nfs_delegation_claim_opens(inode, &delegation->stateid,
533 if (!issync || err != -EAGAIN)
536 * Guard against state recovery
538 err = nfs4_wait_clnt_recover(clp);
542 nfs_abort_delegation_return(delegation, clp);
546 err = nfs_do_return_delegation(inode, delegation, issync);
548 /* Refcount matched in nfs_start_delegation_return_locked() */
549 nfs_put_delegation(delegation);
553 static bool nfs_delegation_need_return(struct nfs_delegation *delegation)
557 if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
559 else if (test_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags)) {
562 spin_lock(&delegation->lock);
563 inode = delegation->inode;
564 if (inode && list_empty(&NFS_I(inode)->open_files))
566 spin_unlock(&delegation->lock);
569 clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
570 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags) ||
571 test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
577 static int nfs_server_return_marked_delegations(struct nfs_server *server,
578 void __always_unused *data)
580 struct nfs_delegation *delegation;
581 struct nfs_delegation *prev;
583 struct inode *place_holder = NULL;
584 struct nfs_delegation *place_holder_deleg = NULL;
589 * To avoid quadratic looping we hold a reference
590 * to an inode place_holder. Each time we restart, we
591 * list delegation in the server from the delegations
593 * prev is an RCU-protected pointer to a delegation which
594 * wasn't marked for return and might be a good choice for
595 * the next place_holder.
601 delegation = rcu_dereference(NFS_I(place_holder)->delegation);
602 if (!delegation || delegation != place_holder_deleg)
603 delegation = list_entry_rcu(server->delegations.next,
604 struct nfs_delegation, super_list);
605 list_for_each_entry_from_rcu(delegation, &server->delegations, super_list) {
606 struct inode *to_put = NULL;
608 if (test_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags))
610 if (!nfs_delegation_need_return(delegation)) {
611 if (nfs4_is_valid_delegation(delegation, 0))
617 struct inode *tmp = nfs_delegation_grab_inode(prev);
619 to_put = place_holder;
621 place_holder_deleg = prev;
625 inode = nfs_delegation_grab_inode(delegation);
631 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
636 err = nfs_end_delegation_return(inode, delegation, 0);
641 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
651 * nfs_client_return_marked_delegations - return previously marked delegations
652 * @clp: nfs_client to process
654 * Note that this function is designed to be called by the state
655 * manager thread. For this reason, it cannot flush the dirty data,
656 * since that could deadlock in case of a state recovery error.
658 * Returns zero on success, or a negative errno value.
660 int nfs_client_return_marked_delegations(struct nfs_client *clp)
662 return nfs_client_for_each_server(clp,
663 nfs_server_return_marked_delegations, NULL);
667 * nfs_inode_evict_delegation - return delegation, don't reclaim opens
668 * @inode: inode to process
670 * Does not protect against delegation reclaims, therefore really only safe
671 * to be called from nfs4_clear_inode(). Guaranteed to always free
672 * the delegation structure.
674 void nfs_inode_evict_delegation(struct inode *inode)
676 struct nfs_delegation *delegation;
678 delegation = nfs_inode_detach_delegation(inode);
679 if (delegation != NULL) {
680 set_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
681 set_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags);
682 nfs_do_return_delegation(inode, delegation, 1);
683 nfs_free_delegation(delegation);
688 * nfs4_inode_return_delegation - synchronously return a delegation
689 * @inode: inode to process
691 * This routine will always flush any dirty data to disk on the
692 * assumption that if we need to return the delegation, then
693 * we should stop caching.
695 * Returns zero on success, or a negative errno value.
697 int nfs4_inode_return_delegation(struct inode *inode)
699 struct nfs_inode *nfsi = NFS_I(inode);
700 struct nfs_delegation *delegation;
704 delegation = nfs_start_delegation_return(nfsi);
705 if (delegation != NULL)
706 err = nfs_end_delegation_return(inode, delegation, 1);
711 * nfs4_inode_return_delegation_on_close - asynchronously return a delegation
712 * @inode: inode to process
714 * This routine is called on file close in order to determine if the
715 * inode delegation needs to be returned immediately.
717 void nfs4_inode_return_delegation_on_close(struct inode *inode)
719 struct nfs_delegation *delegation;
720 struct nfs_delegation *ret = NULL;
725 delegation = nfs4_get_valid_delegation(inode);
728 if (test_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags) ||
729 atomic_long_read(&nfs_active_delegations) >= nfs_delegation_watermark) {
730 spin_lock(&delegation->lock);
731 if (delegation->inode &&
732 list_empty(&NFS_I(inode)->open_files) &&
733 !test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) {
734 clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
735 /* Refcount matched in nfs_end_delegation_return() */
736 ret = nfs_get_delegation(delegation);
738 spin_unlock(&delegation->lock);
740 nfs_clear_verifier_delegated(inode);
744 nfs_end_delegation_return(inode, ret, 0);
748 * nfs4_inode_make_writeable
749 * @inode: pointer to inode
751 * Make the inode writeable by returning the delegation if necessary
753 * Returns zero on success, or a negative errno value.
755 int nfs4_inode_make_writeable(struct inode *inode)
757 struct nfs_delegation *delegation;
760 delegation = nfs4_get_valid_delegation(inode);
761 if (delegation == NULL ||
762 (nfs4_has_session(NFS_SERVER(inode)->nfs_client) &&
763 (delegation->type & FMODE_WRITE))) {
768 return nfs4_inode_return_delegation(inode);
771 static void nfs_mark_return_if_closed_delegation(struct nfs_server *server,
772 struct nfs_delegation *delegation)
774 set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
775 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
778 static void nfs_mark_return_delegation(struct nfs_server *server,
779 struct nfs_delegation *delegation)
781 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
782 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
785 static bool nfs_server_mark_return_all_delegations(struct nfs_server *server)
787 struct nfs_delegation *delegation;
790 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
791 nfs_mark_return_delegation(server, delegation);
797 static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
799 struct nfs_server *server;
802 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
803 nfs_server_mark_return_all_delegations(server);
807 static void nfs_delegation_run_state_manager(struct nfs_client *clp)
809 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
810 nfs4_schedule_state_manager(clp);
814 * nfs_expire_all_delegations
815 * @clp: client to process
818 void nfs_expire_all_delegations(struct nfs_client *clp)
820 nfs_client_mark_return_all_delegations(clp);
821 nfs_delegation_run_state_manager(clp);
825 * nfs_server_return_all_delegations - return delegations for one superblock
826 * @server: pointer to nfs_server to process
829 void nfs_server_return_all_delegations(struct nfs_server *server)
831 struct nfs_client *clp = server->nfs_client;
838 need_wait = nfs_server_mark_return_all_delegations(server);
842 nfs4_schedule_state_manager(clp);
843 nfs4_wait_clnt_recover(clp);
847 static void nfs_mark_return_unused_delegation_types(struct nfs_server *server,
850 struct nfs_delegation *delegation;
852 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
853 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
855 if (delegation->type & flags)
856 nfs_mark_return_if_closed_delegation(server, delegation);
860 static void nfs_client_mark_return_unused_delegation_types(struct nfs_client *clp,
863 struct nfs_server *server;
866 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
867 nfs_mark_return_unused_delegation_types(server, flags);
871 static void nfs_revoke_delegation(struct inode *inode,
872 const nfs4_stateid *stateid)
874 struct nfs_delegation *delegation;
879 delegation = rcu_dereference(NFS_I(inode)->delegation);
880 if (delegation == NULL)
882 if (stateid == NULL) {
883 nfs4_stateid_copy(&tmp, &delegation->stateid);
886 if (!nfs4_stateid_match_other(stateid, &delegation->stateid))
888 spin_lock(&delegation->lock);
889 if (stateid->seqid) {
890 if (nfs4_stateid_is_newer(&delegation->stateid, stateid)) {
891 spin_unlock(&delegation->lock);
894 delegation->stateid.seqid = stateid->seqid;
896 spin_unlock(&delegation->lock);
898 nfs_mark_delegation_revoked(delegation);
903 nfs_inode_find_state_and_recover(inode, stateid);
906 void nfs_remove_bad_delegation(struct inode *inode,
907 const nfs4_stateid *stateid)
909 nfs_revoke_delegation(inode, stateid);
911 EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation);
913 void nfs_delegation_mark_returned(struct inode *inode,
914 const nfs4_stateid *stateid)
916 struct nfs_delegation *delegation;
922 delegation = rcu_dereference(NFS_I(inode)->delegation);
926 spin_lock(&delegation->lock);
927 if (!nfs4_stateid_match_other(stateid, &delegation->stateid))
928 goto out_spin_unlock;
929 if (stateid->seqid) {
930 /* If delegation->stateid is newer, dont mark as returned */
931 if (nfs4_stateid_is_newer(&delegation->stateid, stateid))
932 goto out_clear_returning;
933 if (delegation->stateid.seqid != stateid->seqid)
934 delegation->stateid.seqid = stateid->seqid;
937 nfs_mark_delegation_revoked(delegation);
940 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
942 spin_unlock(&delegation->lock);
946 nfs_inode_find_state_and_recover(inode, stateid);
950 * nfs_expire_unused_delegation_types
951 * @clp: client to process
952 * @flags: delegation types to expire
955 void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags)
957 nfs_client_mark_return_unused_delegation_types(clp, flags);
958 nfs_delegation_run_state_manager(clp);
961 static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
963 struct nfs_delegation *delegation;
965 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
966 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
968 nfs_mark_return_if_closed_delegation(server, delegation);
973 * nfs_expire_unreferenced_delegations - Eliminate unused delegations
974 * @clp: nfs_client to process
977 void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
979 struct nfs_server *server;
982 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
983 nfs_mark_return_unreferenced_delegations(server);
986 nfs_delegation_run_state_manager(clp);
990 * nfs_async_inode_return_delegation - asynchronously return a delegation
991 * @inode: inode to process
992 * @stateid: state ID information
994 * Returns zero on success, or a negative errno value.
996 int nfs_async_inode_return_delegation(struct inode *inode,
997 const nfs4_stateid *stateid)
999 struct nfs_server *server = NFS_SERVER(inode);
1000 struct nfs_client *clp = server->nfs_client;
1001 struct nfs_delegation *delegation;
1004 delegation = nfs4_get_valid_delegation(inode);
1005 if (delegation == NULL)
1007 if (stateid != NULL &&
1008 !clp->cl_mvops->match_stateid(&delegation->stateid, stateid))
1010 nfs_mark_return_delegation(server, delegation);
1013 nfs_delegation_run_state_manager(clp);
1020 static struct inode *
1021 nfs_delegation_find_inode_server(struct nfs_server *server,
1022 const struct nfs_fh *fhandle)
1024 struct nfs_delegation *delegation;
1025 struct super_block *freeme = NULL;
1026 struct inode *res = NULL;
1028 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
1029 spin_lock(&delegation->lock);
1030 if (delegation->inode != NULL &&
1031 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) &&
1032 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
1033 if (nfs_sb_active(server->super)) {
1034 freeme = server->super;
1035 res = igrab(delegation->inode);
1037 spin_unlock(&delegation->lock);
1042 nfs_sb_deactive(freeme);
1045 return ERR_PTR(-EAGAIN);
1047 spin_unlock(&delegation->lock);
1049 return ERR_PTR(-ENOENT);
1053 * nfs_delegation_find_inode - retrieve the inode associated with a delegation
1054 * @clp: client state handle
1055 * @fhandle: filehandle from a delegation recall
1057 * Returns pointer to inode matching "fhandle," or NULL if a matching inode
1060 struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
1061 const struct nfs_fh *fhandle)
1063 struct nfs_server *server;
1067 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1068 res = nfs_delegation_find_inode_server(server, fhandle);
1069 if (res != ERR_PTR(-ENOENT)) {
1075 return ERR_PTR(-ENOENT);
1078 static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
1080 struct nfs_delegation *delegation;
1082 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
1084 * If the delegation may have been admin revoked, then we
1085 * cannot reclaim it.
1087 if (test_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags))
1089 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
1094 * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
1095 * @clp: nfs_client to process
1098 void nfs_delegation_mark_reclaim(struct nfs_client *clp)
1100 struct nfs_server *server;
1103 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1104 nfs_delegation_mark_reclaim_server(server);
1108 static int nfs_server_reap_unclaimed_delegations(struct nfs_server *server,
1109 void __always_unused *data)
1111 struct nfs_delegation *delegation;
1112 struct inode *inode;
1116 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
1117 if (test_bit(NFS_DELEGATION_INODE_FREEING,
1118 &delegation->flags) ||
1119 test_bit(NFS_DELEGATION_RETURNING,
1120 &delegation->flags) ||
1121 test_bit(NFS_DELEGATION_NEED_RECLAIM,
1122 &delegation->flags) == 0)
1124 inode = nfs_delegation_grab_inode(delegation);
1126 goto restart_locked;
1127 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
1129 if (delegation != NULL) {
1130 if (nfs_detach_delegation(NFS_I(inode), delegation,
1132 nfs_free_delegation(delegation);
1133 /* Match nfs_start_delegation_return_locked */
1134 nfs_put_delegation(delegation);
1145 * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
1146 * @clp: nfs_client to process
1149 void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
1151 nfs_client_for_each_server(clp, nfs_server_reap_unclaimed_delegations,
1155 static inline bool nfs4_server_rebooted(const struct nfs_client *clp)
1157 return (clp->cl_state & (BIT(NFS4CLNT_CHECK_LEASE) |
1158 BIT(NFS4CLNT_LEASE_EXPIRED) |
1159 BIT(NFS4CLNT_SESSION_RESET))) != 0;
1162 static void nfs_mark_test_expired_delegation(struct nfs_server *server,
1163 struct nfs_delegation *delegation)
1165 if (delegation->stateid.type == NFS4_INVALID_STATEID_TYPE)
1167 clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
1168 set_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
1169 set_bit(NFS4CLNT_DELEGATION_EXPIRED, &server->nfs_client->cl_state);
1172 static void nfs_inode_mark_test_expired_delegation(struct nfs_server *server,
1173 struct inode *inode)
1175 struct nfs_delegation *delegation;
1178 delegation = rcu_dereference(NFS_I(inode)->delegation);
1180 nfs_mark_test_expired_delegation(server, delegation);
1185 static void nfs_delegation_mark_test_expired_server(struct nfs_server *server)
1187 struct nfs_delegation *delegation;
1189 list_for_each_entry_rcu(delegation, &server->delegations, super_list)
1190 nfs_mark_test_expired_delegation(server, delegation);
1194 * nfs_mark_test_expired_all_delegations - mark all delegations for testing
1195 * @clp: nfs_client to process
1197 * Iterates through all the delegations associated with this server and
1198 * marks them as needing to be checked for validity.
1200 void nfs_mark_test_expired_all_delegations(struct nfs_client *clp)
1202 struct nfs_server *server;
1205 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1206 nfs_delegation_mark_test_expired_server(server);
1211 * nfs_test_expired_all_delegations - test all delegations for a client
1212 * @clp: nfs_client to process
1214 * Helper for handling "recallable state revoked" status from server.
1216 void nfs_test_expired_all_delegations(struct nfs_client *clp)
1218 nfs_mark_test_expired_all_delegations(clp);
1219 nfs4_schedule_state_manager(clp);
1223 nfs_delegation_test_free_expired(struct inode *inode,
1224 nfs4_stateid *stateid,
1225 const struct cred *cred)
1227 struct nfs_server *server = NFS_SERVER(inode);
1228 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
1233 status = ops->test_and_free_expired(server, stateid, cred);
1234 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
1235 nfs_remove_bad_delegation(inode, stateid);
1238 static int nfs_server_reap_expired_delegations(struct nfs_server *server,
1239 void __always_unused *data)
1241 struct nfs_delegation *delegation;
1242 struct inode *inode;
1243 const struct cred *cred;
1244 nfs4_stateid stateid;
1248 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
1249 if (test_bit(NFS_DELEGATION_INODE_FREEING,
1250 &delegation->flags) ||
1251 test_bit(NFS_DELEGATION_RETURNING,
1252 &delegation->flags) ||
1253 test_bit(NFS_DELEGATION_TEST_EXPIRED,
1254 &delegation->flags) == 0)
1256 inode = nfs_delegation_grab_inode(delegation);
1258 goto restart_locked;
1259 spin_lock(&delegation->lock);
1260 cred = get_cred_rcu(delegation->cred);
1261 nfs4_stateid_copy(&stateid, &delegation->stateid);
1262 spin_unlock(&delegation->lock);
1263 clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
1265 nfs_delegation_test_free_expired(inode, &stateid, cred);
1267 if (!nfs4_server_rebooted(server->nfs_client)) {
1272 nfs_inode_mark_test_expired_delegation(server,inode);
1281 * nfs_reap_expired_delegations - reap expired delegations
1282 * @clp: nfs_client to process
1284 * Iterates through all the delegations associated with this server and
1285 * checks if they have may have been revoked. This function is usually
1286 * expected to be called in cases where the server may have lost its
1289 void nfs_reap_expired_delegations(struct nfs_client *clp)
1291 nfs_client_for_each_server(clp, nfs_server_reap_expired_delegations,
1295 void nfs_inode_find_delegation_state_and_recover(struct inode *inode,
1296 const nfs4_stateid *stateid)
1298 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1299 struct nfs_delegation *delegation;
1303 delegation = rcu_dereference(NFS_I(inode)->delegation);
1305 nfs4_stateid_match_or_older(&delegation->stateid, stateid) &&
1306 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
1307 nfs_mark_test_expired_delegation(NFS_SERVER(inode), delegation);
1312 nfs4_schedule_state_manager(clp);
1316 * nfs_delegations_present - check for existence of delegations
1317 * @clp: client state handle
1319 * Returns one if there are any nfs_delegation structures attached
1320 * to this nfs_client.
1322 int nfs_delegations_present(struct nfs_client *clp)
1324 struct nfs_server *server;
1328 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1329 if (!list_empty(&server->delegations)) {
1338 * nfs4_refresh_delegation_stateid - Update delegation stateid seqid
1339 * @dst: stateid to refresh
1340 * @inode: inode to check
1342 * Returns "true" and updates "dst->seqid" * if inode had a delegation
1343 * that matches our delegation stateid. Otherwise "false" is returned.
1345 bool nfs4_refresh_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
1347 struct nfs_delegation *delegation;
1353 delegation = rcu_dereference(NFS_I(inode)->delegation);
1354 if (delegation != NULL &&
1355 nfs4_stateid_match_other(dst, &delegation->stateid) &&
1356 nfs4_stateid_is_newer(&delegation->stateid, dst) &&
1357 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
1358 dst->seqid = delegation->stateid.seqid;
1367 * nfs4_copy_delegation_stateid - Copy inode's state ID information
1368 * @inode: inode to check
1369 * @flags: delegation type requirement
1370 * @dst: stateid data structure to fill in
1371 * @cred: optional argument to retrieve credential
1373 * Returns "true" and fills in "dst->data" * if inode had a delegation,
1374 * otherwise "false" is returned.
1376 bool nfs4_copy_delegation_stateid(struct inode *inode, fmode_t flags,
1377 nfs4_stateid *dst, const struct cred **cred)
1379 struct nfs_inode *nfsi = NFS_I(inode);
1380 struct nfs_delegation *delegation;
1383 flags &= FMODE_READ|FMODE_WRITE;
1385 delegation = rcu_dereference(nfsi->delegation);
1388 spin_lock(&delegation->lock);
1389 ret = nfs4_is_valid_delegation(delegation, flags);
1391 nfs4_stateid_copy(dst, &delegation->stateid);
1392 nfs_mark_delegation_referenced(delegation);
1394 *cred = get_cred(delegation->cred);
1396 spin_unlock(&delegation->lock);
1403 * nfs4_delegation_flush_on_close - Check if we must flush file on close
1404 * @inode: inode to check
1406 * This function checks the number of outstanding writes to the file
1407 * against the delegation 'space_limit' field to see if
1408 * the spec requires us to flush the file on close.
1410 bool nfs4_delegation_flush_on_close(const struct inode *inode)
1412 struct nfs_inode *nfsi = NFS_I(inode);
1413 struct nfs_delegation *delegation;
1417 delegation = rcu_dereference(nfsi->delegation);
1418 if (delegation == NULL || !(delegation->type & FMODE_WRITE))
1420 if (atomic_long_read(&nfsi->nrequests) < delegation->pagemod_limit)
1427 module_param_named(delegation_watermark, nfs_delegation_watermark, uint, 0644);