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 atomic_long_t nfs_active_delegations;
30 static void __nfs_free_delegation(struct nfs_delegation *delegation)
32 put_cred(delegation->cred);
33 delegation->cred = NULL;
34 kfree_rcu(delegation, rcu);
37 static void nfs_mark_delegation_revoked(struct nfs_delegation *delegation)
39 if (!test_and_set_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
40 delegation->stateid.type = NFS4_INVALID_STATEID_TYPE;
41 atomic_long_dec(&nfs_active_delegations);
45 static void nfs_free_delegation(struct nfs_delegation *delegation)
47 nfs_mark_delegation_revoked(delegation);
48 __nfs_free_delegation(delegation);
52 * nfs_mark_delegation_referenced - set delegation's REFERENCED flag
53 * @delegation: delegation to process
56 void nfs_mark_delegation_referenced(struct nfs_delegation *delegation)
58 set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags);
62 nfs4_is_valid_delegation(const struct nfs_delegation *delegation,
65 if (delegation != NULL && (delegation->type & flags) == flags &&
66 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) &&
67 !test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
72 struct nfs_delegation *nfs4_get_valid_delegation(const struct inode *inode)
74 struct nfs_delegation *delegation;
76 delegation = rcu_dereference(NFS_I(inode)->delegation);
77 if (nfs4_is_valid_delegation(delegation, 0))
83 nfs4_do_check_delegation(struct inode *inode, fmode_t flags, bool mark)
85 struct nfs_delegation *delegation;
88 flags &= FMODE_READ|FMODE_WRITE;
90 delegation = rcu_dereference(NFS_I(inode)->delegation);
91 if (nfs4_is_valid_delegation(delegation, flags)) {
93 nfs_mark_delegation_referenced(delegation);
100 * nfs_have_delegation - check if inode has a delegation, mark it
101 * NFS_DELEGATION_REFERENCED if there is one.
102 * @inode: inode to check
103 * @flags: delegation types to check for
105 * Returns one if inode has the indicated delegation, otherwise zero.
107 int nfs4_have_delegation(struct inode *inode, fmode_t flags)
109 return nfs4_do_check_delegation(inode, flags, true);
113 * nfs4_check_delegation - check if inode has a delegation, do not mark
114 * NFS_DELEGATION_REFERENCED if it has one.
116 int nfs4_check_delegation(struct inode *inode, fmode_t flags)
118 return nfs4_do_check_delegation(inode, flags, false);
121 static int nfs_delegation_claim_locks(struct nfs4_state *state, const nfs4_stateid *stateid)
123 struct inode *inode = state->inode;
124 struct file_lock *fl;
125 struct file_lock_context *flctx = inode->i_flctx;
126 struct list_head *list;
132 list = &flctx->flc_posix;
133 spin_lock(&flctx->flc_lock);
135 list_for_each_entry(fl, list, fl_list) {
136 if (nfs_file_open_context(fl->fl_file)->state != state)
138 spin_unlock(&flctx->flc_lock);
139 status = nfs4_lock_delegation_recall(fl, state, stateid);
142 spin_lock(&flctx->flc_lock);
144 if (list == &flctx->flc_posix) {
145 list = &flctx->flc_flock;
148 spin_unlock(&flctx->flc_lock);
153 static int nfs_delegation_claim_opens(struct inode *inode,
154 const nfs4_stateid *stateid, fmode_t type)
156 struct nfs_inode *nfsi = NFS_I(inode);
157 struct nfs_open_context *ctx;
158 struct nfs4_state_owner *sp;
159 struct nfs4_state *state;
165 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
169 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
171 if (!nfs4_valid_open_stateid(state))
173 if (!nfs4_stateid_match(&state->stateid, stateid))
175 if (!get_nfs_open_context(ctx))
179 /* Block nfs4_proc_unlck */
180 mutex_lock(&sp->so_delegreturn_mutex);
181 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
182 err = nfs4_open_delegation_recall(ctx, state, stateid);
184 err = nfs_delegation_claim_locks(state, stateid);
185 if (!err && read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
187 mutex_unlock(&sp->so_delegreturn_mutex);
188 put_nfs_open_context(ctx);
198 * nfs_inode_reclaim_delegation - process a delegation reclaim request
199 * @inode: inode to process
200 * @cred: credential to use for request
201 * @type: delegation type
202 * @stateid: delegation stateid
203 * @pagemod_limit: write delegation "space_limit"
206 void nfs_inode_reclaim_delegation(struct inode *inode, const struct cred *cred,
208 const nfs4_stateid *stateid,
209 unsigned long pagemod_limit)
211 struct nfs_delegation *delegation;
212 const struct cred *oldcred = NULL;
215 delegation = rcu_dereference(NFS_I(inode)->delegation);
216 if (delegation != NULL) {
217 spin_lock(&delegation->lock);
218 if (nfs4_is_valid_delegation(delegation, 0)) {
219 nfs4_stateid_copy(&delegation->stateid, stateid);
220 delegation->type = type;
221 delegation->pagemod_limit = pagemod_limit;
222 oldcred = delegation->cred;
223 delegation->cred = get_cred(cred);
224 clear_bit(NFS_DELEGATION_NEED_RECLAIM,
226 spin_unlock(&delegation->lock);
229 trace_nfs4_reclaim_delegation(inode, type);
232 /* We appear to have raced with a delegation return. */
233 spin_unlock(&delegation->lock);
236 nfs_inode_set_delegation(inode, cred, type, stateid, pagemod_limit);
239 static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
243 if (!test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
244 res = nfs4_proc_delegreturn(inode,
246 &delegation->stateid,
251 static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
253 struct inode *inode = NULL;
255 spin_lock(&delegation->lock);
256 if (delegation->inode != NULL)
257 inode = igrab(delegation->inode);
259 set_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags);
260 spin_unlock(&delegation->lock);
264 static struct nfs_delegation *
265 nfs_start_delegation_return_locked(struct nfs_inode *nfsi)
267 struct nfs_delegation *ret = NULL;
268 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
270 if (delegation == NULL)
272 spin_lock(&delegation->lock);
273 if (!test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
275 spin_unlock(&delegation->lock);
280 static struct nfs_delegation *
281 nfs_start_delegation_return(struct nfs_inode *nfsi)
283 struct nfs_delegation *delegation;
286 delegation = nfs_start_delegation_return_locked(nfsi);
292 nfs_abort_delegation_return(struct nfs_delegation *delegation,
293 struct nfs_client *clp)
296 spin_lock(&delegation->lock);
297 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
298 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
299 spin_unlock(&delegation->lock);
300 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
303 static struct nfs_delegation *
304 nfs_detach_delegation_locked(struct nfs_inode *nfsi,
305 struct nfs_delegation *delegation,
306 struct nfs_client *clp)
308 struct nfs_delegation *deleg_cur =
309 rcu_dereference_protected(nfsi->delegation,
310 lockdep_is_held(&clp->cl_lock));
312 if (deleg_cur == NULL || delegation != deleg_cur)
315 spin_lock(&delegation->lock);
316 if (!delegation->inode) {
317 spin_unlock(&delegation->lock);
320 list_del_rcu(&delegation->super_list);
321 delegation->inode = NULL;
322 rcu_assign_pointer(nfsi->delegation, NULL);
323 spin_unlock(&delegation->lock);
327 static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi,
328 struct nfs_delegation *delegation,
329 struct nfs_server *server)
331 struct nfs_client *clp = server->nfs_client;
333 spin_lock(&clp->cl_lock);
334 delegation = nfs_detach_delegation_locked(nfsi, delegation, clp);
335 spin_unlock(&clp->cl_lock);
339 static struct nfs_delegation *
340 nfs_inode_detach_delegation(struct inode *inode)
342 struct nfs_inode *nfsi = NFS_I(inode);
343 struct nfs_server *server = NFS_SERVER(inode);
344 struct nfs_delegation *delegation;
347 delegation = rcu_dereference(nfsi->delegation);
348 if (delegation != NULL)
349 delegation = nfs_detach_delegation(nfsi, delegation, server);
355 nfs_update_inplace_delegation(struct nfs_delegation *delegation,
356 const struct nfs_delegation *update)
358 if (nfs4_stateid_is_newer(&update->stateid, &delegation->stateid)) {
359 delegation->stateid.seqid = update->stateid.seqid;
361 delegation->type = update->type;
362 if (test_and_clear_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
363 atomic_long_inc(&nfs_active_delegations);
368 * nfs_inode_set_delegation - set up a delegation on an inode
369 * @inode: inode to which delegation applies
370 * @cred: cred to use for subsequent delegation processing
371 * @type: delegation type
372 * @stateid: delegation stateid
373 * @pagemod_limit: write delegation "space_limit"
375 * Returns zero on success, or a negative errno value.
377 int nfs_inode_set_delegation(struct inode *inode, const struct cred *cred,
379 const nfs4_stateid *stateid,
380 unsigned long pagemod_limit)
382 struct nfs_server *server = NFS_SERVER(inode);
383 struct nfs_client *clp = server->nfs_client;
384 struct nfs_inode *nfsi = NFS_I(inode);
385 struct nfs_delegation *delegation, *old_delegation;
386 struct nfs_delegation *freeme = NULL;
389 delegation = kmalloc(sizeof(*delegation), GFP_NOFS);
390 if (delegation == NULL)
392 nfs4_stateid_copy(&delegation->stateid, stateid);
393 delegation->type = type;
394 delegation->pagemod_limit = pagemod_limit;
395 delegation->change_attr = inode_peek_iversion_raw(inode);
396 delegation->cred = get_cred(cred);
397 delegation->inode = inode;
398 delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
399 spin_lock_init(&delegation->lock);
401 spin_lock(&clp->cl_lock);
402 old_delegation = rcu_dereference_protected(nfsi->delegation,
403 lockdep_is_held(&clp->cl_lock));
404 if (old_delegation == NULL)
406 /* Is this an update of the existing delegation? */
407 if (nfs4_stateid_match_other(&old_delegation->stateid,
408 &delegation->stateid)) {
409 spin_lock(&old_delegation->lock);
410 nfs_update_inplace_delegation(old_delegation,
412 spin_unlock(&old_delegation->lock);
415 if (!test_bit(NFS_DELEGATION_REVOKED, &old_delegation->flags)) {
417 * Deal with broken servers that hand out two
418 * delegations for the same file.
419 * Allow for upgrades to a WRITE delegation, but
422 dfprintk(FILE, "%s: server %s handed out "
423 "a duplicate delegation!\n",
424 __func__, clp->cl_hostname);
425 if (delegation->type == old_delegation->type ||
426 !(delegation->type & FMODE_WRITE)) {
431 if (test_and_set_bit(NFS_DELEGATION_RETURNING,
432 &old_delegation->flags))
435 freeme = nfs_detach_delegation_locked(nfsi, old_delegation, clp);
439 list_add_tail_rcu(&delegation->super_list, &server->delegations);
440 rcu_assign_pointer(nfsi->delegation, delegation);
443 atomic_long_inc(&nfs_active_delegations);
445 trace_nfs4_set_delegation(inode, type);
447 spin_lock(&inode->i_lock);
448 if (NFS_I(inode)->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ATIME))
449 NFS_I(inode)->cache_validity |= NFS_INO_REVAL_FORCED;
450 spin_unlock(&inode->i_lock);
452 spin_unlock(&clp->cl_lock);
453 if (delegation != NULL)
454 __nfs_free_delegation(delegation);
455 if (freeme != NULL) {
456 nfs_do_return_delegation(inode, freeme, 0);
457 nfs_free_delegation(freeme);
463 * Basic procedure for returning a delegation to the server
465 static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync)
467 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
470 if (delegation == NULL)
473 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
475 err = nfs_delegation_claim_opens(inode, &delegation->stateid,
477 if (!issync || err != -EAGAIN)
480 * Guard against state recovery
482 err = nfs4_wait_clnt_recover(clp);
486 nfs_abort_delegation_return(delegation, clp);
490 err = nfs_do_return_delegation(inode, delegation, issync);
495 static bool nfs_delegation_need_return(struct nfs_delegation *delegation)
499 if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
501 else if (test_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags)) {
504 spin_lock(&delegation->lock);
505 inode = delegation->inode;
506 if (inode && list_empty(&NFS_I(inode)->open_files))
508 spin_unlock(&delegation->lock);
511 clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
512 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags) ||
513 test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
520 * nfs_client_return_marked_delegations - return previously marked delegations
521 * @clp: nfs_client to process
523 * Note that this function is designed to be called by the state
524 * manager thread. For this reason, it cannot flush the dirty data,
525 * since that could deadlock in case of a state recovery error.
527 * Returns zero on success, or a negative errno value.
529 int nfs_client_return_marked_delegations(struct nfs_client *clp)
531 struct nfs_delegation *delegation;
532 struct nfs_delegation *prev;
533 struct nfs_server *server;
535 struct inode *place_holder = NULL;
536 struct nfs_delegation *place_holder_deleg = NULL;
541 * To avoid quadratic looping we hold a reference
542 * to an inode place_holder. Each time we restart, we
543 * list nfs_servers from the server of that inode, and
544 * delegation in the server from the delegations of that
546 * prev is an RCU-protected pointer to a delegation which
547 * wasn't marked for return and might be a good choice for
548 * the next place_holder.
553 server = NFS_SERVER(place_holder);
555 server = list_entry_rcu(clp->cl_superblocks.next,
556 struct nfs_server, client_link);
557 list_for_each_entry_from_rcu(server, &clp->cl_superblocks, client_link) {
559 if (place_holder && server == NFS_SERVER(place_holder))
560 delegation = rcu_dereference(NFS_I(place_holder)->delegation);
561 if (!delegation || delegation != place_holder_deleg)
562 delegation = list_entry_rcu(server->delegations.next,
563 struct nfs_delegation, super_list);
564 list_for_each_entry_from_rcu(delegation, &server->delegations, super_list) {
565 struct inode *to_put = NULL;
567 if (!nfs_delegation_need_return(delegation)) {
571 if (!nfs_sb_active(server->super))
572 break; /* continue in outer loop */
577 tmp = nfs_delegation_grab_inode(prev);
579 to_put = place_holder;
581 place_holder_deleg = prev;
585 inode = nfs_delegation_grab_inode(delegation);
590 nfs_sb_deactive(server->super);
593 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
599 err = nfs_end_delegation_return(inode, delegation, 0);
601 nfs_sb_deactive(server->super);
605 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
618 * nfs_inode_evict_delegation - return delegation, don't reclaim opens
619 * @inode: inode to process
621 * Does not protect against delegation reclaims, therefore really only safe
622 * to be called from nfs4_clear_inode(). Guaranteed to always free
623 * the delegation structure.
625 void nfs_inode_evict_delegation(struct inode *inode)
627 struct nfs_delegation *delegation;
629 delegation = nfs_inode_detach_delegation(inode);
630 if (delegation != NULL) {
631 set_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
632 set_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags);
633 nfs_do_return_delegation(inode, delegation, 1);
634 nfs_free_delegation(delegation);
639 * nfs_inode_return_delegation - synchronously return a delegation
640 * @inode: inode to process
642 * This routine will always flush any dirty data to disk on the
643 * assumption that if we need to return the delegation, then
644 * we should stop caching.
646 * Returns zero on success, or a negative errno value.
648 int nfs4_inode_return_delegation(struct inode *inode)
650 struct nfs_inode *nfsi = NFS_I(inode);
651 struct nfs_delegation *delegation;
655 delegation = nfs_start_delegation_return(nfsi);
656 if (delegation != NULL)
657 err = nfs_end_delegation_return(inode, delegation, 1);
662 * nfs_inode_return_delegation_on_close - asynchronously return a delegation
663 * @inode: inode to process
665 * This routine is called on file close in order to determine if the
666 * inode delegation needs to be returned immediately.
668 void nfs4_inode_return_delegation_on_close(struct inode *inode)
670 struct nfs_delegation *delegation;
671 struct nfs_delegation *ret = NULL;
676 delegation = nfs4_get_valid_delegation(inode);
679 if (test_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags)) {
680 spin_lock(&delegation->lock);
681 if (delegation->inode &&
682 list_empty(&NFS_I(inode)->open_files) &&
683 !test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) {
684 clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
687 spin_unlock(&delegation->lock);
691 nfs_end_delegation_return(inode, ret, 0);
695 * nfs4_inode_make_writeable
696 * @inode: pointer to inode
698 * Make the inode writeable by returning the delegation if necessary
700 * Returns zero on success, or a negative errno value.
702 int nfs4_inode_make_writeable(struct inode *inode)
704 struct nfs_delegation *delegation;
707 delegation = nfs4_get_valid_delegation(inode);
708 if (delegation == NULL ||
709 (nfs4_has_session(NFS_SERVER(inode)->nfs_client) &&
710 (delegation->type & FMODE_WRITE))) {
715 return nfs4_inode_return_delegation(inode);
718 static void nfs_mark_return_if_closed_delegation(struct nfs_server *server,
719 struct nfs_delegation *delegation)
721 set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
722 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
725 static void nfs_mark_return_delegation(struct nfs_server *server,
726 struct nfs_delegation *delegation)
728 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
729 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
732 static bool nfs_server_mark_return_all_delegations(struct nfs_server *server)
734 struct nfs_delegation *delegation;
737 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
738 nfs_mark_return_delegation(server, delegation);
744 static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
746 struct nfs_server *server;
749 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
750 nfs_server_mark_return_all_delegations(server);
754 static void nfs_delegation_run_state_manager(struct nfs_client *clp)
756 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
757 nfs4_schedule_state_manager(clp);
761 * nfs_expire_all_delegations
762 * @clp: client to process
765 void nfs_expire_all_delegations(struct nfs_client *clp)
767 nfs_client_mark_return_all_delegations(clp);
768 nfs_delegation_run_state_manager(clp);
772 * nfs_super_return_all_delegations - return delegations for one superblock
773 * @server: pointer to nfs_server to process
776 void nfs_server_return_all_delegations(struct nfs_server *server)
778 struct nfs_client *clp = server->nfs_client;
785 need_wait = nfs_server_mark_return_all_delegations(server);
789 nfs4_schedule_state_manager(clp);
790 nfs4_wait_clnt_recover(clp);
794 static void nfs_mark_return_unused_delegation_types(struct nfs_server *server,
797 struct nfs_delegation *delegation;
799 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
800 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
802 if (delegation->type & flags)
803 nfs_mark_return_if_closed_delegation(server, delegation);
807 static void nfs_client_mark_return_unused_delegation_types(struct nfs_client *clp,
810 struct nfs_server *server;
813 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
814 nfs_mark_return_unused_delegation_types(server, flags);
818 static void nfs_revoke_delegation(struct inode *inode,
819 const nfs4_stateid *stateid)
821 struct nfs_delegation *delegation;
826 delegation = rcu_dereference(NFS_I(inode)->delegation);
827 if (delegation == NULL)
829 if (stateid == NULL) {
830 nfs4_stateid_copy(&tmp, &delegation->stateid);
833 if (!nfs4_stateid_match_other(stateid, &delegation->stateid))
835 spin_lock(&delegation->lock);
836 if (stateid->seqid) {
837 if (nfs4_stateid_is_newer(&delegation->stateid, stateid)) {
838 spin_unlock(&delegation->lock);
841 delegation->stateid.seqid = stateid->seqid;
843 spin_unlock(&delegation->lock);
845 nfs_mark_delegation_revoked(delegation);
850 nfs_inode_find_state_and_recover(inode, stateid);
853 void nfs_remove_bad_delegation(struct inode *inode,
854 const nfs4_stateid *stateid)
856 nfs_revoke_delegation(inode, stateid);
858 EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation);
860 void nfs_delegation_mark_returned(struct inode *inode,
861 const nfs4_stateid *stateid)
863 struct nfs_delegation *delegation;
869 delegation = rcu_dereference(NFS_I(inode)->delegation);
873 spin_lock(&delegation->lock);
874 if (!nfs4_stateid_match_other(stateid, &delegation->stateid))
875 goto out_spin_unlock;
876 if (stateid->seqid) {
877 /* If delegation->stateid is newer, dont mark as returned */
878 if (nfs4_stateid_is_newer(&delegation->stateid, stateid))
879 goto out_clear_returning;
880 if (delegation->stateid.seqid != stateid->seqid)
881 delegation->stateid.seqid = stateid->seqid;
884 nfs_mark_delegation_revoked(delegation);
887 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
889 spin_unlock(&delegation->lock);
893 nfs_inode_find_state_and_recover(inode, stateid);
897 * nfs_expire_unused_delegation_types
898 * @clp: client to process
899 * @flags: delegation types to expire
902 void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags)
904 nfs_client_mark_return_unused_delegation_types(clp, flags);
905 nfs_delegation_run_state_manager(clp);
908 static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
910 struct nfs_delegation *delegation;
912 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
913 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
915 nfs_mark_return_if_closed_delegation(server, delegation);
920 * nfs_expire_unreferenced_delegations - Eliminate unused delegations
921 * @clp: nfs_client to process
924 void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
926 struct nfs_server *server;
929 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
930 nfs_mark_return_unreferenced_delegations(server);
933 nfs_delegation_run_state_manager(clp);
937 * nfs_async_inode_return_delegation - asynchronously return a delegation
938 * @inode: inode to process
939 * @stateid: state ID information
941 * Returns zero on success, or a negative errno value.
943 int nfs_async_inode_return_delegation(struct inode *inode,
944 const nfs4_stateid *stateid)
946 struct nfs_server *server = NFS_SERVER(inode);
947 struct nfs_client *clp = server->nfs_client;
948 struct nfs_delegation *delegation;
951 delegation = nfs4_get_valid_delegation(inode);
952 if (delegation == NULL)
954 if (stateid != NULL &&
955 !clp->cl_mvops->match_stateid(&delegation->stateid, stateid))
957 nfs_mark_return_delegation(server, delegation);
960 nfs_delegation_run_state_manager(clp);
967 static struct inode *
968 nfs_delegation_find_inode_server(struct nfs_server *server,
969 const struct nfs_fh *fhandle)
971 struct nfs_delegation *delegation;
972 struct inode *freeme, *res = NULL;
974 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
975 spin_lock(&delegation->lock);
976 if (delegation->inode != NULL &&
977 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) &&
978 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
979 freeme = igrab(delegation->inode);
980 if (freeme && nfs_sb_active(freeme->i_sb))
982 spin_unlock(&delegation->lock);
990 return ERR_PTR(-EAGAIN);
992 spin_unlock(&delegation->lock);
994 return ERR_PTR(-ENOENT);
998 * nfs_delegation_find_inode - retrieve the inode associated with a delegation
999 * @clp: client state handle
1000 * @fhandle: filehandle from a delegation recall
1002 * Returns pointer to inode matching "fhandle," or NULL if a matching inode
1005 struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
1006 const struct nfs_fh *fhandle)
1008 struct nfs_server *server;
1012 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1013 res = nfs_delegation_find_inode_server(server, fhandle);
1014 if (res != ERR_PTR(-ENOENT)) {
1020 return ERR_PTR(-ENOENT);
1023 static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
1025 struct nfs_delegation *delegation;
1027 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
1029 * If the delegation may have been admin revoked, then we
1030 * cannot reclaim it.
1032 if (test_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags))
1034 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
1039 * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
1040 * @clp: nfs_client to process
1043 void nfs_delegation_mark_reclaim(struct nfs_client *clp)
1045 struct nfs_server *server;
1048 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1049 nfs_delegation_mark_reclaim_server(server);
1054 * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
1055 * @clp: nfs_client to process
1058 void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
1060 struct nfs_delegation *delegation;
1061 struct nfs_server *server;
1062 struct inode *inode;
1066 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1067 list_for_each_entry_rcu(delegation, &server->delegations,
1069 if (test_bit(NFS_DELEGATION_INODE_FREEING,
1070 &delegation->flags) ||
1071 test_bit(NFS_DELEGATION_RETURNING,
1072 &delegation->flags) ||
1073 test_bit(NFS_DELEGATION_NEED_RECLAIM,
1074 &delegation->flags) == 0)
1076 if (!nfs_sb_active(server->super))
1077 break; /* continue in outer loop */
1078 inode = nfs_delegation_grab_inode(delegation);
1079 if (inode == NULL) {
1081 nfs_sb_deactive(server->super);
1084 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
1086 if (delegation != NULL) {
1087 delegation = nfs_detach_delegation(NFS_I(inode),
1088 delegation, server);
1089 if (delegation != NULL)
1090 nfs_free_delegation(delegation);
1093 nfs_sb_deactive(server->super);
1101 static inline bool nfs4_server_rebooted(const struct nfs_client *clp)
1103 return (clp->cl_state & (BIT(NFS4CLNT_CHECK_LEASE) |
1104 BIT(NFS4CLNT_LEASE_EXPIRED) |
1105 BIT(NFS4CLNT_SESSION_RESET))) != 0;
1108 static void nfs_mark_test_expired_delegation(struct nfs_server *server,
1109 struct nfs_delegation *delegation)
1111 if (delegation->stateid.type == NFS4_INVALID_STATEID_TYPE)
1113 clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
1114 set_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
1115 set_bit(NFS4CLNT_DELEGATION_EXPIRED, &server->nfs_client->cl_state);
1118 static void nfs_inode_mark_test_expired_delegation(struct nfs_server *server,
1119 struct inode *inode)
1121 struct nfs_delegation *delegation;
1124 delegation = rcu_dereference(NFS_I(inode)->delegation);
1126 nfs_mark_test_expired_delegation(server, delegation);
1131 static void nfs_delegation_mark_test_expired_server(struct nfs_server *server)
1133 struct nfs_delegation *delegation;
1135 list_for_each_entry_rcu(delegation, &server->delegations, super_list)
1136 nfs_mark_test_expired_delegation(server, delegation);
1140 * nfs_mark_test_expired_all_delegations - mark all delegations for testing
1141 * @clp: nfs_client to process
1143 * Iterates through all the delegations associated with this server and
1144 * marks them as needing to be checked for validity.
1146 void nfs_mark_test_expired_all_delegations(struct nfs_client *clp)
1148 struct nfs_server *server;
1151 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1152 nfs_delegation_mark_test_expired_server(server);
1157 * nfs_test_expired_all_delegations - test all delegations for a client
1158 * @clp: nfs_client to process
1160 * Helper for handling "recallable state revoked" status from server.
1162 void nfs_test_expired_all_delegations(struct nfs_client *clp)
1164 nfs_mark_test_expired_all_delegations(clp);
1165 nfs4_schedule_state_manager(clp);
1169 nfs_delegation_test_free_expired(struct inode *inode,
1170 nfs4_stateid *stateid,
1171 const struct cred *cred)
1173 struct nfs_server *server = NFS_SERVER(inode);
1174 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
1179 status = ops->test_and_free_expired(server, stateid, cred);
1180 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
1181 nfs_remove_bad_delegation(inode, stateid);
1185 * nfs_reap_expired_delegations - reap expired delegations
1186 * @clp: nfs_client to process
1188 * Iterates through all the delegations associated with this server and
1189 * checks if they have may have been revoked. This function is usually
1190 * expected to be called in cases where the server may have lost its
1193 void nfs_reap_expired_delegations(struct nfs_client *clp)
1195 struct nfs_delegation *delegation;
1196 struct nfs_server *server;
1197 struct inode *inode;
1198 const struct cred *cred;
1199 nfs4_stateid stateid;
1203 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1204 list_for_each_entry_rcu(delegation, &server->delegations,
1206 if (test_bit(NFS_DELEGATION_INODE_FREEING,
1207 &delegation->flags) ||
1208 test_bit(NFS_DELEGATION_RETURNING,
1209 &delegation->flags) ||
1210 test_bit(NFS_DELEGATION_TEST_EXPIRED,
1211 &delegation->flags) == 0)
1213 if (!nfs_sb_active(server->super))
1214 break; /* continue in outer loop */
1215 inode = nfs_delegation_grab_inode(delegation);
1216 if (inode == NULL) {
1218 nfs_sb_deactive(server->super);
1221 cred = get_cred_rcu(delegation->cred);
1222 nfs4_stateid_copy(&stateid, &delegation->stateid);
1223 clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
1225 nfs_delegation_test_free_expired(inode, &stateid, cred);
1227 if (nfs4_server_rebooted(clp)) {
1228 nfs_inode_mark_test_expired_delegation(server,inode);
1230 nfs_sb_deactive(server->super);
1234 nfs_sb_deactive(server->super);
1242 void nfs_inode_find_delegation_state_and_recover(struct inode *inode,
1243 const nfs4_stateid *stateid)
1245 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1246 struct nfs_delegation *delegation;
1250 delegation = rcu_dereference(NFS_I(inode)->delegation);
1252 nfs4_stateid_match_or_older(&delegation->stateid, stateid) &&
1253 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
1254 nfs_mark_test_expired_delegation(NFS_SERVER(inode), delegation);
1259 nfs4_schedule_state_manager(clp);
1263 * nfs_delegations_present - check for existence of delegations
1264 * @clp: client state handle
1266 * Returns one if there are any nfs_delegation structures attached
1267 * to this nfs_client.
1269 int nfs_delegations_present(struct nfs_client *clp)
1271 struct nfs_server *server;
1275 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1276 if (!list_empty(&server->delegations)) {
1285 * nfs4_refresh_delegation_stateid - Update delegation stateid seqid
1286 * @dst: stateid to refresh
1287 * @inode: inode to check
1289 * Returns "true" and updates "dst->seqid" * if inode had a delegation
1290 * that matches our delegation stateid. Otherwise "false" is returned.
1292 bool nfs4_refresh_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
1294 struct nfs_delegation *delegation;
1300 delegation = rcu_dereference(NFS_I(inode)->delegation);
1301 if (delegation != NULL &&
1302 nfs4_stateid_match_other(dst, &delegation->stateid) &&
1303 nfs4_stateid_is_newer(&delegation->stateid, dst) &&
1304 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
1305 dst->seqid = delegation->stateid.seqid;
1314 * nfs4_copy_delegation_stateid - Copy inode's state ID information
1315 * @inode: inode to check
1316 * @flags: delegation type requirement
1317 * @dst: stateid data structure to fill in
1318 * @cred: optional argument to retrieve credential
1320 * Returns "true" and fills in "dst->data" * if inode had a delegation,
1321 * otherwise "false" is returned.
1323 bool nfs4_copy_delegation_stateid(struct inode *inode, fmode_t flags,
1324 nfs4_stateid *dst, const struct cred **cred)
1326 struct nfs_inode *nfsi = NFS_I(inode);
1327 struct nfs_delegation *delegation;
1330 flags &= FMODE_READ|FMODE_WRITE;
1332 delegation = rcu_dereference(nfsi->delegation);
1333 ret = nfs4_is_valid_delegation(delegation, flags);
1335 nfs4_stateid_copy(dst, &delegation->stateid);
1336 nfs_mark_delegation_referenced(delegation);
1338 *cred = get_cred(delegation->cred);
1345 * nfs4_delegation_flush_on_close - Check if we must flush file on close
1346 * @inode: inode to check
1348 * This function checks the number of outstanding writes to the file
1349 * against the delegation 'space_limit' field to see if
1350 * the spec requires us to flush the file on close.
1352 bool nfs4_delegation_flush_on_close(const struct inode *inode)
1354 struct nfs_inode *nfsi = NFS_I(inode);
1355 struct nfs_delegation *delegation;
1359 delegation = rcu_dereference(nfsi->delegation);
1360 if (delegation == NULL || !(delegation->type & FMODE_WRITE))
1362 if (atomic_long_read(&nfsi->nrequests) < delegation->pagemod_limit)