blob: 11833f4caeaa9a2ea5549675636330a689c0e80b [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);
Trond Myklebust603c83d2007-10-18 19:59:20 -0400171 if (delegation != NULL)
172 nfs_free_delegation(delegation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 return status;
174}
175
176static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
177{
178 int res = 0;
179
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid);
Trond Myklebust905f8d12007-08-06 12:18:34 -0400181 nfs_free_delegation(delegation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 return res;
183}
184
185/* Sync all data to disk upon delegation return */
186static void nfs_msync_inode(struct inode *inode)
187{
188 filemap_fdatawrite(inode->i_mapping);
189 nfs_wb_all(inode);
190 filemap_fdatawait(inode->i_mapping);
191}
192
193/*
194 * Basic procedure for returning a delegation to the server
195 */
Trond Myklebust90163022007-07-05 14:55:18 -0400196static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
David Howells7539bba2006-08-22 20:06:09 -0400198 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 struct nfs_inode *nfsi = NFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200
201 nfs_msync_inode(inode);
202 down_read(&clp->cl_sem);
203 /* Guard against new delegated open calls */
204 down_write(&nfsi->rwsem);
Trond Myklebust90163022007-07-05 14:55:18 -0400205 nfs_delegation_claim_opens(inode, &delegation->stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 up_write(&nfsi->rwsem);
207 up_read(&clp->cl_sem);
208 nfs_msync_inode(inode);
209
Trond Myklebust90163022007-07-05 14:55:18 -0400210 return nfs_do_return_delegation(inode, delegation);
211}
212
213static struct nfs_delegation *nfs_detach_delegation_locked(struct nfs_inode *nfsi, const nfs4_stateid *stateid)
214{
Trond Myklebust8383e462007-07-06 15:12:04 -0400215 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust90163022007-07-05 14:55:18 -0400216
217 if (delegation == NULL)
218 goto nomatch;
219 if (stateid != NULL && memcmp(delegation->stateid.data, stateid->data,
220 sizeof(delegation->stateid.data)) != 0)
221 goto nomatch;
Trond Myklebust8383e462007-07-06 15:12:04 -0400222 list_del_rcu(&delegation->super_list);
Trond Myklebust90163022007-07-05 14:55:18 -0400223 nfsi->delegation_state = 0;
Trond Myklebust8383e462007-07-06 15:12:04 -0400224 rcu_assign_pointer(nfsi->delegation, NULL);
Trond Myklebust90163022007-07-05 14:55:18 -0400225 return delegation;
226nomatch:
227 return NULL;
228}
229
230int nfs_inode_return_delegation(struct inode *inode)
231{
232 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
233 struct nfs_inode *nfsi = NFS_I(inode);
234 struct nfs_delegation *delegation;
235 int err = 0;
236
Trond Myklebust8383e462007-07-06 15:12:04 -0400237 if (rcu_dereference(nfsi->delegation) != NULL) {
Trond Myklebust90163022007-07-05 14:55:18 -0400238 spin_lock(&clp->cl_lock);
239 delegation = nfs_detach_delegation_locked(nfsi, NULL);
240 spin_unlock(&clp->cl_lock);
241 if (delegation != NULL)
242 err = __nfs_inode_return_delegation(inode, delegation);
243 }
244 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
246
247/*
248 * Return all delegations associated to a super block
249 */
250void nfs_return_all_delegations(struct super_block *sb)
251{
David Howells7539bba2006-08-22 20:06:09 -0400252 struct nfs_client *clp = NFS_SB(sb)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 struct nfs_delegation *delegation;
254 struct inode *inode;
255
256 if (clp == NULL)
257 return;
258restart:
Trond Myklebust8383e462007-07-06 15:12:04 -0400259 rcu_read_lock();
260 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (delegation->inode->i_sb != sb)
262 continue;
263 inode = igrab(delegation->inode);
264 if (inode == NULL)
265 continue;
Trond Myklebust8383e462007-07-06 15:12:04 -0400266 spin_lock(&clp->cl_lock);
267 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 spin_unlock(&clp->cl_lock);
Trond Myklebust8383e462007-07-06 15:12:04 -0400269 rcu_read_unlock();
270 if (delegation != NULL)
271 __nfs_inode_return_delegation(inode, delegation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 iput(inode);
273 goto restart;
274 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400275 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
Trond Myklebust10afec92007-05-14 17:16:04 -0400278static int nfs_do_expire_all_delegations(void *ptr)
Trond Myklebust58d97142006-01-03 09:55:24 +0100279{
David Howellsadfa6f92006-08-22 20:06:08 -0400280 struct nfs_client *clp = ptr;
Trond Myklebust58d97142006-01-03 09:55:24 +0100281 struct nfs_delegation *delegation;
282 struct inode *inode;
Trond Myklebust58d97142006-01-03 09:55:24 +0100283
284 allow_signal(SIGKILL);
285restart:
Trond Myklebust58d97142006-01-03 09:55:24 +0100286 if (test_bit(NFS4CLNT_STATE_RECOVER, &clp->cl_state) != 0)
287 goto out;
288 if (test_bit(NFS4CLNT_LEASE_EXPIRED, &clp->cl_state) == 0)
289 goto out;
Trond Myklebust8383e462007-07-06 15:12:04 -0400290 rcu_read_lock();
291 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Trond Myklebust58d97142006-01-03 09:55:24 +0100292 inode = igrab(delegation->inode);
293 if (inode == NULL)
294 continue;
Trond Myklebust8383e462007-07-06 15:12:04 -0400295 spin_lock(&clp->cl_lock);
296 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
Trond Myklebust58d97142006-01-03 09:55:24 +0100297 spin_unlock(&clp->cl_lock);
Trond Myklebust8383e462007-07-06 15:12:04 -0400298 rcu_read_unlock();
299 if (delegation)
300 __nfs_inode_return_delegation(inode, delegation);
Trond Myklebust58d97142006-01-03 09:55:24 +0100301 iput(inode);
Trond Myklebust26c78e12006-01-03 09:55:58 +0100302 goto restart;
Trond Myklebust58d97142006-01-03 09:55:24 +0100303 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400304 rcu_read_unlock();
Trond Myklebust58d97142006-01-03 09:55:24 +0100305out:
David Howells24c8dbb2006-08-22 20:06:10 -0400306 nfs_put_client(clp);
Trond Myklebust58d97142006-01-03 09:55:24 +0100307 module_put_and_exit(0);
308}
309
David Howellsadfa6f92006-08-22 20:06:08 -0400310void nfs_expire_all_delegations(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +0100311{
312 struct task_struct *task;
313
314 __module_get(THIS_MODULE);
315 atomic_inc(&clp->cl_count);
316 task = kthread_run(nfs_do_expire_all_delegations, clp,
317 "%u.%u.%u.%u-delegreturn",
David Howells24c8dbb2006-08-22 20:06:10 -0400318 NIPQUAD(clp->cl_addr.sin_addr));
Trond Myklebust58d97142006-01-03 09:55:24 +0100319 if (!IS_ERR(task))
320 return;
David Howells24c8dbb2006-08-22 20:06:10 -0400321 nfs_put_client(clp);
Trond Myklebust58d97142006-01-03 09:55:24 +0100322 module_put(THIS_MODULE);
323}
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325/*
326 * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
327 */
David Howellsadfa6f92006-08-22 20:06:08 -0400328void nfs_handle_cb_pathdown(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329{
330 struct nfs_delegation *delegation;
331 struct inode *inode;
332
333 if (clp == NULL)
334 return;
335restart:
Trond Myklebust8383e462007-07-06 15:12:04 -0400336 rcu_read_lock();
337 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 inode = igrab(delegation->inode);
339 if (inode == NULL)
340 continue;
Trond Myklebust8383e462007-07-06 15:12:04 -0400341 spin_lock(&clp->cl_lock);
342 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 spin_unlock(&clp->cl_lock);
Trond Myklebust8383e462007-07-06 15:12:04 -0400344 rcu_read_unlock();
345 if (delegation != NULL)
346 __nfs_inode_return_delegation(inode, delegation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 iput(inode);
348 goto restart;
349 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400350 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351}
352
353struct recall_threadargs {
354 struct inode *inode;
David Howellsadfa6f92006-08-22 20:06:08 -0400355 struct nfs_client *clp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 const nfs4_stateid *stateid;
357
358 struct completion started;
359 int result;
360};
361
362static int recall_thread(void *data)
363{
364 struct recall_threadargs *args = (struct recall_threadargs *)data;
365 struct inode *inode = igrab(args->inode);
David Howells7539bba2006-08-22 20:06:09 -0400366 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 struct nfs_inode *nfsi = NFS_I(inode);
368 struct nfs_delegation *delegation;
369
370 daemonize("nfsv4-delegreturn");
371
372 nfs_msync_inode(inode);
373 down_read(&clp->cl_sem);
374 down_write(&nfsi->rwsem);
375 spin_lock(&clp->cl_lock);
Trond Myklebust90163022007-07-05 14:55:18 -0400376 delegation = nfs_detach_delegation_locked(nfsi, args->stateid);
377 if (delegation != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 args->result = 0;
Trond Myklebust90163022007-07-05 14:55:18 -0400379 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 args->result = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 spin_unlock(&clp->cl_lock);
382 complete(&args->started);
Trond Myklebust90163022007-07-05 14:55:18 -0400383 nfs_delegation_claim_opens(inode, args->stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 up_write(&nfsi->rwsem);
385 up_read(&clp->cl_sem);
386 nfs_msync_inode(inode);
387
388 if (delegation != NULL)
389 nfs_do_return_delegation(inode, delegation);
390 iput(inode);
391 module_put_and_exit(0);
392}
393
394/*
395 * Asynchronous delegation recall!
396 */
397int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid)
398{
399 struct recall_threadargs data = {
400 .inode = inode,
401 .stateid = stateid,
402 };
403 int status;
404
405 init_completion(&data.started);
406 __module_get(THIS_MODULE);
407 status = kernel_thread(recall_thread, &data, CLONE_KERNEL);
408 if (status < 0)
409 goto out_module_put;
410 wait_for_completion(&data.started);
411 return data.result;
412out_module_put:
413 module_put(THIS_MODULE);
414 return status;
415}
416
417/*
418 * Retrieve the inode associated with a delegation
419 */
David Howellsadfa6f92006-08-22 20:06:08 -0400420struct inode *nfs_delegation_find_inode(struct nfs_client *clp, const struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 struct nfs_delegation *delegation;
423 struct inode *res = NULL;
Trond Myklebust8383e462007-07-06 15:12:04 -0400424 rcu_read_lock();
425 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
427 res = igrab(delegation->inode);
428 break;
429 }
430 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400431 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return res;
433}
434
435/*
436 * Mark all delegations as needing to be reclaimed
437 */
David Howellsadfa6f92006-08-22 20:06:08 -0400438void nfs_delegation_mark_reclaim(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
440 struct nfs_delegation *delegation;
Trond Myklebust8383e462007-07-06 15:12:04 -0400441 rcu_read_lock();
442 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 delegation->flags |= NFS_DELEGATION_NEED_RECLAIM;
Trond Myklebust8383e462007-07-06 15:12:04 -0400444 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445}
446
447/*
448 * Reap all unclaimed delegations after reboot recovery is done
449 */
David Howellsadfa6f92006-08-22 20:06:08 -0400450void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Trond Myklebust8383e462007-07-06 15:12:04 -0400452 struct nfs_delegation *delegation;
453restart:
454 rcu_read_lock();
455 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if ((delegation->flags & NFS_DELEGATION_NEED_RECLAIM) == 0)
457 continue;
Trond Myklebust8383e462007-07-06 15:12:04 -0400458 spin_lock(&clp->cl_lock);
459 delegation = nfs_detach_delegation_locked(NFS_I(delegation->inode), NULL);
460 spin_unlock(&clp->cl_lock);
461 rcu_read_unlock();
462 if (delegation != NULL)
Trond Myklebust905f8d12007-08-06 12:18:34 -0400463 nfs_free_delegation(delegation);
Trond Myklebust8383e462007-07-06 15:12:04 -0400464 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400466 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500468
469int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
470{
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500471 struct nfs_inode *nfsi = NFS_I(inode);
472 struct nfs_delegation *delegation;
Trond Myklebust8383e462007-07-06 15:12:04 -0400473 int ret = 0;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500474
Trond Myklebust8383e462007-07-06 15:12:04 -0400475 rcu_read_lock();
476 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500477 if (delegation != NULL) {
478 memcpy(dst->data, delegation->stateid.data, sizeof(dst->data));
Trond Myklebust8383e462007-07-06 15:12:04 -0400479 ret = 1;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500480 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400481 rcu_read_unlock();
482 return ret;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500483}