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>
19 #include "delegation.h"
21 static struct nfs_delegation *nfs_alloc_delegation(void)
23 return (struct nfs_delegation *)kmalloc(sizeof(struct nfs_delegation), GFP_KERNEL);
26 static void nfs_free_delegation(struct nfs_delegation *delegation)
29 put_rpccred(delegation->cred);
33 static void nfs_delegation_claim_opens(struct inode *inode)
35 struct nfs_inode *nfsi = NFS_I(inode);
36 struct nfs_open_context *ctx;
37 struct nfs4_state *state;
40 spin_lock(&inode->i_lock);
41 list_for_each_entry(ctx, &nfsi->open_files, list) {
45 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
47 get_nfs_open_context(ctx);
48 spin_unlock(&inode->i_lock);
49 if (nfs4_open_delegation_recall(ctx->dentry, state) < 0)
51 put_nfs_open_context(ctx);
54 spin_unlock(&inode->i_lock);
58 * Set up a delegation on an inode
60 void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
62 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
64 if (delegation == NULL)
66 memcpy(delegation->stateid.data, res->delegation.data,
67 sizeof(delegation->stateid.data));
68 delegation->type = res->delegation_type;
69 delegation->maxsize = res->maxsize;
71 delegation->cred = get_rpccred(cred);
72 delegation->flags &= ~NFS_DELEGATION_NEED_RECLAIM;
73 NFS_I(inode)->delegation_state = delegation->type;
78 * Set up a delegation on an inode
80 int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
82 struct nfs4_client *clp = NFS_SERVER(inode)->nfs4_state;
83 struct nfs_inode *nfsi = NFS_I(inode);
84 struct nfs_delegation *delegation;
87 delegation = nfs_alloc_delegation();
88 if (delegation == NULL)
90 memcpy(delegation->stateid.data, res->delegation.data,
91 sizeof(delegation->stateid.data));
92 delegation->type = res->delegation_type;
93 delegation->maxsize = res->maxsize;
94 delegation->cred = get_rpccred(cred);
95 delegation->inode = inode;
97 spin_lock(&clp->cl_lock);
98 if (nfsi->delegation == NULL) {
99 list_add(&delegation->super_list, &clp->cl_delegations);
100 nfsi->delegation = delegation;
101 nfsi->delegation_state = delegation->type;
104 if (memcmp(&delegation->stateid, &nfsi->delegation->stateid,
105 sizeof(delegation->stateid)) != 0 ||
106 delegation->type != nfsi->delegation->type) {
107 printk("%s: server %u.%u.%u.%u, handed out a duplicate delegation!\n",
108 __FUNCTION__, NIPQUAD(clp->cl_addr));
112 spin_unlock(&clp->cl_lock);
113 if (delegation != NULL)
118 static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
122 __nfs_revalidate_inode(NFS_SERVER(inode), inode);
124 res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid);
125 nfs_free_delegation(delegation);
129 /* Sync all data to disk upon delegation return */
130 static void nfs_msync_inode(struct inode *inode)
132 filemap_fdatawrite(inode->i_mapping);
134 filemap_fdatawait(inode->i_mapping);
138 * Basic procedure for returning a delegation to the server
140 int nfs_inode_return_delegation(struct inode *inode)
142 struct nfs4_client *clp = NFS_SERVER(inode)->nfs4_state;
143 struct nfs_inode *nfsi = NFS_I(inode);
144 struct nfs_delegation *delegation;
147 nfs_msync_inode(inode);
148 down_read(&clp->cl_sem);
149 /* Guard against new delegated open calls */
150 down_write(&nfsi->rwsem);
151 spin_lock(&clp->cl_lock);
152 delegation = nfsi->delegation;
153 if (delegation != NULL) {
154 list_del_init(&delegation->super_list);
155 nfsi->delegation = NULL;
156 nfsi->delegation_state = 0;
158 spin_unlock(&clp->cl_lock);
159 nfs_delegation_claim_opens(inode);
160 up_write(&nfsi->rwsem);
161 up_read(&clp->cl_sem);
162 nfs_msync_inode(inode);
164 if (delegation != NULL)
165 res = nfs_do_return_delegation(inode, delegation);
170 * Return all delegations associated to a super block
172 void nfs_return_all_delegations(struct super_block *sb)
174 struct nfs4_client *clp = NFS_SB(sb)->nfs4_state;
175 struct nfs_delegation *delegation;
181 spin_lock(&clp->cl_lock);
182 list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
183 if (delegation->inode->i_sb != sb)
185 inode = igrab(delegation->inode);
188 spin_unlock(&clp->cl_lock);
189 nfs_inode_return_delegation(inode);
193 spin_unlock(&clp->cl_lock);
197 * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
199 void nfs_handle_cb_pathdown(struct nfs4_client *clp)
201 struct nfs_delegation *delegation;
207 spin_lock(&clp->cl_lock);
208 list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
209 inode = igrab(delegation->inode);
212 spin_unlock(&clp->cl_lock);
213 nfs_inode_return_delegation(inode);
217 spin_unlock(&clp->cl_lock);
220 struct recall_threadargs {
222 struct nfs4_client *clp;
223 const nfs4_stateid *stateid;
225 struct completion started;
229 static int recall_thread(void *data)
231 struct recall_threadargs *args = (struct recall_threadargs *)data;
232 struct inode *inode = igrab(args->inode);
233 struct nfs4_client *clp = NFS_SERVER(inode)->nfs4_state;
234 struct nfs_inode *nfsi = NFS_I(inode);
235 struct nfs_delegation *delegation;
237 daemonize("nfsv4-delegreturn");
239 nfs_msync_inode(inode);
240 down_read(&clp->cl_sem);
241 down_write(&nfsi->rwsem);
242 spin_lock(&clp->cl_lock);
243 delegation = nfsi->delegation;
244 if (delegation != NULL && memcmp(delegation->stateid.data,
246 sizeof(delegation->stateid.data)) == 0) {
247 list_del_init(&delegation->super_list);
248 nfsi->delegation = NULL;
249 nfsi->delegation_state = 0;
253 args->result = -ENOENT;
255 spin_unlock(&clp->cl_lock);
256 complete(&args->started);
257 nfs_delegation_claim_opens(inode);
258 up_write(&nfsi->rwsem);
259 up_read(&clp->cl_sem);
260 nfs_msync_inode(inode);
262 if (delegation != NULL)
263 nfs_do_return_delegation(inode, delegation);
265 module_put_and_exit(0);
269 * Asynchronous delegation recall!
271 int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid)
273 struct recall_threadargs data = {
279 init_completion(&data.started);
280 __module_get(THIS_MODULE);
281 status = kernel_thread(recall_thread, &data, CLONE_KERNEL);
284 wait_for_completion(&data.started);
287 module_put(THIS_MODULE);
292 * Retrieve the inode associated with a delegation
294 struct inode *nfs_delegation_find_inode(struct nfs4_client *clp, const struct nfs_fh *fhandle)
296 struct nfs_delegation *delegation;
297 struct inode *res = NULL;
298 spin_lock(&clp->cl_lock);
299 list_for_each_entry(delegation, &clp->cl_delegations, super_list) {
300 if (nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
301 res = igrab(delegation->inode);
305 spin_unlock(&clp->cl_lock);
310 * Mark all delegations as needing to be reclaimed
312 void nfs_delegation_mark_reclaim(struct nfs4_client *clp)
314 struct nfs_delegation *delegation;
315 spin_lock(&clp->cl_lock);
316 list_for_each_entry(delegation, &clp->cl_delegations, super_list)
317 delegation->flags |= NFS_DELEGATION_NEED_RECLAIM;
318 spin_unlock(&clp->cl_lock);
322 * Reap all unclaimed delegations after reboot recovery is done
324 void nfs_delegation_reap_unclaimed(struct nfs4_client *clp)
326 struct nfs_delegation *delegation, *n;
328 spin_lock(&clp->cl_lock);
329 list_for_each_entry_safe(delegation, n, &clp->cl_delegations, super_list) {
330 if ((delegation->flags & NFS_DELEGATION_NEED_RECLAIM) == 0)
332 list_move(&delegation->super_list, &head);
333 NFS_I(delegation->inode)->delegation = NULL;
334 NFS_I(delegation->inode)->delegation_state = 0;
336 spin_unlock(&clp->cl_lock);
337 while(!list_empty(&head)) {
338 delegation = list_entry(head.next, struct nfs_delegation, super_list);
339 list_del(&delegation->super_list);
340 nfs_free_delegation(delegation);