NFS: Add an asynchronous delegreturn operation for use in nfs_clear_inode
[deliverable/linux.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
52 for (fl = inode->i_flock; fl != 0; fl = fl->fl_next) {
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",
63 __FUNCTION__, status);
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
128/*
129 * Set up a delegation on an inode
130 */
131int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
132{
7539bbab 133 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1da177e4
LT
134 struct nfs_inode *nfsi = NFS_I(inode);
135 struct nfs_delegation *delegation;
136 int status = 0;
137
f52720ca 138 delegation = kmalloc(sizeof(*delegation), GFP_KERNEL);
1da177e4
LT
139 if (delegation == NULL)
140 return -ENOMEM;
141 memcpy(delegation->stateid.data, res->delegation.data,
142 sizeof(delegation->stateid.data));
143 delegation->type = res->delegation_type;
144 delegation->maxsize = res->maxsize;
beb2a5ec 145 delegation->change_attr = nfsi->change_attr;
1da177e4
LT
146 delegation->cred = get_rpccred(cred);
147 delegation->inode = inode;
148
149 spin_lock(&clp->cl_lock);
8383e460
TM
150 if (rcu_dereference(nfsi->delegation) == NULL) {
151 list_add_rcu(&delegation->super_list, &clp->cl_delegations);
1da177e4 152 nfsi->delegation_state = delegation->type;
8383e460 153 rcu_assign_pointer(nfsi->delegation, delegation);
1da177e4
LT
154 delegation = NULL;
155 } else {
156 if (memcmp(&delegation->stateid, &nfsi->delegation->stateid,
157 sizeof(delegation->stateid)) != 0 ||
158 delegation->type != nfsi->delegation->type) {
5d8515ca
CL
159 printk(KERN_WARNING "%s: server %s handed out "
160 "a duplicate delegation!\n",
161 __FUNCTION__, clp->cl_hostname);
1da177e4
LT
162 status = -EIO;
163 }
164 }
412c77ce
TM
165
166 /* Ensure we revalidate the attributes and page cache! */
167 spin_lock(&inode->i_lock);
168 nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
169 spin_unlock(&inode->i_lock);
170
1da177e4 171 spin_unlock(&clp->cl_lock);
603c83da
TM
172 if (delegation != NULL)
173 nfs_free_delegation(delegation);
1da177e4
LT
174 return status;
175}
176
e6f81075 177static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
1da177e4
LT
178{
179 int res = 0;
180
e6f81075 181 res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid, issync);
905f8d16 182 nfs_free_delegation(delegation);
1da177e4
LT
183 return res;
184}
185
186/* Sync all data to disk upon delegation return */
187static void nfs_msync_inode(struct inode *inode)
188{
189 filemap_fdatawrite(inode->i_mapping);
190 nfs_wb_all(inode);
191 filemap_fdatawait(inode->i_mapping);
192}
193
194/*
195 * Basic procedure for returning a delegation to the server
196 */
90163027 197static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
1da177e4 198{
7539bbab 199 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1da177e4 200 struct nfs_inode *nfsi = NFS_I(inode);
1da177e4
LT
201
202 nfs_msync_inode(inode);
203 down_read(&clp->cl_sem);
204 /* Guard against new delegated open calls */
205 down_write(&nfsi->rwsem);
90163027 206 nfs_delegation_claim_opens(inode, &delegation->stateid);
1da177e4
LT
207 up_write(&nfsi->rwsem);
208 up_read(&clp->cl_sem);
209 nfs_msync_inode(inode);
210
e6f81075 211 return nfs_do_return_delegation(inode, delegation, 1);
90163027
TM
212}
213
214static struct nfs_delegation *nfs_detach_delegation_locked(struct nfs_inode *nfsi, const nfs4_stateid *stateid)
215{
8383e460 216 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
90163027
TM
217
218 if (delegation == NULL)
219 goto nomatch;
220 if (stateid != NULL && memcmp(delegation->stateid.data, stateid->data,
221 sizeof(delegation->stateid.data)) != 0)
222 goto nomatch;
8383e460 223 list_del_rcu(&delegation->super_list);
90163027 224 nfsi->delegation_state = 0;
8383e460 225 rcu_assign_pointer(nfsi->delegation, NULL);
90163027
TM
226 return delegation;
227nomatch:
228 return NULL;
229}
230
e6f81075
TM
231/*
232 * This function returns the delegation without reclaiming opens
233 * or protecting against delegation reclaims.
234 * It is therefore really only safe to be called from
235 * nfs4_clear_inode()
236 */
237void nfs_inode_return_delegation_noreclaim(struct inode *inode)
238{
239 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
240 struct nfs_inode *nfsi = NFS_I(inode);
241 struct nfs_delegation *delegation;
242
243 if (rcu_dereference(nfsi->delegation) != NULL) {
244 spin_lock(&clp->cl_lock);
245 delegation = nfs_detach_delegation_locked(nfsi, NULL);
246 spin_unlock(&clp->cl_lock);
247 if (delegation != NULL)
248 nfs_do_return_delegation(inode, delegation, 0);
249 }
250}
251
90163027
TM
252int nfs_inode_return_delegation(struct inode *inode)
253{
254 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
255 struct nfs_inode *nfsi = NFS_I(inode);
256 struct nfs_delegation *delegation;
257 int err = 0;
258
8383e460 259 if (rcu_dereference(nfsi->delegation) != NULL) {
90163027
TM
260 spin_lock(&clp->cl_lock);
261 delegation = nfs_detach_delegation_locked(nfsi, NULL);
262 spin_unlock(&clp->cl_lock);
263 if (delegation != NULL)
264 err = __nfs_inode_return_delegation(inode, delegation);
265 }
266 return err;
1da177e4
LT
267}
268
269/*
270 * Return all delegations associated to a super block
271 */
272void nfs_return_all_delegations(struct super_block *sb)
273{
7539bbab 274 struct nfs_client *clp = NFS_SB(sb)->nfs_client;
1da177e4
LT
275 struct nfs_delegation *delegation;
276 struct inode *inode;
277
278 if (clp == NULL)
279 return;
280restart:
8383e460
TM
281 rcu_read_lock();
282 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
1da177e4
LT
283 if (delegation->inode->i_sb != sb)
284 continue;
285 inode = igrab(delegation->inode);
286 if (inode == NULL)
287 continue;
8383e460
TM
288 spin_lock(&clp->cl_lock);
289 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
1da177e4 290 spin_unlock(&clp->cl_lock);
8383e460
TM
291 rcu_read_unlock();
292 if (delegation != NULL)
293 __nfs_inode_return_delegation(inode, delegation);
1da177e4
LT
294 iput(inode);
295 goto restart;
296 }
8383e460 297 rcu_read_unlock();
1da177e4
LT
298}
299
10afec90 300static int nfs_do_expire_all_delegations(void *ptr)
58d9714a 301{
adfa6f98 302 struct nfs_client *clp = ptr;
58d9714a
TM
303 struct nfs_delegation *delegation;
304 struct inode *inode;
58d9714a
TM
305
306 allow_signal(SIGKILL);
307restart:
58d9714a
TM
308 if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) != 0)
309 goto out;
310 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0)
311 goto out;
8383e460
TM
312 rcu_read_lock();
313 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
58d9714a
TM
314 inode = igrab(delegation->inode);
315 if (inode == NULL)
316 continue;
8383e460
TM
317 spin_lock(&clp->cl_lock);
318 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
58d9714a 319 spin_unlock(&clp->cl_lock);
8383e460
TM
320 rcu_read_unlock();
321 if (delegation)
322 __nfs_inode_return_delegation(inode, delegation);
58d9714a 323 iput(inode);
26c78e15 324 goto restart;
58d9714a 325 }
8383e460 326 rcu_read_unlock();
58d9714a 327out:
24c8dbbb 328 nfs_put_client(clp);
58d9714a
TM
329 module_put_and_exit(0);
330}
331
adfa6f98 332void nfs_expire_all_delegations(struct nfs_client *clp)
58d9714a
TM
333{
334 struct task_struct *task;
335
336 __module_get(THIS_MODULE);
337 atomic_inc(&clp->cl_count);
338 task = kthread_run(nfs_do_expire_all_delegations, clp,
5d8515ca
CL
339 "%s-delegreturn",
340 rpc_peeraddr2str(clp->cl_rpcclient,
341 RPC_DISPLAY_ADDR));
58d9714a
TM
342 if (!IS_ERR(task))
343 return;
24c8dbbb 344 nfs_put_client(clp);
58d9714a
TM
345 module_put(THIS_MODULE);
346}
347
1da177e4
LT
348/*
349 * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
350 */
adfa6f98 351void nfs_handle_cb_pathdown(struct nfs_client *clp)
1da177e4
LT
352{
353 struct nfs_delegation *delegation;
354 struct inode *inode;
355
356 if (clp == NULL)
357 return;
358restart:
8383e460
TM
359 rcu_read_lock();
360 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
1da177e4
LT
361 inode = igrab(delegation->inode);
362 if (inode == NULL)
363 continue;
8383e460
TM
364 spin_lock(&clp->cl_lock);
365 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
1da177e4 366 spin_unlock(&clp->cl_lock);
8383e460
TM
367 rcu_read_unlock();
368 if (delegation != NULL)
369 __nfs_inode_return_delegation(inode, delegation);
1da177e4
LT
370 iput(inode);
371 goto restart;
372 }
8383e460 373 rcu_read_unlock();
1da177e4
LT
374}
375
376struct recall_threadargs {
377 struct inode *inode;
adfa6f98 378 struct nfs_client *clp;
1da177e4
LT
379 const nfs4_stateid *stateid;
380
381 struct completion started;
382 int result;
383};
384
385static int recall_thread(void *data)
386{
387 struct recall_threadargs *args = (struct recall_threadargs *)data;
388 struct inode *inode = igrab(args->inode);
7539bbab 389 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1da177e4
LT
390 struct nfs_inode *nfsi = NFS_I(inode);
391 struct nfs_delegation *delegation;
392
393 daemonize("nfsv4-delegreturn");
394
395 nfs_msync_inode(inode);
396 down_read(&clp->cl_sem);
397 down_write(&nfsi->rwsem);
398 spin_lock(&clp->cl_lock);
90163027
TM
399 delegation = nfs_detach_delegation_locked(nfsi, args->stateid);
400 if (delegation != NULL)
1da177e4 401 args->result = 0;
90163027 402 else
1da177e4 403 args->result = -ENOENT;
1da177e4
LT
404 spin_unlock(&clp->cl_lock);
405 complete(&args->started);
90163027 406 nfs_delegation_claim_opens(inode, args->stateid);
1da177e4
LT
407 up_write(&nfsi->rwsem);
408 up_read(&clp->cl_sem);
409 nfs_msync_inode(inode);
410
411 if (delegation != NULL)
e6f81075 412 nfs_do_return_delegation(inode, delegation, 1);
1da177e4
LT
413 iput(inode);
414 module_put_and_exit(0);
415}
416
417/*
418 * Asynchronous delegation recall!
419 */
420int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid)
421{
422 struct recall_threadargs data = {
423 .inode = inode,
424 .stateid = stateid,
425 };
426 int status;
427
428 init_completion(&data.started);
429 __module_get(THIS_MODULE);
430 status = kernel_thread(recall_thread, &data, CLONE_KERNEL);
431 if (status < 0)
432 goto out_module_put;
433 wait_for_completion(&data.started);
434 return data.result;
435out_module_put:
436 module_put(THIS_MODULE);
437 return status;
438}
439
440/*
441 * Retrieve the inode associated with a delegation
442 */
adfa6f98 443struct inode *nfs_delegation_find_inode(struct nfs_client *clp, const struct nfs_fh *fhandle)
1da177e4
LT
444{
445 struct nfs_delegation *delegation;
446 struct inode *res = NULL;
8383e460
TM
447 rcu_read_lock();
448 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
1da177e4
LT
449 if (nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
450 res = igrab(delegation->inode);
451 break;
452 }
453 }
8383e460 454 rcu_read_unlock();
1da177e4
LT
455 return res;
456}
457
458/*
459 * Mark all delegations as needing to be reclaimed
460 */
adfa6f98 461void nfs_delegation_mark_reclaim(struct nfs_client *clp)
1da177e4
LT
462{
463 struct nfs_delegation *delegation;
8383e460
TM
464 rcu_read_lock();
465 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list)
1da177e4 466 delegation->flags |= NFS_DELEGATION_NEED_RECLAIM;
8383e460 467 rcu_read_unlock();
1da177e4
LT
468}
469
470/*
471 * Reap all unclaimed delegations after reboot recovery is done
472 */
adfa6f98 473void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
1da177e4 474{
8383e460
TM
475 struct nfs_delegation *delegation;
476restart:
477 rcu_read_lock();
478 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
1da177e4
LT
479 if ((delegation->flags & NFS_DELEGATION_NEED_RECLAIM) == 0)
480 continue;
8383e460
TM
481 spin_lock(&clp->cl_lock);
482 delegation = nfs_detach_delegation_locked(NFS_I(delegation->inode), NULL);
483 spin_unlock(&clp->cl_lock);
484 rcu_read_unlock();
485 if (delegation != NULL)
905f8d16 486 nfs_free_delegation(delegation);
8383e460 487 goto restart;
1da177e4 488 }
8383e460 489 rcu_read_unlock();
1da177e4 490}
3e4f6290
TM
491
492int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
493{
3e4f6290
TM
494 struct nfs_inode *nfsi = NFS_I(inode);
495 struct nfs_delegation *delegation;
8383e460 496 int ret = 0;
3e4f6290 497
8383e460
TM
498 rcu_read_lock();
499 delegation = rcu_dereference(nfsi->delegation);
3e4f6290
TM
500 if (delegation != NULL) {
501 memcpy(dst->data, delegation->stateid.data, sizeof(dst->data));
8383e460 502 ret = 1;
3e4f6290 503 }
8383e460
TM
504 rcu_read_unlock();
505 return ret;
3e4f6290 506}
This page took 0.317628 seconds and 5 git commands to generate.