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 void nfs_free_delegation(struct nfs_delegation *delegation)
52 nfs_mark_delegation_revoked(delegation);
53 __nfs_free_delegation(delegation);
57 * nfs_mark_delegation_referenced - set delegation's REFERENCED flag
58 * @delegation: delegation to process
61 void nfs_mark_delegation_referenced(struct nfs_delegation *delegation)
63 set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags);
67 nfs4_is_valid_delegation(const struct nfs_delegation *delegation,
70 if (delegation != NULL && (delegation->type & flags) == flags &&
71 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) &&
72 !test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
77 struct nfs_delegation *nfs4_get_valid_delegation(const struct inode *inode)
79 struct nfs_delegation *delegation;
81 delegation = rcu_dereference(NFS_I(inode)->delegation);
82 if (nfs4_is_valid_delegation(delegation, 0))
88 nfs4_do_check_delegation(struct inode *inode, fmode_t flags, bool mark)
90 struct nfs_delegation *delegation;
93 flags &= FMODE_READ|FMODE_WRITE;
95 delegation = rcu_dereference(NFS_I(inode)->delegation);
96 if (nfs4_is_valid_delegation(delegation, flags)) {
98 nfs_mark_delegation_referenced(delegation);
105 * nfs_have_delegation - check if inode has a delegation, mark it
106 * NFS_DELEGATION_REFERENCED if there is one.
107 * @inode: inode to check
108 * @flags: delegation types to check for
110 * Returns one if inode has the indicated delegation, otherwise zero.
112 int nfs4_have_delegation(struct inode *inode, fmode_t flags)
114 return nfs4_do_check_delegation(inode, flags, true);
118 * nfs4_check_delegation - check if inode has a delegation, do not mark
119 * NFS_DELEGATION_REFERENCED if it has one.
121 int nfs4_check_delegation(struct inode *inode, fmode_t flags)
123 return nfs4_do_check_delegation(inode, flags, false);
126 static int nfs_delegation_claim_locks(struct nfs4_state *state, const nfs4_stateid *stateid)
128 struct inode *inode = state->inode;
129 struct file_lock *fl;
130 struct file_lock_context *flctx = inode->i_flctx;
131 struct list_head *list;
137 list = &flctx->flc_posix;
138 spin_lock(&flctx->flc_lock);
140 list_for_each_entry(fl, list, fl_list) {
141 if (nfs_file_open_context(fl->fl_file)->state != state)
143 spin_unlock(&flctx->flc_lock);
144 status = nfs4_lock_delegation_recall(fl, state, stateid);
147 spin_lock(&flctx->flc_lock);
149 if (list == &flctx->flc_posix) {
150 list = &flctx->flc_flock;
153 spin_unlock(&flctx->flc_lock);
158 static int nfs_delegation_claim_opens(struct inode *inode,
159 const nfs4_stateid *stateid, fmode_t type)
161 struct nfs_inode *nfsi = NFS_I(inode);
162 struct nfs_open_context *ctx;
163 struct nfs4_state_owner *sp;
164 struct nfs4_state *state;
170 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
174 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
176 if (!nfs4_valid_open_stateid(state))
178 if (!nfs4_stateid_match(&state->stateid, stateid))
180 if (!get_nfs_open_context(ctx))
184 /* Block nfs4_proc_unlck */
185 mutex_lock(&sp->so_delegreturn_mutex);
186 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
187 err = nfs4_open_delegation_recall(ctx, state, stateid);
189 err = nfs_delegation_claim_locks(state, stateid);
190 if (!err && read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
192 mutex_unlock(&sp->so_delegreturn_mutex);
193 put_nfs_open_context(ctx);
203 * nfs_inode_reclaim_delegation - process a delegation reclaim request
204 * @inode: inode to process
205 * @cred: credential to use for request
206 * @type: delegation type
207 * @stateid: delegation stateid
208 * @pagemod_limit: write delegation "space_limit"
211 void nfs_inode_reclaim_delegation(struct inode *inode, const struct cred *cred,
213 const nfs4_stateid *stateid,
214 unsigned long pagemod_limit)
216 struct nfs_delegation *delegation;
217 const struct cred *oldcred = NULL;
220 delegation = rcu_dereference(NFS_I(inode)->delegation);
221 if (delegation != NULL) {
222 spin_lock(&delegation->lock);
223 if (nfs4_is_valid_delegation(delegation, 0)) {
224 nfs4_stateid_copy(&delegation->stateid, stateid);
225 delegation->type = type;
226 delegation->pagemod_limit = pagemod_limit;
227 oldcred = delegation->cred;
228 delegation->cred = get_cred(cred);
229 clear_bit(NFS_DELEGATION_NEED_RECLAIM,
231 spin_unlock(&delegation->lock);
234 trace_nfs4_reclaim_delegation(inode, type);
237 /* We appear to have raced with a delegation return. */
238 spin_unlock(&delegation->lock);
241 nfs_inode_set_delegation(inode, cred, type, stateid, pagemod_limit);
244 static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
248 if (!test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
249 res = nfs4_proc_delegreturn(inode,
251 &delegation->stateid,
256 static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
258 struct inode *inode = NULL;
260 spin_lock(&delegation->lock);
261 if (delegation->inode != NULL)
262 inode = igrab(delegation->inode);
264 set_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags);
265 spin_unlock(&delegation->lock);
269 static struct nfs_delegation *
270 nfs_start_delegation_return_locked(struct nfs_inode *nfsi)
272 struct nfs_delegation *ret = NULL;
273 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
275 if (delegation == NULL)
277 spin_lock(&delegation->lock);
278 if (!test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
280 spin_unlock(&delegation->lock);
282 nfs_clear_verifier_delegated(&nfsi->vfs_inode);
287 static struct nfs_delegation *
288 nfs_start_delegation_return(struct nfs_inode *nfsi)
290 struct nfs_delegation *delegation;
293 delegation = nfs_start_delegation_return_locked(nfsi);
299 nfs_abort_delegation_return(struct nfs_delegation *delegation,
300 struct nfs_client *clp)
303 spin_lock(&delegation->lock);
304 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
305 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
306 spin_unlock(&delegation->lock);
307 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
310 static struct nfs_delegation *
311 nfs_detach_delegation_locked(struct nfs_inode *nfsi,
312 struct nfs_delegation *delegation,
313 struct nfs_client *clp)
315 struct nfs_delegation *deleg_cur =
316 rcu_dereference_protected(nfsi->delegation,
317 lockdep_is_held(&clp->cl_lock));
319 if (deleg_cur == NULL || delegation != deleg_cur)
322 spin_lock(&delegation->lock);
323 if (!delegation->inode) {
324 spin_unlock(&delegation->lock);
327 list_del_rcu(&delegation->super_list);
328 delegation->inode = NULL;
329 rcu_assign_pointer(nfsi->delegation, NULL);
330 spin_unlock(&delegation->lock);
334 static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi,
335 struct nfs_delegation *delegation,
336 struct nfs_server *server)
338 struct nfs_client *clp = server->nfs_client;
340 spin_lock(&clp->cl_lock);
341 delegation = nfs_detach_delegation_locked(nfsi, delegation, clp);
342 spin_unlock(&clp->cl_lock);
346 static struct nfs_delegation *
347 nfs_inode_detach_delegation(struct inode *inode)
349 struct nfs_inode *nfsi = NFS_I(inode);
350 struct nfs_server *server = NFS_SERVER(inode);
351 struct nfs_delegation *delegation;
354 delegation = rcu_dereference(nfsi->delegation);
355 if (delegation != NULL)
356 delegation = nfs_detach_delegation(nfsi, delegation, server);
362 nfs_update_inplace_delegation(struct nfs_delegation *delegation,
363 const struct nfs_delegation *update)
365 if (nfs4_stateid_is_newer(&update->stateid, &delegation->stateid)) {
366 delegation->stateid.seqid = update->stateid.seqid;
368 delegation->type = update->type;
369 if (test_and_clear_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
370 atomic_long_inc(&nfs_active_delegations);
375 * nfs_inode_set_delegation - set up a delegation on an inode
376 * @inode: inode to which delegation applies
377 * @cred: cred to use for subsequent delegation processing
378 * @type: delegation type
379 * @stateid: delegation stateid
380 * @pagemod_limit: write delegation "space_limit"
382 * Returns zero on success, or a negative errno value.
384 int nfs_inode_set_delegation(struct inode *inode, const struct cred *cred,
386 const nfs4_stateid *stateid,
387 unsigned long pagemod_limit)
389 struct nfs_server *server = NFS_SERVER(inode);
390 struct nfs_client *clp = server->nfs_client;
391 struct nfs_inode *nfsi = NFS_I(inode);
392 struct nfs_delegation *delegation, *old_delegation;
393 struct nfs_delegation *freeme = NULL;
396 delegation = kmalloc(sizeof(*delegation), GFP_NOFS);
397 if (delegation == NULL)
399 nfs4_stateid_copy(&delegation->stateid, stateid);
400 delegation->type = type;
401 delegation->pagemod_limit = pagemod_limit;
402 delegation->change_attr = inode_peek_iversion_raw(inode);
403 delegation->cred = get_cred(cred);
404 delegation->inode = inode;
405 delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
406 spin_lock_init(&delegation->lock);
408 spin_lock(&clp->cl_lock);
409 old_delegation = rcu_dereference_protected(nfsi->delegation,
410 lockdep_is_held(&clp->cl_lock));
411 if (old_delegation == NULL)
413 /* Is this an update of the existing delegation? */
414 if (nfs4_stateid_match_other(&old_delegation->stateid,
415 &delegation->stateid)) {
416 spin_lock(&old_delegation->lock);
417 nfs_update_inplace_delegation(old_delegation,
419 spin_unlock(&old_delegation->lock);
422 if (!test_bit(NFS_DELEGATION_REVOKED, &old_delegation->flags)) {
424 * Deal with broken servers that hand out two
425 * delegations for the same file.
426 * Allow for upgrades to a WRITE delegation, but
429 dfprintk(FILE, "%s: server %s handed out "
430 "a duplicate delegation!\n",
431 __func__, clp->cl_hostname);
432 if (delegation->type == old_delegation->type ||
433 !(delegation->type & FMODE_WRITE)) {
438 if (test_and_set_bit(NFS_DELEGATION_RETURNING,
439 &old_delegation->flags))
442 freeme = nfs_detach_delegation_locked(nfsi, old_delegation, clp);
446 list_add_tail_rcu(&delegation->super_list, &server->delegations);
447 rcu_assign_pointer(nfsi->delegation, delegation);
450 atomic_long_inc(&nfs_active_delegations);
452 trace_nfs4_set_delegation(inode, type);
454 spin_lock(&inode->i_lock);
455 if (NFS_I(inode)->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ATIME))
456 NFS_I(inode)->cache_validity |= NFS_INO_REVAL_FORCED;
457 spin_unlock(&inode->i_lock);
459 spin_unlock(&clp->cl_lock);
460 if (delegation != NULL)
461 __nfs_free_delegation(delegation);
462 if (freeme != NULL) {
463 nfs_do_return_delegation(inode, freeme, 0);
464 nfs_free_delegation(freeme);
470 * Basic procedure for returning a delegation to the server
472 static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync)
474 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
477 if (delegation == NULL)
480 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
482 err = nfs_delegation_claim_opens(inode, &delegation->stateid,
484 if (!issync || err != -EAGAIN)
487 * Guard against state recovery
489 err = nfs4_wait_clnt_recover(clp);
493 nfs_abort_delegation_return(delegation, clp);
497 err = nfs_do_return_delegation(inode, delegation, issync);
502 static bool nfs_delegation_need_return(struct nfs_delegation *delegation)
506 if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
508 else if (test_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags)) {
511 spin_lock(&delegation->lock);
512 inode = delegation->inode;
513 if (inode && list_empty(&NFS_I(inode)->open_files))
515 spin_unlock(&delegation->lock);
518 clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
519 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags) ||
520 test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
527 * nfs_client_return_marked_delegations - return previously marked delegations
528 * @clp: nfs_client to process
530 * Note that this function is designed to be called by the state
531 * manager thread. For this reason, it cannot flush the dirty data,
532 * since that could deadlock in case of a state recovery error.
534 * Returns zero on success, or a negative errno value.
536 int nfs_client_return_marked_delegations(struct nfs_client *clp)
538 struct nfs_delegation *delegation;
539 struct nfs_delegation *prev;
540 struct nfs_server *server;
542 struct inode *place_holder = NULL;
543 struct nfs_delegation *place_holder_deleg = NULL;
548 * To avoid quadratic looping we hold a reference
549 * to an inode place_holder. Each time we restart, we
550 * list nfs_servers from the server of that inode, and
551 * delegation in the server from the delegations of that
553 * prev is an RCU-protected pointer to a delegation which
554 * wasn't marked for return and might be a good choice for
555 * the next place_holder.
560 server = NFS_SERVER(place_holder);
562 server = list_entry_rcu(clp->cl_superblocks.next,
563 struct nfs_server, client_link);
564 list_for_each_entry_from_rcu(server, &clp->cl_superblocks, client_link) {
566 if (place_holder && server == NFS_SERVER(place_holder))
567 delegation = rcu_dereference(NFS_I(place_holder)->delegation);
568 if (!delegation || delegation != place_holder_deleg)
569 delegation = list_entry_rcu(server->delegations.next,
570 struct nfs_delegation, super_list);
571 list_for_each_entry_from_rcu(delegation, &server->delegations, super_list) {
572 struct inode *to_put = NULL;
574 if (!nfs_delegation_need_return(delegation)) {
578 if (!nfs_sb_active(server->super))
579 break; /* continue in outer loop */
584 tmp = nfs_delegation_grab_inode(prev);
586 to_put = place_holder;
588 place_holder_deleg = prev;
592 inode = nfs_delegation_grab_inode(delegation);
597 nfs_sb_deactive(server->super);
600 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
606 err = nfs_end_delegation_return(inode, delegation, 0);
608 nfs_sb_deactive(server->super);
612 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
625 * nfs_inode_evict_delegation - return delegation, don't reclaim opens
626 * @inode: inode to process
628 * Does not protect against delegation reclaims, therefore really only safe
629 * to be called from nfs4_clear_inode(). Guaranteed to always free
630 * the delegation structure.
632 void nfs_inode_evict_delegation(struct inode *inode)
634 struct nfs_delegation *delegation;
636 delegation = nfs_inode_detach_delegation(inode);
637 if (delegation != NULL) {
638 set_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
639 set_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags);
640 nfs_do_return_delegation(inode, delegation, 1);
641 nfs_free_delegation(delegation);
646 * nfs_inode_return_delegation - synchronously return a delegation
647 * @inode: inode to process
649 * This routine will always flush any dirty data to disk on the
650 * assumption that if we need to return the delegation, then
651 * we should stop caching.
653 * Returns zero on success, or a negative errno value.
655 int nfs4_inode_return_delegation(struct inode *inode)
657 struct nfs_inode *nfsi = NFS_I(inode);
658 struct nfs_delegation *delegation;
662 delegation = nfs_start_delegation_return(nfsi);
663 if (delegation != NULL)
664 err = nfs_end_delegation_return(inode, delegation, 1);
669 * nfs_inode_return_delegation_on_close - asynchronously return a delegation
670 * @inode: inode to process
672 * This routine is called on file close in order to determine if the
673 * inode delegation needs to be returned immediately.
675 void nfs4_inode_return_delegation_on_close(struct inode *inode)
677 struct nfs_delegation *delegation;
678 struct nfs_delegation *ret = NULL;
683 delegation = nfs4_get_valid_delegation(inode);
686 if (test_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags) ||
687 atomic_long_read(&nfs_active_delegations) >= nfs_delegation_watermark) {
688 spin_lock(&delegation->lock);
689 if (delegation->inode &&
690 list_empty(&NFS_I(inode)->open_files) &&
691 !test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) {
692 clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
695 spin_unlock(&delegation->lock);
697 nfs_clear_verifier_delegated(inode);
701 nfs_end_delegation_return(inode, ret, 0);
705 * nfs4_inode_make_writeable
706 * @inode: pointer to inode
708 * Make the inode writeable by returning the delegation if necessary
710 * Returns zero on success, or a negative errno value.
712 int nfs4_inode_make_writeable(struct inode *inode)
714 struct nfs_delegation *delegation;
717 delegation = nfs4_get_valid_delegation(inode);
718 if (delegation == NULL ||
719 (nfs4_has_session(NFS_SERVER(inode)->nfs_client) &&
720 (delegation->type & FMODE_WRITE))) {
725 return nfs4_inode_return_delegation(inode);
728 static void nfs_mark_return_if_closed_delegation(struct nfs_server *server,
729 struct nfs_delegation *delegation)
731 set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
732 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
735 static void nfs_mark_return_delegation(struct nfs_server *server,
736 struct nfs_delegation *delegation)
738 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
739 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
742 static bool nfs_server_mark_return_all_delegations(struct nfs_server *server)
744 struct nfs_delegation *delegation;
747 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
748 nfs_mark_return_delegation(server, delegation);
754 static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
756 struct nfs_server *server;
759 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
760 nfs_server_mark_return_all_delegations(server);
764 static void nfs_delegation_run_state_manager(struct nfs_client *clp)
766 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
767 nfs4_schedule_state_manager(clp);
771 * nfs_expire_all_delegations
772 * @clp: client to process
775 void nfs_expire_all_delegations(struct nfs_client *clp)
777 nfs_client_mark_return_all_delegations(clp);
778 nfs_delegation_run_state_manager(clp);
782 * nfs_super_return_all_delegations - return delegations for one superblock
783 * @server: pointer to nfs_server to process
786 void nfs_server_return_all_delegations(struct nfs_server *server)
788 struct nfs_client *clp = server->nfs_client;
795 need_wait = nfs_server_mark_return_all_delegations(server);
799 nfs4_schedule_state_manager(clp);
800 nfs4_wait_clnt_recover(clp);
804 static void nfs_mark_return_unused_delegation_types(struct nfs_server *server,
807 struct nfs_delegation *delegation;
809 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
810 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
812 if (delegation->type & flags)
813 nfs_mark_return_if_closed_delegation(server, delegation);
817 static void nfs_client_mark_return_unused_delegation_types(struct nfs_client *clp,
820 struct nfs_server *server;
823 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
824 nfs_mark_return_unused_delegation_types(server, flags);
828 static void nfs_revoke_delegation(struct inode *inode,
829 const nfs4_stateid *stateid)
831 struct nfs_delegation *delegation;
836 delegation = rcu_dereference(NFS_I(inode)->delegation);
837 if (delegation == NULL)
839 if (stateid == NULL) {
840 nfs4_stateid_copy(&tmp, &delegation->stateid);
843 if (!nfs4_stateid_match_other(stateid, &delegation->stateid))
845 spin_lock(&delegation->lock);
846 if (stateid->seqid) {
847 if (nfs4_stateid_is_newer(&delegation->stateid, stateid)) {
848 spin_unlock(&delegation->lock);
851 delegation->stateid.seqid = stateid->seqid;
853 spin_unlock(&delegation->lock);
855 nfs_mark_delegation_revoked(delegation);
860 nfs_inode_find_state_and_recover(inode, stateid);
863 void nfs_remove_bad_delegation(struct inode *inode,
864 const nfs4_stateid *stateid)
866 nfs_revoke_delegation(inode, stateid);
868 EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation);
870 void nfs_delegation_mark_returned(struct inode *inode,
871 const nfs4_stateid *stateid)
873 struct nfs_delegation *delegation;
879 delegation = rcu_dereference(NFS_I(inode)->delegation);
883 spin_lock(&delegation->lock);
884 if (!nfs4_stateid_match_other(stateid, &delegation->stateid))
885 goto out_spin_unlock;
886 if (stateid->seqid) {
887 /* If delegation->stateid is newer, dont mark as returned */
888 if (nfs4_stateid_is_newer(&delegation->stateid, stateid))
889 goto out_clear_returning;
890 if (delegation->stateid.seqid != stateid->seqid)
891 delegation->stateid.seqid = stateid->seqid;
894 nfs_mark_delegation_revoked(delegation);
897 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
899 spin_unlock(&delegation->lock);
903 nfs_inode_find_state_and_recover(inode, stateid);
907 * nfs_expire_unused_delegation_types
908 * @clp: client to process
909 * @flags: delegation types to expire
912 void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags)
914 nfs_client_mark_return_unused_delegation_types(clp, flags);
915 nfs_delegation_run_state_manager(clp);
918 static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
920 struct nfs_delegation *delegation;
922 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
923 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
925 nfs_mark_return_if_closed_delegation(server, delegation);
930 * nfs_expire_unreferenced_delegations - Eliminate unused delegations
931 * @clp: nfs_client to process
934 void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
936 struct nfs_server *server;
939 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
940 nfs_mark_return_unreferenced_delegations(server);
943 nfs_delegation_run_state_manager(clp);
947 * nfs_async_inode_return_delegation - asynchronously return a delegation
948 * @inode: inode to process
949 * @stateid: state ID information
951 * Returns zero on success, or a negative errno value.
953 int nfs_async_inode_return_delegation(struct inode *inode,
954 const nfs4_stateid *stateid)
956 struct nfs_server *server = NFS_SERVER(inode);
957 struct nfs_client *clp = server->nfs_client;
958 struct nfs_delegation *delegation;
961 delegation = nfs4_get_valid_delegation(inode);
962 if (delegation == NULL)
964 if (stateid != NULL &&
965 !clp->cl_mvops->match_stateid(&delegation->stateid, stateid))
967 nfs_mark_return_delegation(server, delegation);
970 nfs_delegation_run_state_manager(clp);
977 static struct inode *
978 nfs_delegation_find_inode_server(struct nfs_server *server,
979 const struct nfs_fh *fhandle)
981 struct nfs_delegation *delegation;
982 struct inode *freeme, *res = NULL;
984 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
985 spin_lock(&delegation->lock);
986 if (delegation->inode != NULL &&
987 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) &&
988 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
989 freeme = igrab(delegation->inode);
990 if (freeme && nfs_sb_active(freeme->i_sb))
992 spin_unlock(&delegation->lock);
1000 return ERR_PTR(-EAGAIN);
1002 spin_unlock(&delegation->lock);
1004 return ERR_PTR(-ENOENT);
1008 * nfs_delegation_find_inode - retrieve the inode associated with a delegation
1009 * @clp: client state handle
1010 * @fhandle: filehandle from a delegation recall
1012 * Returns pointer to inode matching "fhandle," or NULL if a matching inode
1015 struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
1016 const struct nfs_fh *fhandle)
1018 struct nfs_server *server;
1022 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1023 res = nfs_delegation_find_inode_server(server, fhandle);
1024 if (res != ERR_PTR(-ENOENT)) {
1030 return ERR_PTR(-ENOENT);
1033 static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
1035 struct nfs_delegation *delegation;
1037 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
1039 * If the delegation may have been admin revoked, then we
1040 * cannot reclaim it.
1042 if (test_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags))
1044 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
1049 * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
1050 * @clp: nfs_client to process
1053 void nfs_delegation_mark_reclaim(struct nfs_client *clp)
1055 struct nfs_server *server;
1058 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1059 nfs_delegation_mark_reclaim_server(server);
1064 * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
1065 * @clp: nfs_client to process
1068 void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
1070 struct nfs_delegation *delegation;
1071 struct nfs_server *server;
1072 struct inode *inode;
1076 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1077 list_for_each_entry_rcu(delegation, &server->delegations,
1079 if (test_bit(NFS_DELEGATION_INODE_FREEING,
1080 &delegation->flags) ||
1081 test_bit(NFS_DELEGATION_RETURNING,
1082 &delegation->flags) ||
1083 test_bit(NFS_DELEGATION_NEED_RECLAIM,
1084 &delegation->flags) == 0)
1086 if (!nfs_sb_active(server->super))
1087 break; /* continue in outer loop */
1088 inode = nfs_delegation_grab_inode(delegation);
1089 if (inode == NULL) {
1091 nfs_sb_deactive(server->super);
1094 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
1096 if (delegation != NULL) {
1097 delegation = nfs_detach_delegation(NFS_I(inode),
1098 delegation, server);
1099 if (delegation != NULL)
1100 nfs_free_delegation(delegation);
1103 nfs_sb_deactive(server->super);
1111 static inline bool nfs4_server_rebooted(const struct nfs_client *clp)
1113 return (clp->cl_state & (BIT(NFS4CLNT_CHECK_LEASE) |
1114 BIT(NFS4CLNT_LEASE_EXPIRED) |
1115 BIT(NFS4CLNT_SESSION_RESET))) != 0;
1118 static void nfs_mark_test_expired_delegation(struct nfs_server *server,
1119 struct nfs_delegation *delegation)
1121 if (delegation->stateid.type == NFS4_INVALID_STATEID_TYPE)
1123 clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
1124 set_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
1125 set_bit(NFS4CLNT_DELEGATION_EXPIRED, &server->nfs_client->cl_state);
1128 static void nfs_inode_mark_test_expired_delegation(struct nfs_server *server,
1129 struct inode *inode)
1131 struct nfs_delegation *delegation;
1134 delegation = rcu_dereference(NFS_I(inode)->delegation);
1136 nfs_mark_test_expired_delegation(server, delegation);
1141 static void nfs_delegation_mark_test_expired_server(struct nfs_server *server)
1143 struct nfs_delegation *delegation;
1145 list_for_each_entry_rcu(delegation, &server->delegations, super_list)
1146 nfs_mark_test_expired_delegation(server, delegation);
1150 * nfs_mark_test_expired_all_delegations - mark all delegations for testing
1151 * @clp: nfs_client to process
1153 * Iterates through all the delegations associated with this server and
1154 * marks them as needing to be checked for validity.
1156 void nfs_mark_test_expired_all_delegations(struct nfs_client *clp)
1158 struct nfs_server *server;
1161 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1162 nfs_delegation_mark_test_expired_server(server);
1167 * nfs_test_expired_all_delegations - test all delegations for a client
1168 * @clp: nfs_client to process
1170 * Helper for handling "recallable state revoked" status from server.
1172 void nfs_test_expired_all_delegations(struct nfs_client *clp)
1174 nfs_mark_test_expired_all_delegations(clp);
1175 nfs4_schedule_state_manager(clp);
1179 nfs_delegation_test_free_expired(struct inode *inode,
1180 nfs4_stateid *stateid,
1181 const struct cred *cred)
1183 struct nfs_server *server = NFS_SERVER(inode);
1184 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
1189 status = ops->test_and_free_expired(server, stateid, cred);
1190 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
1191 nfs_remove_bad_delegation(inode, stateid);
1195 * nfs_reap_expired_delegations - reap expired delegations
1196 * @clp: nfs_client to process
1198 * Iterates through all the delegations associated with this server and
1199 * checks if they have may have been revoked. This function is usually
1200 * expected to be called in cases where the server may have lost its
1203 void nfs_reap_expired_delegations(struct nfs_client *clp)
1205 struct nfs_delegation *delegation;
1206 struct nfs_server *server;
1207 struct inode *inode;
1208 const struct cred *cred;
1209 nfs4_stateid stateid;
1213 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1214 list_for_each_entry_rcu(delegation, &server->delegations,
1216 if (test_bit(NFS_DELEGATION_INODE_FREEING,
1217 &delegation->flags) ||
1218 test_bit(NFS_DELEGATION_RETURNING,
1219 &delegation->flags) ||
1220 test_bit(NFS_DELEGATION_TEST_EXPIRED,
1221 &delegation->flags) == 0)
1223 if (!nfs_sb_active(server->super))
1224 break; /* continue in outer loop */
1225 inode = nfs_delegation_grab_inode(delegation);
1226 if (inode == NULL) {
1228 nfs_sb_deactive(server->super);
1231 cred = get_cred_rcu(delegation->cred);
1232 nfs4_stateid_copy(&stateid, &delegation->stateid);
1233 clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
1235 nfs_delegation_test_free_expired(inode, &stateid, cred);
1237 if (nfs4_server_rebooted(clp)) {
1238 nfs_inode_mark_test_expired_delegation(server,inode);
1240 nfs_sb_deactive(server->super);
1244 nfs_sb_deactive(server->super);
1252 void nfs_inode_find_delegation_state_and_recover(struct inode *inode,
1253 const nfs4_stateid *stateid)
1255 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1256 struct nfs_delegation *delegation;
1260 delegation = rcu_dereference(NFS_I(inode)->delegation);
1262 nfs4_stateid_match_or_older(&delegation->stateid, stateid) &&
1263 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
1264 nfs_mark_test_expired_delegation(NFS_SERVER(inode), delegation);
1269 nfs4_schedule_state_manager(clp);
1273 * nfs_delegations_present - check for existence of delegations
1274 * @clp: client state handle
1276 * Returns one if there are any nfs_delegation structures attached
1277 * to this nfs_client.
1279 int nfs_delegations_present(struct nfs_client *clp)
1281 struct nfs_server *server;
1285 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1286 if (!list_empty(&server->delegations)) {
1295 * nfs4_refresh_delegation_stateid - Update delegation stateid seqid
1296 * @dst: stateid to refresh
1297 * @inode: inode to check
1299 * Returns "true" and updates "dst->seqid" * if inode had a delegation
1300 * that matches our delegation stateid. Otherwise "false" is returned.
1302 bool nfs4_refresh_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
1304 struct nfs_delegation *delegation;
1310 delegation = rcu_dereference(NFS_I(inode)->delegation);
1311 if (delegation != NULL &&
1312 nfs4_stateid_match_other(dst, &delegation->stateid) &&
1313 nfs4_stateid_is_newer(&delegation->stateid, dst) &&
1314 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
1315 dst->seqid = delegation->stateid.seqid;
1324 * nfs4_copy_delegation_stateid - Copy inode's state ID information
1325 * @inode: inode to check
1326 * @flags: delegation type requirement
1327 * @dst: stateid data structure to fill in
1328 * @cred: optional argument to retrieve credential
1330 * Returns "true" and fills in "dst->data" * if inode had a delegation,
1331 * otherwise "false" is returned.
1333 bool nfs4_copy_delegation_stateid(struct inode *inode, fmode_t flags,
1334 nfs4_stateid *dst, const struct cred **cred)
1336 struct nfs_inode *nfsi = NFS_I(inode);
1337 struct nfs_delegation *delegation;
1340 flags &= FMODE_READ|FMODE_WRITE;
1342 delegation = rcu_dereference(nfsi->delegation);
1343 ret = nfs4_is_valid_delegation(delegation, flags);
1345 nfs4_stateid_copy(dst, &delegation->stateid);
1346 nfs_mark_delegation_referenced(delegation);
1348 *cred = get_cred(delegation->cred);
1355 * nfs4_delegation_flush_on_close - Check if we must flush file on close
1356 * @inode: inode to check
1358 * This function checks the number of outstanding writes to the file
1359 * against the delegation 'space_limit' field to see if
1360 * the spec requires us to flush the file on close.
1362 bool nfs4_delegation_flush_on_close(const struct inode *inode)
1364 struct nfs_inode *nfsi = NFS_I(inode);
1365 struct nfs_delegation *delegation;
1369 delegation = rcu_dereference(nfsi->delegation);
1370 if (delegation == NULL || !(delegation->type & FMODE_WRITE))
1372 if (atomic_long_read(&nfsi->nrequests) < delegation->pagemod_limit)
1379 module_param_named(delegation_watermark, nfs_delegation_watermark, uint, 0644);