blob: d4f669f0683efdde7d0bf626919bceda01354bfc [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 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;
Trond Myklebustd5122202009-06-17 13:22:58 -070071 int status = 0;
Trond Myklebust888e6942005-11-04 15:38:11 -050072
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);
Trond Myklebustd5122202009-06-17 13:22:58 -070079 if (status < 0)
80 break;
Trond Myklebust888e6942005-11-04 15:38:11 -050081 }
Trond Myklebust888e6942005-11-04 15:38:11 -050082 return status;
83}
84
Trond Myklebust90163022007-07-05 14:55:18 -040085static void nfs_delegation_claim_opens(struct inode *inode, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
87 struct nfs_inode *nfsi = NFS_I(inode);
88 struct nfs_open_context *ctx;
89 struct nfs4_state *state;
Trond Myklebust888e6942005-11-04 15:38:11 -050090 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92again:
93 spin_lock(&inode->i_lock);
94 list_for_each_entry(ctx, &nfsi->open_files, list) {
95 state = ctx->state;
96 if (state == NULL)
97 continue;
98 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
99 continue;
Trond Myklebust90163022007-07-05 14:55:18 -0400100 if (memcmp(state->stateid.data, stateid->data, sizeof(state->stateid.data)) != 0)
101 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 get_nfs_open_context(ctx);
103 spin_unlock(&inode->i_lock);
Trond Myklebust13437e12007-07-06 15:10:43 -0400104 err = nfs4_open_delegation_recall(ctx, state, stateid);
Trond Myklebust888e6942005-11-04 15:38:11 -0500105 if (err >= 0)
106 err = nfs_delegation_claim_locks(ctx, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 put_nfs_open_context(ctx);
Trond Myklebust888e6942005-11-04 15:38:11 -0500108 if (err != 0)
109 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 goto again;
111 }
112 spin_unlock(&inode->i_lock);
113}
114
115/*
116 * Set up a delegation on an inode
117 */
118void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
119{
120 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
Trond Myklebust05c88ba2007-10-11 15:11:51 -0400121 struct rpc_cred *oldcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 if (delegation == NULL)
124 return;
125 memcpy(delegation->stateid.data, res->delegation.data,
126 sizeof(delegation->stateid.data));
127 delegation->type = res->delegation_type;
128 delegation->maxsize = res->maxsize;
Trond Myklebust05c88ba2007-10-11 15:11:51 -0400129 oldcred = delegation->cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 delegation->cred = get_rpccred(cred);
Trond Myklebust15c831b2008-12-23 15:21:39 -0500131 clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 NFS_I(inode)->delegation_state = delegation->type;
133 smp_wmb();
Trond Myklebust05c88ba2007-10-11 15:11:51 -0400134 put_rpccred(oldcred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
Trond Myklebust57bfa892008-01-25 16:38:18 -0500137static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
138{
139 int res = 0;
140
141 res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid, issync);
142 nfs_free_delegation(delegation);
143 return res;
144}
145
Trond Myklebust86e89482008-12-23 15:21:39 -0500146static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
147{
148 struct inode *inode = NULL;
149
150 spin_lock(&delegation->lock);
151 if (delegation->inode != NULL)
152 inode = igrab(delegation->inode);
153 spin_unlock(&delegation->lock);
154 return inode;
155}
156
Trond Myklebust57bfa892008-01-25 16:38:18 -0500157static struct nfs_delegation *nfs_detach_delegation_locked(struct nfs_inode *nfsi, const nfs4_stateid *stateid)
158{
159 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
160
161 if (delegation == NULL)
162 goto nomatch;
Trond Myklebust34310432008-12-23 15:21:38 -0500163 spin_lock(&delegation->lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500164 if (stateid != NULL && memcmp(delegation->stateid.data, stateid->data,
165 sizeof(delegation->stateid.data)) != 0)
Trond Myklebust34310432008-12-23 15:21:38 -0500166 goto nomatch_unlock;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500167 list_del_rcu(&delegation->super_list);
Trond Myklebust86e89482008-12-23 15:21:39 -0500168 delegation->inode = NULL;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500169 nfsi->delegation_state = 0;
170 rcu_assign_pointer(nfsi->delegation, NULL);
Trond Myklebust34310432008-12-23 15:21:38 -0500171 spin_unlock(&delegation->lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500172 return delegation;
Trond Myklebust34310432008-12-23 15:21:38 -0500173nomatch_unlock:
174 spin_unlock(&delegation->lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500175nomatch:
176 return NULL;
177}
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179/*
180 * Set up a delegation on an inode
181 */
182int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
183{
David Howells7539bba2006-08-22 20:06:09 -0400184 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 struct nfs_inode *nfsi = NFS_I(inode);
186 struct nfs_delegation *delegation;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500187 struct nfs_delegation *freeme = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 int status = 0;
189
Panagiotis Issarisf52720c2006-09-27 01:49:39 -0700190 delegation = kmalloc(sizeof(*delegation), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 if (delegation == NULL)
192 return -ENOMEM;
193 memcpy(delegation->stateid.data, res->delegation.data,
194 sizeof(delegation->stateid.data));
195 delegation->type = res->delegation_type;
196 delegation->maxsize = res->maxsize;
Trond Myklebustbeb2a5e2006-01-03 09:55:37 +0100197 delegation->change_attr = nfsi->change_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 delegation->cred = get_rpccred(cred);
199 delegation->inode = inode;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500200 delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
Trond Myklebust34310432008-12-23 15:21:38 -0500201 spin_lock_init(&delegation->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 spin_lock(&clp->cl_lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500204 if (rcu_dereference(nfsi->delegation) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 if (memcmp(&delegation->stateid, &nfsi->delegation->stateid,
Trond Myklebust57bfa892008-01-25 16:38:18 -0500206 sizeof(delegation->stateid)) == 0 &&
207 delegation->type == nfsi->delegation->type) {
208 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
Trond Myklebust57bfa892008-01-25 16:38:18 -0500210 /*
211 * Deal with broken servers that hand out two
212 * delegations for the same file.
213 */
214 dfprintk(FILE, "%s: server %s handed out "
215 "a duplicate delegation!\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700216 __func__, clp->cl_hostname);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500217 if (delegation->type <= nfsi->delegation->type) {
218 freeme = delegation;
219 delegation = NULL;
220 goto out;
221 }
222 freeme = nfs_detach_delegation_locked(nfsi, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 }
Trond Myklebust57bfa892008-01-25 16:38:18 -0500224 list_add_rcu(&delegation->super_list, &clp->cl_delegations);
225 nfsi->delegation_state = delegation->type;
226 rcu_assign_pointer(nfsi->delegation, delegation);
227 delegation = NULL;
Trond Myklebust412c77c2007-07-03 16:10:55 -0400228
229 /* Ensure we revalidate the attributes and page cache! */
230 spin_lock(&inode->i_lock);
231 nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
232 spin_unlock(&inode->i_lock);
233
Trond Myklebust57bfa892008-01-25 16:38:18 -0500234out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 spin_unlock(&clp->cl_lock);
Trond Myklebust603c83d2007-10-18 19:59:20 -0400236 if (delegation != NULL)
237 nfs_free_delegation(delegation);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500238 if (freeme != NULL)
239 nfs_do_return_delegation(inode, freeme, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return status;
241}
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243/* Sync all data to disk upon delegation return */
244static void nfs_msync_inode(struct inode *inode)
245{
246 filemap_fdatawrite(inode->i_mapping);
247 nfs_wb_all(inode);
248 filemap_fdatawait(inode->i_mapping);
249}
250
251/*
252 * Basic procedure for returning a delegation to the server
253 */
Trond Myklebust90163022007-07-05 14:55:18 -0400254static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 struct nfs_inode *nfsi = NFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 nfs_msync_inode(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 /* Guard against new delegated open calls */
260 down_write(&nfsi->rwsem);
Trond Myklebust90163022007-07-05 14:55:18 -0400261 nfs_delegation_claim_opens(inode, &delegation->stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 up_write(&nfsi->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 nfs_msync_inode(inode);
264
Trond Myklebuste6f81072008-01-24 18:14:34 -0500265 return nfs_do_return_delegation(inode, delegation, 1);
Trond Myklebust90163022007-07-05 14:55:18 -0400266}
267
Trond Myklebuste6f81072008-01-24 18:14:34 -0500268/*
Trond Myklebust515d8612008-12-23 15:21:46 -0500269 * Return all delegations that have been marked for return
270 */
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500271void nfs_client_return_marked_delegations(struct nfs_client *clp)
Trond Myklebust515d8612008-12-23 15:21:46 -0500272{
273 struct nfs_delegation *delegation;
274 struct inode *inode;
275
276restart:
277 rcu_read_lock();
278 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
279 if (!test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
280 continue;
281 inode = nfs_delegation_grab_inode(delegation);
282 if (inode == NULL)
283 continue;
284 spin_lock(&clp->cl_lock);
285 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
286 spin_unlock(&clp->cl_lock);
287 rcu_read_unlock();
288 if (delegation != NULL)
289 __nfs_inode_return_delegation(inode, delegation);
290 iput(inode);
291 goto restart;
292 }
293 rcu_read_unlock();
294}
295
296/*
Trond Myklebuste6f81072008-01-24 18:14:34 -0500297 * This function returns the delegation without reclaiming opens
298 * or protecting against delegation reclaims.
299 * It is therefore really only safe to be called from
300 * nfs4_clear_inode()
301 */
302void nfs_inode_return_delegation_noreclaim(struct inode *inode)
303{
304 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
305 struct nfs_inode *nfsi = NFS_I(inode);
306 struct nfs_delegation *delegation;
307
308 if (rcu_dereference(nfsi->delegation) != NULL) {
309 spin_lock(&clp->cl_lock);
310 delegation = nfs_detach_delegation_locked(nfsi, NULL);
311 spin_unlock(&clp->cl_lock);
312 if (delegation != NULL)
313 nfs_do_return_delegation(inode, delegation, 0);
314 }
315}
316
Trond Myklebust90163022007-07-05 14:55:18 -0400317int nfs_inode_return_delegation(struct inode *inode)
318{
319 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
320 struct nfs_inode *nfsi = NFS_I(inode);
321 struct nfs_delegation *delegation;
322 int err = 0;
323
Trond Myklebust8383e462007-07-06 15:12:04 -0400324 if (rcu_dereference(nfsi->delegation) != NULL) {
Trond Myklebust90163022007-07-05 14:55:18 -0400325 spin_lock(&clp->cl_lock);
326 delegation = nfs_detach_delegation_locked(nfsi, NULL);
327 spin_unlock(&clp->cl_lock);
328 if (delegation != NULL)
329 err = __nfs_inode_return_delegation(inode, delegation);
330 }
331 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
Trond Myklebust6411bd42008-12-23 15:21:51 -0500334static void nfs_mark_return_delegation(struct nfs_client *clp, struct nfs_delegation *delegation)
335{
336 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
337 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
338}
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340/*
341 * Return all delegations associated to a super block
342 */
Trond Myklebust515d8612008-12-23 15:21:46 -0500343void nfs_super_return_all_delegations(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
David Howells7539bba2006-08-22 20:06:09 -0400345 struct nfs_client *clp = NFS_SB(sb)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 struct nfs_delegation *delegation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
348 if (clp == NULL)
349 return;
Trond Myklebust8383e462007-07-06 15:12:04 -0400350 rcu_read_lock();
351 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Trond Myklebust86e89482008-12-23 15:21:39 -0500352 spin_lock(&delegation->lock);
353 if (delegation->inode != NULL && delegation->inode->i_sb == sb)
Trond Myklebust515d8612008-12-23 15:21:46 -0500354 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
Trond Myklebust86e89482008-12-23 15:21:39 -0500355 spin_unlock(&delegation->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400357 rcu_read_unlock();
Trond Myklebust515d8612008-12-23 15:21:46 -0500358 nfs_client_return_marked_delegations(clp);
359}
360
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500361static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
Trond Myklebust515d8612008-12-23 15:21:46 -0500362{
363 struct nfs_delegation *delegation;
364
365 rcu_read_lock();
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500366 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Trond Myklebust515d8612008-12-23 15:21:46 -0500367 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500368 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
369 }
Trond Myklebust515d8612008-12-23 15:21:46 -0500370 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
Trond Myklebustb0d3ded2008-12-23 15:21:50 -0500373static void nfs_delegation_run_state_manager(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +0100374{
Trond Myklebustb0d3ded2008-12-23 15:21:50 -0500375 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
376 nfs4_schedule_state_manager(clp);
Trond Myklebust58d97142006-01-03 09:55:24 +0100377}
378
David Howellsadfa6f92006-08-22 20:06:08 -0400379void nfs_expire_all_delegations(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +0100380{
Trond Myklebustb0d3ded2008-12-23 15:21:50 -0500381 nfs_client_mark_return_all_delegations(clp);
382 nfs_delegation_run_state_manager(clp);
Trond Myklebust58d97142006-01-03 09:55:24 +0100383}
384
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385/*
386 * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
387 */
David Howellsadfa6f92006-08-22 20:06:08 -0400388void nfs_handle_cb_pathdown(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (clp == NULL)
391 return;
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500392 nfs_client_mark_return_all_delegations(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393}
394
Trond Myklebustb7391f42008-12-23 15:21:52 -0500395static void nfs_client_mark_return_unreferenced_delegations(struct nfs_client *clp)
396{
397 struct nfs_delegation *delegation;
398
399 rcu_read_lock();
400 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
401 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
402 continue;
403 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
404 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
405 }
406 rcu_read_unlock();
407}
408
409void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
410{
411 nfs_client_mark_return_unreferenced_delegations(clp);
412 nfs_delegation_run_state_manager(clp);
413}
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415/*
416 * Asynchronous delegation recall!
417 */
418int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid)
419{
Trond Myklebust6411bd42008-12-23 15:21:51 -0500420 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
421 struct nfs_delegation *delegation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
Trond Myklebust6411bd42008-12-23 15:21:51 -0500423 rcu_read_lock();
424 delegation = rcu_dereference(NFS_I(inode)->delegation);
425 if (delegation == NULL || memcmp(delegation->stateid.data, stateid->data,
426 sizeof(delegation->stateid.data)) != 0) {
427 rcu_read_unlock();
428 return -ENOENT;
429 }
430 nfs_mark_return_delegation(clp, delegation);
431 rcu_read_unlock();
432 nfs_delegation_run_state_manager(clp);
433 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
436/*
437 * Retrieve the inode associated with a delegation
438 */
David Howellsadfa6f92006-08-22 20:06:08 -0400439struct inode *nfs_delegation_find_inode(struct nfs_client *clp, const struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
441 struct nfs_delegation *delegation;
442 struct inode *res = NULL;
Trond Myklebust8383e462007-07-06 15:12:04 -0400443 rcu_read_lock();
444 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Trond Myklebust86e89482008-12-23 15:21:39 -0500445 spin_lock(&delegation->lock);
446 if (delegation->inode != NULL &&
447 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 res = igrab(delegation->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 }
Trond Myklebust86e89482008-12-23 15:21:39 -0500450 spin_unlock(&delegation->lock);
451 if (res != NULL)
452 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400454 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 return res;
456}
457
458/*
459 * Mark all delegations as needing to be reclaimed
460 */
David Howellsadfa6f92006-08-22 20:06:08 -0400461void nfs_delegation_mark_reclaim(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462{
463 struct nfs_delegation *delegation;
Trond Myklebust8383e462007-07-06 15:12:04 -0400464 rcu_read_lock();
465 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list)
Trond Myklebust15c831b2008-12-23 15:21:39 -0500466 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
Trond Myklebust8383e462007-07-06 15:12:04 -0400467 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
470/*
471 * Reap all unclaimed delegations after reboot recovery is done
472 */
David Howellsadfa6f92006-08-22 20:06:08 -0400473void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Trond Myklebust8383e462007-07-06 15:12:04 -0400475 struct nfs_delegation *delegation;
Trond Myklebust86e89482008-12-23 15:21:39 -0500476 struct inode *inode;
Trond Myklebust8383e462007-07-06 15:12:04 -0400477restart:
478 rcu_read_lock();
479 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Trond Myklebust15c831b2008-12-23 15:21:39 -0500480 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 continue;
Trond Myklebust86e89482008-12-23 15:21:39 -0500482 inode = nfs_delegation_grab_inode(delegation);
483 if (inode == NULL)
484 continue;
Trond Myklebust8383e462007-07-06 15:12:04 -0400485 spin_lock(&clp->cl_lock);
Trond Myklebust86e89482008-12-23 15:21:39 -0500486 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
Trond Myklebust8383e462007-07-06 15:12:04 -0400487 spin_unlock(&clp->cl_lock);
488 rcu_read_unlock();
489 if (delegation != NULL)
Trond Myklebust905f8d12007-08-06 12:18:34 -0400490 nfs_free_delegation(delegation);
Trond Myklebust86e89482008-12-23 15:21:39 -0500491 iput(inode);
Trond Myklebust8383e462007-07-06 15:12:04 -0400492 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400494 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500496
497int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
498{
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500499 struct nfs_inode *nfsi = NFS_I(inode);
500 struct nfs_delegation *delegation;
Trond Myklebust8383e462007-07-06 15:12:04 -0400501 int ret = 0;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500502
Trond Myklebust8383e462007-07-06 15:12:04 -0400503 rcu_read_lock();
504 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500505 if (delegation != NULL) {
506 memcpy(dst->data, delegation->stateid.data, sizeof(dst->data));
Trond Myklebust8383e462007-07-06 15:12:04 -0400507 ret = 1;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500508 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400509 rcu_read_unlock();
510 return ret;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500511}