blob: ff2c1585d23fd076ee1aba27f8dfea7d487c888c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/nfs/delegation.c
3 *
4 * Copyright (C) 2004 Trond Myklebust
5 *
6 * NFS file delegation management
7 *
8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/completion.h>
Trond Myklebust58d97142006-01-03 09:55:24 +010010#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#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
Trond Myklebust4ce79712005-06-22 17:16:21 +000019#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "delegation.h"
David Howells24c8dbb2006-08-22 20:06:10 -040021#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Trond Myklebust905f8d12007-08-06 12:18:34 -040023static void nfs_do_free_delegation(struct nfs_delegation *delegation)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024{
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 kfree(delegation);
26}
27
Trond Myklebust8383e462007-07-06 15:12:04 -040028static void nfs_free_delegation_callback(struct rcu_head *head)
29{
30 struct nfs_delegation *delegation = container_of(head, struct nfs_delegation, rcu);
31
Trond Myklebust905f8d12007-08-06 12:18:34 -040032 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);
Trond Myklebust8383e462007-07-06 15:12:04 -040044}
45
Trond Myklebust888e6942005-11-04 15:38:11 -050046static 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
Harvey Harrison90dc7d22008-02-20 13:03:05 -080052 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
Trond Myklebust888e6942005-11-04 15:38:11 -050053 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
54 continue;
Trond Myklebustcd3758e2007-08-10 17:44:32 -040055 if (nfs_file_open_context(fl->fl_file) != ctx)
Trond Myklebust888e6942005-11-04 15:38:11 -050056 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",
Harvey Harrison3110ff82008-05-02 13:42:44 -070063 __func__, status);
Trond Myklebust888e6942005-11-04 15:38:11 -050064 case -NFS4ERR_EXPIRED:
65 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
66 case -NFS4ERR_STALE_CLIENTID:
David Howells7539bba2006-08-22 20:06:09 -040067 nfs4_schedule_state_recovery(NFS_SERVER(inode)->nfs_client);
Trond Myklebust888e6942005-11-04 15:38:11 -050068 goto out_err;
69 }
70 }
71 return 0;
72out_err:
73 return status;
74}
75
Trond Myklebust90163022007-07-05 14:55:18 -040076static void nfs_delegation_claim_opens(struct inode *inode, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
78 struct nfs_inode *nfsi = NFS_I(inode);
79 struct nfs_open_context *ctx;
80 struct nfs4_state *state;
Trond Myklebust888e6942005-11-04 15:38:11 -050081 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
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;
Trond Myklebust90163022007-07-05 14:55:18 -040091 if (memcmp(state->stateid.data, stateid->data, sizeof(state->stateid.data)) != 0)
92 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 get_nfs_open_context(ctx);
94 spin_unlock(&inode->i_lock);
Trond Myklebust13437e12007-07-06 15:10:43 -040095 err = nfs4_open_delegation_recall(ctx, state, stateid);
Trond Myklebust888e6942005-11-04 15:38:11 -050096 if (err >= 0)
97 err = nfs_delegation_claim_locks(ctx, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 put_nfs_open_context(ctx);
Trond Myklebust888e6942005-11-04 15:38:11 -050099 if (err != 0)
100 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 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;
Trond Myklebust05c88ba2007-10-11 15:11:51 -0400112 struct rpc_cred *oldcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
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;
Trond Myklebust05c88ba2007-10-11 15:11:51 -0400120 oldcred = delegation->cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 delegation->cred = get_rpccred(cred);
Trond Myklebust15c831b2008-12-23 15:21:39 -0500122 clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 NFS_I(inode)->delegation_state = delegation->type;
124 smp_wmb();
Trond Myklebust05c88ba2007-10-11 15:11:51 -0400125 put_rpccred(oldcred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
Trond Myklebust57bfa892008-01-25 16:38:18 -0500128static 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
Trond Myklebust86e89482008-12-23 15:21:39 -0500137static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
138{
139 struct inode *inode = NULL;
140
141 spin_lock(&delegation->lock);
142 if (delegation->inode != NULL)
143 inode = igrab(delegation->inode);
144 spin_unlock(&delegation->lock);
145 return inode;
146}
147
Trond Myklebust57bfa892008-01-25 16:38:18 -0500148static struct nfs_delegation *nfs_detach_delegation_locked(struct nfs_inode *nfsi, const nfs4_stateid *stateid)
149{
150 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
151
152 if (delegation == NULL)
153 goto nomatch;
Trond Myklebust34310432008-12-23 15:21:38 -0500154 spin_lock(&delegation->lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500155 if (stateid != NULL && memcmp(delegation->stateid.data, stateid->data,
156 sizeof(delegation->stateid.data)) != 0)
Trond Myklebust34310432008-12-23 15:21:38 -0500157 goto nomatch_unlock;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500158 list_del_rcu(&delegation->super_list);
Trond Myklebust86e89482008-12-23 15:21:39 -0500159 delegation->inode = NULL;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500160 nfsi->delegation_state = 0;
161 rcu_assign_pointer(nfsi->delegation, NULL);
Trond Myklebust34310432008-12-23 15:21:38 -0500162 spin_unlock(&delegation->lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500163 return delegation;
Trond Myklebust34310432008-12-23 15:21:38 -0500164nomatch_unlock:
165 spin_unlock(&delegation->lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500166nomatch:
167 return NULL;
168}
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170/*
171 * Set up a delegation on an inode
172 */
173int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
174{
David Howells7539bba2006-08-22 20:06:09 -0400175 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 struct nfs_inode *nfsi = NFS_I(inode);
177 struct nfs_delegation *delegation;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500178 struct nfs_delegation *freeme = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 int status = 0;
180
Panagiotis Issarisf52720c2006-09-27 01:49:39 -0700181 delegation = kmalloc(sizeof(*delegation), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 if (delegation == NULL)
183 return -ENOMEM;
184 memcpy(delegation->stateid.data, res->delegation.data,
185 sizeof(delegation->stateid.data));
186 delegation->type = res->delegation_type;
187 delegation->maxsize = res->maxsize;
Trond Myklebustbeb2a5e2006-01-03 09:55:37 +0100188 delegation->change_attr = nfsi->change_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 delegation->cred = get_rpccred(cred);
190 delegation->inode = inode;
Trond Myklebust34310432008-12-23 15:21:38 -0500191 spin_lock_init(&delegation->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
193 spin_lock(&clp->cl_lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500194 if (rcu_dereference(nfsi->delegation) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 if (memcmp(&delegation->stateid, &nfsi->delegation->stateid,
Trond Myklebust57bfa892008-01-25 16:38:18 -0500196 sizeof(delegation->stateid)) == 0 &&
197 delegation->type == nfsi->delegation->type) {
198 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
Trond Myklebust57bfa892008-01-25 16:38:18 -0500200 /*
201 * Deal with broken servers that hand out two
202 * delegations for the same file.
203 */
204 dfprintk(FILE, "%s: server %s handed out "
205 "a duplicate delegation!\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700206 __func__, clp->cl_hostname);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500207 if (delegation->type <= nfsi->delegation->type) {
208 freeme = delegation;
209 delegation = NULL;
210 goto out;
211 }
212 freeme = nfs_detach_delegation_locked(nfsi, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 }
Trond Myklebust57bfa892008-01-25 16:38:18 -0500214 list_add_rcu(&delegation->super_list, &clp->cl_delegations);
215 nfsi->delegation_state = delegation->type;
216 rcu_assign_pointer(nfsi->delegation, delegation);
217 delegation = NULL;
Trond Myklebust412c77c2007-07-03 16:10:55 -0400218
219 /* Ensure we revalidate the attributes and page cache! */
220 spin_lock(&inode->i_lock);
221 nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
222 spin_unlock(&inode->i_lock);
223
Trond Myklebust57bfa892008-01-25 16:38:18 -0500224out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 spin_unlock(&clp->cl_lock);
Trond Myklebust603c83d2007-10-18 19:59:20 -0400226 if (delegation != NULL)
227 nfs_free_delegation(delegation);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500228 if (freeme != NULL)
229 nfs_do_return_delegation(inode, freeme, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 return status;
231}
232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233/* Sync all data to disk upon delegation return */
234static void nfs_msync_inode(struct inode *inode)
235{
236 filemap_fdatawrite(inode->i_mapping);
237 nfs_wb_all(inode);
238 filemap_fdatawait(inode->i_mapping);
239}
240
241/*
242 * Basic procedure for returning a delegation to the server
243 */
Trond Myklebust90163022007-07-05 14:55:18 -0400244static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 struct nfs_inode *nfsi = NFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 nfs_msync_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 /* Guard against new delegated open calls */
250 down_write(&nfsi->rwsem);
Trond Myklebust90163022007-07-05 14:55:18 -0400251 nfs_delegation_claim_opens(inode, &delegation->stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 up_write(&nfsi->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 nfs_msync_inode(inode);
254
Trond Myklebuste6f81072008-01-24 18:14:34 -0500255 return nfs_do_return_delegation(inode, delegation, 1);
Trond Myklebust90163022007-07-05 14:55:18 -0400256}
257
Trond Myklebuste6f81072008-01-24 18:14:34 -0500258/*
Trond Myklebust515d8612008-12-23 15:21:46 -0500259 * Return all delegations that have been marked for return
260 */
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500261void nfs_client_return_marked_delegations(struct nfs_client *clp)
Trond Myklebust515d8612008-12-23 15:21:46 -0500262{
263 struct nfs_delegation *delegation;
264 struct inode *inode;
265
266restart:
267 rcu_read_lock();
268 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
269 if (!test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
270 continue;
271 inode = nfs_delegation_grab_inode(delegation);
272 if (inode == NULL)
273 continue;
274 spin_lock(&clp->cl_lock);
275 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
276 spin_unlock(&clp->cl_lock);
277 rcu_read_unlock();
278 if (delegation != NULL)
279 __nfs_inode_return_delegation(inode, delegation);
280 iput(inode);
281 goto restart;
282 }
283 rcu_read_unlock();
284}
285
286/*
Trond Myklebuste6f81072008-01-24 18:14:34 -0500287 * This function returns the delegation without reclaiming opens
288 * or protecting against delegation reclaims.
289 * It is therefore really only safe to be called from
290 * nfs4_clear_inode()
291 */
292void nfs_inode_return_delegation_noreclaim(struct inode *inode)
293{
294 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
295 struct nfs_inode *nfsi = NFS_I(inode);
296 struct nfs_delegation *delegation;
297
298 if (rcu_dereference(nfsi->delegation) != NULL) {
299 spin_lock(&clp->cl_lock);
300 delegation = nfs_detach_delegation_locked(nfsi, NULL);
301 spin_unlock(&clp->cl_lock);
302 if (delegation != NULL)
303 nfs_do_return_delegation(inode, delegation, 0);
304 }
305}
306
Trond Myklebust90163022007-07-05 14:55:18 -0400307int nfs_inode_return_delegation(struct inode *inode)
308{
309 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
310 struct nfs_inode *nfsi = NFS_I(inode);
311 struct nfs_delegation *delegation;
312 int err = 0;
313
Trond Myklebust8383e462007-07-06 15:12:04 -0400314 if (rcu_dereference(nfsi->delegation) != NULL) {
Trond Myklebust90163022007-07-05 14:55:18 -0400315 spin_lock(&clp->cl_lock);
316 delegation = nfs_detach_delegation_locked(nfsi, NULL);
317 spin_unlock(&clp->cl_lock);
318 if (delegation != NULL)
319 err = __nfs_inode_return_delegation(inode, delegation);
320 }
321 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322}
323
324/*
325 * Return all delegations associated to a super block
326 */
Trond Myklebust515d8612008-12-23 15:21:46 -0500327void nfs_super_return_all_delegations(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
David Howells7539bba2006-08-22 20:06:09 -0400329 struct nfs_client *clp = NFS_SB(sb)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 struct nfs_delegation *delegation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 if (clp == NULL)
333 return;
Trond Myklebust8383e462007-07-06 15:12:04 -0400334 rcu_read_lock();
335 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Trond Myklebust86e89482008-12-23 15:21:39 -0500336 spin_lock(&delegation->lock);
337 if (delegation->inode != NULL && delegation->inode->i_sb == sb)
Trond Myklebust515d8612008-12-23 15:21:46 -0500338 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
Trond Myklebust86e89482008-12-23 15:21:39 -0500339 spin_unlock(&delegation->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400341 rcu_read_unlock();
Trond Myklebust515d8612008-12-23 15:21:46 -0500342 nfs_client_return_marked_delegations(clp);
343}
344
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500345static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
Trond Myklebust515d8612008-12-23 15:21:46 -0500346{
347 struct nfs_delegation *delegation;
348
349 rcu_read_lock();
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500350 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Trond Myklebust515d8612008-12-23 15:21:46 -0500351 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500352 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
353 }
Trond Myklebust515d8612008-12-23 15:21:46 -0500354 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
356
Trond Myklebust10afec902007-05-14 17:16:04 -0400357static int nfs_do_expire_all_delegations(void *ptr)
Trond Myklebust58d97142006-01-03 09:55:24 +0100358{
David Howellsadfa6f92006-08-22 20:06:08 -0400359 struct nfs_client *clp = ptr;
Trond Myklebust58d97142006-01-03 09:55:24 +0100360
361 allow_signal(SIGKILL);
Trond Myklebust515d8612008-12-23 15:21:46 -0500362
Trond Myklebust58d97142006-01-03 09:55:24 +0100363 if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) != 0)
364 goto out;
365 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0)
366 goto out;
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500367 nfs_client_mark_return_all_delegations(clp);
368 nfs_client_return_marked_delegations(clp);
Trond Myklebust58d97142006-01-03 09:55:24 +0100369out:
David Howells24c8dbb2006-08-22 20:06:10 -0400370 nfs_put_client(clp);
Trond Myklebust58d97142006-01-03 09:55:24 +0100371 module_put_and_exit(0);
372}
373
David Howellsadfa6f92006-08-22 20:06:08 -0400374void nfs_expire_all_delegations(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +0100375{
376 struct task_struct *task;
377
378 __module_get(THIS_MODULE);
379 atomic_inc(&clp->cl_count);
380 task = kthread_run(nfs_do_expire_all_delegations, clp,
Chuck Lever5d8515c2007-12-10 14:57:16 -0500381 "%s-delegreturn",
382 rpc_peeraddr2str(clp->cl_rpcclient,
383 RPC_DISPLAY_ADDR));
Trond Myklebust58d97142006-01-03 09:55:24 +0100384 if (!IS_ERR(task))
385 return;
David Howells24c8dbb2006-08-22 20:06:10 -0400386 nfs_put_client(clp);
Trond Myklebust58d97142006-01-03 09:55:24 +0100387 module_put(THIS_MODULE);
388}
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390/*
391 * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
392 */
David Howellsadfa6f92006-08-22 20:06:08 -0400393void nfs_handle_cb_pathdown(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (clp == NULL)
396 return;
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500397 nfs_client_mark_return_all_delegations(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
400struct recall_threadargs {
401 struct inode *inode;
David Howellsadfa6f92006-08-22 20:06:08 -0400402 struct nfs_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 const nfs4_stateid *stateid;
404
405 struct completion started;
406 int result;
407};
408
409static int recall_thread(void *data)
410{
411 struct recall_threadargs *args = (struct recall_threadargs *)data;
412 struct inode *inode = igrab(args->inode);
David Howells7539bba2006-08-22 20:06:09 -0400413 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 struct nfs_inode *nfsi = NFS_I(inode);
415 struct nfs_delegation *delegation;
416
417 daemonize("nfsv4-delegreturn");
418
419 nfs_msync_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 down_write(&nfsi->rwsem);
421 spin_lock(&clp->cl_lock);
Trond Myklebust90163022007-07-05 14:55:18 -0400422 delegation = nfs_detach_delegation_locked(nfsi, args->stateid);
423 if (delegation != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 args->result = 0;
Trond Myklebust90163022007-07-05 14:55:18 -0400425 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 args->result = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 spin_unlock(&clp->cl_lock);
428 complete(&args->started);
Trond Myklebust90163022007-07-05 14:55:18 -0400429 nfs_delegation_claim_opens(inode, args->stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 up_write(&nfsi->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 nfs_msync_inode(inode);
432
433 if (delegation != NULL)
Trond Myklebuste6f81072008-01-24 18:14:34 -0500434 nfs_do_return_delegation(inode, delegation, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 iput(inode);
436 module_put_and_exit(0);
437}
438
439/*
440 * Asynchronous delegation recall!
441 */
442int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid)
443{
444 struct recall_threadargs data = {
445 .inode = inode,
446 .stateid = stateid,
447 };
448 int status;
449
450 init_completion(&data.started);
451 __module_get(THIS_MODULE);
452 status = kernel_thread(recall_thread, &data, CLONE_KERNEL);
453 if (status < 0)
454 goto out_module_put;
455 wait_for_completion(&data.started);
456 return data.result;
457out_module_put:
458 module_put(THIS_MODULE);
459 return status;
460}
461
462/*
463 * Retrieve the inode associated with a delegation
464 */
David Howellsadfa6f92006-08-22 20:06:08 -0400465struct inode *nfs_delegation_find_inode(struct nfs_client *clp, const struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
467 struct nfs_delegation *delegation;
468 struct inode *res = NULL;
Trond Myklebust8383e462007-07-06 15:12:04 -0400469 rcu_read_lock();
470 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Trond Myklebust86e89482008-12-23 15:21:39 -0500471 spin_lock(&delegation->lock);
472 if (delegation->inode != NULL &&
473 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 res = igrab(delegation->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 }
Trond Myklebust86e89482008-12-23 15:21:39 -0500476 spin_unlock(&delegation->lock);
477 if (res != NULL)
478 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400480 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 return res;
482}
483
484/*
485 * Mark all delegations as needing to be reclaimed
486 */
David Howellsadfa6f92006-08-22 20:06:08 -0400487void nfs_delegation_mark_reclaim(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
489 struct nfs_delegation *delegation;
Trond Myklebust8383e462007-07-06 15:12:04 -0400490 rcu_read_lock();
491 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list)
Trond Myklebust15c831b2008-12-23 15:21:39 -0500492 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
Trond Myklebust8383e462007-07-06 15:12:04 -0400493 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
496/*
497 * Reap all unclaimed delegations after reboot recovery is done
498 */
David Howellsadfa6f92006-08-22 20:06:08 -0400499void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Trond Myklebust8383e462007-07-06 15:12:04 -0400501 struct nfs_delegation *delegation;
Trond Myklebust86e89482008-12-23 15:21:39 -0500502 struct inode *inode;
Trond Myklebust8383e462007-07-06 15:12:04 -0400503restart:
504 rcu_read_lock();
505 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Trond Myklebust15c831b2008-12-23 15:21:39 -0500506 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 continue;
Trond Myklebust86e89482008-12-23 15:21:39 -0500508 inode = nfs_delegation_grab_inode(delegation);
509 if (inode == NULL)
510 continue;
Trond Myklebust8383e462007-07-06 15:12:04 -0400511 spin_lock(&clp->cl_lock);
Trond Myklebust86e89482008-12-23 15:21:39 -0500512 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
Trond Myklebust8383e462007-07-06 15:12:04 -0400513 spin_unlock(&clp->cl_lock);
514 rcu_read_unlock();
515 if (delegation != NULL)
Trond Myklebust905f8d12007-08-06 12:18:34 -0400516 nfs_free_delegation(delegation);
Trond Myklebust86e89482008-12-23 15:21:39 -0500517 iput(inode);
Trond Myklebust8383e462007-07-06 15:12:04 -0400518 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400520 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500522
523int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
524{
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500525 struct nfs_inode *nfsi = NFS_I(inode);
526 struct nfs_delegation *delegation;
Trond Myklebust8383e462007-07-06 15:12:04 -0400527 int ret = 0;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500528
Trond Myklebust8383e462007-07-06 15:12:04 -0400529 rcu_read_lock();
530 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500531 if (delegation != NULL) {
532 memcpy(dst->data, delegation->stateid.data, sizeof(dst->data));
Trond Myklebust8383e462007-07-06 15:12:04 -0400533 ret = 1;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500534 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400535 rcu_read_unlock();
536 return ret;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500537}