blob: 968225a88015b5c544394dc321831e642ee210ff [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 Myklebustb7391f42008-12-23 15:21:52 -050046void nfs_mark_delegation_referenced(struct nfs_delegation *delegation)
47{
48 set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags);
49}
50
Trond Myklebustbd7bf9d2008-12-23 15:21:53 -050051int nfs_have_delegation(struct inode *inode, fmode_t flags)
Trond Myklebustb7391f42008-12-23 15:21:52 -050052{
53 struct nfs_delegation *delegation;
54 int ret = 0;
55
56 flags &= FMODE_READ|FMODE_WRITE;
57 rcu_read_lock();
58 delegation = rcu_dereference(NFS_I(inode)->delegation);
59 if (delegation != NULL && (delegation->type & flags) == flags) {
60 nfs_mark_delegation_referenced(delegation);
61 ret = 1;
62 }
63 rcu_read_unlock();
64 return ret;
65}
66
Trond Myklebust888e6942005-11-04 15:38:11 -050067static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state)
68{
69 struct inode *inode = state->inode;
70 struct file_lock *fl;
71 int status;
72
Harvey Harrison90dc7d22008-02-20 13:03:05 -080073 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
Trond Myklebust888e6942005-11-04 15:38:11 -050074 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
75 continue;
Trond Myklebustcd3758e2007-08-10 17:44:32 -040076 if (nfs_file_open_context(fl->fl_file) != ctx)
Trond Myklebust888e6942005-11-04 15:38:11 -050077 continue;
78 status = nfs4_lock_delegation_recall(state, fl);
79 if (status >= 0)
80 continue;
81 switch (status) {
82 default:
83 printk(KERN_ERR "%s: unhandled error %d.\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -070084 __func__, status);
Trond Myklebust888e6942005-11-04 15:38:11 -050085 case -NFS4ERR_EXPIRED:
86 /* kill_proc(fl->fl_pid, SIGLOST, 1); */
87 case -NFS4ERR_STALE_CLIENTID:
David Howells7539bba2006-08-22 20:06:09 -040088 nfs4_schedule_state_recovery(NFS_SERVER(inode)->nfs_client);
Trond Myklebust888e6942005-11-04 15:38:11 -050089 goto out_err;
90 }
91 }
92 return 0;
93out_err:
94 return status;
95}
96
Trond Myklebust90163022007-07-05 14:55:18 -040097static void nfs_delegation_claim_opens(struct inode *inode, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
99 struct nfs_inode *nfsi = NFS_I(inode);
100 struct nfs_open_context *ctx;
101 struct nfs4_state *state;
Trond Myklebust888e6942005-11-04 15:38:11 -0500102 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104again:
105 spin_lock(&inode->i_lock);
106 list_for_each_entry(ctx, &nfsi->open_files, list) {
107 state = ctx->state;
108 if (state == NULL)
109 continue;
110 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
111 continue;
Trond Myklebust90163022007-07-05 14:55:18 -0400112 if (memcmp(state->stateid.data, stateid->data, sizeof(state->stateid.data)) != 0)
113 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 get_nfs_open_context(ctx);
115 spin_unlock(&inode->i_lock);
Trond Myklebust13437e12007-07-06 15:10:43 -0400116 err = nfs4_open_delegation_recall(ctx, state, stateid);
Trond Myklebust888e6942005-11-04 15:38:11 -0500117 if (err >= 0)
118 err = nfs_delegation_claim_locks(ctx, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 put_nfs_open_context(ctx);
Trond Myklebust888e6942005-11-04 15:38:11 -0500120 if (err != 0)
121 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 goto again;
123 }
124 spin_unlock(&inode->i_lock);
125}
126
127/*
128 * Set up a delegation on an inode
129 */
130void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
131{
132 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
Trond Myklebust05c88ba2007-10-11 15:11:51 -0400133 struct rpc_cred *oldcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
135 if (delegation == NULL)
136 return;
137 memcpy(delegation->stateid.data, res->delegation.data,
138 sizeof(delegation->stateid.data));
139 delegation->type = res->delegation_type;
140 delegation->maxsize = res->maxsize;
Trond Myklebust05c88ba2007-10-11 15:11:51 -0400141 oldcred = delegation->cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 delegation->cred = get_rpccred(cred);
Trond Myklebust15c831b2008-12-23 15:21:39 -0500143 clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 NFS_I(inode)->delegation_state = delegation->type;
145 smp_wmb();
Trond Myklebust05c88ba2007-10-11 15:11:51 -0400146 put_rpccred(oldcred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
Trond Myklebust57bfa892008-01-25 16:38:18 -0500149static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
150{
151 int res = 0;
152
153 res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid, issync);
154 nfs_free_delegation(delegation);
155 return res;
156}
157
Trond Myklebust86e89482008-12-23 15:21:39 -0500158static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
159{
160 struct inode *inode = NULL;
161
162 spin_lock(&delegation->lock);
163 if (delegation->inode != NULL)
164 inode = igrab(delegation->inode);
165 spin_unlock(&delegation->lock);
166 return inode;
167}
168
Trond Myklebust57bfa892008-01-25 16:38:18 -0500169static struct nfs_delegation *nfs_detach_delegation_locked(struct nfs_inode *nfsi, const nfs4_stateid *stateid)
170{
171 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
172
173 if (delegation == NULL)
174 goto nomatch;
Trond Myklebust34310432008-12-23 15:21:38 -0500175 spin_lock(&delegation->lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500176 if (stateid != NULL && memcmp(delegation->stateid.data, stateid->data,
177 sizeof(delegation->stateid.data)) != 0)
Trond Myklebust34310432008-12-23 15:21:38 -0500178 goto nomatch_unlock;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500179 list_del_rcu(&delegation->super_list);
Trond Myklebust86e89482008-12-23 15:21:39 -0500180 delegation->inode = NULL;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500181 nfsi->delegation_state = 0;
182 rcu_assign_pointer(nfsi->delegation, NULL);
Trond Myklebust34310432008-12-23 15:21:38 -0500183 spin_unlock(&delegation->lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500184 return delegation;
Trond Myklebust34310432008-12-23 15:21:38 -0500185nomatch_unlock:
186 spin_unlock(&delegation->lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500187nomatch:
188 return NULL;
189}
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191/*
192 * Set up a delegation on an inode
193 */
194int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
195{
David Howells7539bba2006-08-22 20:06:09 -0400196 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 struct nfs_inode *nfsi = NFS_I(inode);
198 struct nfs_delegation *delegation;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500199 struct nfs_delegation *freeme = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 int status = 0;
201
Panagiotis Issarisf52720c2006-09-27 01:49:39 -0700202 delegation = kmalloc(sizeof(*delegation), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 if (delegation == NULL)
204 return -ENOMEM;
205 memcpy(delegation->stateid.data, res->delegation.data,
206 sizeof(delegation->stateid.data));
207 delegation->type = res->delegation_type;
208 delegation->maxsize = res->maxsize;
Trond Myklebustbeb2a5e2006-01-03 09:55:37 +0100209 delegation->change_attr = nfsi->change_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 delegation->cred = get_rpccred(cred);
211 delegation->inode = inode;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500212 delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
Trond Myklebust34310432008-12-23 15:21:38 -0500213 spin_lock_init(&delegation->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 spin_lock(&clp->cl_lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500216 if (rcu_dereference(nfsi->delegation) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (memcmp(&delegation->stateid, &nfsi->delegation->stateid,
Trond Myklebust57bfa892008-01-25 16:38:18 -0500218 sizeof(delegation->stateid)) == 0 &&
219 delegation->type == nfsi->delegation->type) {
220 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
Trond Myklebust57bfa892008-01-25 16:38:18 -0500222 /*
223 * Deal with broken servers that hand out two
224 * delegations for the same file.
225 */
226 dfprintk(FILE, "%s: server %s handed out "
227 "a duplicate delegation!\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700228 __func__, clp->cl_hostname);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500229 if (delegation->type <= nfsi->delegation->type) {
230 freeme = delegation;
231 delegation = NULL;
232 goto out;
233 }
234 freeme = nfs_detach_delegation_locked(nfsi, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
Trond Myklebust57bfa892008-01-25 16:38:18 -0500236 list_add_rcu(&delegation->super_list, &clp->cl_delegations);
237 nfsi->delegation_state = delegation->type;
238 rcu_assign_pointer(nfsi->delegation, delegation);
239 delegation = NULL;
Trond Myklebust412c77c2007-07-03 16:10:55 -0400240
241 /* Ensure we revalidate the attributes and page cache! */
242 spin_lock(&inode->i_lock);
243 nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
244 spin_unlock(&inode->i_lock);
245
Trond Myklebust57bfa892008-01-25 16:38:18 -0500246out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 spin_unlock(&clp->cl_lock);
Trond Myklebust603c83d2007-10-18 19:59:20 -0400248 if (delegation != NULL)
249 nfs_free_delegation(delegation);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500250 if (freeme != NULL)
251 nfs_do_return_delegation(inode, freeme, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 return status;
253}
254
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255/* Sync all data to disk upon delegation return */
256static void nfs_msync_inode(struct inode *inode)
257{
258 filemap_fdatawrite(inode->i_mapping);
259 nfs_wb_all(inode);
260 filemap_fdatawait(inode->i_mapping);
261}
262
263/*
264 * Basic procedure for returning a delegation to the server
265 */
Trond Myklebust90163022007-07-05 14:55:18 -0400266static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 struct nfs_inode *nfsi = NFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 nfs_msync_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 /* Guard against new delegated open calls */
272 down_write(&nfsi->rwsem);
Trond Myklebust90163022007-07-05 14:55:18 -0400273 nfs_delegation_claim_opens(inode, &delegation->stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 up_write(&nfsi->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 nfs_msync_inode(inode);
276
Trond Myklebuste6f81072008-01-24 18:14:34 -0500277 return nfs_do_return_delegation(inode, delegation, 1);
Trond Myklebust90163022007-07-05 14:55:18 -0400278}
279
Trond Myklebuste6f81072008-01-24 18:14:34 -0500280/*
Trond Myklebust515d8612008-12-23 15:21:46 -0500281 * Return all delegations that have been marked for return
282 */
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500283void nfs_client_return_marked_delegations(struct nfs_client *clp)
Trond Myklebust515d8612008-12-23 15:21:46 -0500284{
285 struct nfs_delegation *delegation;
286 struct inode *inode;
287
288restart:
289 rcu_read_lock();
290 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
291 if (!test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
292 continue;
293 inode = nfs_delegation_grab_inode(delegation);
294 if (inode == NULL)
295 continue;
296 spin_lock(&clp->cl_lock);
297 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
298 spin_unlock(&clp->cl_lock);
299 rcu_read_unlock();
300 if (delegation != NULL)
301 __nfs_inode_return_delegation(inode, delegation);
302 iput(inode);
303 goto restart;
304 }
305 rcu_read_unlock();
306}
307
308/*
Trond Myklebuste6f81072008-01-24 18:14:34 -0500309 * This function returns the delegation without reclaiming opens
310 * or protecting against delegation reclaims.
311 * It is therefore really only safe to be called from
312 * nfs4_clear_inode()
313 */
314void nfs_inode_return_delegation_noreclaim(struct inode *inode)
315{
316 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
317 struct nfs_inode *nfsi = NFS_I(inode);
318 struct nfs_delegation *delegation;
319
320 if (rcu_dereference(nfsi->delegation) != NULL) {
321 spin_lock(&clp->cl_lock);
322 delegation = nfs_detach_delegation_locked(nfsi, NULL);
323 spin_unlock(&clp->cl_lock);
324 if (delegation != NULL)
325 nfs_do_return_delegation(inode, delegation, 0);
326 }
327}
328
Trond Myklebust90163022007-07-05 14:55:18 -0400329int nfs_inode_return_delegation(struct inode *inode)
330{
331 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
332 struct nfs_inode *nfsi = NFS_I(inode);
333 struct nfs_delegation *delegation;
334 int err = 0;
335
Trond Myklebust8383e462007-07-06 15:12:04 -0400336 if (rcu_dereference(nfsi->delegation) != NULL) {
Trond Myklebust90163022007-07-05 14:55:18 -0400337 spin_lock(&clp->cl_lock);
338 delegation = nfs_detach_delegation_locked(nfsi, NULL);
339 spin_unlock(&clp->cl_lock);
340 if (delegation != NULL)
341 err = __nfs_inode_return_delegation(inode, delegation);
342 }
343 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
Trond Myklebust6411bd42008-12-23 15:21:51 -0500346static void nfs_mark_return_delegation(struct nfs_client *clp, struct nfs_delegation *delegation)
347{
348 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
349 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
350}
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352/*
353 * Return all delegations associated to a super block
354 */
Trond Myklebust515d8612008-12-23 15:21:46 -0500355void nfs_super_return_all_delegations(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
David Howells7539bba2006-08-22 20:06:09 -0400357 struct nfs_client *clp = NFS_SB(sb)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 struct nfs_delegation *delegation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 if (clp == NULL)
361 return;
Trond Myklebust8383e462007-07-06 15:12:04 -0400362 rcu_read_lock();
363 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Trond Myklebust86e89482008-12-23 15:21:39 -0500364 spin_lock(&delegation->lock);
365 if (delegation->inode != NULL && delegation->inode->i_sb == sb)
Trond Myklebust515d8612008-12-23 15:21:46 -0500366 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
Trond Myklebust86e89482008-12-23 15:21:39 -0500367 spin_unlock(&delegation->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400369 rcu_read_unlock();
Trond Myklebust515d8612008-12-23 15:21:46 -0500370 nfs_client_return_marked_delegations(clp);
371}
372
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500373static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
Trond Myklebust515d8612008-12-23 15:21:46 -0500374{
375 struct nfs_delegation *delegation;
376
377 rcu_read_lock();
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500378 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Trond Myklebust515d8612008-12-23 15:21:46 -0500379 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500380 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
381 }
Trond Myklebust515d8612008-12-23 15:21:46 -0500382 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
Trond Myklebustb0d3ded2008-12-23 15:21:50 -0500385static void nfs_delegation_run_state_manager(struct nfs_client *clp)
Trond Myklebust58d9714a2006-01-03 09:55:24 +0100386{
Trond Myklebustb0d3ded2008-12-23 15:21:50 -0500387 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
388 nfs4_schedule_state_manager(clp);
Trond Myklebust58d9714a2006-01-03 09:55:24 +0100389}
390
David Howellsadfa6f92006-08-22 20:06:08 -0400391void nfs_expire_all_delegations(struct nfs_client *clp)
Trond Myklebust58d9714a2006-01-03 09:55:24 +0100392{
Trond Myklebustb0d3ded2008-12-23 15:21:50 -0500393 nfs_client_mark_return_all_delegations(clp);
394 nfs_delegation_run_state_manager(clp);
Trond Myklebust58d9714a2006-01-03 09:55:24 +0100395}
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397/*
398 * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
399 */
David Howellsadfa6f92006-08-22 20:06:08 -0400400void nfs_handle_cb_pathdown(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if (clp == NULL)
403 return;
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500404 nfs_client_mark_return_all_delegations(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
Trond Myklebustb7391f42008-12-23 15:21:52 -0500407static void nfs_client_mark_return_unreferenced_delegations(struct nfs_client *clp)
408{
409 struct nfs_delegation *delegation;
410
411 rcu_read_lock();
412 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
413 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
414 continue;
415 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
416 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
417 }
418 rcu_read_unlock();
419}
420
421void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
422{
423 nfs_client_mark_return_unreferenced_delegations(clp);
424 nfs_delegation_run_state_manager(clp);
425}
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427/*
428 * Asynchronous delegation recall!
429 */
430int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid)
431{
Trond Myklebust6411bd42008-12-23 15:21:51 -0500432 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
433 struct nfs_delegation *delegation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Trond Myklebust6411bd42008-12-23 15:21:51 -0500435 rcu_read_lock();
436 delegation = rcu_dereference(NFS_I(inode)->delegation);
437 if (delegation == NULL || memcmp(delegation->stateid.data, stateid->data,
438 sizeof(delegation->stateid.data)) != 0) {
439 rcu_read_unlock();
440 return -ENOENT;
441 }
442 nfs_mark_return_delegation(clp, delegation);
443 rcu_read_unlock();
444 nfs_delegation_run_state_manager(clp);
445 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
448/*
449 * Retrieve the inode associated with a delegation
450 */
David Howellsadfa6f92006-08-22 20:06:08 -0400451struct inode *nfs_delegation_find_inode(struct nfs_client *clp, const struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 struct nfs_delegation *delegation;
454 struct inode *res = NULL;
Trond Myklebust8383e462007-07-06 15:12:04 -0400455 rcu_read_lock();
456 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Trond Myklebust86e89482008-12-23 15:21:39 -0500457 spin_lock(&delegation->lock);
458 if (delegation->inode != NULL &&
459 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 res = igrab(delegation->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
Trond Myklebust86e89482008-12-23 15:21:39 -0500462 spin_unlock(&delegation->lock);
463 if (res != NULL)
464 break;
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 return res;
468}
469
470/*
471 * Mark all delegations as needing to be reclaimed
472 */
David Howellsadfa6f92006-08-22 20:06:08 -0400473void nfs_delegation_mark_reclaim(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
475 struct nfs_delegation *delegation;
Trond Myklebust8383e462007-07-06 15:12:04 -0400476 rcu_read_lock();
477 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list)
Trond Myklebust15c831b2008-12-23 15:21:39 -0500478 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
Trond Myklebust8383e462007-07-06 15:12:04 -0400479 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481
482/*
483 * Reap all unclaimed delegations after reboot recovery is done
484 */
David Howellsadfa6f92006-08-22 20:06:08 -0400485void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Trond Myklebust8383e462007-07-06 15:12:04 -0400487 struct nfs_delegation *delegation;
Trond Myklebust86e89482008-12-23 15:21:39 -0500488 struct inode *inode;
Trond Myklebust8383e462007-07-06 15:12:04 -0400489restart:
490 rcu_read_lock();
491 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Trond Myklebust15c831b2008-12-23 15:21:39 -0500492 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 continue;
Trond Myklebust86e89482008-12-23 15:21:39 -0500494 inode = nfs_delegation_grab_inode(delegation);
495 if (inode == NULL)
496 continue;
Trond Myklebust8383e462007-07-06 15:12:04 -0400497 spin_lock(&clp->cl_lock);
Trond Myklebust86e89482008-12-23 15:21:39 -0500498 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
Trond Myklebust8383e462007-07-06 15:12:04 -0400499 spin_unlock(&clp->cl_lock);
500 rcu_read_unlock();
501 if (delegation != NULL)
Trond Myklebust905f8d12007-08-06 12:18:34 -0400502 nfs_free_delegation(delegation);
Trond Myklebust86e89482008-12-23 15:21:39 -0500503 iput(inode);
Trond Myklebust8383e462007-07-06 15:12:04 -0400504 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400506 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500508
509int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
510{
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500511 struct nfs_inode *nfsi = NFS_I(inode);
512 struct nfs_delegation *delegation;
Trond Myklebust8383e462007-07-06 15:12:04 -0400513 int ret = 0;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500514
Trond Myklebust8383e462007-07-06 15:12:04 -0400515 rcu_read_lock();
516 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500517 if (delegation != NULL) {
518 memcpy(dst->data, delegation->stateid.data, sizeof(dst->data));
Trond Myklebust8383e462007-07-06 15:12:04 -0400519 ret = 1;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500520 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400521 rcu_read_unlock();
522 return ret;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500523}