1 // SPDX-License-Identifier: GPL-2.0-only
3 * linux/fs/nfs/delegation.c
5 * Copyright (C) 2004 Trond Myklebust
7 * NFS file delegation management
10 #include <linux/completion.h>
11 #include <linux/kthread.h>
12 #include <linux/module.h>
13 #include <linux/sched.h>
14 #include <linux/slab.h>
15 #include <linux/spinlock.h>
16 #include <linux/iversion.h>
18 #include <linux/nfs4.h>
19 #include <linux/nfs_fs.h>
20 #include <linux/nfs_xdr.h>
23 #include "nfs4session.h"
24 #include "delegation.h"
26 #include "nfs4trace.h"
28 #define NFS_DEFAULT_DELEGATION_WATERMARK (5000U)
30 static atomic_long_t nfs_active_delegations
;
31 static unsigned nfs_delegation_watermark
= NFS_DEFAULT_DELEGATION_WATERMARK
;
33 static void __nfs_free_delegation(struct nfs_delegation
*delegation
)
35 put_cred(delegation
->cred
);
36 delegation
->cred
= NULL
;
37 kfree_rcu(delegation
, rcu
);
40 static void nfs_mark_delegation_revoked(struct nfs_delegation
*delegation
)
42 if (!test_and_set_bit(NFS_DELEGATION_REVOKED
, &delegation
->flags
)) {
43 delegation
->stateid
.type
= NFS4_INVALID_STATEID_TYPE
;
44 atomic_long_dec(&nfs_active_delegations
);
48 static void nfs_free_delegation(struct nfs_delegation
*delegation
)
50 nfs_mark_delegation_revoked(delegation
);
51 __nfs_free_delegation(delegation
);
55 * nfs_mark_delegation_referenced - set delegation's REFERENCED flag
56 * @delegation: delegation to process
59 void nfs_mark_delegation_referenced(struct nfs_delegation
*delegation
)
61 set_bit(NFS_DELEGATION_REFERENCED
, &delegation
->flags
);
65 nfs4_is_valid_delegation(const struct nfs_delegation
*delegation
,
68 if (delegation
!= NULL
&& (delegation
->type
& flags
) == flags
&&
69 !test_bit(NFS_DELEGATION_REVOKED
, &delegation
->flags
) &&
70 !test_bit(NFS_DELEGATION_RETURNING
, &delegation
->flags
))
75 struct nfs_delegation
*nfs4_get_valid_delegation(const struct inode
*inode
)
77 struct nfs_delegation
*delegation
;
79 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
80 if (nfs4_is_valid_delegation(delegation
, 0))
86 nfs4_do_check_delegation(struct inode
*inode
, fmode_t flags
, bool mark
)
88 struct nfs_delegation
*delegation
;
91 flags
&= FMODE_READ
|FMODE_WRITE
;
93 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
94 if (nfs4_is_valid_delegation(delegation
, flags
)) {
96 nfs_mark_delegation_referenced(delegation
);
103 * nfs_have_delegation - check if inode has a delegation, mark it
104 * NFS_DELEGATION_REFERENCED if there is one.
105 * @inode: inode to check
106 * @flags: delegation types to check for
108 * Returns one if inode has the indicated delegation, otherwise zero.
110 int nfs4_have_delegation(struct inode
*inode
, fmode_t flags
)
112 return nfs4_do_check_delegation(inode
, flags
, true);
116 * nfs4_check_delegation - check if inode has a delegation, do not mark
117 * NFS_DELEGATION_REFERENCED if it has one.
119 int nfs4_check_delegation(struct inode
*inode
, fmode_t flags
)
121 return nfs4_do_check_delegation(inode
, flags
, false);
124 static int nfs_delegation_claim_locks(struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
126 struct inode
*inode
= state
->inode
;
127 struct file_lock
*fl
;
128 struct file_lock_context
*flctx
= inode
->i_flctx
;
129 struct list_head
*list
;
135 list
= &flctx
->flc_posix
;
136 spin_lock(&flctx
->flc_lock
);
138 list_for_each_entry(fl
, list
, fl_list
) {
139 if (nfs_file_open_context(fl
->fl_file
)->state
!= state
)
141 spin_unlock(&flctx
->flc_lock
);
142 status
= nfs4_lock_delegation_recall(fl
, state
, stateid
);
145 spin_lock(&flctx
->flc_lock
);
147 if (list
== &flctx
->flc_posix
) {
148 list
= &flctx
->flc_flock
;
151 spin_unlock(&flctx
->flc_lock
);
156 static int nfs_delegation_claim_opens(struct inode
*inode
,
157 const nfs4_stateid
*stateid
, fmode_t type
)
159 struct nfs_inode
*nfsi
= NFS_I(inode
);
160 struct nfs_open_context
*ctx
;
161 struct nfs4_state_owner
*sp
;
162 struct nfs4_state
*state
;
168 list_for_each_entry_rcu(ctx
, &nfsi
->open_files
, list
) {
172 if (!test_bit(NFS_DELEGATED_STATE
, &state
->flags
))
174 if (!nfs4_valid_open_stateid(state
))
176 if (!nfs4_stateid_match(&state
->stateid
, stateid
))
178 if (!get_nfs_open_context(ctx
))
182 /* Block nfs4_proc_unlck */
183 mutex_lock(&sp
->so_delegreturn_mutex
);
184 seq
= raw_seqcount_begin(&sp
->so_reclaim_seqcount
);
185 err
= nfs4_open_delegation_recall(ctx
, state
, stateid
);
187 err
= nfs_delegation_claim_locks(state
, stateid
);
188 if (!err
&& read_seqcount_retry(&sp
->so_reclaim_seqcount
, seq
))
190 mutex_unlock(&sp
->so_delegreturn_mutex
);
191 put_nfs_open_context(ctx
);
201 * nfs_inode_reclaim_delegation - process a delegation reclaim request
202 * @inode: inode to process
203 * @cred: credential to use for request
204 * @type: delegation type
205 * @stateid: delegation stateid
206 * @pagemod_limit: write delegation "space_limit"
209 void nfs_inode_reclaim_delegation(struct inode
*inode
, const struct cred
*cred
,
211 const nfs4_stateid
*stateid
,
212 unsigned long pagemod_limit
)
214 struct nfs_delegation
*delegation
;
215 const struct cred
*oldcred
= NULL
;
218 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
219 if (delegation
!= NULL
) {
220 spin_lock(&delegation
->lock
);
221 if (nfs4_is_valid_delegation(delegation
, 0)) {
222 nfs4_stateid_copy(&delegation
->stateid
, stateid
);
223 delegation
->type
= type
;
224 delegation
->pagemod_limit
= pagemod_limit
;
225 oldcred
= delegation
->cred
;
226 delegation
->cred
= get_cred(cred
);
227 clear_bit(NFS_DELEGATION_NEED_RECLAIM
,
229 spin_unlock(&delegation
->lock
);
232 trace_nfs4_reclaim_delegation(inode
, type
);
235 /* We appear to have raced with a delegation return. */
236 spin_unlock(&delegation
->lock
);
239 nfs_inode_set_delegation(inode
, cred
, type
, stateid
, pagemod_limit
);
242 static int nfs_do_return_delegation(struct inode
*inode
, struct nfs_delegation
*delegation
, int issync
)
246 if (!test_bit(NFS_DELEGATION_REVOKED
, &delegation
->flags
))
247 res
= nfs4_proc_delegreturn(inode
,
249 &delegation
->stateid
,
254 static struct inode
*nfs_delegation_grab_inode(struct nfs_delegation
*delegation
)
256 struct inode
*inode
= NULL
;
258 spin_lock(&delegation
->lock
);
259 if (delegation
->inode
!= NULL
)
260 inode
= igrab(delegation
->inode
);
262 set_bit(NFS_DELEGATION_INODE_FREEING
, &delegation
->flags
);
263 spin_unlock(&delegation
->lock
);
267 static struct nfs_delegation
*
268 nfs_start_delegation_return_locked(struct nfs_inode
*nfsi
)
270 struct nfs_delegation
*ret
= NULL
;
271 struct nfs_delegation
*delegation
= rcu_dereference(nfsi
->delegation
);
273 if (delegation
== NULL
)
275 spin_lock(&delegation
->lock
);
276 if (!test_and_set_bit(NFS_DELEGATION_RETURNING
, &delegation
->flags
))
278 spin_unlock(&delegation
->lock
);
283 static struct nfs_delegation
*
284 nfs_start_delegation_return(struct nfs_inode
*nfsi
)
286 struct nfs_delegation
*delegation
;
289 delegation
= nfs_start_delegation_return_locked(nfsi
);
295 nfs_abort_delegation_return(struct nfs_delegation
*delegation
,
296 struct nfs_client
*clp
)
299 spin_lock(&delegation
->lock
);
300 clear_bit(NFS_DELEGATION_RETURNING
, &delegation
->flags
);
301 set_bit(NFS_DELEGATION_RETURN
, &delegation
->flags
);
302 spin_unlock(&delegation
->lock
);
303 set_bit(NFS4CLNT_DELEGRETURN
, &clp
->cl_state
);
306 static struct nfs_delegation
*
307 nfs_detach_delegation_locked(struct nfs_inode
*nfsi
,
308 struct nfs_delegation
*delegation
,
309 struct nfs_client
*clp
)
311 struct nfs_delegation
*deleg_cur
=
312 rcu_dereference_protected(nfsi
->delegation
,
313 lockdep_is_held(&clp
->cl_lock
));
315 if (deleg_cur
== NULL
|| delegation
!= deleg_cur
)
318 spin_lock(&delegation
->lock
);
319 if (!delegation
->inode
) {
320 spin_unlock(&delegation
->lock
);
323 list_del_rcu(&delegation
->super_list
);
324 delegation
->inode
= NULL
;
325 rcu_assign_pointer(nfsi
->delegation
, NULL
);
326 spin_unlock(&delegation
->lock
);
330 static struct nfs_delegation
*nfs_detach_delegation(struct nfs_inode
*nfsi
,
331 struct nfs_delegation
*delegation
,
332 struct nfs_server
*server
)
334 struct nfs_client
*clp
= server
->nfs_client
;
336 spin_lock(&clp
->cl_lock
);
337 delegation
= nfs_detach_delegation_locked(nfsi
, delegation
, clp
);
338 spin_unlock(&clp
->cl_lock
);
342 static struct nfs_delegation
*
343 nfs_inode_detach_delegation(struct inode
*inode
)
345 struct nfs_inode
*nfsi
= NFS_I(inode
);
346 struct nfs_server
*server
= NFS_SERVER(inode
);
347 struct nfs_delegation
*delegation
;
350 delegation
= rcu_dereference(nfsi
->delegation
);
351 if (delegation
!= NULL
)
352 delegation
= nfs_detach_delegation(nfsi
, delegation
, server
);
358 nfs_update_inplace_delegation(struct nfs_delegation
*delegation
,
359 const struct nfs_delegation
*update
)
361 if (nfs4_stateid_is_newer(&update
->stateid
, &delegation
->stateid
)) {
362 delegation
->stateid
.seqid
= update
->stateid
.seqid
;
364 delegation
->type
= update
->type
;
365 if (test_and_clear_bit(NFS_DELEGATION_REVOKED
, &delegation
->flags
))
366 atomic_long_inc(&nfs_active_delegations
);
371 * nfs_inode_set_delegation - set up a delegation on an inode
372 * @inode: inode to which delegation applies
373 * @cred: cred to use for subsequent delegation processing
374 * @type: delegation type
375 * @stateid: delegation stateid
376 * @pagemod_limit: write delegation "space_limit"
378 * Returns zero on success, or a negative errno value.
380 int nfs_inode_set_delegation(struct inode
*inode
, const struct cred
*cred
,
382 const nfs4_stateid
*stateid
,
383 unsigned long pagemod_limit
)
385 struct nfs_server
*server
= NFS_SERVER(inode
);
386 struct nfs_client
*clp
= server
->nfs_client
;
387 struct nfs_inode
*nfsi
= NFS_I(inode
);
388 struct nfs_delegation
*delegation
, *old_delegation
;
389 struct nfs_delegation
*freeme
= NULL
;
392 delegation
= kmalloc(sizeof(*delegation
), GFP_NOFS
);
393 if (delegation
== NULL
)
395 nfs4_stateid_copy(&delegation
->stateid
, stateid
);
396 delegation
->type
= type
;
397 delegation
->pagemod_limit
= pagemod_limit
;
398 delegation
->change_attr
= inode_peek_iversion_raw(inode
);
399 delegation
->cred
= get_cred(cred
);
400 delegation
->inode
= inode
;
401 delegation
->flags
= 1<<NFS_DELEGATION_REFERENCED
;
402 spin_lock_init(&delegation
->lock
);
404 spin_lock(&clp
->cl_lock
);
405 old_delegation
= rcu_dereference_protected(nfsi
->delegation
,
406 lockdep_is_held(&clp
->cl_lock
));
407 if (old_delegation
== NULL
)
409 /* Is this an update of the existing delegation? */
410 if (nfs4_stateid_match_other(&old_delegation
->stateid
,
411 &delegation
->stateid
)) {
412 spin_lock(&old_delegation
->lock
);
413 nfs_update_inplace_delegation(old_delegation
,
415 spin_unlock(&old_delegation
->lock
);
418 if (!test_bit(NFS_DELEGATION_REVOKED
, &old_delegation
->flags
)) {
420 * Deal with broken servers that hand out two
421 * delegations for the same file.
422 * Allow for upgrades to a WRITE delegation, but
425 dfprintk(FILE, "%s: server %s handed out "
426 "a duplicate delegation!\n",
427 __func__
, clp
->cl_hostname
);
428 if (delegation
->type
== old_delegation
->type
||
429 !(delegation
->type
& FMODE_WRITE
)) {
434 if (test_and_set_bit(NFS_DELEGATION_RETURNING
,
435 &old_delegation
->flags
))
438 freeme
= nfs_detach_delegation_locked(nfsi
, old_delegation
, clp
);
442 list_add_tail_rcu(&delegation
->super_list
, &server
->delegations
);
443 rcu_assign_pointer(nfsi
->delegation
, delegation
);
446 atomic_long_inc(&nfs_active_delegations
);
448 trace_nfs4_set_delegation(inode
, type
);
450 spin_lock(&inode
->i_lock
);
451 if (NFS_I(inode
)->cache_validity
& (NFS_INO_INVALID_ATTR
|NFS_INO_INVALID_ATIME
))
452 NFS_I(inode
)->cache_validity
|= NFS_INO_REVAL_FORCED
;
453 spin_unlock(&inode
->i_lock
);
455 spin_unlock(&clp
->cl_lock
);
456 if (delegation
!= NULL
)
457 __nfs_free_delegation(delegation
);
458 if (freeme
!= NULL
) {
459 nfs_do_return_delegation(inode
, freeme
, 0);
460 nfs_free_delegation(freeme
);
466 * Basic procedure for returning a delegation to the server
468 static int nfs_end_delegation_return(struct inode
*inode
, struct nfs_delegation
*delegation
, int issync
)
470 struct nfs_client
*clp
= NFS_SERVER(inode
)->nfs_client
;
473 if (delegation
== NULL
)
476 if (test_bit(NFS_DELEGATION_REVOKED
, &delegation
->flags
))
478 err
= nfs_delegation_claim_opens(inode
, &delegation
->stateid
,
480 if (!issync
|| err
!= -EAGAIN
)
483 * Guard against state recovery
485 err
= nfs4_wait_clnt_recover(clp
);
489 nfs_abort_delegation_return(delegation
, clp
);
493 err
= nfs_do_return_delegation(inode
, delegation
, issync
);
498 static bool nfs_delegation_need_return(struct nfs_delegation
*delegation
)
502 if (test_and_clear_bit(NFS_DELEGATION_RETURN
, &delegation
->flags
))
504 else if (test_bit(NFS_DELEGATION_RETURN_IF_CLOSED
, &delegation
->flags
)) {
507 spin_lock(&delegation
->lock
);
508 inode
= delegation
->inode
;
509 if (inode
&& list_empty(&NFS_I(inode
)->open_files
))
511 spin_unlock(&delegation
->lock
);
514 clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED
, &delegation
->flags
);
515 if (test_bit(NFS_DELEGATION_RETURNING
, &delegation
->flags
) ||
516 test_bit(NFS_DELEGATION_REVOKED
, &delegation
->flags
))
523 * nfs_client_return_marked_delegations - return previously marked delegations
524 * @clp: nfs_client to process
526 * Note that this function is designed to be called by the state
527 * manager thread. For this reason, it cannot flush the dirty data,
528 * since that could deadlock in case of a state recovery error.
530 * Returns zero on success, or a negative errno value.
532 int nfs_client_return_marked_delegations(struct nfs_client
*clp
)
534 struct nfs_delegation
*delegation
;
535 struct nfs_delegation
*prev
;
536 struct nfs_server
*server
;
538 struct inode
*place_holder
= NULL
;
539 struct nfs_delegation
*place_holder_deleg
= NULL
;
544 * To avoid quadratic looping we hold a reference
545 * to an inode place_holder. Each time we restart, we
546 * list nfs_servers from the server of that inode, and
547 * delegation in the server from the delegations of that
549 * prev is an RCU-protected pointer to a delegation which
550 * wasn't marked for return and might be a good choice for
551 * the next place_holder.
556 server
= NFS_SERVER(place_holder
);
558 server
= list_entry_rcu(clp
->cl_superblocks
.next
,
559 struct nfs_server
, client_link
);
560 list_for_each_entry_from_rcu(server
, &clp
->cl_superblocks
, client_link
) {
562 if (place_holder
&& server
== NFS_SERVER(place_holder
))
563 delegation
= rcu_dereference(NFS_I(place_holder
)->delegation
);
564 if (!delegation
|| delegation
!= place_holder_deleg
)
565 delegation
= list_entry_rcu(server
->delegations
.next
,
566 struct nfs_delegation
, super_list
);
567 list_for_each_entry_from_rcu(delegation
, &server
->delegations
, super_list
) {
568 struct inode
*to_put
= NULL
;
570 if (!nfs_delegation_need_return(delegation
)) {
574 if (!nfs_sb_active(server
->super
))
575 break; /* continue in outer loop */
580 tmp
= nfs_delegation_grab_inode(prev
);
582 to_put
= place_holder
;
584 place_holder_deleg
= prev
;
588 inode
= nfs_delegation_grab_inode(delegation
);
593 nfs_sb_deactive(server
->super
);
596 delegation
= nfs_start_delegation_return_locked(NFS_I(inode
));
602 err
= nfs_end_delegation_return(inode
, delegation
, 0);
604 nfs_sb_deactive(server
->super
);
608 set_bit(NFS4CLNT_DELEGRETURN
, &clp
->cl_state
);
621 * nfs_inode_evict_delegation - return delegation, don't reclaim opens
622 * @inode: inode to process
624 * Does not protect against delegation reclaims, therefore really only safe
625 * to be called from nfs4_clear_inode(). Guaranteed to always free
626 * the delegation structure.
628 void nfs_inode_evict_delegation(struct inode
*inode
)
630 struct nfs_delegation
*delegation
;
632 delegation
= nfs_inode_detach_delegation(inode
);
633 if (delegation
!= NULL
) {
634 set_bit(NFS_DELEGATION_RETURNING
, &delegation
->flags
);
635 set_bit(NFS_DELEGATION_INODE_FREEING
, &delegation
->flags
);
636 nfs_do_return_delegation(inode
, delegation
, 1);
637 nfs_free_delegation(delegation
);
642 * nfs_inode_return_delegation - synchronously return a delegation
643 * @inode: inode to process
645 * This routine will always flush any dirty data to disk on the
646 * assumption that if we need to return the delegation, then
647 * we should stop caching.
649 * Returns zero on success, or a negative errno value.
651 int nfs4_inode_return_delegation(struct inode
*inode
)
653 struct nfs_inode
*nfsi
= NFS_I(inode
);
654 struct nfs_delegation
*delegation
;
658 delegation
= nfs_start_delegation_return(nfsi
);
659 if (delegation
!= NULL
)
660 err
= nfs_end_delegation_return(inode
, delegation
, 1);
665 * nfs_inode_return_delegation_on_close - asynchronously return a delegation
666 * @inode: inode to process
668 * This routine is called on file close in order to determine if the
669 * inode delegation needs to be returned immediately.
671 void nfs4_inode_return_delegation_on_close(struct inode
*inode
)
673 struct nfs_delegation
*delegation
;
674 struct nfs_delegation
*ret
= NULL
;
679 delegation
= nfs4_get_valid_delegation(inode
);
682 if (test_bit(NFS_DELEGATION_RETURN_IF_CLOSED
, &delegation
->flags
) ||
683 atomic_long_read(&nfs_active_delegations
) >= nfs_delegation_watermark
) {
684 spin_lock(&delegation
->lock
);
685 if (delegation
->inode
&&
686 list_empty(&NFS_I(inode
)->open_files
) &&
687 !test_and_set_bit(NFS_DELEGATION_RETURNING
, &delegation
->flags
)) {
688 clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED
, &delegation
->flags
);
691 spin_unlock(&delegation
->lock
);
695 nfs_end_delegation_return(inode
, ret
, 0);
699 * nfs4_inode_make_writeable
700 * @inode: pointer to inode
702 * Make the inode writeable by returning the delegation if necessary
704 * Returns zero on success, or a negative errno value.
706 int nfs4_inode_make_writeable(struct inode
*inode
)
708 struct nfs_delegation
*delegation
;
711 delegation
= nfs4_get_valid_delegation(inode
);
712 if (delegation
== NULL
||
713 (nfs4_has_session(NFS_SERVER(inode
)->nfs_client
) &&
714 (delegation
->type
& FMODE_WRITE
))) {
719 return nfs4_inode_return_delegation(inode
);
722 static void nfs_mark_return_if_closed_delegation(struct nfs_server
*server
,
723 struct nfs_delegation
*delegation
)
725 set_bit(NFS_DELEGATION_RETURN_IF_CLOSED
, &delegation
->flags
);
726 set_bit(NFS4CLNT_DELEGRETURN
, &server
->nfs_client
->cl_state
);
729 static void nfs_mark_return_delegation(struct nfs_server
*server
,
730 struct nfs_delegation
*delegation
)
732 set_bit(NFS_DELEGATION_RETURN
, &delegation
->flags
);
733 set_bit(NFS4CLNT_DELEGRETURN
, &server
->nfs_client
->cl_state
);
736 static bool nfs_server_mark_return_all_delegations(struct nfs_server
*server
)
738 struct nfs_delegation
*delegation
;
741 list_for_each_entry_rcu(delegation
, &server
->delegations
, super_list
) {
742 nfs_mark_return_delegation(server
, delegation
);
748 static void nfs_client_mark_return_all_delegations(struct nfs_client
*clp
)
750 struct nfs_server
*server
;
753 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
)
754 nfs_server_mark_return_all_delegations(server
);
758 static void nfs_delegation_run_state_manager(struct nfs_client
*clp
)
760 if (test_bit(NFS4CLNT_DELEGRETURN
, &clp
->cl_state
))
761 nfs4_schedule_state_manager(clp
);
765 * nfs_expire_all_delegations
766 * @clp: client to process
769 void nfs_expire_all_delegations(struct nfs_client
*clp
)
771 nfs_client_mark_return_all_delegations(clp
);
772 nfs_delegation_run_state_manager(clp
);
776 * nfs_super_return_all_delegations - return delegations for one superblock
777 * @server: pointer to nfs_server to process
780 void nfs_server_return_all_delegations(struct nfs_server
*server
)
782 struct nfs_client
*clp
= server
->nfs_client
;
789 need_wait
= nfs_server_mark_return_all_delegations(server
);
793 nfs4_schedule_state_manager(clp
);
794 nfs4_wait_clnt_recover(clp
);
798 static void nfs_mark_return_unused_delegation_types(struct nfs_server
*server
,
801 struct nfs_delegation
*delegation
;
803 list_for_each_entry_rcu(delegation
, &server
->delegations
, super_list
) {
804 if ((delegation
->type
== (FMODE_READ
|FMODE_WRITE
)) && !(flags
& FMODE_WRITE
))
806 if (delegation
->type
& flags
)
807 nfs_mark_return_if_closed_delegation(server
, delegation
);
811 static void nfs_client_mark_return_unused_delegation_types(struct nfs_client
*clp
,
814 struct nfs_server
*server
;
817 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
)
818 nfs_mark_return_unused_delegation_types(server
, flags
);
822 static void nfs_revoke_delegation(struct inode
*inode
,
823 const nfs4_stateid
*stateid
)
825 struct nfs_delegation
*delegation
;
830 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
831 if (delegation
== NULL
)
833 if (stateid
== NULL
) {
834 nfs4_stateid_copy(&tmp
, &delegation
->stateid
);
837 if (!nfs4_stateid_match_other(stateid
, &delegation
->stateid
))
839 spin_lock(&delegation
->lock
);
840 if (stateid
->seqid
) {
841 if (nfs4_stateid_is_newer(&delegation
->stateid
, stateid
)) {
842 spin_unlock(&delegation
->lock
);
845 delegation
->stateid
.seqid
= stateid
->seqid
;
847 spin_unlock(&delegation
->lock
);
849 nfs_mark_delegation_revoked(delegation
);
854 nfs_inode_find_state_and_recover(inode
, stateid
);
857 void nfs_remove_bad_delegation(struct inode
*inode
,
858 const nfs4_stateid
*stateid
)
860 nfs_revoke_delegation(inode
, stateid
);
862 EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation
);
864 void nfs_delegation_mark_returned(struct inode
*inode
,
865 const nfs4_stateid
*stateid
)
867 struct nfs_delegation
*delegation
;
873 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
877 spin_lock(&delegation
->lock
);
878 if (!nfs4_stateid_match_other(stateid
, &delegation
->stateid
))
879 goto out_spin_unlock
;
880 if (stateid
->seqid
) {
881 /* If delegation->stateid is newer, dont mark as returned */
882 if (nfs4_stateid_is_newer(&delegation
->stateid
, stateid
))
883 goto out_clear_returning
;
884 if (delegation
->stateid
.seqid
!= stateid
->seqid
)
885 delegation
->stateid
.seqid
= stateid
->seqid
;
888 nfs_mark_delegation_revoked(delegation
);
891 clear_bit(NFS_DELEGATION_RETURNING
, &delegation
->flags
);
893 spin_unlock(&delegation
->lock
);
897 nfs_inode_find_state_and_recover(inode
, stateid
);
901 * nfs_expire_unused_delegation_types
902 * @clp: client to process
903 * @flags: delegation types to expire
906 void nfs_expire_unused_delegation_types(struct nfs_client
*clp
, fmode_t flags
)
908 nfs_client_mark_return_unused_delegation_types(clp
, flags
);
909 nfs_delegation_run_state_manager(clp
);
912 static void nfs_mark_return_unreferenced_delegations(struct nfs_server
*server
)
914 struct nfs_delegation
*delegation
;
916 list_for_each_entry_rcu(delegation
, &server
->delegations
, super_list
) {
917 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED
, &delegation
->flags
))
919 nfs_mark_return_if_closed_delegation(server
, delegation
);
924 * nfs_expire_unreferenced_delegations - Eliminate unused delegations
925 * @clp: nfs_client to process
928 void nfs_expire_unreferenced_delegations(struct nfs_client
*clp
)
930 struct nfs_server
*server
;
933 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
)
934 nfs_mark_return_unreferenced_delegations(server
);
937 nfs_delegation_run_state_manager(clp
);
941 * nfs_async_inode_return_delegation - asynchronously return a delegation
942 * @inode: inode to process
943 * @stateid: state ID information
945 * Returns zero on success, or a negative errno value.
947 int nfs_async_inode_return_delegation(struct inode
*inode
,
948 const nfs4_stateid
*stateid
)
950 struct nfs_server
*server
= NFS_SERVER(inode
);
951 struct nfs_client
*clp
= server
->nfs_client
;
952 struct nfs_delegation
*delegation
;
955 delegation
= nfs4_get_valid_delegation(inode
);
956 if (delegation
== NULL
)
958 if (stateid
!= NULL
&&
959 !clp
->cl_mvops
->match_stateid(&delegation
->stateid
, stateid
))
961 nfs_mark_return_delegation(server
, delegation
);
964 nfs_delegation_run_state_manager(clp
);
971 static struct inode
*
972 nfs_delegation_find_inode_server(struct nfs_server
*server
,
973 const struct nfs_fh
*fhandle
)
975 struct nfs_delegation
*delegation
;
976 struct inode
*freeme
, *res
= NULL
;
978 list_for_each_entry_rcu(delegation
, &server
->delegations
, super_list
) {
979 spin_lock(&delegation
->lock
);
980 if (delegation
->inode
!= NULL
&&
981 !test_bit(NFS_DELEGATION_REVOKED
, &delegation
->flags
) &&
982 nfs_compare_fh(fhandle
, &NFS_I(delegation
->inode
)->fh
) == 0) {
983 freeme
= igrab(delegation
->inode
);
984 if (freeme
&& nfs_sb_active(freeme
->i_sb
))
986 spin_unlock(&delegation
->lock
);
994 return ERR_PTR(-EAGAIN
);
996 spin_unlock(&delegation
->lock
);
998 return ERR_PTR(-ENOENT
);
1002 * nfs_delegation_find_inode - retrieve the inode associated with a delegation
1003 * @clp: client state handle
1004 * @fhandle: filehandle from a delegation recall
1006 * Returns pointer to inode matching "fhandle," or NULL if a matching inode
1009 struct inode
*nfs_delegation_find_inode(struct nfs_client
*clp
,
1010 const struct nfs_fh
*fhandle
)
1012 struct nfs_server
*server
;
1016 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
) {
1017 res
= nfs_delegation_find_inode_server(server
, fhandle
);
1018 if (res
!= ERR_PTR(-ENOENT
)) {
1024 return ERR_PTR(-ENOENT
);
1027 static void nfs_delegation_mark_reclaim_server(struct nfs_server
*server
)
1029 struct nfs_delegation
*delegation
;
1031 list_for_each_entry_rcu(delegation
, &server
->delegations
, super_list
) {
1033 * If the delegation may have been admin revoked, then we
1034 * cannot reclaim it.
1036 if (test_bit(NFS_DELEGATION_TEST_EXPIRED
, &delegation
->flags
))
1038 set_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
);
1043 * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
1044 * @clp: nfs_client to process
1047 void nfs_delegation_mark_reclaim(struct nfs_client
*clp
)
1049 struct nfs_server
*server
;
1052 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
)
1053 nfs_delegation_mark_reclaim_server(server
);
1058 * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
1059 * @clp: nfs_client to process
1062 void nfs_delegation_reap_unclaimed(struct nfs_client
*clp
)
1064 struct nfs_delegation
*delegation
;
1065 struct nfs_server
*server
;
1066 struct inode
*inode
;
1070 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
) {
1071 list_for_each_entry_rcu(delegation
, &server
->delegations
,
1073 if (test_bit(NFS_DELEGATION_INODE_FREEING
,
1074 &delegation
->flags
) ||
1075 test_bit(NFS_DELEGATION_RETURNING
,
1076 &delegation
->flags
) ||
1077 test_bit(NFS_DELEGATION_NEED_RECLAIM
,
1078 &delegation
->flags
) == 0)
1080 if (!nfs_sb_active(server
->super
))
1081 break; /* continue in outer loop */
1082 inode
= nfs_delegation_grab_inode(delegation
);
1083 if (inode
== NULL
) {
1085 nfs_sb_deactive(server
->super
);
1088 delegation
= nfs_start_delegation_return_locked(NFS_I(inode
));
1090 if (delegation
!= NULL
) {
1091 delegation
= nfs_detach_delegation(NFS_I(inode
),
1092 delegation
, server
);
1093 if (delegation
!= NULL
)
1094 nfs_free_delegation(delegation
);
1097 nfs_sb_deactive(server
->super
);
1105 static inline bool nfs4_server_rebooted(const struct nfs_client
*clp
)
1107 return (clp
->cl_state
& (BIT(NFS4CLNT_CHECK_LEASE
) |
1108 BIT(NFS4CLNT_LEASE_EXPIRED
) |
1109 BIT(NFS4CLNT_SESSION_RESET
))) != 0;
1112 static void nfs_mark_test_expired_delegation(struct nfs_server
*server
,
1113 struct nfs_delegation
*delegation
)
1115 if (delegation
->stateid
.type
== NFS4_INVALID_STATEID_TYPE
)
1117 clear_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
);
1118 set_bit(NFS_DELEGATION_TEST_EXPIRED
, &delegation
->flags
);
1119 set_bit(NFS4CLNT_DELEGATION_EXPIRED
, &server
->nfs_client
->cl_state
);
1122 static void nfs_inode_mark_test_expired_delegation(struct nfs_server
*server
,
1123 struct inode
*inode
)
1125 struct nfs_delegation
*delegation
;
1128 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
1130 nfs_mark_test_expired_delegation(server
, delegation
);
1135 static void nfs_delegation_mark_test_expired_server(struct nfs_server
*server
)
1137 struct nfs_delegation
*delegation
;
1139 list_for_each_entry_rcu(delegation
, &server
->delegations
, super_list
)
1140 nfs_mark_test_expired_delegation(server
, delegation
);
1144 * nfs_mark_test_expired_all_delegations - mark all delegations for testing
1145 * @clp: nfs_client to process
1147 * Iterates through all the delegations associated with this server and
1148 * marks them as needing to be checked for validity.
1150 void nfs_mark_test_expired_all_delegations(struct nfs_client
*clp
)
1152 struct nfs_server
*server
;
1155 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
)
1156 nfs_delegation_mark_test_expired_server(server
);
1161 * nfs_test_expired_all_delegations - test all delegations for a client
1162 * @clp: nfs_client to process
1164 * Helper for handling "recallable state revoked" status from server.
1166 void nfs_test_expired_all_delegations(struct nfs_client
*clp
)
1168 nfs_mark_test_expired_all_delegations(clp
);
1169 nfs4_schedule_state_manager(clp
);
1173 nfs_delegation_test_free_expired(struct inode
*inode
,
1174 nfs4_stateid
*stateid
,
1175 const struct cred
*cred
)
1177 struct nfs_server
*server
= NFS_SERVER(inode
);
1178 const struct nfs4_minor_version_ops
*ops
= server
->nfs_client
->cl_mvops
;
1183 status
= ops
->test_and_free_expired(server
, stateid
, cred
);
1184 if (status
== -NFS4ERR_EXPIRED
|| status
== -NFS4ERR_BAD_STATEID
)
1185 nfs_remove_bad_delegation(inode
, stateid
);
1189 * nfs_reap_expired_delegations - reap expired delegations
1190 * @clp: nfs_client to process
1192 * Iterates through all the delegations associated with this server and
1193 * checks if they have may have been revoked. This function is usually
1194 * expected to be called in cases where the server may have lost its
1197 void nfs_reap_expired_delegations(struct nfs_client
*clp
)
1199 struct nfs_delegation
*delegation
;
1200 struct nfs_server
*server
;
1201 struct inode
*inode
;
1202 const struct cred
*cred
;
1203 nfs4_stateid stateid
;
1207 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
) {
1208 list_for_each_entry_rcu(delegation
, &server
->delegations
,
1210 if (test_bit(NFS_DELEGATION_INODE_FREEING
,
1211 &delegation
->flags
) ||
1212 test_bit(NFS_DELEGATION_RETURNING
,
1213 &delegation
->flags
) ||
1214 test_bit(NFS_DELEGATION_TEST_EXPIRED
,
1215 &delegation
->flags
) == 0)
1217 if (!nfs_sb_active(server
->super
))
1218 break; /* continue in outer loop */
1219 inode
= nfs_delegation_grab_inode(delegation
);
1220 if (inode
== NULL
) {
1222 nfs_sb_deactive(server
->super
);
1225 cred
= get_cred_rcu(delegation
->cred
);
1226 nfs4_stateid_copy(&stateid
, &delegation
->stateid
);
1227 clear_bit(NFS_DELEGATION_TEST_EXPIRED
, &delegation
->flags
);
1229 nfs_delegation_test_free_expired(inode
, &stateid
, cred
);
1231 if (nfs4_server_rebooted(clp
)) {
1232 nfs_inode_mark_test_expired_delegation(server
,inode
);
1234 nfs_sb_deactive(server
->super
);
1238 nfs_sb_deactive(server
->super
);
1246 void nfs_inode_find_delegation_state_and_recover(struct inode
*inode
,
1247 const nfs4_stateid
*stateid
)
1249 struct nfs_client
*clp
= NFS_SERVER(inode
)->nfs_client
;
1250 struct nfs_delegation
*delegation
;
1254 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
1256 nfs4_stateid_match_or_older(&delegation
->stateid
, stateid
) &&
1257 !test_bit(NFS_DELEGATION_REVOKED
, &delegation
->flags
)) {
1258 nfs_mark_test_expired_delegation(NFS_SERVER(inode
), delegation
);
1263 nfs4_schedule_state_manager(clp
);
1267 * nfs_delegations_present - check for existence of delegations
1268 * @clp: client state handle
1270 * Returns one if there are any nfs_delegation structures attached
1271 * to this nfs_client.
1273 int nfs_delegations_present(struct nfs_client
*clp
)
1275 struct nfs_server
*server
;
1279 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
)
1280 if (!list_empty(&server
->delegations
)) {
1289 * nfs4_refresh_delegation_stateid - Update delegation stateid seqid
1290 * @dst: stateid to refresh
1291 * @inode: inode to check
1293 * Returns "true" and updates "dst->seqid" * if inode had a delegation
1294 * that matches our delegation stateid. Otherwise "false" is returned.
1296 bool nfs4_refresh_delegation_stateid(nfs4_stateid
*dst
, struct inode
*inode
)
1298 struct nfs_delegation
*delegation
;
1304 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
1305 if (delegation
!= NULL
&&
1306 nfs4_stateid_match_other(dst
, &delegation
->stateid
) &&
1307 nfs4_stateid_is_newer(&delegation
->stateid
, dst
) &&
1308 !test_bit(NFS_DELEGATION_REVOKED
, &delegation
->flags
)) {
1309 dst
->seqid
= delegation
->stateid
.seqid
;
1318 * nfs4_copy_delegation_stateid - Copy inode's state ID information
1319 * @inode: inode to check
1320 * @flags: delegation type requirement
1321 * @dst: stateid data structure to fill in
1322 * @cred: optional argument to retrieve credential
1324 * Returns "true" and fills in "dst->data" * if inode had a delegation,
1325 * otherwise "false" is returned.
1327 bool nfs4_copy_delegation_stateid(struct inode
*inode
, fmode_t flags
,
1328 nfs4_stateid
*dst
, const struct cred
**cred
)
1330 struct nfs_inode
*nfsi
= NFS_I(inode
);
1331 struct nfs_delegation
*delegation
;
1334 flags
&= FMODE_READ
|FMODE_WRITE
;
1336 delegation
= rcu_dereference(nfsi
->delegation
);
1337 ret
= nfs4_is_valid_delegation(delegation
, flags
);
1339 nfs4_stateid_copy(dst
, &delegation
->stateid
);
1340 nfs_mark_delegation_referenced(delegation
);
1342 *cred
= get_cred(delegation
->cred
);
1349 * nfs4_delegation_flush_on_close - Check if we must flush file on close
1350 * @inode: inode to check
1352 * This function checks the number of outstanding writes to the file
1353 * against the delegation 'space_limit' field to see if
1354 * the spec requires us to flush the file on close.
1356 bool nfs4_delegation_flush_on_close(const struct inode
*inode
)
1358 struct nfs_inode
*nfsi
= NFS_I(inode
);
1359 struct nfs_delegation
*delegation
;
1363 delegation
= rcu_dereference(nfsi
->delegation
);
1364 if (delegation
== NULL
|| !(delegation
->type
& FMODE_WRITE
))
1366 if (atomic_long_read(&nfsi
->nrequests
) < delegation
->pagemod_limit
)
1373 module_param_named(delegation_watermark
, nfs_delegation_watermark
, uint
, 0644);