NFSv4: Fix the locking in nfs_inode_reclaim_delegation()
[linux-block.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>
5a0e3ad6 13#include <linux/slab.h>
405f5571 14#include <linux/smp_lock.h>
1da177e4
LT
15#include <linux/spinlock.h>
16
17#include <linux/nfs4.h>
18#include <linux/nfs_fs.h>
19#include <linux/nfs_xdr.h>
20
4ce79717 21#include "nfs4_fs.h"
1da177e4 22#include "delegation.h"
24c8dbbb 23#include "internal.h"
1da177e4 24
905f8d16 25static void nfs_do_free_delegation(struct nfs_delegation *delegation)
1da177e4 26{
1da177e4
LT
27 kfree(delegation);
28}
29
8383e460
TM
30static void nfs_free_delegation_callback(struct rcu_head *head)
31{
32 struct nfs_delegation *delegation = container_of(head, struct nfs_delegation, rcu);
33
905f8d16
TM
34 nfs_do_free_delegation(delegation);
35}
36
37static void nfs_free_delegation(struct nfs_delegation *delegation)
38{
39 struct rpc_cred *cred;
40
41 cred = rcu_dereference(delegation->cred);
42 rcu_assign_pointer(delegation->cred, NULL);
43 call_rcu(&delegation->rcu, nfs_free_delegation_callback);
44 if (cred)
45 put_rpccred(cred);
8383e460
TM
46}
47
b7391f44
TM
48void nfs_mark_delegation_referenced(struct nfs_delegation *delegation)
49{
50 set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags);
51}
52
bd7bf9d5 53int nfs_have_delegation(struct inode *inode, fmode_t flags)
b7391f44
TM
54{
55 struct nfs_delegation *delegation;
56 int ret = 0;
57
58 flags &= FMODE_READ|FMODE_WRITE;
59 rcu_read_lock();
60 delegation = rcu_dereference(NFS_I(inode)->delegation);
61 if (delegation != NULL && (delegation->type & flags) == flags) {
62 nfs_mark_delegation_referenced(delegation);
63 ret = 1;
64 }
65 rcu_read_unlock();
66 return ret;
67}
68
888e694c
TM
69static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state)
70{
71 struct inode *inode = state->inode;
72 struct file_lock *fl;
d5122201 73 int status = 0;
888e694c 74
3f09df70
TM
75 if (inode->i_flock == NULL)
76 goto out;
77
78 /* Protect inode->i_flock using the BKL */
79 lock_kernel();
90dc7d27 80 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
888e694c
TM
81 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
82 continue;
cd3758e3 83 if (nfs_file_open_context(fl->fl_file) != ctx)
888e694c 84 continue;
3f09df70 85 unlock_kernel();
888e694c 86 status = nfs4_lock_delegation_recall(state, fl);
d5122201 87 if (status < 0)
3f09df70
TM
88 goto out;
89 lock_kernel();
888e694c 90 }
3f09df70
TM
91 unlock_kernel();
92out:
888e694c
TM
93 return status;
94}
95
d18cc1fd 96static int nfs_delegation_claim_opens(struct inode *inode, const nfs4_stateid *stateid)
1da177e4
LT
97{
98 struct nfs_inode *nfsi = NFS_I(inode);
99 struct nfs_open_context *ctx;
100 struct nfs4_state *state;
888e694c 101 int err;
1da177e4
LT
102
103again:
104 spin_lock(&inode->i_lock);
105 list_for_each_entry(ctx, &nfsi->open_files, list) {
106 state = ctx->state;
107 if (state == NULL)
108 continue;
109 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
110 continue;
90163027
TM
111 if (memcmp(state->stateid.data, stateid->data, sizeof(state->stateid.data)) != 0)
112 continue;
1da177e4
LT
113 get_nfs_open_context(ctx);
114 spin_unlock(&inode->i_lock);
13437e12 115 err = nfs4_open_delegation_recall(ctx, state, stateid);
888e694c
TM
116 if (err >= 0)
117 err = nfs_delegation_claim_locks(ctx, state);
1da177e4 118 put_nfs_open_context(ctx);
888e694c 119 if (err != 0)
d18cc1fd 120 return err;
1da177e4
LT
121 goto again;
122 }
123 spin_unlock(&inode->i_lock);
d18cc1fd 124 return 0;
1da177e4
LT
125}
126
127/*
128 * Set up a delegation on an inode
129 */
130void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
131{
8f649c37
TM
132 struct nfs_delegation *delegation;
133 struct rpc_cred *oldcred = NULL;
1da177e4 134
8f649c37
TM
135 rcu_read_lock();
136 delegation = rcu_dereference(NFS_I(inode)->delegation);
137 if (delegation != NULL) {
138 spin_lock(&delegation->lock);
139 if (delegation->inode != NULL) {
140 memcpy(delegation->stateid.data, res->delegation.data,
141 sizeof(delegation->stateid.data));
142 delegation->type = res->delegation_type;
143 delegation->maxsize = res->maxsize;
144 oldcred = delegation->cred;
145 delegation->cred = get_rpccred(cred);
146 clear_bit(NFS_DELEGATION_NEED_RECLAIM,
147 &delegation->flags);
148 NFS_I(inode)->delegation_state = delegation->type;
149 spin_unlock(&delegation->lock);
150 put_rpccred(oldcred);
151 rcu_read_unlock();
152 } else {
153 /* We appear to have raced with a delegation return. */
154 spin_unlock(&delegation->lock);
155 rcu_read_unlock();
156 nfs_inode_set_delegation(inode, cred, res);
157 }
158 } else {
159 rcu_read_unlock();
160 }
1da177e4
LT
161}
162
57bfa891
TM
163static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
164{
165 int res = 0;
166
167 res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid, issync);
168 nfs_free_delegation(delegation);
169 return res;
170}
171
86e89489
TM
172static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
173{
174 struct inode *inode = NULL;
175
176 spin_lock(&delegation->lock);
177 if (delegation->inode != NULL)
178 inode = igrab(delegation->inode);
179 spin_unlock(&delegation->lock);
180 return inode;
181}
182
57bfa891
TM
183static struct nfs_delegation *nfs_detach_delegation_locked(struct nfs_inode *nfsi, const nfs4_stateid *stateid)
184{
185 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
186
187 if (delegation == NULL)
188 goto nomatch;
34310430 189 spin_lock(&delegation->lock);
57bfa891
TM
190 if (stateid != NULL && memcmp(delegation->stateid.data, stateid->data,
191 sizeof(delegation->stateid.data)) != 0)
34310430 192 goto nomatch_unlock;
57bfa891 193 list_del_rcu(&delegation->super_list);
86e89489 194 delegation->inode = NULL;
57bfa891
TM
195 nfsi->delegation_state = 0;
196 rcu_assign_pointer(nfsi->delegation, NULL);
34310430 197 spin_unlock(&delegation->lock);
57bfa891 198 return delegation;
34310430
TM
199nomatch_unlock:
200 spin_unlock(&delegation->lock);
57bfa891
TM
201nomatch:
202 return NULL;
203}
204
1da177e4
LT
205/*
206 * Set up a delegation on an inode
207 */
208int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
209{
7539bbab 210 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1da177e4
LT
211 struct nfs_inode *nfsi = NFS_I(inode);
212 struct nfs_delegation *delegation;
57bfa891 213 struct nfs_delegation *freeme = NULL;
1da177e4
LT
214 int status = 0;
215
f52720ca 216 delegation = kmalloc(sizeof(*delegation), GFP_KERNEL);
1da177e4
LT
217 if (delegation == NULL)
218 return -ENOMEM;
219 memcpy(delegation->stateid.data, res->delegation.data,
220 sizeof(delegation->stateid.data));
221 delegation->type = res->delegation_type;
222 delegation->maxsize = res->maxsize;
beb2a5ec 223 delegation->change_attr = nfsi->change_attr;
1da177e4
LT
224 delegation->cred = get_rpccred(cred);
225 delegation->inode = inode;
b7391f44 226 delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
34310430 227 spin_lock_init(&delegation->lock);
1da177e4
LT
228
229 spin_lock(&clp->cl_lock);
57bfa891 230 if (rcu_dereference(nfsi->delegation) != NULL) {
1da177e4 231 if (memcmp(&delegation->stateid, &nfsi->delegation->stateid,
57bfa891
TM
232 sizeof(delegation->stateid)) == 0 &&
233 delegation->type == nfsi->delegation->type) {
234 goto out;
1da177e4 235 }
57bfa891
TM
236 /*
237 * Deal with broken servers that hand out two
238 * delegations for the same file.
239 */
240 dfprintk(FILE, "%s: server %s handed out "
241 "a duplicate delegation!\n",
3110ff80 242 __func__, clp->cl_hostname);
57bfa891
TM
243 if (delegation->type <= nfsi->delegation->type) {
244 freeme = delegation;
245 delegation = NULL;
246 goto out;
247 }
248 freeme = nfs_detach_delegation_locked(nfsi, NULL);
1da177e4 249 }
57bfa891
TM
250 list_add_rcu(&delegation->super_list, &clp->cl_delegations);
251 nfsi->delegation_state = delegation->type;
252 rcu_assign_pointer(nfsi->delegation, delegation);
253 delegation = NULL;
412c77ce
TM
254
255 /* Ensure we revalidate the attributes and page cache! */
256 spin_lock(&inode->i_lock);
257 nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
258 spin_unlock(&inode->i_lock);
259
57bfa891 260out:
1da177e4 261 spin_unlock(&clp->cl_lock);
603c83da
TM
262 if (delegation != NULL)
263 nfs_free_delegation(delegation);
57bfa891
TM
264 if (freeme != NULL)
265 nfs_do_return_delegation(inode, freeme, 0);
1da177e4
LT
266 return status;
267}
268
1da177e4
LT
269/* Sync all data to disk upon delegation return */
270static void nfs_msync_inode(struct inode *inode)
271{
272 filemap_fdatawrite(inode->i_mapping);
273 nfs_wb_all(inode);
274 filemap_fdatawait(inode->i_mapping);
275}
276
277/*
278 * Basic procedure for returning a delegation to the server
279 */
d18cc1fd 280static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
1da177e4 281{
1da177e4 282 struct nfs_inode *nfsi = NFS_I(inode);
d18cc1fd 283 int err;
1da177e4 284
3f09df70
TM
285 /*
286 * Guard against new delegated open/lock/unlock calls and against
287 * state recovery
288 */
1da177e4 289 down_write(&nfsi->rwsem);
d18cc1fd 290 err = nfs_delegation_claim_opens(inode, &delegation->stateid);
1da177e4 291 up_write(&nfsi->rwsem);
d18cc1fd
TM
292 if (err)
293 goto out;
1da177e4 294
d18cc1fd
TM
295 err = nfs_do_return_delegation(inode, delegation, issync);
296out:
297 return err;
90163027
TM
298}
299
515d8611
TM
300/*
301 * Return all delegations that have been marked for return
302 */
d18cc1fd 303int nfs_client_return_marked_delegations(struct nfs_client *clp)
515d8611
TM
304{
305 struct nfs_delegation *delegation;
306 struct inode *inode;
d18cc1fd 307 int err = 0;
515d8611
TM
308
309restart:
310 rcu_read_lock();
311 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
312 if (!test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
313 continue;
314 inode = nfs_delegation_grab_inode(delegation);
315 if (inode == NULL)
316 continue;
317 spin_lock(&clp->cl_lock);
318 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
319 spin_unlock(&clp->cl_lock);
320 rcu_read_unlock();
d18cc1fd
TM
321 if (delegation != NULL) {
322 filemap_flush(inode->i_mapping);
323 err = __nfs_inode_return_delegation(inode, delegation, 0);
324 }
515d8611 325 iput(inode);
d18cc1fd
TM
326 if (!err)
327 goto restart;
328 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
329 return err;
515d8611
TM
330 }
331 rcu_read_unlock();
d18cc1fd 332 return 0;
515d8611
TM
333}
334
e6f81075
TM
335/*
336 * This function returns the delegation without reclaiming opens
337 * or protecting against delegation reclaims.
338 * It is therefore really only safe to be called from
339 * nfs4_clear_inode()
340 */
341void nfs_inode_return_delegation_noreclaim(struct inode *inode)
342{
343 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
344 struct nfs_inode *nfsi = NFS_I(inode);
345 struct nfs_delegation *delegation;
346
347 if (rcu_dereference(nfsi->delegation) != NULL) {
348 spin_lock(&clp->cl_lock);
349 delegation = nfs_detach_delegation_locked(nfsi, NULL);
350 spin_unlock(&clp->cl_lock);
351 if (delegation != NULL)
352 nfs_do_return_delegation(inode, delegation, 0);
353 }
354}
355
90163027
TM
356int nfs_inode_return_delegation(struct inode *inode)
357{
358 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
359 struct nfs_inode *nfsi = NFS_I(inode);
360 struct nfs_delegation *delegation;
361 int err = 0;
362
8383e460 363 if (rcu_dereference(nfsi->delegation) != NULL) {
90163027
TM
364 spin_lock(&clp->cl_lock);
365 delegation = nfs_detach_delegation_locked(nfsi, NULL);
366 spin_unlock(&clp->cl_lock);
d18cc1fd
TM
367 if (delegation != NULL) {
368 nfs_msync_inode(inode);
369 err = __nfs_inode_return_delegation(inode, delegation, 1);
370 }
90163027
TM
371 }
372 return err;
1da177e4
LT
373}
374
6411bd4a
TM
375static void nfs_mark_return_delegation(struct nfs_client *clp, struct nfs_delegation *delegation)
376{
377 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
378 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
379}
380
1da177e4
LT
381/*
382 * Return all delegations associated to a super block
383 */
515d8611 384void nfs_super_return_all_delegations(struct super_block *sb)
1da177e4 385{
7539bbab 386 struct nfs_client *clp = NFS_SB(sb)->nfs_client;
1da177e4 387 struct nfs_delegation *delegation;
1da177e4
LT
388
389 if (clp == NULL)
390 return;
8383e460
TM
391 rcu_read_lock();
392 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
86e89489
TM
393 spin_lock(&delegation->lock);
394 if (delegation->inode != NULL && delegation->inode->i_sb == sb)
515d8611 395 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
86e89489 396 spin_unlock(&delegation->lock);
1da177e4 397 }
8383e460 398 rcu_read_unlock();
d18cc1fd
TM
399 if (nfs_client_return_marked_delegations(clp) != 0)
400 nfs4_schedule_state_manager(clp);
515d8611
TM
401}
402
c79571a5
AB
403static
404void nfs_client_mark_return_all_delegation_types(struct nfs_client *clp, fmode_t flags)
515d8611
TM
405{
406 struct nfs_delegation *delegation;
407
408 rcu_read_lock();
707fb4b3 409 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
c79571a5
AB
410 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
411 continue;
412 if (delegation->type & flags)
413 nfs_mark_return_delegation(clp, delegation);
707fb4b3 414 }
515d8611 415 rcu_read_unlock();
1da177e4
LT
416}
417
c79571a5
AB
418static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
419{
420 nfs_client_mark_return_all_delegation_types(clp, FMODE_READ|FMODE_WRITE);
421}
422
b0d3ded1 423static void nfs_delegation_run_state_manager(struct nfs_client *clp)
58d9714a 424{
b0d3ded1
TM
425 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
426 nfs4_schedule_state_manager(clp);
58d9714a
TM
427}
428
31f09607 429void nfs_expire_all_delegation_types(struct nfs_client *clp, fmode_t flags)
58d9714a 430{
c79571a5 431 nfs_client_mark_return_all_delegation_types(clp, flags);
b0d3ded1 432 nfs_delegation_run_state_manager(clp);
58d9714a
TM
433}
434
c79571a5
AB
435void nfs_expire_all_delegations(struct nfs_client *clp)
436{
437 nfs_expire_all_delegation_types(clp, FMODE_READ|FMODE_WRITE);
438}
439
1da177e4
LT
440/*
441 * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
442 */
adfa6f98 443void nfs_handle_cb_pathdown(struct nfs_client *clp)
1da177e4 444{
1da177e4
LT
445 if (clp == NULL)
446 return;
707fb4b3 447 nfs_client_mark_return_all_delegations(clp);
1da177e4
LT
448}
449
b7391f44
TM
450static void nfs_client_mark_return_unreferenced_delegations(struct nfs_client *clp)
451{
452 struct nfs_delegation *delegation;
453
454 rcu_read_lock();
455 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
456 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
457 continue;
b4a6f496 458 nfs_mark_return_delegation(clp, delegation);
b7391f44
TM
459 }
460 rcu_read_unlock();
461}
462
463void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
464{
465 nfs_client_mark_return_unreferenced_delegations(clp);
466 nfs_delegation_run_state_manager(clp);
467}
468
1da177e4
LT
469/*
470 * Asynchronous delegation recall!
471 */
2597641d
AB
472int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid,
473 int (*validate_stateid)(struct nfs_delegation *delegation,
474 const nfs4_stateid *stateid))
1da177e4 475{
6411bd4a
TM
476 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
477 struct nfs_delegation *delegation;
1da177e4 478
6411bd4a
TM
479 rcu_read_lock();
480 delegation = rcu_dereference(NFS_I(inode)->delegation);
2597641d
AB
481
482 if (!validate_stateid(delegation, stateid)) {
6411bd4a
TM
483 rcu_read_unlock();
484 return -ENOENT;
485 }
2597641d 486
6411bd4a
TM
487 nfs_mark_return_delegation(clp, delegation);
488 rcu_read_unlock();
489 nfs_delegation_run_state_manager(clp);
490 return 0;
1da177e4
LT
491}
492
493/*
494 * Retrieve the inode associated with a delegation
495 */
adfa6f98 496struct inode *nfs_delegation_find_inode(struct nfs_client *clp, const struct nfs_fh *fhandle)
1da177e4
LT
497{
498 struct nfs_delegation *delegation;
499 struct inode *res = NULL;
8383e460
TM
500 rcu_read_lock();
501 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
86e89489
TM
502 spin_lock(&delegation->lock);
503 if (delegation->inode != NULL &&
504 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
1da177e4 505 res = igrab(delegation->inode);
1da177e4 506 }
86e89489
TM
507 spin_unlock(&delegation->lock);
508 if (res != NULL)
509 break;
1da177e4 510 }
8383e460 511 rcu_read_unlock();
1da177e4
LT
512 return res;
513}
514
515/*
516 * Mark all delegations as needing to be reclaimed
517 */
adfa6f98 518void nfs_delegation_mark_reclaim(struct nfs_client *clp)
1da177e4
LT
519{
520 struct nfs_delegation *delegation;
8383e460
TM
521 rcu_read_lock();
522 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list)
15c831bf 523 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
8383e460 524 rcu_read_unlock();
1da177e4
LT
525}
526
527/*
528 * Reap all unclaimed delegations after reboot recovery is done
529 */
adfa6f98 530void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
1da177e4 531{
8383e460 532 struct nfs_delegation *delegation;
86e89489 533 struct inode *inode;
8383e460
TM
534restart:
535 rcu_read_lock();
536 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
15c831bf 537 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0)
1da177e4 538 continue;
86e89489
TM
539 inode = nfs_delegation_grab_inode(delegation);
540 if (inode == NULL)
541 continue;
8383e460 542 spin_lock(&clp->cl_lock);
86e89489 543 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
8383e460
TM
544 spin_unlock(&clp->cl_lock);
545 rcu_read_unlock();
546 if (delegation != NULL)
905f8d16 547 nfs_free_delegation(delegation);
86e89489 548 iput(inode);
8383e460 549 goto restart;
1da177e4 550 }
8383e460 551 rcu_read_unlock();
1da177e4 552}
3e4f6290
TM
553
554int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
555{
3e4f6290
TM
556 struct nfs_inode *nfsi = NFS_I(inode);
557 struct nfs_delegation *delegation;
8383e460 558 int ret = 0;
3e4f6290 559
8383e460
TM
560 rcu_read_lock();
561 delegation = rcu_dereference(nfsi->delegation);
3e4f6290
TM
562 if (delegation != NULL) {
563 memcpy(dst->data, delegation->stateid.data, sizeof(dst->data));
8383e460 564 ret = 1;
3e4f6290 565 }
8383e460
TM
566 rcu_read_unlock();
567 return ret;
3e4f6290 568}