NFSv4: Clean up the asynchronous delegation return
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / nfs / delegation.c
blob00c350c031b40832d54864b3b5f7dcc8bb40a6b3
1 /*
2 * linux/fs/nfs/delegation.c
4 * Copyright (C) 2004 Trond Myklebust
6 * NFS file delegation management
8 */
9 #include <linux/completion.h>
10 #include <linux/kthread.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 "nfs4_fs.h"
20 #include "delegation.h"
21 #include "internal.h"
23 static void nfs_do_free_delegation(struct nfs_delegation *delegation)
25 kfree(delegation);
28 static void nfs_free_delegation_callback(struct rcu_head *head)
30 struct nfs_delegation *delegation = container_of(head, struct nfs_delegation, rcu);
32 nfs_do_free_delegation(delegation);
35 static void nfs_free_delegation(struct nfs_delegation *delegation)
37 struct rpc_cred *cred;
39 cred = rcu_dereference(delegation->cred);
40 rcu_assign_pointer(delegation->cred, NULL);
41 call_rcu(&delegation->rcu, nfs_free_delegation_callback);
42 if (cred)
43 put_rpccred(cred);
46 static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state)
48 struct inode *inode = state->inode;
49 struct file_lock *fl;
50 int status;
52 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
53 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
54 continue;
55 if (nfs_file_open_context(fl->fl_file) != ctx)
56 continue;
57 status = nfs4_lock_delegation_recall(state, fl);
58 if (status >= 0)
59 continue;
60 switch (status) {
61 default:
62 printk(KERN_ERR "%s: unhandled error %d.\n",
63 __func__, status);
64 case -NFS4ERR_EXPIRED:
65 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
66 case -NFS4ERR_STALE_CLIENTID:
67 nfs4_schedule_state_recovery(NFS_SERVER(inode)->nfs_client);
68 goto out_err;
71 return 0;
72 out_err:
73 return status;
76 static void nfs_delegation_claim_opens(struct inode *inode, const nfs4_stateid *stateid)
78 struct nfs_inode *nfsi = NFS_I(inode);
79 struct nfs_open_context *ctx;
80 struct nfs4_state *state;
81 int err;
83 again:
84 spin_lock(&inode->i_lock);
85 list_for_each_entry(ctx, &nfsi->open_files, list) {
86 state = ctx->state;
87 if (state == NULL)
88 continue;
89 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
90 continue;
91 if (memcmp(state->stateid.data, stateid->data, sizeof(state->stateid.data)) != 0)
92 continue;
93 get_nfs_open_context(ctx);
94 spin_unlock(&inode->i_lock);
95 err = nfs4_open_delegation_recall(ctx, state, stateid);
96 if (err >= 0)
97 err = nfs_delegation_claim_locks(ctx, state);
98 put_nfs_open_context(ctx);
99 if (err != 0)
100 return;
101 goto again;
103 spin_unlock(&inode->i_lock);
107 * Set up a delegation on an inode
109 void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
111 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
112 struct rpc_cred *oldcred;
114 if (delegation == NULL)
115 return;
116 memcpy(delegation->stateid.data, res->delegation.data,
117 sizeof(delegation->stateid.data));
118 delegation->type = res->delegation_type;
119 delegation->maxsize = res->maxsize;
120 oldcred = delegation->cred;
121 delegation->cred = get_rpccred(cred);
122 clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
123 NFS_I(inode)->delegation_state = delegation->type;
124 smp_wmb();
125 put_rpccred(oldcred);
128 static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
130 int res = 0;
132 res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid, issync);
133 nfs_free_delegation(delegation);
134 return res;
137 static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
139 struct inode *inode = NULL;
141 spin_lock(&delegation->lock);
142 if (delegation->inode != NULL)
143 inode = igrab(delegation->inode);
144 spin_unlock(&delegation->lock);
145 return inode;
148 static struct nfs_delegation *nfs_detach_delegation_locked(struct nfs_inode *nfsi, const nfs4_stateid *stateid)
150 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
152 if (delegation == NULL)
153 goto nomatch;
154 spin_lock(&delegation->lock);
155 if (stateid != NULL && memcmp(delegation->stateid.data, stateid->data,
156 sizeof(delegation->stateid.data)) != 0)
157 goto nomatch_unlock;
158 list_del_rcu(&delegation->super_list);
159 delegation->inode = NULL;
160 nfsi->delegation_state = 0;
161 rcu_assign_pointer(nfsi->delegation, NULL);
162 spin_unlock(&delegation->lock);
163 return delegation;
164 nomatch_unlock:
165 spin_unlock(&delegation->lock);
166 nomatch:
167 return NULL;
171 * Set up a delegation on an inode
173 int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
175 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
176 struct nfs_inode *nfsi = NFS_I(inode);
177 struct nfs_delegation *delegation;
178 struct nfs_delegation *freeme = NULL;
179 int status = 0;
181 delegation = kmalloc(sizeof(*delegation), GFP_KERNEL);
182 if (delegation == NULL)
183 return -ENOMEM;
184 memcpy(delegation->stateid.data, res->delegation.data,
185 sizeof(delegation->stateid.data));
186 delegation->type = res->delegation_type;
187 delegation->maxsize = res->maxsize;
188 delegation->change_attr = nfsi->change_attr;
189 delegation->cred = get_rpccred(cred);
190 delegation->inode = inode;
191 spin_lock_init(&delegation->lock);
193 spin_lock(&clp->cl_lock);
194 if (rcu_dereference(nfsi->delegation) != NULL) {
195 if (memcmp(&delegation->stateid, &nfsi->delegation->stateid,
196 sizeof(delegation->stateid)) == 0 &&
197 delegation->type == nfsi->delegation->type) {
198 goto out;
201 * Deal with broken servers that hand out two
202 * delegations for the same file.
204 dfprintk(FILE, "%s: server %s handed out "
205 "a duplicate delegation!\n",
206 __func__, clp->cl_hostname);
207 if (delegation->type <= nfsi->delegation->type) {
208 freeme = delegation;
209 delegation = NULL;
210 goto out;
212 freeme = nfs_detach_delegation_locked(nfsi, NULL);
214 list_add_rcu(&delegation->super_list, &clp->cl_delegations);
215 nfsi->delegation_state = delegation->type;
216 rcu_assign_pointer(nfsi->delegation, delegation);
217 delegation = NULL;
219 /* Ensure we revalidate the attributes and page cache! */
220 spin_lock(&inode->i_lock);
221 nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
222 spin_unlock(&inode->i_lock);
224 out:
225 spin_unlock(&clp->cl_lock);
226 if (delegation != NULL)
227 nfs_free_delegation(delegation);
228 if (freeme != NULL)
229 nfs_do_return_delegation(inode, freeme, 0);
230 return status;
233 /* Sync all data to disk upon delegation return */
234 static void nfs_msync_inode(struct inode *inode)
236 filemap_fdatawrite(inode->i_mapping);
237 nfs_wb_all(inode);
238 filemap_fdatawait(inode->i_mapping);
242 * Basic procedure for returning a delegation to the server
244 static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
246 struct nfs_inode *nfsi = NFS_I(inode);
248 nfs_msync_inode(inode);
249 /* Guard against new delegated open calls */
250 down_write(&nfsi->rwsem);
251 nfs_delegation_claim_opens(inode, &delegation->stateid);
252 up_write(&nfsi->rwsem);
253 nfs_msync_inode(inode);
255 return nfs_do_return_delegation(inode, delegation, 1);
259 * Return all delegations that have been marked for return
261 void nfs_client_return_marked_delegations(struct nfs_client *clp)
263 struct nfs_delegation *delegation;
264 struct inode *inode;
266 restart:
267 rcu_read_lock();
268 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
269 if (!test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
270 continue;
271 inode = nfs_delegation_grab_inode(delegation);
272 if (inode == NULL)
273 continue;
274 spin_lock(&clp->cl_lock);
275 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
276 spin_unlock(&clp->cl_lock);
277 rcu_read_unlock();
278 if (delegation != NULL)
279 __nfs_inode_return_delegation(inode, delegation);
280 iput(inode);
281 goto restart;
283 rcu_read_unlock();
287 * This function returns the delegation without reclaiming opens
288 * or protecting against delegation reclaims.
289 * It is therefore really only safe to be called from
290 * nfs4_clear_inode()
292 void nfs_inode_return_delegation_noreclaim(struct inode *inode)
294 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
295 struct nfs_inode *nfsi = NFS_I(inode);
296 struct nfs_delegation *delegation;
298 if (rcu_dereference(nfsi->delegation) != NULL) {
299 spin_lock(&clp->cl_lock);
300 delegation = nfs_detach_delegation_locked(nfsi, NULL);
301 spin_unlock(&clp->cl_lock);
302 if (delegation != NULL)
303 nfs_do_return_delegation(inode, delegation, 0);
307 int nfs_inode_return_delegation(struct inode *inode)
309 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
310 struct nfs_inode *nfsi = NFS_I(inode);
311 struct nfs_delegation *delegation;
312 int err = 0;
314 if (rcu_dereference(nfsi->delegation) != NULL) {
315 spin_lock(&clp->cl_lock);
316 delegation = nfs_detach_delegation_locked(nfsi, NULL);
317 spin_unlock(&clp->cl_lock);
318 if (delegation != NULL)
319 err = __nfs_inode_return_delegation(inode, delegation);
321 return err;
324 static void nfs_mark_return_delegation(struct nfs_client *clp, struct nfs_delegation *delegation)
326 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
327 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
331 * Return all delegations associated to a super block
333 void nfs_super_return_all_delegations(struct super_block *sb)
335 struct nfs_client *clp = NFS_SB(sb)->nfs_client;
336 struct nfs_delegation *delegation;
338 if (clp == NULL)
339 return;
340 rcu_read_lock();
341 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
342 spin_lock(&delegation->lock);
343 if (delegation->inode != NULL && delegation->inode->i_sb == sb)
344 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
345 spin_unlock(&delegation->lock);
347 rcu_read_unlock();
348 nfs_client_return_marked_delegations(clp);
351 static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
353 struct nfs_delegation *delegation;
355 rcu_read_lock();
356 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
357 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
358 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
360 rcu_read_unlock();
363 static void nfs_delegation_run_state_manager(struct nfs_client *clp)
365 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
366 nfs4_schedule_state_manager(clp);
369 void nfs_expire_all_delegations(struct nfs_client *clp)
371 nfs_client_mark_return_all_delegations(clp);
372 nfs_delegation_run_state_manager(clp);
376 * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
378 void nfs_handle_cb_pathdown(struct nfs_client *clp)
380 if (clp == NULL)
381 return;
382 nfs_client_mark_return_all_delegations(clp);
386 * Asynchronous delegation recall!
388 int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid)
390 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
391 struct nfs_delegation *delegation;
393 rcu_read_lock();
394 delegation = rcu_dereference(NFS_I(inode)->delegation);
395 if (delegation == NULL || memcmp(delegation->stateid.data, stateid->data,
396 sizeof(delegation->stateid.data)) != 0) {
397 rcu_read_unlock();
398 return -ENOENT;
400 nfs_mark_return_delegation(clp, delegation);
401 rcu_read_unlock();
402 nfs_delegation_run_state_manager(clp);
403 return 0;
407 * Retrieve the inode associated with a delegation
409 struct inode *nfs_delegation_find_inode(struct nfs_client *clp, const struct nfs_fh *fhandle)
411 struct nfs_delegation *delegation;
412 struct inode *res = NULL;
413 rcu_read_lock();
414 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
415 spin_lock(&delegation->lock);
416 if (delegation->inode != NULL &&
417 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
418 res = igrab(delegation->inode);
420 spin_unlock(&delegation->lock);
421 if (res != NULL)
422 break;
424 rcu_read_unlock();
425 return res;
429 * Mark all delegations as needing to be reclaimed
431 void nfs_delegation_mark_reclaim(struct nfs_client *clp)
433 struct nfs_delegation *delegation;
434 rcu_read_lock();
435 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list)
436 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
437 rcu_read_unlock();
441 * Reap all unclaimed delegations after reboot recovery is done
443 void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
445 struct nfs_delegation *delegation;
446 struct inode *inode;
447 restart:
448 rcu_read_lock();
449 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
450 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0)
451 continue;
452 inode = nfs_delegation_grab_inode(delegation);
453 if (inode == NULL)
454 continue;
455 spin_lock(&clp->cl_lock);
456 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
457 spin_unlock(&clp->cl_lock);
458 rcu_read_unlock();
459 if (delegation != NULL)
460 nfs_free_delegation(delegation);
461 iput(inode);
462 goto restart;
464 rcu_read_unlock();
467 int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
469 struct nfs_inode *nfsi = NFS_I(inode);
470 struct nfs_delegation *delegation;
471 int ret = 0;
473 rcu_read_lock();
474 delegation = rcu_dereference(nfsi->delegation);
475 if (delegation != NULL) {
476 memcpy(dst->data, delegation->stateid.data, sizeof(dst->data));
477 ret = 1;
479 rcu_read_unlock();
480 return ret;