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 NFS_I(inode)->delegation_state = delegation->type;
179 spin_unlock(&delegation->lock);
180 put_rpccred(oldcred);
182 trace_nfs4_reclaim_delegation(inode, res->delegation_type);
184 /* We appear to have raced with a delegation return. */
185 spin_unlock(&delegation->lock);
187 nfs_inode_set_delegation(inode, cred, res);
194 static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
198 if (!test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
199 res = nfs4_proc_delegreturn(inode,
201 &delegation->stateid,
203 nfs_free_delegation(delegation);
207 static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
209 struct inode *inode = NULL;
211 spin_lock(&delegation->lock);
212 if (delegation->inode != NULL)
213 inode = igrab(delegation->inode);
214 spin_unlock(&delegation->lock);
218 static struct nfs_delegation *
219 nfs_start_delegation_return_locked(struct nfs_inode *nfsi)
221 struct nfs_delegation *ret = NULL;
222 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
224 if (delegation == NULL)
226 spin_lock(&delegation->lock);
227 if (!test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
229 spin_unlock(&delegation->lock);
234 static struct nfs_delegation *
235 nfs_start_delegation_return(struct nfs_inode *nfsi)
237 struct nfs_delegation *delegation;
240 delegation = nfs_start_delegation_return_locked(nfsi);
246 nfs_abort_delegation_return(struct nfs_delegation *delegation,
247 struct nfs_client *clp)
250 spin_lock(&delegation->lock);
251 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
252 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
253 spin_unlock(&delegation->lock);
254 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
257 static struct nfs_delegation *
258 nfs_detach_delegation_locked(struct nfs_inode *nfsi,
259 struct nfs_delegation *delegation,
260 struct nfs_client *clp)
262 struct nfs_delegation *deleg_cur =
263 rcu_dereference_protected(nfsi->delegation,
264 lockdep_is_held(&clp->cl_lock));
266 if (deleg_cur == NULL || delegation != deleg_cur)
269 spin_lock(&delegation->lock);
270 set_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
271 list_del_rcu(&delegation->super_list);
272 delegation->inode = NULL;
273 nfsi->delegation_state = 0;
274 rcu_assign_pointer(nfsi->delegation, NULL);
275 spin_unlock(&delegation->lock);
279 static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi,
280 struct nfs_delegation *delegation,
281 struct nfs_server *server)
283 struct nfs_client *clp = server->nfs_client;
285 spin_lock(&clp->cl_lock);
286 delegation = nfs_detach_delegation_locked(nfsi, delegation, clp);
287 spin_unlock(&clp->cl_lock);
291 static struct nfs_delegation *
292 nfs_inode_detach_delegation(struct inode *inode)
294 struct nfs_inode *nfsi = NFS_I(inode);
295 struct nfs_server *server = NFS_SERVER(inode);
296 struct nfs_delegation *delegation;
298 delegation = nfs_start_delegation_return(nfsi);
299 if (delegation == NULL)
301 return nfs_detach_delegation(nfsi, delegation, server);
305 * nfs_inode_set_delegation - set up a delegation on an inode
306 * @inode: inode to which delegation applies
307 * @cred: cred to use for subsequent delegation processing
308 * @res: new delegation state from server
310 * Returns zero on success, or a negative errno value.
312 int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
314 struct nfs_server *server = NFS_SERVER(inode);
315 struct nfs_client *clp = server->nfs_client;
316 struct nfs_inode *nfsi = NFS_I(inode);
317 struct nfs_delegation *delegation, *old_delegation;
318 struct nfs_delegation *freeme = NULL;
321 delegation = kmalloc(sizeof(*delegation), GFP_NOFS);
322 if (delegation == NULL)
324 nfs4_stateid_copy(&delegation->stateid, &res->delegation);
325 delegation->type = res->delegation_type;
326 delegation->maxsize = res->maxsize;
327 delegation->change_attr = inode->i_version;
328 delegation->cred = get_rpccred(cred);
329 delegation->inode = inode;
330 delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
331 spin_lock_init(&delegation->lock);
333 spin_lock(&clp->cl_lock);
334 old_delegation = rcu_dereference_protected(nfsi->delegation,
335 lockdep_is_held(&clp->cl_lock));
336 if (old_delegation != NULL) {
337 if (nfs4_stateid_match(&delegation->stateid,
338 &old_delegation->stateid) &&
339 delegation->type == old_delegation->type) {
343 * Deal with broken servers that hand out two
344 * delegations for the same file.
345 * Allow for upgrades to a WRITE delegation, but
348 dfprintk(FILE, "%s: server %s handed out "
349 "a duplicate delegation!\n",
350 __func__, clp->cl_hostname);
351 if (delegation->type == old_delegation->type ||
352 !(delegation->type & FMODE_WRITE)) {
357 freeme = nfs_detach_delegation_locked(nfsi,
358 old_delegation, clp);
362 list_add_rcu(&delegation->super_list, &server->delegations);
363 nfsi->delegation_state = delegation->type;
364 rcu_assign_pointer(nfsi->delegation, delegation);
367 /* Ensure we revalidate the attributes and page cache! */
368 spin_lock(&inode->i_lock);
369 nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
370 spin_unlock(&inode->i_lock);
371 trace_nfs4_set_delegation(inode, res->delegation_type);
374 spin_unlock(&clp->cl_lock);
375 if (delegation != NULL)
376 nfs_free_delegation(delegation);
378 nfs_do_return_delegation(inode, freeme, 0);
383 * Basic procedure for returning a delegation to the server
385 static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync)
387 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
388 struct nfs_inode *nfsi = NFS_I(inode);
391 if (delegation == NULL)
394 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
396 err = nfs_delegation_claim_opens(inode, &delegation->stateid);
397 if (!issync || err != -EAGAIN)
400 * Guard against state recovery
402 err = nfs4_wait_clnt_recover(clp);
406 nfs_abort_delegation_return(delegation, clp);
409 if (!nfs_detach_delegation(nfsi, delegation, NFS_SERVER(inode)))
412 err = nfs_do_return_delegation(inode, delegation, issync);
417 static bool nfs_delegation_need_return(struct nfs_delegation *delegation)
421 if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
423 if (test_and_clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags) && !ret) {
426 spin_lock(&delegation->lock);
427 inode = delegation->inode;
428 if (inode && list_empty(&NFS_I(inode)->open_files))
430 spin_unlock(&delegation->lock);
436 * nfs_client_return_marked_delegations - return previously marked delegations
437 * @clp: nfs_client to process
439 * Note that this function is designed to be called by the state
440 * manager thread. For this reason, it cannot flush the dirty data,
441 * since that could deadlock in case of a state recovery error.
443 * Returns zero on success, or a negative errno value.
445 int nfs_client_return_marked_delegations(struct nfs_client *clp)
447 struct nfs_delegation *delegation;
448 struct nfs_server *server;
454 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
455 list_for_each_entry_rcu(delegation, &server->delegations,
457 if (!nfs_delegation_need_return(delegation))
459 inode = nfs_delegation_grab_inode(delegation);
462 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
465 err = nfs_end_delegation_return(inode, delegation, 0);
469 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
478 * nfs_inode_return_delegation_noreclaim - return delegation, don't reclaim opens
479 * @inode: inode to process
481 * Does not protect against delegation reclaims, therefore really only safe
482 * to be called from nfs4_clear_inode().
484 void nfs_inode_return_delegation_noreclaim(struct inode *inode)
486 struct nfs_delegation *delegation;
488 delegation = nfs_inode_detach_delegation(inode);
489 if (delegation != NULL)
490 nfs_do_return_delegation(inode, delegation, 0);
494 * nfs_inode_return_delegation - synchronously return a delegation
495 * @inode: inode to process
497 * This routine will always flush any dirty data to disk on the
498 * assumption that if we need to return the delegation, then
499 * we should stop caching.
501 * Returns zero on success, or a negative errno value.
503 int nfs4_inode_return_delegation(struct inode *inode)
505 struct nfs_inode *nfsi = NFS_I(inode);
506 struct nfs_delegation *delegation;
510 delegation = nfs_start_delegation_return(nfsi);
511 if (delegation != NULL)
512 err = nfs_end_delegation_return(inode, delegation, 1);
516 static void nfs_mark_return_if_closed_delegation(struct nfs_server *server,
517 struct nfs_delegation *delegation)
519 set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
520 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
523 static void nfs_mark_return_delegation(struct nfs_server *server,
524 struct nfs_delegation *delegation)
526 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
527 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
530 static bool nfs_server_mark_return_all_delegations(struct nfs_server *server)
532 struct nfs_delegation *delegation;
535 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
536 nfs_mark_return_delegation(server, delegation);
542 static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
544 struct nfs_server *server;
547 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
548 nfs_server_mark_return_all_delegations(server);
552 static void nfs_delegation_run_state_manager(struct nfs_client *clp)
554 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
555 nfs4_schedule_state_manager(clp);
559 * nfs_expire_all_delegations
560 * @clp: client to process
563 void nfs_expire_all_delegations(struct nfs_client *clp)
565 nfs_client_mark_return_all_delegations(clp);
566 nfs_delegation_run_state_manager(clp);
570 * nfs_super_return_all_delegations - return delegations for one superblock
574 void nfs_server_return_all_delegations(struct nfs_server *server)
576 struct nfs_client *clp = server->nfs_client;
583 need_wait = nfs_server_mark_return_all_delegations(server);
587 nfs4_schedule_state_manager(clp);
588 nfs4_wait_clnt_recover(clp);
592 static void nfs_mark_return_unused_delegation_types(struct nfs_server *server,
595 struct nfs_delegation *delegation;
597 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
598 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
600 if (delegation->type & flags)
601 nfs_mark_return_if_closed_delegation(server, delegation);
605 static void nfs_client_mark_return_unused_delegation_types(struct nfs_client *clp,
608 struct nfs_server *server;
611 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
612 nfs_mark_return_unused_delegation_types(server, flags);
616 static void nfs_revoke_delegation(struct inode *inode)
618 struct nfs_delegation *delegation;
620 delegation = rcu_dereference(NFS_I(inode)->delegation);
621 if (delegation != NULL) {
622 set_bit(NFS_DELEGATION_REVOKED, &delegation->flags);
623 nfs_mark_return_delegation(NFS_SERVER(inode), delegation);
628 void nfs_remove_bad_delegation(struct inode *inode)
630 struct nfs_delegation *delegation;
632 nfs_revoke_delegation(inode);
633 delegation = nfs_inode_detach_delegation(inode);
635 nfs_inode_find_state_and_recover(inode, &delegation->stateid);
636 nfs_free_delegation(delegation);
639 EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation);
642 * nfs_expire_unused_delegation_types
643 * @clp: client to process
644 * @flags: delegation types to expire
647 void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags)
649 nfs_client_mark_return_unused_delegation_types(clp, flags);
650 nfs_delegation_run_state_manager(clp);
653 static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
655 struct nfs_delegation *delegation;
657 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
658 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
660 nfs_mark_return_if_closed_delegation(server, delegation);
665 * nfs_expire_unreferenced_delegations - Eliminate unused delegations
666 * @clp: nfs_client to process
669 void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
671 struct nfs_server *server;
674 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
675 nfs_mark_return_unreferenced_delegations(server);
678 nfs_delegation_run_state_manager(clp);
682 * nfs_async_inode_return_delegation - asynchronously return a delegation
683 * @inode: inode to process
684 * @stateid: state ID information
686 * Returns zero on success, or a negative errno value.
688 int nfs_async_inode_return_delegation(struct inode *inode,
689 const nfs4_stateid *stateid)
691 struct nfs_server *server = NFS_SERVER(inode);
692 struct nfs_client *clp = server->nfs_client;
693 struct nfs_delegation *delegation;
695 filemap_flush(inode->i_mapping);
698 delegation = rcu_dereference(NFS_I(inode)->delegation);
699 if (delegation == NULL)
702 if (!clp->cl_mvops->match_stateid(&delegation->stateid, stateid))
704 nfs_mark_return_delegation(server, delegation);
707 nfs_delegation_run_state_manager(clp);
714 static struct inode *
715 nfs_delegation_find_inode_server(struct nfs_server *server,
716 const struct nfs_fh *fhandle)
718 struct nfs_delegation *delegation;
719 struct inode *res = NULL;
721 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
722 spin_lock(&delegation->lock);
723 if (delegation->inode != NULL &&
724 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
725 res = igrab(delegation->inode);
727 spin_unlock(&delegation->lock);
735 * nfs_delegation_find_inode - retrieve the inode associated with a delegation
736 * @clp: client state handle
737 * @fhandle: filehandle from a delegation recall
739 * Returns pointer to inode matching "fhandle," or NULL if a matching inode
742 struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
743 const struct nfs_fh *fhandle)
745 struct nfs_server *server;
746 struct inode *res = NULL;
749 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
750 res = nfs_delegation_find_inode_server(server, fhandle);
758 static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
760 struct nfs_delegation *delegation;
762 list_for_each_entry_rcu(delegation, &server->delegations, super_list)
763 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
767 * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
768 * @clp: nfs_client to process
771 void nfs_delegation_mark_reclaim(struct nfs_client *clp)
773 struct nfs_server *server;
776 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
777 nfs_delegation_mark_reclaim_server(server);
782 * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
783 * @clp: nfs_client to process
786 void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
788 struct nfs_delegation *delegation;
789 struct nfs_server *server;
794 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
795 list_for_each_entry_rcu(delegation, &server->delegations,
797 if (test_bit(NFS_DELEGATION_NEED_RECLAIM,
798 &delegation->flags) == 0)
800 inode = nfs_delegation_grab_inode(delegation);
803 delegation = nfs_detach_delegation(NFS_I(inode),
807 if (delegation != NULL)
808 nfs_free_delegation(delegation);
817 * nfs_delegations_present - check for existence of delegations
818 * @clp: client state handle
820 * Returns one if there are any nfs_delegation structures attached
821 * to this nfs_client.
823 int nfs_delegations_present(struct nfs_client *clp)
825 struct nfs_server *server;
829 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
830 if (!list_empty(&server->delegations)) {
839 * nfs4_copy_delegation_stateid - Copy inode's state ID information
840 * @dst: stateid data structure to fill in
841 * @inode: inode to check
842 * @flags: delegation type requirement
844 * Returns "true" and fills in "dst->data" * if inode had a delegation,
845 * otherwise "false" is returned.
847 bool nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode,
850 struct nfs_inode *nfsi = NFS_I(inode);
851 struct nfs_delegation *delegation;
854 flags &= FMODE_READ|FMODE_WRITE;
856 delegation = rcu_dereference(nfsi->delegation);
857 ret = (delegation != NULL && (delegation->type & flags) == flags);
859 nfs4_stateid_copy(dst, &delegation->stateid);
860 nfs_mark_delegation_referenced(delegation);