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);
233 set_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags);
234 spin_unlock(&delegation->lock);
238 static struct nfs_delegation *
239 nfs_start_delegation_return_locked(struct nfs_inode *nfsi)
241 struct nfs_delegation *ret = NULL;
242 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
244 if (delegation == NULL)
246 spin_lock(&delegation->lock);
247 if (!test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
249 spin_unlock(&delegation->lock);
254 static struct nfs_delegation *
255 nfs_start_delegation_return(struct nfs_inode *nfsi)
257 struct nfs_delegation *delegation;
260 delegation = nfs_start_delegation_return_locked(nfsi);
266 nfs_abort_delegation_return(struct nfs_delegation *delegation,
267 struct nfs_client *clp)
270 spin_lock(&delegation->lock);
271 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
272 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
273 spin_unlock(&delegation->lock);
274 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
277 static struct nfs_delegation *
278 nfs_detach_delegation_locked(struct nfs_inode *nfsi,
279 struct nfs_delegation *delegation,
280 struct nfs_client *clp)
282 struct nfs_delegation *deleg_cur =
283 rcu_dereference_protected(nfsi->delegation,
284 lockdep_is_held(&clp->cl_lock));
286 if (deleg_cur == NULL || delegation != deleg_cur)
289 spin_lock(&delegation->lock);
290 set_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
291 list_del_rcu(&delegation->super_list);
292 delegation->inode = NULL;
293 rcu_assign_pointer(nfsi->delegation, NULL);
294 spin_unlock(&delegation->lock);
298 static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi,
299 struct nfs_delegation *delegation,
300 struct nfs_server *server)
302 struct nfs_client *clp = server->nfs_client;
304 spin_lock(&clp->cl_lock);
305 delegation = nfs_detach_delegation_locked(nfsi, delegation, clp);
306 spin_unlock(&clp->cl_lock);
310 static struct nfs_delegation *
311 nfs_inode_detach_delegation(struct inode *inode)
313 struct nfs_inode *nfsi = NFS_I(inode);
314 struct nfs_server *server = NFS_SERVER(inode);
315 struct nfs_delegation *delegation;
317 delegation = nfs_start_delegation_return(nfsi);
318 if (delegation == NULL)
320 return nfs_detach_delegation(nfsi, delegation, server);
324 nfs_update_inplace_delegation(struct nfs_delegation *delegation,
325 const struct nfs_delegation *update)
327 if (nfs4_stateid_is_newer(&update->stateid, &delegation->stateid)) {
328 delegation->stateid.seqid = update->stateid.seqid;
330 delegation->type = update->type;
335 * nfs_inode_set_delegation - set up a delegation on an inode
336 * @inode: inode to which delegation applies
337 * @cred: cred to use for subsequent delegation processing
338 * @type: delegation type
339 * @stateid: delegation stateid
340 * @pagemod_limit: write delegation "space_limit"
342 * Returns zero on success, or a negative errno value.
344 int nfs_inode_set_delegation(struct inode *inode, const struct cred *cred,
346 const nfs4_stateid *stateid,
347 unsigned long pagemod_limit)
349 struct nfs_server *server = NFS_SERVER(inode);
350 struct nfs_client *clp = server->nfs_client;
351 struct nfs_inode *nfsi = NFS_I(inode);
352 struct nfs_delegation *delegation, *old_delegation;
353 struct nfs_delegation *freeme = NULL;
356 delegation = kmalloc(sizeof(*delegation), GFP_NOFS);
357 if (delegation == NULL)
359 nfs4_stateid_copy(&delegation->stateid, stateid);
360 delegation->type = type;
361 delegation->pagemod_limit = pagemod_limit;
362 delegation->change_attr = inode_peek_iversion_raw(inode);
363 delegation->cred = get_cred(cred);
364 delegation->inode = inode;
365 delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
366 spin_lock_init(&delegation->lock);
368 spin_lock(&clp->cl_lock);
369 old_delegation = rcu_dereference_protected(nfsi->delegation,
370 lockdep_is_held(&clp->cl_lock));
371 if (old_delegation != NULL) {
372 /* Is this an update of the existing delegation? */
373 if (nfs4_stateid_match_other(&old_delegation->stateid,
374 &delegation->stateid)) {
375 nfs_update_inplace_delegation(old_delegation,
380 * Deal with broken servers that hand out two
381 * delegations for the same file.
382 * Allow for upgrades to a WRITE delegation, but
385 dfprintk(FILE, "%s: server %s handed out "
386 "a duplicate delegation!\n",
387 __func__, clp->cl_hostname);
388 if (delegation->type == old_delegation->type ||
389 !(delegation->type & FMODE_WRITE)) {
394 if (test_and_set_bit(NFS_DELEGATION_RETURNING,
395 &old_delegation->flags))
397 freeme = nfs_detach_delegation_locked(nfsi,
398 old_delegation, clp);
402 list_add_tail_rcu(&delegation->super_list, &server->delegations);
403 rcu_assign_pointer(nfsi->delegation, delegation);
406 trace_nfs4_set_delegation(inode, type);
408 spin_lock(&inode->i_lock);
409 if (NFS_I(inode)->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ATIME))
410 NFS_I(inode)->cache_validity |= NFS_INO_REVAL_FORCED;
411 spin_unlock(&inode->i_lock);
413 spin_unlock(&clp->cl_lock);
414 if (delegation != NULL)
415 nfs_free_delegation(delegation);
417 nfs_do_return_delegation(inode, freeme, 0);
422 * Basic procedure for returning a delegation to the server
424 static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync)
426 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
427 struct nfs_inode *nfsi = NFS_I(inode);
430 if (delegation == NULL)
433 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
435 err = nfs_delegation_claim_opens(inode, &delegation->stateid,
437 if (!issync || err != -EAGAIN)
440 * Guard against state recovery
442 err = nfs4_wait_clnt_recover(clp);
446 nfs_abort_delegation_return(delegation, clp);
449 if (!nfs_detach_delegation(nfsi, delegation, NFS_SERVER(inode)))
452 err = nfs_do_return_delegation(inode, delegation, issync);
457 static bool nfs_delegation_need_return(struct nfs_delegation *delegation)
461 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
463 if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
465 if (test_and_clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags) && !ret) {
468 spin_lock(&delegation->lock);
469 inode = delegation->inode;
470 if (inode && list_empty(&NFS_I(inode)->open_files))
472 spin_unlock(&delegation->lock);
479 * nfs_client_return_marked_delegations - return previously marked delegations
480 * @clp: nfs_client to process
482 * Note that this function is designed to be called by the state
483 * manager thread. For this reason, it cannot flush the dirty data,
484 * since that could deadlock in case of a state recovery error.
486 * Returns zero on success, or a negative errno value.
488 int nfs_client_return_marked_delegations(struct nfs_client *clp)
490 struct nfs_delegation *delegation;
491 struct nfs_delegation *prev;
492 struct nfs_server *server;
494 struct inode *place_holder = NULL;
495 struct nfs_delegation *place_holder_deleg = NULL;
500 * To avoid quadratic looping we hold a reference
501 * to an inode place_holder. Each time we restart, we
502 * list nfs_servers from the server of that inode, and
503 * delegation in the server from the delegations of that
505 * prev is an RCU-protected pointer to a delegation which
506 * wasn't marked for return and might be a good choice for
507 * the next place_holder.
512 server = NFS_SERVER(place_holder);
514 server = list_entry_rcu(clp->cl_superblocks.next,
515 struct nfs_server, client_link);
516 list_for_each_entry_from_rcu(server, &clp->cl_superblocks, client_link) {
518 if (place_holder && server == NFS_SERVER(place_holder))
519 delegation = rcu_dereference(NFS_I(place_holder)->delegation);
520 if (!delegation || delegation != place_holder_deleg)
521 delegation = list_entry_rcu(server->delegations.next,
522 struct nfs_delegation, super_list);
523 list_for_each_entry_from_rcu(delegation, &server->delegations, super_list) {
524 struct inode *to_put = NULL;
526 if (!nfs_delegation_need_return(delegation)) {
530 if (!nfs_sb_active(server->super))
531 break; /* continue in outer loop */
536 tmp = nfs_delegation_grab_inode(prev);
538 to_put = place_holder;
540 place_holder_deleg = prev;
544 inode = nfs_delegation_grab_inode(delegation);
549 nfs_sb_deactive(server->super);
552 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
558 err = nfs_end_delegation_return(inode, delegation, 0);
560 nfs_sb_deactive(server->super);
564 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
577 * nfs_inode_return_delegation_noreclaim - return delegation, don't reclaim opens
578 * @inode: inode to process
580 * Does not protect against delegation reclaims, therefore really only safe
581 * to be called from nfs4_clear_inode().
583 void nfs_inode_return_delegation_noreclaim(struct inode *inode)
585 struct nfs_delegation *delegation;
587 delegation = nfs_inode_detach_delegation(inode);
588 if (delegation != NULL)
589 nfs_do_return_delegation(inode, delegation, 1);
593 * nfs_inode_return_delegation - synchronously return a delegation
594 * @inode: inode to process
596 * This routine will always flush any dirty data to disk on the
597 * assumption that if we need to return the delegation, then
598 * we should stop caching.
600 * Returns zero on success, or a negative errno value.
602 int nfs4_inode_return_delegation(struct inode *inode)
604 struct nfs_inode *nfsi = NFS_I(inode);
605 struct nfs_delegation *delegation;
609 delegation = nfs_start_delegation_return(nfsi);
610 if (delegation != NULL)
611 err = nfs_end_delegation_return(inode, delegation, 1);
616 * nfs4_inode_make_writeable
617 * @inode: pointer to inode
619 * Make the inode writeable by returning the delegation if necessary
621 * Returns zero on success, or a negative errno value.
623 int nfs4_inode_make_writeable(struct inode *inode)
625 if (!nfs4_has_session(NFS_SERVER(inode)->nfs_client) ||
626 !nfs4_check_delegation(inode, FMODE_WRITE))
627 return nfs4_inode_return_delegation(inode);
631 static void nfs_mark_return_if_closed_delegation(struct nfs_server *server,
632 struct nfs_delegation *delegation)
634 set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
635 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
638 static void nfs_mark_return_delegation(struct nfs_server *server,
639 struct nfs_delegation *delegation)
641 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
642 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
645 static bool nfs_server_mark_return_all_delegations(struct nfs_server *server)
647 struct nfs_delegation *delegation;
650 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
651 nfs_mark_return_delegation(server, delegation);
657 static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
659 struct nfs_server *server;
662 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
663 nfs_server_mark_return_all_delegations(server);
667 static void nfs_delegation_run_state_manager(struct nfs_client *clp)
669 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
670 nfs4_schedule_state_manager(clp);
674 * nfs_expire_all_delegations
675 * @clp: client to process
678 void nfs_expire_all_delegations(struct nfs_client *clp)
680 nfs_client_mark_return_all_delegations(clp);
681 nfs_delegation_run_state_manager(clp);
685 * nfs_super_return_all_delegations - return delegations for one superblock
686 * @server: pointer to nfs_server to process
689 void nfs_server_return_all_delegations(struct nfs_server *server)
691 struct nfs_client *clp = server->nfs_client;
698 need_wait = nfs_server_mark_return_all_delegations(server);
702 nfs4_schedule_state_manager(clp);
703 nfs4_wait_clnt_recover(clp);
707 static void nfs_mark_return_unused_delegation_types(struct nfs_server *server,
710 struct nfs_delegation *delegation;
712 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
713 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
715 if (delegation->type & flags)
716 nfs_mark_return_if_closed_delegation(server, delegation);
720 static void nfs_client_mark_return_unused_delegation_types(struct nfs_client *clp,
723 struct nfs_server *server;
726 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
727 nfs_mark_return_unused_delegation_types(server, flags);
731 static void nfs_mark_delegation_revoked(struct nfs_server *server,
732 struct nfs_delegation *delegation)
734 set_bit(NFS_DELEGATION_REVOKED, &delegation->flags);
735 delegation->stateid.type = NFS4_INVALID_STATEID_TYPE;
736 nfs_mark_return_delegation(server, delegation);
739 static bool nfs_revoke_delegation(struct inode *inode,
740 const nfs4_stateid *stateid)
742 struct nfs_delegation *delegation;
747 delegation = rcu_dereference(NFS_I(inode)->delegation);
748 if (delegation == NULL)
750 if (stateid == NULL) {
751 nfs4_stateid_copy(&tmp, &delegation->stateid);
753 } else if (!nfs4_stateid_match(stateid, &delegation->stateid))
755 nfs_mark_delegation_revoked(NFS_SERVER(inode), delegation);
760 nfs_inode_find_state_and_recover(inode, stateid);
764 void nfs_remove_bad_delegation(struct inode *inode,
765 const nfs4_stateid *stateid)
767 struct nfs_delegation *delegation;
769 if (!nfs_revoke_delegation(inode, stateid))
771 delegation = nfs_inode_detach_delegation(inode);
773 nfs_free_delegation(delegation);
775 EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation);
778 * nfs_expire_unused_delegation_types
779 * @clp: client to process
780 * @flags: delegation types to expire
783 void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags)
785 nfs_client_mark_return_unused_delegation_types(clp, flags);
786 nfs_delegation_run_state_manager(clp);
789 static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
791 struct nfs_delegation *delegation;
793 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
794 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
796 nfs_mark_return_if_closed_delegation(server, delegation);
801 * nfs_expire_unreferenced_delegations - Eliminate unused delegations
802 * @clp: nfs_client to process
805 void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
807 struct nfs_server *server;
810 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
811 nfs_mark_return_unreferenced_delegations(server);
814 nfs_delegation_run_state_manager(clp);
818 * nfs_async_inode_return_delegation - asynchronously return a delegation
819 * @inode: inode to process
820 * @stateid: state ID information
822 * Returns zero on success, or a negative errno value.
824 int nfs_async_inode_return_delegation(struct inode *inode,
825 const nfs4_stateid *stateid)
827 struct nfs_server *server = NFS_SERVER(inode);
828 struct nfs_client *clp = server->nfs_client;
829 struct nfs_delegation *delegation;
832 delegation = rcu_dereference(NFS_I(inode)->delegation);
833 if (delegation == NULL)
835 if (stateid != NULL &&
836 !clp->cl_mvops->match_stateid(&delegation->stateid, stateid))
838 nfs_mark_return_delegation(server, delegation);
841 nfs_delegation_run_state_manager(clp);
848 static struct inode *
849 nfs_delegation_find_inode_server(struct nfs_server *server,
850 const struct nfs_fh *fhandle)
852 struct nfs_delegation *delegation;
853 struct inode *freeme, *res = NULL;
855 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
856 spin_lock(&delegation->lock);
857 if (delegation->inode != NULL &&
858 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
859 freeme = igrab(delegation->inode);
860 if (freeme && nfs_sb_active(freeme->i_sb))
862 spin_unlock(&delegation->lock);
870 return ERR_PTR(-EAGAIN);
872 spin_unlock(&delegation->lock);
874 return ERR_PTR(-ENOENT);
878 * nfs_delegation_find_inode - retrieve the inode associated with a delegation
879 * @clp: client state handle
880 * @fhandle: filehandle from a delegation recall
882 * Returns pointer to inode matching "fhandle," or NULL if a matching inode
885 struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
886 const struct nfs_fh *fhandle)
888 struct nfs_server *server;
892 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
893 res = nfs_delegation_find_inode_server(server, fhandle);
894 if (res != ERR_PTR(-ENOENT)) {
900 return ERR_PTR(-ENOENT);
903 static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
905 struct nfs_delegation *delegation;
907 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
909 * If the delegation may have been admin revoked, then we
912 if (test_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags))
914 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
919 * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
920 * @clp: nfs_client to process
923 void nfs_delegation_mark_reclaim(struct nfs_client *clp)
925 struct nfs_server *server;
928 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
929 nfs_delegation_mark_reclaim_server(server);
934 * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
935 * @clp: nfs_client to process
938 void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
940 struct nfs_delegation *delegation;
941 struct nfs_server *server;
946 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
947 list_for_each_entry_rcu(delegation, &server->delegations,
949 if (test_bit(NFS_DELEGATION_INODE_FREEING,
950 &delegation->flags) ||
951 test_bit(NFS_DELEGATION_RETURNING,
952 &delegation->flags) ||
953 test_bit(NFS_DELEGATION_NEED_RECLAIM,
954 &delegation->flags) == 0)
956 if (!nfs_sb_active(server->super))
957 break; /* continue in outer loop */
958 inode = nfs_delegation_grab_inode(delegation);
961 nfs_sb_deactive(server->super);
964 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
966 if (delegation != NULL) {
967 delegation = nfs_detach_delegation(NFS_I(inode),
969 if (delegation != NULL)
970 nfs_free_delegation(delegation);
973 nfs_sb_deactive(server->super);
981 static inline bool nfs4_server_rebooted(const struct nfs_client *clp)
983 return (clp->cl_state & (BIT(NFS4CLNT_CHECK_LEASE) |
984 BIT(NFS4CLNT_LEASE_EXPIRED) |
985 BIT(NFS4CLNT_SESSION_RESET))) != 0;
988 static void nfs_mark_test_expired_delegation(struct nfs_server *server,
989 struct nfs_delegation *delegation)
991 if (delegation->stateid.type == NFS4_INVALID_STATEID_TYPE)
993 clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
994 set_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
995 set_bit(NFS4CLNT_DELEGATION_EXPIRED, &server->nfs_client->cl_state);
998 static void nfs_inode_mark_test_expired_delegation(struct nfs_server *server,
1001 struct nfs_delegation *delegation;
1004 delegation = rcu_dereference(NFS_I(inode)->delegation);
1006 nfs_mark_test_expired_delegation(server, delegation);
1011 static void nfs_delegation_mark_test_expired_server(struct nfs_server *server)
1013 struct nfs_delegation *delegation;
1015 list_for_each_entry_rcu(delegation, &server->delegations, super_list)
1016 nfs_mark_test_expired_delegation(server, delegation);
1020 * nfs_mark_test_expired_all_delegations - mark all delegations for testing
1021 * @clp: nfs_client to process
1023 * Iterates through all the delegations associated with this server and
1024 * marks them as needing to be checked for validity.
1026 void nfs_mark_test_expired_all_delegations(struct nfs_client *clp)
1028 struct nfs_server *server;
1031 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1032 nfs_delegation_mark_test_expired_server(server);
1037 * nfs_reap_expired_delegations - reap expired delegations
1038 * @clp: nfs_client to process
1040 * Iterates through all the delegations associated with this server and
1041 * checks if they have may have been revoked. This function is usually
1042 * expected to be called in cases where the server may have lost its
1045 void nfs_reap_expired_delegations(struct nfs_client *clp)
1047 const struct nfs4_minor_version_ops *ops = clp->cl_mvops;
1048 struct nfs_delegation *delegation;
1049 struct nfs_server *server;
1050 struct inode *inode;
1051 const struct cred *cred;
1052 nfs4_stateid stateid;
1056 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1057 list_for_each_entry_rcu(delegation, &server->delegations,
1059 if (test_bit(NFS_DELEGATION_INODE_FREEING,
1060 &delegation->flags) ||
1061 test_bit(NFS_DELEGATION_RETURNING,
1062 &delegation->flags) ||
1063 test_bit(NFS_DELEGATION_TEST_EXPIRED,
1064 &delegation->flags) == 0)
1066 if (!nfs_sb_active(server->super))
1067 break; /* continue in outer loop */
1068 inode = nfs_delegation_grab_inode(delegation);
1069 if (inode == NULL) {
1071 nfs_sb_deactive(server->super);
1074 cred = get_cred_rcu(delegation->cred);
1075 nfs4_stateid_copy(&stateid, &delegation->stateid);
1076 clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
1079 ops->test_and_free_expired(server, &stateid, cred) < 0) {
1080 nfs_revoke_delegation(inode, &stateid);
1081 nfs_inode_find_state_and_recover(inode, &stateid);
1084 if (nfs4_server_rebooted(clp)) {
1085 nfs_inode_mark_test_expired_delegation(server,inode);
1087 nfs_sb_deactive(server->super);
1091 nfs_sb_deactive(server->super);
1099 void nfs_inode_find_delegation_state_and_recover(struct inode *inode,
1100 const nfs4_stateid *stateid)
1102 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1103 struct nfs_delegation *delegation;
1107 delegation = rcu_dereference(NFS_I(inode)->delegation);
1109 nfs4_stateid_match_other(&delegation->stateid, stateid)) {
1110 nfs_mark_test_expired_delegation(NFS_SERVER(inode), delegation);
1115 nfs4_schedule_state_manager(clp);
1119 * nfs_delegations_present - check for existence of delegations
1120 * @clp: client state handle
1122 * Returns one if there are any nfs_delegation structures attached
1123 * to this nfs_client.
1125 int nfs_delegations_present(struct nfs_client *clp)
1127 struct nfs_server *server;
1131 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1132 if (!list_empty(&server->delegations)) {
1141 * nfs4_refresh_delegation_stateid - Update delegation stateid seqid
1142 * @dst: stateid to refresh
1143 * @inode: inode to check
1145 * Returns "true" and updates "dst->seqid" * if inode had a delegation
1146 * that matches our delegation stateid. Otherwise "false" is returned.
1148 bool nfs4_refresh_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
1150 struct nfs_delegation *delegation;
1156 delegation = rcu_dereference(NFS_I(inode)->delegation);
1157 if (delegation != NULL &&
1158 nfs4_stateid_match_other(dst, &delegation->stateid)) {
1159 dst->seqid = delegation->stateid.seqid;
1168 * nfs4_copy_delegation_stateid - Copy inode's state ID information
1169 * @inode: inode to check
1170 * @flags: delegation type requirement
1171 * @dst: stateid data structure to fill in
1172 * @cred: optional argument to retrieve credential
1174 * Returns "true" and fills in "dst->data" * if inode had a delegation,
1175 * otherwise "false" is returned.
1177 bool nfs4_copy_delegation_stateid(struct inode *inode, fmode_t flags,
1178 nfs4_stateid *dst, const struct cred **cred)
1180 struct nfs_inode *nfsi = NFS_I(inode);
1181 struct nfs_delegation *delegation;
1184 flags &= FMODE_READ|FMODE_WRITE;
1186 delegation = rcu_dereference(nfsi->delegation);
1187 ret = nfs4_is_valid_delegation(delegation, flags);
1189 nfs4_stateid_copy(dst, &delegation->stateid);
1190 nfs_mark_delegation_referenced(delegation);
1192 *cred = get_cred(delegation->cred);
1199 * nfs4_delegation_flush_on_close - Check if we must flush file on close
1200 * @inode: inode to check
1202 * This function checks the number of outstanding writes to the file
1203 * against the delegation 'space_limit' field to see if
1204 * the spec requires us to flush the file on close.
1206 bool nfs4_delegation_flush_on_close(const struct inode *inode)
1208 struct nfs_inode *nfsi = NFS_I(inode);
1209 struct nfs_delegation *delegation;
1213 delegation = rcu_dereference(nfsi->delegation);
1214 if (delegation == NULL || !(delegation->type & FMODE_WRITE))
1216 if (atomic_long_read(&nfsi->nrequests) < delegation->pagemod_limit)