blob: 8d9ec494a9446332b0908b092ff5ff902ff72339 [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{
Trond Myklebust8f649c32010-05-01 12:36:18 -0400132 struct nfs_delegation *delegation;
133 struct rpc_cred *oldcred = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134
Trond Myklebust8f649c32010-05-01 12:36:18 -0400135 rcu_read_lock();
136 delegation = rcu_dereference(NFS_I(inode)->delegation);
137 if (delegation != NULL) {
138 spin_lock(&delegation->lock);
139 if (delegation->inode != NULL) {
140 memcpy(delegation->stateid.data, res->delegation.data,
141 sizeof(delegation->stateid.data));
142 delegation->type = res->delegation_type;
143 delegation->maxsize = res->maxsize;
144 oldcred = delegation->cred;
145 delegation->cred = get_rpccred(cred);
146 clear_bit(NFS_DELEGATION_NEED_RECLAIM,
147 &delegation->flags);
148 NFS_I(inode)->delegation_state = delegation->type;
149 spin_unlock(&delegation->lock);
150 put_rpccred(oldcred);
151 rcu_read_unlock();
152 } else {
153 /* We appear to have raced with a delegation return. */
154 spin_unlock(&delegation->lock);
155 rcu_read_unlock();
156 nfs_inode_set_delegation(inode, cred, res);
157 }
158 } else {
159 rcu_read_unlock();
160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
Trond Myklebust57bfa892008-01-25 16:38:18 -0500163static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
164{
165 int res = 0;
166
167 res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid, issync);
168 nfs_free_delegation(delegation);
169 return res;
170}
171
Trond Myklebust86e89482008-12-23 15:21:39 -0500172static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
173{
174 struct inode *inode = NULL;
175
176 spin_lock(&delegation->lock);
177 if (delegation->inode != NULL)
178 inode = igrab(delegation->inode);
179 spin_unlock(&delegation->lock);
180 return inode;
181}
182
Trond Myklebust57bfa892008-01-25 16:38:18 -0500183static struct nfs_delegation *nfs_detach_delegation_locked(struct nfs_inode *nfsi, const nfs4_stateid *stateid)
184{
185 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
186
187 if (delegation == NULL)
188 goto nomatch;
Trond Myklebust34310432008-12-23 15:21:38 -0500189 spin_lock(&delegation->lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500190 if (stateid != NULL && memcmp(delegation->stateid.data, stateid->data,
191 sizeof(delegation->stateid.data)) != 0)
Trond Myklebust34310432008-12-23 15:21:38 -0500192 goto nomatch_unlock;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500193 list_del_rcu(&delegation->super_list);
Trond Myklebust86e89482008-12-23 15:21:39 -0500194 delegation->inode = NULL;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500195 nfsi->delegation_state = 0;
196 rcu_assign_pointer(nfsi->delegation, NULL);
Trond Myklebust34310432008-12-23 15:21:38 -0500197 spin_unlock(&delegation->lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500198 return delegation;
Trond Myklebust34310432008-12-23 15:21:38 -0500199nomatch_unlock:
200 spin_unlock(&delegation->lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500201nomatch:
202 return NULL;
203}
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205/*
206 * Set up a delegation on an inode
207 */
208int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
209{
David Howells7539bba2006-08-22 20:06:09 -0400210 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 struct nfs_inode *nfsi = NFS_I(inode);
212 struct nfs_delegation *delegation;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500213 struct nfs_delegation *freeme = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 int status = 0;
215
Panagiotis Issarisf52720c2006-09-27 01:49:39 -0700216 delegation = kmalloc(sizeof(*delegation), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (delegation == NULL)
218 return -ENOMEM;
219 memcpy(delegation->stateid.data, res->delegation.data,
220 sizeof(delegation->stateid.data));
221 delegation->type = res->delegation_type;
222 delegation->maxsize = res->maxsize;
Trond Myklebustbeb2a5e2006-01-03 09:55:37 +0100223 delegation->change_attr = nfsi->change_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 delegation->cred = get_rpccred(cred);
225 delegation->inode = inode;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500226 delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
Trond Myklebust34310432008-12-23 15:21:38 -0500227 spin_lock_init(&delegation->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
229 spin_lock(&clp->cl_lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500230 if (rcu_dereference(nfsi->delegation) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 if (memcmp(&delegation->stateid, &nfsi->delegation->stateid,
Trond Myklebust57bfa892008-01-25 16:38:18 -0500232 sizeof(delegation->stateid)) == 0 &&
233 delegation->type == nfsi->delegation->type) {
234 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
Trond Myklebust57bfa892008-01-25 16:38:18 -0500236 /*
237 * Deal with broken servers that hand out two
238 * delegations for the same file.
239 */
240 dfprintk(FILE, "%s: server %s handed out "
241 "a duplicate delegation!\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700242 __func__, clp->cl_hostname);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500243 if (delegation->type <= nfsi->delegation->type) {
244 freeme = delegation;
245 delegation = NULL;
246 goto out;
247 }
248 freeme = nfs_detach_delegation_locked(nfsi, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
Trond Myklebust57bfa892008-01-25 16:38:18 -0500250 list_add_rcu(&delegation->super_list, &clp->cl_delegations);
251 nfsi->delegation_state = delegation->type;
252 rcu_assign_pointer(nfsi->delegation, delegation);
253 delegation = NULL;
Trond Myklebust412c77c2007-07-03 16:10:55 -0400254
255 /* Ensure we revalidate the attributes and page cache! */
256 spin_lock(&inode->i_lock);
257 nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
258 spin_unlock(&inode->i_lock);
259
Trond Myklebust57bfa892008-01-25 16:38:18 -0500260out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 spin_unlock(&clp->cl_lock);
Trond Myklebust603c83d2007-10-18 19:59:20 -0400262 if (delegation != NULL)
263 nfs_free_delegation(delegation);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500264 if (freeme != NULL)
265 nfs_do_return_delegation(inode, freeme, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return status;
267}
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269/* Sync all data to disk upon delegation return */
270static void nfs_msync_inode(struct inode *inode)
271{
272 filemap_fdatawrite(inode->i_mapping);
273 nfs_wb_all(inode);
274 filemap_fdatawait(inode->i_mapping);
275}
276
277/*
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;
317 spin_lock(&clp->cl_lock);
318 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
319 spin_unlock(&clp->cl_lock);
320 rcu_read_unlock();
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500321 if (delegation != NULL) {
322 filemap_flush(inode->i_mapping);
323 err = __nfs_inode_return_delegation(inode, delegation, 0);
324 }
Trond Myklebust515d8612008-12-23 15:21:46 -0500325 iput(inode);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500326 if (!err)
327 goto restart;
328 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
329 return err;
Trond Myklebust515d8612008-12-23 15:21:46 -0500330 }
331 rcu_read_unlock();
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500332 return 0;
Trond Myklebust515d8612008-12-23 15:21:46 -0500333}
334
335/*
Trond Myklebuste6f81072008-01-24 18:14:34 -0500336 * This function returns the delegation without reclaiming opens
337 * or protecting against delegation reclaims.
338 * It is therefore really only safe to be called from
339 * nfs4_clear_inode()
340 */
341void nfs_inode_return_delegation_noreclaim(struct inode *inode)
342{
343 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
344 struct nfs_inode *nfsi = NFS_I(inode);
345 struct nfs_delegation *delegation;
346
347 if (rcu_dereference(nfsi->delegation) != NULL) {
348 spin_lock(&clp->cl_lock);
349 delegation = nfs_detach_delegation_locked(nfsi, NULL);
350 spin_unlock(&clp->cl_lock);
351 if (delegation != NULL)
352 nfs_do_return_delegation(inode, delegation, 0);
353 }
354}
355
Trond Myklebust90163022007-07-05 14:55:18 -0400356int nfs_inode_return_delegation(struct inode *inode)
357{
358 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
359 struct nfs_inode *nfsi = NFS_I(inode);
360 struct nfs_delegation *delegation;
361 int err = 0;
362
Trond Myklebust8383e462007-07-06 15:12:04 -0400363 if (rcu_dereference(nfsi->delegation) != NULL) {
Trond Myklebust90163022007-07-05 14:55:18 -0400364 spin_lock(&clp->cl_lock);
365 delegation = nfs_detach_delegation_locked(nfsi, NULL);
366 spin_unlock(&clp->cl_lock);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500367 if (delegation != NULL) {
368 nfs_msync_inode(inode);
369 err = __nfs_inode_return_delegation(inode, delegation, 1);
370 }
Trond Myklebust90163022007-07-05 14:55:18 -0400371 }
372 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373}
374
Trond Myklebust6411bd42008-12-23 15:21:51 -0500375static void nfs_mark_return_delegation(struct nfs_client *clp, struct nfs_delegation *delegation)
376{
377 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
378 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
379}
380
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381/*
382 * Return all delegations associated to a super block
383 */
Trond Myklebust515d8612008-12-23 15:21:46 -0500384void nfs_super_return_all_delegations(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
David Howells7539bba2006-08-22 20:06:09 -0400386 struct nfs_client *clp = NFS_SB(sb)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 struct nfs_delegation *delegation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
389 if (clp == NULL)
390 return;
Trond Myklebust8383e462007-07-06 15:12:04 -0400391 rcu_read_lock();
392 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Trond Myklebust86e89482008-12-23 15:21:39 -0500393 spin_lock(&delegation->lock);
394 if (delegation->inode != NULL && delegation->inode->i_sb == sb)
Trond Myklebust515d8612008-12-23 15:21:46 -0500395 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
Trond Myklebust86e89482008-12-23 15:21:39 -0500396 spin_unlock(&delegation->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400398 rcu_read_unlock();
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500399 if (nfs_client_return_marked_delegations(clp) != 0)
400 nfs4_schedule_state_manager(clp);
Trond Myklebust515d8612008-12-23 15:21:46 -0500401}
402
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500403static
404void nfs_client_mark_return_all_delegation_types(struct nfs_client *clp, fmode_t flags)
Trond Myklebust515d8612008-12-23 15:21:46 -0500405{
406 struct nfs_delegation *delegation;
407
408 rcu_read_lock();
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500409 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500410 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
411 continue;
412 if (delegation->type & flags)
413 nfs_mark_return_delegation(clp, delegation);
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500414 }
Trond Myklebust515d8612008-12-23 15:21:46 -0500415 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416}
417
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500418static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
419{
420 nfs_client_mark_return_all_delegation_types(clp, FMODE_READ|FMODE_WRITE);
421}
422
Trond Myklebustb0d3ded2008-12-23 15:21:50 -0500423static void nfs_delegation_run_state_manager(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +0100424{
Trond Myklebustb0d3ded2008-12-23 15:21:50 -0500425 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
426 nfs4_schedule_state_manager(clp);
Trond Myklebust58d97142006-01-03 09:55:24 +0100427}
428
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500429void nfs_expire_all_delegation_types(struct nfs_client *clp, fmode_t flags)
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500430{
431 nfs_client_mark_return_all_delegation_types(clp, flags);
432 nfs_delegation_run_state_manager(clp);
433}
434
David Howellsadfa6f92006-08-22 20:06:08 -0400435void nfs_expire_all_delegations(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +0100436{
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500437 nfs_expire_all_delegation_types(clp, FMODE_READ|FMODE_WRITE);
Trond Myklebust58d97142006-01-03 09:55:24 +0100438}
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440/*
441 * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
442 */
David Howellsadfa6f92006-08-22 20:06:08 -0400443void nfs_handle_cb_pathdown(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 if (clp == NULL)
446 return;
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500447 nfs_client_mark_return_all_delegations(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
Trond Myklebustb7391f42008-12-23 15:21:52 -0500450static void nfs_client_mark_return_unreferenced_delegations(struct nfs_client *clp)
451{
452 struct nfs_delegation *delegation;
453
454 rcu_read_lock();
455 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
456 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
457 continue;
Alexandros Batsakisb4a6f492009-12-05 13:19:11 -0500458 nfs_mark_return_delegation(clp, delegation);
Trond Myklebustb7391f42008-12-23 15:21:52 -0500459 }
460 rcu_read_unlock();
461}
462
463void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
464{
465 nfs_client_mark_return_unreferenced_delegations(clp);
466 nfs_delegation_run_state_manager(clp);
467}
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469/*
470 * Asynchronous delegation recall!
471 */
Alexandros Batsakis25976412009-12-05 13:48:55 -0500472int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid,
473 int (*validate_stateid)(struct nfs_delegation *delegation,
474 const nfs4_stateid *stateid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Trond Myklebust6411bd42008-12-23 15:21:51 -0500476 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
477 struct nfs_delegation *delegation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Trond Myklebust6411bd42008-12-23 15:21:51 -0500479 rcu_read_lock();
480 delegation = rcu_dereference(NFS_I(inode)->delegation);
Alexandros Batsakis25976412009-12-05 13:48:55 -0500481
482 if (!validate_stateid(delegation, stateid)) {
Trond Myklebust6411bd42008-12-23 15:21:51 -0500483 rcu_read_unlock();
484 return -ENOENT;
485 }
Alexandros Batsakis25976412009-12-05 13:48:55 -0500486
Trond Myklebust6411bd42008-12-23 15:21:51 -0500487 nfs_mark_return_delegation(clp, delegation);
488 rcu_read_unlock();
489 nfs_delegation_run_state_manager(clp);
490 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491}
492
493/*
494 * Retrieve the inode associated with a delegation
495 */
David Howellsadfa6f92006-08-22 20:06:08 -0400496struct inode *nfs_delegation_find_inode(struct nfs_client *clp, const struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
498 struct nfs_delegation *delegation;
499 struct inode *res = NULL;
Trond Myklebust8383e462007-07-06 15:12:04 -0400500 rcu_read_lock();
501 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Trond Myklebust86e89482008-12-23 15:21:39 -0500502 spin_lock(&delegation->lock);
503 if (delegation->inode != NULL &&
504 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 res = igrab(delegation->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 }
Trond Myklebust86e89482008-12-23 15:21:39 -0500507 spin_unlock(&delegation->lock);
508 if (res != NULL)
509 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400511 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 return res;
513}
514
515/*
516 * Mark all delegations as needing to be reclaimed
517 */
David Howellsadfa6f92006-08-22 20:06:08 -0400518void nfs_delegation_mark_reclaim(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
520 struct nfs_delegation *delegation;
Trond Myklebust8383e462007-07-06 15:12:04 -0400521 rcu_read_lock();
522 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list)
Trond Myklebust15c831b2008-12-23 15:21:39 -0500523 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
Trond Myklebust8383e462007-07-06 15:12:04 -0400524 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525}
526
527/*
528 * Reap all unclaimed delegations after reboot recovery is done
529 */
David Howellsadfa6f92006-08-22 20:06:08 -0400530void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Trond Myklebust8383e462007-07-06 15:12:04 -0400532 struct nfs_delegation *delegation;
Trond Myklebust86e89482008-12-23 15:21:39 -0500533 struct inode *inode;
Trond Myklebust8383e462007-07-06 15:12:04 -0400534restart:
535 rcu_read_lock();
536 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Trond Myklebust15c831b2008-12-23 15:21:39 -0500537 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 continue;
Trond Myklebust86e89482008-12-23 15:21:39 -0500539 inode = nfs_delegation_grab_inode(delegation);
540 if (inode == NULL)
541 continue;
Trond Myklebust8383e462007-07-06 15:12:04 -0400542 spin_lock(&clp->cl_lock);
Trond Myklebust86e89482008-12-23 15:21:39 -0500543 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
Trond Myklebust8383e462007-07-06 15:12:04 -0400544 spin_unlock(&clp->cl_lock);
545 rcu_read_unlock();
546 if (delegation != NULL)
Trond Myklebust905f8d12007-08-06 12:18:34 -0400547 nfs_free_delegation(delegation);
Trond Myklebust86e89482008-12-23 15:21:39 -0500548 iput(inode);
Trond Myklebust8383e462007-07-06 15:12:04 -0400549 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400551 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552}
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500553
554int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
555{
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500556 struct nfs_inode *nfsi = NFS_I(inode);
557 struct nfs_delegation *delegation;
Trond Myklebust8383e462007-07-06 15:12:04 -0400558 int ret = 0;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500559
Trond Myklebust8383e462007-07-06 15:12:04 -0400560 rcu_read_lock();
561 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500562 if (delegation != NULL) {
563 memcpy(dst->data, delegation->stateid.data, sizeof(dst->data));
Trond Myklebust8383e462007-07-06 15:12:04 -0400564 ret = 1;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500565 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400566 rcu_read_unlock();
567 return ret;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500568}