]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/nfs/delegation.c
NFSv4: Fix up another delegation related race
[net-next-2.6.git] / fs / nfs / delegation.c
CommitLineData
1da177e4
LT
1/*
2 * linux/fs/nfs/delegation.c
3 *
4 * Copyright (C) 2004 Trond Myklebust
5 *
6 * NFS file delegation management
7 *
8 */
1da177e4 9#include <linux/completion.h>
58d9714a 10#include <linux/kthread.h>
1da177e4
LT
11#include <linux/module.h>
12#include <linux/sched.h>
13#include <linux/spinlock.h>
14
15#include <linux/nfs4.h>
16#include <linux/nfs_fs.h>
17#include <linux/nfs_xdr.h>
18
4ce79717 19#include "nfs4_fs.h"
1da177e4 20#include "delegation.h"
24c8dbbb 21#include "internal.h"
1da177e4 22
905f8d16 23static void nfs_do_free_delegation(struct nfs_delegation *delegation)
1da177e4 24{
1da177e4
LT
25 kfree(delegation);
26}
27
8383e460
TM
28static void nfs_free_delegation_callback(struct rcu_head *head)
29{
30 struct nfs_delegation *delegation = container_of(head, struct nfs_delegation, rcu);
31
905f8d16
TM
32 nfs_do_free_delegation(delegation);
33}
34
35static void nfs_free_delegation(struct nfs_delegation *delegation)
36{
37 struct rpc_cred *cred;
38
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);
8383e460
TM
44}
45
888e694c
TM
46static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state)
47{
48 struct inode *inode = state->inode;
49 struct file_lock *fl;
50 int status;
51
90dc7d27 52 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
888e694c
TM
53 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
54 continue;
cd3758e3 55 if (nfs_file_open_context(fl->fl_file) != ctx)
888e694c
TM
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",
3110ff80 63 __func__, status);
888e694c
TM
64 case -NFS4ERR_EXPIRED:
65 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
66 case -NFS4ERR_STALE_CLIENTID:
7539bbab 67 nfs4_schedule_state_recovery(NFS_SERVER(inode)->nfs_client);
888e694c
TM
68 goto out_err;
69 }
70 }
71 return 0;
72out_err:
73 return status;
74}
75
90163027 76static void nfs_delegation_claim_opens(struct inode *inode, const nfs4_stateid *stateid)
1da177e4
LT
77{
78 struct nfs_inode *nfsi = NFS_I(inode);
79 struct nfs_open_context *ctx;
80 struct nfs4_state *state;
888e694c 81 int err;
1da177e4
LT
82
83again:
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;
90163027
TM
91 if (memcmp(state->stateid.data, stateid->data, sizeof(state->stateid.data)) != 0)
92 continue;
1da177e4
LT
93 get_nfs_open_context(ctx);
94 spin_unlock(&inode->i_lock);
13437e12 95 err = nfs4_open_delegation_recall(ctx, state, stateid);
888e694c
TM
96 if (err >= 0)
97 err = nfs_delegation_claim_locks(ctx, state);
1da177e4 98 put_nfs_open_context(ctx);
888e694c
TM
99 if (err != 0)
100 return;
1da177e4
LT
101 goto again;
102 }
103 spin_unlock(&inode->i_lock);
104}
105
106/*
107 * Set up a delegation on an inode
108 */
109void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
110{
111 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
05c88bab 112 struct rpc_cred *oldcred;
1da177e4
LT
113
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;
05c88bab 120 oldcred = delegation->cred;
1da177e4
LT
121 delegation->cred = get_rpccred(cred);
122 delegation->flags &= ~NFS_DELEGATION_NEED_RECLAIM;
123 NFS_I(inode)->delegation_state = delegation->type;
124 smp_wmb();
05c88bab 125 put_rpccred(oldcred);
1da177e4
LT
126}
127
57bfa891
TM
128static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
129{
130 int res = 0;
131
132 res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid, issync);
133 nfs_free_delegation(delegation);
134 return res;
135}
136
137static struct nfs_delegation *nfs_detach_delegation_locked(struct nfs_inode *nfsi, const nfs4_stateid *stateid)
138{
139 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
140
141 if (delegation == NULL)
142 goto nomatch;
34310430 143 spin_lock(&delegation->lock);
57bfa891
TM
144 if (stateid != NULL && memcmp(delegation->stateid.data, stateid->data,
145 sizeof(delegation->stateid.data)) != 0)
34310430 146 goto nomatch_unlock;
57bfa891
TM
147 list_del_rcu(&delegation->super_list);
148 nfsi->delegation_state = 0;
149 rcu_assign_pointer(nfsi->delegation, NULL);
34310430 150 spin_unlock(&delegation->lock);
57bfa891 151 return delegation;
34310430
TM
152nomatch_unlock:
153 spin_unlock(&delegation->lock);
57bfa891
TM
154nomatch:
155 return NULL;
156}
157
1da177e4
LT
158/*
159 * Set up a delegation on an inode
160 */
161int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
162{
7539bbab 163 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1da177e4
LT
164 struct nfs_inode *nfsi = NFS_I(inode);
165 struct nfs_delegation *delegation;
57bfa891 166 struct nfs_delegation *freeme = NULL;
1da177e4
LT
167 int status = 0;
168
f52720ca 169 delegation = kmalloc(sizeof(*delegation), GFP_KERNEL);
1da177e4
LT
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;
beb2a5ec 176 delegation->change_attr = nfsi->change_attr;
1da177e4
LT
177 delegation->cred = get_rpccred(cred);
178 delegation->inode = inode;
34310430 179 spin_lock_init(&delegation->lock);
1da177e4
LT
180
181 spin_lock(&clp->cl_lock);
57bfa891 182 if (rcu_dereference(nfsi->delegation) != NULL) {
1da177e4 183 if (memcmp(&delegation->stateid, &nfsi->delegation->stateid,
57bfa891
TM
184 sizeof(delegation->stateid)) == 0 &&
185 delegation->type == nfsi->delegation->type) {
186 goto out;
1da177e4 187 }
57bfa891
TM
188 /*
189 * Deal with broken servers that hand out two
190 * delegations for the same file.
191 */
192 dfprintk(FILE, "%s: server %s handed out "
193 "a duplicate delegation!\n",
3110ff80 194 __func__, clp->cl_hostname);
57bfa891
TM
195 if (delegation->type <= nfsi->delegation->type) {
196 freeme = delegation;
197 delegation = NULL;
198 goto out;
199 }
200 freeme = nfs_detach_delegation_locked(nfsi, NULL);
1da177e4 201 }
57bfa891
TM
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;
412c77ce
TM
206
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);
211
57bfa891 212out:
1da177e4 213 spin_unlock(&clp->cl_lock);
603c83da
TM
214 if (delegation != NULL)
215 nfs_free_delegation(delegation);
57bfa891
TM
216 if (freeme != NULL)
217 nfs_do_return_delegation(inode, freeme, 0);
1da177e4
LT
218 return status;
219}
220
1da177e4
LT
221/* Sync all data to disk upon delegation return */
222static void nfs_msync_inode(struct inode *inode)
223{
224 filemap_fdatawrite(inode->i_mapping);
225 nfs_wb_all(inode);
226 filemap_fdatawait(inode->i_mapping);
227}
228
229/*
230 * Basic procedure for returning a delegation to the server
231 */
90163027 232static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
1da177e4 233{
7539bbab 234 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1da177e4 235 struct nfs_inode *nfsi = NFS_I(inode);
1da177e4
LT
236
237 nfs_msync_inode(inode);
238 down_read(&clp->cl_sem);
239 /* Guard against new delegated open calls */
240 down_write(&nfsi->rwsem);
90163027 241 nfs_delegation_claim_opens(inode, &delegation->stateid);
1da177e4
LT
242 up_write(&nfsi->rwsem);
243 up_read(&clp->cl_sem);
244 nfs_msync_inode(inode);
245
e6f81075 246 return nfs_do_return_delegation(inode, delegation, 1);
90163027
TM
247}
248
e6f81075
TM
249/*
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()
254 */
255void nfs_inode_return_delegation_noreclaim(struct inode *inode)
256{
257 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
258 struct nfs_inode *nfsi = NFS_I(inode);
259 struct nfs_delegation *delegation;
260
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);
267 }
268}
269
90163027
TM
270int nfs_inode_return_delegation(struct inode *inode)
271{
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;
276
8383e460 277 if (rcu_dereference(nfsi->delegation) != NULL) {
90163027
TM
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);
283 }
284 return err;
1da177e4
LT
285}
286
287/*
288 * Return all delegations associated to a super block
289 */
290void nfs_return_all_delegations(struct super_block *sb)
291{
7539bbab 292 struct nfs_client *clp = NFS_SB(sb)->nfs_client;
1da177e4
LT
293 struct nfs_delegation *delegation;
294 struct inode *inode;
295
296 if (clp == NULL)
297 return;
298restart:
8383e460
TM
299 rcu_read_lock();
300 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
1da177e4
LT
301 if (delegation->inode->i_sb != sb)
302 continue;
303 inode = igrab(delegation->inode);
304 if (inode == NULL)
305 continue;
8383e460
TM
306 spin_lock(&clp->cl_lock);
307 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
1da177e4 308 spin_unlock(&clp->cl_lock);
8383e460
TM
309 rcu_read_unlock();
310 if (delegation != NULL)
311 __nfs_inode_return_delegation(inode, delegation);
1da177e4
LT
312 iput(inode);
313 goto restart;
314 }
8383e460 315 rcu_read_unlock();
1da177e4
LT
316}
317
10afec90 318static int nfs_do_expire_all_delegations(void *ptr)
58d9714a 319{
adfa6f98 320 struct nfs_client *clp = ptr;
58d9714a
TM
321 struct nfs_delegation *delegation;
322 struct inode *inode;
58d9714a
TM
323
324 allow_signal(SIGKILL);
325restart:
58d9714a
TM
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;
8383e460
TM
330 rcu_read_lock();
331 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
58d9714a
TM
332 inode = igrab(delegation->inode);
333 if (inode == NULL)
334 continue;
8383e460
TM
335 spin_lock(&clp->cl_lock);
336 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
58d9714a 337 spin_unlock(&clp->cl_lock);
8383e460
TM
338 rcu_read_unlock();
339 if (delegation)
340 __nfs_inode_return_delegation(inode, delegation);
58d9714a 341 iput(inode);
26c78e15 342 goto restart;
58d9714a 343 }
8383e460 344 rcu_read_unlock();
58d9714a 345out:
24c8dbbb 346 nfs_put_client(clp);
58d9714a
TM
347 module_put_and_exit(0);
348}
349
adfa6f98 350void nfs_expire_all_delegations(struct nfs_client *clp)
58d9714a
TM
351{
352 struct task_struct *task;
353
354 __module_get(THIS_MODULE);
355 atomic_inc(&clp->cl_count);
356 task = kthread_run(nfs_do_expire_all_delegations, clp,
5d8515ca
CL
357 "%s-delegreturn",
358 rpc_peeraddr2str(clp->cl_rpcclient,
359 RPC_DISPLAY_ADDR));
58d9714a
TM
360 if (!IS_ERR(task))
361 return;
24c8dbbb 362 nfs_put_client(clp);
58d9714a
TM
363 module_put(THIS_MODULE);
364}
365
1da177e4
LT
366/*
367 * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
368 */
adfa6f98 369void nfs_handle_cb_pathdown(struct nfs_client *clp)
1da177e4
LT
370{
371 struct nfs_delegation *delegation;
372 struct inode *inode;
373
374 if (clp == NULL)
375 return;
376restart:
8383e460
TM
377 rcu_read_lock();
378 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
1da177e4
LT
379 inode = igrab(delegation->inode);
380 if (inode == NULL)
381 continue;
8383e460
TM
382 spin_lock(&clp->cl_lock);
383 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
1da177e4 384 spin_unlock(&clp->cl_lock);
8383e460
TM
385 rcu_read_unlock();
386 if (delegation != NULL)
387 __nfs_inode_return_delegation(inode, delegation);
1da177e4
LT
388 iput(inode);
389 goto restart;
390 }
8383e460 391 rcu_read_unlock();
1da177e4
LT
392}
393
394struct recall_threadargs {
395 struct inode *inode;
adfa6f98 396 struct nfs_client *clp;
1da177e4
LT
397 const nfs4_stateid *stateid;
398
399 struct completion started;
400 int result;
401};
402
403static int recall_thread(void *data)
404{
405 struct recall_threadargs *args = (struct recall_threadargs *)data;
406 struct inode *inode = igrab(args->inode);
7539bbab 407 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1da177e4
LT
408 struct nfs_inode *nfsi = NFS_I(inode);
409 struct nfs_delegation *delegation;
410
411 daemonize("nfsv4-delegreturn");
412
413 nfs_msync_inode(inode);
414 down_read(&clp->cl_sem);
415 down_write(&nfsi->rwsem);
416 spin_lock(&clp->cl_lock);
90163027
TM
417 delegation = nfs_detach_delegation_locked(nfsi, args->stateid);
418 if (delegation != NULL)
1da177e4 419 args->result = 0;
90163027 420 else
1da177e4 421 args->result = -ENOENT;
1da177e4
LT
422 spin_unlock(&clp->cl_lock);
423 complete(&args->started);
90163027 424 nfs_delegation_claim_opens(inode, args->stateid);
1da177e4
LT
425 up_write(&nfsi->rwsem);
426 up_read(&clp->cl_sem);
427 nfs_msync_inode(inode);
428
429 if (delegation != NULL)
e6f81075 430 nfs_do_return_delegation(inode, delegation, 1);
1da177e4
LT
431 iput(inode);
432 module_put_and_exit(0);
433}
434
435/*
436 * Asynchronous delegation recall!
437 */
438int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid)
439{
440 struct recall_threadargs data = {
441 .inode = inode,
442 .stateid = stateid,
443 };
444 int status;
445
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;
453out_module_put:
454 module_put(THIS_MODULE);
455 return status;
456}
457
458/*
459 * Retrieve the inode associated with a delegation
460 */
adfa6f98 461struct inode *nfs_delegation_find_inode(struct nfs_client *clp, const struct nfs_fh *fhandle)
1da177e4
LT
462{
463 struct nfs_delegation *delegation;
464 struct inode *res = NULL;
8383e460
TM
465 rcu_read_lock();
466 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
1da177e4
LT
467 if (nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
468 res = igrab(delegation->inode);
469 break;
470 }
471 }
8383e460 472 rcu_read_unlock();
1da177e4
LT
473 return res;
474}
475
476/*
477 * Mark all delegations as needing to be reclaimed
478 */
adfa6f98 479void nfs_delegation_mark_reclaim(struct nfs_client *clp)
1da177e4
LT
480{
481 struct nfs_delegation *delegation;
8383e460
TM
482 rcu_read_lock();
483 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list)
1da177e4 484 delegation->flags |= NFS_DELEGATION_NEED_RECLAIM;
8383e460 485 rcu_read_unlock();
1da177e4
LT
486}
487
488/*
489 * Reap all unclaimed delegations after reboot recovery is done
490 */
adfa6f98 491void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
1da177e4 492{
8383e460
TM
493 struct nfs_delegation *delegation;
494restart:
495 rcu_read_lock();
496 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
1da177e4
LT
497 if ((delegation->flags & NFS_DELEGATION_NEED_RECLAIM) == 0)
498 continue;
8383e460
TM
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)
905f8d16 504 nfs_free_delegation(delegation);
8383e460 505 goto restart;
1da177e4 506 }
8383e460 507 rcu_read_unlock();
1da177e4 508}
3e4f6290
TM
509
510int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
511{
3e4f6290
TM
512 struct nfs_inode *nfsi = NFS_I(inode);
513 struct nfs_delegation *delegation;
8383e460 514 int ret = 0;
3e4f6290 515
8383e460
TM
516 rcu_read_lock();
517 delegation = rcu_dereference(nfsi->delegation);
3e4f6290
TM
518 if (delegation != NULL) {
519 memcpy(dst->data, delegation->stateid.data, sizeof(dst->data));
8383e460 520 ret = 1;
3e4f6290 521 }
8383e460
TM
522 rcu_read_unlock();
523 return ret;
3e4f6290 524}