2 * linux/fs/nfs/delegation.c
4 * Copyright (C) 2004 Trond Myklebust
6 * NFS file delegation management
9 #include <linux/config.h>
10 #include <linux/completion.h>
11 #include <linux/module.h>
12 #include <linux/sched.h>
13 #include <linux/spinlock.h>
15 #include <linux/nfs4.h>
16 #include <linux/nfs_fs.h>
17 #include <linux/nfs_xdr.h>
20 #include "delegation.h"
22 static struct nfs_delegation *nfs_alloc_delegation(void)
24 return (struct nfs_delegation *)kmalloc(sizeof(struct nfs_delegation), GFP_KERNEL);
27 static void nfs_free_delegation(struct nfs_delegation *delegation)
30 put_rpccred(delegation->cred);
34 static void nfs_delegation_claim_opens(struct inode *inode)
36 struct nfs_inode *nfsi = NFS_I(inode);
37 struct nfs_open_context *ctx;
38 struct nfs4_state *state;
41 spin_lock(&inode->i_lock);
42 list_for_each_entry(ctx, &nfsi->open_files, list) {
46 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
48 get_nfs_open_context(ctx);
49 spin_unlock(&inode->i_lock);
50 if (nfs4_open_delegation_recall(ctx->dentry, state) < 0)
52 put_nfs_open_context(ctx);
55 spin_unlock(&inode->i_lock);
59 * Set up a delegation on an inode
61 void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
63 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
65 if (delegation == NULL)
67 memcpy(delegation->stateid.data, res->delegation.data,
68 sizeof(delegation->stateid.data));
69 delegation->type = res->delegation_type;
70 delegation->maxsize = res->maxsize;
72 delegation->cred = get_rpccred(cred);
73 delegation->flags &= ~NFS_DELEGATION_NEED_RECLAIM;
74 NFS_I(inode)->delegation_state = delegation->type;
79 * Set up a delegation on an inode
81 int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
83 struct nfs4_client *clp = NFS_SERVER(inode)->nfs4_state;
84 struct nfs_inode *nfsi = NFS_I(inode);
85 struct nfs_delegation *delegation;
88 delegation = nfs_alloc_delegation();
89 if (delegation == NULL)
91 memcpy(delegation->stateid.data, res->delegation.data,
92 sizeof(delegation->stateid.data));
93 delegation->type = res->delegation_type;
94 delegation->maxsize = res->maxsize;
95 delegation->cred = get_rpccred(cred);
96 delegation->inode = inode;
98 spin_lock(&clp->cl_lock);
99 if (nfsi->delegation == NULL) {
100 list_add(&delegation->super_list, &clp->cl_delegations);
101 nfsi->delegation = delegation;
102 nfsi->delegation_state = delegation->type;
105 if (memcmp(&delegation->stateid, &nfsi->delegation->stateid,
106 sizeof(delegation->stateid)) != 0 ||
107 delegation->type != nfsi->delegation->type) {
108 printk("%s: server %u.%u.%u.%u, handed out a duplicate delegation!\n",
109 __FUNCTION__, NIPQUAD(clp->cl_addr));
113 spin_unlock(&clp->cl_lock);
114 if (delegation != NULL)
119 static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
123 __nfs_revalidate_inode(NFS_SERVER(inode), inode);
125 res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid);
126 nfs_free_delegation(delegation);
130 /* Sync all data to disk upon delegation return */
131 static void nfs_msync_inode(struct inode *inode)
133 filemap_fdatawrite(inode->i_mapping);
135 filemap_fdatawait(inode->i_mapping);
139 * Basic procedure for returning a delegation to the server
141 int nfs_inode_return_delegation(struct inode *inode)
143 struct nfs4_client *clp = NFS_SERVER(inode)->nfs4_state;
144 struct nfs_inode *nfsi = NFS_I(inode);
145 struct nfs_delegation *delegation;
148 nfs_msync_inode(inode);
149 down_read(&clp->cl_sem);
150 /* Guard against new delegated open calls */
151 down_write(&nfsi->rwsem);
152 spin_lock(&clp->cl_lock);
153 delegation = nfsi->delegation;
154 if (delegation != NULL) {
155 list_del_init(&delegation->super_list);
156 nfsi->delegation = NULL;
157 nfsi->delegation_state = 0;
159 spin_unlock(&clp->cl_lock);
160 nfs_delegation_claim_opens(inode);
161 up_write(&nfsi->rwsem);
162 up_read(&clp->cl_sem);
163 nfs_msync_inode(inode);
165 if (delegation != NULL)
166 res = nfs_do_return_delegation(inode, delegation);
171 * Return all delegations associated to a super block
173 void nfs_return_all_delegations(struct super_block *sb)
175 struct nfs4_client *clp = NFS_SB(sb)->nfs4_state;
176 struct nfs_delegation *delegation;
182 spin_lock(&clp->cl_lock);
183 list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
184 if (delegation->inode->i_sb != sb)
186 inode = igrab(delegation->inode);
189 spin_unlock(&clp->cl_lock);
190 nfs_inode_return_delegation(inode);
194 spin_unlock(&clp->cl_lock);
198 * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
200 void nfs_handle_cb_pathdown(struct nfs4_client *clp)
202 struct nfs_delegation *delegation;
208 spin_lock(&clp->cl_lock);
209 list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
210 inode = igrab(delegation->inode);
213 spin_unlock(&clp->cl_lock);
214 nfs_inode_return_delegation(inode);
218 spin_unlock(&clp->cl_lock);
221 struct recall_threadargs {
223 struct nfs4_client *clp;
224 const nfs4_stateid *stateid;
226 struct completion started;
230 static int recall_thread(void *data)
232 struct recall_threadargs *args = (struct recall_threadargs *)data;
233 struct inode *inode = igrab(args->inode);
234 struct nfs4_client *clp = NFS_SERVER(inode)->nfs4_state;
235 struct nfs_inode *nfsi = NFS_I(inode);
236 struct nfs_delegation *delegation;
238 daemonize("nfsv4-delegreturn");
240 nfs_msync_inode(inode);
241 down_read(&clp->cl_sem);
242 down_write(&nfsi->rwsem);
243 spin_lock(&clp->cl_lock);
244 delegation = nfsi->delegation;
245 if (delegation != NULL && memcmp(delegation->stateid.data,
247 sizeof(delegation->stateid.data)) == 0) {
248 list_del_init(&delegation->super_list);
249 nfsi->delegation = NULL;
250 nfsi->delegation_state = 0;
254 args->result = -ENOENT;
256 spin_unlock(&clp->cl_lock);
257 complete(&args->started);
258 nfs_delegation_claim_opens(inode);
259 up_write(&nfsi->rwsem);
260 up_read(&clp->cl_sem);
261 nfs_msync_inode(inode);
263 if (delegation != NULL)
264 nfs_do_return_delegation(inode, delegation);
266 module_put_and_exit(0);
270 * Asynchronous delegation recall!
272 int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid)
274 struct recall_threadargs data = {
280 init_completion(&data.started);
281 __module_get(THIS_MODULE);
282 status = kernel_thread(recall_thread, &data, CLONE_KERNEL);
285 wait_for_completion(&data.started);
288 module_put(THIS_MODULE);
293 * Retrieve the inode associated with a delegation
295 struct inode *nfs_delegation_find_inode(struct nfs4_client *clp, const struct nfs_fh *fhandle)
297 struct nfs_delegation *delegation;
298 struct inode *res = NULL;
299 spin_lock(&clp->cl_lock);
300 list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
301 if (nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
302 res = igrab(delegation->inode);
306 spin_unlock(&clp->cl_lock);
311 * Mark all delegations as needing to be reclaimed
313 void nfs_delegation_mark_reclaim(struct nfs4_client *clp)
315 struct nfs_delegation *delegation;
316 spin_lock(&clp->cl_lock);
317 list_for_each_entry(delegation, &clp->cl_delegations, super_list)
318 delegation->flags |= NFS_DELEGATION_NEED_RECLAIM;
319 spin_unlock(&clp->cl_lock);
323 * Reap all unclaimed delegations after reboot recovery is done
325 void nfs_delegation_reap_unclaimed(struct nfs4_client *clp)
327 struct nfs_delegation *delegation, *n;
329 spin_lock(&clp->cl_lock);
330 list_for_each_entry_safe(delegation, n, &clp->cl_delegations, super_list) {
331 if ((delegation->flags & NFS_DELEGATION_NEED_RECLAIM) == 0)
333 list_move(&delegation->super_list, &head);
334 NFS_I(delegation->inode)->delegation = NULL;
335 NFS_I(delegation->inode)->delegation_state = 0;
337 spin_unlock(&clp->cl_lock);
338 while(!list_empty(&head)) {
339 delegation = list_entry(head.next, struct nfs_delegation, super_list);
340 list_del(&delegation->super_list);
341 nfs_free_delegation(delegation);