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 if (delegation
->cred
) {
30 put_rpccred(delegation
->cred
);
31 delegation
->cred
= NULL
;
33 kfree_rcu(delegation
, rcu
);
37 * nfs_mark_delegation_referenced - set delegation's REFERENCED flag
38 * @delegation: delegation to process
41 void nfs_mark_delegation_referenced(struct nfs_delegation
*delegation
)
43 set_bit(NFS_DELEGATION_REFERENCED
, &delegation
->flags
);
47 nfs4_is_valid_delegation(const struct nfs_delegation
*delegation
,
50 if (delegation
!= NULL
&& (delegation
->type
& flags
) == flags
&&
51 !test_bit(NFS_DELEGATION_REVOKED
, &delegation
->flags
) &&
52 !test_bit(NFS_DELEGATION_RETURNING
, &delegation
->flags
))
58 nfs4_do_check_delegation(struct inode
*inode
, fmode_t flags
, bool mark
)
60 struct nfs_delegation
*delegation
;
63 flags
&= FMODE_READ
|FMODE_WRITE
;
65 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
66 if (nfs4_is_valid_delegation(delegation
, flags
)) {
68 nfs_mark_delegation_referenced(delegation
);
75 * nfs_have_delegation - check if inode has a delegation, mark it
76 * NFS_DELEGATION_REFERENCED if there is one.
77 * @inode: inode to check
78 * @flags: delegation types to check for
80 * Returns one if inode has the indicated delegation, otherwise zero.
82 int nfs4_have_delegation(struct inode
*inode
, fmode_t flags
)
84 return nfs4_do_check_delegation(inode
, flags
, true);
88 * nfs4_check_delegation - check if inode has a delegation, do not mark
89 * NFS_DELEGATION_REFERENCED if it has one.
91 int nfs4_check_delegation(struct inode
*inode
, fmode_t flags
)
93 return nfs4_do_check_delegation(inode
, flags
, false);
96 static int nfs_delegation_claim_locks(struct nfs_open_context
*ctx
, struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
98 struct inode
*inode
= state
->inode
;
100 struct file_lock_context
*flctx
= inode
->i_flctx
;
101 struct list_head
*list
;
107 list
= &flctx
->flc_posix
;
108 spin_lock(&flctx
->flc_lock
);
110 list_for_each_entry(fl
, list
, fl_list
) {
111 if (nfs_file_open_context(fl
->fl_file
) != ctx
)
113 spin_unlock(&flctx
->flc_lock
);
114 status
= nfs4_lock_delegation_recall(fl
, state
, stateid
);
117 spin_lock(&flctx
->flc_lock
);
119 if (list
== &flctx
->flc_posix
) {
120 list
= &flctx
->flc_flock
;
123 spin_unlock(&flctx
->flc_lock
);
128 static int nfs_delegation_claim_opens(struct inode
*inode
,
129 const nfs4_stateid
*stateid
, fmode_t type
)
131 struct nfs_inode
*nfsi
= NFS_I(inode
);
132 struct nfs_open_context
*ctx
;
133 struct nfs4_state_owner
*sp
;
134 struct nfs4_state
*state
;
139 spin_lock(&inode
->i_lock
);
140 list_for_each_entry(ctx
, &nfsi
->open_files
, list
) {
144 if (!test_bit(NFS_DELEGATED_STATE
, &state
->flags
))
146 if (!nfs4_valid_open_stateid(state
))
148 if (!nfs4_stateid_match(&state
->stateid
, stateid
))
150 get_nfs_open_context(ctx
);
151 spin_unlock(&inode
->i_lock
);
153 /* Block nfs4_proc_unlck */
154 mutex_lock(&sp
->so_delegreturn_mutex
);
155 seq
= raw_seqcount_begin(&sp
->so_reclaim_seqcount
);
156 err
= nfs4_open_delegation_recall(ctx
, state
, stateid
, type
);
158 err
= nfs_delegation_claim_locks(ctx
, state
, stateid
);
159 if (!err
&& read_seqcount_retry(&sp
->so_reclaim_seqcount
, seq
))
161 mutex_unlock(&sp
->so_delegreturn_mutex
);
162 put_nfs_open_context(ctx
);
167 spin_unlock(&inode
->i_lock
);
172 * nfs_inode_reclaim_delegation - process a delegation reclaim request
173 * @inode: inode to process
174 * @cred: credential to use for request
175 * @type: delegation type
176 * @stateid: delegation stateid
177 * @pagemod_limit: write delegation "space_limit"
180 void nfs_inode_reclaim_delegation(struct inode
*inode
, struct rpc_cred
*cred
,
182 const nfs4_stateid
*stateid
,
183 unsigned long pagemod_limit
)
185 struct nfs_delegation
*delegation
;
186 struct rpc_cred
*oldcred
= NULL
;
189 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
190 if (delegation
!= NULL
) {
191 spin_lock(&delegation
->lock
);
192 if (delegation
->inode
!= NULL
) {
193 nfs4_stateid_copy(&delegation
->stateid
, stateid
);
194 delegation
->type
= type
;
195 delegation
->pagemod_limit
= pagemod_limit
;
196 oldcred
= delegation
->cred
;
197 delegation
->cred
= get_rpccred(cred
);
198 clear_bit(NFS_DELEGATION_NEED_RECLAIM
,
200 spin_unlock(&delegation
->lock
);
202 put_rpccred(oldcred
);
203 trace_nfs4_reclaim_delegation(inode
, type
);
206 /* We appear to have raced with a delegation return. */
207 spin_unlock(&delegation
->lock
);
210 nfs_inode_set_delegation(inode
, cred
, type
, stateid
, pagemod_limit
);
213 static int nfs_do_return_delegation(struct inode
*inode
, struct nfs_delegation
*delegation
, int issync
)
217 if (!test_bit(NFS_DELEGATION_REVOKED
, &delegation
->flags
))
218 res
= nfs4_proc_delegreturn(inode
,
220 &delegation
->stateid
,
222 nfs_free_delegation(delegation
);
226 static struct inode
*nfs_delegation_grab_inode(struct nfs_delegation
*delegation
)
228 struct inode
*inode
= NULL
;
230 spin_lock(&delegation
->lock
);
231 if (delegation
->inode
!= NULL
)
232 inode
= igrab(delegation
->inode
);
233 spin_unlock(&delegation
->lock
);
237 static struct nfs_delegation
*
238 nfs_start_delegation_return_locked(struct nfs_inode
*nfsi
)
240 struct nfs_delegation
*ret
= NULL
;
241 struct nfs_delegation
*delegation
= rcu_dereference(nfsi
->delegation
);
243 if (delegation
== NULL
)
245 spin_lock(&delegation
->lock
);
246 if (!test_and_set_bit(NFS_DELEGATION_RETURNING
, &delegation
->flags
))
248 spin_unlock(&delegation
->lock
);
253 static struct nfs_delegation
*
254 nfs_start_delegation_return(struct nfs_inode
*nfsi
)
256 struct nfs_delegation
*delegation
;
259 delegation
= nfs_start_delegation_return_locked(nfsi
);
265 nfs_abort_delegation_return(struct nfs_delegation
*delegation
,
266 struct nfs_client
*clp
)
269 spin_lock(&delegation
->lock
);
270 clear_bit(NFS_DELEGATION_RETURNING
, &delegation
->flags
);
271 set_bit(NFS_DELEGATION_RETURN
, &delegation
->flags
);
272 spin_unlock(&delegation
->lock
);
273 set_bit(NFS4CLNT_DELEGRETURN
, &clp
->cl_state
);
276 static struct nfs_delegation
*
277 nfs_detach_delegation_locked(struct nfs_inode
*nfsi
,
278 struct nfs_delegation
*delegation
,
279 struct nfs_client
*clp
)
281 struct nfs_delegation
*deleg_cur
=
282 rcu_dereference_protected(nfsi
->delegation
,
283 lockdep_is_held(&clp
->cl_lock
));
285 if (deleg_cur
== NULL
|| delegation
!= deleg_cur
)
288 spin_lock(&delegation
->lock
);
289 set_bit(NFS_DELEGATION_RETURNING
, &delegation
->flags
);
290 list_del_rcu(&delegation
->super_list
);
291 delegation
->inode
= NULL
;
292 rcu_assign_pointer(nfsi
->delegation
, NULL
);
293 spin_unlock(&delegation
->lock
);
297 static struct nfs_delegation
*nfs_detach_delegation(struct nfs_inode
*nfsi
,
298 struct nfs_delegation
*delegation
,
299 struct nfs_server
*server
)
301 struct nfs_client
*clp
= server
->nfs_client
;
303 spin_lock(&clp
->cl_lock
);
304 delegation
= nfs_detach_delegation_locked(nfsi
, delegation
, clp
);
305 spin_unlock(&clp
->cl_lock
);
309 static struct nfs_delegation
*
310 nfs_inode_detach_delegation(struct inode
*inode
)
312 struct nfs_inode
*nfsi
= NFS_I(inode
);
313 struct nfs_server
*server
= NFS_SERVER(inode
);
314 struct nfs_delegation
*delegation
;
316 delegation
= nfs_start_delegation_return(nfsi
);
317 if (delegation
== NULL
)
319 return nfs_detach_delegation(nfsi
, delegation
, server
);
323 nfs_update_inplace_delegation(struct nfs_delegation
*delegation
,
324 const struct nfs_delegation
*update
)
326 if (nfs4_stateid_is_newer(&update
->stateid
, &delegation
->stateid
)) {
327 delegation
->stateid
.seqid
= update
->stateid
.seqid
;
329 delegation
->type
= update
->type
;
334 * nfs_inode_set_delegation - set up a delegation on an inode
335 * @inode: inode to which delegation applies
336 * @cred: cred to use for subsequent delegation processing
337 * @type: delegation type
338 * @stateid: delegation stateid
339 * @pagemod_limit: write delegation "space_limit"
341 * Returns zero on success, or a negative errno value.
343 int nfs_inode_set_delegation(struct inode
*inode
, struct rpc_cred
*cred
,
345 const nfs4_stateid
*stateid
,
346 unsigned long pagemod_limit
)
348 struct nfs_server
*server
= NFS_SERVER(inode
);
349 struct nfs_client
*clp
= server
->nfs_client
;
350 struct nfs_inode
*nfsi
= NFS_I(inode
);
351 struct nfs_delegation
*delegation
, *old_delegation
;
352 struct nfs_delegation
*freeme
= NULL
;
355 delegation
= kmalloc(sizeof(*delegation
), GFP_NOFS
);
356 if (delegation
== NULL
)
358 nfs4_stateid_copy(&delegation
->stateid
, stateid
);
359 delegation
->type
= type
;
360 delegation
->pagemod_limit
= pagemod_limit
;
361 delegation
->change_attr
= inode_peek_iversion_raw(inode
);
362 delegation
->cred
= get_rpccred(cred
);
363 delegation
->inode
= inode
;
364 delegation
->flags
= 1<<NFS_DELEGATION_REFERENCED
;
365 spin_lock_init(&delegation
->lock
);
367 spin_lock(&clp
->cl_lock
);
368 old_delegation
= rcu_dereference_protected(nfsi
->delegation
,
369 lockdep_is_held(&clp
->cl_lock
));
370 if (old_delegation
!= NULL
) {
371 /* Is this an update of the existing delegation? */
372 if (nfs4_stateid_match_other(&old_delegation
->stateid
,
373 &delegation
->stateid
)) {
374 nfs_update_inplace_delegation(old_delegation
,
379 * Deal with broken servers that hand out two
380 * delegations for the same file.
381 * Allow for upgrades to a WRITE delegation, but
384 dfprintk(FILE, "%s: server %s handed out "
385 "a duplicate delegation!\n",
386 __func__
, clp
->cl_hostname
);
387 if (delegation
->type
== old_delegation
->type
||
388 !(delegation
->type
& FMODE_WRITE
)) {
393 if (test_and_set_bit(NFS_DELEGATION_RETURNING
,
394 &old_delegation
->flags
))
396 freeme
= nfs_detach_delegation_locked(nfsi
,
397 old_delegation
, clp
);
401 list_add_tail_rcu(&delegation
->super_list
, &server
->delegations
);
402 rcu_assign_pointer(nfsi
->delegation
, delegation
);
405 trace_nfs4_set_delegation(inode
, type
);
407 spin_lock(&inode
->i_lock
);
408 if (NFS_I(inode
)->cache_validity
& (NFS_INO_INVALID_ATTR
|NFS_INO_INVALID_ATIME
))
409 NFS_I(inode
)->cache_validity
|= NFS_INO_REVAL_FORCED
;
410 spin_unlock(&inode
->i_lock
);
412 spin_unlock(&clp
->cl_lock
);
413 if (delegation
!= NULL
)
414 nfs_free_delegation(delegation
);
416 nfs_do_return_delegation(inode
, freeme
, 0);
421 * Basic procedure for returning a delegation to the server
423 static int nfs_end_delegation_return(struct inode
*inode
, struct nfs_delegation
*delegation
, int issync
)
425 struct nfs_client
*clp
= NFS_SERVER(inode
)->nfs_client
;
426 struct nfs_inode
*nfsi
= NFS_I(inode
);
429 if (delegation
== NULL
)
432 if (test_bit(NFS_DELEGATION_REVOKED
, &delegation
->flags
))
434 err
= nfs_delegation_claim_opens(inode
, &delegation
->stateid
,
436 if (!issync
|| err
!= -EAGAIN
)
439 * Guard against state recovery
441 err
= nfs4_wait_clnt_recover(clp
);
445 nfs_abort_delegation_return(delegation
, clp
);
448 if (!nfs_detach_delegation(nfsi
, delegation
, NFS_SERVER(inode
)))
451 err
= nfs_do_return_delegation(inode
, delegation
, issync
);
456 static bool nfs_delegation_need_return(struct nfs_delegation
*delegation
)
460 if (test_bit(NFS_DELEGATION_RETURNING
, &delegation
->flags
))
462 if (test_and_clear_bit(NFS_DELEGATION_RETURN
, &delegation
->flags
))
464 if (test_and_clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED
, &delegation
->flags
) && !ret
) {
467 spin_lock(&delegation
->lock
);
468 inode
= delegation
->inode
;
469 if (inode
&& list_empty(&NFS_I(inode
)->open_files
))
471 spin_unlock(&delegation
->lock
);
478 * nfs_client_return_marked_delegations - return previously marked delegations
479 * @clp: nfs_client to process
481 * Note that this function is designed to be called by the state
482 * manager thread. For this reason, it cannot flush the dirty data,
483 * since that could deadlock in case of a state recovery error.
485 * Returns zero on success, or a negative errno value.
487 int nfs_client_return_marked_delegations(struct nfs_client
*clp
)
489 struct nfs_delegation
*delegation
;
490 struct nfs_delegation
*prev
;
491 struct nfs_server
*server
;
493 struct inode
*place_holder
= NULL
;
494 struct nfs_delegation
*place_holder_deleg
= NULL
;
499 * To avoid quadratic looping we hold a reference
500 * to an inode place_holder. Each time we restart, we
501 * list nfs_servers from the server of that inode, and
502 * delegation in the server from the delegations of that
504 * prev is an RCU-protected pointer to a delegation which
505 * wasn't marked for return and might be a good choice for
506 * the next place_holder.
511 server
= NFS_SERVER(place_holder
);
513 server
= list_entry_rcu(clp
->cl_superblocks
.next
,
514 struct nfs_server
, client_link
);
515 list_for_each_entry_from_rcu(server
, &clp
->cl_superblocks
, client_link
) {
517 if (place_holder
&& server
== NFS_SERVER(place_holder
))
518 delegation
= rcu_dereference(NFS_I(place_holder
)->delegation
);
519 if (!delegation
|| delegation
!= place_holder_deleg
)
520 delegation
= list_entry_rcu(server
->delegations
.next
,
521 struct nfs_delegation
, super_list
);
522 list_for_each_entry_from_rcu(delegation
, &server
->delegations
, super_list
) {
523 struct inode
*to_put
= NULL
;
525 if (!nfs_delegation_need_return(delegation
)) {
529 if (!nfs_sb_active(server
->super
))
530 break; /* continue in outer loop */
535 tmp
= nfs_delegation_grab_inode(prev
);
537 to_put
= place_holder
;
539 place_holder_deleg
= prev
;
543 inode
= nfs_delegation_grab_inode(delegation
);
548 nfs_sb_deactive(server
->super
);
551 delegation
= nfs_start_delegation_return_locked(NFS_I(inode
));
557 err
= nfs_end_delegation_return(inode
, delegation
, 0);
559 nfs_sb_deactive(server
->super
);
563 set_bit(NFS4CLNT_DELEGRETURN
, &clp
->cl_state
);
576 * nfs_inode_return_delegation_noreclaim - return delegation, don't reclaim opens
577 * @inode: inode to process
579 * Does not protect against delegation reclaims, therefore really only safe
580 * to be called from nfs4_clear_inode().
582 void nfs_inode_return_delegation_noreclaim(struct inode
*inode
)
584 struct nfs_delegation
*delegation
;
586 delegation
= nfs_inode_detach_delegation(inode
);
587 if (delegation
!= NULL
)
588 nfs_do_return_delegation(inode
, delegation
, 1);
592 * nfs_inode_return_delegation - synchronously return a delegation
593 * @inode: inode to process
595 * This routine will always flush any dirty data to disk on the
596 * assumption that if we need to return the delegation, then
597 * we should stop caching.
599 * Returns zero on success, or a negative errno value.
601 int nfs4_inode_return_delegation(struct inode
*inode
)
603 struct nfs_inode
*nfsi
= NFS_I(inode
);
604 struct nfs_delegation
*delegation
;
608 delegation
= nfs_start_delegation_return(nfsi
);
609 if (delegation
!= NULL
)
610 err
= nfs_end_delegation_return(inode
, delegation
, 1);
615 * nfs4_inode_make_writeable
616 * @inode: pointer to inode
618 * Make the inode writeable by returning the delegation if necessary
620 * Returns zero on success, or a negative errno value.
622 int nfs4_inode_make_writeable(struct inode
*inode
)
624 if (!nfs4_has_session(NFS_SERVER(inode
)->nfs_client
) ||
625 !nfs4_check_delegation(inode
, FMODE_WRITE
))
626 return nfs4_inode_return_delegation(inode
);
630 static void nfs_mark_return_if_closed_delegation(struct nfs_server
*server
,
631 struct nfs_delegation
*delegation
)
633 set_bit(NFS_DELEGATION_RETURN_IF_CLOSED
, &delegation
->flags
);
634 set_bit(NFS4CLNT_DELEGRETURN
, &server
->nfs_client
->cl_state
);
637 static void nfs_mark_return_delegation(struct nfs_server
*server
,
638 struct nfs_delegation
*delegation
)
640 set_bit(NFS_DELEGATION_RETURN
, &delegation
->flags
);
641 set_bit(NFS4CLNT_DELEGRETURN
, &server
->nfs_client
->cl_state
);
644 static bool nfs_server_mark_return_all_delegations(struct nfs_server
*server
)
646 struct nfs_delegation
*delegation
;
649 list_for_each_entry_rcu(delegation
, &server
->delegations
, super_list
) {
650 nfs_mark_return_delegation(server
, delegation
);
656 static void nfs_client_mark_return_all_delegations(struct nfs_client
*clp
)
658 struct nfs_server
*server
;
661 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
)
662 nfs_server_mark_return_all_delegations(server
);
666 static void nfs_delegation_run_state_manager(struct nfs_client
*clp
)
668 if (test_bit(NFS4CLNT_DELEGRETURN
, &clp
->cl_state
))
669 nfs4_schedule_state_manager(clp
);
673 * nfs_expire_all_delegations
674 * @clp: client to process
677 void nfs_expire_all_delegations(struct nfs_client
*clp
)
679 nfs_client_mark_return_all_delegations(clp
);
680 nfs_delegation_run_state_manager(clp
);
684 * nfs_super_return_all_delegations - return delegations for one superblock
688 void nfs_server_return_all_delegations(struct nfs_server
*server
)
690 struct nfs_client
*clp
= server
->nfs_client
;
697 need_wait
= nfs_server_mark_return_all_delegations(server
);
701 nfs4_schedule_state_manager(clp
);
702 nfs4_wait_clnt_recover(clp
);
706 static void nfs_mark_return_unused_delegation_types(struct nfs_server
*server
,
709 struct nfs_delegation
*delegation
;
711 list_for_each_entry_rcu(delegation
, &server
->delegations
, super_list
) {
712 if ((delegation
->type
== (FMODE_READ
|FMODE_WRITE
)) && !(flags
& FMODE_WRITE
))
714 if (delegation
->type
& flags
)
715 nfs_mark_return_if_closed_delegation(server
, delegation
);
719 static void nfs_client_mark_return_unused_delegation_types(struct nfs_client
*clp
,
722 struct nfs_server
*server
;
725 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
)
726 nfs_mark_return_unused_delegation_types(server
, flags
);
730 static void nfs_mark_delegation_revoked(struct nfs_server
*server
,
731 struct nfs_delegation
*delegation
)
733 set_bit(NFS_DELEGATION_REVOKED
, &delegation
->flags
);
734 delegation
->stateid
.type
= NFS4_INVALID_STATEID_TYPE
;
735 nfs_mark_return_delegation(server
, delegation
);
738 static bool nfs_revoke_delegation(struct inode
*inode
,
739 const nfs4_stateid
*stateid
)
741 struct nfs_delegation
*delegation
;
746 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
747 if (delegation
== NULL
)
749 if (stateid
== NULL
) {
750 nfs4_stateid_copy(&tmp
, &delegation
->stateid
);
752 } else if (!nfs4_stateid_match(stateid
, &delegation
->stateid
))
754 nfs_mark_delegation_revoked(NFS_SERVER(inode
), delegation
);
759 nfs_inode_find_state_and_recover(inode
, stateid
);
763 void nfs_remove_bad_delegation(struct inode
*inode
,
764 const nfs4_stateid
*stateid
)
766 struct nfs_delegation
*delegation
;
768 if (!nfs_revoke_delegation(inode
, stateid
))
770 delegation
= nfs_inode_detach_delegation(inode
);
772 nfs_free_delegation(delegation
);
774 EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation
);
777 * nfs_expire_unused_delegation_types
778 * @clp: client to process
779 * @flags: delegation types to expire
782 void nfs_expire_unused_delegation_types(struct nfs_client
*clp
, fmode_t flags
)
784 nfs_client_mark_return_unused_delegation_types(clp
, flags
);
785 nfs_delegation_run_state_manager(clp
);
788 static void nfs_mark_return_unreferenced_delegations(struct nfs_server
*server
)
790 struct nfs_delegation
*delegation
;
792 list_for_each_entry_rcu(delegation
, &server
->delegations
, super_list
) {
793 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED
, &delegation
->flags
))
795 nfs_mark_return_if_closed_delegation(server
, delegation
);
800 * nfs_expire_unreferenced_delegations - Eliminate unused delegations
801 * @clp: nfs_client to process
804 void nfs_expire_unreferenced_delegations(struct nfs_client
*clp
)
806 struct nfs_server
*server
;
809 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
)
810 nfs_mark_return_unreferenced_delegations(server
);
813 nfs_delegation_run_state_manager(clp
);
817 * nfs_async_inode_return_delegation - asynchronously return a delegation
818 * @inode: inode to process
819 * @stateid: state ID information
821 * Returns zero on success, or a negative errno value.
823 int nfs_async_inode_return_delegation(struct inode
*inode
,
824 const nfs4_stateid
*stateid
)
826 struct nfs_server
*server
= NFS_SERVER(inode
);
827 struct nfs_client
*clp
= server
->nfs_client
;
828 struct nfs_delegation
*delegation
;
831 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
832 if (delegation
== NULL
)
834 if (stateid
!= NULL
&&
835 !clp
->cl_mvops
->match_stateid(&delegation
->stateid
, stateid
))
837 nfs_mark_return_delegation(server
, delegation
);
840 nfs_delegation_run_state_manager(clp
);
847 static struct inode
*
848 nfs_delegation_find_inode_server(struct nfs_server
*server
,
849 const struct nfs_fh
*fhandle
)
851 struct nfs_delegation
*delegation
;
852 struct inode
*res
= NULL
;
854 list_for_each_entry_rcu(delegation
, &server
->delegations
, super_list
) {
855 spin_lock(&delegation
->lock
);
856 if (delegation
->inode
!= NULL
&&
857 nfs_compare_fh(fhandle
, &NFS_I(delegation
->inode
)->fh
) == 0) {
858 res
= igrab(delegation
->inode
);
859 spin_unlock(&delegation
->lock
);
862 return ERR_PTR(-EAGAIN
);
864 spin_unlock(&delegation
->lock
);
866 return ERR_PTR(-ENOENT
);
870 * nfs_delegation_find_inode - retrieve the inode associated with a delegation
871 * @clp: client state handle
872 * @fhandle: filehandle from a delegation recall
874 * Returns pointer to inode matching "fhandle," or NULL if a matching inode
877 struct inode
*nfs_delegation_find_inode(struct nfs_client
*clp
,
878 const struct nfs_fh
*fhandle
)
880 struct nfs_server
*server
;
884 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
) {
885 res
= nfs_delegation_find_inode_server(server
, fhandle
);
886 if (res
!= ERR_PTR(-ENOENT
))
890 return ERR_PTR(-ENOENT
);
893 static void nfs_delegation_mark_reclaim_server(struct nfs_server
*server
)
895 struct nfs_delegation
*delegation
;
897 list_for_each_entry_rcu(delegation
, &server
->delegations
, super_list
) {
899 * If the delegation may have been admin revoked, then we
902 if (test_bit(NFS_DELEGATION_TEST_EXPIRED
, &delegation
->flags
))
904 set_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
);
909 * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
910 * @clp: nfs_client to process
913 void nfs_delegation_mark_reclaim(struct nfs_client
*clp
)
915 struct nfs_server
*server
;
918 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
)
919 nfs_delegation_mark_reclaim_server(server
);
924 * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
925 * @clp: nfs_client to process
928 void nfs_delegation_reap_unclaimed(struct nfs_client
*clp
)
930 struct nfs_delegation
*delegation
;
931 struct nfs_server
*server
;
936 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
) {
937 list_for_each_entry_rcu(delegation
, &server
->delegations
,
939 if (test_bit(NFS_DELEGATION_RETURNING
,
942 if (test_bit(NFS_DELEGATION_NEED_RECLAIM
,
943 &delegation
->flags
) == 0)
945 if (!nfs_sb_active(server
->super
))
946 break; /* continue in outer loop */
947 inode
= nfs_delegation_grab_inode(delegation
);
950 nfs_sb_deactive(server
->super
);
953 delegation
= nfs_start_delegation_return_locked(NFS_I(inode
));
955 if (delegation
!= NULL
) {
956 delegation
= nfs_detach_delegation(NFS_I(inode
),
958 if (delegation
!= NULL
)
959 nfs_free_delegation(delegation
);
962 nfs_sb_deactive(server
->super
);
970 static inline bool nfs4_server_rebooted(const struct nfs_client
*clp
)
972 return (clp
->cl_state
& (BIT(NFS4CLNT_CHECK_LEASE
) |
973 BIT(NFS4CLNT_LEASE_EXPIRED
) |
974 BIT(NFS4CLNT_SESSION_RESET
))) != 0;
977 static void nfs_mark_test_expired_delegation(struct nfs_server
*server
,
978 struct nfs_delegation
*delegation
)
980 if (delegation
->stateid
.type
== NFS4_INVALID_STATEID_TYPE
)
982 clear_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
);
983 set_bit(NFS_DELEGATION_TEST_EXPIRED
, &delegation
->flags
);
984 set_bit(NFS4CLNT_DELEGATION_EXPIRED
, &server
->nfs_client
->cl_state
);
987 static void nfs_inode_mark_test_expired_delegation(struct nfs_server
*server
,
990 struct nfs_delegation
*delegation
;
993 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
995 nfs_mark_test_expired_delegation(server
, delegation
);
1000 static void nfs_delegation_mark_test_expired_server(struct nfs_server
*server
)
1002 struct nfs_delegation
*delegation
;
1004 list_for_each_entry_rcu(delegation
, &server
->delegations
, super_list
)
1005 nfs_mark_test_expired_delegation(server
, delegation
);
1009 * nfs_mark_test_expired_all_delegations - mark all delegations for testing
1010 * @clp: nfs_client to process
1012 * Iterates through all the delegations associated with this server and
1013 * marks them as needing to be checked for validity.
1015 void nfs_mark_test_expired_all_delegations(struct nfs_client
*clp
)
1017 struct nfs_server
*server
;
1020 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
)
1021 nfs_delegation_mark_test_expired_server(server
);
1026 * nfs_reap_expired_delegations - reap expired delegations
1027 * @clp: nfs_client to process
1029 * Iterates through all the delegations associated with this server and
1030 * checks if they have may have been revoked. This function is usually
1031 * expected to be called in cases where the server may have lost its
1034 void nfs_reap_expired_delegations(struct nfs_client
*clp
)
1036 const struct nfs4_minor_version_ops
*ops
= clp
->cl_mvops
;
1037 struct nfs_delegation
*delegation
;
1038 struct nfs_server
*server
;
1039 struct inode
*inode
;
1040 struct rpc_cred
*cred
;
1041 nfs4_stateid stateid
;
1045 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
) {
1046 list_for_each_entry_rcu(delegation
, &server
->delegations
,
1048 if (test_bit(NFS_DELEGATION_RETURNING
,
1049 &delegation
->flags
))
1051 if (test_bit(NFS_DELEGATION_TEST_EXPIRED
,
1052 &delegation
->flags
) == 0)
1054 if (!nfs_sb_active(server
->super
))
1055 break; /* continue in outer loop */
1056 inode
= nfs_delegation_grab_inode(delegation
);
1057 if (inode
== NULL
) {
1059 nfs_sb_deactive(server
->super
);
1062 cred
= get_rpccred_rcu(delegation
->cred
);
1063 nfs4_stateid_copy(&stateid
, &delegation
->stateid
);
1064 clear_bit(NFS_DELEGATION_TEST_EXPIRED
, &delegation
->flags
);
1067 ops
->test_and_free_expired(server
, &stateid
, cred
) < 0) {
1068 nfs_revoke_delegation(inode
, &stateid
);
1069 nfs_inode_find_state_and_recover(inode
, &stateid
);
1072 if (nfs4_server_rebooted(clp
)) {
1073 nfs_inode_mark_test_expired_delegation(server
,inode
);
1075 nfs_sb_deactive(server
->super
);
1079 nfs_sb_deactive(server
->super
);
1087 void nfs_inode_find_delegation_state_and_recover(struct inode
*inode
,
1088 const nfs4_stateid
*stateid
)
1090 struct nfs_client
*clp
= NFS_SERVER(inode
)->nfs_client
;
1091 struct nfs_delegation
*delegation
;
1095 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
1097 nfs4_stateid_match_other(&delegation
->stateid
, stateid
)) {
1098 nfs_mark_test_expired_delegation(NFS_SERVER(inode
), delegation
);
1103 nfs4_schedule_state_manager(clp
);
1107 * nfs_delegations_present - check for existence of delegations
1108 * @clp: client state handle
1110 * Returns one if there are any nfs_delegation structures attached
1111 * to this nfs_client.
1113 int nfs_delegations_present(struct nfs_client
*clp
)
1115 struct nfs_server
*server
;
1119 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
)
1120 if (!list_empty(&server
->delegations
)) {
1129 * nfs4_refresh_delegation_stateid - Update delegation stateid seqid
1130 * @dst: stateid to refresh
1131 * @inode: inode to check
1133 * Returns "true" and updates "dst->seqid" * if inode had a delegation
1134 * that matches our delegation stateid. Otherwise "false" is returned.
1136 bool nfs4_refresh_delegation_stateid(nfs4_stateid
*dst
, struct inode
*inode
)
1138 struct nfs_delegation
*delegation
;
1144 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
1145 if (delegation
!= NULL
&&
1146 nfs4_stateid_match_other(dst
, &delegation
->stateid
)) {
1147 dst
->seqid
= delegation
->stateid
.seqid
;
1156 * nfs4_copy_delegation_stateid - Copy inode's state ID information
1157 * @inode: inode to check
1158 * @flags: delegation type requirement
1159 * @dst: stateid data structure to fill in
1160 * @cred: optional argument to retrieve credential
1162 * Returns "true" and fills in "dst->data" * if inode had a delegation,
1163 * otherwise "false" is returned.
1165 bool nfs4_copy_delegation_stateid(struct inode
*inode
, fmode_t flags
,
1166 nfs4_stateid
*dst
, struct rpc_cred
**cred
)
1168 struct nfs_inode
*nfsi
= NFS_I(inode
);
1169 struct nfs_delegation
*delegation
;
1172 flags
&= FMODE_READ
|FMODE_WRITE
;
1174 delegation
= rcu_dereference(nfsi
->delegation
);
1175 ret
= nfs4_is_valid_delegation(delegation
, flags
);
1177 nfs4_stateid_copy(dst
, &delegation
->stateid
);
1178 nfs_mark_delegation_referenced(delegation
);
1180 *cred
= get_rpccred(delegation
->cred
);
1187 * nfs4_delegation_flush_on_close - Check if we must flush file on close
1188 * @inode: inode to check
1190 * This function checks the number of outstanding writes to the file
1191 * against the delegation 'space_limit' field to see if
1192 * the spec requires us to flush the file on close.
1194 bool nfs4_delegation_flush_on_close(const struct inode
*inode
)
1196 struct nfs_inode
*nfsi
= NFS_I(inode
);
1197 struct nfs_delegation
*delegation
;
1201 delegation
= rcu_dereference(nfsi
->delegation
);
1202 if (delegation
== NULL
|| !(delegation
->type
& FMODE_WRITE
))
1204 if (atomic_long_read(&nfsi
->nrequests
) < delegation
->pagemod_limit
)