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 nfs4_do_check_delegation(struct inode *inode, fmode_t flags, bool mark)
47 struct nfs_delegation *delegation;
50 flags &= FMODE_READ|FMODE_WRITE;
52 delegation = rcu_dereference(NFS_I(inode)->delegation);
53 if (delegation != NULL && (delegation->type & flags) == flags &&
54 !test_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) {
56 nfs_mark_delegation_referenced(delegation);
63 * nfs_have_delegation - check if inode has a delegation, mark it
64 * NFS_DELEGATION_REFERENCED if there is one.
65 * @inode: inode to check
66 * @flags: delegation types to check for
68 * Returns one if inode has the indicated delegation, otherwise zero.
70 int nfs4_have_delegation(struct inode *inode, fmode_t flags)
72 return nfs4_do_check_delegation(inode, flags, true);
76 * nfs4_check_delegation - check if inode has a delegation, do not mark
77 * NFS_DELEGATION_REFERENCED if it has one.
79 int nfs4_check_delegation(struct inode *inode, fmode_t flags)
81 return nfs4_do_check_delegation(inode, flags, false);
84 static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
86 struct inode *inode = state->inode;
90 if (inode->i_flock == NULL)
93 /* Protect inode->i_flock using the i_lock */
94 spin_lock(&inode->i_lock);
95 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
96 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
98 if (nfs_file_open_context(fl->fl_file) != ctx)
100 spin_unlock(&inode->i_lock);
101 status = nfs4_lock_delegation_recall(fl, state, stateid);
104 spin_lock(&inode->i_lock);
106 spin_unlock(&inode->i_lock);
111 static int nfs_delegation_claim_opens(struct inode *inode, const nfs4_stateid *stateid)
113 struct nfs_inode *nfsi = NFS_I(inode);
114 struct nfs_open_context *ctx;
115 struct nfs4_state_owner *sp;
116 struct nfs4_state *state;
121 spin_lock(&inode->i_lock);
122 list_for_each_entry(ctx, &nfsi->open_files, list) {
126 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
128 if (!nfs4_valid_open_stateid(state))
130 if (!nfs4_stateid_match(&state->stateid, stateid))
132 get_nfs_open_context(ctx);
133 spin_unlock(&inode->i_lock);
135 /* Block nfs4_proc_unlck */
136 mutex_lock(&sp->so_delegreturn_mutex);
137 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
138 err = nfs4_open_delegation_recall(ctx, state, stateid);
140 err = nfs_delegation_claim_locks(ctx, state, stateid);
141 if (!err && read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
143 mutex_unlock(&sp->so_delegreturn_mutex);
144 put_nfs_open_context(ctx);
149 spin_unlock(&inode->i_lock);
154 * nfs_inode_reclaim_delegation - process a delegation reclaim request
155 * @inode: inode to process
156 * @cred: credential to use for request
157 * @res: new delegation state from server
160 void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred,
161 struct nfs_openres *res)
163 struct nfs_delegation *delegation;
164 struct rpc_cred *oldcred = NULL;
167 delegation = rcu_dereference(NFS_I(inode)->delegation);
168 if (delegation != NULL) {
169 spin_lock(&delegation->lock);
170 if (delegation->inode != NULL) {
171 nfs4_stateid_copy(&delegation->stateid, &res->delegation);
172 delegation->type = res->delegation_type;
173 delegation->maxsize = res->maxsize;
174 oldcred = delegation->cred;
175 delegation->cred = get_rpccred(cred);
176 clear_bit(NFS_DELEGATION_NEED_RECLAIM,
178 spin_unlock(&delegation->lock);
179 put_rpccred(oldcred);
181 trace_nfs4_reclaim_delegation(inode, res->delegation_type);
183 /* We appear to have raced with a delegation return. */
184 spin_unlock(&delegation->lock);
186 nfs_inode_set_delegation(inode, cred, res);
193 static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
197 if (!test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
198 res = nfs4_proc_delegreturn(inode,
200 &delegation->stateid,
202 nfs_free_delegation(delegation);
206 static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
208 struct inode *inode = NULL;
210 spin_lock(&delegation->lock);
211 if (delegation->inode != NULL)
212 inode = igrab(delegation->inode);
213 spin_unlock(&delegation->lock);
217 static struct nfs_delegation *
218 nfs_start_delegation_return_locked(struct nfs_inode *nfsi)
220 struct nfs_delegation *ret = NULL;
221 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
223 if (delegation == NULL)
225 spin_lock(&delegation->lock);
226 if (!test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
228 spin_unlock(&delegation->lock);
233 static struct nfs_delegation *
234 nfs_start_delegation_return(struct nfs_inode *nfsi)
236 struct nfs_delegation *delegation;
239 delegation = nfs_start_delegation_return_locked(nfsi);
245 nfs_abort_delegation_return(struct nfs_delegation *delegation,
246 struct nfs_client *clp)
249 spin_lock(&delegation->lock);
250 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
251 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
252 spin_unlock(&delegation->lock);
253 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
256 static struct nfs_delegation *
257 nfs_detach_delegation_locked(struct nfs_inode *nfsi,
258 struct nfs_delegation *delegation,
259 struct nfs_client *clp)
261 struct nfs_delegation *deleg_cur =
262 rcu_dereference_protected(nfsi->delegation,
263 lockdep_is_held(&clp->cl_lock));
265 if (deleg_cur == NULL || delegation != deleg_cur)
268 spin_lock(&delegation->lock);
269 set_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
270 list_del_rcu(&delegation->super_list);
271 delegation->inode = NULL;
272 rcu_assign_pointer(nfsi->delegation, NULL);
273 spin_unlock(&delegation->lock);
277 static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi,
278 struct nfs_delegation *delegation,
279 struct nfs_server *server)
281 struct nfs_client *clp = server->nfs_client;
283 spin_lock(&clp->cl_lock);
284 delegation = nfs_detach_delegation_locked(nfsi, delegation, clp);
285 spin_unlock(&clp->cl_lock);
289 static struct nfs_delegation *
290 nfs_inode_detach_delegation(struct inode *inode)
292 struct nfs_inode *nfsi = NFS_I(inode);
293 struct nfs_server *server = NFS_SERVER(inode);
294 struct nfs_delegation *delegation;
296 delegation = nfs_start_delegation_return(nfsi);
297 if (delegation == NULL)
299 return nfs_detach_delegation(nfsi, delegation, server);
303 nfs_update_inplace_delegation(struct nfs_delegation *delegation,
304 const struct nfs_delegation *update)
306 if (nfs4_stateid_is_newer(&update->stateid, &delegation->stateid)) {
307 delegation->stateid.seqid = update->stateid.seqid;
309 delegation->type = update->type;
314 * nfs_inode_set_delegation - set up a delegation on an inode
315 * @inode: inode to which delegation applies
316 * @cred: cred to use for subsequent delegation processing
317 * @res: new delegation state from server
319 * Returns zero on success, or a negative errno value.
321 int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
323 struct nfs_server *server = NFS_SERVER(inode);
324 struct nfs_client *clp = server->nfs_client;
325 struct nfs_inode *nfsi = NFS_I(inode);
326 struct nfs_delegation *delegation, *old_delegation;
327 struct nfs_delegation *freeme = NULL;
330 delegation = kmalloc(sizeof(*delegation), GFP_NOFS);
331 if (delegation == NULL)
333 nfs4_stateid_copy(&delegation->stateid, &res->delegation);
334 delegation->type = res->delegation_type;
335 delegation->maxsize = res->maxsize;
336 delegation->change_attr = inode->i_version;
337 delegation->cred = get_rpccred(cred);
338 delegation->inode = inode;
339 delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
340 spin_lock_init(&delegation->lock);
342 spin_lock(&clp->cl_lock);
343 old_delegation = rcu_dereference_protected(nfsi->delegation,
344 lockdep_is_held(&clp->cl_lock));
345 if (old_delegation != NULL) {
346 /* Is this an update of the existing delegation? */
347 if (nfs4_stateid_match_other(&old_delegation->stateid,
348 &delegation->stateid)) {
349 nfs_update_inplace_delegation(old_delegation,
354 * Deal with broken servers that hand out two
355 * delegations for the same file.
356 * Allow for upgrades to a WRITE delegation, but
359 dfprintk(FILE, "%s: server %s handed out "
360 "a duplicate delegation!\n",
361 __func__, clp->cl_hostname);
362 if (delegation->type == old_delegation->type ||
363 !(delegation->type & FMODE_WRITE)) {
368 freeme = nfs_detach_delegation_locked(nfsi,
369 old_delegation, clp);
373 list_add_rcu(&delegation->super_list, &server->delegations);
374 rcu_assign_pointer(nfsi->delegation, delegation);
377 /* Ensure we revalidate the attributes and page cache! */
378 spin_lock(&inode->i_lock);
379 nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
380 spin_unlock(&inode->i_lock);
381 trace_nfs4_set_delegation(inode, res->delegation_type);
384 spin_unlock(&clp->cl_lock);
385 if (delegation != NULL)
386 nfs_free_delegation(delegation);
388 nfs_do_return_delegation(inode, freeme, 0);
393 * Basic procedure for returning a delegation to the server
395 static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync)
397 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
398 struct nfs_inode *nfsi = NFS_I(inode);
401 if (delegation == NULL)
404 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
406 err = nfs_delegation_claim_opens(inode, &delegation->stateid);
407 if (!issync || err != -EAGAIN)
410 * Guard against state recovery
412 err = nfs4_wait_clnt_recover(clp);
416 nfs_abort_delegation_return(delegation, clp);
419 if (!nfs_detach_delegation(nfsi, delegation, NFS_SERVER(inode)))
422 err = nfs_do_return_delegation(inode, delegation, issync);
427 static bool nfs_delegation_need_return(struct nfs_delegation *delegation)
431 if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
433 if (test_and_clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags) && !ret) {
436 spin_lock(&delegation->lock);
437 inode = delegation->inode;
438 if (inode && list_empty(&NFS_I(inode)->open_files))
440 spin_unlock(&delegation->lock);
446 * nfs_client_return_marked_delegations - return previously marked delegations
447 * @clp: nfs_client to process
449 * Note that this function is designed to be called by the state
450 * manager thread. For this reason, it cannot flush the dirty data,
451 * since that could deadlock in case of a state recovery error.
453 * Returns zero on success, or a negative errno value.
455 int nfs_client_return_marked_delegations(struct nfs_client *clp)
457 struct nfs_delegation *delegation;
458 struct nfs_server *server;
464 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
465 list_for_each_entry_rcu(delegation, &server->delegations,
467 if (!nfs_delegation_need_return(delegation))
469 inode = nfs_delegation_grab_inode(delegation);
472 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
475 err = nfs_end_delegation_return(inode, delegation, 0);
479 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
488 * nfs_inode_return_delegation_noreclaim - return delegation, don't reclaim opens
489 * @inode: inode to process
491 * Does not protect against delegation reclaims, therefore really only safe
492 * to be called from nfs4_clear_inode().
494 void nfs_inode_return_delegation_noreclaim(struct inode *inode)
496 struct nfs_delegation *delegation;
498 delegation = nfs_inode_detach_delegation(inode);
499 if (delegation != NULL)
500 nfs_do_return_delegation(inode, delegation, 0);
504 * nfs_inode_return_delegation - synchronously return a delegation
505 * @inode: inode to process
507 * This routine will always flush any dirty data to disk on the
508 * assumption that if we need to return the delegation, then
509 * we should stop caching.
511 * Returns zero on success, or a negative errno value.
513 int nfs4_inode_return_delegation(struct inode *inode)
515 struct nfs_inode *nfsi = NFS_I(inode);
516 struct nfs_delegation *delegation;
520 delegation = nfs_start_delegation_return(nfsi);
521 if (delegation != NULL)
522 err = nfs_end_delegation_return(inode, delegation, 1);
526 static void nfs_mark_return_if_closed_delegation(struct nfs_server *server,
527 struct nfs_delegation *delegation)
529 set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
530 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
533 static void nfs_mark_return_delegation(struct nfs_server *server,
534 struct nfs_delegation *delegation)
536 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
537 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
540 static bool nfs_server_mark_return_all_delegations(struct nfs_server *server)
542 struct nfs_delegation *delegation;
545 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
546 nfs_mark_return_delegation(server, delegation);
552 static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
554 struct nfs_server *server;
557 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
558 nfs_server_mark_return_all_delegations(server);
562 static void nfs_delegation_run_state_manager(struct nfs_client *clp)
564 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
565 nfs4_schedule_state_manager(clp);
569 * nfs_expire_all_delegations
570 * @clp: client to process
573 void nfs_expire_all_delegations(struct nfs_client *clp)
575 nfs_client_mark_return_all_delegations(clp);
576 nfs_delegation_run_state_manager(clp);
580 * nfs_super_return_all_delegations - return delegations for one superblock
584 void nfs_server_return_all_delegations(struct nfs_server *server)
586 struct nfs_client *clp = server->nfs_client;
593 need_wait = nfs_server_mark_return_all_delegations(server);
597 nfs4_schedule_state_manager(clp);
598 nfs4_wait_clnt_recover(clp);
602 static void nfs_mark_return_unused_delegation_types(struct nfs_server *server,
605 struct nfs_delegation *delegation;
607 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
608 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
610 if (delegation->type & flags)
611 nfs_mark_return_if_closed_delegation(server, delegation);
615 static void nfs_client_mark_return_unused_delegation_types(struct nfs_client *clp,
618 struct nfs_server *server;
621 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
622 nfs_mark_return_unused_delegation_types(server, flags);
626 static void nfs_revoke_delegation(struct inode *inode)
628 struct nfs_delegation *delegation;
630 delegation = rcu_dereference(NFS_I(inode)->delegation);
631 if (delegation != NULL) {
632 set_bit(NFS_DELEGATION_REVOKED, &delegation->flags);
633 nfs_mark_return_delegation(NFS_SERVER(inode), delegation);
638 void nfs_remove_bad_delegation(struct inode *inode)
640 struct nfs_delegation *delegation;
642 nfs_revoke_delegation(inode);
643 delegation = nfs_inode_detach_delegation(inode);
645 nfs_inode_find_state_and_recover(inode, &delegation->stateid);
646 nfs_free_delegation(delegation);
649 EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation);
652 * nfs_expire_unused_delegation_types
653 * @clp: client to process
654 * @flags: delegation types to expire
657 void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags)
659 nfs_client_mark_return_unused_delegation_types(clp, flags);
660 nfs_delegation_run_state_manager(clp);
663 static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
665 struct nfs_delegation *delegation;
667 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
668 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
670 nfs_mark_return_if_closed_delegation(server, delegation);
675 * nfs_expire_unreferenced_delegations - Eliminate unused delegations
676 * @clp: nfs_client to process
679 void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
681 struct nfs_server *server;
684 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
685 nfs_mark_return_unreferenced_delegations(server);
688 nfs_delegation_run_state_manager(clp);
692 * nfs_async_inode_return_delegation - asynchronously return a delegation
693 * @inode: inode to process
694 * @stateid: state ID information
696 * Returns zero on success, or a negative errno value.
698 int nfs_async_inode_return_delegation(struct inode *inode,
699 const nfs4_stateid *stateid)
701 struct nfs_server *server = NFS_SERVER(inode);
702 struct nfs_client *clp = server->nfs_client;
703 struct nfs_delegation *delegation;
705 filemap_flush(inode->i_mapping);
708 delegation = rcu_dereference(NFS_I(inode)->delegation);
709 if (delegation == NULL)
712 if (!clp->cl_mvops->match_stateid(&delegation->stateid, stateid))
714 nfs_mark_return_delegation(server, delegation);
717 nfs_delegation_run_state_manager(clp);
724 static struct inode *
725 nfs_delegation_find_inode_server(struct nfs_server *server,
726 const struct nfs_fh *fhandle)
728 struct nfs_delegation *delegation;
729 struct inode *res = NULL;
731 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
732 spin_lock(&delegation->lock);
733 if (delegation->inode != NULL &&
734 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
735 res = igrab(delegation->inode);
737 spin_unlock(&delegation->lock);
745 * nfs_delegation_find_inode - retrieve the inode associated with a delegation
746 * @clp: client state handle
747 * @fhandle: filehandle from a delegation recall
749 * Returns pointer to inode matching "fhandle," or NULL if a matching inode
752 struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
753 const struct nfs_fh *fhandle)
755 struct nfs_server *server;
756 struct inode *res = NULL;
759 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
760 res = nfs_delegation_find_inode_server(server, fhandle);
768 static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
770 struct nfs_delegation *delegation;
772 list_for_each_entry_rcu(delegation, &server->delegations, super_list)
773 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
777 * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
778 * @clp: nfs_client to process
781 void nfs_delegation_mark_reclaim(struct nfs_client *clp)
783 struct nfs_server *server;
786 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
787 nfs_delegation_mark_reclaim_server(server);
792 * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
793 * @clp: nfs_client to process
796 void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
798 struct nfs_delegation *delegation;
799 struct nfs_server *server;
804 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
805 list_for_each_entry_rcu(delegation, &server->delegations,
807 if (test_bit(NFS_DELEGATION_NEED_RECLAIM,
808 &delegation->flags) == 0)
810 inode = nfs_delegation_grab_inode(delegation);
813 delegation = nfs_detach_delegation(NFS_I(inode),
817 if (delegation != NULL)
818 nfs_free_delegation(delegation);
827 * nfs_delegations_present - check for existence of delegations
828 * @clp: client state handle
830 * Returns one if there are any nfs_delegation structures attached
831 * to this nfs_client.
833 int nfs_delegations_present(struct nfs_client *clp)
835 struct nfs_server *server;
839 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
840 if (!list_empty(&server->delegations)) {
849 * nfs4_copy_delegation_stateid - Copy inode's state ID information
850 * @dst: stateid data structure to fill in
851 * @inode: inode to check
852 * @flags: delegation type requirement
854 * Returns "true" and fills in "dst->data" * if inode had a delegation,
855 * otherwise "false" is returned.
857 bool nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode,
860 struct nfs_inode *nfsi = NFS_I(inode);
861 struct nfs_delegation *delegation;
864 flags &= FMODE_READ|FMODE_WRITE;
866 delegation = rcu_dereference(nfsi->delegation);
867 ret = (delegation != NULL && (delegation->type & flags) == flags);
869 nfs4_stateid_copy(dst, &delegation->stateid);
870 nfs_mark_delegation_referenced(delegation);