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 static void nfs_free_delegation(struct nfs_delegation *delegation)
30 put_cred(delegation->cred);
31 delegation->cred = NULL;
32 kfree_rcu(delegation, rcu);
36 * nfs_mark_delegation_referenced - set delegation's REFERENCED flag
37 * @delegation: delegation to process
40 void nfs_mark_delegation_referenced(struct nfs_delegation *delegation)
42 set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags);
46 nfs4_is_valid_delegation(const struct nfs_delegation *delegation,
49 if (delegation != NULL && (delegation->type & flags) == flags &&
50 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) &&
51 !test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
56 struct nfs_delegation *nfs4_get_valid_delegation(const struct inode *inode)
58 struct nfs_delegation *delegation;
60 delegation = rcu_dereference(NFS_I(inode)->delegation);
61 if (nfs4_is_valid_delegation(delegation, 0))
67 nfs4_do_check_delegation(struct inode *inode, fmode_t flags, bool mark)
69 struct nfs_delegation *delegation;
72 flags &= FMODE_READ|FMODE_WRITE;
74 delegation = rcu_dereference(NFS_I(inode)->delegation);
75 if (nfs4_is_valid_delegation(delegation, flags)) {
77 nfs_mark_delegation_referenced(delegation);
84 * nfs_have_delegation - check if inode has a delegation, mark it
85 * NFS_DELEGATION_REFERENCED if there is one.
86 * @inode: inode to check
87 * @flags: delegation types to check for
89 * Returns one if inode has the indicated delegation, otherwise zero.
91 int nfs4_have_delegation(struct inode *inode, fmode_t flags)
93 return nfs4_do_check_delegation(inode, flags, true);
97 * nfs4_check_delegation - check if inode has a delegation, do not mark
98 * NFS_DELEGATION_REFERENCED if it has one.
100 int nfs4_check_delegation(struct inode *inode, fmode_t flags)
102 return nfs4_do_check_delegation(inode, flags, false);
105 static int nfs_delegation_claim_locks(struct nfs4_state *state, const nfs4_stateid *stateid)
107 struct inode *inode = state->inode;
108 struct file_lock *fl;
109 struct file_lock_context *flctx = inode->i_flctx;
110 struct list_head *list;
116 list = &flctx->flc_posix;
117 spin_lock(&flctx->flc_lock);
119 list_for_each_entry(fl, list, fl_list) {
120 if (nfs_file_open_context(fl->fl_file)->state != state)
122 spin_unlock(&flctx->flc_lock);
123 status = nfs4_lock_delegation_recall(fl, state, stateid);
126 spin_lock(&flctx->flc_lock);
128 if (list == &flctx->flc_posix) {
129 list = &flctx->flc_flock;
132 spin_unlock(&flctx->flc_lock);
137 static int nfs_delegation_claim_opens(struct inode *inode,
138 const nfs4_stateid *stateid, fmode_t type)
140 struct nfs_inode *nfsi = NFS_I(inode);
141 struct nfs_open_context *ctx;
142 struct nfs4_state_owner *sp;
143 struct nfs4_state *state;
149 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
153 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
155 if (!nfs4_valid_open_stateid(state))
157 if (!nfs4_stateid_match(&state->stateid, stateid))
159 if (!get_nfs_open_context(ctx))
163 /* Block nfs4_proc_unlck */
164 mutex_lock(&sp->so_delegreturn_mutex);
165 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
166 err = nfs4_open_delegation_recall(ctx, state, stateid);
168 err = nfs_delegation_claim_locks(state, stateid);
169 if (!err && read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
171 mutex_unlock(&sp->so_delegreturn_mutex);
172 put_nfs_open_context(ctx);
182 * nfs_inode_reclaim_delegation - process a delegation reclaim request
183 * @inode: inode to process
184 * @cred: credential to use for request
185 * @type: delegation type
186 * @stateid: delegation stateid
187 * @pagemod_limit: write delegation "space_limit"
190 void nfs_inode_reclaim_delegation(struct inode *inode, const struct cred *cred,
192 const nfs4_stateid *stateid,
193 unsigned long pagemod_limit)
195 struct nfs_delegation *delegation;
196 const struct cred *oldcred = NULL;
199 delegation = rcu_dereference(NFS_I(inode)->delegation);
200 if (delegation != NULL) {
201 spin_lock(&delegation->lock);
202 if (nfs4_is_valid_delegation(delegation, 0)) {
203 nfs4_stateid_copy(&delegation->stateid, stateid);
204 delegation->type = type;
205 delegation->pagemod_limit = pagemod_limit;
206 oldcred = delegation->cred;
207 delegation->cred = get_cred(cred);
208 clear_bit(NFS_DELEGATION_NEED_RECLAIM,
210 spin_unlock(&delegation->lock);
213 trace_nfs4_reclaim_delegation(inode, type);
216 /* We appear to have raced with a delegation return. */
217 spin_unlock(&delegation->lock);
220 nfs_inode_set_delegation(inode, cred, type, stateid, pagemod_limit);
223 static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
227 if (!test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
228 res = nfs4_proc_delegreturn(inode,
230 &delegation->stateid,
235 static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
237 struct inode *inode = NULL;
239 spin_lock(&delegation->lock);
240 if (delegation->inode != NULL)
241 inode = igrab(delegation->inode);
243 set_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags);
244 spin_unlock(&delegation->lock);
248 static struct nfs_delegation *
249 nfs_start_delegation_return_locked(struct nfs_inode *nfsi)
251 struct nfs_delegation *ret = NULL;
252 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
254 if (delegation == NULL)
256 spin_lock(&delegation->lock);
257 if (!test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
259 spin_unlock(&delegation->lock);
264 static struct nfs_delegation *
265 nfs_start_delegation_return(struct nfs_inode *nfsi)
267 struct nfs_delegation *delegation;
270 delegation = nfs_start_delegation_return_locked(nfsi);
276 nfs_abort_delegation_return(struct nfs_delegation *delegation,
277 struct nfs_client *clp)
280 spin_lock(&delegation->lock);
281 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
282 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
283 spin_unlock(&delegation->lock);
284 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
287 static struct nfs_delegation *
288 nfs_detach_delegation_locked(struct nfs_inode *nfsi,
289 struct nfs_delegation *delegation,
290 struct nfs_client *clp)
292 struct nfs_delegation *deleg_cur =
293 rcu_dereference_protected(nfsi->delegation,
294 lockdep_is_held(&clp->cl_lock));
296 if (deleg_cur == NULL || delegation != deleg_cur)
299 spin_lock(&delegation->lock);
300 if (!delegation->inode) {
301 spin_unlock(&delegation->lock);
304 list_del_rcu(&delegation->super_list);
305 delegation->inode = NULL;
306 rcu_assign_pointer(nfsi->delegation, NULL);
307 spin_unlock(&delegation->lock);
311 static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi,
312 struct nfs_delegation *delegation,
313 struct nfs_server *server)
315 struct nfs_client *clp = server->nfs_client;
317 spin_lock(&clp->cl_lock);
318 delegation = nfs_detach_delegation_locked(nfsi, delegation, clp);
319 spin_unlock(&clp->cl_lock);
323 static struct nfs_delegation *
324 nfs_inode_detach_delegation(struct inode *inode)
326 struct nfs_inode *nfsi = NFS_I(inode);
327 struct nfs_server *server = NFS_SERVER(inode);
328 struct nfs_delegation *delegation;
331 delegation = rcu_dereference(nfsi->delegation);
332 if (delegation != NULL)
333 delegation = nfs_detach_delegation(nfsi, delegation, server);
339 nfs_update_inplace_delegation(struct nfs_delegation *delegation,
340 const struct nfs_delegation *update)
342 if (nfs4_stateid_is_newer(&update->stateid, &delegation->stateid)) {
343 delegation->stateid.seqid = update->stateid.seqid;
345 delegation->type = update->type;
346 clear_bit(NFS_DELEGATION_REVOKED, &delegation->flags);
351 * nfs_inode_set_delegation - set up a delegation on an inode
352 * @inode: inode to which delegation applies
353 * @cred: cred to use for subsequent delegation processing
354 * @type: delegation type
355 * @stateid: delegation stateid
356 * @pagemod_limit: write delegation "space_limit"
358 * Returns zero on success, or a negative errno value.
360 int nfs_inode_set_delegation(struct inode *inode, const struct cred *cred,
362 const nfs4_stateid *stateid,
363 unsigned long pagemod_limit)
365 struct nfs_server *server = NFS_SERVER(inode);
366 struct nfs_client *clp = server->nfs_client;
367 struct nfs_inode *nfsi = NFS_I(inode);
368 struct nfs_delegation *delegation, *old_delegation;
369 struct nfs_delegation *freeme = NULL;
372 delegation = kmalloc(sizeof(*delegation), GFP_NOFS);
373 if (delegation == NULL)
375 nfs4_stateid_copy(&delegation->stateid, stateid);
376 delegation->type = type;
377 delegation->pagemod_limit = pagemod_limit;
378 delegation->change_attr = inode_peek_iversion_raw(inode);
379 delegation->cred = get_cred(cred);
380 delegation->inode = inode;
381 delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
382 spin_lock_init(&delegation->lock);
384 spin_lock(&clp->cl_lock);
385 old_delegation = rcu_dereference_protected(nfsi->delegation,
386 lockdep_is_held(&clp->cl_lock));
387 if (old_delegation == NULL)
389 /* Is this an update of the existing delegation? */
390 if (nfs4_stateid_match_other(&old_delegation->stateid,
391 &delegation->stateid)) {
392 spin_lock(&old_delegation->lock);
393 nfs_update_inplace_delegation(old_delegation,
395 spin_unlock(&old_delegation->lock);
398 if (!test_bit(NFS_DELEGATION_REVOKED, &old_delegation->flags)) {
400 * Deal with broken servers that hand out two
401 * delegations for the same file.
402 * Allow for upgrades to a WRITE delegation, but
405 dfprintk(FILE, "%s: server %s handed out "
406 "a duplicate delegation!\n",
407 __func__, clp->cl_hostname);
408 if (delegation->type == old_delegation->type ||
409 !(delegation->type & FMODE_WRITE)) {
414 if (test_and_set_bit(NFS_DELEGATION_RETURNING,
415 &old_delegation->flags))
418 freeme = nfs_detach_delegation_locked(nfsi, old_delegation, clp);
422 list_add_tail_rcu(&delegation->super_list, &server->delegations);
423 rcu_assign_pointer(nfsi->delegation, delegation);
426 trace_nfs4_set_delegation(inode, type);
428 spin_lock(&inode->i_lock);
429 if (NFS_I(inode)->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ATIME))
430 NFS_I(inode)->cache_validity |= NFS_INO_REVAL_FORCED;
431 spin_unlock(&inode->i_lock);
433 spin_unlock(&clp->cl_lock);
434 if (delegation != NULL)
435 nfs_free_delegation(delegation);
436 if (freeme != NULL) {
437 nfs_do_return_delegation(inode, freeme, 0);
438 nfs_free_delegation(freeme);
444 * Basic procedure for returning a delegation to the server
446 static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync)
448 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
451 if (delegation == NULL)
454 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
456 err = nfs_delegation_claim_opens(inode, &delegation->stateid,
458 if (!issync || err != -EAGAIN)
461 * Guard against state recovery
463 err = nfs4_wait_clnt_recover(clp);
467 nfs_abort_delegation_return(delegation, clp);
471 err = nfs_do_return_delegation(inode, delegation, issync);
476 static bool nfs_delegation_need_return(struct nfs_delegation *delegation)
480 if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
482 else if (test_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags)) {
485 spin_lock(&delegation->lock);
486 inode = delegation->inode;
487 if (inode && list_empty(&NFS_I(inode)->open_files))
489 spin_unlock(&delegation->lock);
492 clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
493 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags) ||
494 test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
501 * nfs_client_return_marked_delegations - return previously marked delegations
502 * @clp: nfs_client to process
504 * Note that this function is designed to be called by the state
505 * manager thread. For this reason, it cannot flush the dirty data,
506 * since that could deadlock in case of a state recovery error.
508 * Returns zero on success, or a negative errno value.
510 int nfs_client_return_marked_delegations(struct nfs_client *clp)
512 struct nfs_delegation *delegation;
513 struct nfs_delegation *prev;
514 struct nfs_server *server;
516 struct inode *place_holder = NULL;
517 struct nfs_delegation *place_holder_deleg = NULL;
522 * To avoid quadratic looping we hold a reference
523 * to an inode place_holder. Each time we restart, we
524 * list nfs_servers from the server of that inode, and
525 * delegation in the server from the delegations of that
527 * prev is an RCU-protected pointer to a delegation which
528 * wasn't marked for return and might be a good choice for
529 * the next place_holder.
534 server = NFS_SERVER(place_holder);
536 server = list_entry_rcu(clp->cl_superblocks.next,
537 struct nfs_server, client_link);
538 list_for_each_entry_from_rcu(server, &clp->cl_superblocks, client_link) {
540 if (place_holder && server == NFS_SERVER(place_holder))
541 delegation = rcu_dereference(NFS_I(place_holder)->delegation);
542 if (!delegation || delegation != place_holder_deleg)
543 delegation = list_entry_rcu(server->delegations.next,
544 struct nfs_delegation, super_list);
545 list_for_each_entry_from_rcu(delegation, &server->delegations, super_list) {
546 struct inode *to_put = NULL;
548 if (!nfs_delegation_need_return(delegation)) {
552 if (!nfs_sb_active(server->super))
553 break; /* continue in outer loop */
558 tmp = nfs_delegation_grab_inode(prev);
560 to_put = place_holder;
562 place_holder_deleg = prev;
566 inode = nfs_delegation_grab_inode(delegation);
571 nfs_sb_deactive(server->super);
574 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
580 err = nfs_end_delegation_return(inode, delegation, 0);
582 nfs_sb_deactive(server->super);
586 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
599 * nfs_inode_evict_delegation - return delegation, don't reclaim opens
600 * @inode: inode to process
602 * Does not protect against delegation reclaims, therefore really only safe
603 * to be called from nfs4_clear_inode(). Guaranteed to always free
604 * the delegation structure.
606 void nfs_inode_evict_delegation(struct inode *inode)
608 struct nfs_delegation *delegation;
610 delegation = nfs_inode_detach_delegation(inode);
611 if (delegation != NULL) {
612 set_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
613 set_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags);
614 nfs_do_return_delegation(inode, delegation, 1);
615 nfs_free_delegation(delegation);
620 * nfs_inode_return_delegation - synchronously return a delegation
621 * @inode: inode to process
623 * This routine will always flush any dirty data to disk on the
624 * assumption that if we need to return the delegation, then
625 * we should stop caching.
627 * Returns zero on success, or a negative errno value.
629 int nfs4_inode_return_delegation(struct inode *inode)
631 struct nfs_inode *nfsi = NFS_I(inode);
632 struct nfs_delegation *delegation;
636 delegation = nfs_start_delegation_return(nfsi);
637 if (delegation != NULL)
638 err = nfs_end_delegation_return(inode, delegation, 1);
643 * nfs_inode_return_delegation_on_close - asynchronously return a delegation
644 * @inode: inode to process
646 * This routine is called on file close in order to determine if the
647 * inode delegation needs to be returned immediately.
649 void nfs4_inode_return_delegation_on_close(struct inode *inode)
651 struct nfs_delegation *delegation;
652 struct nfs_delegation *ret = NULL;
657 delegation = nfs4_get_valid_delegation(inode);
660 if (test_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags)) {
661 spin_lock(&delegation->lock);
662 if (delegation->inode &&
663 list_empty(&NFS_I(inode)->open_files) &&
664 !test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) {
665 clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
668 spin_unlock(&delegation->lock);
672 nfs_end_delegation_return(inode, ret, 0);
676 * nfs4_inode_make_writeable
677 * @inode: pointer to inode
679 * Make the inode writeable by returning the delegation if necessary
681 * Returns zero on success, or a negative errno value.
683 int nfs4_inode_make_writeable(struct inode *inode)
685 struct nfs_delegation *delegation;
688 delegation = nfs4_get_valid_delegation(inode);
689 if (delegation == NULL ||
690 (nfs4_has_session(NFS_SERVER(inode)->nfs_client) &&
691 (delegation->type & FMODE_WRITE))) {
696 return nfs4_inode_return_delegation(inode);
699 static void nfs_mark_return_if_closed_delegation(struct nfs_server *server,
700 struct nfs_delegation *delegation)
702 set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
703 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
706 static void nfs_mark_return_delegation(struct nfs_server *server,
707 struct nfs_delegation *delegation)
709 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
710 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
713 static bool nfs_server_mark_return_all_delegations(struct nfs_server *server)
715 struct nfs_delegation *delegation;
718 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
719 nfs_mark_return_delegation(server, delegation);
725 static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
727 struct nfs_server *server;
730 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
731 nfs_server_mark_return_all_delegations(server);
735 static void nfs_delegation_run_state_manager(struct nfs_client *clp)
737 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
738 nfs4_schedule_state_manager(clp);
742 * nfs_expire_all_delegations
743 * @clp: client to process
746 void nfs_expire_all_delegations(struct nfs_client *clp)
748 nfs_client_mark_return_all_delegations(clp);
749 nfs_delegation_run_state_manager(clp);
753 * nfs_super_return_all_delegations - return delegations for one superblock
754 * @server: pointer to nfs_server to process
757 void nfs_server_return_all_delegations(struct nfs_server *server)
759 struct nfs_client *clp = server->nfs_client;
766 need_wait = nfs_server_mark_return_all_delegations(server);
770 nfs4_schedule_state_manager(clp);
771 nfs4_wait_clnt_recover(clp);
775 static void nfs_mark_return_unused_delegation_types(struct nfs_server *server,
778 struct nfs_delegation *delegation;
780 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
781 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
783 if (delegation->type & flags)
784 nfs_mark_return_if_closed_delegation(server, delegation);
788 static void nfs_client_mark_return_unused_delegation_types(struct nfs_client *clp,
791 struct nfs_server *server;
794 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
795 nfs_mark_return_unused_delegation_types(server, flags);
799 static void nfs_mark_delegation_revoked(struct nfs_server *server,
800 struct nfs_delegation *delegation)
802 set_bit(NFS_DELEGATION_REVOKED, &delegation->flags);
803 delegation->stateid.type = NFS4_INVALID_STATEID_TYPE;
806 static void nfs_revoke_delegation(struct inode *inode,
807 const nfs4_stateid *stateid)
809 struct nfs_delegation *delegation;
814 delegation = rcu_dereference(NFS_I(inode)->delegation);
815 if (delegation == NULL)
817 if (stateid == NULL) {
818 nfs4_stateid_copy(&tmp, &delegation->stateid);
821 if (!nfs4_stateid_match_other(stateid, &delegation->stateid))
823 spin_lock(&delegation->lock);
824 if (stateid->seqid) {
825 if (nfs4_stateid_is_newer(&delegation->stateid, stateid)) {
826 spin_unlock(&delegation->lock);
829 delegation->stateid.seqid = stateid->seqid;
831 spin_unlock(&delegation->lock);
833 nfs_mark_delegation_revoked(NFS_SERVER(inode), delegation);
838 nfs_inode_find_state_and_recover(inode, stateid);
841 void nfs_remove_bad_delegation(struct inode *inode,
842 const nfs4_stateid *stateid)
844 nfs_revoke_delegation(inode, stateid);
846 EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation);
848 void nfs_delegation_mark_returned(struct inode *inode,
849 const nfs4_stateid *stateid)
851 struct nfs_delegation *delegation;
857 delegation = rcu_dereference(NFS_I(inode)->delegation);
861 spin_lock(&delegation->lock);
862 if (!nfs4_stateid_match_other(stateid, &delegation->stateid))
863 goto out_spin_unlock;
864 if (stateid->seqid) {
865 /* If delegation->stateid is newer, dont mark as returned */
866 if (nfs4_stateid_is_newer(&delegation->stateid, stateid))
867 goto out_clear_returning;
868 if (delegation->stateid.seqid != stateid->seqid)
869 delegation->stateid.seqid = stateid->seqid;
872 nfs_mark_delegation_revoked(NFS_SERVER(inode), delegation);
875 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
877 spin_unlock(&delegation->lock);
881 nfs_inode_find_state_and_recover(inode, stateid);
885 * nfs_expire_unused_delegation_types
886 * @clp: client to process
887 * @flags: delegation types to expire
890 void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags)
892 nfs_client_mark_return_unused_delegation_types(clp, flags);
893 nfs_delegation_run_state_manager(clp);
896 static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
898 struct nfs_delegation *delegation;
900 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
901 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
903 nfs_mark_return_if_closed_delegation(server, delegation);
908 * nfs_expire_unreferenced_delegations - Eliminate unused delegations
909 * @clp: nfs_client to process
912 void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
914 struct nfs_server *server;
917 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
918 nfs_mark_return_unreferenced_delegations(server);
921 nfs_delegation_run_state_manager(clp);
925 * nfs_async_inode_return_delegation - asynchronously return a delegation
926 * @inode: inode to process
927 * @stateid: state ID information
929 * Returns zero on success, or a negative errno value.
931 int nfs_async_inode_return_delegation(struct inode *inode,
932 const nfs4_stateid *stateid)
934 struct nfs_server *server = NFS_SERVER(inode);
935 struct nfs_client *clp = server->nfs_client;
936 struct nfs_delegation *delegation;
939 delegation = nfs4_get_valid_delegation(inode);
940 if (delegation == NULL)
942 if (stateid != NULL &&
943 !clp->cl_mvops->match_stateid(&delegation->stateid, stateid))
945 nfs_mark_return_delegation(server, delegation);
948 nfs_delegation_run_state_manager(clp);
955 static struct inode *
956 nfs_delegation_find_inode_server(struct nfs_server *server,
957 const struct nfs_fh *fhandle)
959 struct nfs_delegation *delegation;
960 struct inode *freeme, *res = NULL;
962 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
963 spin_lock(&delegation->lock);
964 if (delegation->inode != NULL &&
965 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) &&
966 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
967 freeme = igrab(delegation->inode);
968 if (freeme && nfs_sb_active(freeme->i_sb))
970 spin_unlock(&delegation->lock);
978 return ERR_PTR(-EAGAIN);
980 spin_unlock(&delegation->lock);
982 return ERR_PTR(-ENOENT);
986 * nfs_delegation_find_inode - retrieve the inode associated with a delegation
987 * @clp: client state handle
988 * @fhandle: filehandle from a delegation recall
990 * Returns pointer to inode matching "fhandle," or NULL if a matching inode
993 struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
994 const struct nfs_fh *fhandle)
996 struct nfs_server *server;
1000 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1001 res = nfs_delegation_find_inode_server(server, fhandle);
1002 if (res != ERR_PTR(-ENOENT)) {
1008 return ERR_PTR(-ENOENT);
1011 static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
1013 struct nfs_delegation *delegation;
1015 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
1017 * If the delegation may have been admin revoked, then we
1018 * cannot reclaim it.
1020 if (test_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags))
1022 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
1027 * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
1028 * @clp: nfs_client to process
1031 void nfs_delegation_mark_reclaim(struct nfs_client *clp)
1033 struct nfs_server *server;
1036 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1037 nfs_delegation_mark_reclaim_server(server);
1042 * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
1043 * @clp: nfs_client to process
1046 void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
1048 struct nfs_delegation *delegation;
1049 struct nfs_server *server;
1050 struct inode *inode;
1054 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1055 list_for_each_entry_rcu(delegation, &server->delegations,
1057 if (test_bit(NFS_DELEGATION_INODE_FREEING,
1058 &delegation->flags) ||
1059 test_bit(NFS_DELEGATION_RETURNING,
1060 &delegation->flags) ||
1061 test_bit(NFS_DELEGATION_NEED_RECLAIM,
1062 &delegation->flags) == 0)
1064 if (!nfs_sb_active(server->super))
1065 break; /* continue in outer loop */
1066 inode = nfs_delegation_grab_inode(delegation);
1067 if (inode == NULL) {
1069 nfs_sb_deactive(server->super);
1072 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
1074 if (delegation != NULL) {
1075 delegation = nfs_detach_delegation(NFS_I(inode),
1076 delegation, server);
1077 if (delegation != NULL)
1078 nfs_free_delegation(delegation);
1081 nfs_sb_deactive(server->super);
1089 static inline bool nfs4_server_rebooted(const struct nfs_client *clp)
1091 return (clp->cl_state & (BIT(NFS4CLNT_CHECK_LEASE) |
1092 BIT(NFS4CLNT_LEASE_EXPIRED) |
1093 BIT(NFS4CLNT_SESSION_RESET))) != 0;
1096 static void nfs_mark_test_expired_delegation(struct nfs_server *server,
1097 struct nfs_delegation *delegation)
1099 if (delegation->stateid.type == NFS4_INVALID_STATEID_TYPE)
1101 clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
1102 set_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
1103 set_bit(NFS4CLNT_DELEGATION_EXPIRED, &server->nfs_client->cl_state);
1106 static void nfs_inode_mark_test_expired_delegation(struct nfs_server *server,
1107 struct inode *inode)
1109 struct nfs_delegation *delegation;
1112 delegation = rcu_dereference(NFS_I(inode)->delegation);
1114 nfs_mark_test_expired_delegation(server, delegation);
1119 static void nfs_delegation_mark_test_expired_server(struct nfs_server *server)
1121 struct nfs_delegation *delegation;
1123 list_for_each_entry_rcu(delegation, &server->delegations, super_list)
1124 nfs_mark_test_expired_delegation(server, delegation);
1128 * nfs_mark_test_expired_all_delegations - mark all delegations for testing
1129 * @clp: nfs_client to process
1131 * Iterates through all the delegations associated with this server and
1132 * marks them as needing to be checked for validity.
1134 void nfs_mark_test_expired_all_delegations(struct nfs_client *clp)
1136 struct nfs_server *server;
1139 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1140 nfs_delegation_mark_test_expired_server(server);
1145 * nfs_test_expired_all_delegations - test all delegations for a client
1146 * @clp: nfs_client to process
1148 * Helper for handling "recallable state revoked" status from server.
1150 void nfs_test_expired_all_delegations(struct nfs_client *clp)
1152 nfs_mark_test_expired_all_delegations(clp);
1153 nfs4_schedule_state_manager(clp);
1157 nfs_delegation_test_free_expired(struct inode *inode,
1158 nfs4_stateid *stateid,
1159 const struct cred *cred)
1161 struct nfs_server *server = NFS_SERVER(inode);
1162 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
1167 status = ops->test_and_free_expired(server, stateid, cred);
1168 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
1169 nfs_remove_bad_delegation(inode, stateid);
1173 * nfs_reap_expired_delegations - reap expired delegations
1174 * @clp: nfs_client to process
1176 * Iterates through all the delegations associated with this server and
1177 * checks if they have may have been revoked. This function is usually
1178 * expected to be called in cases where the server may have lost its
1181 void nfs_reap_expired_delegations(struct nfs_client *clp)
1183 struct nfs_delegation *delegation;
1184 struct nfs_server *server;
1185 struct inode *inode;
1186 const struct cred *cred;
1187 nfs4_stateid stateid;
1191 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1192 list_for_each_entry_rcu(delegation, &server->delegations,
1194 if (test_bit(NFS_DELEGATION_INODE_FREEING,
1195 &delegation->flags) ||
1196 test_bit(NFS_DELEGATION_RETURNING,
1197 &delegation->flags) ||
1198 test_bit(NFS_DELEGATION_TEST_EXPIRED,
1199 &delegation->flags) == 0)
1201 if (!nfs_sb_active(server->super))
1202 break; /* continue in outer loop */
1203 inode = nfs_delegation_grab_inode(delegation);
1204 if (inode == NULL) {
1206 nfs_sb_deactive(server->super);
1209 cred = get_cred_rcu(delegation->cred);
1210 nfs4_stateid_copy(&stateid, &delegation->stateid);
1211 clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
1213 nfs_delegation_test_free_expired(inode, &stateid, cred);
1215 if (nfs4_server_rebooted(clp)) {
1216 nfs_inode_mark_test_expired_delegation(server,inode);
1218 nfs_sb_deactive(server->super);
1222 nfs_sb_deactive(server->super);
1230 void nfs_inode_find_delegation_state_and_recover(struct inode *inode,
1231 const nfs4_stateid *stateid)
1233 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1234 struct nfs_delegation *delegation;
1238 delegation = rcu_dereference(NFS_I(inode)->delegation);
1240 nfs4_stateid_match_or_older(&delegation->stateid, stateid) &&
1241 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
1242 nfs_mark_test_expired_delegation(NFS_SERVER(inode), delegation);
1247 nfs4_schedule_state_manager(clp);
1251 * nfs_delegations_present - check for existence of delegations
1252 * @clp: client state handle
1254 * Returns one if there are any nfs_delegation structures attached
1255 * to this nfs_client.
1257 int nfs_delegations_present(struct nfs_client *clp)
1259 struct nfs_server *server;
1263 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1264 if (!list_empty(&server->delegations)) {
1273 * nfs4_refresh_delegation_stateid - Update delegation stateid seqid
1274 * @dst: stateid to refresh
1275 * @inode: inode to check
1277 * Returns "true" and updates "dst->seqid" * if inode had a delegation
1278 * that matches our delegation stateid. Otherwise "false" is returned.
1280 bool nfs4_refresh_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
1282 struct nfs_delegation *delegation;
1288 delegation = rcu_dereference(NFS_I(inode)->delegation);
1289 if (delegation != NULL &&
1290 nfs4_stateid_match_other(dst, &delegation->stateid) &&
1291 nfs4_stateid_is_newer(&delegation->stateid, dst) &&
1292 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
1293 dst->seqid = delegation->stateid.seqid;
1302 * nfs4_copy_delegation_stateid - Copy inode's state ID information
1303 * @inode: inode to check
1304 * @flags: delegation type requirement
1305 * @dst: stateid data structure to fill in
1306 * @cred: optional argument to retrieve credential
1308 * Returns "true" and fills in "dst->data" * if inode had a delegation,
1309 * otherwise "false" is returned.
1311 bool nfs4_copy_delegation_stateid(struct inode *inode, fmode_t flags,
1312 nfs4_stateid *dst, const struct cred **cred)
1314 struct nfs_inode *nfsi = NFS_I(inode);
1315 struct nfs_delegation *delegation;
1318 flags &= FMODE_READ|FMODE_WRITE;
1320 delegation = rcu_dereference(nfsi->delegation);
1321 ret = nfs4_is_valid_delegation(delegation, flags);
1323 nfs4_stateid_copy(dst, &delegation->stateid);
1324 nfs_mark_delegation_referenced(delegation);
1326 *cred = get_cred(delegation->cred);
1333 * nfs4_delegation_flush_on_close - Check if we must flush file on close
1334 * @inode: inode to check
1336 * This function checks the number of outstanding writes to the file
1337 * against the delegation 'space_limit' field to see if
1338 * the spec requires us to flush the file on close.
1340 bool nfs4_delegation_flush_on_close(const struct inode *inode)
1342 struct nfs_inode *nfsi = NFS_I(inode);
1343 struct nfs_delegation *delegation;
1347 delegation = rcu_dereference(nfsi->delegation);
1348 if (delegation == NULL || !(delegation->type & FMODE_WRITE))
1350 if (atomic_long_read(&nfsi->nrequests) < delegation->pagemod_limit)