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"
23 #include "nfs4trace.h"
25 static void nfs_free_delegation(struct nfs_delegation *delegation)
27 if (delegation->cred) {
28 put_rpccred(delegation->cred);
29 delegation->cred = NULL;
31 kfree_rcu(delegation, rcu);
35 * nfs_mark_delegation_referenced - set delegation's REFERENCED flag
36 * @delegation: delegation to process
39 void nfs_mark_delegation_referenced(struct nfs_delegation *delegation)
41 set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags);
45 * nfs_have_delegation - check if inode has a delegation
46 * @inode: inode to check
47 * @flags: delegation types to check for
49 * Returns one if inode has the indicated delegation, otherwise zero.
51 int nfs4_have_delegation(struct inode *inode, fmode_t flags)
53 struct nfs_delegation *delegation;
56 flags &= FMODE_READ|FMODE_WRITE;
58 delegation = rcu_dereference(NFS_I(inode)->delegation);
59 if (delegation != NULL && (delegation->type & flags) == flags &&
60 !test_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) {
61 nfs_mark_delegation_referenced(delegation);
68 static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
70 struct inode *inode = state->inode;
74 if (inode->i_flock == NULL)
77 /* Protect inode->i_flock using the i_lock */
78 spin_lock(&inode->i_lock);
79 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
80 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
82 if (nfs_file_open_context(fl->fl_file) != ctx)
84 spin_unlock(&inode->i_lock);
85 status = nfs4_lock_delegation_recall(fl, state, stateid);
88 spin_lock(&inode->i_lock);
90 spin_unlock(&inode->i_lock);
95 static int nfs_delegation_claim_opens(struct inode *inode, const nfs4_stateid *stateid)
97 struct nfs_inode *nfsi = NFS_I(inode);
98 struct nfs_open_context *ctx;
99 struct nfs4_state_owner *sp;
100 struct nfs4_state *state;
105 spin_lock(&inode->i_lock);
106 list_for_each_entry(ctx, &nfsi->open_files, list) {
110 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
112 if (!nfs4_valid_open_stateid(state))
114 if (!nfs4_stateid_match(&state->stateid, stateid))
116 get_nfs_open_context(ctx);
117 spin_unlock(&inode->i_lock);
119 /* Block nfs4_proc_unlck */
120 mutex_lock(&sp->so_delegreturn_mutex);
121 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
122 err = nfs4_open_delegation_recall(ctx, state, stateid);
124 err = nfs_delegation_claim_locks(ctx, state, stateid);
125 if (!err && read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
127 mutex_unlock(&sp->so_delegreturn_mutex);
128 put_nfs_open_context(ctx);
133 spin_unlock(&inode->i_lock);
138 * nfs_inode_reclaim_delegation - process a delegation reclaim request
139 * @inode: inode to process
140 * @cred: credential to use for request
141 * @res: new delegation state from server
144 void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred,
145 struct nfs_openres *res)
147 struct nfs_delegation *delegation;
148 struct rpc_cred *oldcred = NULL;
151 delegation = rcu_dereference(NFS_I(inode)->delegation);
152 if (delegation != NULL) {
153 spin_lock(&delegation->lock);
154 if (delegation->inode != NULL) {
155 nfs4_stateid_copy(&delegation->stateid, &res->delegation);
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);
166 trace_nfs4_reclaim_delegation(inode, res->delegation_type);
168 /* We appear to have raced with a delegation return. */
169 spin_unlock(&delegation->lock);
171 nfs_inode_set_delegation(inode, cred, res);
178 static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
182 if (!test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
183 res = nfs4_proc_delegreturn(inode,
185 &delegation->stateid,
187 nfs_free_delegation(delegation);
191 static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
193 struct inode *inode = NULL;
195 spin_lock(&delegation->lock);
196 if (delegation->inode != NULL)
197 inode = igrab(delegation->inode);
198 spin_unlock(&delegation->lock);
202 static struct nfs_delegation *
203 nfs_start_delegation_return_locked(struct nfs_inode *nfsi)
205 struct nfs_delegation *ret = NULL;
206 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
208 if (delegation == NULL)
210 spin_lock(&delegation->lock);
211 if (!test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
213 spin_unlock(&delegation->lock);
218 static struct nfs_delegation *
219 nfs_start_delegation_return(struct nfs_inode *nfsi)
221 struct nfs_delegation *delegation;
224 delegation = nfs_start_delegation_return_locked(nfsi);
230 nfs_abort_delegation_return(struct nfs_delegation *delegation,
231 struct nfs_client *clp)
234 spin_lock(&delegation->lock);
235 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
236 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
237 spin_unlock(&delegation->lock);
238 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
241 static struct nfs_delegation *
242 nfs_detach_delegation_locked(struct nfs_inode *nfsi,
243 struct nfs_delegation *delegation,
244 struct nfs_client *clp)
246 struct nfs_delegation *deleg_cur =
247 rcu_dereference_protected(nfsi->delegation,
248 lockdep_is_held(&clp->cl_lock));
250 if (deleg_cur == NULL || delegation != deleg_cur)
253 spin_lock(&delegation->lock);
254 set_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
255 list_del_rcu(&delegation->super_list);
256 delegation->inode = NULL;
257 nfsi->delegation_state = 0;
258 rcu_assign_pointer(nfsi->delegation, NULL);
259 spin_unlock(&delegation->lock);
263 static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi,
264 struct nfs_delegation *delegation,
265 struct nfs_server *server)
267 struct nfs_client *clp = server->nfs_client;
269 spin_lock(&clp->cl_lock);
270 delegation = nfs_detach_delegation_locked(nfsi, delegation, clp);
271 spin_unlock(&clp->cl_lock);
275 static struct nfs_delegation *
276 nfs_inode_detach_delegation(struct inode *inode)
278 struct nfs_inode *nfsi = NFS_I(inode);
279 struct nfs_server *server = NFS_SERVER(inode);
280 struct nfs_delegation *delegation;
282 delegation = nfs_start_delegation_return(nfsi);
283 if (delegation == NULL)
285 return nfs_detach_delegation(nfsi, delegation, server);
289 * nfs_inode_set_delegation - set up a delegation on an inode
290 * @inode: inode to which delegation applies
291 * @cred: cred to use for subsequent delegation processing
292 * @res: new delegation state from server
294 * Returns zero on success, or a negative errno value.
296 int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
298 struct nfs_server *server = NFS_SERVER(inode);
299 struct nfs_client *clp = server->nfs_client;
300 struct nfs_inode *nfsi = NFS_I(inode);
301 struct nfs_delegation *delegation, *old_delegation;
302 struct nfs_delegation *freeme = NULL;
305 delegation = kmalloc(sizeof(*delegation), GFP_NOFS);
306 if (delegation == NULL)
308 nfs4_stateid_copy(&delegation->stateid, &res->delegation);
309 delegation->type = res->delegation_type;
310 delegation->maxsize = res->maxsize;
311 delegation->change_attr = inode->i_version;
312 delegation->cred = get_rpccred(cred);
313 delegation->inode = inode;
314 delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
315 spin_lock_init(&delegation->lock);
317 spin_lock(&clp->cl_lock);
318 old_delegation = rcu_dereference_protected(nfsi->delegation,
319 lockdep_is_held(&clp->cl_lock));
320 if (old_delegation != NULL) {
321 if (nfs4_stateid_match(&delegation->stateid,
322 &old_delegation->stateid) &&
323 delegation->type == old_delegation->type) {
327 * Deal with broken servers that hand out two
328 * delegations for the same file.
329 * Allow for upgrades to a WRITE delegation, but
332 dfprintk(FILE, "%s: server %s handed out "
333 "a duplicate delegation!\n",
334 __func__, clp->cl_hostname);
335 if (delegation->type == old_delegation->type ||
336 !(delegation->type & FMODE_WRITE)) {
341 freeme = nfs_detach_delegation_locked(nfsi,
342 old_delegation, clp);
346 list_add_rcu(&delegation->super_list, &server->delegations);
347 nfsi->delegation_state = delegation->type;
348 rcu_assign_pointer(nfsi->delegation, delegation);
351 /* Ensure we revalidate the attributes and page cache! */
352 spin_lock(&inode->i_lock);
353 nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
354 spin_unlock(&inode->i_lock);
355 trace_nfs4_set_delegation(inode, res->delegation_type);
358 spin_unlock(&clp->cl_lock);
359 if (delegation != NULL)
360 nfs_free_delegation(delegation);
362 nfs_do_return_delegation(inode, freeme, 0);
367 * Basic procedure for returning a delegation to the server
369 static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync)
371 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
372 struct nfs_inode *nfsi = NFS_I(inode);
375 if (delegation == NULL)
378 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
380 err = nfs_delegation_claim_opens(inode, &delegation->stateid);
381 if (!issync || err != -EAGAIN)
384 * Guard against state recovery
386 err = nfs4_wait_clnt_recover(clp);
390 nfs_abort_delegation_return(delegation, clp);
393 if (!nfs_detach_delegation(nfsi, delegation, NFS_SERVER(inode)))
396 err = nfs_do_return_delegation(inode, delegation, issync);
401 static bool nfs_delegation_need_return(struct nfs_delegation *delegation)
405 if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
407 if (test_and_clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags) && !ret) {
410 spin_lock(&delegation->lock);
411 inode = delegation->inode;
412 if (inode && list_empty(&NFS_I(inode)->open_files))
414 spin_unlock(&delegation->lock);
420 * nfs_client_return_marked_delegations - return previously marked delegations
421 * @clp: nfs_client to process
423 * Note that this function is designed to be called by the state
424 * manager thread. For this reason, it cannot flush the dirty data,
425 * since that could deadlock in case of a state recovery error.
427 * Returns zero on success, or a negative errno value.
429 int nfs_client_return_marked_delegations(struct nfs_client *clp)
431 struct nfs_delegation *delegation;
432 struct nfs_server *server;
438 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
439 list_for_each_entry_rcu(delegation, &server->delegations,
441 if (!nfs_delegation_need_return(delegation))
443 inode = nfs_delegation_grab_inode(delegation);
446 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
449 err = nfs_end_delegation_return(inode, delegation, 0);
453 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
462 * nfs_inode_return_delegation_noreclaim - return delegation, don't reclaim opens
463 * @inode: inode to process
465 * Does not protect against delegation reclaims, therefore really only safe
466 * to be called from nfs4_clear_inode().
468 void nfs_inode_return_delegation_noreclaim(struct inode *inode)
470 struct nfs_delegation *delegation;
472 delegation = nfs_inode_detach_delegation(inode);
473 if (delegation != NULL)
474 nfs_do_return_delegation(inode, delegation, 0);
478 * nfs_inode_return_delegation - synchronously return a delegation
479 * @inode: inode to process
481 * This routine will always flush any dirty data to disk on the
482 * assumption that if we need to return the delegation, then
483 * we should stop caching.
485 * Returns zero on success, or a negative errno value.
487 int nfs4_inode_return_delegation(struct inode *inode)
489 struct nfs_inode *nfsi = NFS_I(inode);
490 struct nfs_delegation *delegation;
494 delegation = nfs_start_delegation_return(nfsi);
495 if (delegation != NULL)
496 err = nfs_end_delegation_return(inode, delegation, 1);
500 static void nfs_mark_return_if_closed_delegation(struct nfs_server *server,
501 struct nfs_delegation *delegation)
503 set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
504 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
507 static void nfs_mark_return_delegation(struct nfs_server *server,
508 struct nfs_delegation *delegation)
510 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
511 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
514 static bool nfs_server_mark_return_all_delegations(struct nfs_server *server)
516 struct nfs_delegation *delegation;
519 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
520 nfs_mark_return_delegation(server, delegation);
526 static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
528 struct nfs_server *server;
531 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
532 nfs_server_mark_return_all_delegations(server);
536 static void nfs_delegation_run_state_manager(struct nfs_client *clp)
538 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
539 nfs4_schedule_state_manager(clp);
543 * nfs_expire_all_delegations
544 * @clp: client to process
547 void nfs_expire_all_delegations(struct nfs_client *clp)
549 nfs_client_mark_return_all_delegations(clp);
550 nfs_delegation_run_state_manager(clp);
554 * nfs_super_return_all_delegations - return delegations for one superblock
558 void nfs_server_return_all_delegations(struct nfs_server *server)
560 struct nfs_client *clp = server->nfs_client;
567 need_wait = nfs_server_mark_return_all_delegations(server);
571 nfs4_schedule_state_manager(clp);
572 nfs4_wait_clnt_recover(clp);
576 static void nfs_mark_return_unused_delegation_types(struct nfs_server *server,
579 struct nfs_delegation *delegation;
581 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
582 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
584 if (delegation->type & flags)
585 nfs_mark_return_if_closed_delegation(server, delegation);
589 static void nfs_client_mark_return_unused_delegation_types(struct nfs_client *clp,
592 struct nfs_server *server;
595 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
596 nfs_mark_return_unused_delegation_types(server, flags);
600 static void nfs_revoke_delegation(struct inode *inode)
602 struct nfs_delegation *delegation;
604 delegation = rcu_dereference(NFS_I(inode)->delegation);
605 if (delegation != NULL) {
606 set_bit(NFS_DELEGATION_REVOKED, &delegation->flags);
607 nfs_mark_return_delegation(NFS_SERVER(inode), delegation);
612 void nfs_remove_bad_delegation(struct inode *inode)
614 struct nfs_delegation *delegation;
616 nfs_revoke_delegation(inode);
617 delegation = nfs_inode_detach_delegation(inode);
619 nfs_inode_find_state_and_recover(inode, &delegation->stateid);
620 nfs_free_delegation(delegation);
623 EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation);
626 * nfs_expire_unused_delegation_types
627 * @clp: client to process
628 * @flags: delegation types to expire
631 void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags)
633 nfs_client_mark_return_unused_delegation_types(clp, flags);
634 nfs_delegation_run_state_manager(clp);
637 static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
639 struct nfs_delegation *delegation;
641 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
642 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
644 nfs_mark_return_if_closed_delegation(server, delegation);
649 * nfs_expire_unreferenced_delegations - Eliminate unused delegations
650 * @clp: nfs_client to process
653 void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
655 struct nfs_server *server;
658 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
659 nfs_mark_return_unreferenced_delegations(server);
662 nfs_delegation_run_state_manager(clp);
666 * nfs_async_inode_return_delegation - asynchronously return a delegation
667 * @inode: inode to process
668 * @stateid: state ID information
670 * Returns zero on success, or a negative errno value.
672 int nfs_async_inode_return_delegation(struct inode *inode,
673 const nfs4_stateid *stateid)
675 struct nfs_server *server = NFS_SERVER(inode);
676 struct nfs_client *clp = server->nfs_client;
677 struct nfs_delegation *delegation;
679 filemap_flush(inode->i_mapping);
682 delegation = rcu_dereference(NFS_I(inode)->delegation);
683 if (delegation == NULL)
686 if (!clp->cl_mvops->match_stateid(&delegation->stateid, stateid))
688 nfs_mark_return_delegation(server, delegation);
691 nfs_delegation_run_state_manager(clp);
698 static struct inode *
699 nfs_delegation_find_inode_server(struct nfs_server *server,
700 const struct nfs_fh *fhandle)
702 struct nfs_delegation *delegation;
703 struct inode *res = NULL;
705 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
706 spin_lock(&delegation->lock);
707 if (delegation->inode != NULL &&
708 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
709 res = igrab(delegation->inode);
711 spin_unlock(&delegation->lock);
719 * nfs_delegation_find_inode - retrieve the inode associated with a delegation
720 * @clp: client state handle
721 * @fhandle: filehandle from a delegation recall
723 * Returns pointer to inode matching "fhandle," or NULL if a matching inode
726 struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
727 const struct nfs_fh *fhandle)
729 struct nfs_server *server;
730 struct inode *res = NULL;
733 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
734 res = nfs_delegation_find_inode_server(server, fhandle);
742 static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
744 struct nfs_delegation *delegation;
746 list_for_each_entry_rcu(delegation, &server->delegations, super_list)
747 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
751 * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
752 * @clp: nfs_client to process
755 void nfs_delegation_mark_reclaim(struct nfs_client *clp)
757 struct nfs_server *server;
760 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
761 nfs_delegation_mark_reclaim_server(server);
766 * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
767 * @clp: nfs_client to process
770 void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
772 struct nfs_delegation *delegation;
773 struct nfs_server *server;
778 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
779 list_for_each_entry_rcu(delegation, &server->delegations,
781 if (test_bit(NFS_DELEGATION_NEED_RECLAIM,
782 &delegation->flags) == 0)
784 inode = nfs_delegation_grab_inode(delegation);
787 delegation = nfs_detach_delegation(NFS_I(inode),
791 if (delegation != NULL)
792 nfs_free_delegation(delegation);
801 * nfs_delegations_present - check for existence of delegations
802 * @clp: client state handle
804 * Returns one if there are any nfs_delegation structures attached
805 * to this nfs_client.
807 int nfs_delegations_present(struct nfs_client *clp)
809 struct nfs_server *server;
813 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
814 if (!list_empty(&server->delegations)) {
823 * nfs4_copy_delegation_stateid - Copy inode's state ID information
824 * @dst: stateid data structure to fill in
825 * @inode: inode to check
826 * @flags: delegation type requirement
828 * Returns "true" and fills in "dst->data" * if inode had a delegation,
829 * otherwise "false" is returned.
831 bool nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode,
834 struct nfs_inode *nfsi = NFS_I(inode);
835 struct nfs_delegation *delegation;
838 flags &= FMODE_READ|FMODE_WRITE;
840 delegation = rcu_dereference(nfsi->delegation);
841 ret = (delegation != NULL && (delegation->type & flags) == flags);
843 nfs4_stateid_copy(dst, &delegation->stateid);
844 nfs_mark_delegation_referenced(delegation);