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