blob: e0cb4ee3b23eb8261e3394ca7098c8ced5e2af05 [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);
122 delegation->flags &= ~NFS_DELEGATION_NEED_RECLAIM;
123 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
137static struct nfs_delegation *nfs_detach_delegation_locked(struct nfs_inode *nfsi, const nfs4_stateid *stateid)
138{
139 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
140
141 if (delegation == NULL)
142 goto nomatch;
Trond Myklebust34310432008-12-23 15:21:38 -0500143 spin_lock(&delegation->lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500144 if (stateid != NULL && memcmp(delegation->stateid.data, stateid->data,
145 sizeof(delegation->stateid.data)) != 0)
Trond Myklebust34310432008-12-23 15:21:38 -0500146 goto nomatch_unlock;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500147 list_del_rcu(&delegation->super_list);
148 nfsi->delegation_state = 0;
149 rcu_assign_pointer(nfsi->delegation, NULL);
Trond Myklebust34310432008-12-23 15:21:38 -0500150 spin_unlock(&delegation->lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500151 return delegation;
Trond Myklebust34310432008-12-23 15:21:38 -0500152nomatch_unlock:
153 spin_unlock(&delegation->lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500154nomatch:
155 return NULL;
156}
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158/*
159 * Set up a delegation on an inode
160 */
161int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
162{
David Howells7539bba2006-08-22 20:06:09 -0400163 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 struct nfs_inode *nfsi = NFS_I(inode);
165 struct nfs_delegation *delegation;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500166 struct nfs_delegation *freeme = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 int status = 0;
168
Panagiotis Issarisf52720c2006-09-27 01:49:39 -0700169 delegation = kmalloc(sizeof(*delegation), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 if (delegation == NULL)
171 return -ENOMEM;
172 memcpy(delegation->stateid.data, res->delegation.data,
173 sizeof(delegation->stateid.data));
174 delegation->type = res->delegation_type;
175 delegation->maxsize = res->maxsize;
Trond Myklebustbeb2a5e2006-01-03 09:55:37 +0100176 delegation->change_attr = nfsi->change_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 delegation->cred = get_rpccred(cred);
178 delegation->inode = inode;
Trond Myklebust34310432008-12-23 15:21:38 -0500179 spin_lock_init(&delegation->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 spin_lock(&clp->cl_lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500182 if (rcu_dereference(nfsi->delegation) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 if (memcmp(&delegation->stateid, &nfsi->delegation->stateid,
Trond Myklebust57bfa892008-01-25 16:38:18 -0500184 sizeof(delegation->stateid)) == 0 &&
185 delegation->type == nfsi->delegation->type) {
186 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
Trond Myklebust57bfa892008-01-25 16:38:18 -0500188 /*
189 * Deal with broken servers that hand out two
190 * delegations for the same file.
191 */
192 dfprintk(FILE, "%s: server %s handed out "
193 "a duplicate delegation!\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700194 __func__, clp->cl_hostname);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500195 if (delegation->type <= nfsi->delegation->type) {
196 freeme = delegation;
197 delegation = NULL;
198 goto out;
199 }
200 freeme = nfs_detach_delegation_locked(nfsi, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 }
Trond Myklebust57bfa892008-01-25 16:38:18 -0500202 list_add_rcu(&delegation->super_list, &clp->cl_delegations);
203 nfsi->delegation_state = delegation->type;
204 rcu_assign_pointer(nfsi->delegation, delegation);
205 delegation = NULL;
Trond Myklebust412c77c2007-07-03 16:10:55 -0400206
207 /* Ensure we revalidate the attributes and page cache! */
208 spin_lock(&inode->i_lock);
209 nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
210 spin_unlock(&inode->i_lock);
211
Trond Myklebust57bfa892008-01-25 16:38:18 -0500212out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 spin_unlock(&clp->cl_lock);
Trond Myklebust603c83d2007-10-18 19:59:20 -0400214 if (delegation != NULL)
215 nfs_free_delegation(delegation);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500216 if (freeme != NULL)
217 nfs_do_return_delegation(inode, freeme, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 return status;
219}
220
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221/* Sync all data to disk upon delegation return */
222static void nfs_msync_inode(struct inode *inode)
223{
224 filemap_fdatawrite(inode->i_mapping);
225 nfs_wb_all(inode);
226 filemap_fdatawait(inode->i_mapping);
227}
228
229/*
230 * Basic procedure for returning a delegation to the server
231 */
Trond Myklebust90163022007-07-05 14:55:18 -0400232static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
David Howells7539bba2006-08-22 20:06:09 -0400234 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 struct nfs_inode *nfsi = NFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 nfs_msync_inode(inode);
238 down_read(&clp->cl_sem);
239 /* Guard against new delegated open calls */
240 down_write(&nfsi->rwsem);
Trond Myklebust90163022007-07-05 14:55:18 -0400241 nfs_delegation_claim_opens(inode, &delegation->stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 up_write(&nfsi->rwsem);
243 up_read(&clp->cl_sem);
244 nfs_msync_inode(inode);
245
Trond Myklebuste6f81072008-01-24 18:14:34 -0500246 return nfs_do_return_delegation(inode, delegation, 1);
Trond Myklebust90163022007-07-05 14:55:18 -0400247}
248
Trond Myklebuste6f81072008-01-24 18:14:34 -0500249/*
250 * This function returns the delegation without reclaiming opens
251 * or protecting against delegation reclaims.
252 * It is therefore really only safe to be called from
253 * nfs4_clear_inode()
254 */
255void nfs_inode_return_delegation_noreclaim(struct inode *inode)
256{
257 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
258 struct nfs_inode *nfsi = NFS_I(inode);
259 struct nfs_delegation *delegation;
260
261 if (rcu_dereference(nfsi->delegation) != NULL) {
262 spin_lock(&clp->cl_lock);
263 delegation = nfs_detach_delegation_locked(nfsi, NULL);
264 spin_unlock(&clp->cl_lock);
265 if (delegation != NULL)
266 nfs_do_return_delegation(inode, delegation, 0);
267 }
268}
269
Trond Myklebust90163022007-07-05 14:55:18 -0400270int nfs_inode_return_delegation(struct inode *inode)
271{
272 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
273 struct nfs_inode *nfsi = NFS_I(inode);
274 struct nfs_delegation *delegation;
275 int err = 0;
276
Trond Myklebust8383e462007-07-06 15:12:04 -0400277 if (rcu_dereference(nfsi->delegation) != NULL) {
Trond Myklebust90163022007-07-05 14:55:18 -0400278 spin_lock(&clp->cl_lock);
279 delegation = nfs_detach_delegation_locked(nfsi, NULL);
280 spin_unlock(&clp->cl_lock);
281 if (delegation != NULL)
282 err = __nfs_inode_return_delegation(inode, delegation);
283 }
284 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
287/*
288 * Return all delegations associated to a super block
289 */
290void nfs_return_all_delegations(struct super_block *sb)
291{
David Howells7539bba2006-08-22 20:06:09 -0400292 struct nfs_client *clp = NFS_SB(sb)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 struct nfs_delegation *delegation;
294 struct inode *inode;
295
296 if (clp == NULL)
297 return;
298restart:
Trond Myklebust8383e462007-07-06 15:12:04 -0400299 rcu_read_lock();
300 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 if (delegation->inode->i_sb != sb)
302 continue;
303 inode = igrab(delegation->inode);
304 if (inode == NULL)
305 continue;
Trond Myklebust8383e462007-07-06 15:12:04 -0400306 spin_lock(&clp->cl_lock);
307 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 spin_unlock(&clp->cl_lock);
Trond Myklebust8383e462007-07-06 15:12:04 -0400309 rcu_read_unlock();
310 if (delegation != NULL)
311 __nfs_inode_return_delegation(inode, delegation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 iput(inode);
313 goto restart;
314 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400315 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316}
317
Trond Myklebust10afec92007-05-14 17:16:04 -0400318static int nfs_do_expire_all_delegations(void *ptr)
Trond Myklebust58d97142006-01-03 09:55:24 +0100319{
David Howellsadfa6f92006-08-22 20:06:08 -0400320 struct nfs_client *clp = ptr;
Trond Myklebust58d97142006-01-03 09:55:24 +0100321 struct nfs_delegation *delegation;
322 struct inode *inode;
Trond Myklebust58d97142006-01-03 09:55:24 +0100323
324 allow_signal(SIGKILL);
325restart:
Trond Myklebust58d97142006-01-03 09:55:24 +0100326 if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) != 0)
327 goto out;
328 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0)
329 goto out;
Trond Myklebust8383e462007-07-06 15:12:04 -0400330 rcu_read_lock();
331 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Trond Myklebust58d97142006-01-03 09:55:24 +0100332 inode = igrab(delegation->inode);
333 if (inode == NULL)
334 continue;
Trond Myklebust8383e462007-07-06 15:12:04 -0400335 spin_lock(&clp->cl_lock);
336 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
Trond Myklebust58d97142006-01-03 09:55:24 +0100337 spin_unlock(&clp->cl_lock);
Trond Myklebust8383e462007-07-06 15:12:04 -0400338 rcu_read_unlock();
339 if (delegation)
340 __nfs_inode_return_delegation(inode, delegation);
Trond Myklebust58d97142006-01-03 09:55:24 +0100341 iput(inode);
Trond Myklebust26c78e12006-01-03 09:55:58 +0100342 goto restart;
Trond Myklebust58d97142006-01-03 09:55:24 +0100343 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400344 rcu_read_unlock();
Trond Myklebust58d97142006-01-03 09:55:24 +0100345out:
David Howells24c8dbb2006-08-22 20:06:10 -0400346 nfs_put_client(clp);
Trond Myklebust58d97142006-01-03 09:55:24 +0100347 module_put_and_exit(0);
348}
349
David Howellsadfa6f92006-08-22 20:06:08 -0400350void nfs_expire_all_delegations(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +0100351{
352 struct task_struct *task;
353
354 __module_get(THIS_MODULE);
355 atomic_inc(&clp->cl_count);
356 task = kthread_run(nfs_do_expire_all_delegations, clp,
Chuck Lever5d8515c2007-12-10 14:57:16 -0500357 "%s-delegreturn",
358 rpc_peeraddr2str(clp->cl_rpcclient,
359 RPC_DISPLAY_ADDR));
Trond Myklebust58d97142006-01-03 09:55:24 +0100360 if (!IS_ERR(task))
361 return;
David Howells24c8dbb2006-08-22 20:06:10 -0400362 nfs_put_client(clp);
Trond Myklebust58d97142006-01-03 09:55:24 +0100363 module_put(THIS_MODULE);
364}
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366/*
367 * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
368 */
David Howellsadfa6f92006-08-22 20:06:08 -0400369void nfs_handle_cb_pathdown(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
371 struct nfs_delegation *delegation;
372 struct inode *inode;
373
374 if (clp == NULL)
375 return;
376restart:
Trond Myklebust8383e462007-07-06 15:12:04 -0400377 rcu_read_lock();
378 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 inode = igrab(delegation->inode);
380 if (inode == NULL)
381 continue;
Trond Myklebust8383e462007-07-06 15:12:04 -0400382 spin_lock(&clp->cl_lock);
383 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 spin_unlock(&clp->cl_lock);
Trond Myklebust8383e462007-07-06 15:12:04 -0400385 rcu_read_unlock();
386 if (delegation != NULL)
387 __nfs_inode_return_delegation(inode, delegation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 iput(inode);
389 goto restart;
390 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400391 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392}
393
394struct recall_threadargs {
395 struct inode *inode;
David Howellsadfa6f92006-08-22 20:06:08 -0400396 struct nfs_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 const nfs4_stateid *stateid;
398
399 struct completion started;
400 int result;
401};
402
403static int recall_thread(void *data)
404{
405 struct recall_threadargs *args = (struct recall_threadargs *)data;
406 struct inode *inode = igrab(args->inode);
David Howells7539bba2006-08-22 20:06:09 -0400407 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 struct nfs_inode *nfsi = NFS_I(inode);
409 struct nfs_delegation *delegation;
410
411 daemonize("nfsv4-delegreturn");
412
413 nfs_msync_inode(inode);
414 down_read(&clp->cl_sem);
415 down_write(&nfsi->rwsem);
416 spin_lock(&clp->cl_lock);
Trond Myklebust90163022007-07-05 14:55:18 -0400417 delegation = nfs_detach_delegation_locked(nfsi, args->stateid);
418 if (delegation != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 args->result = 0;
Trond Myklebust90163022007-07-05 14:55:18 -0400420 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 args->result = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 spin_unlock(&clp->cl_lock);
423 complete(&args->started);
Trond Myklebust90163022007-07-05 14:55:18 -0400424 nfs_delegation_claim_opens(inode, args->stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 up_write(&nfsi->rwsem);
426 up_read(&clp->cl_sem);
427 nfs_msync_inode(inode);
428
429 if (delegation != NULL)
Trond Myklebuste6f81072008-01-24 18:14:34 -0500430 nfs_do_return_delegation(inode, delegation, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 iput(inode);
432 module_put_and_exit(0);
433}
434
435/*
436 * Asynchronous delegation recall!
437 */
438int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid)
439{
440 struct recall_threadargs data = {
441 .inode = inode,
442 .stateid = stateid,
443 };
444 int status;
445
446 init_completion(&data.started);
447 __module_get(THIS_MODULE);
448 status = kernel_thread(recall_thread, &data, CLONE_KERNEL);
449 if (status < 0)
450 goto out_module_put;
451 wait_for_completion(&data.started);
452 return data.result;
453out_module_put:
454 module_put(THIS_MODULE);
455 return status;
456}
457
458/*
459 * Retrieve the inode associated with a delegation
460 */
David Howellsadfa6f92006-08-22 20:06:08 -0400461struct inode *nfs_delegation_find_inode(struct nfs_client *clp, const struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
463 struct nfs_delegation *delegation;
464 struct inode *res = NULL;
Trond Myklebust8383e462007-07-06 15:12:04 -0400465 rcu_read_lock();
466 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
468 res = igrab(delegation->inode);
469 break;
470 }
471 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400472 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 return res;
474}
475
476/*
477 * Mark all delegations as needing to be reclaimed
478 */
David Howellsadfa6f92006-08-22 20:06:08 -0400479void nfs_delegation_mark_reclaim(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480{
481 struct nfs_delegation *delegation;
Trond Myklebust8383e462007-07-06 15:12:04 -0400482 rcu_read_lock();
483 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 delegation->flags |= NFS_DELEGATION_NEED_RECLAIM;
Trond Myklebust8383e462007-07-06 15:12:04 -0400485 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
488/*
489 * Reap all unclaimed delegations after reboot recovery is done
490 */
David Howellsadfa6f92006-08-22 20:06:08 -0400491void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
Trond Myklebust8383e462007-07-06 15:12:04 -0400493 struct nfs_delegation *delegation;
494restart:
495 rcu_read_lock();
496 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 if ((delegation->flags & NFS_DELEGATION_NEED_RECLAIM) == 0)
498 continue;
Trond Myklebust8383e462007-07-06 15:12:04 -0400499 spin_lock(&clp->cl_lock);
500 delegation = nfs_detach_delegation_locked(NFS_I(delegation->inode), NULL);
501 spin_unlock(&clp->cl_lock);
502 rcu_read_unlock();
503 if (delegation != NULL)
Trond Myklebust905f8d12007-08-06 12:18:34 -0400504 nfs_free_delegation(delegation);
Trond Myklebust8383e462007-07-06 15:12:04 -0400505 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400507 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508}
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500509
510int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
511{
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500512 struct nfs_inode *nfsi = NFS_I(inode);
513 struct nfs_delegation *delegation;
Trond Myklebust8383e462007-07-06 15:12:04 -0400514 int ret = 0;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500515
Trond Myklebust8383e462007-07-06 15:12:04 -0400516 rcu_read_lock();
517 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500518 if (delegation != NULL) {
519 memcpy(dst->data, delegation->stateid.data, sizeof(dst->data));
Trond Myklebust8383e462007-07-06 15:12:04 -0400520 ret = 1;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500521 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400522 rcu_read_unlock();
523 return ret;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500524}