blob: 00a5e4405e16e2c41475f27c28ed7cc75c5a7bf7 [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 Myklebust58d9714a2006-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",
63 __FUNCTION__, status);
64 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;
143 if (stateid != NULL && memcmp(delegation->stateid.data, stateid->data,
144 sizeof(delegation->stateid.data)) != 0)
145 goto nomatch;
146 list_del_rcu(&delegation->super_list);
147 nfsi->delegation_state = 0;
148 rcu_assign_pointer(nfsi->delegation, NULL);
149 return delegation;
150nomatch:
151 return NULL;
152}
153
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154/*
155 * Set up a delegation on an inode
156 */
157int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
158{
David Howells7539bba2006-08-22 20:06:09 -0400159 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 struct nfs_inode *nfsi = NFS_I(inode);
161 struct nfs_delegation *delegation;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500162 struct nfs_delegation *freeme = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 int status = 0;
164
Panagiotis Issarisf52720c2006-09-27 01:49:39 -0700165 delegation = kmalloc(sizeof(*delegation), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 if (delegation == NULL)
167 return -ENOMEM;
168 memcpy(delegation->stateid.data, res->delegation.data,
169 sizeof(delegation->stateid.data));
170 delegation->type = res->delegation_type;
171 delegation->maxsize = res->maxsize;
Trond Myklebustbeb2a5e2006-01-03 09:55:37 +0100172 delegation->change_attr = nfsi->change_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 delegation->cred = get_rpccred(cred);
174 delegation->inode = inode;
175
176 spin_lock(&clp->cl_lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500177 if (rcu_dereference(nfsi->delegation) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 if (memcmp(&delegation->stateid, &nfsi->delegation->stateid,
Trond Myklebust57bfa892008-01-25 16:38:18 -0500179 sizeof(delegation->stateid)) == 0 &&
180 delegation->type == nfsi->delegation->type) {
181 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 }
Trond Myklebust57bfa892008-01-25 16:38:18 -0500183 /*
184 * Deal with broken servers that hand out two
185 * delegations for the same file.
186 */
187 dfprintk(FILE, "%s: server %s handed out "
188 "a duplicate delegation!\n",
189 __FUNCTION__, clp->cl_hostname);
190 if (delegation->type <= nfsi->delegation->type) {
191 freeme = delegation;
192 delegation = NULL;
193 goto out;
194 }
195 freeme = nfs_detach_delegation_locked(nfsi, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
Trond Myklebust57bfa892008-01-25 16:38:18 -0500197 list_add_rcu(&delegation->super_list, &clp->cl_delegations);
198 nfsi->delegation_state = delegation->type;
199 rcu_assign_pointer(nfsi->delegation, delegation);
200 delegation = NULL;
Trond Myklebust412c77c2007-07-03 16:10:55 -0400201
202 /* Ensure we revalidate the attributes and page cache! */
203 spin_lock(&inode->i_lock);
204 nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
205 spin_unlock(&inode->i_lock);
206
Trond Myklebust57bfa892008-01-25 16:38:18 -0500207out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 spin_unlock(&clp->cl_lock);
Trond Myklebust603c83d2007-10-18 19:59:20 -0400209 if (delegation != NULL)
210 nfs_free_delegation(delegation);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500211 if (freeme != NULL)
212 nfs_do_return_delegation(inode, freeme, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 return status;
214}
215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216/* Sync all data to disk upon delegation return */
217static void nfs_msync_inode(struct inode *inode)
218{
219 filemap_fdatawrite(inode->i_mapping);
220 nfs_wb_all(inode);
221 filemap_fdatawait(inode->i_mapping);
222}
223
224/*
225 * Basic procedure for returning a delegation to the server
226 */
Trond Myklebust90163022007-07-05 14:55:18 -0400227static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
David Howells7539bba2006-08-22 20:06:09 -0400229 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 struct nfs_inode *nfsi = NFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 nfs_msync_inode(inode);
233 down_read(&clp->cl_sem);
234 /* Guard against new delegated open calls */
235 down_write(&nfsi->rwsem);
Trond Myklebust90163022007-07-05 14:55:18 -0400236 nfs_delegation_claim_opens(inode, &delegation->stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 up_write(&nfsi->rwsem);
238 up_read(&clp->cl_sem);
239 nfs_msync_inode(inode);
240
Trond Myklebuste6f81072008-01-24 18:14:34 -0500241 return nfs_do_return_delegation(inode, delegation, 1);
Trond Myklebust90163022007-07-05 14:55:18 -0400242}
243
Trond Myklebuste6f81072008-01-24 18:14:34 -0500244/*
245 * This function returns the delegation without reclaiming opens
246 * or protecting against delegation reclaims.
247 * It is therefore really only safe to be called from
248 * nfs4_clear_inode()
249 */
250void nfs_inode_return_delegation_noreclaim(struct inode *inode)
251{
252 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
253 struct nfs_inode *nfsi = NFS_I(inode);
254 struct nfs_delegation *delegation;
255
256 if (rcu_dereference(nfsi->delegation) != NULL) {
257 spin_lock(&clp->cl_lock);
258 delegation = nfs_detach_delegation_locked(nfsi, NULL);
259 spin_unlock(&clp->cl_lock);
260 if (delegation != NULL)
261 nfs_do_return_delegation(inode, delegation, 0);
262 }
263}
264
Trond Myklebust90163022007-07-05 14:55:18 -0400265int nfs_inode_return_delegation(struct inode *inode)
266{
267 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
268 struct nfs_inode *nfsi = NFS_I(inode);
269 struct nfs_delegation *delegation;
270 int err = 0;
271
Trond Myklebust8383e462007-07-06 15:12:04 -0400272 if (rcu_dereference(nfsi->delegation) != NULL) {
Trond Myklebust90163022007-07-05 14:55:18 -0400273 spin_lock(&clp->cl_lock);
274 delegation = nfs_detach_delegation_locked(nfsi, NULL);
275 spin_unlock(&clp->cl_lock);
276 if (delegation != NULL)
277 err = __nfs_inode_return_delegation(inode, delegation);
278 }
279 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
282/*
283 * Return all delegations associated to a super block
284 */
285void nfs_return_all_delegations(struct super_block *sb)
286{
David Howells7539bba2006-08-22 20:06:09 -0400287 struct nfs_client *clp = NFS_SB(sb)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 struct nfs_delegation *delegation;
289 struct inode *inode;
290
291 if (clp == NULL)
292 return;
293restart:
Trond Myklebust8383e462007-07-06 15:12:04 -0400294 rcu_read_lock();
295 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if (delegation->inode->i_sb != sb)
297 continue;
298 inode = igrab(delegation->inode);
299 if (inode == NULL)
300 continue;
Trond Myklebust8383e462007-07-06 15:12:04 -0400301 spin_lock(&clp->cl_lock);
302 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 spin_unlock(&clp->cl_lock);
Trond Myklebust8383e462007-07-06 15:12:04 -0400304 rcu_read_unlock();
305 if (delegation != NULL)
306 __nfs_inode_return_delegation(inode, delegation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 iput(inode);
308 goto restart;
309 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400310 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
Trond Myklebust10afec92007-05-14 17:16:04 -0400313static int nfs_do_expire_all_delegations(void *ptr)
Trond Myklebust58d9714a2006-01-03 09:55:24 +0100314{
David Howellsadfa6f92006-08-22 20:06:08 -0400315 struct nfs_client *clp = ptr;
Trond Myklebust58d9714a2006-01-03 09:55:24 +0100316 struct nfs_delegation *delegation;
317 struct inode *inode;
Trond Myklebust58d9714a2006-01-03 09:55:24 +0100318
319 allow_signal(SIGKILL);
320restart:
Trond Myklebust58d9714a2006-01-03 09:55:24 +0100321 if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) != 0)
322 goto out;
323 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0)
324 goto out;
Trond Myklebust8383e462007-07-06 15:12:04 -0400325 rcu_read_lock();
326 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Trond Myklebust58d9714a2006-01-03 09:55:24 +0100327 inode = igrab(delegation->inode);
328 if (inode == NULL)
329 continue;
Trond Myklebust8383e462007-07-06 15:12:04 -0400330 spin_lock(&clp->cl_lock);
331 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
Trond Myklebust58d9714a2006-01-03 09:55:24 +0100332 spin_unlock(&clp->cl_lock);
Trond Myklebust8383e462007-07-06 15:12:04 -0400333 rcu_read_unlock();
334 if (delegation)
335 __nfs_inode_return_delegation(inode, delegation);
Trond Myklebust58d9714a2006-01-03 09:55:24 +0100336 iput(inode);
Trond Myklebust26c78e12006-01-03 09:55:58 +0100337 goto restart;
Trond Myklebust58d9714a2006-01-03 09:55:24 +0100338 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400339 rcu_read_unlock();
Trond Myklebust58d9714a2006-01-03 09:55:24 +0100340out:
David Howells24c8dbb2006-08-22 20:06:10 -0400341 nfs_put_client(clp);
Trond Myklebust58d9714a2006-01-03 09:55:24 +0100342 module_put_and_exit(0);
343}
344
David Howellsadfa6f92006-08-22 20:06:08 -0400345void nfs_expire_all_delegations(struct nfs_client *clp)
Trond Myklebust58d9714a2006-01-03 09:55:24 +0100346{
347 struct task_struct *task;
348
349 __module_get(THIS_MODULE);
350 atomic_inc(&clp->cl_count);
351 task = kthread_run(nfs_do_expire_all_delegations, clp,
Chuck Lever5d8515c2007-12-10 14:57:16 -0500352 "%s-delegreturn",
353 rpc_peeraddr2str(clp->cl_rpcclient,
354 RPC_DISPLAY_ADDR));
Trond Myklebust58d9714a2006-01-03 09:55:24 +0100355 if (!IS_ERR(task))
356 return;
David Howells24c8dbb2006-08-22 20:06:10 -0400357 nfs_put_client(clp);
Trond Myklebust58d9714a2006-01-03 09:55:24 +0100358 module_put(THIS_MODULE);
359}
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361/*
362 * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
363 */
David Howellsadfa6f92006-08-22 20:06:08 -0400364void nfs_handle_cb_pathdown(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365{
366 struct nfs_delegation *delegation;
367 struct inode *inode;
368
369 if (clp == NULL)
370 return;
371restart:
Trond Myklebust8383e462007-07-06 15:12:04 -0400372 rcu_read_lock();
373 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 inode = igrab(delegation->inode);
375 if (inode == NULL)
376 continue;
Trond Myklebust8383e462007-07-06 15:12:04 -0400377 spin_lock(&clp->cl_lock);
378 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 spin_unlock(&clp->cl_lock);
Trond Myklebust8383e462007-07-06 15:12:04 -0400380 rcu_read_unlock();
381 if (delegation != NULL)
382 __nfs_inode_return_delegation(inode, delegation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 iput(inode);
384 goto restart;
385 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400386 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
389struct recall_threadargs {
390 struct inode *inode;
David Howellsadfa6f92006-08-22 20:06:08 -0400391 struct nfs_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 const nfs4_stateid *stateid;
393
394 struct completion started;
395 int result;
396};
397
398static int recall_thread(void *data)
399{
400 struct recall_threadargs *args = (struct recall_threadargs *)data;
401 struct inode *inode = igrab(args->inode);
David Howells7539bba2006-08-22 20:06:09 -0400402 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 struct nfs_inode *nfsi = NFS_I(inode);
404 struct nfs_delegation *delegation;
405
406 daemonize("nfsv4-delegreturn");
407
408 nfs_msync_inode(inode);
409 down_read(&clp->cl_sem);
410 down_write(&nfsi->rwsem);
411 spin_lock(&clp->cl_lock);
Trond Myklebust90163022007-07-05 14:55:18 -0400412 delegation = nfs_detach_delegation_locked(nfsi, args->stateid);
413 if (delegation != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 args->result = 0;
Trond Myklebust90163022007-07-05 14:55:18 -0400415 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 args->result = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 spin_unlock(&clp->cl_lock);
418 complete(&args->started);
Trond Myklebust90163022007-07-05 14:55:18 -0400419 nfs_delegation_claim_opens(inode, args->stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 up_write(&nfsi->rwsem);
421 up_read(&clp->cl_sem);
422 nfs_msync_inode(inode);
423
424 if (delegation != NULL)
Trond Myklebuste6f81072008-01-24 18:14:34 -0500425 nfs_do_return_delegation(inode, delegation, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 iput(inode);
427 module_put_and_exit(0);
428}
429
430/*
431 * Asynchronous delegation recall!
432 */
433int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid)
434{
435 struct recall_threadargs data = {
436 .inode = inode,
437 .stateid = stateid,
438 };
439 int status;
440
441 init_completion(&data.started);
442 __module_get(THIS_MODULE);
443 status = kernel_thread(recall_thread, &data, CLONE_KERNEL);
444 if (status < 0)
445 goto out_module_put;
446 wait_for_completion(&data.started);
447 return data.result;
448out_module_put:
449 module_put(THIS_MODULE);
450 return status;
451}
452
453/*
454 * Retrieve the inode associated with a delegation
455 */
David Howellsadfa6f92006-08-22 20:06:08 -0400456struct inode *nfs_delegation_find_inode(struct nfs_client *clp, const struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457{
458 struct nfs_delegation *delegation;
459 struct inode *res = NULL;
Trond Myklebust8383e462007-07-06 15:12:04 -0400460 rcu_read_lock();
461 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
463 res = igrab(delegation->inode);
464 break;
465 }
466 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400467 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return res;
469}
470
471/*
472 * Mark all delegations as needing to be reclaimed
473 */
David Howellsadfa6f92006-08-22 20:06:08 -0400474void nfs_delegation_mark_reclaim(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
476 struct nfs_delegation *delegation;
Trond Myklebust8383e462007-07-06 15:12:04 -0400477 rcu_read_lock();
478 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 delegation->flags |= NFS_DELEGATION_NEED_RECLAIM;
Trond Myklebust8383e462007-07-06 15:12:04 -0400480 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481}
482
483/*
484 * Reap all unclaimed delegations after reboot recovery is done
485 */
David Howellsadfa6f92006-08-22 20:06:08 -0400486void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487{
Trond Myklebust8383e462007-07-06 15:12:04 -0400488 struct nfs_delegation *delegation;
489restart:
490 rcu_read_lock();
491 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if ((delegation->flags & NFS_DELEGATION_NEED_RECLAIM) == 0)
493 continue;
Trond Myklebust8383e462007-07-06 15:12:04 -0400494 spin_lock(&clp->cl_lock);
495 delegation = nfs_detach_delegation_locked(NFS_I(delegation->inode), NULL);
496 spin_unlock(&clp->cl_lock);
497 rcu_read_unlock();
498 if (delegation != NULL)
Trond Myklebust905f8d12007-08-06 12:18:34 -0400499 nfs_free_delegation(delegation);
Trond Myklebust8383e462007-07-06 15:12:04 -0400500 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400502 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503}
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500504
505int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
506{
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500507 struct nfs_inode *nfsi = NFS_I(inode);
508 struct nfs_delegation *delegation;
Trond Myklebust8383e462007-07-06 15:12:04 -0400509 int ret = 0;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500510
Trond Myklebust8383e462007-07-06 15:12:04 -0400511 rcu_read_lock();
512 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500513 if (delegation != NULL) {
514 memcpy(dst->data, delegation->stateid.data, sizeof(dst->data));
Trond Myklebust8383e462007-07-06 15:12:04 -0400515 ret = 1;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500516 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400517 rcu_read_unlock();
518 return ret;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500519}