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