blob: 3016345439744d4ae13166a3eba9c80d86322e35 [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>
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{
David Howells17d2c0a2010-05-01 12:37:18 -040027 if (delegation->cred)
28 put_rpccred(delegation->cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -070029 kfree(delegation);
30}
31
Trond Myklebust8383e462007-07-06 15:12:04 -040032static void nfs_free_delegation_callback(struct rcu_head *head)
33{
34 struct nfs_delegation *delegation = container_of(head, struct nfs_delegation, rcu);
35
Trond Myklebust905f8d12007-08-06 12:18:34 -040036 nfs_do_free_delegation(delegation);
37}
38
39static void nfs_free_delegation(struct nfs_delegation *delegation)
40{
Trond Myklebust905f8d12007-08-06 12:18:34 -040041 call_rcu(&delegation->rcu, nfs_free_delegation_callback);
Trond Myklebust8383e462007-07-06 15:12:04 -040042}
43
Trond Myklebustb7391f42008-12-23 15:21:52 -050044void nfs_mark_delegation_referenced(struct nfs_delegation *delegation)
45{
46 set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags);
47}
48
Trond Myklebustbd7bf9d2008-12-23 15:21:53 -050049int nfs_have_delegation(struct inode *inode, fmode_t flags)
Trond Myklebustb7391f42008-12-23 15:21:52 -050050{
51 struct nfs_delegation *delegation;
52 int ret = 0;
53
54 flags &= FMODE_READ|FMODE_WRITE;
55 rcu_read_lock();
56 delegation = rcu_dereference(NFS_I(inode)->delegation);
57 if (delegation != NULL && (delegation->type & flags) == flags) {
58 nfs_mark_delegation_referenced(delegation);
59 ret = 1;
60 }
61 rcu_read_unlock();
62 return ret;
63}
64
Trond Myklebust888e6942005-11-04 15:38:11 -050065static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state)
66{
67 struct inode *inode = state->inode;
68 struct file_lock *fl;
Trond Myklebustd5122202009-06-17 13:22:58 -070069 int status = 0;
Trond Myklebust888e6942005-11-04 15:38:11 -050070
Trond Myklebust3f09df72009-06-17 13:23:00 -070071 if (inode->i_flock == NULL)
72 goto out;
73
74 /* Protect inode->i_flock using the BKL */
75 lock_kernel();
Harvey Harrison90dc7d22008-02-20 13:03:05 -080076 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
Trond Myklebust888e6942005-11-04 15:38:11 -050077 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
78 continue;
Trond Myklebustcd3758e2007-08-10 17:44:32 -040079 if (nfs_file_open_context(fl->fl_file) != ctx)
Trond Myklebust888e6942005-11-04 15:38:11 -050080 continue;
Trond Myklebust3f09df72009-06-17 13:23:00 -070081 unlock_kernel();
Trond Myklebust888e6942005-11-04 15:38:11 -050082 status = nfs4_lock_delegation_recall(state, fl);
Trond Myklebustd5122202009-06-17 13:22:58 -070083 if (status < 0)
Trond Myklebust3f09df72009-06-17 13:23:00 -070084 goto out;
85 lock_kernel();
Trond Myklebust888e6942005-11-04 15:38:11 -050086 }
Trond Myklebust3f09df72009-06-17 13:23:00 -070087 unlock_kernel();
88out:
Trond Myklebust888e6942005-11-04 15:38:11 -050089 return status;
90}
91
Trond Myklebustd18cc1f2009-12-03 08:10:17 -050092static int nfs_delegation_claim_opens(struct inode *inode, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
94 struct nfs_inode *nfsi = NFS_I(inode);
95 struct nfs_open_context *ctx;
96 struct nfs4_state *state;
Trond Myklebust888e6942005-11-04 15:38:11 -050097 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99again:
100 spin_lock(&inode->i_lock);
101 list_for_each_entry(ctx, &nfsi->open_files, list) {
102 state = ctx->state;
103 if (state == NULL)
104 continue;
105 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
106 continue;
Trond Myklebust90163022007-07-05 14:55:18 -0400107 if (memcmp(state->stateid.data, stateid->data, sizeof(state->stateid.data)) != 0)
108 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 get_nfs_open_context(ctx);
110 spin_unlock(&inode->i_lock);
Trond Myklebust13437e12007-07-06 15:10:43 -0400111 err = nfs4_open_delegation_recall(ctx, state, stateid);
Trond Myklebust888e6942005-11-04 15:38:11 -0500112 if (err >= 0)
113 err = nfs_delegation_claim_locks(ctx, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 put_nfs_open_context(ctx);
Trond Myklebust888e6942005-11-04 15:38:11 -0500115 if (err != 0)
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500116 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 goto again;
118 }
119 spin_unlock(&inode->i_lock);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500120 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
123/*
124 * Set up a delegation on an inode
125 */
126void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
127{
Trond Myklebust8f649c32010-05-01 12:36:18 -0400128 struct nfs_delegation *delegation;
129 struct rpc_cred *oldcred = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Trond Myklebust8f649c32010-05-01 12:36:18 -0400131 rcu_read_lock();
132 delegation = rcu_dereference(NFS_I(inode)->delegation);
133 if (delegation != NULL) {
134 spin_lock(&delegation->lock);
135 if (delegation->inode != NULL) {
136 memcpy(delegation->stateid.data, res->delegation.data,
137 sizeof(delegation->stateid.data));
138 delegation->type = res->delegation_type;
139 delegation->maxsize = res->maxsize;
140 oldcred = delegation->cred;
141 delegation->cred = get_rpccred(cred);
142 clear_bit(NFS_DELEGATION_NEED_RECLAIM,
143 &delegation->flags);
144 NFS_I(inode)->delegation_state = delegation->type;
145 spin_unlock(&delegation->lock);
146 put_rpccred(oldcred);
147 rcu_read_unlock();
148 } else {
149 /* We appear to have raced with a delegation return. */
150 spin_unlock(&delegation->lock);
151 rcu_read_unlock();
152 nfs_inode_set_delegation(inode, cred, res);
153 }
154 } else {
155 rcu_read_unlock();
156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
Trond Myklebust57bfa892008-01-25 16:38:18 -0500159static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
160{
161 int res = 0;
162
163 res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid, issync);
164 nfs_free_delegation(delegation);
165 return res;
166}
167
Trond Myklebust86e89482008-12-23 15:21:39 -0500168static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
169{
170 struct inode *inode = NULL;
171
172 spin_lock(&delegation->lock);
173 if (delegation->inode != NULL)
174 inode = igrab(delegation->inode);
175 spin_unlock(&delegation->lock);
176 return inode;
177}
178
David Howells17d2c0a2010-05-01 12:37:18 -0400179static struct nfs_delegation *nfs_detach_delegation_locked(struct nfs_inode *nfsi,
180 const nfs4_stateid *stateid,
181 struct nfs_client *clp)
Trond Myklebust57bfa892008-01-25 16:38:18 -0500182{
David Howells17d2c0a2010-05-01 12:37:18 -0400183 struct nfs_delegation *delegation =
184 rcu_dereference_protected(nfsi->delegation,
185 lockdep_is_held(&clp->cl_lock));
Trond Myklebust57bfa892008-01-25 16:38:18 -0500186
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);
David Howells17d2c0a2010-05-01 12:37:18 -0400212 struct nfs_delegation *delegation, *old_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
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400216 delegation = kmalloc(sizeof(*delegation), GFP_NOFS);
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);
David Howells17d2c0a2010-05-01 12:37:18 -0400230 old_delegation = rcu_dereference_protected(nfsi->delegation,
231 lockdep_is_held(&clp->cl_lock));
232 if (old_delegation != NULL) {
233 if (memcmp(&delegation->stateid, &old_delegation->stateid,
234 sizeof(old_delegation->stateid)) == 0 &&
235 delegation->type == old_delegation->type) {
Trond Myklebust57bfa892008-01-25 16:38:18 -0500236 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 }
Trond Myklebust57bfa892008-01-25 16:38:18 -0500238 /*
239 * Deal with broken servers that hand out two
240 * delegations for the same file.
241 */
242 dfprintk(FILE, "%s: server %s handed out "
243 "a duplicate delegation!\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700244 __func__, clp->cl_hostname);
David Howells17d2c0a2010-05-01 12:37:18 -0400245 if (delegation->type <= old_delegation->type) {
Trond Myklebust57bfa892008-01-25 16:38:18 -0500246 freeme = delegation;
247 delegation = NULL;
248 goto out;
249 }
David Howells17d2c0a2010-05-01 12:37:18 -0400250 freeme = nfs_detach_delegation_locked(nfsi, NULL, clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 }
Trond Myklebust57bfa892008-01-25 16:38:18 -0500252 list_add_rcu(&delegation->super_list, &clp->cl_delegations);
253 nfsi->delegation_state = delegation->type;
254 rcu_assign_pointer(nfsi->delegation, delegation);
255 delegation = NULL;
Trond Myklebust412c77c2007-07-03 16:10:55 -0400256
257 /* Ensure we revalidate the attributes and page cache! */
258 spin_lock(&inode->i_lock);
259 nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
260 spin_unlock(&inode->i_lock);
261
Trond Myklebust57bfa892008-01-25 16:38:18 -0500262out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 spin_unlock(&clp->cl_lock);
Trond Myklebust603c83d2007-10-18 19:59:20 -0400264 if (delegation != NULL)
265 nfs_free_delegation(delegation);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500266 if (freeme != NULL)
267 nfs_do_return_delegation(inode, freeme, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return status;
269}
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271/* Sync all data to disk upon delegation return */
272static void nfs_msync_inode(struct inode *inode)
273{
274 filemap_fdatawrite(inode->i_mapping);
275 nfs_wb_all(inode);
276 filemap_fdatawait(inode->i_mapping);
277}
278
279/*
280 * Basic procedure for returning a delegation to the server
281 */
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500282static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500285 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286
Trond Myklebust3f09df72009-06-17 13:23:00 -0700287 /*
288 * Guard against new delegated open/lock/unlock calls and against
289 * state recovery
290 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 down_write(&nfsi->rwsem);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500292 err = nfs_delegation_claim_opens(inode, &delegation->stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 up_write(&nfsi->rwsem);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500294 if (err)
295 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500297 err = nfs_do_return_delegation(inode, delegation, issync);
298out:
299 return err;
Trond Myklebust90163022007-07-05 14:55:18 -0400300}
301
Trond Myklebuste6f81072008-01-24 18:14:34 -0500302/*
Trond Myklebust515d8612008-12-23 15:21:46 -0500303 * Return all delegations that have been marked for return
304 */
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500305int nfs_client_return_marked_delegations(struct nfs_client *clp)
Trond Myklebust515d8612008-12-23 15:21:46 -0500306{
307 struct nfs_delegation *delegation;
308 struct inode *inode;
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500309 int err = 0;
Trond Myklebust515d8612008-12-23 15:21:46 -0500310
311restart:
312 rcu_read_lock();
313 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
314 if (!test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
315 continue;
316 inode = nfs_delegation_grab_inode(delegation);
317 if (inode == NULL)
318 continue;
319 spin_lock(&clp->cl_lock);
David Howells17d2c0a2010-05-01 12:37:18 -0400320 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL, clp);
Trond Myklebust515d8612008-12-23 15:21:46 -0500321 spin_unlock(&clp->cl_lock);
322 rcu_read_unlock();
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500323 if (delegation != NULL) {
324 filemap_flush(inode->i_mapping);
325 err = __nfs_inode_return_delegation(inode, delegation, 0);
326 }
Trond Myklebust515d8612008-12-23 15:21:46 -0500327 iput(inode);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500328 if (!err)
329 goto restart;
330 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
331 return err;
Trond Myklebust515d8612008-12-23 15:21:46 -0500332 }
333 rcu_read_unlock();
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500334 return 0;
Trond Myklebust515d8612008-12-23 15:21:46 -0500335}
336
337/*
Trond Myklebuste6f81072008-01-24 18:14:34 -0500338 * This function returns the delegation without reclaiming opens
339 * or protecting against delegation reclaims.
340 * It is therefore really only safe to be called from
341 * nfs4_clear_inode()
342 */
343void nfs_inode_return_delegation_noreclaim(struct inode *inode)
344{
345 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
346 struct nfs_inode *nfsi = NFS_I(inode);
347 struct nfs_delegation *delegation;
348
David Howells17d2c0a2010-05-01 12:37:18 -0400349 if (rcu_access_pointer(nfsi->delegation) != NULL) {
Trond Myklebuste6f81072008-01-24 18:14:34 -0500350 spin_lock(&clp->cl_lock);
David Howells17d2c0a2010-05-01 12:37:18 -0400351 delegation = nfs_detach_delegation_locked(nfsi, NULL, clp);
Trond Myklebuste6f81072008-01-24 18:14:34 -0500352 spin_unlock(&clp->cl_lock);
353 if (delegation != NULL)
354 nfs_do_return_delegation(inode, delegation, 0);
355 }
356}
357
Trond Myklebust90163022007-07-05 14:55:18 -0400358int nfs_inode_return_delegation(struct inode *inode)
359{
360 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
361 struct nfs_inode *nfsi = NFS_I(inode);
362 struct nfs_delegation *delegation;
363 int err = 0;
364
David Howells17d2c0a2010-05-01 12:37:18 -0400365 if (rcu_access_pointer(nfsi->delegation) != NULL) {
Trond Myklebust90163022007-07-05 14:55:18 -0400366 spin_lock(&clp->cl_lock);
David Howells17d2c0a2010-05-01 12:37:18 -0400367 delegation = nfs_detach_delegation_locked(nfsi, NULL, clp);
Trond Myklebust90163022007-07-05 14:55:18 -0400368 spin_unlock(&clp->cl_lock);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500369 if (delegation != NULL) {
370 nfs_msync_inode(inode);
371 err = __nfs_inode_return_delegation(inode, delegation, 1);
372 }
Trond Myklebust90163022007-07-05 14:55:18 -0400373 }
374 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
Trond Myklebust6411bd42008-12-23 15:21:51 -0500377static void nfs_mark_return_delegation(struct nfs_client *clp, struct nfs_delegation *delegation)
378{
379 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
380 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
381}
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383/*
384 * Return all delegations associated to a super block
385 */
Trond Myklebust515d8612008-12-23 15:21:46 -0500386void nfs_super_return_all_delegations(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387{
David Howells7539bba2006-08-22 20:06:09 -0400388 struct nfs_client *clp = NFS_SB(sb)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 struct nfs_delegation *delegation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
391 if (clp == NULL)
392 return;
Trond Myklebust8383e462007-07-06 15:12:04 -0400393 rcu_read_lock();
394 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Trond Myklebust86e89482008-12-23 15:21:39 -0500395 spin_lock(&delegation->lock);
396 if (delegation->inode != NULL && delegation->inode->i_sb == sb)
Trond Myklebust515d8612008-12-23 15:21:46 -0500397 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
Trond Myklebust86e89482008-12-23 15:21:39 -0500398 spin_unlock(&delegation->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400400 rcu_read_unlock();
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500401 if (nfs_client_return_marked_delegations(clp) != 0)
402 nfs4_schedule_state_manager(clp);
Trond Myklebust515d8612008-12-23 15:21:46 -0500403}
404
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500405static
406void nfs_client_mark_return_all_delegation_types(struct nfs_client *clp, fmode_t flags)
Trond Myklebust515d8612008-12-23 15:21:46 -0500407{
408 struct nfs_delegation *delegation;
409
410 rcu_read_lock();
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500411 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500412 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
413 continue;
414 if (delegation->type & flags)
415 nfs_mark_return_delegation(clp, delegation);
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500416 }
Trond Myklebust515d8612008-12-23 15:21:46 -0500417 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500420static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
421{
422 nfs_client_mark_return_all_delegation_types(clp, FMODE_READ|FMODE_WRITE);
423}
424
Trond Myklebustb0d3ded2008-12-23 15:21:50 -0500425static void nfs_delegation_run_state_manager(struct nfs_client *clp)
Trond Myklebust58d9714a2006-01-03 09:55:24 +0100426{
Trond Myklebustb0d3ded2008-12-23 15:21:50 -0500427 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
428 nfs4_schedule_state_manager(clp);
Trond Myklebust58d9714a2006-01-03 09:55:24 +0100429}
430
Alexandros Batsakis31f09602009-12-05 13:27:02 -0500431void nfs_expire_all_delegation_types(struct nfs_client *clp, fmode_t flags)
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500432{
433 nfs_client_mark_return_all_delegation_types(clp, flags);
434 nfs_delegation_run_state_manager(clp);
435}
436
David Howellsadfa6f92006-08-22 20:06:08 -0400437void nfs_expire_all_delegations(struct nfs_client *clp)
Trond Myklebust58d9714a2006-01-03 09:55:24 +0100438{
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500439 nfs_expire_all_delegation_types(clp, FMODE_READ|FMODE_WRITE);
Trond Myklebust58d9714a2006-01-03 09:55:24 +0100440}
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442/*
443 * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
444 */
David Howellsadfa6f92006-08-22 20:06:08 -0400445void nfs_handle_cb_pathdown(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 if (clp == NULL)
448 return;
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500449 nfs_client_mark_return_all_delegations(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450}
451
Trond Myklebustb7391f42008-12-23 15:21:52 -0500452static void nfs_client_mark_return_unreferenced_delegations(struct nfs_client *clp)
453{
454 struct nfs_delegation *delegation;
455
456 rcu_read_lock();
457 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
458 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
459 continue;
Alexandros Batsakisb4a6f492009-12-05 13:19:11 -0500460 nfs_mark_return_delegation(clp, delegation);
Trond Myklebustb7391f42008-12-23 15:21:52 -0500461 }
462 rcu_read_unlock();
463}
464
465void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
466{
467 nfs_client_mark_return_unreferenced_delegations(clp);
468 nfs_delegation_run_state_manager(clp);
469}
470
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471/*
472 * Asynchronous delegation recall!
473 */
Alexandros Batsakis25976412009-12-05 13:48:55 -0500474int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid,
475 int (*validate_stateid)(struct nfs_delegation *delegation,
476 const nfs4_stateid *stateid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Trond Myklebust6411bd42008-12-23 15:21:51 -0500478 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
479 struct nfs_delegation *delegation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Trond Myklebust6411bd42008-12-23 15:21:51 -0500481 rcu_read_lock();
482 delegation = rcu_dereference(NFS_I(inode)->delegation);
Alexandros Batsakis25976412009-12-05 13:48:55 -0500483
484 if (!validate_stateid(delegation, stateid)) {
Trond Myklebust6411bd42008-12-23 15:21:51 -0500485 rcu_read_unlock();
486 return -ENOENT;
487 }
Alexandros Batsakis25976412009-12-05 13:48:55 -0500488
Trond Myklebust6411bd42008-12-23 15:21:51 -0500489 nfs_mark_return_delegation(clp, delegation);
490 rcu_read_unlock();
491 nfs_delegation_run_state_manager(clp);
492 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493}
494
495/*
496 * Retrieve the inode associated with a delegation
497 */
David Howellsadfa6f92006-08-22 20:06:08 -0400498struct inode *nfs_delegation_find_inode(struct nfs_client *clp, const struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499{
500 struct nfs_delegation *delegation;
501 struct inode *res = NULL;
Trond Myklebust8383e462007-07-06 15:12:04 -0400502 rcu_read_lock();
503 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Trond Myklebust86e89482008-12-23 15:21:39 -0500504 spin_lock(&delegation->lock);
505 if (delegation->inode != NULL &&
506 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 res = igrab(delegation->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
Trond Myklebust86e89482008-12-23 15:21:39 -0500509 spin_unlock(&delegation->lock);
510 if (res != NULL)
511 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400513 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 return res;
515}
516
517/*
518 * Mark all delegations as needing to be reclaimed
519 */
David Howellsadfa6f92006-08-22 20:06:08 -0400520void nfs_delegation_mark_reclaim(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
522 struct nfs_delegation *delegation;
Trond Myklebust8383e462007-07-06 15:12:04 -0400523 rcu_read_lock();
524 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list)
Trond Myklebust15c831b2008-12-23 15:21:39 -0500525 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
Trond Myklebust8383e462007-07-06 15:12:04 -0400526 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
529/*
530 * Reap all unclaimed delegations after reboot recovery is done
531 */
David Howellsadfa6f92006-08-22 20:06:08 -0400532void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
Trond Myklebust8383e462007-07-06 15:12:04 -0400534 struct nfs_delegation *delegation;
Trond Myklebust86e89482008-12-23 15:21:39 -0500535 struct inode *inode;
Trond Myklebust8383e462007-07-06 15:12:04 -0400536restart:
537 rcu_read_lock();
538 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Trond Myklebust15c831b2008-12-23 15:21:39 -0500539 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 continue;
Trond Myklebust86e89482008-12-23 15:21:39 -0500541 inode = nfs_delegation_grab_inode(delegation);
542 if (inode == NULL)
543 continue;
Trond Myklebust8383e462007-07-06 15:12:04 -0400544 spin_lock(&clp->cl_lock);
David Howells17d2c0a2010-05-01 12:37:18 -0400545 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL, clp);
Trond Myklebust8383e462007-07-06 15:12:04 -0400546 spin_unlock(&clp->cl_lock);
547 rcu_read_unlock();
548 if (delegation != NULL)
Trond Myklebust905f8d12007-08-06 12:18:34 -0400549 nfs_free_delegation(delegation);
Trond Myklebust86e89482008-12-23 15:21:39 -0500550 iput(inode);
Trond Myklebust8383e462007-07-06 15:12:04 -0400551 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400553 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554}
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500555
556int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
557{
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500558 struct nfs_inode *nfsi = NFS_I(inode);
559 struct nfs_delegation *delegation;
Trond Myklebust8383e462007-07-06 15:12:04 -0400560 int ret = 0;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500561
Trond Myklebust8383e462007-07-06 15:12:04 -0400562 rcu_read_lock();
563 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500564 if (delegation != NULL) {
565 memcpy(dst->data, delegation->stateid.data, sizeof(dst->data));
Trond Myklebust8383e462007-07-06 15:12:04 -0400566 ret = 1;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500567 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400568 rcu_read_unlock();
569 return ret;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500570}