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 res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid, issync);
183 nfs_free_delegation(delegation);
187 static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
189 struct inode *inode = NULL;
191 spin_lock(&delegation->lock);
192 if (delegation->inode != NULL)
193 inode = igrab(delegation->inode);
194 spin_unlock(&delegation->lock);
198 static struct nfs_delegation *
199 nfs_start_delegation_return_locked(struct nfs_inode *nfsi)
201 struct nfs_delegation *ret = NULL;
202 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
204 if (delegation == NULL)
206 spin_lock(&delegation->lock);
207 if (!test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
209 spin_unlock(&delegation->lock);
214 static struct nfs_delegation *
215 nfs_start_delegation_return(struct nfs_inode *nfsi)
217 struct nfs_delegation *delegation;
220 delegation = nfs_start_delegation_return_locked(nfsi);
226 nfs_abort_delegation_return(struct nfs_delegation *delegation,
227 struct nfs_client *clp)
230 spin_lock(&delegation->lock);
231 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
232 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
233 spin_unlock(&delegation->lock);
234 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
237 static struct nfs_delegation *
238 nfs_detach_delegation_locked(struct nfs_inode *nfsi,
239 struct nfs_delegation *delegation,
240 struct nfs_client *clp)
242 struct nfs_delegation *deleg_cur =
243 rcu_dereference_protected(nfsi->delegation,
244 lockdep_is_held(&clp->cl_lock));
246 if (deleg_cur == NULL || delegation != deleg_cur)
249 spin_lock(&delegation->lock);
250 set_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
251 list_del_rcu(&delegation->super_list);
252 delegation->inode = NULL;
253 nfsi->delegation_state = 0;
254 rcu_assign_pointer(nfsi->delegation, NULL);
255 spin_unlock(&delegation->lock);
259 static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi,
260 struct nfs_delegation *delegation,
261 struct nfs_server *server)
263 struct nfs_client *clp = server->nfs_client;
265 spin_lock(&clp->cl_lock);
266 delegation = nfs_detach_delegation_locked(nfsi, delegation, clp);
267 spin_unlock(&clp->cl_lock);
271 static struct nfs_delegation *
272 nfs_inode_detach_delegation(struct inode *inode)
274 struct nfs_inode *nfsi = NFS_I(inode);
275 struct nfs_server *server = NFS_SERVER(inode);
276 struct nfs_delegation *delegation;
278 delegation = nfs_start_delegation_return(nfsi);
279 if (delegation == NULL)
281 return nfs_detach_delegation(nfsi, delegation, server);
285 * nfs_inode_set_delegation - set up a delegation on an inode
286 * @inode: inode to which delegation applies
287 * @cred: cred to use for subsequent delegation processing
288 * @res: new delegation state from server
290 * Returns zero on success, or a negative errno value.
292 int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
294 struct nfs_server *server = NFS_SERVER(inode);
295 struct nfs_client *clp = server->nfs_client;
296 struct nfs_inode *nfsi = NFS_I(inode);
297 struct nfs_delegation *delegation, *old_delegation;
298 struct nfs_delegation *freeme = NULL;
301 delegation = kmalloc(sizeof(*delegation), GFP_NOFS);
302 if (delegation == NULL)
304 nfs4_stateid_copy(&delegation->stateid, &res->delegation);
305 delegation->type = res->delegation_type;
306 delegation->maxsize = res->maxsize;
307 delegation->change_attr = inode->i_version;
308 delegation->cred = get_rpccred(cred);
309 delegation->inode = inode;
310 delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
311 spin_lock_init(&delegation->lock);
313 spin_lock(&clp->cl_lock);
314 old_delegation = rcu_dereference_protected(nfsi->delegation,
315 lockdep_is_held(&clp->cl_lock));
316 if (old_delegation != NULL) {
317 if (nfs4_stateid_match(&delegation->stateid,
318 &old_delegation->stateid) &&
319 delegation->type == old_delegation->type) {
323 * Deal with broken servers that hand out two
324 * delegations for the same file.
325 * Allow for upgrades to a WRITE delegation, but
328 dfprintk(FILE, "%s: server %s handed out "
329 "a duplicate delegation!\n",
330 __func__, clp->cl_hostname);
331 if (delegation->type == old_delegation->type ||
332 !(delegation->type & FMODE_WRITE)) {
337 freeme = nfs_detach_delegation_locked(nfsi,
338 old_delegation, clp);
342 list_add_rcu(&delegation->super_list, &server->delegations);
343 nfsi->delegation_state = delegation->type;
344 rcu_assign_pointer(nfsi->delegation, delegation);
347 /* Ensure we revalidate the attributes and page cache! */
348 spin_lock(&inode->i_lock);
349 nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
350 spin_unlock(&inode->i_lock);
351 trace_nfs4_set_delegation(inode, res->delegation_type);
354 spin_unlock(&clp->cl_lock);
355 if (delegation != NULL)
356 nfs_free_delegation(delegation);
358 nfs_do_return_delegation(inode, freeme, 0);
363 * Basic procedure for returning a delegation to the server
365 static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync)
367 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
368 struct nfs_inode *nfsi = NFS_I(inode);
371 if (delegation == NULL)
374 err = nfs_delegation_claim_opens(inode, &delegation->stateid);
375 if (!issync || err != -EAGAIN)
378 * Guard against state recovery
380 err = nfs4_wait_clnt_recover(clp);
384 nfs_abort_delegation_return(delegation, clp);
387 if (!nfs_detach_delegation(nfsi, delegation, NFS_SERVER(inode)))
390 err = nfs_do_return_delegation(inode, delegation, issync);
395 static bool nfs_delegation_need_return(struct nfs_delegation *delegation)
399 if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
401 if (test_and_clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags) && !ret) {
404 spin_lock(&delegation->lock);
405 inode = delegation->inode;
406 if (inode && list_empty(&NFS_I(inode)->open_files))
408 spin_unlock(&delegation->lock);
414 * nfs_client_return_marked_delegations - return previously marked delegations
415 * @clp: nfs_client to process
417 * Note that this function is designed to be called by the state
418 * manager thread. For this reason, it cannot flush the dirty data,
419 * since that could deadlock in case of a state recovery error.
421 * Returns zero on success, or a negative errno value.
423 int nfs_client_return_marked_delegations(struct nfs_client *clp)
425 struct nfs_delegation *delegation;
426 struct nfs_server *server;
432 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
433 list_for_each_entry_rcu(delegation, &server->delegations,
435 if (!nfs_delegation_need_return(delegation))
437 inode = nfs_delegation_grab_inode(delegation);
440 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
443 err = nfs_end_delegation_return(inode, delegation, 0);
447 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
456 * nfs_inode_return_delegation_noreclaim - return delegation, don't reclaim opens
457 * @inode: inode to process
459 * Does not protect against delegation reclaims, therefore really only safe
460 * to be called from nfs4_clear_inode().
462 void nfs_inode_return_delegation_noreclaim(struct inode *inode)
464 struct nfs_delegation *delegation;
466 delegation = nfs_inode_detach_delegation(inode);
467 if (delegation != NULL)
468 nfs_do_return_delegation(inode, delegation, 0);
472 * nfs_inode_return_delegation - synchronously return a delegation
473 * @inode: inode to process
475 * This routine will always flush any dirty data to disk on the
476 * assumption that if we need to return the delegation, then
477 * we should stop caching.
479 * Returns zero on success, or a negative errno value.
481 int nfs4_inode_return_delegation(struct inode *inode)
483 struct nfs_inode *nfsi = NFS_I(inode);
484 struct nfs_delegation *delegation;
488 delegation = nfs_start_delegation_return(nfsi);
489 if (delegation != NULL)
490 err = nfs_end_delegation_return(inode, delegation, 1);
494 static void nfs_mark_return_if_closed_delegation(struct nfs_server *server,
495 struct nfs_delegation *delegation)
497 set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
498 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
501 static void nfs_mark_return_delegation(struct nfs_server *server,
502 struct nfs_delegation *delegation)
504 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
505 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
508 static bool nfs_server_mark_return_all_delegations(struct nfs_server *server)
510 struct nfs_delegation *delegation;
513 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
514 nfs_mark_return_delegation(server, delegation);
520 static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
522 struct nfs_server *server;
525 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
526 nfs_server_mark_return_all_delegations(server);
530 static void nfs_delegation_run_state_manager(struct nfs_client *clp)
532 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
533 nfs4_schedule_state_manager(clp);
537 * nfs_expire_all_delegations
538 * @clp: client to process
541 void nfs_expire_all_delegations(struct nfs_client *clp)
543 nfs_client_mark_return_all_delegations(clp);
544 nfs_delegation_run_state_manager(clp);
548 * nfs_super_return_all_delegations - return delegations for one superblock
552 void nfs_server_return_all_delegations(struct nfs_server *server)
554 struct nfs_client *clp = server->nfs_client;
561 need_wait = nfs_server_mark_return_all_delegations(server);
565 nfs4_schedule_state_manager(clp);
566 nfs4_wait_clnt_recover(clp);
570 static void nfs_mark_return_unused_delegation_types(struct nfs_server *server,
573 struct nfs_delegation *delegation;
575 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
576 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
578 if (delegation->type & flags)
579 nfs_mark_return_if_closed_delegation(server, delegation);
583 static void nfs_client_mark_return_unused_delegation_types(struct nfs_client *clp,
586 struct nfs_server *server;
589 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
590 nfs_mark_return_unused_delegation_types(server, flags);
594 void nfs_remove_bad_delegation(struct inode *inode)
596 struct nfs_delegation *delegation;
598 delegation = nfs_inode_detach_delegation(inode);
600 nfs_inode_find_state_and_recover(inode, &delegation->stateid);
601 nfs_free_delegation(delegation);
604 EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation);
607 * nfs_expire_unused_delegation_types
608 * @clp: client to process
609 * @flags: delegation types to expire
612 void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags)
614 nfs_client_mark_return_unused_delegation_types(clp, flags);
615 nfs_delegation_run_state_manager(clp);
618 static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
620 struct nfs_delegation *delegation;
622 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
623 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
625 nfs_mark_return_if_closed_delegation(server, delegation);
630 * nfs_expire_unreferenced_delegations - Eliminate unused delegations
631 * @clp: nfs_client to process
634 void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
636 struct nfs_server *server;
639 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
640 nfs_mark_return_unreferenced_delegations(server);
643 nfs_delegation_run_state_manager(clp);
647 * nfs_async_inode_return_delegation - asynchronously return a delegation
648 * @inode: inode to process
649 * @stateid: state ID information
651 * Returns zero on success, or a negative errno value.
653 int nfs_async_inode_return_delegation(struct inode *inode,
654 const nfs4_stateid *stateid)
656 struct nfs_server *server = NFS_SERVER(inode);
657 struct nfs_client *clp = server->nfs_client;
658 struct nfs_delegation *delegation;
660 filemap_flush(inode->i_mapping);
663 delegation = rcu_dereference(NFS_I(inode)->delegation);
664 if (delegation == NULL)
667 if (!clp->cl_mvops->match_stateid(&delegation->stateid, stateid))
669 nfs_mark_return_delegation(server, delegation);
672 nfs_delegation_run_state_manager(clp);
679 static struct inode *
680 nfs_delegation_find_inode_server(struct nfs_server *server,
681 const struct nfs_fh *fhandle)
683 struct nfs_delegation *delegation;
684 struct inode *res = NULL;
686 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
687 spin_lock(&delegation->lock);
688 if (delegation->inode != NULL &&
689 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
690 res = igrab(delegation->inode);
692 spin_unlock(&delegation->lock);
700 * nfs_delegation_find_inode - retrieve the inode associated with a delegation
701 * @clp: client state handle
702 * @fhandle: filehandle from a delegation recall
704 * Returns pointer to inode matching "fhandle," or NULL if a matching inode
707 struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
708 const struct nfs_fh *fhandle)
710 struct nfs_server *server;
711 struct inode *res = NULL;
714 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
715 res = nfs_delegation_find_inode_server(server, fhandle);
723 static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
725 struct nfs_delegation *delegation;
727 list_for_each_entry_rcu(delegation, &server->delegations, super_list)
728 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
732 * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
733 * @clp: nfs_client to process
736 void nfs_delegation_mark_reclaim(struct nfs_client *clp)
738 struct nfs_server *server;
741 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
742 nfs_delegation_mark_reclaim_server(server);
747 * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
748 * @clp: nfs_client to process
751 void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
753 struct nfs_delegation *delegation;
754 struct nfs_server *server;
759 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
760 list_for_each_entry_rcu(delegation, &server->delegations,
762 if (test_bit(NFS_DELEGATION_NEED_RECLAIM,
763 &delegation->flags) == 0)
765 inode = nfs_delegation_grab_inode(delegation);
768 delegation = nfs_detach_delegation(NFS_I(inode),
772 if (delegation != NULL)
773 nfs_free_delegation(delegation);
782 * nfs_delegations_present - check for existence of delegations
783 * @clp: client state handle
785 * Returns one if there are any nfs_delegation structures attached
786 * to this nfs_client.
788 int nfs_delegations_present(struct nfs_client *clp)
790 struct nfs_server *server;
794 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
795 if (!list_empty(&server->delegations)) {
804 * nfs4_copy_delegation_stateid - Copy inode's state ID information
805 * @dst: stateid data structure to fill in
806 * @inode: inode to check
807 * @flags: delegation type requirement
809 * Returns "true" and fills in "dst->data" * if inode had a delegation,
810 * otherwise "false" is returned.
812 bool nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode,
815 struct nfs_inode *nfsi = NFS_I(inode);
816 struct nfs_delegation *delegation;
819 flags &= FMODE_READ|FMODE_WRITE;
821 delegation = rcu_dereference(nfsi->delegation);
822 ret = (delegation != NULL && (delegation->type & flags) == flags);
824 nfs4_stateid_copy(dst, &delegation->stateid);
825 nfs_mark_delegation_referenced(delegation);