blob: b03dcd8403f1d61944f991e2dfe0b17b751986c1 [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) {
Chuck Lever5d8515c2007-12-10 14:57:16 -0500159 printk(KERN_WARNING "%s: server %s handed out "
160 "a duplicate delegation!\n",
161 __FUNCTION__, clp->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 status = -EIO;
163 }
164 }
Trond Myklebust412c77c2007-07-03 16:10:55 -0400165
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 spin_unlock(&clp->cl_lock);
Trond Myklebust603c83d2007-10-18 19:59:20 -0400172 if (delegation != NULL)
173 nfs_free_delegation(delegation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return status;
175}
176
177static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
178{
179 int res = 0;
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid);
Trond Myklebust905f8d12007-08-06 12:18:34 -0400182 nfs_free_delegation(delegation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 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 */
Trond Myklebust90163022007-07-05 14:55:18 -0400197static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
David Howells7539bba2006-08-22 20:06:09 -0400199 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 struct nfs_inode *nfsi = NFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 nfs_msync_inode(inode);
203 down_read(&clp->cl_sem);
204 /* Guard against new delegated open calls */
205 down_write(&nfsi->rwsem);
Trond Myklebust90163022007-07-05 14:55:18 -0400206 nfs_delegation_claim_opens(inode, &delegation->stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 up_write(&nfsi->rwsem);
208 up_read(&clp->cl_sem);
209 nfs_msync_inode(inode);
210
Trond Myklebust90163022007-07-05 14:55:18 -0400211 return nfs_do_return_delegation(inode, delegation);
212}
213
214static struct nfs_delegation *nfs_detach_delegation_locked(struct nfs_inode *nfsi, const nfs4_stateid *stateid)
215{
Trond Myklebust8383e462007-07-06 15:12:04 -0400216 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust90163022007-07-05 14:55:18 -0400217
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;
Trond Myklebust8383e462007-07-06 15:12:04 -0400223 list_del_rcu(&delegation->super_list);
Trond Myklebust90163022007-07-05 14:55:18 -0400224 nfsi->delegation_state = 0;
Trond Myklebust8383e462007-07-06 15:12:04 -0400225 rcu_assign_pointer(nfsi->delegation, NULL);
Trond Myklebust90163022007-07-05 14:55:18 -0400226 return delegation;
227nomatch:
228 return NULL;
229}
230
231int nfs_inode_return_delegation(struct inode *inode)
232{
233 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
234 struct nfs_inode *nfsi = NFS_I(inode);
235 struct nfs_delegation *delegation;
236 int err = 0;
237
Trond Myklebust8383e462007-07-06 15:12:04 -0400238 if (rcu_dereference(nfsi->delegation) != NULL) {
Trond Myklebust90163022007-07-05 14:55:18 -0400239 spin_lock(&clp->cl_lock);
240 delegation = nfs_detach_delegation_locked(nfsi, NULL);
241 spin_unlock(&clp->cl_lock);
242 if (delegation != NULL)
243 err = __nfs_inode_return_delegation(inode, delegation);
244 }
245 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246}
247
248/*
249 * Return all delegations associated to a super block
250 */
251void nfs_return_all_delegations(struct super_block *sb)
252{
David Howells7539bba2006-08-22 20:06:09 -0400253 struct nfs_client *clp = NFS_SB(sb)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 struct nfs_delegation *delegation;
255 struct inode *inode;
256
257 if (clp == NULL)
258 return;
259restart:
Trond Myklebust8383e462007-07-06 15:12:04 -0400260 rcu_read_lock();
261 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 if (delegation->inode->i_sb != sb)
263 continue;
264 inode = igrab(delegation->inode);
265 if (inode == NULL)
266 continue;
Trond Myklebust8383e462007-07-06 15:12:04 -0400267 spin_lock(&clp->cl_lock);
268 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 spin_unlock(&clp->cl_lock);
Trond Myklebust8383e462007-07-06 15:12:04 -0400270 rcu_read_unlock();
271 if (delegation != NULL)
272 __nfs_inode_return_delegation(inode, delegation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 iput(inode);
274 goto restart;
275 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400276 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
Trond Myklebust10afec92007-05-14 17:16:04 -0400279static int nfs_do_expire_all_delegations(void *ptr)
Trond Myklebust58d97142006-01-03 09:55:24 +0100280{
David Howellsadfa6f92006-08-22 20:06:08 -0400281 struct nfs_client *clp = ptr;
Trond Myklebust58d97142006-01-03 09:55:24 +0100282 struct nfs_delegation *delegation;
283 struct inode *inode;
Trond Myklebust58d97142006-01-03 09:55:24 +0100284
285 allow_signal(SIGKILL);
286restart:
Trond Myklebust58d97142006-01-03 09:55:24 +0100287 if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) != 0)
288 goto out;
289 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0)
290 goto out;
Trond Myklebust8383e462007-07-06 15:12:04 -0400291 rcu_read_lock();
292 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Trond Myklebust58d97142006-01-03 09:55:24 +0100293 inode = igrab(delegation->inode);
294 if (inode == NULL)
295 continue;
Trond Myklebust8383e462007-07-06 15:12:04 -0400296 spin_lock(&clp->cl_lock);
297 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
Trond Myklebust58d97142006-01-03 09:55:24 +0100298 spin_unlock(&clp->cl_lock);
Trond Myklebust8383e462007-07-06 15:12:04 -0400299 rcu_read_unlock();
300 if (delegation)
301 __nfs_inode_return_delegation(inode, delegation);
Trond Myklebust58d97142006-01-03 09:55:24 +0100302 iput(inode);
Trond Myklebust26c78e12006-01-03 09:55:58 +0100303 goto restart;
Trond Myklebust58d97142006-01-03 09:55:24 +0100304 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400305 rcu_read_unlock();
Trond Myklebust58d97142006-01-03 09:55:24 +0100306out:
David Howells24c8dbb2006-08-22 20:06:10 -0400307 nfs_put_client(clp);
Trond Myklebust58d97142006-01-03 09:55:24 +0100308 module_put_and_exit(0);
309}
310
David Howellsadfa6f92006-08-22 20:06:08 -0400311void nfs_expire_all_delegations(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +0100312{
313 struct task_struct *task;
314
315 __module_get(THIS_MODULE);
316 atomic_inc(&clp->cl_count);
317 task = kthread_run(nfs_do_expire_all_delegations, clp,
Chuck Lever5d8515c2007-12-10 14:57:16 -0500318 "%s-delegreturn",
319 rpc_peeraddr2str(clp->cl_rpcclient,
320 RPC_DISPLAY_ADDR));
Trond Myklebust58d97142006-01-03 09:55:24 +0100321 if (!IS_ERR(task))
322 return;
David Howells24c8dbb2006-08-22 20:06:10 -0400323 nfs_put_client(clp);
Trond Myklebust58d97142006-01-03 09:55:24 +0100324 module_put(THIS_MODULE);
325}
326
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327/*
328 * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
329 */
David Howellsadfa6f92006-08-22 20:06:08 -0400330void nfs_handle_cb_pathdown(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 struct nfs_delegation *delegation;
333 struct inode *inode;
334
335 if (clp == NULL)
336 return;
337restart:
Trond Myklebust8383e462007-07-06 15:12:04 -0400338 rcu_read_lock();
339 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 inode = igrab(delegation->inode);
341 if (inode == NULL)
342 continue;
Trond Myklebust8383e462007-07-06 15:12:04 -0400343 spin_lock(&clp->cl_lock);
344 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 spin_unlock(&clp->cl_lock);
Trond Myklebust8383e462007-07-06 15:12:04 -0400346 rcu_read_unlock();
347 if (delegation != NULL)
348 __nfs_inode_return_delegation(inode, delegation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 iput(inode);
350 goto restart;
351 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400352 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
355struct recall_threadargs {
356 struct inode *inode;
David Howellsadfa6f92006-08-22 20:06:08 -0400357 struct nfs_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 const nfs4_stateid *stateid;
359
360 struct completion started;
361 int result;
362};
363
364static int recall_thread(void *data)
365{
366 struct recall_threadargs *args = (struct recall_threadargs *)data;
367 struct inode *inode = igrab(args->inode);
David Howells7539bba2006-08-22 20:06:09 -0400368 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 struct nfs_inode *nfsi = NFS_I(inode);
370 struct nfs_delegation *delegation;
371
372 daemonize("nfsv4-delegreturn");
373
374 nfs_msync_inode(inode);
375 down_read(&clp->cl_sem);
376 down_write(&nfsi->rwsem);
377 spin_lock(&clp->cl_lock);
Trond Myklebust90163022007-07-05 14:55:18 -0400378 delegation = nfs_detach_delegation_locked(nfsi, args->stateid);
379 if (delegation != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 args->result = 0;
Trond Myklebust90163022007-07-05 14:55:18 -0400381 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 args->result = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 spin_unlock(&clp->cl_lock);
384 complete(&args->started);
Trond Myklebust90163022007-07-05 14:55:18 -0400385 nfs_delegation_claim_opens(inode, args->stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 up_write(&nfsi->rwsem);
387 up_read(&clp->cl_sem);
388 nfs_msync_inode(inode);
389
390 if (delegation != NULL)
391 nfs_do_return_delegation(inode, delegation);
392 iput(inode);
393 module_put_and_exit(0);
394}
395
396/*
397 * Asynchronous delegation recall!
398 */
399int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid)
400{
401 struct recall_threadargs data = {
402 .inode = inode,
403 .stateid = stateid,
404 };
405 int status;
406
407 init_completion(&data.started);
408 __module_get(THIS_MODULE);
409 status = kernel_thread(recall_thread, &data, CLONE_KERNEL);
410 if (status < 0)
411 goto out_module_put;
412 wait_for_completion(&data.started);
413 return data.result;
414out_module_put:
415 module_put(THIS_MODULE);
416 return status;
417}
418
419/*
420 * Retrieve the inode associated with a delegation
421 */
David Howellsadfa6f92006-08-22 20:06:08 -0400422struct inode *nfs_delegation_find_inode(struct nfs_client *clp, const struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
424 struct nfs_delegation *delegation;
425 struct inode *res = NULL;
Trond Myklebust8383e462007-07-06 15:12:04 -0400426 rcu_read_lock();
427 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
429 res = igrab(delegation->inode);
430 break;
431 }
432 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400433 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 return res;
435}
436
437/*
438 * Mark all delegations as needing to be reclaimed
439 */
David Howellsadfa6f92006-08-22 20:06:08 -0400440void nfs_delegation_mark_reclaim(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441{
442 struct nfs_delegation *delegation;
Trond Myklebust8383e462007-07-06 15:12:04 -0400443 rcu_read_lock();
444 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 delegation->flags |= NFS_DELEGATION_NEED_RECLAIM;
Trond Myklebust8383e462007-07-06 15:12:04 -0400446 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447}
448
449/*
450 * Reap all unclaimed delegations after reboot recovery is done
451 */
David Howellsadfa6f92006-08-22 20:06:08 -0400452void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453{
Trond Myklebust8383e462007-07-06 15:12:04 -0400454 struct nfs_delegation *delegation;
455restart:
456 rcu_read_lock();
457 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 if ((delegation->flags & NFS_DELEGATION_NEED_RECLAIM) == 0)
459 continue;
Trond Myklebust8383e462007-07-06 15:12:04 -0400460 spin_lock(&clp->cl_lock);
461 delegation = nfs_detach_delegation_locked(NFS_I(delegation->inode), NULL);
462 spin_unlock(&clp->cl_lock);
463 rcu_read_unlock();
464 if (delegation != NULL)
Trond Myklebust905f8d12007-08-06 12:18:34 -0400465 nfs_free_delegation(delegation);
Trond Myklebust8383e462007-07-06 15:12:04 -0400466 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400468 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500470
471int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
472{
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500473 struct nfs_inode *nfsi = NFS_I(inode);
474 struct nfs_delegation *delegation;
Trond Myklebust8383e462007-07-06 15:12:04 -0400475 int ret = 0;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500476
Trond Myklebust8383e462007-07-06 15:12:04 -0400477 rcu_read_lock();
478 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500479 if (delegation != NULL) {
480 memcpy(dst->data, delegation->stateid.data, sizeof(dst->data));
Trond Myklebust8383e462007-07-06 15:12:04 -0400481 ret = 1;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500482 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400483 rcu_read_unlock();
484 return ret;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500485}