2 * linux/fs/nfs/delegation.c
4 * Copyright (C) 2004 Trond Myklebust
6 * NFS file delegation management
9 #include <linux/completion.h>
10 #include <linux/kthread.h>
11 #include <linux/module.h>
12 #include <linux/sched.h>
13 #include <linux/slab.h>
14 #include <linux/spinlock.h>
15 #include <linux/iversion.h>
17 #include <linux/nfs4.h>
18 #include <linux/nfs_fs.h>
19 #include <linux/nfs_xdr.h>
22 #include "nfs4session.h"
23 #include "delegation.h"
25 #include "nfs4trace.h"
27 static void nfs_free_delegation(struct nfs_delegation *delegation)
29 put_cred(delegation->cred);
30 delegation->cred = NULL;
31 kfree_rcu(delegation, rcu);
35 * nfs_mark_delegation_referenced - set delegation's REFERENCED flag
36 * @delegation: delegation to process
39 void nfs_mark_delegation_referenced(struct nfs_delegation *delegation)
41 set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags);
45 nfs4_is_valid_delegation(const struct nfs_delegation *delegation,
48 if (delegation != NULL && (delegation->type & flags) == flags &&
49 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) &&
50 !test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
56 nfs4_do_check_delegation(struct inode *inode, fmode_t flags, bool mark)
58 struct nfs_delegation *delegation;
61 flags &= FMODE_READ|FMODE_WRITE;
63 delegation = rcu_dereference(NFS_I(inode)->delegation);
64 if (nfs4_is_valid_delegation(delegation, flags)) {
66 nfs_mark_delegation_referenced(delegation);
73 * nfs_have_delegation - check if inode has a delegation, mark it
74 * NFS_DELEGATION_REFERENCED if there is one.
75 * @inode: inode to check
76 * @flags: delegation types to check for
78 * Returns one if inode has the indicated delegation, otherwise zero.
80 int nfs4_have_delegation(struct inode *inode, fmode_t flags)
82 return nfs4_do_check_delegation(inode, flags, true);
86 * nfs4_check_delegation - check if inode has a delegation, do not mark
87 * NFS_DELEGATION_REFERENCED if it has one.
89 int nfs4_check_delegation(struct inode *inode, fmode_t flags)
91 return nfs4_do_check_delegation(inode, flags, false);
94 static int nfs_delegation_claim_locks(struct nfs4_state *state, const nfs4_stateid *stateid)
96 struct inode *inode = state->inode;
98 struct file_lock_context *flctx = inode->i_flctx;
99 struct list_head *list;
105 list = &flctx->flc_posix;
106 spin_lock(&flctx->flc_lock);
108 list_for_each_entry(fl, list, fl_list) {
109 if (nfs_file_open_context(fl->fl_file)->state != state)
111 spin_unlock(&flctx->flc_lock);
112 status = nfs4_lock_delegation_recall(fl, state, stateid);
115 spin_lock(&flctx->flc_lock);
117 if (list == &flctx->flc_posix) {
118 list = &flctx->flc_flock;
121 spin_unlock(&flctx->flc_lock);
126 static int nfs_delegation_claim_opens(struct inode *inode,
127 const nfs4_stateid *stateid, fmode_t type)
129 struct nfs_inode *nfsi = NFS_I(inode);
130 struct nfs_open_context *ctx;
131 struct nfs4_state_owner *sp;
132 struct nfs4_state *state;
138 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
142 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
144 if (!nfs4_valid_open_stateid(state))
146 if (!nfs4_stateid_match(&state->stateid, stateid))
148 if (!get_nfs_open_context(ctx))
152 /* Block nfs4_proc_unlck */
153 mutex_lock(&sp->so_delegreturn_mutex);
154 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
155 err = nfs4_open_delegation_recall(ctx, state, stateid, type);
157 err = nfs_delegation_claim_locks(state, stateid);
158 if (!err && read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
160 mutex_unlock(&sp->so_delegreturn_mutex);
161 put_nfs_open_context(ctx);
171 * nfs_inode_reclaim_delegation - process a delegation reclaim request
172 * @inode: inode to process
173 * @cred: credential to use for request
174 * @type: delegation type
175 * @stateid: delegation stateid
176 * @pagemod_limit: write delegation "space_limit"
179 void nfs_inode_reclaim_delegation(struct inode *inode, const struct cred *cred,
181 const nfs4_stateid *stateid,
182 unsigned long pagemod_limit)
184 struct nfs_delegation *delegation;
185 const struct cred *oldcred = NULL;
188 delegation = rcu_dereference(NFS_I(inode)->delegation);
189 if (delegation != NULL) {
190 spin_lock(&delegation->lock);
191 if (delegation->inode != NULL) {
192 nfs4_stateid_copy(&delegation->stateid, stateid);
193 delegation->type = type;
194 delegation->pagemod_limit = pagemod_limit;
195 oldcred = delegation->cred;
196 delegation->cred = get_cred(cred);
197 clear_bit(NFS_DELEGATION_NEED_RECLAIM,
199 spin_unlock(&delegation->lock);
202 trace_nfs4_reclaim_delegation(inode, type);
205 /* We appear to have raced with a delegation return. */
206 spin_unlock(&delegation->lock);
209 nfs_inode_set_delegation(inode, cred, type, stateid, pagemod_limit);
212 static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
216 if (!test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
217 res = nfs4_proc_delegreturn(inode,
219 &delegation->stateid,
221 nfs_free_delegation(delegation);
225 static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
227 struct inode *inode = NULL;
229 spin_lock(&delegation->lock);
230 if (delegation->inode != NULL)
231 inode = igrab(delegation->inode);
232 spin_unlock(&delegation->lock);
236 static struct nfs_delegation *
237 nfs_start_delegation_return_locked(struct nfs_inode *nfsi)
239 struct nfs_delegation *ret = NULL;
240 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
242 if (delegation == NULL)
244 spin_lock(&delegation->lock);
245 if (!test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
247 spin_unlock(&delegation->lock);
252 static struct nfs_delegation *
253 nfs_start_delegation_return(struct nfs_inode *nfsi)
255 struct nfs_delegation *delegation;
258 delegation = nfs_start_delegation_return_locked(nfsi);
264 nfs_abort_delegation_return(struct nfs_delegation *delegation,
265 struct nfs_client *clp)
268 spin_lock(&delegation->lock);
269 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
270 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
271 spin_unlock(&delegation->lock);
272 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
275 static struct nfs_delegation *
276 nfs_detach_delegation_locked(struct nfs_inode *nfsi,
277 struct nfs_delegation *delegation,
278 struct nfs_client *clp)
280 struct nfs_delegation *deleg_cur =
281 rcu_dereference_protected(nfsi->delegation,
282 lockdep_is_held(&clp->cl_lock));
284 if (deleg_cur == NULL || delegation != deleg_cur)
287 spin_lock(&delegation->lock);
288 set_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
289 list_del_rcu(&delegation->super_list);
290 delegation->inode = NULL;
291 rcu_assign_pointer(nfsi->delegation, NULL);
292 spin_unlock(&delegation->lock);
296 static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi,
297 struct nfs_delegation *delegation,
298 struct nfs_server *server)
300 struct nfs_client *clp = server->nfs_client;
302 spin_lock(&clp->cl_lock);
303 delegation = nfs_detach_delegation_locked(nfsi, delegation, clp);
304 spin_unlock(&clp->cl_lock);
308 static struct nfs_delegation *
309 nfs_inode_detach_delegation(struct inode *inode)
311 struct nfs_inode *nfsi = NFS_I(inode);
312 struct nfs_server *server = NFS_SERVER(inode);
313 struct nfs_delegation *delegation;
315 delegation = nfs_start_delegation_return(nfsi);
316 if (delegation == NULL)
318 return nfs_detach_delegation(nfsi, delegation, server);
322 nfs_update_inplace_delegation(struct nfs_delegation *delegation,
323 const struct nfs_delegation *update)
325 if (nfs4_stateid_is_newer(&update->stateid, &delegation->stateid)) {
326 delegation->stateid.seqid = update->stateid.seqid;
328 delegation->type = update->type;
333 * nfs_inode_set_delegation - set up a delegation on an inode
334 * @inode: inode to which delegation applies
335 * @cred: cred to use for subsequent delegation processing
336 * @type: delegation type
337 * @stateid: delegation stateid
338 * @pagemod_limit: write delegation "space_limit"
340 * Returns zero on success, or a negative errno value.
342 int nfs_inode_set_delegation(struct inode *inode, const struct cred *cred,
344 const nfs4_stateid *stateid,
345 unsigned long pagemod_limit)
347 struct nfs_server *server = NFS_SERVER(inode);
348 struct nfs_client *clp = server->nfs_client;
349 struct nfs_inode *nfsi = NFS_I(inode);
350 struct nfs_delegation *delegation, *old_delegation;
351 struct nfs_delegation *freeme = NULL;
354 delegation = kmalloc(sizeof(*delegation), GFP_NOFS);
355 if (delegation == NULL)
357 nfs4_stateid_copy(&delegation->stateid, stateid);
358 delegation->type = type;
359 delegation->pagemod_limit = pagemod_limit;
360 delegation->change_attr = inode_peek_iversion_raw(inode);
361 delegation->cred = get_cred(cred);
362 delegation->inode = inode;
363 delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
364 spin_lock_init(&delegation->lock);
366 spin_lock(&clp->cl_lock);
367 old_delegation = rcu_dereference_protected(nfsi->delegation,
368 lockdep_is_held(&clp->cl_lock));
369 if (old_delegation != NULL) {
370 /* Is this an update of the existing delegation? */
371 if (nfs4_stateid_match_other(&old_delegation->stateid,
372 &delegation->stateid)) {
373 nfs_update_inplace_delegation(old_delegation,
378 * Deal with broken servers that hand out two
379 * delegations for the same file.
380 * Allow for upgrades to a WRITE delegation, but
383 dfprintk(FILE, "%s: server %s handed out "
384 "a duplicate delegation!\n",
385 __func__, clp->cl_hostname);
386 if (delegation->type == old_delegation->type ||
387 !(delegation->type & FMODE_WRITE)) {
392 if (test_and_set_bit(NFS_DELEGATION_RETURNING,
393 &old_delegation->flags))
395 freeme = nfs_detach_delegation_locked(nfsi,
396 old_delegation, clp);
400 list_add_tail_rcu(&delegation->super_list, &server->delegations);
401 rcu_assign_pointer(nfsi->delegation, delegation);
404 trace_nfs4_set_delegation(inode, type);
406 spin_lock(&inode->i_lock);
407 if (NFS_I(inode)->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ATIME))
408 NFS_I(inode)->cache_validity |= NFS_INO_REVAL_FORCED;
409 spin_unlock(&inode->i_lock);
411 spin_unlock(&clp->cl_lock);
412 if (delegation != NULL)
413 nfs_free_delegation(delegation);
415 nfs_do_return_delegation(inode, freeme, 0);
420 * Basic procedure for returning a delegation to the server
422 static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync)
424 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
425 struct nfs_inode *nfsi = NFS_I(inode);
428 if (delegation == NULL)
431 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
433 err = nfs_delegation_claim_opens(inode, &delegation->stateid,
435 if (!issync || err != -EAGAIN)
438 * Guard against state recovery
440 err = nfs4_wait_clnt_recover(clp);
444 nfs_abort_delegation_return(delegation, clp);
447 if (!nfs_detach_delegation(nfsi, delegation, NFS_SERVER(inode)))
450 err = nfs_do_return_delegation(inode, delegation, issync);
455 static bool nfs_delegation_need_return(struct nfs_delegation *delegation)
459 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
461 if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
463 if (test_and_clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags) && !ret) {
466 spin_lock(&delegation->lock);
467 inode = delegation->inode;
468 if (inode && list_empty(&NFS_I(inode)->open_files))
470 spin_unlock(&delegation->lock);
477 * nfs_client_return_marked_delegations - return previously marked delegations
478 * @clp: nfs_client to process
480 * Note that this function is designed to be called by the state
481 * manager thread. For this reason, it cannot flush the dirty data,
482 * since that could deadlock in case of a state recovery error.
484 * Returns zero on success, or a negative errno value.
486 int nfs_client_return_marked_delegations(struct nfs_client *clp)
488 struct nfs_delegation *delegation;
489 struct nfs_delegation *prev;
490 struct nfs_server *server;
492 struct inode *place_holder = NULL;
493 struct nfs_delegation *place_holder_deleg = NULL;
498 * To avoid quadratic looping we hold a reference
499 * to an inode place_holder. Each time we restart, we
500 * list nfs_servers from the server of that inode, and
501 * delegation in the server from the delegations of that
503 * prev is an RCU-protected pointer to a delegation which
504 * wasn't marked for return and might be a good choice for
505 * the next place_holder.
510 server = NFS_SERVER(place_holder);
512 server = list_entry_rcu(clp->cl_superblocks.next,
513 struct nfs_server, client_link);
514 list_for_each_entry_from_rcu(server, &clp->cl_superblocks, client_link) {
516 if (place_holder && server == NFS_SERVER(place_holder))
517 delegation = rcu_dereference(NFS_I(place_holder)->delegation);
518 if (!delegation || delegation != place_holder_deleg)
519 delegation = list_entry_rcu(server->delegations.next,
520 struct nfs_delegation, super_list);
521 list_for_each_entry_from_rcu(delegation, &server->delegations, super_list) {
522 struct inode *to_put = NULL;
524 if (!nfs_delegation_need_return(delegation)) {
528 if (!nfs_sb_active(server->super))
529 break; /* continue in outer loop */
534 tmp = nfs_delegation_grab_inode(prev);
536 to_put = place_holder;
538 place_holder_deleg = prev;
542 inode = nfs_delegation_grab_inode(delegation);
547 nfs_sb_deactive(server->super);
550 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
556 err = nfs_end_delegation_return(inode, delegation, 0);
558 nfs_sb_deactive(server->super);
562 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
575 * nfs_inode_return_delegation_noreclaim - return delegation, don't reclaim opens
576 * @inode: inode to process
578 * Does not protect against delegation reclaims, therefore really only safe
579 * to be called from nfs4_clear_inode().
581 void nfs_inode_return_delegation_noreclaim(struct inode *inode)
583 struct nfs_delegation *delegation;
585 delegation = nfs_inode_detach_delegation(inode);
586 if (delegation != NULL)
587 nfs_do_return_delegation(inode, delegation, 1);
591 * nfs_inode_return_delegation - synchronously return a delegation
592 * @inode: inode to process
594 * This routine will always flush any dirty data to disk on the
595 * assumption that if we need to return the delegation, then
596 * we should stop caching.
598 * Returns zero on success, or a negative errno value.
600 int nfs4_inode_return_delegation(struct inode *inode)
602 struct nfs_inode *nfsi = NFS_I(inode);
603 struct nfs_delegation *delegation;
607 delegation = nfs_start_delegation_return(nfsi);
608 if (delegation != NULL)
609 err = nfs_end_delegation_return(inode, delegation, 1);
614 * nfs4_inode_make_writeable
615 * @inode: pointer to inode
617 * Make the inode writeable by returning the delegation if necessary
619 * Returns zero on success, or a negative errno value.
621 int nfs4_inode_make_writeable(struct inode *inode)
623 if (!nfs4_has_session(NFS_SERVER(inode)->nfs_client) ||
624 !nfs4_check_delegation(inode, FMODE_WRITE))
625 return nfs4_inode_return_delegation(inode);
629 static void nfs_mark_return_if_closed_delegation(struct nfs_server *server,
630 struct nfs_delegation *delegation)
632 set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
633 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
636 static void nfs_mark_return_delegation(struct nfs_server *server,
637 struct nfs_delegation *delegation)
639 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
640 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
643 static bool nfs_server_mark_return_all_delegations(struct nfs_server *server)
645 struct nfs_delegation *delegation;
648 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
649 nfs_mark_return_delegation(server, delegation);
655 static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
657 struct nfs_server *server;
660 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
661 nfs_server_mark_return_all_delegations(server);
665 static void nfs_delegation_run_state_manager(struct nfs_client *clp)
667 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
668 nfs4_schedule_state_manager(clp);
672 * nfs_expire_all_delegations
673 * @clp: client to process
676 void nfs_expire_all_delegations(struct nfs_client *clp)
678 nfs_client_mark_return_all_delegations(clp);
679 nfs_delegation_run_state_manager(clp);
683 * nfs_super_return_all_delegations - return delegations for one superblock
687 void nfs_server_return_all_delegations(struct nfs_server *server)
689 struct nfs_client *clp = server->nfs_client;
696 need_wait = nfs_server_mark_return_all_delegations(server);
700 nfs4_schedule_state_manager(clp);
701 nfs4_wait_clnt_recover(clp);
705 static void nfs_mark_return_unused_delegation_types(struct nfs_server *server,
708 struct nfs_delegation *delegation;
710 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
711 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
713 if (delegation->type & flags)
714 nfs_mark_return_if_closed_delegation(server, delegation);
718 static void nfs_client_mark_return_unused_delegation_types(struct nfs_client *clp,
721 struct nfs_server *server;
724 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
725 nfs_mark_return_unused_delegation_types(server, flags);
729 static void nfs_mark_delegation_revoked(struct nfs_server *server,
730 struct nfs_delegation *delegation)
732 set_bit(NFS_DELEGATION_REVOKED, &delegation->flags);
733 delegation->stateid.type = NFS4_INVALID_STATEID_TYPE;
734 nfs_mark_return_delegation(server, delegation);
737 static bool nfs_revoke_delegation(struct inode *inode,
738 const nfs4_stateid *stateid)
740 struct nfs_delegation *delegation;
745 delegation = rcu_dereference(NFS_I(inode)->delegation);
746 if (delegation == NULL)
748 if (stateid == NULL) {
749 nfs4_stateid_copy(&tmp, &delegation->stateid);
751 } else if (!nfs4_stateid_match(stateid, &delegation->stateid))
753 nfs_mark_delegation_revoked(NFS_SERVER(inode), delegation);
758 nfs_inode_find_state_and_recover(inode, stateid);
762 void nfs_remove_bad_delegation(struct inode *inode,
763 const nfs4_stateid *stateid)
765 struct nfs_delegation *delegation;
767 if (!nfs_revoke_delegation(inode, stateid))
769 delegation = nfs_inode_detach_delegation(inode);
771 nfs_free_delegation(delegation);
773 EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation);
776 * nfs_expire_unused_delegation_types
777 * @clp: client to process
778 * @flags: delegation types to expire
781 void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags)
783 nfs_client_mark_return_unused_delegation_types(clp, flags);
784 nfs_delegation_run_state_manager(clp);
787 static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
789 struct nfs_delegation *delegation;
791 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
792 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
794 nfs_mark_return_if_closed_delegation(server, delegation);
799 * nfs_expire_unreferenced_delegations - Eliminate unused delegations
800 * @clp: nfs_client to process
803 void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
805 struct nfs_server *server;
808 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
809 nfs_mark_return_unreferenced_delegations(server);
812 nfs_delegation_run_state_manager(clp);
816 * nfs_async_inode_return_delegation - asynchronously return a delegation
817 * @inode: inode to process
818 * @stateid: state ID information
820 * Returns zero on success, or a negative errno value.
822 int nfs_async_inode_return_delegation(struct inode *inode,
823 const nfs4_stateid *stateid)
825 struct nfs_server *server = NFS_SERVER(inode);
826 struct nfs_client *clp = server->nfs_client;
827 struct nfs_delegation *delegation;
830 delegation = rcu_dereference(NFS_I(inode)->delegation);
831 if (delegation == NULL)
833 if (stateid != NULL &&
834 !clp->cl_mvops->match_stateid(&delegation->stateid, stateid))
836 nfs_mark_return_delegation(server, delegation);
839 nfs_delegation_run_state_manager(clp);
846 static struct inode *
847 nfs_delegation_find_inode_server(struct nfs_server *server,
848 const struct nfs_fh *fhandle)
850 struct nfs_delegation *delegation;
851 struct inode *freeme, *res = NULL;
853 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
854 spin_lock(&delegation->lock);
855 if (delegation->inode != NULL &&
856 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
857 freeme = igrab(delegation->inode);
858 if (freeme && nfs_sb_active(freeme->i_sb))
860 spin_unlock(&delegation->lock);
868 return ERR_PTR(-EAGAIN);
870 spin_unlock(&delegation->lock);
872 return ERR_PTR(-ENOENT);
876 * nfs_delegation_find_inode - retrieve the inode associated with a delegation
877 * @clp: client state handle
878 * @fhandle: filehandle from a delegation recall
880 * Returns pointer to inode matching "fhandle," or NULL if a matching inode
883 struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
884 const struct nfs_fh *fhandle)
886 struct nfs_server *server;
890 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
891 res = nfs_delegation_find_inode_server(server, fhandle);
892 if (res != ERR_PTR(-ENOENT)) {
898 return ERR_PTR(-ENOENT);
901 static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
903 struct nfs_delegation *delegation;
905 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
907 * If the delegation may have been admin revoked, then we
910 if (test_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags))
912 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
917 * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
918 * @clp: nfs_client to process
921 void nfs_delegation_mark_reclaim(struct nfs_client *clp)
923 struct nfs_server *server;
926 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
927 nfs_delegation_mark_reclaim_server(server);
932 * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
933 * @clp: nfs_client to process
936 void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
938 struct nfs_delegation *delegation;
939 struct nfs_server *server;
944 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
945 list_for_each_entry_rcu(delegation, &server->delegations,
947 if (test_bit(NFS_DELEGATION_RETURNING,
950 if (test_bit(NFS_DELEGATION_NEED_RECLAIM,
951 &delegation->flags) == 0)
953 if (!nfs_sb_active(server->super))
954 break; /* continue in outer loop */
955 inode = nfs_delegation_grab_inode(delegation);
958 nfs_sb_deactive(server->super);
961 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
963 if (delegation != NULL) {
964 delegation = nfs_detach_delegation(NFS_I(inode),
966 if (delegation != NULL)
967 nfs_free_delegation(delegation);
970 nfs_sb_deactive(server->super);
978 static inline bool nfs4_server_rebooted(const struct nfs_client *clp)
980 return (clp->cl_state & (BIT(NFS4CLNT_CHECK_LEASE) |
981 BIT(NFS4CLNT_LEASE_EXPIRED) |
982 BIT(NFS4CLNT_SESSION_RESET))) != 0;
985 static void nfs_mark_test_expired_delegation(struct nfs_server *server,
986 struct nfs_delegation *delegation)
988 if (delegation->stateid.type == NFS4_INVALID_STATEID_TYPE)
990 clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
991 set_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
992 set_bit(NFS4CLNT_DELEGATION_EXPIRED, &server->nfs_client->cl_state);
995 static void nfs_inode_mark_test_expired_delegation(struct nfs_server *server,
998 struct nfs_delegation *delegation;
1001 delegation = rcu_dereference(NFS_I(inode)->delegation);
1003 nfs_mark_test_expired_delegation(server, delegation);
1008 static void nfs_delegation_mark_test_expired_server(struct nfs_server *server)
1010 struct nfs_delegation *delegation;
1012 list_for_each_entry_rcu(delegation, &server->delegations, super_list)
1013 nfs_mark_test_expired_delegation(server, delegation);
1017 * nfs_mark_test_expired_all_delegations - mark all delegations for testing
1018 * @clp: nfs_client to process
1020 * Iterates through all the delegations associated with this server and
1021 * marks them as needing to be checked for validity.
1023 void nfs_mark_test_expired_all_delegations(struct nfs_client *clp)
1025 struct nfs_server *server;
1028 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1029 nfs_delegation_mark_test_expired_server(server);
1034 * nfs_reap_expired_delegations - reap expired delegations
1035 * @clp: nfs_client to process
1037 * Iterates through all the delegations associated with this server and
1038 * checks if they have may have been revoked. This function is usually
1039 * expected to be called in cases where the server may have lost its
1042 void nfs_reap_expired_delegations(struct nfs_client *clp)
1044 const struct nfs4_minor_version_ops *ops = clp->cl_mvops;
1045 struct nfs_delegation *delegation;
1046 struct nfs_server *server;
1047 struct inode *inode;
1048 const struct cred *cred;
1049 nfs4_stateid stateid;
1053 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1054 list_for_each_entry_rcu(delegation, &server->delegations,
1056 if (test_bit(NFS_DELEGATION_RETURNING,
1057 &delegation->flags))
1059 if (test_bit(NFS_DELEGATION_TEST_EXPIRED,
1060 &delegation->flags) == 0)
1062 if (!nfs_sb_active(server->super))
1063 break; /* continue in outer loop */
1064 inode = nfs_delegation_grab_inode(delegation);
1065 if (inode == NULL) {
1067 nfs_sb_deactive(server->super);
1070 cred = get_cred_rcu(delegation->cred);
1071 nfs4_stateid_copy(&stateid, &delegation->stateid);
1072 clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
1075 ops->test_and_free_expired(server, &stateid, cred) < 0) {
1076 nfs_revoke_delegation(inode, &stateid);
1077 nfs_inode_find_state_and_recover(inode, &stateid);
1080 if (nfs4_server_rebooted(clp)) {
1081 nfs_inode_mark_test_expired_delegation(server,inode);
1083 nfs_sb_deactive(server->super);
1087 nfs_sb_deactive(server->super);
1095 void nfs_inode_find_delegation_state_and_recover(struct inode *inode,
1096 const nfs4_stateid *stateid)
1098 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1099 struct nfs_delegation *delegation;
1103 delegation = rcu_dereference(NFS_I(inode)->delegation);
1105 nfs4_stateid_match_other(&delegation->stateid, stateid)) {
1106 nfs_mark_test_expired_delegation(NFS_SERVER(inode), delegation);
1111 nfs4_schedule_state_manager(clp);
1115 * nfs_delegations_present - check for existence of delegations
1116 * @clp: client state handle
1118 * Returns one if there are any nfs_delegation structures attached
1119 * to this nfs_client.
1121 int nfs_delegations_present(struct nfs_client *clp)
1123 struct nfs_server *server;
1127 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1128 if (!list_empty(&server->delegations)) {
1137 * nfs4_refresh_delegation_stateid - Update delegation stateid seqid
1138 * @dst: stateid to refresh
1139 * @inode: inode to check
1141 * Returns "true" and updates "dst->seqid" * if inode had a delegation
1142 * that matches our delegation stateid. Otherwise "false" is returned.
1144 bool nfs4_refresh_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
1146 struct nfs_delegation *delegation;
1152 delegation = rcu_dereference(NFS_I(inode)->delegation);
1153 if (delegation != NULL &&
1154 nfs4_stateid_match_other(dst, &delegation->stateid)) {
1155 dst->seqid = delegation->stateid.seqid;
1164 * nfs4_copy_delegation_stateid - Copy inode's state ID information
1165 * @inode: inode to check
1166 * @flags: delegation type requirement
1167 * @dst: stateid data structure to fill in
1168 * @cred: optional argument to retrieve credential
1170 * Returns "true" and fills in "dst->data" * if inode had a delegation,
1171 * otherwise "false" is returned.
1173 bool nfs4_copy_delegation_stateid(struct inode *inode, fmode_t flags,
1174 nfs4_stateid *dst, const struct cred **cred)
1176 struct nfs_inode *nfsi = NFS_I(inode);
1177 struct nfs_delegation *delegation;
1180 flags &= FMODE_READ|FMODE_WRITE;
1182 delegation = rcu_dereference(nfsi->delegation);
1183 ret = nfs4_is_valid_delegation(delegation, flags);
1185 nfs4_stateid_copy(dst, &delegation->stateid);
1186 nfs_mark_delegation_referenced(delegation);
1188 *cred = get_cred(delegation->cred);
1195 * nfs4_delegation_flush_on_close - Check if we must flush file on close
1196 * @inode: inode to check
1198 * This function checks the number of outstanding writes to the file
1199 * against the delegation 'space_limit' field to see if
1200 * the spec requires us to flush the file on close.
1202 bool nfs4_delegation_flush_on_close(const struct inode *inode)
1204 struct nfs_inode *nfsi = NFS_I(inode);
1205 struct nfs_delegation *delegation;
1209 delegation = rcu_dereference(nfsi->delegation);
1210 if (delegation == NULL || !(delegation->type & FMODE_WRITE))
1212 if (atomic_long_read(&nfsi->nrequests) < delegation->pagemod_limit)