blob: af8b235d405dba2ea74c260fd13aba31a5ac46f1 [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
52 for (fl = inode->i_flock; fl != 0; fl = fl->fl_next) {
53 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
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{
David Howells7539bba2006-08-22 20:06:09 -0400133 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 struct nfs_inode *nfsi = NFS_I(inode);
135 struct nfs_delegation *delegation;
136 int status = 0;
137
Panagiotis Issarisf52720c2006-09-27 01:49:39 -0700138 delegation = kmalloc(sizeof(*delegation), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 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;
Trond Myklebustbeb2a5e2006-01-03 09:55:37 +0100145 delegation->change_attr = nfsi->change_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 delegation->cred = get_rpccred(cred);
147 delegation->inode = inode;
148
149 spin_lock(&clp->cl_lock);
Trond Myklebust8383e462007-07-06 15:12:04 -0400150 if (rcu_dereference(nfsi->delegation) == NULL) {
151 list_add_rcu(&delegation->super_list, &clp->cl_delegations);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 nfsi->delegation_state = delegation->type;
Trond Myklebust8383e462007-07-06 15:12:04 -0400153 rcu_assign_pointer(nfsi->delegation, delegation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 delegation = NULL;
155 } else {
156 if (memcmp(&delegation->stateid, &nfsi->delegation->stateid,
157 sizeof(delegation->stateid)) != 0 ||
158 delegation->type != nfsi->delegation->type) {
159 printk("%s: server %u.%u.%u.%u, handed out a duplicate delegation!\n",
David Howells24c8dbb2006-08-22 20:06:10 -0400160 __FUNCTION__, NIPQUAD(clp->cl_addr.sin_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 status = -EIO;
162 }
163 }
Trond Myklebust412c77c2007-07-03 16:10:55 -0400164
165 /* Ensure we revalidate the attributes and page cache! */
166 spin_lock(&inode->i_lock);
167 nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
168 spin_unlock(&inode->i_lock);
169
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 spin_unlock(&clp->cl_lock);
Jesper Juhlf99d49a2005-11-07 01:01:34 -0800171 kfree(delegation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 return status;
173}
174
175static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
176{
177 int res = 0;
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid);
Trond Myklebust905f8d12007-08-06 12:18:34 -0400180 nfs_free_delegation(delegation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 return res;
182}
183
184/* Sync all data to disk upon delegation return */
185static void nfs_msync_inode(struct inode *inode)
186{
187 filemap_fdatawrite(inode->i_mapping);
188 nfs_wb_all(inode);
189 filemap_fdatawait(inode->i_mapping);
190}
191
192/*
193 * Basic procedure for returning a delegation to the server
194 */
Trond Myklebust90163022007-07-05 14:55:18 -0400195static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
David Howells7539bba2006-08-22 20:06:09 -0400197 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 struct nfs_inode *nfsi = NFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199
200 nfs_msync_inode(inode);
201 down_read(&clp->cl_sem);
202 /* Guard against new delegated open calls */
203 down_write(&nfsi->rwsem);
Trond Myklebust90163022007-07-05 14:55:18 -0400204 nfs_delegation_claim_opens(inode, &delegation->stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 up_write(&nfsi->rwsem);
206 up_read(&clp->cl_sem);
207 nfs_msync_inode(inode);
208
Trond Myklebust90163022007-07-05 14:55:18 -0400209 return nfs_do_return_delegation(inode, delegation);
210}
211
212static struct nfs_delegation *nfs_detach_delegation_locked(struct nfs_inode *nfsi, const nfs4_stateid *stateid)
213{
Trond Myklebust8383e462007-07-06 15:12:04 -0400214 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust90163022007-07-05 14:55:18 -0400215
216 if (delegation == NULL)
217 goto nomatch;
218 if (stateid != NULL && memcmp(delegation->stateid.data, stateid->data,
219 sizeof(delegation->stateid.data)) != 0)
220 goto nomatch;
Trond Myklebust8383e462007-07-06 15:12:04 -0400221 list_del_rcu(&delegation->super_list);
Trond Myklebust90163022007-07-05 14:55:18 -0400222 nfsi->delegation_state = 0;
Trond Myklebust8383e462007-07-06 15:12:04 -0400223 rcu_assign_pointer(nfsi->delegation, NULL);
Trond Myklebust90163022007-07-05 14:55:18 -0400224 return delegation;
225nomatch:
226 return NULL;
227}
228
229int nfs_inode_return_delegation(struct inode *inode)
230{
231 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
232 struct nfs_inode *nfsi = NFS_I(inode);
233 struct nfs_delegation *delegation;
234 int err = 0;
235
Trond Myklebust8383e462007-07-06 15:12:04 -0400236 if (rcu_dereference(nfsi->delegation) != NULL) {
Trond Myklebust90163022007-07-05 14:55:18 -0400237 spin_lock(&clp->cl_lock);
238 delegation = nfs_detach_delegation_locked(nfsi, NULL);
239 spin_unlock(&clp->cl_lock);
240 if (delegation != NULL)
241 err = __nfs_inode_return_delegation(inode, delegation);
242 }
243 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
246/*
247 * Return all delegations associated to a super block
248 */
249void nfs_return_all_delegations(struct super_block *sb)
250{
David Howells7539bba2006-08-22 20:06:09 -0400251 struct nfs_client *clp = NFS_SB(sb)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 struct nfs_delegation *delegation;
253 struct inode *inode;
254
255 if (clp == NULL)
256 return;
257restart:
Trond Myklebust8383e462007-07-06 15:12:04 -0400258 rcu_read_lock();
259 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 if (delegation->inode->i_sb != sb)
261 continue;
262 inode = igrab(delegation->inode);
263 if (inode == NULL)
264 continue;
Trond Myklebust8383e462007-07-06 15:12:04 -0400265 spin_lock(&clp->cl_lock);
266 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 spin_unlock(&clp->cl_lock);
Trond Myklebust8383e462007-07-06 15:12:04 -0400268 rcu_read_unlock();
269 if (delegation != NULL)
270 __nfs_inode_return_delegation(inode, delegation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 iput(inode);
272 goto restart;
273 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400274 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275}
276
Trond Myklebust10afec92007-05-14 17:16:04 -0400277static int nfs_do_expire_all_delegations(void *ptr)
Trond Myklebust58d97142006-01-03 09:55:24 +0100278{
David Howellsadfa6f92006-08-22 20:06:08 -0400279 struct nfs_client *clp = ptr;
Trond Myklebust58d97142006-01-03 09:55:24 +0100280 struct nfs_delegation *delegation;
281 struct inode *inode;
Trond Myklebust58d97142006-01-03 09:55:24 +0100282
283 allow_signal(SIGKILL);
284restart:
Trond Myklebust58d97142006-01-03 09:55:24 +0100285 if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) != 0)
286 goto out;
287 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0)
288 goto out;
Trond Myklebust8383e462007-07-06 15:12:04 -0400289 rcu_read_lock();
290 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Trond Myklebust58d97142006-01-03 09:55:24 +0100291 inode = igrab(delegation->inode);
292 if (inode == NULL)
293 continue;
Trond Myklebust8383e462007-07-06 15:12:04 -0400294 spin_lock(&clp->cl_lock);
295 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
Trond Myklebust58d97142006-01-03 09:55:24 +0100296 spin_unlock(&clp->cl_lock);
Trond Myklebust8383e462007-07-06 15:12:04 -0400297 rcu_read_unlock();
298 if (delegation)
299 __nfs_inode_return_delegation(inode, delegation);
Trond Myklebust58d97142006-01-03 09:55:24 +0100300 iput(inode);
Trond Myklebust26c78e12006-01-03 09:55:58 +0100301 goto restart;
Trond Myklebust58d97142006-01-03 09:55:24 +0100302 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400303 rcu_read_unlock();
Trond Myklebust58d97142006-01-03 09:55:24 +0100304out:
David Howells24c8dbb2006-08-22 20:06:10 -0400305 nfs_put_client(clp);
Trond Myklebust58d97142006-01-03 09:55:24 +0100306 module_put_and_exit(0);
307}
308
David Howellsadfa6f92006-08-22 20:06:08 -0400309void nfs_expire_all_delegations(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +0100310{
311 struct task_struct *task;
312
313 __module_get(THIS_MODULE);
314 atomic_inc(&clp->cl_count);
315 task = kthread_run(nfs_do_expire_all_delegations, clp,
316 "%u.%u.%u.%u-delegreturn",
David Howells24c8dbb2006-08-22 20:06:10 -0400317 NIPQUAD(clp->cl_addr.sin_addr));
Trond Myklebust58d97142006-01-03 09:55:24 +0100318 if (!IS_ERR(task))
319 return;
David Howells24c8dbb2006-08-22 20:06:10 -0400320 nfs_put_client(clp);
Trond Myklebust58d97142006-01-03 09:55:24 +0100321 module_put(THIS_MODULE);
322}
323
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324/*
325 * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
326 */
David Howellsadfa6f92006-08-22 20:06:08 -0400327void nfs_handle_cb_pathdown(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
329 struct nfs_delegation *delegation;
330 struct inode *inode;
331
332 if (clp == NULL)
333 return;
334restart:
Trond Myklebust8383e462007-07-06 15:12:04 -0400335 rcu_read_lock();
336 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 inode = igrab(delegation->inode);
338 if (inode == NULL)
339 continue;
Trond Myklebust8383e462007-07-06 15:12:04 -0400340 spin_lock(&clp->cl_lock);
341 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 spin_unlock(&clp->cl_lock);
Trond Myklebust8383e462007-07-06 15:12:04 -0400343 rcu_read_unlock();
344 if (delegation != NULL)
345 __nfs_inode_return_delegation(inode, delegation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 iput(inode);
347 goto restart;
348 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400349 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
351
352struct recall_threadargs {
353 struct inode *inode;
David Howellsadfa6f92006-08-22 20:06:08 -0400354 struct nfs_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 const nfs4_stateid *stateid;
356
357 struct completion started;
358 int result;
359};
360
361static int recall_thread(void *data)
362{
363 struct recall_threadargs *args = (struct recall_threadargs *)data;
364 struct inode *inode = igrab(args->inode);
David Howells7539bba2006-08-22 20:06:09 -0400365 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 struct nfs_inode *nfsi = NFS_I(inode);
367 struct nfs_delegation *delegation;
368
369 daemonize("nfsv4-delegreturn");
370
371 nfs_msync_inode(inode);
372 down_read(&clp->cl_sem);
373 down_write(&nfsi->rwsem);
374 spin_lock(&clp->cl_lock);
Trond Myklebust90163022007-07-05 14:55:18 -0400375 delegation = nfs_detach_delegation_locked(nfsi, args->stateid);
376 if (delegation != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 args->result = 0;
Trond Myklebust90163022007-07-05 14:55:18 -0400378 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 args->result = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 spin_unlock(&clp->cl_lock);
381 complete(&args->started);
Trond Myklebust90163022007-07-05 14:55:18 -0400382 nfs_delegation_claim_opens(inode, args->stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 up_write(&nfsi->rwsem);
384 up_read(&clp->cl_sem);
385 nfs_msync_inode(inode);
386
387 if (delegation != NULL)
388 nfs_do_return_delegation(inode, delegation);
389 iput(inode);
390 module_put_and_exit(0);
391}
392
393/*
394 * Asynchronous delegation recall!
395 */
396int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid)
397{
398 struct recall_threadargs data = {
399 .inode = inode,
400 .stateid = stateid,
401 };
402 int status;
403
404 init_completion(&data.started);
405 __module_get(THIS_MODULE);
406 status = kernel_thread(recall_thread, &data, CLONE_KERNEL);
407 if (status < 0)
408 goto out_module_put;
409 wait_for_completion(&data.started);
410 return data.result;
411out_module_put:
412 module_put(THIS_MODULE);
413 return status;
414}
415
416/*
417 * Retrieve the inode associated with a delegation
418 */
David Howellsadfa6f92006-08-22 20:06:08 -0400419struct inode *nfs_delegation_find_inode(struct nfs_client *clp, const struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
421 struct nfs_delegation *delegation;
422 struct inode *res = NULL;
Trond Myklebust8383e462007-07-06 15:12:04 -0400423 rcu_read_lock();
424 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 if (nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
426 res = igrab(delegation->inode);
427 break;
428 }
429 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400430 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 return res;
432}
433
434/*
435 * Mark all delegations as needing to be reclaimed
436 */
David Howellsadfa6f92006-08-22 20:06:08 -0400437void nfs_delegation_mark_reclaim(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
439 struct nfs_delegation *delegation;
Trond Myklebust8383e462007-07-06 15:12:04 -0400440 rcu_read_lock();
441 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 delegation->flags |= NFS_DELEGATION_NEED_RECLAIM;
Trond Myklebust8383e462007-07-06 15:12:04 -0400443 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
446/*
447 * Reap all unclaimed delegations after reboot recovery is done
448 */
David Howellsadfa6f92006-08-22 20:06:08 -0400449void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
Trond Myklebust8383e462007-07-06 15:12:04 -0400451 struct nfs_delegation *delegation;
452restart:
453 rcu_read_lock();
454 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if ((delegation->flags & NFS_DELEGATION_NEED_RECLAIM) == 0)
456 continue;
Trond Myklebust8383e462007-07-06 15:12:04 -0400457 spin_lock(&clp->cl_lock);
458 delegation = nfs_detach_delegation_locked(NFS_I(delegation->inode), NULL);
459 spin_unlock(&clp->cl_lock);
460 rcu_read_unlock();
461 if (delegation != NULL)
Trond Myklebust905f8d12007-08-06 12:18:34 -0400462 nfs_free_delegation(delegation);
Trond Myklebust8383e462007-07-06 15:12:04 -0400463 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400465 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500467
468int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
469{
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500470 struct nfs_inode *nfsi = NFS_I(inode);
471 struct nfs_delegation *delegation;
Trond Myklebust8383e462007-07-06 15:12:04 -0400472 int ret = 0;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500473
Trond Myklebust8383e462007-07-06 15:12:04 -0400474 rcu_read_lock();
475 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500476 if (delegation != NULL) {
477 memcpy(dst->data, delegation->stateid.data, sizeof(dst->data));
Trond Myklebust8383e462007-07-06 15:12:04 -0400478 ret = 1;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500479 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400480 rcu_read_unlock();
481 return ret;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500482}