blob: 521d71b8182514f936003f59ee36e4dd1ba0159f [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/spinlock.h>
15
16#include <linux/nfs4.h>
17#include <linux/nfs_fs.h>
18#include <linux/nfs_xdr.h>
19
Trond Myklebust4ce79712005-06-22 17:16:21 +000020#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "delegation.h"
David Howells24c8dbb2006-08-22 20:06:10 -040022#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Trond Myklebust905f8d12007-08-06 12:18:34 -040024static void nfs_do_free_delegation(struct nfs_delegation *delegation)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
David Howells17d2c0a2010-05-01 12:37:18 -040026 if (delegation->cred)
27 put_rpccred(delegation->cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -070028 kfree(delegation);
29}
30
Trond Myklebust8383e462007-07-06 15:12:04 -040031static void nfs_free_delegation_callback(struct rcu_head *head)
32{
33 struct nfs_delegation *delegation = container_of(head, struct nfs_delegation, rcu);
34
Trond Myklebust905f8d12007-08-06 12:18:34 -040035 nfs_do_free_delegation(delegation);
36}
37
38static void nfs_free_delegation(struct nfs_delegation *delegation)
39{
Trond Myklebust905f8d12007-08-06 12:18:34 -040040 call_rcu(&delegation->rcu, nfs_free_delegation_callback);
Trond Myklebust8383e462007-07-06 15:12:04 -040041}
42
Trond Myklebustb7391f42008-12-23 15:21:52 -050043void nfs_mark_delegation_referenced(struct nfs_delegation *delegation)
44{
45 set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags);
46}
47
Trond Myklebustbd7bf9d2008-12-23 15:21:53 -050048int nfs_have_delegation(struct inode *inode, fmode_t flags)
Trond Myklebustb7391f42008-12-23 15:21:52 -050049{
50 struct nfs_delegation *delegation;
51 int ret = 0;
52
53 flags &= FMODE_READ|FMODE_WRITE;
54 rcu_read_lock();
55 delegation = rcu_dereference(NFS_I(inode)->delegation);
56 if (delegation != NULL && (delegation->type & flags) == flags) {
57 nfs_mark_delegation_referenced(delegation);
58 ret = 1;
59 }
60 rcu_read_unlock();
61 return ret;
62}
63
Trond Myklebust888e6942005-11-04 15:38:11 -050064static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state)
65{
66 struct inode *inode = state->inode;
67 struct file_lock *fl;
Trond Myklebustd5122202009-06-17 13:22:58 -070068 int status = 0;
Trond Myklebust888e6942005-11-04 15:38:11 -050069
Trond Myklebust3f09df72009-06-17 13:23:00 -070070 if (inode->i_flock == NULL)
71 goto out;
72
Arnd Bergmannb89f4322010-09-18 15:09:31 +020073 /* Protect inode->i_flock using the file locks lock */
74 lock_flocks();
Harvey Harrison90dc7d22008-02-20 13:03:05 -080075 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
Trond Myklebust888e6942005-11-04 15:38:11 -050076 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
77 continue;
Trond Myklebustcd3758e2007-08-10 17:44:32 -040078 if (nfs_file_open_context(fl->fl_file) != ctx)
Trond Myklebust888e6942005-11-04 15:38:11 -050079 continue;
Arnd Bergmannb89f4322010-09-18 15:09:31 +020080 unlock_flocks();
Trond Myklebust888e6942005-11-04 15:38:11 -050081 status = nfs4_lock_delegation_recall(state, fl);
Trond Myklebustd5122202009-06-17 13:22:58 -070082 if (status < 0)
Trond Myklebust3f09df72009-06-17 13:23:00 -070083 goto out;
Arnd Bergmannb89f4322010-09-18 15:09:31 +020084 lock_flocks();
Trond Myklebust888e6942005-11-04 15:38:11 -050085 }
Arnd Bergmannb89f4322010-09-18 15:09:31 +020086 unlock_flocks();
Trond Myklebust3f09df72009-06-17 13:23:00 -070087out:
Trond Myklebust888e6942005-11-04 15:38:11 -050088 return status;
89}
90
Trond Myklebustd18cc1f2009-12-03 08:10:17 -050091static int nfs_delegation_claim_opens(struct inode *inode, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
93 struct nfs_inode *nfsi = NFS_I(inode);
94 struct nfs_open_context *ctx;
95 struct nfs4_state *state;
Trond Myklebust888e6942005-11-04 15:38:11 -050096 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097
98again:
99 spin_lock(&inode->i_lock);
100 list_for_each_entry(ctx, &nfsi->open_files, list) {
101 state = ctx->state;
102 if (state == NULL)
103 continue;
104 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
105 continue;
Trond Myklebust90163022007-07-05 14:55:18 -0400106 if (memcmp(state->stateid.data, stateid->data, sizeof(state->stateid.data)) != 0)
107 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 get_nfs_open_context(ctx);
109 spin_unlock(&inode->i_lock);
Trond Myklebust13437e12007-07-06 15:10:43 -0400110 err = nfs4_open_delegation_recall(ctx, state, stateid);
Trond Myklebust888e6942005-11-04 15:38:11 -0500111 if (err >= 0)
112 err = nfs_delegation_claim_locks(ctx, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 put_nfs_open_context(ctx);
Trond Myklebust888e6942005-11-04 15:38:11 -0500114 if (err != 0)
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500115 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 goto again;
117 }
118 spin_unlock(&inode->i_lock);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500119 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120}
121
122/*
123 * Set up a delegation on an inode
124 */
125void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
126{
Trond Myklebust8f649c32010-05-01 12:36:18 -0400127 struct nfs_delegation *delegation;
128 struct rpc_cred *oldcred = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129
Trond Myklebust8f649c32010-05-01 12:36:18 -0400130 rcu_read_lock();
131 delegation = rcu_dereference(NFS_I(inode)->delegation);
132 if (delegation != NULL) {
133 spin_lock(&delegation->lock);
134 if (delegation->inode != NULL) {
135 memcpy(delegation->stateid.data, res->delegation.data,
136 sizeof(delegation->stateid.data));
137 delegation->type = res->delegation_type;
138 delegation->maxsize = res->maxsize;
139 oldcred = delegation->cred;
140 delegation->cred = get_rpccred(cred);
141 clear_bit(NFS_DELEGATION_NEED_RECLAIM,
142 &delegation->flags);
143 NFS_I(inode)->delegation_state = delegation->type;
144 spin_unlock(&delegation->lock);
145 put_rpccred(oldcred);
146 rcu_read_unlock();
147 } else {
148 /* We appear to have raced with a delegation return. */
149 spin_unlock(&delegation->lock);
150 rcu_read_unlock();
151 nfs_inode_set_delegation(inode, cred, res);
152 }
153 } else {
154 rcu_read_unlock();
155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
Trond Myklebust57bfa892008-01-25 16:38:18 -0500158static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
159{
160 int res = 0;
161
162 res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid, issync);
163 nfs_free_delegation(delegation);
164 return res;
165}
166
Trond Myklebust86e89482008-12-23 15:21:39 -0500167static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
168{
169 struct inode *inode = NULL;
170
171 spin_lock(&delegation->lock);
172 if (delegation->inode != NULL)
173 inode = igrab(delegation->inode);
174 spin_unlock(&delegation->lock);
175 return inode;
176}
177
Chuck Leverdda4b222010-12-24 01:32:54 +0000178static struct nfs_delegation *
179nfs_detach_delegation_locked(struct nfs_inode *nfsi,
180 struct nfs_client *clp)
Trond Myklebust57bfa892008-01-25 16:38:18 -0500181{
David Howells17d2c0a2010-05-01 12:37:18 -0400182 struct nfs_delegation *delegation =
183 rcu_dereference_protected(nfsi->delegation,
184 lockdep_is_held(&clp->cl_lock));
Trond Myklebust57bfa892008-01-25 16:38:18 -0500185
186 if (delegation == NULL)
187 goto nomatch;
Chuck Leverdda4b222010-12-24 01:32:54 +0000188
Trond Myklebust34310432008-12-23 15:21:38 -0500189 spin_lock(&delegation->lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500190 list_del_rcu(&delegation->super_list);
Trond Myklebust86e89482008-12-23 15:21:39 -0500191 delegation->inode = NULL;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500192 nfsi->delegation_state = 0;
193 rcu_assign_pointer(nfsi->delegation, NULL);
Trond Myklebust34310432008-12-23 15:21:38 -0500194 spin_unlock(&delegation->lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500195 return delegation;
196nomatch:
197 return NULL;
198}
199
Chuck Leverdda4b222010-12-24 01:32:54 +0000200static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi,
201 struct nfs_client *clp)
202{
203 struct nfs_delegation *delegation;
204
205 spin_lock(&clp->cl_lock);
206 delegation = nfs_detach_delegation_locked(nfsi, clp);
207 spin_unlock(&clp->cl_lock);
208 return delegation;
209}
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211/*
212 * Set up a delegation on an inode
213 */
214int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
215{
David Howells7539bba2006-08-22 20:06:09 -0400216 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 struct nfs_inode *nfsi = NFS_I(inode);
David Howells17d2c0a2010-05-01 12:37:18 -0400218 struct nfs_delegation *delegation, *old_delegation;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500219 struct nfs_delegation *freeme = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 int status = 0;
221
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400222 delegation = kmalloc(sizeof(*delegation), GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 if (delegation == NULL)
224 return -ENOMEM;
225 memcpy(delegation->stateid.data, res->delegation.data,
226 sizeof(delegation->stateid.data));
227 delegation->type = res->delegation_type;
228 delegation->maxsize = res->maxsize;
Trond Myklebustbeb2a5e2006-01-03 09:55:37 +0100229 delegation->change_attr = nfsi->change_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 delegation->cred = get_rpccred(cred);
231 delegation->inode = inode;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500232 delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
Trond Myklebust34310432008-12-23 15:21:38 -0500233 spin_lock_init(&delegation->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 spin_lock(&clp->cl_lock);
David Howells17d2c0a2010-05-01 12:37:18 -0400236 old_delegation = rcu_dereference_protected(nfsi->delegation,
237 lockdep_is_held(&clp->cl_lock));
238 if (old_delegation != NULL) {
239 if (memcmp(&delegation->stateid, &old_delegation->stateid,
240 sizeof(old_delegation->stateid)) == 0 &&
241 delegation->type == old_delegation->type) {
Trond Myklebust57bfa892008-01-25 16:38:18 -0500242 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
Trond Myklebust57bfa892008-01-25 16:38:18 -0500244 /*
245 * Deal with broken servers that hand out two
246 * delegations for the same file.
247 */
248 dfprintk(FILE, "%s: server %s handed out "
249 "a duplicate delegation!\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700250 __func__, clp->cl_hostname);
David Howells17d2c0a2010-05-01 12:37:18 -0400251 if (delegation->type <= old_delegation->type) {
Trond Myklebust57bfa892008-01-25 16:38:18 -0500252 freeme = delegation;
253 delegation = NULL;
254 goto out;
255 }
Chuck Leverdda4b222010-12-24 01:32:54 +0000256 freeme = nfs_detach_delegation_locked(nfsi, clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
Trond Myklebust57bfa892008-01-25 16:38:18 -0500258 list_add_rcu(&delegation->super_list, &clp->cl_delegations);
259 nfsi->delegation_state = delegation->type;
260 rcu_assign_pointer(nfsi->delegation, delegation);
261 delegation = NULL;
Trond Myklebust412c77c2007-07-03 16:10:55 -0400262
263 /* Ensure we revalidate the attributes and page cache! */
264 spin_lock(&inode->i_lock);
265 nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
266 spin_unlock(&inode->i_lock);
267
Trond Myklebust57bfa892008-01-25 16:38:18 -0500268out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 spin_unlock(&clp->cl_lock);
Trond Myklebust603c83d2007-10-18 19:59:20 -0400270 if (delegation != NULL)
271 nfs_free_delegation(delegation);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500272 if (freeme != NULL)
273 nfs_do_return_delegation(inode, freeme, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 return status;
275}
276
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277/*
278 * Basic procedure for returning a delegation to the server
279 */
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500280static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500283 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284
Trond Myklebust3f09df72009-06-17 13:23:00 -0700285 /*
286 * Guard against new delegated open/lock/unlock calls and against
287 * state recovery
288 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 down_write(&nfsi->rwsem);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500290 err = nfs_delegation_claim_opens(inode, &delegation->stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 up_write(&nfsi->rwsem);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500292 if (err)
293 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500295 err = nfs_do_return_delegation(inode, delegation, issync);
296out:
297 return err;
Trond Myklebust90163022007-07-05 14:55:18 -0400298}
299
Trond Myklebuste6f81072008-01-24 18:14:34 -0500300/*
Trond Myklebust515d8612008-12-23 15:21:46 -0500301 * Return all delegations that have been marked for return
302 */
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500303int nfs_client_return_marked_delegations(struct nfs_client *clp)
Trond Myklebust515d8612008-12-23 15:21:46 -0500304{
305 struct nfs_delegation *delegation;
306 struct inode *inode;
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500307 int err = 0;
Trond Myklebust515d8612008-12-23 15:21:46 -0500308
309restart:
310 rcu_read_lock();
311 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
312 if (!test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
313 continue;
314 inode = nfs_delegation_grab_inode(delegation);
315 if (inode == NULL)
316 continue;
Chuck Leverdda4b222010-12-24 01:32:54 +0000317 delegation = nfs_detach_delegation(NFS_I(inode), clp);
Trond Myklebust515d8612008-12-23 15:21:46 -0500318 rcu_read_unlock();
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500319 if (delegation != NULL) {
320 filemap_flush(inode->i_mapping);
321 err = __nfs_inode_return_delegation(inode, delegation, 0);
322 }
Trond Myklebust515d8612008-12-23 15:21:46 -0500323 iput(inode);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500324 if (!err)
325 goto restart;
326 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
327 return err;
Trond Myklebust515d8612008-12-23 15:21:46 -0500328 }
329 rcu_read_unlock();
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500330 return 0;
Trond Myklebust515d8612008-12-23 15:21:46 -0500331}
332
333/*
Trond Myklebuste6f81072008-01-24 18:14:34 -0500334 * This function returns the delegation without reclaiming opens
335 * or protecting against delegation reclaims.
336 * It is therefore really only safe to be called from
337 * nfs4_clear_inode()
338 */
339void nfs_inode_return_delegation_noreclaim(struct inode *inode)
340{
341 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
342 struct nfs_inode *nfsi = NFS_I(inode);
343 struct nfs_delegation *delegation;
344
David Howells17d2c0a2010-05-01 12:37:18 -0400345 if (rcu_access_pointer(nfsi->delegation) != NULL) {
Chuck Leverdda4b222010-12-24 01:32:54 +0000346 delegation = nfs_detach_delegation(nfsi, clp);
Trond Myklebuste6f81072008-01-24 18:14:34 -0500347 if (delegation != NULL)
348 nfs_do_return_delegation(inode, delegation, 0);
349 }
350}
351
Trond Myklebust90163022007-07-05 14:55:18 -0400352int nfs_inode_return_delegation(struct inode *inode)
353{
354 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
355 struct nfs_inode *nfsi = NFS_I(inode);
356 struct nfs_delegation *delegation;
357 int err = 0;
358
David Howells17d2c0a2010-05-01 12:37:18 -0400359 if (rcu_access_pointer(nfsi->delegation) != NULL) {
Chuck Leverdda4b222010-12-24 01:32:54 +0000360 delegation = nfs_detach_delegation(nfsi, clp);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500361 if (delegation != NULL) {
Trond Myklebust1b924e52010-07-31 14:29:06 -0400362 nfs_wb_all(inode);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500363 err = __nfs_inode_return_delegation(inode, delegation, 1);
364 }
Trond Myklebust90163022007-07-05 14:55:18 -0400365 }
366 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
Trond Myklebust6411bd42008-12-23 15:21:51 -0500369static void nfs_mark_return_delegation(struct nfs_client *clp, struct nfs_delegation *delegation)
370{
371 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
372 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
373}
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375/*
376 * Return all delegations associated to a super block
377 */
Trond Myklebust515d8612008-12-23 15:21:46 -0500378void nfs_super_return_all_delegations(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379{
David Howells7539bba2006-08-22 20:06:09 -0400380 struct nfs_client *clp = NFS_SB(sb)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 struct nfs_delegation *delegation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382
383 if (clp == NULL)
384 return;
Trond Myklebust8383e462007-07-06 15:12:04 -0400385 rcu_read_lock();
386 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Trond Myklebust86e89482008-12-23 15:21:39 -0500387 spin_lock(&delegation->lock);
388 if (delegation->inode != NULL && delegation->inode->i_sb == sb)
Trond Myklebust515d8612008-12-23 15:21:46 -0500389 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
Trond Myklebust86e89482008-12-23 15:21:39 -0500390 spin_unlock(&delegation->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400392 rcu_read_unlock();
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500393 if (nfs_client_return_marked_delegations(clp) != 0)
394 nfs4_schedule_state_manager(clp);
Trond Myklebust515d8612008-12-23 15:21:46 -0500395}
396
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500397static
398void nfs_client_mark_return_all_delegation_types(struct nfs_client *clp, fmode_t flags)
Trond Myklebust515d8612008-12-23 15:21:46 -0500399{
400 struct nfs_delegation *delegation;
401
402 rcu_read_lock();
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500403 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500404 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
405 continue;
406 if (delegation->type & flags)
407 nfs_mark_return_delegation(clp, delegation);
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500408 }
Trond Myklebust515d8612008-12-23 15:21:46 -0500409 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410}
411
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500412static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
413{
414 nfs_client_mark_return_all_delegation_types(clp, FMODE_READ|FMODE_WRITE);
415}
416
Trond Myklebustb0d3ded2008-12-23 15:21:50 -0500417static void nfs_delegation_run_state_manager(struct nfs_client *clp)
Trond Myklebust58d9714a2006-01-03 09:55:24 +0100418{
Trond Myklebustb0d3ded2008-12-23 15:21:50 -0500419 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
420 nfs4_schedule_state_manager(clp);
Trond Myklebust58d9714a2006-01-03 09:55:24 +0100421}
422
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500423void nfs_expire_all_delegation_types(struct nfs_client *clp, fmode_t flags)
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500424{
425 nfs_client_mark_return_all_delegation_types(clp, flags);
426 nfs_delegation_run_state_manager(clp);
427}
428
David Howellsadfa6f92006-08-22 20:06:08 -0400429void nfs_expire_all_delegations(struct nfs_client *clp)
Trond Myklebust58d9714a2006-01-03 09:55:24 +0100430{
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500431 nfs_expire_all_delegation_types(clp, FMODE_READ|FMODE_WRITE);
Trond Myklebust58d9714a2006-01-03 09:55:24 +0100432}
433
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434/*
435 * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
436 */
David Howellsadfa6f92006-08-22 20:06:08 -0400437void nfs_handle_cb_pathdown(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 if (clp == NULL)
440 return;
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500441 nfs_client_mark_return_all_delegations(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442}
443
Trond Myklebustb7391f42008-12-23 15:21:52 -0500444static void nfs_client_mark_return_unreferenced_delegations(struct nfs_client *clp)
445{
446 struct nfs_delegation *delegation;
447
448 rcu_read_lock();
449 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
450 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
451 continue;
Alexandros Batsakisb4a6f492009-12-05 13:19:11 -0500452 nfs_mark_return_delegation(clp, delegation);
Trond Myklebustb7391f42008-12-23 15:21:52 -0500453 }
454 rcu_read_unlock();
455}
456
457void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
458{
459 nfs_client_mark_return_unreferenced_delegations(clp);
460 nfs_delegation_run_state_manager(clp);
461}
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463/*
464 * Asynchronous delegation recall!
465 */
Trond Myklebuste047a102010-06-16 09:52:27 -0400466int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Trond Myklebust6411bd42008-12-23 15:21:51 -0500468 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
469 struct nfs_delegation *delegation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470
Trond Myklebust6411bd42008-12-23 15:21:51 -0500471 rcu_read_lock();
472 delegation = rcu_dereference(NFS_I(inode)->delegation);
Alexandros Batsakis25976412009-12-05 13:48:55 -0500473
Trond Myklebuste047a102010-06-16 09:52:27 -0400474 if (!clp->cl_mvops->validate_stateid(delegation, stateid)) {
Trond Myklebust6411bd42008-12-23 15:21:51 -0500475 rcu_read_unlock();
476 return -ENOENT;
477 }
Alexandros Batsakis25976412009-12-05 13:48:55 -0500478
Trond Myklebust6411bd42008-12-23 15:21:51 -0500479 nfs_mark_return_delegation(clp, delegation);
480 rcu_read_unlock();
481 nfs_delegation_run_state_manager(clp);
482 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483}
484
485/*
486 * Retrieve the inode associated with a delegation
487 */
David Howellsadfa6f92006-08-22 20:06:08 -0400488struct inode *nfs_delegation_find_inode(struct nfs_client *clp, const struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 struct nfs_delegation *delegation;
491 struct inode *res = NULL;
Trond Myklebust8383e462007-07-06 15:12:04 -0400492 rcu_read_lock();
493 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Trond Myklebust86e89482008-12-23 15:21:39 -0500494 spin_lock(&delegation->lock);
495 if (delegation->inode != NULL &&
496 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 res = igrab(delegation->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
Trond Myklebust86e89482008-12-23 15:21:39 -0500499 spin_unlock(&delegation->lock);
500 if (res != NULL)
501 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400503 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 return res;
505}
506
507/*
508 * Mark all delegations as needing to be reclaimed
509 */
David Howellsadfa6f92006-08-22 20:06:08 -0400510void nfs_delegation_mark_reclaim(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
512 struct nfs_delegation *delegation;
Trond Myklebust8383e462007-07-06 15:12:04 -0400513 rcu_read_lock();
514 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list)
Trond Myklebust15c831b2008-12-23 15:21:39 -0500515 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
Trond Myklebust8383e462007-07-06 15:12:04 -0400516 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517}
518
519/*
520 * Reap all unclaimed delegations after reboot recovery is done
521 */
David Howellsadfa6f92006-08-22 20:06:08 -0400522void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Trond Myklebust8383e462007-07-06 15:12:04 -0400524 struct nfs_delegation *delegation;
Trond Myklebust86e89482008-12-23 15:21:39 -0500525 struct inode *inode;
Trond Myklebust8383e462007-07-06 15:12:04 -0400526restart:
527 rcu_read_lock();
528 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Trond Myklebust15c831b2008-12-23 15:21:39 -0500529 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 continue;
Trond Myklebust86e89482008-12-23 15:21:39 -0500531 inode = nfs_delegation_grab_inode(delegation);
532 if (inode == NULL)
533 continue;
Chuck Leverdda4b222010-12-24 01:32:54 +0000534 delegation = nfs_detach_delegation(NFS_I(inode), clp);
Trond Myklebust8383e462007-07-06 15:12:04 -0400535 rcu_read_unlock();
536 if (delegation != NULL)
Trond Myklebust905f8d12007-08-06 12:18:34 -0400537 nfs_free_delegation(delegation);
Trond Myklebust86e89482008-12-23 15:21:39 -0500538 iput(inode);
Trond Myklebust8383e462007-07-06 15:12:04 -0400539 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400541 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500543
544int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
545{
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500546 struct nfs_inode *nfsi = NFS_I(inode);
547 struct nfs_delegation *delegation;
Trond Myklebust8383e462007-07-06 15:12:04 -0400548 int ret = 0;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500549
Trond Myklebust8383e462007-07-06 15:12:04 -0400550 rcu_read_lock();
551 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500552 if (delegation != NULL) {
553 memcpy(dst->data, delegation->stateid.data, sizeof(dst->data));
Trond Myklebust8383e462007-07-06 15:12:04 -0400554 ret = 1;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500555 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400556 rcu_read_unlock();
557 return ret;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500558}