1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/fs/nfs/delegation.c
5 * Copyright (C) 2004 Trond Myklebust
7 * NFS file delegation management
10 #include <linux/completion.h>
11 #include <linux/kthread.h>
12 #include <linux/module.h>
13 #include <linux/sched.h>
14 #include <linux/slab.h>
15 #include <linux/spinlock.h>
16 #include <linux/iversion.h>
18 #include <linux/nfs4.h>
19 #include <linux/nfs_fs.h>
20 #include <linux/nfs_xdr.h>
23 #include "nfs4session.h"
24 #include "delegation.h"
26 #include "nfs4trace.h"
28 #define NFS_DEFAULT_DELEGATION_WATERMARK (5000U)
30 static atomic_long_t nfs_active_delegations;
31 static unsigned nfs_delegation_watermark = NFS_DEFAULT_DELEGATION_WATERMARK;
33 static void __nfs_free_delegation(struct nfs_delegation *delegation)
35 put_cred(delegation->cred);
36 delegation->cred = NULL;
37 kfree_rcu(delegation, rcu);
40 static void nfs_mark_delegation_revoked(struct nfs_delegation *delegation)
42 if (!test_and_set_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
43 delegation->stateid.type = NFS4_INVALID_STATEID_TYPE;
44 atomic_long_dec(&nfs_active_delegations);
45 if (!test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
46 nfs_clear_verifier_delegated(delegation->inode);
50 static struct nfs_delegation *nfs_get_delegation(struct nfs_delegation *delegation)
52 refcount_inc(&delegation->refcount);
56 static void nfs_put_delegation(struct nfs_delegation *delegation)
58 if (refcount_dec_and_test(&delegation->refcount))
59 __nfs_free_delegation(delegation);
62 static void nfs_free_delegation(struct nfs_delegation *delegation)
64 nfs_mark_delegation_revoked(delegation);
65 nfs_put_delegation(delegation);
69 * nfs_mark_delegation_referenced - set delegation's REFERENCED flag
70 * @delegation: delegation to process
73 void nfs_mark_delegation_referenced(struct nfs_delegation *delegation)
75 set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags);
79 nfs4_is_valid_delegation(const struct nfs_delegation *delegation,
82 if (delegation != NULL && (delegation->type & flags) == flags &&
83 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) &&
84 !test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
89 struct nfs_delegation *nfs4_get_valid_delegation(const struct inode *inode)
91 struct nfs_delegation *delegation;
93 delegation = rcu_dereference(NFS_I(inode)->delegation);
94 if (nfs4_is_valid_delegation(delegation, 0))
100 nfs4_do_check_delegation(struct inode *inode, fmode_t flags, bool mark)
102 struct nfs_delegation *delegation;
105 flags &= FMODE_READ|FMODE_WRITE;
107 delegation = rcu_dereference(NFS_I(inode)->delegation);
108 if (nfs4_is_valid_delegation(delegation, flags)) {
110 nfs_mark_delegation_referenced(delegation);
117 * nfs_have_delegation - check if inode has a delegation, mark it
118 * NFS_DELEGATION_REFERENCED if there is one.
119 * @inode: inode to check
120 * @flags: delegation types to check for
122 * Returns one if inode has the indicated delegation, otherwise zero.
124 int nfs4_have_delegation(struct inode *inode, fmode_t flags)
126 return nfs4_do_check_delegation(inode, flags, true);
130 * nfs4_check_delegation - check if inode has a delegation, do not mark
131 * NFS_DELEGATION_REFERENCED if it has one.
133 int nfs4_check_delegation(struct inode *inode, fmode_t flags)
135 return nfs4_do_check_delegation(inode, flags, false);
138 static int nfs_delegation_claim_locks(struct nfs4_state *state, const nfs4_stateid *stateid)
140 struct inode *inode = state->inode;
141 struct file_lock *fl;
142 struct file_lock_context *flctx = inode->i_flctx;
143 struct list_head *list;
149 list = &flctx->flc_posix;
150 spin_lock(&flctx->flc_lock);
152 list_for_each_entry(fl, list, fl_list) {
153 if (nfs_file_open_context(fl->fl_file)->state != state)
155 spin_unlock(&flctx->flc_lock);
156 status = nfs4_lock_delegation_recall(fl, state, stateid);
159 spin_lock(&flctx->flc_lock);
161 if (list == &flctx->flc_posix) {
162 list = &flctx->flc_flock;
165 spin_unlock(&flctx->flc_lock);
170 static int nfs_delegation_claim_opens(struct inode *inode,
171 const nfs4_stateid *stateid, fmode_t type)
173 struct nfs_inode *nfsi = NFS_I(inode);
174 struct nfs_open_context *ctx;
175 struct nfs4_state_owner *sp;
176 struct nfs4_state *state;
182 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
186 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
188 if (!nfs4_valid_open_stateid(state))
190 if (!nfs4_stateid_match(&state->stateid, stateid))
192 if (!get_nfs_open_context(ctx))
196 /* Block nfs4_proc_unlck */
197 mutex_lock(&sp->so_delegreturn_mutex);
198 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
199 err = nfs4_open_delegation_recall(ctx, state, stateid);
201 err = nfs_delegation_claim_locks(state, stateid);
202 if (!err && read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
204 mutex_unlock(&sp->so_delegreturn_mutex);
205 put_nfs_open_context(ctx);
215 * nfs_inode_reclaim_delegation - process a delegation reclaim request
216 * @inode: inode to process
217 * @cred: credential to use for request
218 * @type: delegation type
219 * @stateid: delegation stateid
220 * @pagemod_limit: write delegation "space_limit"
223 void nfs_inode_reclaim_delegation(struct inode *inode, const struct cred *cred,
225 const nfs4_stateid *stateid,
226 unsigned long pagemod_limit)
228 struct nfs_delegation *delegation;
229 const struct cred *oldcred = NULL;
232 delegation = rcu_dereference(NFS_I(inode)->delegation);
233 if (delegation != NULL) {
234 spin_lock(&delegation->lock);
235 if (nfs4_is_valid_delegation(delegation, 0)) {
236 nfs4_stateid_copy(&delegation->stateid, stateid);
237 delegation->type = type;
238 delegation->pagemod_limit = pagemod_limit;
239 oldcred = delegation->cred;
240 delegation->cred = get_cred(cred);
241 clear_bit(NFS_DELEGATION_NEED_RECLAIM,
243 spin_unlock(&delegation->lock);
246 trace_nfs4_reclaim_delegation(inode, type);
249 /* We appear to have raced with a delegation return. */
250 spin_unlock(&delegation->lock);
253 nfs_inode_set_delegation(inode, cred, type, stateid, pagemod_limit);
256 static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
258 const struct cred *cred;
261 if (!test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
262 spin_lock(&delegation->lock);
263 cred = get_cred(delegation->cred);
264 spin_unlock(&delegation->lock);
265 res = nfs4_proc_delegreturn(inode, cred,
266 &delegation->stateid,
273 static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
275 struct inode *inode = NULL;
277 spin_lock(&delegation->lock);
278 if (delegation->inode != NULL)
279 inode = igrab(delegation->inode);
281 set_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags);
282 spin_unlock(&delegation->lock);
286 static struct nfs_delegation *
287 nfs_start_delegation_return_locked(struct nfs_inode *nfsi)
289 struct nfs_delegation *ret = NULL;
290 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
292 if (delegation == NULL)
294 spin_lock(&delegation->lock);
295 if (!test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) {
296 /* Refcount matched in nfs_end_delegation_return() */
297 ret = nfs_get_delegation(delegation);
299 spin_unlock(&delegation->lock);
301 nfs_clear_verifier_delegated(&nfsi->vfs_inode);
306 static struct nfs_delegation *
307 nfs_start_delegation_return(struct nfs_inode *nfsi)
309 struct nfs_delegation *delegation;
312 delegation = nfs_start_delegation_return_locked(nfsi);
318 nfs_abort_delegation_return(struct nfs_delegation *delegation,
319 struct nfs_client *clp)
322 spin_lock(&delegation->lock);
323 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
324 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
325 spin_unlock(&delegation->lock);
326 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
329 static struct nfs_delegation *
330 nfs_detach_delegation_locked(struct nfs_inode *nfsi,
331 struct nfs_delegation *delegation,
332 struct nfs_client *clp)
334 struct nfs_delegation *deleg_cur =
335 rcu_dereference_protected(nfsi->delegation,
336 lockdep_is_held(&clp->cl_lock));
338 if (deleg_cur == NULL || delegation != deleg_cur)
341 spin_lock(&delegation->lock);
342 if (!delegation->inode) {
343 spin_unlock(&delegation->lock);
346 list_del_rcu(&delegation->super_list);
347 delegation->inode = NULL;
348 rcu_assign_pointer(nfsi->delegation, NULL);
349 spin_unlock(&delegation->lock);
353 static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi,
354 struct nfs_delegation *delegation,
355 struct nfs_server *server)
357 struct nfs_client *clp = server->nfs_client;
359 spin_lock(&clp->cl_lock);
360 delegation = nfs_detach_delegation_locked(nfsi, delegation, clp);
361 spin_unlock(&clp->cl_lock);
365 static struct nfs_delegation *
366 nfs_inode_detach_delegation(struct inode *inode)
368 struct nfs_inode *nfsi = NFS_I(inode);
369 struct nfs_server *server = NFS_SERVER(inode);
370 struct nfs_delegation *delegation;
373 delegation = rcu_dereference(nfsi->delegation);
374 if (delegation != NULL)
375 delegation = nfs_detach_delegation(nfsi, delegation, server);
381 nfs_update_delegation_cred(struct nfs_delegation *delegation,
382 const struct cred *cred)
384 const struct cred *old;
386 if (cred_fscmp(delegation->cred, cred) != 0) {
387 old = xchg(&delegation->cred, get_cred(cred));
393 nfs_update_inplace_delegation(struct nfs_delegation *delegation,
394 const struct nfs_delegation *update)
396 if (nfs4_stateid_is_newer(&update->stateid, &delegation->stateid)) {
397 delegation->stateid.seqid = update->stateid.seqid;
399 delegation->type = update->type;
400 delegation->pagemod_limit = update->pagemod_limit;
401 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
402 delegation->change_attr = update->change_attr;
403 nfs_update_delegation_cred(delegation, update->cred);
404 /* smp_mb__before_atomic() is implicit due to xchg() */
405 clear_bit(NFS_DELEGATION_REVOKED, &delegation->flags);
406 atomic_long_inc(&nfs_active_delegations);
412 * nfs_inode_set_delegation - set up a delegation on an inode
413 * @inode: inode to which delegation applies
414 * @cred: cred to use for subsequent delegation processing
415 * @type: delegation type
416 * @stateid: delegation stateid
417 * @pagemod_limit: write delegation "space_limit"
419 * Returns zero on success, or a negative errno value.
421 int nfs_inode_set_delegation(struct inode *inode, const struct cred *cred,
423 const nfs4_stateid *stateid,
424 unsigned long pagemod_limit)
426 struct nfs_server *server = NFS_SERVER(inode);
427 struct nfs_client *clp = server->nfs_client;
428 struct nfs_inode *nfsi = NFS_I(inode);
429 struct nfs_delegation *delegation, *old_delegation;
430 struct nfs_delegation *freeme = NULL;
433 delegation = kmalloc(sizeof(*delegation), GFP_NOFS);
434 if (delegation == NULL)
436 nfs4_stateid_copy(&delegation->stateid, stateid);
437 refcount_set(&delegation->refcount, 1);
438 delegation->type = type;
439 delegation->pagemod_limit = pagemod_limit;
440 delegation->change_attr = inode_peek_iversion_raw(inode);
441 delegation->cred = get_cred(cred);
442 delegation->inode = inode;
443 delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
444 spin_lock_init(&delegation->lock);
446 spin_lock(&clp->cl_lock);
447 old_delegation = rcu_dereference_protected(nfsi->delegation,
448 lockdep_is_held(&clp->cl_lock));
449 if (old_delegation == NULL)
451 /* Is this an update of the existing delegation? */
452 if (nfs4_stateid_match_other(&old_delegation->stateid,
453 &delegation->stateid)) {
454 spin_lock(&old_delegation->lock);
455 nfs_update_inplace_delegation(old_delegation,
457 spin_unlock(&old_delegation->lock);
460 if (!test_bit(NFS_DELEGATION_REVOKED, &old_delegation->flags)) {
462 * Deal with broken servers that hand out two
463 * delegations for the same file.
464 * Allow for upgrades to a WRITE delegation, but
467 dfprintk(FILE, "%s: server %s handed out "
468 "a duplicate delegation!\n",
469 __func__, clp->cl_hostname);
470 if (delegation->type == old_delegation->type ||
471 !(delegation->type & FMODE_WRITE)) {
476 if (test_and_set_bit(NFS_DELEGATION_RETURNING,
477 &old_delegation->flags))
480 freeme = nfs_detach_delegation_locked(nfsi, old_delegation, clp);
484 list_add_tail_rcu(&delegation->super_list, &server->delegations);
485 rcu_assign_pointer(nfsi->delegation, delegation);
488 atomic_long_inc(&nfs_active_delegations);
490 trace_nfs4_set_delegation(inode, type);
492 spin_lock(&inode->i_lock);
493 if (NFS_I(inode)->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ATIME))
494 NFS_I(inode)->cache_validity |= NFS_INO_REVAL_FORCED;
495 spin_unlock(&inode->i_lock);
497 spin_unlock(&clp->cl_lock);
498 if (delegation != NULL)
499 __nfs_free_delegation(delegation);
500 if (freeme != NULL) {
501 nfs_do_return_delegation(inode, freeme, 0);
502 nfs_free_delegation(freeme);
508 * Basic procedure for returning a delegation to the server
510 static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync)
512 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
515 if (delegation == NULL)
518 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
520 err = nfs_delegation_claim_opens(inode, &delegation->stateid,
522 if (!issync || err != -EAGAIN)
525 * Guard against state recovery
527 err = nfs4_wait_clnt_recover(clp);
531 nfs_abort_delegation_return(delegation, clp);
535 err = nfs_do_return_delegation(inode, delegation, issync);
537 /* Refcount matched in nfs_start_delegation_return_locked() */
538 nfs_put_delegation(delegation);
542 static bool nfs_delegation_need_return(struct nfs_delegation *delegation)
546 if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
548 else if (test_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags)) {
551 spin_lock(&delegation->lock);
552 inode = delegation->inode;
553 if (inode && list_empty(&NFS_I(inode)->open_files))
555 spin_unlock(&delegation->lock);
558 clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
559 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags) ||
560 test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
567 * nfs_client_return_marked_delegations - return previously marked delegations
568 * @clp: nfs_client to process
570 * Note that this function is designed to be called by the state
571 * manager thread. For this reason, it cannot flush the dirty data,
572 * since that could deadlock in case of a state recovery error.
574 * Returns zero on success, or a negative errno value.
576 int nfs_client_return_marked_delegations(struct nfs_client *clp)
578 struct nfs_delegation *delegation;
579 struct nfs_delegation *prev;
580 struct nfs_server *server;
582 struct inode *place_holder = NULL;
583 struct nfs_delegation *place_holder_deleg = NULL;
588 * To avoid quadratic looping we hold a reference
589 * to an inode place_holder. Each time we restart, we
590 * list nfs_servers from the server of that inode, and
591 * delegation in the server from the delegations of that
593 * prev is an RCU-protected pointer to a delegation which
594 * wasn't marked for return and might be a good choice for
595 * the next place_holder.
600 server = NFS_SERVER(place_holder);
602 server = list_entry_rcu(clp->cl_superblocks.next,
603 struct nfs_server, client_link);
604 list_for_each_entry_from_rcu(server, &clp->cl_superblocks, client_link) {
606 if (place_holder && server == NFS_SERVER(place_holder))
607 delegation = rcu_dereference(NFS_I(place_holder)->delegation);
608 if (!delegation || delegation != place_holder_deleg)
609 delegation = list_entry_rcu(server->delegations.next,
610 struct nfs_delegation, super_list);
611 list_for_each_entry_from_rcu(delegation, &server->delegations, super_list) {
612 struct inode *to_put = NULL;
614 if (!nfs_delegation_need_return(delegation)) {
618 if (!nfs_sb_active(server->super))
619 break; /* continue in outer loop */
624 tmp = nfs_delegation_grab_inode(prev);
626 to_put = place_holder;
628 place_holder_deleg = prev;
632 inode = nfs_delegation_grab_inode(delegation);
637 nfs_sb_deactive(server->super);
640 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
646 err = nfs_end_delegation_return(inode, delegation, 0);
648 nfs_sb_deactive(server->super);
652 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
665 * nfs_inode_evict_delegation - return delegation, don't reclaim opens
666 * @inode: inode to process
668 * Does not protect against delegation reclaims, therefore really only safe
669 * to be called from nfs4_clear_inode(). Guaranteed to always free
670 * the delegation structure.
672 void nfs_inode_evict_delegation(struct inode *inode)
674 struct nfs_delegation *delegation;
676 delegation = nfs_inode_detach_delegation(inode);
677 if (delegation != NULL) {
678 set_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
679 set_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags);
680 nfs_do_return_delegation(inode, delegation, 1);
681 nfs_free_delegation(delegation);
686 * nfs_inode_return_delegation - synchronously return a delegation
687 * @inode: inode to process
689 * This routine will always flush any dirty data to disk on the
690 * assumption that if we need to return the delegation, then
691 * we should stop caching.
693 * Returns zero on success, or a negative errno value.
695 int nfs4_inode_return_delegation(struct inode *inode)
697 struct nfs_inode *nfsi = NFS_I(inode);
698 struct nfs_delegation *delegation;
702 delegation = nfs_start_delegation_return(nfsi);
703 if (delegation != NULL)
704 err = nfs_end_delegation_return(inode, delegation, 1);
709 * nfs_inode_return_delegation_on_close - asynchronously return a delegation
710 * @inode: inode to process
712 * This routine is called on file close in order to determine if the
713 * inode delegation needs to be returned immediately.
715 void nfs4_inode_return_delegation_on_close(struct inode *inode)
717 struct nfs_delegation *delegation;
718 struct nfs_delegation *ret = NULL;
723 delegation = nfs4_get_valid_delegation(inode);
726 if (test_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags) ||
727 atomic_long_read(&nfs_active_delegations) >= nfs_delegation_watermark) {
728 spin_lock(&delegation->lock);
729 if (delegation->inode &&
730 list_empty(&NFS_I(inode)->open_files) &&
731 !test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) {
732 clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
733 /* Refcount matched in nfs_end_delegation_return() */
734 ret = nfs_get_delegation(delegation);
736 spin_unlock(&delegation->lock);
738 nfs_clear_verifier_delegated(inode);
742 nfs_end_delegation_return(inode, ret, 0);
746 * nfs4_inode_make_writeable
747 * @inode: pointer to inode
749 * Make the inode writeable by returning the delegation if necessary
751 * Returns zero on success, or a negative errno value.
753 int nfs4_inode_make_writeable(struct inode *inode)
755 struct nfs_delegation *delegation;
758 delegation = nfs4_get_valid_delegation(inode);
759 if (delegation == NULL ||
760 (nfs4_has_session(NFS_SERVER(inode)->nfs_client) &&
761 (delegation->type & FMODE_WRITE))) {
766 return nfs4_inode_return_delegation(inode);
769 static void nfs_mark_return_if_closed_delegation(struct nfs_server *server,
770 struct nfs_delegation *delegation)
772 set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
773 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
776 static void nfs_mark_return_delegation(struct nfs_server *server,
777 struct nfs_delegation *delegation)
779 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
780 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
783 static bool nfs_server_mark_return_all_delegations(struct nfs_server *server)
785 struct nfs_delegation *delegation;
788 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
789 nfs_mark_return_delegation(server, delegation);
795 static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
797 struct nfs_server *server;
800 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
801 nfs_server_mark_return_all_delegations(server);
805 static void nfs_delegation_run_state_manager(struct nfs_client *clp)
807 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
808 nfs4_schedule_state_manager(clp);
812 * nfs_expire_all_delegations
813 * @clp: client to process
816 void nfs_expire_all_delegations(struct nfs_client *clp)
818 nfs_client_mark_return_all_delegations(clp);
819 nfs_delegation_run_state_manager(clp);
823 * nfs_super_return_all_delegations - return delegations for one superblock
824 * @server: pointer to nfs_server to process
827 void nfs_server_return_all_delegations(struct nfs_server *server)
829 struct nfs_client *clp = server->nfs_client;
836 need_wait = nfs_server_mark_return_all_delegations(server);
840 nfs4_schedule_state_manager(clp);
841 nfs4_wait_clnt_recover(clp);
845 static void nfs_mark_return_unused_delegation_types(struct nfs_server *server,
848 struct nfs_delegation *delegation;
850 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
851 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
853 if (delegation->type & flags)
854 nfs_mark_return_if_closed_delegation(server, delegation);
858 static void nfs_client_mark_return_unused_delegation_types(struct nfs_client *clp,
861 struct nfs_server *server;
864 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
865 nfs_mark_return_unused_delegation_types(server, flags);
869 static void nfs_revoke_delegation(struct inode *inode,
870 const nfs4_stateid *stateid)
872 struct nfs_delegation *delegation;
877 delegation = rcu_dereference(NFS_I(inode)->delegation);
878 if (delegation == NULL)
880 if (stateid == NULL) {
881 nfs4_stateid_copy(&tmp, &delegation->stateid);
884 if (!nfs4_stateid_match_other(stateid, &delegation->stateid))
886 spin_lock(&delegation->lock);
887 if (stateid->seqid) {
888 if (nfs4_stateid_is_newer(&delegation->stateid, stateid)) {
889 spin_unlock(&delegation->lock);
892 delegation->stateid.seqid = stateid->seqid;
894 spin_unlock(&delegation->lock);
896 nfs_mark_delegation_revoked(delegation);
901 nfs_inode_find_state_and_recover(inode, stateid);
904 void nfs_remove_bad_delegation(struct inode *inode,
905 const nfs4_stateid *stateid)
907 nfs_revoke_delegation(inode, stateid);
909 EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation);
911 void nfs_delegation_mark_returned(struct inode *inode,
912 const nfs4_stateid *stateid)
914 struct nfs_delegation *delegation;
920 delegation = rcu_dereference(NFS_I(inode)->delegation);
924 spin_lock(&delegation->lock);
925 if (!nfs4_stateid_match_other(stateid, &delegation->stateid))
926 goto out_spin_unlock;
927 if (stateid->seqid) {
928 /* If delegation->stateid is newer, dont mark as returned */
929 if (nfs4_stateid_is_newer(&delegation->stateid, stateid))
930 goto out_clear_returning;
931 if (delegation->stateid.seqid != stateid->seqid)
932 delegation->stateid.seqid = stateid->seqid;
935 nfs_mark_delegation_revoked(delegation);
938 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
940 spin_unlock(&delegation->lock);
944 nfs_inode_find_state_and_recover(inode, stateid);
948 * nfs_expire_unused_delegation_types
949 * @clp: client to process
950 * @flags: delegation types to expire
953 void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags)
955 nfs_client_mark_return_unused_delegation_types(clp, flags);
956 nfs_delegation_run_state_manager(clp);
959 static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
961 struct nfs_delegation *delegation;
963 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
964 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
966 nfs_mark_return_if_closed_delegation(server, delegation);
971 * nfs_expire_unreferenced_delegations - Eliminate unused delegations
972 * @clp: nfs_client to process
975 void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
977 struct nfs_server *server;
980 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
981 nfs_mark_return_unreferenced_delegations(server);
984 nfs_delegation_run_state_manager(clp);
988 * nfs_async_inode_return_delegation - asynchronously return a delegation
989 * @inode: inode to process
990 * @stateid: state ID information
992 * Returns zero on success, or a negative errno value.
994 int nfs_async_inode_return_delegation(struct inode *inode,
995 const nfs4_stateid *stateid)
997 struct nfs_server *server = NFS_SERVER(inode);
998 struct nfs_client *clp = server->nfs_client;
999 struct nfs_delegation *delegation;
1002 delegation = nfs4_get_valid_delegation(inode);
1003 if (delegation == NULL)
1005 if (stateid != NULL &&
1006 !clp->cl_mvops->match_stateid(&delegation->stateid, stateid))
1008 nfs_mark_return_delegation(server, delegation);
1011 nfs_delegation_run_state_manager(clp);
1018 static struct inode *
1019 nfs_delegation_find_inode_server(struct nfs_server *server,
1020 const struct nfs_fh *fhandle)
1022 struct nfs_delegation *delegation;
1023 struct inode *freeme, *res = NULL;
1025 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
1026 spin_lock(&delegation->lock);
1027 if (delegation->inode != NULL &&
1028 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) &&
1029 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
1030 freeme = igrab(delegation->inode);
1031 if (freeme && nfs_sb_active(freeme->i_sb))
1033 spin_unlock(&delegation->lock);
1041 return ERR_PTR(-EAGAIN);
1043 spin_unlock(&delegation->lock);
1045 return ERR_PTR(-ENOENT);
1049 * nfs_delegation_find_inode - retrieve the inode associated with a delegation
1050 * @clp: client state handle
1051 * @fhandle: filehandle from a delegation recall
1053 * Returns pointer to inode matching "fhandle," or NULL if a matching inode
1056 struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
1057 const struct nfs_fh *fhandle)
1059 struct nfs_server *server;
1063 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1064 res = nfs_delegation_find_inode_server(server, fhandle);
1065 if (res != ERR_PTR(-ENOENT)) {
1071 return ERR_PTR(-ENOENT);
1074 static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
1076 struct nfs_delegation *delegation;
1078 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
1080 * If the delegation may have been admin revoked, then we
1081 * cannot reclaim it.
1083 if (test_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags))
1085 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
1090 * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
1091 * @clp: nfs_client to process
1094 void nfs_delegation_mark_reclaim(struct nfs_client *clp)
1096 struct nfs_server *server;
1099 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1100 nfs_delegation_mark_reclaim_server(server);
1105 * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
1106 * @clp: nfs_client to process
1109 void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
1111 struct nfs_delegation *delegation;
1112 struct nfs_server *server;
1113 struct inode *inode;
1117 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1118 list_for_each_entry_rcu(delegation, &server->delegations,
1120 if (test_bit(NFS_DELEGATION_INODE_FREEING,
1121 &delegation->flags) ||
1122 test_bit(NFS_DELEGATION_RETURNING,
1123 &delegation->flags) ||
1124 test_bit(NFS_DELEGATION_NEED_RECLAIM,
1125 &delegation->flags) == 0)
1127 if (!nfs_sb_active(server->super))
1128 break; /* continue in outer loop */
1129 inode = nfs_delegation_grab_inode(delegation);
1130 if (inode == NULL) {
1132 nfs_sb_deactive(server->super);
1135 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
1137 if (delegation != NULL) {
1138 if (nfs_detach_delegation(NFS_I(inode), delegation,
1140 nfs_free_delegation(delegation);
1141 /* Match nfs_start_delegation_return_locked */
1142 nfs_put_delegation(delegation);
1145 nfs_sb_deactive(server->super);
1153 static inline bool nfs4_server_rebooted(const struct nfs_client *clp)
1155 return (clp->cl_state & (BIT(NFS4CLNT_CHECK_LEASE) |
1156 BIT(NFS4CLNT_LEASE_EXPIRED) |
1157 BIT(NFS4CLNT_SESSION_RESET))) != 0;
1160 static void nfs_mark_test_expired_delegation(struct nfs_server *server,
1161 struct nfs_delegation *delegation)
1163 if (delegation->stateid.type == NFS4_INVALID_STATEID_TYPE)
1165 clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
1166 set_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
1167 set_bit(NFS4CLNT_DELEGATION_EXPIRED, &server->nfs_client->cl_state);
1170 static void nfs_inode_mark_test_expired_delegation(struct nfs_server *server,
1171 struct inode *inode)
1173 struct nfs_delegation *delegation;
1176 delegation = rcu_dereference(NFS_I(inode)->delegation);
1178 nfs_mark_test_expired_delegation(server, delegation);
1183 static void nfs_delegation_mark_test_expired_server(struct nfs_server *server)
1185 struct nfs_delegation *delegation;
1187 list_for_each_entry_rcu(delegation, &server->delegations, super_list)
1188 nfs_mark_test_expired_delegation(server, delegation);
1192 * nfs_mark_test_expired_all_delegations - mark all delegations for testing
1193 * @clp: nfs_client to process
1195 * Iterates through all the delegations associated with this server and
1196 * marks them as needing to be checked for validity.
1198 void nfs_mark_test_expired_all_delegations(struct nfs_client *clp)
1200 struct nfs_server *server;
1203 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1204 nfs_delegation_mark_test_expired_server(server);
1209 * nfs_test_expired_all_delegations - test all delegations for a client
1210 * @clp: nfs_client to process
1212 * Helper for handling "recallable state revoked" status from server.
1214 void nfs_test_expired_all_delegations(struct nfs_client *clp)
1216 nfs_mark_test_expired_all_delegations(clp);
1217 nfs4_schedule_state_manager(clp);
1221 nfs_delegation_test_free_expired(struct inode *inode,
1222 nfs4_stateid *stateid,
1223 const struct cred *cred)
1225 struct nfs_server *server = NFS_SERVER(inode);
1226 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
1231 status = ops->test_and_free_expired(server, stateid, cred);
1232 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
1233 nfs_remove_bad_delegation(inode, stateid);
1237 * nfs_reap_expired_delegations - reap expired delegations
1238 * @clp: nfs_client to process
1240 * Iterates through all the delegations associated with this server and
1241 * checks if they have may have been revoked. This function is usually
1242 * expected to be called in cases where the server may have lost its
1245 void nfs_reap_expired_delegations(struct nfs_client *clp)
1247 struct nfs_delegation *delegation;
1248 struct nfs_server *server;
1249 struct inode *inode;
1250 const struct cred *cred;
1251 nfs4_stateid stateid;
1255 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1256 list_for_each_entry_rcu(delegation, &server->delegations,
1258 if (test_bit(NFS_DELEGATION_INODE_FREEING,
1259 &delegation->flags) ||
1260 test_bit(NFS_DELEGATION_RETURNING,
1261 &delegation->flags) ||
1262 test_bit(NFS_DELEGATION_TEST_EXPIRED,
1263 &delegation->flags) == 0)
1265 if (!nfs_sb_active(server->super))
1266 break; /* continue in outer loop */
1267 inode = nfs_delegation_grab_inode(delegation);
1268 if (inode == NULL) {
1270 nfs_sb_deactive(server->super);
1273 cred = get_cred_rcu(delegation->cred);
1274 nfs4_stateid_copy(&stateid, &delegation->stateid);
1275 clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
1277 nfs_delegation_test_free_expired(inode, &stateid, cred);
1279 if (nfs4_server_rebooted(clp)) {
1280 nfs_inode_mark_test_expired_delegation(server,inode);
1282 nfs_sb_deactive(server->super);
1286 nfs_sb_deactive(server->super);
1294 void nfs_inode_find_delegation_state_and_recover(struct inode *inode,
1295 const nfs4_stateid *stateid)
1297 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1298 struct nfs_delegation *delegation;
1302 delegation = rcu_dereference(NFS_I(inode)->delegation);
1304 nfs4_stateid_match_or_older(&delegation->stateid, stateid) &&
1305 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
1306 nfs_mark_test_expired_delegation(NFS_SERVER(inode), delegation);
1311 nfs4_schedule_state_manager(clp);
1315 * nfs_delegations_present - check for existence of delegations
1316 * @clp: client state handle
1318 * Returns one if there are any nfs_delegation structures attached
1319 * to this nfs_client.
1321 int nfs_delegations_present(struct nfs_client *clp)
1323 struct nfs_server *server;
1327 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1328 if (!list_empty(&server->delegations)) {
1337 * nfs4_refresh_delegation_stateid - Update delegation stateid seqid
1338 * @dst: stateid to refresh
1339 * @inode: inode to check
1341 * Returns "true" and updates "dst->seqid" * if inode had a delegation
1342 * that matches our delegation stateid. Otherwise "false" is returned.
1344 bool nfs4_refresh_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
1346 struct nfs_delegation *delegation;
1352 delegation = rcu_dereference(NFS_I(inode)->delegation);
1353 if (delegation != NULL &&
1354 nfs4_stateid_match_other(dst, &delegation->stateid) &&
1355 nfs4_stateid_is_newer(&delegation->stateid, dst) &&
1356 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
1357 dst->seqid = delegation->stateid.seqid;
1366 * nfs4_copy_delegation_stateid - Copy inode's state ID information
1367 * @inode: inode to check
1368 * @flags: delegation type requirement
1369 * @dst: stateid data structure to fill in
1370 * @cred: optional argument to retrieve credential
1372 * Returns "true" and fills in "dst->data" * if inode had a delegation,
1373 * otherwise "false" is returned.
1375 bool nfs4_copy_delegation_stateid(struct inode *inode, fmode_t flags,
1376 nfs4_stateid *dst, const struct cred **cred)
1378 struct nfs_inode *nfsi = NFS_I(inode);
1379 struct nfs_delegation *delegation;
1382 flags &= FMODE_READ|FMODE_WRITE;
1384 delegation = rcu_dereference(nfsi->delegation);
1385 ret = nfs4_is_valid_delegation(delegation, flags);
1387 nfs4_stateid_copy(dst, &delegation->stateid);
1388 nfs_mark_delegation_referenced(delegation);
1390 *cred = get_cred(delegation->cred);
1397 * nfs4_delegation_flush_on_close - Check if we must flush file on close
1398 * @inode: inode to check
1400 * This function checks the number of outstanding writes to the file
1401 * against the delegation 'space_limit' field to see if
1402 * the spec requires us to flush the file on close.
1404 bool nfs4_delegation_flush_on_close(const struct inode *inode)
1406 struct nfs_inode *nfsi = NFS_I(inode);
1407 struct nfs_delegation *delegation;
1411 delegation = rcu_dereference(nfsi->delegation);
1412 if (delegation == NULL || !(delegation->type & FMODE_WRITE))
1414 if (atomic_long_read(&nfsi->nrequests) < delegation->pagemod_limit)
1421 module_param_named(delegation_watermark, nfs_delegation_watermark, uint, 0644);