NFSv4: Fix up another delegation related race
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / nfs / delegation.c
blobe0cb4ee3b23eb8261e3394ca7098c8ced5e2af05
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 delegation->flags &= ~NFS_DELEGATION_NEED_RECLAIM;
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 nfs_delegation *nfs_detach_delegation_locked(struct nfs_inode *nfsi, const nfs4_stateid *stateid)
139 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
141 if (delegation == NULL)
142 goto nomatch;
143 spin_lock(&delegation->lock);
144 if (stateid != NULL && memcmp(delegation->stateid.data, stateid->data,
145 sizeof(delegation->stateid.data)) != 0)
146 goto nomatch_unlock;
147 list_del_rcu(&delegation->super_list);
148 nfsi->delegation_state = 0;
149 rcu_assign_pointer(nfsi->delegation, NULL);
150 spin_unlock(&delegation->lock);
151 return delegation;
152 nomatch_unlock:
153 spin_unlock(&delegation->lock);
154 nomatch:
155 return NULL;
159 * Set up a delegation on an inode
161 int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
163 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
164 struct nfs_inode *nfsi = NFS_I(inode);
165 struct nfs_delegation *delegation;
166 struct nfs_delegation *freeme = NULL;
167 int status = 0;
169 delegation = kmalloc(sizeof(*delegation), GFP_KERNEL);
170 if (delegation == NULL)
171 return -ENOMEM;
172 memcpy(delegation->stateid.data, res->delegation.data,
173 sizeof(delegation->stateid.data));
174 delegation->type = res->delegation_type;
175 delegation->maxsize = res->maxsize;
176 delegation->change_attr = nfsi->change_attr;
177 delegation->cred = get_rpccred(cred);
178 delegation->inode = inode;
179 spin_lock_init(&delegation->lock);
181 spin_lock(&clp->cl_lock);
182 if (rcu_dereference(nfsi->delegation) != NULL) {
183 if (memcmp(&delegation->stateid, &nfsi->delegation->stateid,
184 sizeof(delegation->stateid)) == 0 &&
185 delegation->type == nfsi->delegation->type) {
186 goto out;
189 * Deal with broken servers that hand out two
190 * delegations for the same file.
192 dfprintk(FILE, "%s: server %s handed out "
193 "a duplicate delegation!\n",
194 __func__, clp->cl_hostname);
195 if (delegation->type <= nfsi->delegation->type) {
196 freeme = delegation;
197 delegation = NULL;
198 goto out;
200 freeme = nfs_detach_delegation_locked(nfsi, NULL);
202 list_add_rcu(&delegation->super_list, &clp->cl_delegations);
203 nfsi->delegation_state = delegation->type;
204 rcu_assign_pointer(nfsi->delegation, delegation);
205 delegation = NULL;
207 /* Ensure we revalidate the attributes and page cache! */
208 spin_lock(&inode->i_lock);
209 nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
210 spin_unlock(&inode->i_lock);
212 out:
213 spin_unlock(&clp->cl_lock);
214 if (delegation != NULL)
215 nfs_free_delegation(delegation);
216 if (freeme != NULL)
217 nfs_do_return_delegation(inode, freeme, 0);
218 return status;
221 /* Sync all data to disk upon delegation return */
222 static void nfs_msync_inode(struct inode *inode)
224 filemap_fdatawrite(inode->i_mapping);
225 nfs_wb_all(inode);
226 filemap_fdatawait(inode->i_mapping);
230 * Basic procedure for returning a delegation to the server
232 static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
234 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
235 struct nfs_inode *nfsi = NFS_I(inode);
237 nfs_msync_inode(inode);
238 down_read(&clp->cl_sem);
239 /* Guard against new delegated open calls */
240 down_write(&nfsi->rwsem);
241 nfs_delegation_claim_opens(inode, &delegation->stateid);
242 up_write(&nfsi->rwsem);
243 up_read(&clp->cl_sem);
244 nfs_msync_inode(inode);
246 return nfs_do_return_delegation(inode, delegation, 1);
250 * This function returns the delegation without reclaiming opens
251 * or protecting against delegation reclaims.
252 * It is therefore really only safe to be called from
253 * nfs4_clear_inode()
255 void nfs_inode_return_delegation_noreclaim(struct inode *inode)
257 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
258 struct nfs_inode *nfsi = NFS_I(inode);
259 struct nfs_delegation *delegation;
261 if (rcu_dereference(nfsi->delegation) != NULL) {
262 spin_lock(&clp->cl_lock);
263 delegation = nfs_detach_delegation_locked(nfsi, NULL);
264 spin_unlock(&clp->cl_lock);
265 if (delegation != NULL)
266 nfs_do_return_delegation(inode, delegation, 0);
270 int nfs_inode_return_delegation(struct inode *inode)
272 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
273 struct nfs_inode *nfsi = NFS_I(inode);
274 struct nfs_delegation *delegation;
275 int err = 0;
277 if (rcu_dereference(nfsi->delegation) != NULL) {
278 spin_lock(&clp->cl_lock);
279 delegation = nfs_detach_delegation_locked(nfsi, NULL);
280 spin_unlock(&clp->cl_lock);
281 if (delegation != NULL)
282 err = __nfs_inode_return_delegation(inode, delegation);
284 return err;
288 * Return all delegations associated to a super block
290 void nfs_return_all_delegations(struct super_block *sb)
292 struct nfs_client *clp = NFS_SB(sb)->nfs_client;
293 struct nfs_delegation *delegation;
294 struct inode *inode;
296 if (clp == NULL)
297 return;
298 restart:
299 rcu_read_lock();
300 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
301 if (delegation->inode->i_sb != sb)
302 continue;
303 inode = igrab(delegation->inode);
304 if (inode == NULL)
305 continue;
306 spin_lock(&clp->cl_lock);
307 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
308 spin_unlock(&clp->cl_lock);
309 rcu_read_unlock();
310 if (delegation != NULL)
311 __nfs_inode_return_delegation(inode, delegation);
312 iput(inode);
313 goto restart;
315 rcu_read_unlock();
318 static int nfs_do_expire_all_delegations(void *ptr)
320 struct nfs_client *clp = ptr;
321 struct nfs_delegation *delegation;
322 struct inode *inode;
324 allow_signal(SIGKILL);
325 restart:
326 if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) != 0)
327 goto out;
328 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0)
329 goto out;
330 rcu_read_lock();
331 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
332 inode = igrab(delegation->inode);
333 if (inode == NULL)
334 continue;
335 spin_lock(&clp->cl_lock);
336 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
337 spin_unlock(&clp->cl_lock);
338 rcu_read_unlock();
339 if (delegation)
340 __nfs_inode_return_delegation(inode, delegation);
341 iput(inode);
342 goto restart;
344 rcu_read_unlock();
345 out:
346 nfs_put_client(clp);
347 module_put_and_exit(0);
350 void nfs_expire_all_delegations(struct nfs_client *clp)
352 struct task_struct *task;
354 __module_get(THIS_MODULE);
355 atomic_inc(&clp->cl_count);
356 task = kthread_run(nfs_do_expire_all_delegations, clp,
357 "%s-delegreturn",
358 rpc_peeraddr2str(clp->cl_rpcclient,
359 RPC_DISPLAY_ADDR));
360 if (!IS_ERR(task))
361 return;
362 nfs_put_client(clp);
363 module_put(THIS_MODULE);
367 * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
369 void nfs_handle_cb_pathdown(struct nfs_client *clp)
371 struct nfs_delegation *delegation;
372 struct inode *inode;
374 if (clp == NULL)
375 return;
376 restart:
377 rcu_read_lock();
378 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
379 inode = igrab(delegation->inode);
380 if (inode == NULL)
381 continue;
382 spin_lock(&clp->cl_lock);
383 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
384 spin_unlock(&clp->cl_lock);
385 rcu_read_unlock();
386 if (delegation != NULL)
387 __nfs_inode_return_delegation(inode, delegation);
388 iput(inode);
389 goto restart;
391 rcu_read_unlock();
394 struct recall_threadargs {
395 struct inode *inode;
396 struct nfs_client *clp;
397 const nfs4_stateid *stateid;
399 struct completion started;
400 int result;
403 static int recall_thread(void *data)
405 struct recall_threadargs *args = (struct recall_threadargs *)data;
406 struct inode *inode = igrab(args->inode);
407 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
408 struct nfs_inode *nfsi = NFS_I(inode);
409 struct nfs_delegation *delegation;
411 daemonize("nfsv4-delegreturn");
413 nfs_msync_inode(inode);
414 down_read(&clp->cl_sem);
415 down_write(&nfsi->rwsem);
416 spin_lock(&clp->cl_lock);
417 delegation = nfs_detach_delegation_locked(nfsi, args->stateid);
418 if (delegation != NULL)
419 args->result = 0;
420 else
421 args->result = -ENOENT;
422 spin_unlock(&clp->cl_lock);
423 complete(&args->started);
424 nfs_delegation_claim_opens(inode, args->stateid);
425 up_write(&nfsi->rwsem);
426 up_read(&clp->cl_sem);
427 nfs_msync_inode(inode);
429 if (delegation != NULL)
430 nfs_do_return_delegation(inode, delegation, 1);
431 iput(inode);
432 module_put_and_exit(0);
436 * Asynchronous delegation recall!
438 int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid)
440 struct recall_threadargs data = {
441 .inode = inode,
442 .stateid = stateid,
444 int status;
446 init_completion(&data.started);
447 __module_get(THIS_MODULE);
448 status = kernel_thread(recall_thread, &data, CLONE_KERNEL);
449 if (status < 0)
450 goto out_module_put;
451 wait_for_completion(&data.started);
452 return data.result;
453 out_module_put:
454 module_put(THIS_MODULE);
455 return status;
459 * Retrieve the inode associated with a delegation
461 struct inode *nfs_delegation_find_inode(struct nfs_client *clp, const struct nfs_fh *fhandle)
463 struct nfs_delegation *delegation;
464 struct inode *res = NULL;
465 rcu_read_lock();
466 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
467 if (nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
468 res = igrab(delegation->inode);
469 break;
472 rcu_read_unlock();
473 return res;
477 * Mark all delegations as needing to be reclaimed
479 void nfs_delegation_mark_reclaim(struct nfs_client *clp)
481 struct nfs_delegation *delegation;
482 rcu_read_lock();
483 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list)
484 delegation->flags |= NFS_DELEGATION_NEED_RECLAIM;
485 rcu_read_unlock();
489 * Reap all unclaimed delegations after reboot recovery is done
491 void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
493 struct nfs_delegation *delegation;
494 restart:
495 rcu_read_lock();
496 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
497 if ((delegation->flags & NFS_DELEGATION_NEED_RECLAIM) == 0)
498 continue;
499 spin_lock(&clp->cl_lock);
500 delegation = nfs_detach_delegation_locked(NFS_I(delegation->inode), NULL);
501 spin_unlock(&clp->cl_lock);
502 rcu_read_unlock();
503 if (delegation != NULL)
504 nfs_free_delegation(delegation);
505 goto restart;
507 rcu_read_unlock();
510 int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
512 struct nfs_inode *nfsi = NFS_I(inode);
513 struct nfs_delegation *delegation;
514 int ret = 0;
516 rcu_read_lock();
517 delegation = rcu_dereference(nfsi->delegation);
518 if (delegation != NULL) {
519 memcpy(dst->data, delegation->stateid.data, sizeof(dst->data));
520 ret = 1;
522 rcu_read_unlock();
523 return ret;