blob: 15671245c6eec62c14fcdce139bd3ea9823664d5 [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Alexey Dobriyan405f5572009-07-11 22:08:37 +040014#include <linux/smp_lock.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/spinlock.h>
16
17#include <linux/nfs4.h>
18#include <linux/nfs_fs.h>
19#include <linux/nfs_xdr.h>
20
Trond Myklebust4ce79712005-06-22 17:16:21 +000021#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "delegation.h"
David Howells24c8dbb2006-08-22 20:06:10 -040023#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Trond Myklebust905f8d12007-08-06 12:18:34 -040025static void nfs_do_free_delegation(struct nfs_delegation *delegation)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026{
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 kfree(delegation);
28}
29
Trond Myklebust8383e462007-07-06 15:12:04 -040030static void nfs_free_delegation_callback(struct rcu_head *head)
31{
32 struct nfs_delegation *delegation = container_of(head, struct nfs_delegation, rcu);
33
Trond Myklebust905f8d12007-08-06 12:18:34 -040034 nfs_do_free_delegation(delegation);
35}
36
37static void nfs_free_delegation(struct nfs_delegation *delegation)
38{
39 struct rpc_cred *cred;
40
41 cred = rcu_dereference(delegation->cred);
42 rcu_assign_pointer(delegation->cred, NULL);
43 call_rcu(&delegation->rcu, nfs_free_delegation_callback);
44 if (cred)
45 put_rpccred(cred);
Trond Myklebust8383e462007-07-06 15:12:04 -040046}
47
Trond Myklebustb7391f42008-12-23 15:21:52 -050048void nfs_mark_delegation_referenced(struct nfs_delegation *delegation)
49{
50 set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags);
51}
52
Trond Myklebustbd7bf9d2008-12-23 15:21:53 -050053int nfs_have_delegation(struct inode *inode, fmode_t flags)
Trond Myklebustb7391f42008-12-23 15:21:52 -050054{
55 struct nfs_delegation *delegation;
56 int ret = 0;
57
58 flags &= FMODE_READ|FMODE_WRITE;
59 rcu_read_lock();
60 delegation = rcu_dereference(NFS_I(inode)->delegation);
61 if (delegation != NULL && (delegation->type & flags) == flags) {
62 nfs_mark_delegation_referenced(delegation);
63 ret = 1;
64 }
65 rcu_read_unlock();
66 return ret;
67}
68
Trond Myklebust888e6942005-11-04 15:38:11 -050069static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state)
70{
71 struct inode *inode = state->inode;
72 struct file_lock *fl;
Trond Myklebustd5122202009-06-17 13:22:58 -070073 int status = 0;
Trond Myklebust888e6942005-11-04 15:38:11 -050074
Trond Myklebust3f09df72009-06-17 13:23:00 -070075 if (inode->i_flock == NULL)
76 goto out;
77
78 /* Protect inode->i_flock using the BKL */
79 lock_kernel();
Harvey Harrison90dc7d22008-02-20 13:03:05 -080080 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
Trond Myklebust888e6942005-11-04 15:38:11 -050081 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
82 continue;
Trond Myklebustcd3758e2007-08-10 17:44:32 -040083 if (nfs_file_open_context(fl->fl_file) != ctx)
Trond Myklebust888e6942005-11-04 15:38:11 -050084 continue;
Trond Myklebust3f09df72009-06-17 13:23:00 -070085 unlock_kernel();
Trond Myklebust888e6942005-11-04 15:38:11 -050086 status = nfs4_lock_delegation_recall(state, fl);
Trond Myklebustd5122202009-06-17 13:22:58 -070087 if (status < 0)
Trond Myklebust3f09df72009-06-17 13:23:00 -070088 goto out;
89 lock_kernel();
Trond Myklebust888e6942005-11-04 15:38:11 -050090 }
Trond Myklebust3f09df72009-06-17 13:23:00 -070091 unlock_kernel();
92out:
Trond Myklebust888e6942005-11-04 15:38:11 -050093 return status;
94}
95
Trond Myklebustd18cc1f2009-12-03 08:10:17 -050096static int nfs_delegation_claim_opens(struct inode *inode, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
98 struct nfs_inode *nfsi = NFS_I(inode);
99 struct nfs_open_context *ctx;
100 struct nfs4_state *state;
Trond Myklebust888e6942005-11-04 15:38:11 -0500101 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103again:
104 spin_lock(&inode->i_lock);
105 list_for_each_entry(ctx, &nfsi->open_files, list) {
106 state = ctx->state;
107 if (state == NULL)
108 continue;
109 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
110 continue;
Trond Myklebust90163022007-07-05 14:55:18 -0400111 if (memcmp(state->stateid.data, stateid->data, sizeof(state->stateid.data)) != 0)
112 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 get_nfs_open_context(ctx);
114 spin_unlock(&inode->i_lock);
Trond Myklebust13437e12007-07-06 15:10:43 -0400115 err = nfs4_open_delegation_recall(ctx, state, stateid);
Trond Myklebust888e6942005-11-04 15:38:11 -0500116 if (err >= 0)
117 err = nfs_delegation_claim_locks(ctx, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 put_nfs_open_context(ctx);
Trond Myklebust888e6942005-11-04 15:38:11 -0500119 if (err != 0)
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500120 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 goto again;
122 }
123 spin_unlock(&inode->i_lock);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500124 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125}
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 Myklebustd18cc1f2009-12-03 08:10:17 -0500266static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500269 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Trond Myklebust3f09df72009-06-17 13:23:00 -0700271 /*
272 * Guard against new delegated open/lock/unlock calls and against
273 * state recovery
274 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 down_write(&nfsi->rwsem);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500276 err = nfs_delegation_claim_opens(inode, &delegation->stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 up_write(&nfsi->rwsem);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500278 if (err)
279 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500281 err = nfs_do_return_delegation(inode, delegation, issync);
282out:
283 return err;
Trond Myklebust90163022007-07-05 14:55:18 -0400284}
285
Trond Myklebuste6f81072008-01-24 18:14:34 -0500286/*
Trond Myklebust515d8612008-12-23 15:21:46 -0500287 * Return all delegations that have been marked for return
288 */
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500289int nfs_client_return_marked_delegations(struct nfs_client *clp)
Trond Myklebust515d8612008-12-23 15:21:46 -0500290{
291 struct nfs_delegation *delegation;
292 struct inode *inode;
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500293 int err = 0;
Trond Myklebust515d8612008-12-23 15:21:46 -0500294
295restart:
296 rcu_read_lock();
297 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
298 if (!test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
299 continue;
300 inode = nfs_delegation_grab_inode(delegation);
301 if (inode == NULL)
302 continue;
303 spin_lock(&clp->cl_lock);
304 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
305 spin_unlock(&clp->cl_lock);
306 rcu_read_unlock();
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500307 if (delegation != NULL) {
308 filemap_flush(inode->i_mapping);
309 err = __nfs_inode_return_delegation(inode, delegation, 0);
310 }
Trond Myklebust515d8612008-12-23 15:21:46 -0500311 iput(inode);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500312 if (!err)
313 goto restart;
314 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
315 return err;
Trond Myklebust515d8612008-12-23 15:21:46 -0500316 }
317 rcu_read_unlock();
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500318 return 0;
Trond Myklebust515d8612008-12-23 15:21:46 -0500319}
320
321/*
Trond Myklebuste6f81072008-01-24 18:14:34 -0500322 * This function returns the delegation without reclaiming opens
323 * or protecting against delegation reclaims.
324 * It is therefore really only safe to be called from
325 * nfs4_clear_inode()
326 */
327void nfs_inode_return_delegation_noreclaim(struct inode *inode)
328{
329 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
330 struct nfs_inode *nfsi = NFS_I(inode);
331 struct nfs_delegation *delegation;
332
333 if (rcu_dereference(nfsi->delegation) != NULL) {
334 spin_lock(&clp->cl_lock);
335 delegation = nfs_detach_delegation_locked(nfsi, NULL);
336 spin_unlock(&clp->cl_lock);
337 if (delegation != NULL)
338 nfs_do_return_delegation(inode, delegation, 0);
339 }
340}
341
Trond Myklebust90163022007-07-05 14:55:18 -0400342int nfs_inode_return_delegation(struct inode *inode)
343{
344 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
345 struct nfs_inode *nfsi = NFS_I(inode);
346 struct nfs_delegation *delegation;
347 int err = 0;
348
Trond Myklebust8383e462007-07-06 15:12:04 -0400349 if (rcu_dereference(nfsi->delegation) != NULL) {
Trond Myklebust90163022007-07-05 14:55:18 -0400350 spin_lock(&clp->cl_lock);
351 delegation = nfs_detach_delegation_locked(nfsi, NULL);
352 spin_unlock(&clp->cl_lock);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500353 if (delegation != NULL) {
354 nfs_msync_inode(inode);
355 err = __nfs_inode_return_delegation(inode, delegation, 1);
356 }
Trond Myklebust90163022007-07-05 14:55:18 -0400357 }
358 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359}
360
Trond Myklebust6411bd42008-12-23 15:21:51 -0500361static void nfs_mark_return_delegation(struct nfs_client *clp, struct nfs_delegation *delegation)
362{
363 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
364 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
365}
366
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367/*
368 * Return all delegations associated to a super block
369 */
Trond Myklebust515d8612008-12-23 15:21:46 -0500370void nfs_super_return_all_delegations(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
David Howells7539bba2006-08-22 20:06:09 -0400372 struct nfs_client *clp = NFS_SB(sb)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 struct nfs_delegation *delegation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
375 if (clp == NULL)
376 return;
Trond Myklebust8383e462007-07-06 15:12:04 -0400377 rcu_read_lock();
378 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Trond Myklebust86e89482008-12-23 15:21:39 -0500379 spin_lock(&delegation->lock);
380 if (delegation->inode != NULL && delegation->inode->i_sb == sb)
Trond Myklebust515d8612008-12-23 15:21:46 -0500381 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
Trond Myklebust86e89482008-12-23 15:21:39 -0500382 spin_unlock(&delegation->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400384 rcu_read_unlock();
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500385 if (nfs_client_return_marked_delegations(clp) != 0)
386 nfs4_schedule_state_manager(clp);
Trond Myklebust515d8612008-12-23 15:21:46 -0500387}
388
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500389static
390void nfs_client_mark_return_all_delegation_types(struct nfs_client *clp, fmode_t flags)
Trond Myklebust515d8612008-12-23 15:21:46 -0500391{
392 struct nfs_delegation *delegation;
393
394 rcu_read_lock();
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500395 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500396 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
397 continue;
398 if (delegation->type & flags)
399 nfs_mark_return_delegation(clp, delegation);
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500400 }
Trond Myklebust515d8612008-12-23 15:21:46 -0500401 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402}
403
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500404static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
405{
406 nfs_client_mark_return_all_delegation_types(clp, FMODE_READ|FMODE_WRITE);
407}
408
Trond Myklebustb0d3ded2008-12-23 15:21:50 -0500409static void nfs_delegation_run_state_manager(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +0100410{
Trond Myklebustb0d3ded2008-12-23 15:21:50 -0500411 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
412 nfs4_schedule_state_manager(clp);
Trond Myklebust58d97142006-01-03 09:55:24 +0100413}
414
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500415void nfs_expire_all_delegation_types(struct nfs_client *clp, fmode_t flags)
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500416{
417 nfs_client_mark_return_all_delegation_types(clp, flags);
418 nfs_delegation_run_state_manager(clp);
419}
420
David Howellsadfa6f92006-08-22 20:06:08 -0400421void nfs_expire_all_delegations(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +0100422{
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500423 nfs_expire_all_delegation_types(clp, FMODE_READ|FMODE_WRITE);
Trond Myklebust58d97142006-01-03 09:55:24 +0100424}
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426/*
427 * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
428 */
David Howellsadfa6f92006-08-22 20:06:08 -0400429void nfs_handle_cb_pathdown(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (clp == NULL)
432 return;
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500433 nfs_client_mark_return_all_delegations(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
Trond Myklebustb7391f42008-12-23 15:21:52 -0500436static void nfs_client_mark_return_unreferenced_delegations(struct nfs_client *clp)
437{
438 struct nfs_delegation *delegation;
439
440 rcu_read_lock();
441 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
442 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
443 continue;
Alexandros Batsakisb4a6f492009-12-05 13:19:11 -0500444 nfs_mark_return_delegation(clp, delegation);
Trond Myklebustb7391f42008-12-23 15:21:52 -0500445 }
446 rcu_read_unlock();
447}
448
449void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
450{
451 nfs_client_mark_return_unreferenced_delegations(clp);
452 nfs_delegation_run_state_manager(clp);
453}
454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455/*
456 * Asynchronous delegation recall!
457 */
Alexandros Batsakis25976412009-12-05 13:48:55 -0500458int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid,
459 int (*validate_stateid)(struct nfs_delegation *delegation,
460 const nfs4_stateid *stateid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Trond Myklebust6411bd42008-12-23 15:21:51 -0500462 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
463 struct nfs_delegation *delegation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Trond Myklebust6411bd42008-12-23 15:21:51 -0500465 rcu_read_lock();
466 delegation = rcu_dereference(NFS_I(inode)->delegation);
Alexandros Batsakis25976412009-12-05 13:48:55 -0500467
468 if (!validate_stateid(delegation, stateid)) {
Trond Myklebust6411bd42008-12-23 15:21:51 -0500469 rcu_read_unlock();
470 return -ENOENT;
471 }
Alexandros Batsakis25976412009-12-05 13:48:55 -0500472
Trond Myklebust6411bd42008-12-23 15:21:51 -0500473 nfs_mark_return_delegation(clp, delegation);
474 rcu_read_unlock();
475 nfs_delegation_run_state_manager(clp);
476 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477}
478
479/*
480 * Retrieve the inode associated with a delegation
481 */
David Howellsadfa6f92006-08-22 20:06:08 -0400482struct inode *nfs_delegation_find_inode(struct nfs_client *clp, const struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
484 struct nfs_delegation *delegation;
485 struct inode *res = NULL;
Trond Myklebust8383e462007-07-06 15:12:04 -0400486 rcu_read_lock();
487 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Trond Myklebust86e89482008-12-23 15:21:39 -0500488 spin_lock(&delegation->lock);
489 if (delegation->inode != NULL &&
490 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 res = igrab(delegation->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
Trond Myklebust86e89482008-12-23 15:21:39 -0500493 spin_unlock(&delegation->lock);
494 if (res != NULL)
495 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400497 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 return res;
499}
500
501/*
502 * Mark all delegations as needing to be reclaimed
503 */
David Howellsadfa6f92006-08-22 20:06:08 -0400504void nfs_delegation_mark_reclaim(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
506 struct nfs_delegation *delegation;
Trond Myklebust8383e462007-07-06 15:12:04 -0400507 rcu_read_lock();
508 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list)
Trond Myklebust15c831b2008-12-23 15:21:39 -0500509 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
Trond Myklebust8383e462007-07-06 15:12:04 -0400510 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
513/*
514 * Reap all unclaimed delegations after reboot recovery is done
515 */
David Howellsadfa6f92006-08-22 20:06:08 -0400516void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517{
Trond Myklebust8383e462007-07-06 15:12:04 -0400518 struct nfs_delegation *delegation;
Trond Myklebust86e89482008-12-23 15:21:39 -0500519 struct inode *inode;
Trond Myklebust8383e462007-07-06 15:12:04 -0400520restart:
521 rcu_read_lock();
522 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Trond Myklebust15c831b2008-12-23 15:21:39 -0500523 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 continue;
Trond Myklebust86e89482008-12-23 15:21:39 -0500525 inode = nfs_delegation_grab_inode(delegation);
526 if (inode == NULL)
527 continue;
Trond Myklebust8383e462007-07-06 15:12:04 -0400528 spin_lock(&clp->cl_lock);
Trond Myklebust86e89482008-12-23 15:21:39 -0500529 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
Trond Myklebust8383e462007-07-06 15:12:04 -0400530 spin_unlock(&clp->cl_lock);
531 rcu_read_unlock();
532 if (delegation != NULL)
Trond Myklebust905f8d12007-08-06 12:18:34 -0400533 nfs_free_delegation(delegation);
Trond Myklebust86e89482008-12-23 15:21:39 -0500534 iput(inode);
Trond Myklebust8383e462007-07-06 15:12:04 -0400535 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400537 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538}
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500539
540int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
541{
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500542 struct nfs_inode *nfsi = NFS_I(inode);
543 struct nfs_delegation *delegation;
Trond Myklebust8383e462007-07-06 15:12:04 -0400544 int ret = 0;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500545
Trond Myklebust8383e462007-07-06 15:12:04 -0400546 rcu_read_lock();
547 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500548 if (delegation != NULL) {
549 memcpy(dst->data, delegation->stateid.data, sizeof(dst->data));
Trond Myklebust8383e462007-07-06 15:12:04 -0400550 ret = 1;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500551 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400552 rcu_read_unlock();
553 return ret;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500554}