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 "delegation.h"
24 #include "nfs4trace.h"
26 static void nfs_free_delegation(struct nfs_delegation
*delegation
)
28 if (delegation
->cred
) {
29 put_rpccred(delegation
->cred
);
30 delegation
->cred
= NULL
;
32 kfree_rcu(delegation
, rcu
);
36 * nfs_mark_delegation_referenced - set delegation's REFERENCED flag
37 * @delegation: delegation to process
40 void nfs_mark_delegation_referenced(struct nfs_delegation
*delegation
)
42 set_bit(NFS_DELEGATION_REFERENCED
, &delegation
->flags
);
46 nfs4_is_valid_delegation(const struct nfs_delegation
*delegation
,
49 if (delegation
!= NULL
&& (delegation
->type
& flags
) == flags
&&
50 !test_bit(NFS_DELEGATION_REVOKED
, &delegation
->flags
) &&
51 !test_bit(NFS_DELEGATION_RETURNING
, &delegation
->flags
))
57 nfs4_do_check_delegation(struct inode
*inode
, fmode_t flags
, bool mark
)
59 struct nfs_delegation
*delegation
;
62 flags
&= FMODE_READ
|FMODE_WRITE
;
64 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
65 if (nfs4_is_valid_delegation(delegation
, flags
)) {
67 nfs_mark_delegation_referenced(delegation
);
74 * nfs_have_delegation - check if inode has a delegation, mark it
75 * NFS_DELEGATION_REFERENCED if there is one.
76 * @inode: inode to check
77 * @flags: delegation types to check for
79 * Returns one if inode has the indicated delegation, otherwise zero.
81 int nfs4_have_delegation(struct inode
*inode
, fmode_t flags
)
83 return nfs4_do_check_delegation(inode
, flags
, true);
87 * nfs4_check_delegation - check if inode has a delegation, do not mark
88 * NFS_DELEGATION_REFERENCED if it has one.
90 int nfs4_check_delegation(struct inode
*inode
, fmode_t flags
)
92 return nfs4_do_check_delegation(inode
, flags
, false);
95 static int nfs_delegation_claim_locks(struct nfs_open_context
*ctx
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
97 struct inode
*inode
= state
->inode
;
99 struct file_lock_context
*flctx
= inode
->i_flctx
;
100 struct list_head
*list
;
106 list
= &flctx
->flc_posix
;
107 spin_lock(&flctx
->flc_lock
);
109 list_for_each_entry(fl
, list
, fl_list
) {
110 if (nfs_file_open_context(fl
->fl_file
) != ctx
)
112 spin_unlock(&flctx
->flc_lock
);
113 status
= nfs4_lock_delegation_recall(fl
, state
, stateid
);
116 spin_lock(&flctx
->flc_lock
);
118 if (list
== &flctx
->flc_posix
) {
119 list
= &flctx
->flc_flock
;
122 spin_unlock(&flctx
->flc_lock
);
127 static int nfs_delegation_claim_opens(struct inode
*inode
,
128 const nfs4_stateid
*stateid
, fmode_t type
)
130 struct nfs_inode
*nfsi
= NFS_I(inode
);
131 struct nfs_open_context
*ctx
;
132 struct nfs4_state_owner
*sp
;
133 struct nfs4_state
*state
;
138 spin_lock(&inode
->i_lock
);
139 list_for_each_entry(ctx
, &nfsi
->open_files
, list
) {
143 if (!test_bit(NFS_DELEGATED_STATE
, &state
->flags
))
145 if (!nfs4_valid_open_stateid(state
))
147 if (!nfs4_stateid_match(&state
->stateid
, stateid
))
149 get_nfs_open_context(ctx
);
150 spin_unlock(&inode
->i_lock
);
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(ctx
, 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
);
166 spin_unlock(&inode
->i_lock
);
171 * nfs_inode_reclaim_delegation - process a delegation reclaim request
172 * @inode: inode to process
173 * @cred: credential to use for request
174 * @res: new delegation state from server
177 void nfs_inode_reclaim_delegation(struct inode
*inode
, struct rpc_cred
*cred
,
178 struct nfs_openres
*res
)
180 struct nfs_delegation
*delegation
;
181 struct rpc_cred
*oldcred
= NULL
;
184 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
185 if (delegation
!= NULL
) {
186 spin_lock(&delegation
->lock
);
187 if (delegation
->inode
!= NULL
) {
188 nfs4_stateid_copy(&delegation
->stateid
, &res
->delegation
);
189 delegation
->type
= res
->delegation_type
;
190 delegation
->pagemod_limit
= res
->pagemod_limit
;
191 oldcred
= delegation
->cred
;
192 delegation
->cred
= get_rpccred(cred
);
193 clear_bit(NFS_DELEGATION_NEED_RECLAIM
,
195 spin_unlock(&delegation
->lock
);
197 put_rpccred(oldcred
);
198 trace_nfs4_reclaim_delegation(inode
, res
->delegation_type
);
201 /* We appear to have raced with a delegation return. */
202 spin_unlock(&delegation
->lock
);
205 nfs_inode_set_delegation(inode
, cred
, res
);
208 static int nfs_do_return_delegation(struct inode
*inode
, struct nfs_delegation
*delegation
, int issync
)
212 if (!test_bit(NFS_DELEGATION_REVOKED
, &delegation
->flags
))
213 res
= nfs4_proc_delegreturn(inode
,
215 &delegation
->stateid
,
217 nfs_free_delegation(delegation
);
221 static struct inode
*nfs_delegation_grab_inode(struct nfs_delegation
*delegation
)
223 struct inode
*inode
= NULL
;
225 spin_lock(&delegation
->lock
);
226 if (delegation
->inode
!= NULL
)
227 inode
= igrab(delegation
->inode
);
228 spin_unlock(&delegation
->lock
);
232 static struct nfs_delegation
*
233 nfs_start_delegation_return_locked(struct nfs_inode
*nfsi
)
235 struct nfs_delegation
*ret
= NULL
;
236 struct nfs_delegation
*delegation
= rcu_dereference(nfsi
->delegation
);
238 if (delegation
== NULL
)
240 spin_lock(&delegation
->lock
);
241 if (!test_and_set_bit(NFS_DELEGATION_RETURNING
, &delegation
->flags
))
243 spin_unlock(&delegation
->lock
);
248 static struct nfs_delegation
*
249 nfs_start_delegation_return(struct nfs_inode
*nfsi
)
251 struct nfs_delegation
*delegation
;
254 delegation
= nfs_start_delegation_return_locked(nfsi
);
260 nfs_abort_delegation_return(struct nfs_delegation
*delegation
,
261 struct nfs_client
*clp
)
264 spin_lock(&delegation
->lock
);
265 clear_bit(NFS_DELEGATION_RETURNING
, &delegation
->flags
);
266 set_bit(NFS_DELEGATION_RETURN
, &delegation
->flags
);
267 spin_unlock(&delegation
->lock
);
268 set_bit(NFS4CLNT_DELEGRETURN
, &clp
->cl_state
);
271 static struct nfs_delegation
*
272 nfs_detach_delegation_locked(struct nfs_inode
*nfsi
,
273 struct nfs_delegation
*delegation
,
274 struct nfs_client
*clp
)
276 struct nfs_delegation
*deleg_cur
=
277 rcu_dereference_protected(nfsi
->delegation
,
278 lockdep_is_held(&clp
->cl_lock
));
280 if (deleg_cur
== NULL
|| delegation
!= deleg_cur
)
283 spin_lock(&delegation
->lock
);
284 set_bit(NFS_DELEGATION_RETURNING
, &delegation
->flags
);
285 list_del_rcu(&delegation
->super_list
);
286 delegation
->inode
= NULL
;
287 rcu_assign_pointer(nfsi
->delegation
, NULL
);
288 spin_unlock(&delegation
->lock
);
292 static struct nfs_delegation
*nfs_detach_delegation(struct nfs_inode
*nfsi
,
293 struct nfs_delegation
*delegation
,
294 struct nfs_server
*server
)
296 struct nfs_client
*clp
= server
->nfs_client
;
298 spin_lock(&clp
->cl_lock
);
299 delegation
= nfs_detach_delegation_locked(nfsi
, delegation
, clp
);
300 spin_unlock(&clp
->cl_lock
);
304 static struct nfs_delegation
*
305 nfs_inode_detach_delegation(struct inode
*inode
)
307 struct nfs_inode
*nfsi
= NFS_I(inode
);
308 struct nfs_server
*server
= NFS_SERVER(inode
);
309 struct nfs_delegation
*delegation
;
311 delegation
= nfs_start_delegation_return(nfsi
);
312 if (delegation
== NULL
)
314 return nfs_detach_delegation(nfsi
, delegation
, server
);
318 nfs_update_inplace_delegation(struct nfs_delegation
*delegation
,
319 const struct nfs_delegation
*update
)
321 if (nfs4_stateid_is_newer(&update
->stateid
, &delegation
->stateid
)) {
322 delegation
->stateid
.seqid
= update
->stateid
.seqid
;
324 delegation
->type
= update
->type
;
329 * nfs_inode_set_delegation - set up a delegation on an inode
330 * @inode: inode to which delegation applies
331 * @cred: cred to use for subsequent delegation processing
332 * @res: new delegation state from server
334 * Returns zero on success, or a negative errno value.
336 int nfs_inode_set_delegation(struct inode
*inode
, struct rpc_cred
*cred
, struct nfs_openres
*res
)
338 struct nfs_server
*server
= NFS_SERVER(inode
);
339 struct nfs_client
*clp
= server
->nfs_client
;
340 struct nfs_inode
*nfsi
= NFS_I(inode
);
341 struct nfs_delegation
*delegation
, *old_delegation
;
342 struct nfs_delegation
*freeme
= NULL
;
345 delegation
= kmalloc(sizeof(*delegation
), GFP_NOFS
);
346 if (delegation
== NULL
)
348 nfs4_stateid_copy(&delegation
->stateid
, &res
->delegation
);
349 delegation
->type
= res
->delegation_type
;
350 delegation
->pagemod_limit
= res
->pagemod_limit
;
351 delegation
->change_attr
= inode_peek_iversion_raw(inode
);
352 delegation
->cred
= get_rpccred(cred
);
353 delegation
->inode
= inode
;
354 delegation
->flags
= 1<<NFS_DELEGATION_REFERENCED
;
355 spin_lock_init(&delegation
->lock
);
357 spin_lock(&clp
->cl_lock
);
358 old_delegation
= rcu_dereference_protected(nfsi
->delegation
,
359 lockdep_is_held(&clp
->cl_lock
));
360 if (old_delegation
!= NULL
) {
361 /* Is this an update of the existing delegation? */
362 if (nfs4_stateid_match_other(&old_delegation
->stateid
,
363 &delegation
->stateid
)) {
364 nfs_update_inplace_delegation(old_delegation
,
369 * Deal with broken servers that hand out two
370 * delegations for the same file.
371 * Allow for upgrades to a WRITE delegation, but
374 dfprintk(FILE, "%s: server %s handed out "
375 "a duplicate delegation!\n",
376 __func__
, clp
->cl_hostname
);
377 if (delegation
->type
== old_delegation
->type
||
378 !(delegation
->type
& FMODE_WRITE
)) {
383 if (test_and_set_bit(NFS_DELEGATION_RETURNING
,
384 &old_delegation
->flags
))
386 freeme
= nfs_detach_delegation_locked(nfsi
,
387 old_delegation
, clp
);
391 list_add_tail_rcu(&delegation
->super_list
, &server
->delegations
);
392 rcu_assign_pointer(nfsi
->delegation
, delegation
);
395 trace_nfs4_set_delegation(inode
, res
->delegation_type
);
398 spin_unlock(&clp
->cl_lock
);
399 if (delegation
!= NULL
)
400 nfs_free_delegation(delegation
);
402 nfs_do_return_delegation(inode
, freeme
, 0);
407 * Basic procedure for returning a delegation to the server
409 static int nfs_end_delegation_return(struct inode
*inode
, struct nfs_delegation
*delegation
, int issync
)
411 struct nfs_client
*clp
= NFS_SERVER(inode
)->nfs_client
;
412 struct nfs_inode
*nfsi
= NFS_I(inode
);
415 if (delegation
== NULL
)
418 if (test_bit(NFS_DELEGATION_REVOKED
, &delegation
->flags
))
420 err
= nfs_delegation_claim_opens(inode
, &delegation
->stateid
,
422 if (!issync
|| err
!= -EAGAIN
)
425 * Guard against state recovery
427 err
= nfs4_wait_clnt_recover(clp
);
431 nfs_abort_delegation_return(delegation
, clp
);
434 if (!nfs_detach_delegation(nfsi
, delegation
, NFS_SERVER(inode
)))
437 err
= nfs_do_return_delegation(inode
, delegation
, issync
);
442 static bool nfs_delegation_need_return(struct nfs_delegation
*delegation
)
446 if (test_bit(NFS_DELEGATION_RETURNING
, &delegation
->flags
))
448 if (test_and_clear_bit(NFS_DELEGATION_RETURN
, &delegation
->flags
))
450 if (test_and_clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED
, &delegation
->flags
) && !ret
) {
453 spin_lock(&delegation
->lock
);
454 inode
= delegation
->inode
;
455 if (inode
&& list_empty(&NFS_I(inode
)->open_files
))
457 spin_unlock(&delegation
->lock
);
464 * nfs_client_return_marked_delegations - return previously marked delegations
465 * @clp: nfs_client to process
467 * Note that this function is designed to be called by the state
468 * manager thread. For this reason, it cannot flush the dirty data,
469 * since that could deadlock in case of a state recovery error.
471 * Returns zero on success, or a negative errno value.
473 int nfs_client_return_marked_delegations(struct nfs_client
*clp
)
475 struct nfs_delegation
*delegation
;
476 struct nfs_server
*server
;
482 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
) {
483 list_for_each_entry_rcu(delegation
, &server
->delegations
,
485 if (!nfs_delegation_need_return(delegation
))
487 if (!nfs_sb_active(server
->super
))
489 inode
= nfs_delegation_grab_inode(delegation
);
492 nfs_sb_deactive(server
->super
);
495 delegation
= nfs_start_delegation_return_locked(NFS_I(inode
));
498 err
= nfs_end_delegation_return(inode
, delegation
, 0);
500 nfs_sb_deactive(server
->super
);
503 set_bit(NFS4CLNT_DELEGRETURN
, &clp
->cl_state
);
512 * nfs_inode_return_delegation_noreclaim - return delegation, don't reclaim opens
513 * @inode: inode to process
515 * Does not protect against delegation reclaims, therefore really only safe
516 * to be called from nfs4_clear_inode().
518 void nfs_inode_return_delegation_noreclaim(struct inode
*inode
)
520 struct nfs_delegation
*delegation
;
522 delegation
= nfs_inode_detach_delegation(inode
);
523 if (delegation
!= NULL
)
524 nfs_do_return_delegation(inode
, delegation
, 1);
528 * nfs_inode_return_delegation - synchronously return a delegation
529 * @inode: inode to process
531 * This routine will always flush any dirty data to disk on the
532 * assumption that if we need to return the delegation, then
533 * we should stop caching.
535 * Returns zero on success, or a negative errno value.
537 int nfs4_inode_return_delegation(struct inode
*inode
)
539 struct nfs_inode
*nfsi
= NFS_I(inode
);
540 struct nfs_delegation
*delegation
;
544 delegation
= nfs_start_delegation_return(nfsi
);
545 if (delegation
!= NULL
)
546 err
= nfs_end_delegation_return(inode
, delegation
, 1);
550 static void nfs_mark_return_if_closed_delegation(struct nfs_server
*server
,
551 struct nfs_delegation
*delegation
)
553 set_bit(NFS_DELEGATION_RETURN_IF_CLOSED
, &delegation
->flags
);
554 set_bit(NFS4CLNT_DELEGRETURN
, &server
->nfs_client
->cl_state
);
557 static void nfs_mark_return_delegation(struct nfs_server
*server
,
558 struct nfs_delegation
*delegation
)
560 set_bit(NFS_DELEGATION_RETURN
, &delegation
->flags
);
561 set_bit(NFS4CLNT_DELEGRETURN
, &server
->nfs_client
->cl_state
);
564 static bool nfs_server_mark_return_all_delegations(struct nfs_server
*server
)
566 struct nfs_delegation
*delegation
;
569 list_for_each_entry_rcu(delegation
, &server
->delegations
, super_list
) {
570 nfs_mark_return_delegation(server
, delegation
);
576 static void nfs_client_mark_return_all_delegations(struct nfs_client
*clp
)
578 struct nfs_server
*server
;
581 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
)
582 nfs_server_mark_return_all_delegations(server
);
586 static void nfs_delegation_run_state_manager(struct nfs_client
*clp
)
588 if (test_bit(NFS4CLNT_DELEGRETURN
, &clp
->cl_state
))
589 nfs4_schedule_state_manager(clp
);
593 * nfs_expire_all_delegations
594 * @clp: client to process
597 void nfs_expire_all_delegations(struct nfs_client
*clp
)
599 nfs_client_mark_return_all_delegations(clp
);
600 nfs_delegation_run_state_manager(clp
);
604 * nfs_super_return_all_delegations - return delegations for one superblock
608 void nfs_server_return_all_delegations(struct nfs_server
*server
)
610 struct nfs_client
*clp
= server
->nfs_client
;
617 need_wait
= nfs_server_mark_return_all_delegations(server
);
621 nfs4_schedule_state_manager(clp
);
622 nfs4_wait_clnt_recover(clp
);
626 static void nfs_mark_return_unused_delegation_types(struct nfs_server
*server
,
629 struct nfs_delegation
*delegation
;
631 list_for_each_entry_rcu(delegation
, &server
->delegations
, super_list
) {
632 if ((delegation
->type
== (FMODE_READ
|FMODE_WRITE
)) && !(flags
& FMODE_WRITE
))
634 if (delegation
->type
& flags
)
635 nfs_mark_return_if_closed_delegation(server
, delegation
);
639 static void nfs_client_mark_return_unused_delegation_types(struct nfs_client
*clp
,
642 struct nfs_server
*server
;
645 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
)
646 nfs_mark_return_unused_delegation_types(server
, flags
);
650 static void nfs_mark_delegation_revoked(struct nfs_server
*server
,
651 struct nfs_delegation
*delegation
)
653 set_bit(NFS_DELEGATION_REVOKED
, &delegation
->flags
);
654 delegation
->stateid
.type
= NFS4_INVALID_STATEID_TYPE
;
655 nfs_mark_return_delegation(server
, delegation
);
658 static bool nfs_revoke_delegation(struct inode
*inode
,
659 const nfs4_stateid
*stateid
)
661 struct nfs_delegation
*delegation
;
666 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
667 if (delegation
== NULL
)
669 if (stateid
== NULL
) {
670 nfs4_stateid_copy(&tmp
, &delegation
->stateid
);
672 } else if (!nfs4_stateid_match(stateid
, &delegation
->stateid
))
674 nfs_mark_delegation_revoked(NFS_SERVER(inode
), delegation
);
679 nfs_inode_find_state_and_recover(inode
, stateid
);
683 void nfs_remove_bad_delegation(struct inode
*inode
,
684 const nfs4_stateid
*stateid
)
686 struct nfs_delegation
*delegation
;
688 if (!nfs_revoke_delegation(inode
, stateid
))
690 delegation
= nfs_inode_detach_delegation(inode
);
692 nfs_free_delegation(delegation
);
694 EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation
);
697 * nfs_expire_unused_delegation_types
698 * @clp: client to process
699 * @flags: delegation types to expire
702 void nfs_expire_unused_delegation_types(struct nfs_client
*clp
, fmode_t flags
)
704 nfs_client_mark_return_unused_delegation_types(clp
, flags
);
705 nfs_delegation_run_state_manager(clp
);
708 static void nfs_mark_return_unreferenced_delegations(struct nfs_server
*server
)
710 struct nfs_delegation
*delegation
;
712 list_for_each_entry_rcu(delegation
, &server
->delegations
, super_list
) {
713 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED
, &delegation
->flags
))
715 nfs_mark_return_if_closed_delegation(server
, delegation
);
720 * nfs_expire_unreferenced_delegations - Eliminate unused delegations
721 * @clp: nfs_client to process
724 void nfs_expire_unreferenced_delegations(struct nfs_client
*clp
)
726 struct nfs_server
*server
;
729 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
)
730 nfs_mark_return_unreferenced_delegations(server
);
733 nfs_delegation_run_state_manager(clp
);
737 * nfs_async_inode_return_delegation - asynchronously return a delegation
738 * @inode: inode to process
739 * @stateid: state ID information
741 * Returns zero on success, or a negative errno value.
743 int nfs_async_inode_return_delegation(struct inode
*inode
,
744 const nfs4_stateid
*stateid
)
746 struct nfs_server
*server
= NFS_SERVER(inode
);
747 struct nfs_client
*clp
= server
->nfs_client
;
748 struct nfs_delegation
*delegation
;
751 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
752 if (delegation
== NULL
)
754 if (stateid
!= NULL
&&
755 !clp
->cl_mvops
->match_stateid(&delegation
->stateid
, stateid
))
757 nfs_mark_return_delegation(server
, delegation
);
760 nfs_delegation_run_state_manager(clp
);
767 static struct inode
*
768 nfs_delegation_find_inode_server(struct nfs_server
*server
,
769 const struct nfs_fh
*fhandle
)
771 struct nfs_delegation
*delegation
;
772 struct inode
*res
= NULL
;
774 list_for_each_entry_rcu(delegation
, &server
->delegations
, super_list
) {
775 spin_lock(&delegation
->lock
);
776 if (delegation
->inode
!= NULL
&&
777 nfs_compare_fh(fhandle
, &NFS_I(delegation
->inode
)->fh
) == 0) {
778 res
= igrab(delegation
->inode
);
780 spin_unlock(&delegation
->lock
);
788 * nfs_delegation_find_inode - retrieve the inode associated with a delegation
789 * @clp: client state handle
790 * @fhandle: filehandle from a delegation recall
792 * Returns pointer to inode matching "fhandle," or NULL if a matching inode
795 struct inode
*nfs_delegation_find_inode(struct nfs_client
*clp
,
796 const struct nfs_fh
*fhandle
)
798 struct nfs_server
*server
;
799 struct inode
*res
= NULL
;
802 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
) {
803 res
= nfs_delegation_find_inode_server(server
, fhandle
);
811 static void nfs_delegation_mark_reclaim_server(struct nfs_server
*server
)
813 struct nfs_delegation
*delegation
;
815 list_for_each_entry_rcu(delegation
, &server
->delegations
, super_list
) {
817 * If the delegation may have been admin revoked, then we
820 if (test_bit(NFS_DELEGATION_TEST_EXPIRED
, &delegation
->flags
))
822 set_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
);
827 * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
828 * @clp: nfs_client to process
831 void nfs_delegation_mark_reclaim(struct nfs_client
*clp
)
833 struct nfs_server
*server
;
836 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
)
837 nfs_delegation_mark_reclaim_server(server
);
842 * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
843 * @clp: nfs_client to process
846 void nfs_delegation_reap_unclaimed(struct nfs_client
*clp
)
848 struct nfs_delegation
*delegation
;
849 struct nfs_server
*server
;
854 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
) {
855 list_for_each_entry_rcu(delegation
, &server
->delegations
,
857 if (test_bit(NFS_DELEGATION_RETURNING
,
860 if (test_bit(NFS_DELEGATION_NEED_RECLAIM
,
861 &delegation
->flags
) == 0)
863 if (!nfs_sb_active(server
->super
))
865 inode
= nfs_delegation_grab_inode(delegation
);
868 nfs_sb_deactive(server
->super
);
871 delegation
= nfs_start_delegation_return_locked(NFS_I(inode
));
873 if (delegation
!= NULL
) {
874 delegation
= nfs_detach_delegation(NFS_I(inode
),
876 if (delegation
!= NULL
)
877 nfs_free_delegation(delegation
);
880 nfs_sb_deactive(server
->super
);
887 static inline bool nfs4_server_rebooted(const struct nfs_client
*clp
)
889 return (clp
->cl_state
& (BIT(NFS4CLNT_CHECK_LEASE
) |
890 BIT(NFS4CLNT_LEASE_EXPIRED
) |
891 BIT(NFS4CLNT_SESSION_RESET
))) != 0;
894 static void nfs_mark_test_expired_delegation(struct nfs_server
*server
,
895 struct nfs_delegation
*delegation
)
897 if (delegation
->stateid
.type
== NFS4_INVALID_STATEID_TYPE
)
899 clear_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
);
900 set_bit(NFS_DELEGATION_TEST_EXPIRED
, &delegation
->flags
);
901 set_bit(NFS4CLNT_DELEGATION_EXPIRED
, &server
->nfs_client
->cl_state
);
904 static void nfs_inode_mark_test_expired_delegation(struct nfs_server
*server
,
907 struct nfs_delegation
*delegation
;
910 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
912 nfs_mark_test_expired_delegation(server
, delegation
);
917 static void nfs_delegation_mark_test_expired_server(struct nfs_server
*server
)
919 struct nfs_delegation
*delegation
;
921 list_for_each_entry_rcu(delegation
, &server
->delegations
, super_list
)
922 nfs_mark_test_expired_delegation(server
, delegation
);
926 * nfs_mark_test_expired_all_delegations - mark all delegations for testing
927 * @clp: nfs_client to process
929 * Iterates through all the delegations associated with this server and
930 * marks them as needing to be checked for validity.
932 void nfs_mark_test_expired_all_delegations(struct nfs_client
*clp
)
934 struct nfs_server
*server
;
937 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
)
938 nfs_delegation_mark_test_expired_server(server
);
943 * nfs_reap_expired_delegations - reap expired delegations
944 * @clp: nfs_client to process
946 * Iterates through all the delegations associated with this server and
947 * checks if they have may have been revoked. This function is usually
948 * expected to be called in cases where the server may have lost its
951 void nfs_reap_expired_delegations(struct nfs_client
*clp
)
953 const struct nfs4_minor_version_ops
*ops
= clp
->cl_mvops
;
954 struct nfs_delegation
*delegation
;
955 struct nfs_server
*server
;
957 struct rpc_cred
*cred
;
958 nfs4_stateid stateid
;
962 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
) {
963 list_for_each_entry_rcu(delegation
, &server
->delegations
,
965 if (test_bit(NFS_DELEGATION_RETURNING
,
968 if (test_bit(NFS_DELEGATION_TEST_EXPIRED
,
969 &delegation
->flags
) == 0)
971 if (!nfs_sb_active(server
->super
))
973 inode
= nfs_delegation_grab_inode(delegation
);
976 nfs_sb_deactive(server
->super
);
979 cred
= get_rpccred_rcu(delegation
->cred
);
980 nfs4_stateid_copy(&stateid
, &delegation
->stateid
);
981 clear_bit(NFS_DELEGATION_TEST_EXPIRED
, &delegation
->flags
);
984 ops
->test_and_free_expired(server
, &stateid
, cred
) < 0) {
985 nfs_revoke_delegation(inode
, &stateid
);
986 nfs_inode_find_state_and_recover(inode
, &stateid
);
989 if (nfs4_server_rebooted(clp
)) {
990 nfs_inode_mark_test_expired_delegation(server
,inode
);
992 nfs_sb_deactive(server
->super
);
996 nfs_sb_deactive(server
->super
);
1003 void nfs_inode_find_delegation_state_and_recover(struct inode
*inode
,
1004 const nfs4_stateid
*stateid
)
1006 struct nfs_client
*clp
= NFS_SERVER(inode
)->nfs_client
;
1007 struct nfs_delegation
*delegation
;
1011 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
1013 nfs4_stateid_match_other(&delegation
->stateid
, stateid
)) {
1014 nfs_mark_test_expired_delegation(NFS_SERVER(inode
), delegation
);
1019 nfs4_schedule_state_manager(clp
);
1023 * nfs_delegations_present - check for existence of delegations
1024 * @clp: client state handle
1026 * Returns one if there are any nfs_delegation structures attached
1027 * to this nfs_client.
1029 int nfs_delegations_present(struct nfs_client
*clp
)
1031 struct nfs_server
*server
;
1035 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
)
1036 if (!list_empty(&server
->delegations
)) {
1045 * nfs4_refresh_delegation_stateid - Update delegation stateid seqid
1046 * @dst: stateid to refresh
1047 * @inode: inode to check
1049 * Returns "true" and updates "dst->seqid" * if inode had a delegation
1050 * that matches our delegation stateid. Otherwise "false" is returned.
1052 bool nfs4_refresh_delegation_stateid(nfs4_stateid
*dst
, struct inode
*inode
)
1054 struct nfs_delegation
*delegation
;
1060 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
1061 if (delegation
!= NULL
&&
1062 nfs4_stateid_match_other(dst
, &delegation
->stateid
)) {
1063 dst
->seqid
= delegation
->stateid
.seqid
;
1072 * nfs4_copy_delegation_stateid - Copy inode's state ID information
1073 * @inode: inode to check
1074 * @flags: delegation type requirement
1075 * @dst: stateid data structure to fill in
1076 * @cred: optional argument to retrieve credential
1078 * Returns "true" and fills in "dst->data" * if inode had a delegation,
1079 * otherwise "false" is returned.
1081 bool nfs4_copy_delegation_stateid(struct inode
*inode
, fmode_t flags
,
1082 nfs4_stateid
*dst
, struct rpc_cred
**cred
)
1084 struct nfs_inode
*nfsi
= NFS_I(inode
);
1085 struct nfs_delegation
*delegation
;
1088 flags
&= FMODE_READ
|FMODE_WRITE
;
1090 delegation
= rcu_dereference(nfsi
->delegation
);
1091 ret
= nfs4_is_valid_delegation(delegation
, flags
);
1093 nfs4_stateid_copy(dst
, &delegation
->stateid
);
1094 nfs_mark_delegation_referenced(delegation
);
1096 *cred
= get_rpccred(delegation
->cred
);
1103 * nfs4_delegation_flush_on_close - Check if we must flush file on close
1104 * @inode: inode to check
1106 * This function checks the number of outstanding writes to the file
1107 * against the delegation 'space_limit' field to see if
1108 * the spec requires us to flush the file on close.
1110 bool nfs4_delegation_flush_on_close(const struct inode
*inode
)
1112 struct nfs_inode
*nfsi
= NFS_I(inode
);
1113 struct nfs_delegation
*delegation
;
1117 delegation
= rcu_dereference(nfsi
->delegation
);
1118 if (delegation
== NULL
|| !(delegation
->type
& FMODE_WRITE
))
1120 if (atomic_long_read(&nfsi
->nrequests
) < delegation
->pagemod_limit
)