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)
260 if (!test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
261 res = nfs4_proc_delegreturn(inode,
263 &delegation->stateid,
268 static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
270 struct inode *inode = NULL;
272 spin_lock(&delegation->lock);
273 if (delegation->inode != NULL)
274 inode = igrab(delegation->inode);
276 set_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags);
277 spin_unlock(&delegation->lock);
281 static struct nfs_delegation *
282 nfs_start_delegation_return_locked(struct nfs_inode *nfsi)
284 struct nfs_delegation *ret = NULL;
285 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
287 if (delegation == NULL)
289 spin_lock(&delegation->lock);
290 if (!test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) {
291 /* Refcount matched in nfs_end_delegation_return() */
292 ret = nfs_get_delegation(delegation);
294 spin_unlock(&delegation->lock);
296 nfs_clear_verifier_delegated(&nfsi->vfs_inode);
301 static struct nfs_delegation *
302 nfs_start_delegation_return(struct nfs_inode *nfsi)
304 struct nfs_delegation *delegation;
307 delegation = nfs_start_delegation_return_locked(nfsi);
313 nfs_abort_delegation_return(struct nfs_delegation *delegation,
314 struct nfs_client *clp)
317 spin_lock(&delegation->lock);
318 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
319 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
320 spin_unlock(&delegation->lock);
321 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
324 static struct nfs_delegation *
325 nfs_detach_delegation_locked(struct nfs_inode *nfsi,
326 struct nfs_delegation *delegation,
327 struct nfs_client *clp)
329 struct nfs_delegation *deleg_cur =
330 rcu_dereference_protected(nfsi->delegation,
331 lockdep_is_held(&clp->cl_lock));
333 if (deleg_cur == NULL || delegation != deleg_cur)
336 spin_lock(&delegation->lock);
337 if (!delegation->inode) {
338 spin_unlock(&delegation->lock);
341 list_del_rcu(&delegation->super_list);
342 delegation->inode = NULL;
343 rcu_assign_pointer(nfsi->delegation, NULL);
344 spin_unlock(&delegation->lock);
348 static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi,
349 struct nfs_delegation *delegation,
350 struct nfs_server *server)
352 struct nfs_client *clp = server->nfs_client;
354 spin_lock(&clp->cl_lock);
355 delegation = nfs_detach_delegation_locked(nfsi, delegation, clp);
356 spin_unlock(&clp->cl_lock);
360 static struct nfs_delegation *
361 nfs_inode_detach_delegation(struct inode *inode)
363 struct nfs_inode *nfsi = NFS_I(inode);
364 struct nfs_server *server = NFS_SERVER(inode);
365 struct nfs_delegation *delegation;
368 delegation = rcu_dereference(nfsi->delegation);
369 if (delegation != NULL)
370 delegation = nfs_detach_delegation(nfsi, delegation, server);
376 nfs_update_inplace_delegation(struct nfs_delegation *delegation,
377 const struct nfs_delegation *update)
379 if (nfs4_stateid_is_newer(&update->stateid, &delegation->stateid)) {
380 delegation->stateid.seqid = update->stateid.seqid;
382 delegation->type = update->type;
383 if (test_and_clear_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
384 atomic_long_inc(&nfs_active_delegations);
389 * nfs_inode_set_delegation - set up a delegation on an inode
390 * @inode: inode to which delegation applies
391 * @cred: cred to use for subsequent delegation processing
392 * @type: delegation type
393 * @stateid: delegation stateid
394 * @pagemod_limit: write delegation "space_limit"
396 * Returns zero on success, or a negative errno value.
398 int nfs_inode_set_delegation(struct inode *inode, const struct cred *cred,
400 const nfs4_stateid *stateid,
401 unsigned long pagemod_limit)
403 struct nfs_server *server = NFS_SERVER(inode);
404 struct nfs_client *clp = server->nfs_client;
405 struct nfs_inode *nfsi = NFS_I(inode);
406 struct nfs_delegation *delegation, *old_delegation;
407 struct nfs_delegation *freeme = NULL;
410 delegation = kmalloc(sizeof(*delegation), GFP_NOFS);
411 if (delegation == NULL)
413 nfs4_stateid_copy(&delegation->stateid, stateid);
414 refcount_set(&delegation->refcount, 1);
415 delegation->type = type;
416 delegation->pagemod_limit = pagemod_limit;
417 delegation->change_attr = inode_peek_iversion_raw(inode);
418 delegation->cred = get_cred(cred);
419 delegation->inode = inode;
420 delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
421 spin_lock_init(&delegation->lock);
423 spin_lock(&clp->cl_lock);
424 old_delegation = rcu_dereference_protected(nfsi->delegation,
425 lockdep_is_held(&clp->cl_lock));
426 if (old_delegation == NULL)
428 /* Is this an update of the existing delegation? */
429 if (nfs4_stateid_match_other(&old_delegation->stateid,
430 &delegation->stateid)) {
431 spin_lock(&old_delegation->lock);
432 nfs_update_inplace_delegation(old_delegation,
434 spin_unlock(&old_delegation->lock);
437 if (!test_bit(NFS_DELEGATION_REVOKED, &old_delegation->flags)) {
439 * Deal with broken servers that hand out two
440 * delegations for the same file.
441 * Allow for upgrades to a WRITE delegation, but
444 dfprintk(FILE, "%s: server %s handed out "
445 "a duplicate delegation!\n",
446 __func__, clp->cl_hostname);
447 if (delegation->type == old_delegation->type ||
448 !(delegation->type & FMODE_WRITE)) {
453 if (test_and_set_bit(NFS_DELEGATION_RETURNING,
454 &old_delegation->flags))
457 freeme = nfs_detach_delegation_locked(nfsi, old_delegation, clp);
461 list_add_tail_rcu(&delegation->super_list, &server->delegations);
462 rcu_assign_pointer(nfsi->delegation, delegation);
465 atomic_long_inc(&nfs_active_delegations);
467 trace_nfs4_set_delegation(inode, type);
469 spin_lock(&inode->i_lock);
470 if (NFS_I(inode)->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ATIME))
471 NFS_I(inode)->cache_validity |= NFS_INO_REVAL_FORCED;
472 spin_unlock(&inode->i_lock);
474 spin_unlock(&clp->cl_lock);
475 if (delegation != NULL)
476 __nfs_free_delegation(delegation);
477 if (freeme != NULL) {
478 nfs_do_return_delegation(inode, freeme, 0);
479 nfs_free_delegation(freeme);
485 * Basic procedure for returning a delegation to the server
487 static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync)
489 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
492 if (delegation == NULL)
495 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
497 err = nfs_delegation_claim_opens(inode, &delegation->stateid,
499 if (!issync || err != -EAGAIN)
502 * Guard against state recovery
504 err = nfs4_wait_clnt_recover(clp);
508 nfs_abort_delegation_return(delegation, clp);
512 err = nfs_do_return_delegation(inode, delegation, issync);
514 /* Refcount matched in nfs_start_delegation_return_locked() */
515 nfs_put_delegation(delegation);
519 static bool nfs_delegation_need_return(struct nfs_delegation *delegation)
523 if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
525 else if (test_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags)) {
528 spin_lock(&delegation->lock);
529 inode = delegation->inode;
530 if (inode && list_empty(&NFS_I(inode)->open_files))
532 spin_unlock(&delegation->lock);
535 clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
536 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags) ||
537 test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
544 * nfs_client_return_marked_delegations - return previously marked delegations
545 * @clp: nfs_client to process
547 * Note that this function is designed to be called by the state
548 * manager thread. For this reason, it cannot flush the dirty data,
549 * since that could deadlock in case of a state recovery error.
551 * Returns zero on success, or a negative errno value.
553 int nfs_client_return_marked_delegations(struct nfs_client *clp)
555 struct nfs_delegation *delegation;
556 struct nfs_delegation *prev;
557 struct nfs_server *server;
559 struct inode *place_holder = NULL;
560 struct nfs_delegation *place_holder_deleg = NULL;
565 * To avoid quadratic looping we hold a reference
566 * to an inode place_holder. Each time we restart, we
567 * list nfs_servers from the server of that inode, and
568 * delegation in the server from the delegations of that
570 * prev is an RCU-protected pointer to a delegation which
571 * wasn't marked for return and might be a good choice for
572 * the next place_holder.
577 server = NFS_SERVER(place_holder);
579 server = list_entry_rcu(clp->cl_superblocks.next,
580 struct nfs_server, client_link);
581 list_for_each_entry_from_rcu(server, &clp->cl_superblocks, client_link) {
583 if (place_holder && server == NFS_SERVER(place_holder))
584 delegation = rcu_dereference(NFS_I(place_holder)->delegation);
585 if (!delegation || delegation != place_holder_deleg)
586 delegation = list_entry_rcu(server->delegations.next,
587 struct nfs_delegation, super_list);
588 list_for_each_entry_from_rcu(delegation, &server->delegations, super_list) {
589 struct inode *to_put = NULL;
591 if (!nfs_delegation_need_return(delegation)) {
595 if (!nfs_sb_active(server->super))
596 break; /* continue in outer loop */
601 tmp = nfs_delegation_grab_inode(prev);
603 to_put = place_holder;
605 place_holder_deleg = prev;
609 inode = nfs_delegation_grab_inode(delegation);
614 nfs_sb_deactive(server->super);
617 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
623 err = nfs_end_delegation_return(inode, delegation, 0);
625 nfs_sb_deactive(server->super);
629 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
642 * nfs_inode_evict_delegation - return delegation, don't reclaim opens
643 * @inode: inode to process
645 * Does not protect against delegation reclaims, therefore really only safe
646 * to be called from nfs4_clear_inode(). Guaranteed to always free
647 * the delegation structure.
649 void nfs_inode_evict_delegation(struct inode *inode)
651 struct nfs_delegation *delegation;
653 delegation = nfs_inode_detach_delegation(inode);
654 if (delegation != NULL) {
655 set_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
656 set_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags);
657 nfs_do_return_delegation(inode, delegation, 1);
658 nfs_free_delegation(delegation);
663 * nfs_inode_return_delegation - synchronously return a delegation
664 * @inode: inode to process
666 * This routine will always flush any dirty data to disk on the
667 * assumption that if we need to return the delegation, then
668 * we should stop caching.
670 * Returns zero on success, or a negative errno value.
672 int nfs4_inode_return_delegation(struct inode *inode)
674 struct nfs_inode *nfsi = NFS_I(inode);
675 struct nfs_delegation *delegation;
679 delegation = nfs_start_delegation_return(nfsi);
680 if (delegation != NULL)
681 err = nfs_end_delegation_return(inode, delegation, 1);
686 * nfs_inode_return_delegation_on_close - asynchronously return a delegation
687 * @inode: inode to process
689 * This routine is called on file close in order to determine if the
690 * inode delegation needs to be returned immediately.
692 void nfs4_inode_return_delegation_on_close(struct inode *inode)
694 struct nfs_delegation *delegation;
695 struct nfs_delegation *ret = NULL;
700 delegation = nfs4_get_valid_delegation(inode);
703 if (test_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags) ||
704 atomic_long_read(&nfs_active_delegations) >= nfs_delegation_watermark) {
705 spin_lock(&delegation->lock);
706 if (delegation->inode &&
707 list_empty(&NFS_I(inode)->open_files) &&
708 !test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) {
709 clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
710 /* Refcount matched in nfs_end_delegation_return() */
711 ret = nfs_get_delegation(delegation);
713 spin_unlock(&delegation->lock);
715 nfs_clear_verifier_delegated(inode);
719 nfs_end_delegation_return(inode, ret, 0);
723 * nfs4_inode_make_writeable
724 * @inode: pointer to inode
726 * Make the inode writeable by returning the delegation if necessary
728 * Returns zero on success, or a negative errno value.
730 int nfs4_inode_make_writeable(struct inode *inode)
732 struct nfs_delegation *delegation;
735 delegation = nfs4_get_valid_delegation(inode);
736 if (delegation == NULL ||
737 (nfs4_has_session(NFS_SERVER(inode)->nfs_client) &&
738 (delegation->type & FMODE_WRITE))) {
743 return nfs4_inode_return_delegation(inode);
746 static void nfs_mark_return_if_closed_delegation(struct nfs_server *server,
747 struct nfs_delegation *delegation)
749 set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
750 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
753 static void nfs_mark_return_delegation(struct nfs_server *server,
754 struct nfs_delegation *delegation)
756 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
757 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
760 static bool nfs_server_mark_return_all_delegations(struct nfs_server *server)
762 struct nfs_delegation *delegation;
765 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
766 nfs_mark_return_delegation(server, delegation);
772 static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
774 struct nfs_server *server;
777 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
778 nfs_server_mark_return_all_delegations(server);
782 static void nfs_delegation_run_state_manager(struct nfs_client *clp)
784 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
785 nfs4_schedule_state_manager(clp);
789 * nfs_expire_all_delegations
790 * @clp: client to process
793 void nfs_expire_all_delegations(struct nfs_client *clp)
795 nfs_client_mark_return_all_delegations(clp);
796 nfs_delegation_run_state_manager(clp);
800 * nfs_super_return_all_delegations - return delegations for one superblock
801 * @server: pointer to nfs_server to process
804 void nfs_server_return_all_delegations(struct nfs_server *server)
806 struct nfs_client *clp = server->nfs_client;
813 need_wait = nfs_server_mark_return_all_delegations(server);
817 nfs4_schedule_state_manager(clp);
818 nfs4_wait_clnt_recover(clp);
822 static void nfs_mark_return_unused_delegation_types(struct nfs_server *server,
825 struct nfs_delegation *delegation;
827 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
828 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
830 if (delegation->type & flags)
831 nfs_mark_return_if_closed_delegation(server, delegation);
835 static void nfs_client_mark_return_unused_delegation_types(struct nfs_client *clp,
838 struct nfs_server *server;
841 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
842 nfs_mark_return_unused_delegation_types(server, flags);
846 static void nfs_revoke_delegation(struct inode *inode,
847 const nfs4_stateid *stateid)
849 struct nfs_delegation *delegation;
854 delegation = rcu_dereference(NFS_I(inode)->delegation);
855 if (delegation == NULL)
857 if (stateid == NULL) {
858 nfs4_stateid_copy(&tmp, &delegation->stateid);
861 if (!nfs4_stateid_match_other(stateid, &delegation->stateid))
863 spin_lock(&delegation->lock);
864 if (stateid->seqid) {
865 if (nfs4_stateid_is_newer(&delegation->stateid, stateid)) {
866 spin_unlock(&delegation->lock);
869 delegation->stateid.seqid = stateid->seqid;
871 spin_unlock(&delegation->lock);
873 nfs_mark_delegation_revoked(delegation);
878 nfs_inode_find_state_and_recover(inode, stateid);
881 void nfs_remove_bad_delegation(struct inode *inode,
882 const nfs4_stateid *stateid)
884 nfs_revoke_delegation(inode, stateid);
886 EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation);
888 void nfs_delegation_mark_returned(struct inode *inode,
889 const nfs4_stateid *stateid)
891 struct nfs_delegation *delegation;
897 delegation = rcu_dereference(NFS_I(inode)->delegation);
901 spin_lock(&delegation->lock);
902 if (!nfs4_stateid_match_other(stateid, &delegation->stateid))
903 goto out_spin_unlock;
904 if (stateid->seqid) {
905 /* If delegation->stateid is newer, dont mark as returned */
906 if (nfs4_stateid_is_newer(&delegation->stateid, stateid))
907 goto out_clear_returning;
908 if (delegation->stateid.seqid != stateid->seqid)
909 delegation->stateid.seqid = stateid->seqid;
912 nfs_mark_delegation_revoked(delegation);
915 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
917 spin_unlock(&delegation->lock);
921 nfs_inode_find_state_and_recover(inode, stateid);
925 * nfs_expire_unused_delegation_types
926 * @clp: client to process
927 * @flags: delegation types to expire
930 void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags)
932 nfs_client_mark_return_unused_delegation_types(clp, flags);
933 nfs_delegation_run_state_manager(clp);
936 static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
938 struct nfs_delegation *delegation;
940 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
941 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
943 nfs_mark_return_if_closed_delegation(server, delegation);
948 * nfs_expire_unreferenced_delegations - Eliminate unused delegations
949 * @clp: nfs_client to process
952 void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
954 struct nfs_server *server;
957 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
958 nfs_mark_return_unreferenced_delegations(server);
961 nfs_delegation_run_state_manager(clp);
965 * nfs_async_inode_return_delegation - asynchronously return a delegation
966 * @inode: inode to process
967 * @stateid: state ID information
969 * Returns zero on success, or a negative errno value.
971 int nfs_async_inode_return_delegation(struct inode *inode,
972 const nfs4_stateid *stateid)
974 struct nfs_server *server = NFS_SERVER(inode);
975 struct nfs_client *clp = server->nfs_client;
976 struct nfs_delegation *delegation;
979 delegation = nfs4_get_valid_delegation(inode);
980 if (delegation == NULL)
982 if (stateid != NULL &&
983 !clp->cl_mvops->match_stateid(&delegation->stateid, stateid))
985 nfs_mark_return_delegation(server, delegation);
988 nfs_delegation_run_state_manager(clp);
995 static struct inode *
996 nfs_delegation_find_inode_server(struct nfs_server *server,
997 const struct nfs_fh *fhandle)
999 struct nfs_delegation *delegation;
1000 struct inode *freeme, *res = NULL;
1002 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
1003 spin_lock(&delegation->lock);
1004 if (delegation->inode != NULL &&
1005 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) &&
1006 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
1007 freeme = igrab(delegation->inode);
1008 if (freeme && nfs_sb_active(freeme->i_sb))
1010 spin_unlock(&delegation->lock);
1018 return ERR_PTR(-EAGAIN);
1020 spin_unlock(&delegation->lock);
1022 return ERR_PTR(-ENOENT);
1026 * nfs_delegation_find_inode - retrieve the inode associated with a delegation
1027 * @clp: client state handle
1028 * @fhandle: filehandle from a delegation recall
1030 * Returns pointer to inode matching "fhandle," or NULL if a matching inode
1033 struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
1034 const struct nfs_fh *fhandle)
1036 struct nfs_server *server;
1040 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1041 res = nfs_delegation_find_inode_server(server, fhandle);
1042 if (res != ERR_PTR(-ENOENT)) {
1048 return ERR_PTR(-ENOENT);
1051 static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
1053 struct nfs_delegation *delegation;
1055 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
1057 * If the delegation may have been admin revoked, then we
1058 * cannot reclaim it.
1060 if (test_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags))
1062 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
1067 * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
1068 * @clp: nfs_client to process
1071 void nfs_delegation_mark_reclaim(struct nfs_client *clp)
1073 struct nfs_server *server;
1076 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1077 nfs_delegation_mark_reclaim_server(server);
1082 * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
1083 * @clp: nfs_client to process
1086 void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
1088 struct nfs_delegation *delegation;
1089 struct nfs_server *server;
1090 struct inode *inode;
1094 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1095 list_for_each_entry_rcu(delegation, &server->delegations,
1097 if (test_bit(NFS_DELEGATION_INODE_FREEING,
1098 &delegation->flags) ||
1099 test_bit(NFS_DELEGATION_RETURNING,
1100 &delegation->flags) ||
1101 test_bit(NFS_DELEGATION_NEED_RECLAIM,
1102 &delegation->flags) == 0)
1104 if (!nfs_sb_active(server->super))
1105 break; /* continue in outer loop */
1106 inode = nfs_delegation_grab_inode(delegation);
1107 if (inode == NULL) {
1109 nfs_sb_deactive(server->super);
1112 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
1114 if (delegation != NULL) {
1115 if (nfs_detach_delegation(NFS_I(inode), delegation,
1117 nfs_free_delegation(delegation);
1118 /* Match nfs_start_delegation_return_locked */
1119 nfs_put_delegation(delegation);
1122 nfs_sb_deactive(server->super);
1130 static inline bool nfs4_server_rebooted(const struct nfs_client *clp)
1132 return (clp->cl_state & (BIT(NFS4CLNT_CHECK_LEASE) |
1133 BIT(NFS4CLNT_LEASE_EXPIRED) |
1134 BIT(NFS4CLNT_SESSION_RESET))) != 0;
1137 static void nfs_mark_test_expired_delegation(struct nfs_server *server,
1138 struct nfs_delegation *delegation)
1140 if (delegation->stateid.type == NFS4_INVALID_STATEID_TYPE)
1142 clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
1143 set_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
1144 set_bit(NFS4CLNT_DELEGATION_EXPIRED, &server->nfs_client->cl_state);
1147 static void nfs_inode_mark_test_expired_delegation(struct nfs_server *server,
1148 struct inode *inode)
1150 struct nfs_delegation *delegation;
1153 delegation = rcu_dereference(NFS_I(inode)->delegation);
1155 nfs_mark_test_expired_delegation(server, delegation);
1160 static void nfs_delegation_mark_test_expired_server(struct nfs_server *server)
1162 struct nfs_delegation *delegation;
1164 list_for_each_entry_rcu(delegation, &server->delegations, super_list)
1165 nfs_mark_test_expired_delegation(server, delegation);
1169 * nfs_mark_test_expired_all_delegations - mark all delegations for testing
1170 * @clp: nfs_client to process
1172 * Iterates through all the delegations associated with this server and
1173 * marks them as needing to be checked for validity.
1175 void nfs_mark_test_expired_all_delegations(struct nfs_client *clp)
1177 struct nfs_server *server;
1180 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1181 nfs_delegation_mark_test_expired_server(server);
1186 * nfs_test_expired_all_delegations - test all delegations for a client
1187 * @clp: nfs_client to process
1189 * Helper for handling "recallable state revoked" status from server.
1191 void nfs_test_expired_all_delegations(struct nfs_client *clp)
1193 nfs_mark_test_expired_all_delegations(clp);
1194 nfs4_schedule_state_manager(clp);
1198 nfs_delegation_test_free_expired(struct inode *inode,
1199 nfs4_stateid *stateid,
1200 const struct cred *cred)
1202 struct nfs_server *server = NFS_SERVER(inode);
1203 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
1208 status = ops->test_and_free_expired(server, stateid, cred);
1209 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
1210 nfs_remove_bad_delegation(inode, stateid);
1214 * nfs_reap_expired_delegations - reap expired delegations
1215 * @clp: nfs_client to process
1217 * Iterates through all the delegations associated with this server and
1218 * checks if they have may have been revoked. This function is usually
1219 * expected to be called in cases where the server may have lost its
1222 void nfs_reap_expired_delegations(struct nfs_client *clp)
1224 struct nfs_delegation *delegation;
1225 struct nfs_server *server;
1226 struct inode *inode;
1227 const struct cred *cred;
1228 nfs4_stateid stateid;
1232 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1233 list_for_each_entry_rcu(delegation, &server->delegations,
1235 if (test_bit(NFS_DELEGATION_INODE_FREEING,
1236 &delegation->flags) ||
1237 test_bit(NFS_DELEGATION_RETURNING,
1238 &delegation->flags) ||
1239 test_bit(NFS_DELEGATION_TEST_EXPIRED,
1240 &delegation->flags) == 0)
1242 if (!nfs_sb_active(server->super))
1243 break; /* continue in outer loop */
1244 inode = nfs_delegation_grab_inode(delegation);
1245 if (inode == NULL) {
1247 nfs_sb_deactive(server->super);
1250 cred = get_cred_rcu(delegation->cred);
1251 nfs4_stateid_copy(&stateid, &delegation->stateid);
1252 clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
1254 nfs_delegation_test_free_expired(inode, &stateid, cred);
1256 if (nfs4_server_rebooted(clp)) {
1257 nfs_inode_mark_test_expired_delegation(server,inode);
1259 nfs_sb_deactive(server->super);
1263 nfs_sb_deactive(server->super);
1271 void nfs_inode_find_delegation_state_and_recover(struct inode *inode,
1272 const nfs4_stateid *stateid)
1274 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1275 struct nfs_delegation *delegation;
1279 delegation = rcu_dereference(NFS_I(inode)->delegation);
1281 nfs4_stateid_match_or_older(&delegation->stateid, stateid) &&
1282 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
1283 nfs_mark_test_expired_delegation(NFS_SERVER(inode), delegation);
1288 nfs4_schedule_state_manager(clp);
1292 * nfs_delegations_present - check for existence of delegations
1293 * @clp: client state handle
1295 * Returns one if there are any nfs_delegation structures attached
1296 * to this nfs_client.
1298 int nfs_delegations_present(struct nfs_client *clp)
1300 struct nfs_server *server;
1304 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1305 if (!list_empty(&server->delegations)) {
1314 * nfs4_refresh_delegation_stateid - Update delegation stateid seqid
1315 * @dst: stateid to refresh
1316 * @inode: inode to check
1318 * Returns "true" and updates "dst->seqid" * if inode had a delegation
1319 * that matches our delegation stateid. Otherwise "false" is returned.
1321 bool nfs4_refresh_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
1323 struct nfs_delegation *delegation;
1329 delegation = rcu_dereference(NFS_I(inode)->delegation);
1330 if (delegation != NULL &&
1331 nfs4_stateid_match_other(dst, &delegation->stateid) &&
1332 nfs4_stateid_is_newer(&delegation->stateid, dst) &&
1333 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
1334 dst->seqid = delegation->stateid.seqid;
1343 * nfs4_copy_delegation_stateid - Copy inode's state ID information
1344 * @inode: inode to check
1345 * @flags: delegation type requirement
1346 * @dst: stateid data structure to fill in
1347 * @cred: optional argument to retrieve credential
1349 * Returns "true" and fills in "dst->data" * if inode had a delegation,
1350 * otherwise "false" is returned.
1352 bool nfs4_copy_delegation_stateid(struct inode *inode, fmode_t flags,
1353 nfs4_stateid *dst, const struct cred **cred)
1355 struct nfs_inode *nfsi = NFS_I(inode);
1356 struct nfs_delegation *delegation;
1359 flags &= FMODE_READ|FMODE_WRITE;
1361 delegation = rcu_dereference(nfsi->delegation);
1362 ret = nfs4_is_valid_delegation(delegation, flags);
1364 nfs4_stateid_copy(dst, &delegation->stateid);
1365 nfs_mark_delegation_referenced(delegation);
1367 *cred = get_cred(delegation->cred);
1374 * nfs4_delegation_flush_on_close - Check if we must flush file on close
1375 * @inode: inode to check
1377 * This function checks the number of outstanding writes to the file
1378 * against the delegation 'space_limit' field to see if
1379 * the spec requires us to flush the file on close.
1381 bool nfs4_delegation_flush_on_close(const struct inode *inode)
1383 struct nfs_inode *nfsi = NFS_I(inode);
1384 struct nfs_delegation *delegation;
1388 delegation = rcu_dereference(nfsi->delegation);
1389 if (delegation == NULL || !(delegation->type & FMODE_WRITE))
1391 if (atomic_long_read(&nfsi->nrequests) < delegation->pagemod_limit)
1398 module_param_named(delegation_watermark, nfs_delegation_watermark, uint, 0644);