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
);
45 if (!test_bit(NFS_DELEGATION_RETURNING
, &delegation
->flags
))
46 nfs_clear_verifier_delegated(delegation
->inode
);
50 static struct nfs_delegation
*nfs_get_delegation(struct nfs_delegation
*delegation
)
52 refcount_inc(&delegation
->refcount
);
56 static void nfs_put_delegation(struct nfs_delegation
*delegation
)
58 if (refcount_dec_and_test(&delegation
->refcount
))
59 __nfs_free_delegation(delegation
);
62 static void nfs_free_delegation(struct nfs_delegation
*delegation
)
64 nfs_mark_delegation_revoked(delegation
);
65 nfs_put_delegation(delegation
);
69 * nfs_mark_delegation_referenced - set delegation's REFERENCED flag
70 * @delegation: delegation to process
73 void nfs_mark_delegation_referenced(struct nfs_delegation
*delegation
)
75 set_bit(NFS_DELEGATION_REFERENCED
, &delegation
->flags
);
79 nfs4_is_valid_delegation(const struct nfs_delegation
*delegation
,
82 if (delegation
!= NULL
&& (delegation
->type
& flags
) == flags
&&
83 !test_bit(NFS_DELEGATION_REVOKED
, &delegation
->flags
) &&
84 !test_bit(NFS_DELEGATION_RETURNING
, &delegation
->flags
))
89 struct nfs_delegation
*nfs4_get_valid_delegation(const struct inode
*inode
)
91 struct nfs_delegation
*delegation
;
93 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
94 if (nfs4_is_valid_delegation(delegation
, 0))
100 nfs4_do_check_delegation(struct inode
*inode
, fmode_t flags
, bool mark
)
102 struct nfs_delegation
*delegation
;
105 flags
&= FMODE_READ
|FMODE_WRITE
;
107 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
108 if (nfs4_is_valid_delegation(delegation
, flags
)) {
110 nfs_mark_delegation_referenced(delegation
);
117 * nfs_have_delegation - check if inode has a delegation, mark it
118 * NFS_DELEGATION_REFERENCED if there is one.
119 * @inode: inode to check
120 * @flags: delegation types to check for
122 * Returns one if inode has the indicated delegation, otherwise zero.
124 int nfs4_have_delegation(struct inode
*inode
, fmode_t flags
)
126 return nfs4_do_check_delegation(inode
, flags
, true);
130 * nfs4_check_delegation - check if inode has a delegation, do not mark
131 * NFS_DELEGATION_REFERENCED if it has one.
133 int nfs4_check_delegation(struct inode
*inode
, fmode_t flags
)
135 return nfs4_do_check_delegation(inode
, flags
, false);
138 static int nfs_delegation_claim_locks(struct nfs4_state
*state
, const nfs4_stateid
*stateid
)
140 struct inode
*inode
= state
->inode
;
141 struct file_lock
*fl
;
142 struct file_lock_context
*flctx
= inode
->i_flctx
;
143 struct list_head
*list
;
149 list
= &flctx
->flc_posix
;
150 spin_lock(&flctx
->flc_lock
);
152 list_for_each_entry(fl
, list
, fl_list
) {
153 if (nfs_file_open_context(fl
->fl_file
)->state
!= state
)
155 spin_unlock(&flctx
->flc_lock
);
156 status
= nfs4_lock_delegation_recall(fl
, state
, stateid
);
159 spin_lock(&flctx
->flc_lock
);
161 if (list
== &flctx
->flc_posix
) {
162 list
= &flctx
->flc_flock
;
165 spin_unlock(&flctx
->flc_lock
);
170 static int nfs_delegation_claim_opens(struct inode
*inode
,
171 const nfs4_stateid
*stateid
, fmode_t type
)
173 struct nfs_inode
*nfsi
= NFS_I(inode
);
174 struct nfs_open_context
*ctx
;
175 struct nfs4_state_owner
*sp
;
176 struct nfs4_state
*state
;
182 list_for_each_entry_rcu(ctx
, &nfsi
->open_files
, list
) {
186 if (!test_bit(NFS_DELEGATED_STATE
, &state
->flags
))
188 if (!nfs4_valid_open_stateid(state
))
190 if (!nfs4_stateid_match(&state
->stateid
, stateid
))
192 if (!get_nfs_open_context(ctx
))
196 /* Block nfs4_proc_unlck */
197 mutex_lock(&sp
->so_delegreturn_mutex
);
198 seq
= raw_seqcount_begin(&sp
->so_reclaim_seqcount
);
199 err
= nfs4_open_delegation_recall(ctx
, state
, stateid
);
201 err
= nfs_delegation_claim_locks(state
, stateid
);
202 if (!err
&& read_seqcount_retry(&sp
->so_reclaim_seqcount
, seq
))
204 mutex_unlock(&sp
->so_delegreturn_mutex
);
205 put_nfs_open_context(ctx
);
215 * nfs_inode_reclaim_delegation - process a delegation reclaim request
216 * @inode: inode to process
217 * @cred: credential to use for request
218 * @type: delegation type
219 * @stateid: delegation stateid
220 * @pagemod_limit: write delegation "space_limit"
223 void nfs_inode_reclaim_delegation(struct inode
*inode
, const struct cred
*cred
,
225 const nfs4_stateid
*stateid
,
226 unsigned long pagemod_limit
)
228 struct nfs_delegation
*delegation
;
229 const struct cred
*oldcred
= NULL
;
232 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
233 if (delegation
!= NULL
) {
234 spin_lock(&delegation
->lock
);
235 if (nfs4_is_valid_delegation(delegation
, 0)) {
236 nfs4_stateid_copy(&delegation
->stateid
, stateid
);
237 delegation
->type
= type
;
238 delegation
->pagemod_limit
= pagemod_limit
;
239 oldcred
= delegation
->cred
;
240 delegation
->cred
= get_cred(cred
);
241 clear_bit(NFS_DELEGATION_NEED_RECLAIM
,
243 spin_unlock(&delegation
->lock
);
246 trace_nfs4_reclaim_delegation(inode
, type
);
249 /* We appear to have raced with a delegation return. */
250 spin_unlock(&delegation
->lock
);
253 nfs_inode_set_delegation(inode
, cred
, type
, stateid
, pagemod_limit
);
256 static int nfs_do_return_delegation(struct inode
*inode
, struct nfs_delegation
*delegation
, int issync
)
258 const struct cred
*cred
;
261 if (!test_bit(NFS_DELEGATION_REVOKED
, &delegation
->flags
)) {
262 spin_lock(&delegation
->lock
);
263 cred
= get_cred(delegation
->cred
);
264 spin_unlock(&delegation
->lock
);
265 res
= nfs4_proc_delegreturn(inode
, cred
,
266 &delegation
->stateid
,
273 static struct inode
*nfs_delegation_grab_inode(struct nfs_delegation
*delegation
)
275 struct inode
*inode
= NULL
;
277 spin_lock(&delegation
->lock
);
278 if (delegation
->inode
!= NULL
)
279 inode
= igrab(delegation
->inode
);
281 set_bit(NFS_DELEGATION_INODE_FREEING
, &delegation
->flags
);
282 spin_unlock(&delegation
->lock
);
286 static struct nfs_delegation
*
287 nfs_start_delegation_return_locked(struct nfs_inode
*nfsi
)
289 struct nfs_delegation
*ret
= NULL
;
290 struct nfs_delegation
*delegation
= rcu_dereference(nfsi
->delegation
);
292 if (delegation
== NULL
)
294 spin_lock(&delegation
->lock
);
295 if (!test_and_set_bit(NFS_DELEGATION_RETURNING
, &delegation
->flags
)) {
296 /* Refcount matched in nfs_end_delegation_return() */
297 ret
= nfs_get_delegation(delegation
);
299 spin_unlock(&delegation
->lock
);
301 nfs_clear_verifier_delegated(&nfsi
->vfs_inode
);
306 static struct nfs_delegation
*
307 nfs_start_delegation_return(struct nfs_inode
*nfsi
)
309 struct nfs_delegation
*delegation
;
312 delegation
= nfs_start_delegation_return_locked(nfsi
);
318 nfs_abort_delegation_return(struct nfs_delegation
*delegation
,
319 struct nfs_client
*clp
)
322 spin_lock(&delegation
->lock
);
323 clear_bit(NFS_DELEGATION_RETURNING
, &delegation
->flags
);
324 set_bit(NFS_DELEGATION_RETURN
, &delegation
->flags
);
325 spin_unlock(&delegation
->lock
);
326 set_bit(NFS4CLNT_DELEGRETURN
, &clp
->cl_state
);
329 static struct nfs_delegation
*
330 nfs_detach_delegation_locked(struct nfs_inode
*nfsi
,
331 struct nfs_delegation
*delegation
,
332 struct nfs_client
*clp
)
334 struct nfs_delegation
*deleg_cur
=
335 rcu_dereference_protected(nfsi
->delegation
,
336 lockdep_is_held(&clp
->cl_lock
));
338 if (deleg_cur
== NULL
|| delegation
!= deleg_cur
)
341 spin_lock(&delegation
->lock
);
342 if (!delegation
->inode
) {
343 spin_unlock(&delegation
->lock
);
346 list_del_rcu(&delegation
->super_list
);
347 delegation
->inode
= NULL
;
348 rcu_assign_pointer(nfsi
->delegation
, NULL
);
349 spin_unlock(&delegation
->lock
);
353 static struct nfs_delegation
*nfs_detach_delegation(struct nfs_inode
*nfsi
,
354 struct nfs_delegation
*delegation
,
355 struct nfs_server
*server
)
357 struct nfs_client
*clp
= server
->nfs_client
;
359 spin_lock(&clp
->cl_lock
);
360 delegation
= nfs_detach_delegation_locked(nfsi
, delegation
, clp
);
361 spin_unlock(&clp
->cl_lock
);
365 static struct nfs_delegation
*
366 nfs_inode_detach_delegation(struct inode
*inode
)
368 struct nfs_inode
*nfsi
= NFS_I(inode
);
369 struct nfs_server
*server
= NFS_SERVER(inode
);
370 struct nfs_delegation
*delegation
;
373 delegation
= rcu_dereference(nfsi
->delegation
);
374 if (delegation
!= NULL
)
375 delegation
= nfs_detach_delegation(nfsi
, delegation
, server
);
381 nfs_update_inplace_delegation(struct nfs_delegation
*delegation
,
382 const struct nfs_delegation
*update
)
384 if (nfs4_stateid_is_newer(&update
->stateid
, &delegation
->stateid
)) {
385 delegation
->stateid
.seqid
= update
->stateid
.seqid
;
387 delegation
->type
= update
->type
;
388 if (test_and_clear_bit(NFS_DELEGATION_REVOKED
, &delegation
->flags
))
389 atomic_long_inc(&nfs_active_delegations
);
394 * nfs_inode_set_delegation - set up a delegation on an inode
395 * @inode: inode to which delegation applies
396 * @cred: cred to use for subsequent delegation processing
397 * @type: delegation type
398 * @stateid: delegation stateid
399 * @pagemod_limit: write delegation "space_limit"
401 * Returns zero on success, or a negative errno value.
403 int nfs_inode_set_delegation(struct inode
*inode
, const struct cred
*cred
,
405 const nfs4_stateid
*stateid
,
406 unsigned long pagemod_limit
)
408 struct nfs_server
*server
= NFS_SERVER(inode
);
409 struct nfs_client
*clp
= server
->nfs_client
;
410 struct nfs_inode
*nfsi
= NFS_I(inode
);
411 struct nfs_delegation
*delegation
, *old_delegation
;
412 struct nfs_delegation
*freeme
= NULL
;
415 delegation
= kmalloc(sizeof(*delegation
), GFP_NOFS
);
416 if (delegation
== NULL
)
418 nfs4_stateid_copy(&delegation
->stateid
, stateid
);
419 refcount_set(&delegation
->refcount
, 1);
420 delegation
->type
= type
;
421 delegation
->pagemod_limit
= pagemod_limit
;
422 delegation
->change_attr
= inode_peek_iversion_raw(inode
);
423 delegation
->cred
= get_cred(cred
);
424 delegation
->inode
= inode
;
425 delegation
->flags
= 1<<NFS_DELEGATION_REFERENCED
;
426 spin_lock_init(&delegation
->lock
);
428 spin_lock(&clp
->cl_lock
);
429 old_delegation
= rcu_dereference_protected(nfsi
->delegation
,
430 lockdep_is_held(&clp
->cl_lock
));
431 if (old_delegation
== NULL
)
433 /* Is this an update of the existing delegation? */
434 if (nfs4_stateid_match_other(&old_delegation
->stateid
,
435 &delegation
->stateid
)) {
436 spin_lock(&old_delegation
->lock
);
437 nfs_update_inplace_delegation(old_delegation
,
439 spin_unlock(&old_delegation
->lock
);
442 if (!test_bit(NFS_DELEGATION_REVOKED
, &old_delegation
->flags
)) {
444 * Deal with broken servers that hand out two
445 * delegations for the same file.
446 * Allow for upgrades to a WRITE delegation, but
449 dfprintk(FILE, "%s: server %s handed out "
450 "a duplicate delegation!\n",
451 __func__
, clp
->cl_hostname
);
452 if (delegation
->type
== old_delegation
->type
||
453 !(delegation
->type
& FMODE_WRITE
)) {
458 if (test_and_set_bit(NFS_DELEGATION_RETURNING
,
459 &old_delegation
->flags
))
462 freeme
= nfs_detach_delegation_locked(nfsi
, old_delegation
, clp
);
466 list_add_tail_rcu(&delegation
->super_list
, &server
->delegations
);
467 rcu_assign_pointer(nfsi
->delegation
, delegation
);
470 atomic_long_inc(&nfs_active_delegations
);
472 trace_nfs4_set_delegation(inode
, type
);
474 spin_lock(&inode
->i_lock
);
475 if (NFS_I(inode
)->cache_validity
& (NFS_INO_INVALID_ATTR
|NFS_INO_INVALID_ATIME
))
476 NFS_I(inode
)->cache_validity
|= NFS_INO_REVAL_FORCED
;
477 spin_unlock(&inode
->i_lock
);
479 spin_unlock(&clp
->cl_lock
);
480 if (delegation
!= NULL
)
481 __nfs_free_delegation(delegation
);
482 if (freeme
!= NULL
) {
483 nfs_do_return_delegation(inode
, freeme
, 0);
484 nfs_free_delegation(freeme
);
490 * Basic procedure for returning a delegation to the server
492 static int nfs_end_delegation_return(struct inode
*inode
, struct nfs_delegation
*delegation
, int issync
)
494 struct nfs_client
*clp
= NFS_SERVER(inode
)->nfs_client
;
497 if (delegation
== NULL
)
500 if (test_bit(NFS_DELEGATION_REVOKED
, &delegation
->flags
))
502 err
= nfs_delegation_claim_opens(inode
, &delegation
->stateid
,
504 if (!issync
|| err
!= -EAGAIN
)
507 * Guard against state recovery
509 err
= nfs4_wait_clnt_recover(clp
);
513 nfs_abort_delegation_return(delegation
, clp
);
517 err
= nfs_do_return_delegation(inode
, delegation
, issync
);
519 /* Refcount matched in nfs_start_delegation_return_locked() */
520 nfs_put_delegation(delegation
);
524 static bool nfs_delegation_need_return(struct nfs_delegation
*delegation
)
528 if (test_and_clear_bit(NFS_DELEGATION_RETURN
, &delegation
->flags
))
530 else if (test_bit(NFS_DELEGATION_RETURN_IF_CLOSED
, &delegation
->flags
)) {
533 spin_lock(&delegation
->lock
);
534 inode
= delegation
->inode
;
535 if (inode
&& list_empty(&NFS_I(inode
)->open_files
))
537 spin_unlock(&delegation
->lock
);
540 clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED
, &delegation
->flags
);
541 if (test_bit(NFS_DELEGATION_RETURNING
, &delegation
->flags
) ||
542 test_bit(NFS_DELEGATION_REVOKED
, &delegation
->flags
))
549 * nfs_client_return_marked_delegations - return previously marked delegations
550 * @clp: nfs_client to process
552 * Note that this function is designed to be called by the state
553 * manager thread. For this reason, it cannot flush the dirty data,
554 * since that could deadlock in case of a state recovery error.
556 * Returns zero on success, or a negative errno value.
558 int nfs_client_return_marked_delegations(struct nfs_client
*clp
)
560 struct nfs_delegation
*delegation
;
561 struct nfs_delegation
*prev
;
562 struct nfs_server
*server
;
564 struct inode
*place_holder
= NULL
;
565 struct nfs_delegation
*place_holder_deleg
= NULL
;
570 * To avoid quadratic looping we hold a reference
571 * to an inode place_holder. Each time we restart, we
572 * list nfs_servers from the server of that inode, and
573 * delegation in the server from the delegations of that
575 * prev is an RCU-protected pointer to a delegation which
576 * wasn't marked for return and might be a good choice for
577 * the next place_holder.
582 server
= NFS_SERVER(place_holder
);
584 server
= list_entry_rcu(clp
->cl_superblocks
.next
,
585 struct nfs_server
, client_link
);
586 list_for_each_entry_from_rcu(server
, &clp
->cl_superblocks
, client_link
) {
588 if (place_holder
&& server
== NFS_SERVER(place_holder
))
589 delegation
= rcu_dereference(NFS_I(place_holder
)->delegation
);
590 if (!delegation
|| delegation
!= place_holder_deleg
)
591 delegation
= list_entry_rcu(server
->delegations
.next
,
592 struct nfs_delegation
, super_list
);
593 list_for_each_entry_from_rcu(delegation
, &server
->delegations
, super_list
) {
594 struct inode
*to_put
= NULL
;
596 if (!nfs_delegation_need_return(delegation
)) {
600 if (!nfs_sb_active(server
->super
))
601 break; /* continue in outer loop */
606 tmp
= nfs_delegation_grab_inode(prev
);
608 to_put
= place_holder
;
610 place_holder_deleg
= prev
;
614 inode
= nfs_delegation_grab_inode(delegation
);
619 nfs_sb_deactive(server
->super
);
622 delegation
= nfs_start_delegation_return_locked(NFS_I(inode
));
628 err
= nfs_end_delegation_return(inode
, delegation
, 0);
630 nfs_sb_deactive(server
->super
);
634 set_bit(NFS4CLNT_DELEGRETURN
, &clp
->cl_state
);
647 * nfs_inode_evict_delegation - return delegation, don't reclaim opens
648 * @inode: inode to process
650 * Does not protect against delegation reclaims, therefore really only safe
651 * to be called from nfs4_clear_inode(). Guaranteed to always free
652 * the delegation structure.
654 void nfs_inode_evict_delegation(struct inode
*inode
)
656 struct nfs_delegation
*delegation
;
658 delegation
= nfs_inode_detach_delegation(inode
);
659 if (delegation
!= NULL
) {
660 set_bit(NFS_DELEGATION_RETURNING
, &delegation
->flags
);
661 set_bit(NFS_DELEGATION_INODE_FREEING
, &delegation
->flags
);
662 nfs_do_return_delegation(inode
, delegation
, 1);
663 nfs_free_delegation(delegation
);
668 * nfs_inode_return_delegation - synchronously return a delegation
669 * @inode: inode to process
671 * This routine will always flush any dirty data to disk on the
672 * assumption that if we need to return the delegation, then
673 * we should stop caching.
675 * Returns zero on success, or a negative errno value.
677 int nfs4_inode_return_delegation(struct inode
*inode
)
679 struct nfs_inode
*nfsi
= NFS_I(inode
);
680 struct nfs_delegation
*delegation
;
684 delegation
= nfs_start_delegation_return(nfsi
);
685 if (delegation
!= NULL
)
686 err
= nfs_end_delegation_return(inode
, delegation
, 1);
691 * nfs_inode_return_delegation_on_close - asynchronously return a delegation
692 * @inode: inode to process
694 * This routine is called on file close in order to determine if the
695 * inode delegation needs to be returned immediately.
697 void nfs4_inode_return_delegation_on_close(struct inode
*inode
)
699 struct nfs_delegation
*delegation
;
700 struct nfs_delegation
*ret
= NULL
;
705 delegation
= nfs4_get_valid_delegation(inode
);
708 if (test_bit(NFS_DELEGATION_RETURN_IF_CLOSED
, &delegation
->flags
) ||
709 atomic_long_read(&nfs_active_delegations
) >= nfs_delegation_watermark
) {
710 spin_lock(&delegation
->lock
);
711 if (delegation
->inode
&&
712 list_empty(&NFS_I(inode
)->open_files
) &&
713 !test_and_set_bit(NFS_DELEGATION_RETURNING
, &delegation
->flags
)) {
714 clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED
, &delegation
->flags
);
715 /* Refcount matched in nfs_end_delegation_return() */
716 ret
= nfs_get_delegation(delegation
);
718 spin_unlock(&delegation
->lock
);
720 nfs_clear_verifier_delegated(inode
);
724 nfs_end_delegation_return(inode
, ret
, 0);
728 * nfs4_inode_make_writeable
729 * @inode: pointer to inode
731 * Make the inode writeable by returning the delegation if necessary
733 * Returns zero on success, or a negative errno value.
735 int nfs4_inode_make_writeable(struct inode
*inode
)
737 struct nfs_delegation
*delegation
;
740 delegation
= nfs4_get_valid_delegation(inode
);
741 if (delegation
== NULL
||
742 (nfs4_has_session(NFS_SERVER(inode
)->nfs_client
) &&
743 (delegation
->type
& FMODE_WRITE
))) {
748 return nfs4_inode_return_delegation(inode
);
751 static void nfs_mark_return_if_closed_delegation(struct nfs_server
*server
,
752 struct nfs_delegation
*delegation
)
754 set_bit(NFS_DELEGATION_RETURN_IF_CLOSED
, &delegation
->flags
);
755 set_bit(NFS4CLNT_DELEGRETURN
, &server
->nfs_client
->cl_state
);
758 static void nfs_mark_return_delegation(struct nfs_server
*server
,
759 struct nfs_delegation
*delegation
)
761 set_bit(NFS_DELEGATION_RETURN
, &delegation
->flags
);
762 set_bit(NFS4CLNT_DELEGRETURN
, &server
->nfs_client
->cl_state
);
765 static bool nfs_server_mark_return_all_delegations(struct nfs_server
*server
)
767 struct nfs_delegation
*delegation
;
770 list_for_each_entry_rcu(delegation
, &server
->delegations
, super_list
) {
771 nfs_mark_return_delegation(server
, delegation
);
777 static void nfs_client_mark_return_all_delegations(struct nfs_client
*clp
)
779 struct nfs_server
*server
;
782 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
)
783 nfs_server_mark_return_all_delegations(server
);
787 static void nfs_delegation_run_state_manager(struct nfs_client
*clp
)
789 if (test_bit(NFS4CLNT_DELEGRETURN
, &clp
->cl_state
))
790 nfs4_schedule_state_manager(clp
);
794 * nfs_expire_all_delegations
795 * @clp: client to process
798 void nfs_expire_all_delegations(struct nfs_client
*clp
)
800 nfs_client_mark_return_all_delegations(clp
);
801 nfs_delegation_run_state_manager(clp
);
805 * nfs_super_return_all_delegations - return delegations for one superblock
806 * @server: pointer to nfs_server to process
809 void nfs_server_return_all_delegations(struct nfs_server
*server
)
811 struct nfs_client
*clp
= server
->nfs_client
;
818 need_wait
= nfs_server_mark_return_all_delegations(server
);
822 nfs4_schedule_state_manager(clp
);
823 nfs4_wait_clnt_recover(clp
);
827 static void nfs_mark_return_unused_delegation_types(struct nfs_server
*server
,
830 struct nfs_delegation
*delegation
;
832 list_for_each_entry_rcu(delegation
, &server
->delegations
, super_list
) {
833 if ((delegation
->type
== (FMODE_READ
|FMODE_WRITE
)) && !(flags
& FMODE_WRITE
))
835 if (delegation
->type
& flags
)
836 nfs_mark_return_if_closed_delegation(server
, delegation
);
840 static void nfs_client_mark_return_unused_delegation_types(struct nfs_client
*clp
,
843 struct nfs_server
*server
;
846 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
)
847 nfs_mark_return_unused_delegation_types(server
, flags
);
851 static void nfs_revoke_delegation(struct inode
*inode
,
852 const nfs4_stateid
*stateid
)
854 struct nfs_delegation
*delegation
;
859 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
860 if (delegation
== NULL
)
862 if (stateid
== NULL
) {
863 nfs4_stateid_copy(&tmp
, &delegation
->stateid
);
866 if (!nfs4_stateid_match_other(stateid
, &delegation
->stateid
))
868 spin_lock(&delegation
->lock
);
869 if (stateid
->seqid
) {
870 if (nfs4_stateid_is_newer(&delegation
->stateid
, stateid
)) {
871 spin_unlock(&delegation
->lock
);
874 delegation
->stateid
.seqid
= stateid
->seqid
;
876 spin_unlock(&delegation
->lock
);
878 nfs_mark_delegation_revoked(delegation
);
883 nfs_inode_find_state_and_recover(inode
, stateid
);
886 void nfs_remove_bad_delegation(struct inode
*inode
,
887 const nfs4_stateid
*stateid
)
889 nfs_revoke_delegation(inode
, stateid
);
891 EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation
);
893 void nfs_delegation_mark_returned(struct inode
*inode
,
894 const nfs4_stateid
*stateid
)
896 struct nfs_delegation
*delegation
;
902 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
906 spin_lock(&delegation
->lock
);
907 if (!nfs4_stateid_match_other(stateid
, &delegation
->stateid
))
908 goto out_spin_unlock
;
909 if (stateid
->seqid
) {
910 /* If delegation->stateid is newer, dont mark as returned */
911 if (nfs4_stateid_is_newer(&delegation
->stateid
, stateid
))
912 goto out_clear_returning
;
913 if (delegation
->stateid
.seqid
!= stateid
->seqid
)
914 delegation
->stateid
.seqid
= stateid
->seqid
;
917 nfs_mark_delegation_revoked(delegation
);
920 clear_bit(NFS_DELEGATION_RETURNING
, &delegation
->flags
);
922 spin_unlock(&delegation
->lock
);
926 nfs_inode_find_state_and_recover(inode
, stateid
);
930 * nfs_expire_unused_delegation_types
931 * @clp: client to process
932 * @flags: delegation types to expire
935 void nfs_expire_unused_delegation_types(struct nfs_client
*clp
, fmode_t flags
)
937 nfs_client_mark_return_unused_delegation_types(clp
, flags
);
938 nfs_delegation_run_state_manager(clp
);
941 static void nfs_mark_return_unreferenced_delegations(struct nfs_server
*server
)
943 struct nfs_delegation
*delegation
;
945 list_for_each_entry_rcu(delegation
, &server
->delegations
, super_list
) {
946 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED
, &delegation
->flags
))
948 nfs_mark_return_if_closed_delegation(server
, delegation
);
953 * nfs_expire_unreferenced_delegations - Eliminate unused delegations
954 * @clp: nfs_client to process
957 void nfs_expire_unreferenced_delegations(struct nfs_client
*clp
)
959 struct nfs_server
*server
;
962 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
)
963 nfs_mark_return_unreferenced_delegations(server
);
966 nfs_delegation_run_state_manager(clp
);
970 * nfs_async_inode_return_delegation - asynchronously return a delegation
971 * @inode: inode to process
972 * @stateid: state ID information
974 * Returns zero on success, or a negative errno value.
976 int nfs_async_inode_return_delegation(struct inode
*inode
,
977 const nfs4_stateid
*stateid
)
979 struct nfs_server
*server
= NFS_SERVER(inode
);
980 struct nfs_client
*clp
= server
->nfs_client
;
981 struct nfs_delegation
*delegation
;
984 delegation
= nfs4_get_valid_delegation(inode
);
985 if (delegation
== NULL
)
987 if (stateid
!= NULL
&&
988 !clp
->cl_mvops
->match_stateid(&delegation
->stateid
, stateid
))
990 nfs_mark_return_delegation(server
, delegation
);
993 nfs_delegation_run_state_manager(clp
);
1000 static struct inode
*
1001 nfs_delegation_find_inode_server(struct nfs_server
*server
,
1002 const struct nfs_fh
*fhandle
)
1004 struct nfs_delegation
*delegation
;
1005 struct inode
*freeme
, *res
= NULL
;
1007 list_for_each_entry_rcu(delegation
, &server
->delegations
, super_list
) {
1008 spin_lock(&delegation
->lock
);
1009 if (delegation
->inode
!= NULL
&&
1010 !test_bit(NFS_DELEGATION_REVOKED
, &delegation
->flags
) &&
1011 nfs_compare_fh(fhandle
, &NFS_I(delegation
->inode
)->fh
) == 0) {
1012 freeme
= igrab(delegation
->inode
);
1013 if (freeme
&& nfs_sb_active(freeme
->i_sb
))
1015 spin_unlock(&delegation
->lock
);
1023 return ERR_PTR(-EAGAIN
);
1025 spin_unlock(&delegation
->lock
);
1027 return ERR_PTR(-ENOENT
);
1031 * nfs_delegation_find_inode - retrieve the inode associated with a delegation
1032 * @clp: client state handle
1033 * @fhandle: filehandle from a delegation recall
1035 * Returns pointer to inode matching "fhandle," or NULL if a matching inode
1038 struct inode
*nfs_delegation_find_inode(struct nfs_client
*clp
,
1039 const struct nfs_fh
*fhandle
)
1041 struct nfs_server
*server
;
1045 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
) {
1046 res
= nfs_delegation_find_inode_server(server
, fhandle
);
1047 if (res
!= ERR_PTR(-ENOENT
)) {
1053 return ERR_PTR(-ENOENT
);
1056 static void nfs_delegation_mark_reclaim_server(struct nfs_server
*server
)
1058 struct nfs_delegation
*delegation
;
1060 list_for_each_entry_rcu(delegation
, &server
->delegations
, super_list
) {
1062 * If the delegation may have been admin revoked, then we
1063 * cannot reclaim it.
1065 if (test_bit(NFS_DELEGATION_TEST_EXPIRED
, &delegation
->flags
))
1067 set_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
);
1072 * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
1073 * @clp: nfs_client to process
1076 void nfs_delegation_mark_reclaim(struct nfs_client
*clp
)
1078 struct nfs_server
*server
;
1081 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
)
1082 nfs_delegation_mark_reclaim_server(server
);
1087 * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
1088 * @clp: nfs_client to process
1091 void nfs_delegation_reap_unclaimed(struct nfs_client
*clp
)
1093 struct nfs_delegation
*delegation
;
1094 struct nfs_server
*server
;
1095 struct inode
*inode
;
1099 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
) {
1100 list_for_each_entry_rcu(delegation
, &server
->delegations
,
1102 if (test_bit(NFS_DELEGATION_INODE_FREEING
,
1103 &delegation
->flags
) ||
1104 test_bit(NFS_DELEGATION_RETURNING
,
1105 &delegation
->flags
) ||
1106 test_bit(NFS_DELEGATION_NEED_RECLAIM
,
1107 &delegation
->flags
) == 0)
1109 if (!nfs_sb_active(server
->super
))
1110 break; /* continue in outer loop */
1111 inode
= nfs_delegation_grab_inode(delegation
);
1112 if (inode
== NULL
) {
1114 nfs_sb_deactive(server
->super
);
1117 delegation
= nfs_start_delegation_return_locked(NFS_I(inode
));
1119 if (delegation
!= NULL
) {
1120 if (nfs_detach_delegation(NFS_I(inode
), delegation
,
1122 nfs_free_delegation(delegation
);
1123 /* Match nfs_start_delegation_return_locked */
1124 nfs_put_delegation(delegation
);
1127 nfs_sb_deactive(server
->super
);
1135 static inline bool nfs4_server_rebooted(const struct nfs_client
*clp
)
1137 return (clp
->cl_state
& (BIT(NFS4CLNT_CHECK_LEASE
) |
1138 BIT(NFS4CLNT_LEASE_EXPIRED
) |
1139 BIT(NFS4CLNT_SESSION_RESET
))) != 0;
1142 static void nfs_mark_test_expired_delegation(struct nfs_server
*server
,
1143 struct nfs_delegation
*delegation
)
1145 if (delegation
->stateid
.type
== NFS4_INVALID_STATEID_TYPE
)
1147 clear_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
);
1148 set_bit(NFS_DELEGATION_TEST_EXPIRED
, &delegation
->flags
);
1149 set_bit(NFS4CLNT_DELEGATION_EXPIRED
, &server
->nfs_client
->cl_state
);
1152 static void nfs_inode_mark_test_expired_delegation(struct nfs_server
*server
,
1153 struct inode
*inode
)
1155 struct nfs_delegation
*delegation
;
1158 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
1160 nfs_mark_test_expired_delegation(server
, delegation
);
1165 static void nfs_delegation_mark_test_expired_server(struct nfs_server
*server
)
1167 struct nfs_delegation
*delegation
;
1169 list_for_each_entry_rcu(delegation
, &server
->delegations
, super_list
)
1170 nfs_mark_test_expired_delegation(server
, delegation
);
1174 * nfs_mark_test_expired_all_delegations - mark all delegations for testing
1175 * @clp: nfs_client to process
1177 * Iterates through all the delegations associated with this server and
1178 * marks them as needing to be checked for validity.
1180 void nfs_mark_test_expired_all_delegations(struct nfs_client
*clp
)
1182 struct nfs_server
*server
;
1185 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
)
1186 nfs_delegation_mark_test_expired_server(server
);
1191 * nfs_test_expired_all_delegations - test all delegations for a client
1192 * @clp: nfs_client to process
1194 * Helper for handling "recallable state revoked" status from server.
1196 void nfs_test_expired_all_delegations(struct nfs_client
*clp
)
1198 nfs_mark_test_expired_all_delegations(clp
);
1199 nfs4_schedule_state_manager(clp
);
1203 nfs_delegation_test_free_expired(struct inode
*inode
,
1204 nfs4_stateid
*stateid
,
1205 const struct cred
*cred
)
1207 struct nfs_server
*server
= NFS_SERVER(inode
);
1208 const struct nfs4_minor_version_ops
*ops
= server
->nfs_client
->cl_mvops
;
1213 status
= ops
->test_and_free_expired(server
, stateid
, cred
);
1214 if (status
== -NFS4ERR_EXPIRED
|| status
== -NFS4ERR_BAD_STATEID
)
1215 nfs_remove_bad_delegation(inode
, stateid
);
1219 * nfs_reap_expired_delegations - reap expired delegations
1220 * @clp: nfs_client to process
1222 * Iterates through all the delegations associated with this server and
1223 * checks if they have may have been revoked. This function is usually
1224 * expected to be called in cases where the server may have lost its
1227 void nfs_reap_expired_delegations(struct nfs_client
*clp
)
1229 struct nfs_delegation
*delegation
;
1230 struct nfs_server
*server
;
1231 struct inode
*inode
;
1232 const struct cred
*cred
;
1233 nfs4_stateid stateid
;
1237 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
) {
1238 list_for_each_entry_rcu(delegation
, &server
->delegations
,
1240 if (test_bit(NFS_DELEGATION_INODE_FREEING
,
1241 &delegation
->flags
) ||
1242 test_bit(NFS_DELEGATION_RETURNING
,
1243 &delegation
->flags
) ||
1244 test_bit(NFS_DELEGATION_TEST_EXPIRED
,
1245 &delegation
->flags
) == 0)
1247 if (!nfs_sb_active(server
->super
))
1248 break; /* continue in outer loop */
1249 inode
= nfs_delegation_grab_inode(delegation
);
1250 if (inode
== NULL
) {
1252 nfs_sb_deactive(server
->super
);
1255 cred
= get_cred_rcu(delegation
->cred
);
1256 nfs4_stateid_copy(&stateid
, &delegation
->stateid
);
1257 clear_bit(NFS_DELEGATION_TEST_EXPIRED
, &delegation
->flags
);
1259 nfs_delegation_test_free_expired(inode
, &stateid
, cred
);
1261 if (nfs4_server_rebooted(clp
)) {
1262 nfs_inode_mark_test_expired_delegation(server
,inode
);
1264 nfs_sb_deactive(server
->super
);
1268 nfs_sb_deactive(server
->super
);
1276 void nfs_inode_find_delegation_state_and_recover(struct inode
*inode
,
1277 const nfs4_stateid
*stateid
)
1279 struct nfs_client
*clp
= NFS_SERVER(inode
)->nfs_client
;
1280 struct nfs_delegation
*delegation
;
1284 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
1286 nfs4_stateid_match_or_older(&delegation
->stateid
, stateid
) &&
1287 !test_bit(NFS_DELEGATION_REVOKED
, &delegation
->flags
)) {
1288 nfs_mark_test_expired_delegation(NFS_SERVER(inode
), delegation
);
1293 nfs4_schedule_state_manager(clp
);
1297 * nfs_delegations_present - check for existence of delegations
1298 * @clp: client state handle
1300 * Returns one if there are any nfs_delegation structures attached
1301 * to this nfs_client.
1303 int nfs_delegations_present(struct nfs_client
*clp
)
1305 struct nfs_server
*server
;
1309 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
)
1310 if (!list_empty(&server
->delegations
)) {
1319 * nfs4_refresh_delegation_stateid - Update delegation stateid seqid
1320 * @dst: stateid to refresh
1321 * @inode: inode to check
1323 * Returns "true" and updates "dst->seqid" * if inode had a delegation
1324 * that matches our delegation stateid. Otherwise "false" is returned.
1326 bool nfs4_refresh_delegation_stateid(nfs4_stateid
*dst
, struct inode
*inode
)
1328 struct nfs_delegation
*delegation
;
1334 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
1335 if (delegation
!= NULL
&&
1336 nfs4_stateid_match_other(dst
, &delegation
->stateid
) &&
1337 nfs4_stateid_is_newer(&delegation
->stateid
, dst
) &&
1338 !test_bit(NFS_DELEGATION_REVOKED
, &delegation
->flags
)) {
1339 dst
->seqid
= delegation
->stateid
.seqid
;
1348 * nfs4_copy_delegation_stateid - Copy inode's state ID information
1349 * @inode: inode to check
1350 * @flags: delegation type requirement
1351 * @dst: stateid data structure to fill in
1352 * @cred: optional argument to retrieve credential
1354 * Returns "true" and fills in "dst->data" * if inode had a delegation,
1355 * otherwise "false" is returned.
1357 bool nfs4_copy_delegation_stateid(struct inode
*inode
, fmode_t flags
,
1358 nfs4_stateid
*dst
, const struct cred
**cred
)
1360 struct nfs_inode
*nfsi
= NFS_I(inode
);
1361 struct nfs_delegation
*delegation
;
1364 flags
&= FMODE_READ
|FMODE_WRITE
;
1366 delegation
= rcu_dereference(nfsi
->delegation
);
1367 ret
= nfs4_is_valid_delegation(delegation
, flags
);
1369 nfs4_stateid_copy(dst
, &delegation
->stateid
);
1370 nfs_mark_delegation_referenced(delegation
);
1372 *cred
= get_cred(delegation
->cred
);
1379 * nfs4_delegation_flush_on_close - Check if we must flush file on close
1380 * @inode: inode to check
1382 * This function checks the number of outstanding writes to the file
1383 * against the delegation 'space_limit' field to see if
1384 * the spec requires us to flush the file on close.
1386 bool nfs4_delegation_flush_on_close(const struct inode
*inode
)
1388 struct nfs_inode
*nfsi
= NFS_I(inode
);
1389 struct nfs_delegation
*delegation
;
1393 delegation
= rcu_dereference(nfsi
->delegation
);
1394 if (delegation
== NULL
|| !(delegation
->type
& FMODE_WRITE
))
1396 if (atomic_long_read(&nfsi
->nrequests
) < delegation
->pagemod_limit
)
1403 module_param_named(delegation_watermark
, nfs_delegation_watermark
, uint
, 0644);