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>
16 #include <linux/nfs4.h>
17 #include <linux/nfs_fs.h>
18 #include <linux/nfs_xdr.h>
21 #include "delegation.h"
24 static void nfs_do_free_delegation(struct nfs_delegation
*delegation
)
29 static void nfs_free_delegation_callback(struct rcu_head
*head
)
31 struct nfs_delegation
*delegation
= container_of(head
, struct nfs_delegation
, rcu
);
33 nfs_do_free_delegation(delegation
);
36 static void nfs_free_delegation(struct nfs_delegation
*delegation
)
38 if (delegation
->cred
) {
39 put_rpccred(delegation
->cred
);
40 delegation
->cred
= NULL
;
42 call_rcu(&delegation
->rcu
, nfs_free_delegation_callback
);
46 * nfs_mark_delegation_referenced - set delegation's REFERENCED flag
47 * @delegation: delegation to process
50 void nfs_mark_delegation_referenced(struct nfs_delegation
*delegation
)
52 set_bit(NFS_DELEGATION_REFERENCED
, &delegation
->flags
);
56 * nfs_have_delegation - check if inode has a delegation
57 * @inode: inode to check
58 * @flags: delegation types to check for
60 * Returns one if inode has the indicated delegation, otherwise zero.
62 int nfs_have_delegation(struct inode
*inode
, fmode_t flags
)
64 struct nfs_delegation
*delegation
;
67 flags
&= FMODE_READ
|FMODE_WRITE
;
69 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
70 if (delegation
!= NULL
&& (delegation
->type
& flags
) == flags
) {
71 nfs_mark_delegation_referenced(delegation
);
78 static int nfs_delegation_claim_locks(struct nfs_open_context
*ctx
, struct nfs4_state
*state
)
80 struct inode
*inode
= state
->inode
;
84 if (inode
->i_flock
== NULL
)
87 /* Protect inode->i_flock using the file locks lock */
89 for (fl
= inode
->i_flock
; fl
!= NULL
; fl
= fl
->fl_next
) {
90 if (!(fl
->fl_flags
& (FL_POSIX
|FL_FLOCK
)))
92 if (nfs_file_open_context(fl
->fl_file
) != ctx
)
95 status
= nfs4_lock_delegation_recall(state
, fl
);
105 static int nfs_delegation_claim_opens(struct inode
*inode
, const nfs4_stateid
*stateid
)
107 struct nfs_inode
*nfsi
= NFS_I(inode
);
108 struct nfs_open_context
*ctx
;
109 struct nfs4_state
*state
;
113 spin_lock(&inode
->i_lock
);
114 list_for_each_entry(ctx
, &nfsi
->open_files
, list
) {
118 if (!test_bit(NFS_DELEGATED_STATE
, &state
->flags
))
120 if (memcmp(state
->stateid
.data
, stateid
->data
, sizeof(state
->stateid
.data
)) != 0)
122 get_nfs_open_context(ctx
);
123 spin_unlock(&inode
->i_lock
);
124 err
= nfs4_open_delegation_recall(ctx
, state
, stateid
);
126 err
= nfs_delegation_claim_locks(ctx
, state
);
127 put_nfs_open_context(ctx
);
132 spin_unlock(&inode
->i_lock
);
137 * nfs_inode_reclaim_delegation - process a delegation reclaim request
138 * @inode: inode to process
139 * @cred: credential to use for request
140 * @res: new delegation state from server
143 void nfs_inode_reclaim_delegation(struct inode
*inode
, struct rpc_cred
*cred
,
144 struct nfs_openres
*res
)
146 struct nfs_delegation
*delegation
;
147 struct rpc_cred
*oldcred
= NULL
;
150 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
151 if (delegation
!= NULL
) {
152 spin_lock(&delegation
->lock
);
153 if (delegation
->inode
!= NULL
) {
154 memcpy(delegation
->stateid
.data
, res
->delegation
.data
,
155 sizeof(delegation
->stateid
.data
));
156 delegation
->type
= res
->delegation_type
;
157 delegation
->maxsize
= res
->maxsize
;
158 oldcred
= delegation
->cred
;
159 delegation
->cred
= get_rpccred(cred
);
160 clear_bit(NFS_DELEGATION_NEED_RECLAIM
,
162 NFS_I(inode
)->delegation_state
= delegation
->type
;
163 spin_unlock(&delegation
->lock
);
164 put_rpccred(oldcred
);
167 /* We appear to have raced with a delegation return. */
168 spin_unlock(&delegation
->lock
);
170 nfs_inode_set_delegation(inode
, cred
, res
);
177 static int nfs_do_return_delegation(struct inode
*inode
, struct nfs_delegation
*delegation
, int issync
)
181 res
= nfs4_proc_delegreturn(inode
, delegation
->cred
, &delegation
->stateid
, issync
);
182 nfs_free_delegation(delegation
);
186 static struct inode
*nfs_delegation_grab_inode(struct nfs_delegation
*delegation
)
188 struct inode
*inode
= NULL
;
190 spin_lock(&delegation
->lock
);
191 if (delegation
->inode
!= NULL
)
192 inode
= igrab(delegation
->inode
);
193 spin_unlock(&delegation
->lock
);
197 static struct nfs_delegation
*
198 nfs_detach_delegation_locked(struct nfs_inode
*nfsi
,
199 struct nfs_server
*server
)
201 struct nfs_delegation
*delegation
=
202 rcu_dereference_protected(nfsi
->delegation
,
203 lockdep_is_held(&server
->nfs_client
->cl_lock
));
205 if (delegation
== NULL
)
208 spin_lock(&delegation
->lock
);
209 list_del_rcu(&delegation
->super_list
);
210 delegation
->inode
= NULL
;
211 nfsi
->delegation_state
= 0;
212 rcu_assign_pointer(nfsi
->delegation
, NULL
);
213 spin_unlock(&delegation
->lock
);
219 static struct nfs_delegation
*nfs_detach_delegation(struct nfs_inode
*nfsi
,
220 struct nfs_server
*server
)
222 struct nfs_client
*clp
= server
->nfs_client
;
223 struct nfs_delegation
*delegation
;
225 spin_lock(&clp
->cl_lock
);
226 delegation
= nfs_detach_delegation_locked(nfsi
, server
);
227 spin_unlock(&clp
->cl_lock
);
232 * nfs_inode_set_delegation - set up a delegation on an inode
233 * @inode: inode to which delegation applies
234 * @cred: cred to use for subsequent delegation processing
235 * @res: new delegation state from server
237 * Returns zero on success, or a negative errno value.
239 int nfs_inode_set_delegation(struct inode
*inode
, struct rpc_cred
*cred
, struct nfs_openres
*res
)
241 struct nfs_server
*server
= NFS_SERVER(inode
);
242 struct nfs_client
*clp
= server
->nfs_client
;
243 struct nfs_inode
*nfsi
= NFS_I(inode
);
244 struct nfs_delegation
*delegation
, *old_delegation
;
245 struct nfs_delegation
*freeme
= NULL
;
248 delegation
= kmalloc(sizeof(*delegation
), GFP_NOFS
);
249 if (delegation
== NULL
)
251 memcpy(delegation
->stateid
.data
, res
->delegation
.data
,
252 sizeof(delegation
->stateid
.data
));
253 delegation
->type
= res
->delegation_type
;
254 delegation
->maxsize
= res
->maxsize
;
255 delegation
->change_attr
= nfsi
->change_attr
;
256 delegation
->cred
= get_rpccred(cred
);
257 delegation
->inode
= inode
;
258 delegation
->flags
= 1<<NFS_DELEGATION_REFERENCED
;
259 spin_lock_init(&delegation
->lock
);
261 spin_lock(&clp
->cl_lock
);
262 old_delegation
= rcu_dereference_protected(nfsi
->delegation
,
263 lockdep_is_held(&clp
->cl_lock
));
264 if (old_delegation
!= NULL
) {
265 if (memcmp(&delegation
->stateid
, &old_delegation
->stateid
,
266 sizeof(old_delegation
->stateid
)) == 0 &&
267 delegation
->type
== old_delegation
->type
) {
271 * Deal with broken servers that hand out two
272 * delegations for the same file.
274 dfprintk(FILE, "%s: server %s handed out "
275 "a duplicate delegation!\n",
276 __func__
, clp
->cl_hostname
);
277 if (delegation
->type
<= old_delegation
->type
) {
282 freeme
= nfs_detach_delegation_locked(nfsi
, server
);
284 list_add_rcu(&delegation
->super_list
, &server
->delegations
);
285 nfsi
->delegation_state
= delegation
->type
;
286 rcu_assign_pointer(nfsi
->delegation
, delegation
);
289 /* Ensure we revalidate the attributes and page cache! */
290 spin_lock(&inode
->i_lock
);
291 nfsi
->cache_validity
|= NFS_INO_REVAL_FORCED
;
292 spin_unlock(&inode
->i_lock
);
295 spin_unlock(&clp
->cl_lock
);
296 if (delegation
!= NULL
)
297 nfs_free_delegation(delegation
);
299 nfs_do_return_delegation(inode
, freeme
, 0);
304 * Basic procedure for returning a delegation to the server
306 static int __nfs_inode_return_delegation(struct inode
*inode
, struct nfs_delegation
*delegation
, int issync
)
308 struct nfs_inode
*nfsi
= NFS_I(inode
);
312 * Guard against new delegated open/lock/unlock calls and against
315 down_write(&nfsi
->rwsem
);
316 err
= nfs_delegation_claim_opens(inode
, &delegation
->stateid
);
317 up_write(&nfsi
->rwsem
);
321 err
= nfs_do_return_delegation(inode
, delegation
, issync
);
327 * nfs_client_return_marked_delegations - return previously marked delegations
328 * @clp: nfs_client to process
330 * Returns zero on success, or a negative errno value.
332 int nfs_client_return_marked_delegations(struct nfs_client
*clp
)
334 struct nfs_delegation
*delegation
;
335 struct nfs_server
*server
;
341 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
) {
342 list_for_each_entry_rcu(delegation
, &server
->delegations
,
344 if (!test_and_clear_bit(NFS_DELEGATION_RETURN
,
347 inode
= nfs_delegation_grab_inode(delegation
);
350 delegation
= nfs_detach_delegation(NFS_I(inode
),
354 if (delegation
!= NULL
) {
355 filemap_flush(inode
->i_mapping
);
356 err
= __nfs_inode_return_delegation(inode
,
362 set_bit(NFS4CLNT_DELEGRETURN
, &clp
->cl_state
);
371 * nfs_inode_return_delegation_noreclaim - return delegation, don't reclaim opens
372 * @inode: inode to process
374 * Does not protect against delegation reclaims, therefore really only safe
375 * to be called from nfs4_clear_inode().
377 void nfs_inode_return_delegation_noreclaim(struct inode
*inode
)
379 struct nfs_server
*server
= NFS_SERVER(inode
);
380 struct nfs_inode
*nfsi
= NFS_I(inode
);
381 struct nfs_delegation
*delegation
;
383 if (rcu_access_pointer(nfsi
->delegation
) != NULL
) {
384 delegation
= nfs_detach_delegation(nfsi
, server
);
385 if (delegation
!= NULL
)
386 nfs_do_return_delegation(inode
, delegation
, 0);
391 * nfs_inode_return_delegation - synchronously return a delegation
392 * @inode: inode to process
394 * Returns zero on success, or a negative errno value.
396 int nfs_inode_return_delegation(struct inode
*inode
)
398 struct nfs_server
*server
= NFS_SERVER(inode
);
399 struct nfs_inode
*nfsi
= NFS_I(inode
);
400 struct nfs_delegation
*delegation
;
403 if (rcu_access_pointer(nfsi
->delegation
) != NULL
) {
404 delegation
= nfs_detach_delegation(nfsi
, server
);
405 if (delegation
!= NULL
) {
407 err
= __nfs_inode_return_delegation(inode
, delegation
, 1);
413 static void nfs_mark_return_delegation(struct nfs_delegation
*delegation
)
415 struct nfs_client
*clp
= NFS_SERVER(delegation
->inode
)->nfs_client
;
417 set_bit(NFS_DELEGATION_RETURN
, &delegation
->flags
);
418 set_bit(NFS4CLNT_DELEGRETURN
, &clp
->cl_state
);
422 * nfs_super_return_all_delegations - return delegations for one superblock
426 void nfs_super_return_all_delegations(struct super_block
*sb
)
428 struct nfs_server
*server
= NFS_SB(sb
);
429 struct nfs_client
*clp
= server
->nfs_client
;
430 struct nfs_delegation
*delegation
;
436 list_for_each_entry_rcu(delegation
, &server
->delegations
, super_list
) {
437 spin_lock(&delegation
->lock
);
438 set_bit(NFS_DELEGATION_RETURN
, &delegation
->flags
);
439 spin_unlock(&delegation
->lock
);
443 if (nfs_client_return_marked_delegations(clp
) != 0)
444 nfs4_schedule_state_manager(clp
);
447 static void nfs_mark_return_all_delegation_types(struct nfs_server
*server
,
450 struct nfs_delegation
*delegation
;
452 list_for_each_entry_rcu(delegation
, &server
->delegations
, super_list
) {
453 if ((delegation
->type
== (FMODE_READ
|FMODE_WRITE
)) && !(flags
& FMODE_WRITE
))
455 if (delegation
->type
& flags
)
456 nfs_mark_return_delegation(delegation
);
460 static void nfs_client_mark_return_all_delegation_types(struct nfs_client
*clp
,
463 struct nfs_server
*server
;
466 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
)
467 nfs_mark_return_all_delegation_types(server
, flags
);
471 static void nfs_client_mark_return_all_delegations(struct nfs_client
*clp
)
473 nfs_client_mark_return_all_delegation_types(clp
, FMODE_READ
|FMODE_WRITE
);
476 static void nfs_delegation_run_state_manager(struct nfs_client
*clp
)
478 if (test_bit(NFS4CLNT_DELEGRETURN
, &clp
->cl_state
))
479 nfs4_schedule_state_manager(clp
);
483 * nfs_expire_all_delegation_types
484 * @clp: client to process
485 * @flags: delegation types to expire
488 void nfs_expire_all_delegation_types(struct nfs_client
*clp
, fmode_t flags
)
490 nfs_client_mark_return_all_delegation_types(clp
, flags
);
491 nfs_delegation_run_state_manager(clp
);
495 * nfs_expire_all_delegations
496 * @clp: client to process
499 void nfs_expire_all_delegations(struct nfs_client
*clp
)
501 nfs_expire_all_delegation_types(clp
, FMODE_READ
|FMODE_WRITE
);
505 * nfs_handle_cb_pathdown - return all delegations after NFS4ERR_CB_PATH_DOWN
506 * @clp: client to process
509 void nfs_handle_cb_pathdown(struct nfs_client
*clp
)
513 nfs_client_mark_return_all_delegations(clp
);
516 static void nfs_mark_return_unreferenced_delegations(struct nfs_server
*server
)
518 struct nfs_delegation
*delegation
;
520 list_for_each_entry_rcu(delegation
, &server
->delegations
, super_list
) {
521 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED
, &delegation
->flags
))
523 nfs_mark_return_delegation(delegation
);
528 * nfs_expire_unreferenced_delegations - Eliminate unused delegations
529 * @clp: nfs_client to process
532 void nfs_expire_unreferenced_delegations(struct nfs_client
*clp
)
534 struct nfs_server
*server
;
537 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
)
538 nfs_mark_return_unreferenced_delegations(server
);
541 nfs_delegation_run_state_manager(clp
);
545 * nfs_async_inode_return_delegation - asynchronously return a delegation
546 * @inode: inode to process
547 * @stateid: state ID information from CB_RECALL arguments
549 * Returns zero on success, or a negative errno value.
551 int nfs_async_inode_return_delegation(struct inode
*inode
,
552 const nfs4_stateid
*stateid
)
554 struct nfs_client
*clp
= NFS_SERVER(inode
)->nfs_client
;
555 struct nfs_delegation
*delegation
;
558 delegation
= rcu_dereference(NFS_I(inode
)->delegation
);
560 if (!clp
->cl_mvops
->validate_stateid(delegation
, stateid
)) {
564 nfs_mark_return_delegation(delegation
);
567 nfs_delegation_run_state_manager(clp
);
571 static struct inode
*
572 nfs_delegation_find_inode_server(struct nfs_server
*server
,
573 const struct nfs_fh
*fhandle
)
575 struct nfs_delegation
*delegation
;
576 struct inode
*res
= NULL
;
578 list_for_each_entry_rcu(delegation
, &server
->delegations
, super_list
) {
579 spin_lock(&delegation
->lock
);
580 if (delegation
->inode
!= NULL
&&
581 nfs_compare_fh(fhandle
, &NFS_I(delegation
->inode
)->fh
) == 0) {
582 res
= igrab(delegation
->inode
);
584 spin_unlock(&delegation
->lock
);
592 * nfs_delegation_find_inode - retrieve the inode associated with a delegation
593 * @clp: client state handle
594 * @fhandle: filehandle from a delegation recall
596 * Returns pointer to inode matching "fhandle," or NULL if a matching inode
599 struct inode
*nfs_delegation_find_inode(struct nfs_client
*clp
,
600 const struct nfs_fh
*fhandle
)
602 struct nfs_server
*server
;
603 struct inode
*res
= NULL
;
606 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
) {
607 res
= nfs_delegation_find_inode_server(server
, fhandle
);
615 static void nfs_delegation_mark_reclaim_server(struct nfs_server
*server
)
617 struct nfs_delegation
*delegation
;
619 list_for_each_entry_rcu(delegation
, &server
->delegations
, super_list
)
620 set_bit(NFS_DELEGATION_NEED_RECLAIM
, &delegation
->flags
);
624 * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
625 * @clp: nfs_client to process
628 void nfs_delegation_mark_reclaim(struct nfs_client
*clp
)
630 struct nfs_server
*server
;
633 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
)
634 nfs_delegation_mark_reclaim_server(server
);
639 * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
640 * @clp: nfs_client to process
643 void nfs_delegation_reap_unclaimed(struct nfs_client
*clp
)
645 struct nfs_delegation
*delegation
;
646 struct nfs_server
*server
;
651 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
) {
652 list_for_each_entry_rcu(delegation
, &server
->delegations
,
654 if (test_bit(NFS_DELEGATION_NEED_RECLAIM
,
655 &delegation
->flags
) == 0)
657 inode
= nfs_delegation_grab_inode(delegation
);
660 delegation
= nfs_detach_delegation(NFS_I(inode
),
664 if (delegation
!= NULL
)
665 nfs_free_delegation(delegation
);
674 * nfs_delegations_present - check for existence of delegations
675 * @clp: client state handle
677 * Returns one if there are any nfs_delegation structures attached
678 * to this nfs_client.
680 int nfs_delegations_present(struct nfs_client
*clp
)
682 struct nfs_server
*server
;
686 list_for_each_entry_rcu(server
, &clp
->cl_superblocks
, client_link
)
687 if (!list_empty(&server
->delegations
)) {
696 * nfs4_copy_delegation_stateid - Copy inode's state ID information
697 * @dst: stateid data structure to fill in
698 * @inode: inode to check
700 * Returns one and fills in "dst->data" * if inode had a delegation,
701 * otherwise zero is returned.
703 int nfs4_copy_delegation_stateid(nfs4_stateid
*dst
, struct inode
*inode
)
705 struct nfs_inode
*nfsi
= NFS_I(inode
);
706 struct nfs_delegation
*delegation
;
710 delegation
= rcu_dereference(nfsi
->delegation
);
711 if (delegation
!= NULL
) {
712 memcpy(dst
->data
, delegation
->stateid
.data
, sizeof(dst
->data
));