blob: eeecd69c130cd5007e230254ba390517fae738aa [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>
Alexey Dobriyan405f5572009-07-11 22:08:37 +040013#include <linux/smp_lock.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{
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 kfree(delegation);
27}
28
Trond Myklebust8383e462007-07-06 15:12:04 -040029static void nfs_free_delegation_callback(struct rcu_head *head)
30{
31 struct nfs_delegation *delegation = container_of(head, struct nfs_delegation, rcu);
32
Trond Myklebust905f8d12007-08-06 12:18:34 -040033 nfs_do_free_delegation(delegation);
34}
35
36static void nfs_free_delegation(struct nfs_delegation *delegation)
37{
38 struct rpc_cred *cred;
39
40 cred = rcu_dereference(delegation->cred);
41 rcu_assign_pointer(delegation->cred, NULL);
42 call_rcu(&delegation->rcu, nfs_free_delegation_callback);
43 if (cred)
44 put_rpccred(cred);
Trond Myklebust8383e462007-07-06 15:12:04 -040045}
46
Trond Myklebustb7391f42008-12-23 15:21:52 -050047void nfs_mark_delegation_referenced(struct nfs_delegation *delegation)
48{
49 set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags);
50}
51
Trond Myklebustbd7bf9d2008-12-23 15:21:53 -050052int nfs_have_delegation(struct inode *inode, fmode_t flags)
Trond Myklebustb7391f42008-12-23 15:21:52 -050053{
54 struct nfs_delegation *delegation;
55 int ret = 0;
56
57 flags &= FMODE_READ|FMODE_WRITE;
58 rcu_read_lock();
59 delegation = rcu_dereference(NFS_I(inode)->delegation);
60 if (delegation != NULL && (delegation->type & flags) == flags) {
61 nfs_mark_delegation_referenced(delegation);
62 ret = 1;
63 }
64 rcu_read_unlock();
65 return ret;
66}
67
Trond Myklebust888e6942005-11-04 15:38:11 -050068static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state)
69{
70 struct inode *inode = state->inode;
71 struct file_lock *fl;
Trond Myklebustd5122202009-06-17 13:22:58 -070072 int status = 0;
Trond Myklebust888e6942005-11-04 15:38:11 -050073
Trond Myklebust3f09df72009-06-17 13:23:00 -070074 if (inode->i_flock == NULL)
75 goto out;
76
77 /* Protect inode->i_flock using the BKL */
78 lock_kernel();
Harvey Harrison90dc7d22008-02-20 13:03:05 -080079 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
Trond Myklebust888e6942005-11-04 15:38:11 -050080 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
81 continue;
Trond Myklebustcd3758e2007-08-10 17:44:32 -040082 if (nfs_file_open_context(fl->fl_file) != ctx)
Trond Myklebust888e6942005-11-04 15:38:11 -050083 continue;
Trond Myklebust3f09df72009-06-17 13:23:00 -070084 unlock_kernel();
Trond Myklebust888e6942005-11-04 15:38:11 -050085 status = nfs4_lock_delegation_recall(state, fl);
Trond Myklebustd5122202009-06-17 13:22:58 -070086 if (status < 0)
Trond Myklebust3f09df72009-06-17 13:23:00 -070087 goto out;
88 lock_kernel();
Trond Myklebust888e6942005-11-04 15:38:11 -050089 }
Trond Myklebust3f09df72009-06-17 13:23:00 -070090 unlock_kernel();
91out:
Trond Myklebust888e6942005-11-04 15:38:11 -050092 return status;
93}
94
Trond Myklebustd18cc1f2009-12-03 08:10:17 -050095static int nfs_delegation_claim_opens(struct inode *inode, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
97 struct nfs_inode *nfsi = NFS_I(inode);
98 struct nfs_open_context *ctx;
99 struct nfs4_state *state;
Trond Myklebust888e6942005-11-04 15:38:11 -0500100 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102again:
103 spin_lock(&inode->i_lock);
104 list_for_each_entry(ctx, &nfsi->open_files, list) {
105 state = ctx->state;
106 if (state == NULL)
107 continue;
108 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
109 continue;
Trond Myklebust90163022007-07-05 14:55:18 -0400110 if (memcmp(state->stateid.data, stateid->data, sizeof(state->stateid.data)) != 0)
111 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 get_nfs_open_context(ctx);
113 spin_unlock(&inode->i_lock);
Trond Myklebust13437e12007-07-06 15:10:43 -0400114 err = nfs4_open_delegation_recall(ctx, state, stateid);
Trond Myklebust888e6942005-11-04 15:38:11 -0500115 if (err >= 0)
116 err = nfs_delegation_claim_locks(ctx, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 put_nfs_open_context(ctx);
Trond Myklebust888e6942005-11-04 15:38:11 -0500118 if (err != 0)
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500119 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 goto again;
121 }
122 spin_unlock(&inode->i_lock);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500123 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
126/*
127 * Set up a delegation on an inode
128 */
129void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
130{
131 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
Trond Myklebust05c88ba2007-10-11 15:11:51 -0400132 struct rpc_cred *oldcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
134 if (delegation == NULL)
135 return;
136 memcpy(delegation->stateid.data, res->delegation.data,
137 sizeof(delegation->stateid.data));
138 delegation->type = res->delegation_type;
139 delegation->maxsize = res->maxsize;
Trond Myklebust05c88ba2007-10-11 15:11:51 -0400140 oldcred = delegation->cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 delegation->cred = get_rpccred(cred);
Trond Myklebust15c831b2008-12-23 15:21:39 -0500142 clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 NFS_I(inode)->delegation_state = delegation->type;
144 smp_wmb();
Trond Myklebust05c88ba2007-10-11 15:11:51 -0400145 put_rpccred(oldcred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
Trond Myklebust57bfa892008-01-25 16:38:18 -0500148static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
149{
150 int res = 0;
151
152 res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid, issync);
153 nfs_free_delegation(delegation);
154 return res;
155}
156
Trond Myklebust86e89482008-12-23 15:21:39 -0500157static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
158{
159 struct inode *inode = NULL;
160
161 spin_lock(&delegation->lock);
162 if (delegation->inode != NULL)
163 inode = igrab(delegation->inode);
164 spin_unlock(&delegation->lock);
165 return inode;
166}
167
Trond Myklebust57bfa892008-01-25 16:38:18 -0500168static struct nfs_delegation *nfs_detach_delegation_locked(struct nfs_inode *nfsi, const nfs4_stateid *stateid)
169{
170 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
171
172 if (delegation == NULL)
173 goto nomatch;
Trond Myklebust34310432008-12-23 15:21:38 -0500174 spin_lock(&delegation->lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500175 if (stateid != NULL && memcmp(delegation->stateid.data, stateid->data,
176 sizeof(delegation->stateid.data)) != 0)
Trond Myklebust34310432008-12-23 15:21:38 -0500177 goto nomatch_unlock;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500178 list_del_rcu(&delegation->super_list);
Trond Myklebust86e89482008-12-23 15:21:39 -0500179 delegation->inode = NULL;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500180 nfsi->delegation_state = 0;
181 rcu_assign_pointer(nfsi->delegation, NULL);
Trond Myklebust34310432008-12-23 15:21:38 -0500182 spin_unlock(&delegation->lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500183 return delegation;
Trond Myklebust34310432008-12-23 15:21:38 -0500184nomatch_unlock:
185 spin_unlock(&delegation->lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500186nomatch:
187 return NULL;
188}
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190/*
191 * Set up a delegation on an inode
192 */
193int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
194{
David Howells7539bba2006-08-22 20:06:09 -0400195 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 struct nfs_inode *nfsi = NFS_I(inode);
197 struct nfs_delegation *delegation;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500198 struct nfs_delegation *freeme = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 int status = 0;
200
Panagiotis Issarisf52720c2006-09-27 01:49:39 -0700201 delegation = kmalloc(sizeof(*delegation), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (delegation == NULL)
203 return -ENOMEM;
204 memcpy(delegation->stateid.data, res->delegation.data,
205 sizeof(delegation->stateid.data));
206 delegation->type = res->delegation_type;
207 delegation->maxsize = res->maxsize;
Trond Myklebustbeb2a5e2006-01-03 09:55:37 +0100208 delegation->change_attr = nfsi->change_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 delegation->cred = get_rpccred(cred);
210 delegation->inode = inode;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500211 delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
Trond Myklebust34310432008-12-23 15:21:38 -0500212 spin_lock_init(&delegation->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
214 spin_lock(&clp->cl_lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500215 if (rcu_dereference(nfsi->delegation) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 if (memcmp(&delegation->stateid, &nfsi->delegation->stateid,
Trond Myklebust57bfa892008-01-25 16:38:18 -0500217 sizeof(delegation->stateid)) == 0 &&
218 delegation->type == nfsi->delegation->type) {
219 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 }
Trond Myklebust57bfa892008-01-25 16:38:18 -0500221 /*
222 * Deal with broken servers that hand out two
223 * delegations for the same file.
224 */
225 dfprintk(FILE, "%s: server %s handed out "
226 "a duplicate delegation!\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700227 __func__, clp->cl_hostname);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500228 if (delegation->type <= nfsi->delegation->type) {
229 freeme = delegation;
230 delegation = NULL;
231 goto out;
232 }
233 freeme = nfs_detach_delegation_locked(nfsi, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 }
Trond Myklebust57bfa892008-01-25 16:38:18 -0500235 list_add_rcu(&delegation->super_list, &clp->cl_delegations);
236 nfsi->delegation_state = delegation->type;
237 rcu_assign_pointer(nfsi->delegation, delegation);
238 delegation = NULL;
Trond Myklebust412c77c2007-07-03 16:10:55 -0400239
240 /* Ensure we revalidate the attributes and page cache! */
241 spin_lock(&inode->i_lock);
242 nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
243 spin_unlock(&inode->i_lock);
244
Trond Myklebust57bfa892008-01-25 16:38:18 -0500245out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 spin_unlock(&clp->cl_lock);
Trond Myklebust603c83d2007-10-18 19:59:20 -0400247 if (delegation != NULL)
248 nfs_free_delegation(delegation);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500249 if (freeme != NULL)
250 nfs_do_return_delegation(inode, freeme, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return status;
252}
253
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254/* Sync all data to disk upon delegation return */
255static void nfs_msync_inode(struct inode *inode)
256{
257 filemap_fdatawrite(inode->i_mapping);
258 nfs_wb_all(inode);
259 filemap_fdatawait(inode->i_mapping);
260}
261
262/*
263 * Basic procedure for returning a delegation to the server
264 */
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500265static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500268 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Trond Myklebust3f09df72009-06-17 13:23:00 -0700270 /*
271 * Guard against new delegated open/lock/unlock calls and against
272 * state recovery
273 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 down_write(&nfsi->rwsem);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500275 err = nfs_delegation_claim_opens(inode, &delegation->stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 up_write(&nfsi->rwsem);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500277 if (err)
278 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500280 err = nfs_do_return_delegation(inode, delegation, issync);
281out:
282 return err;
Trond Myklebust90163022007-07-05 14:55:18 -0400283}
284
Trond Myklebuste6f81072008-01-24 18:14:34 -0500285/*
Trond Myklebust515d8612008-12-23 15:21:46 -0500286 * Return all delegations that have been marked for return
287 */
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500288int nfs_client_return_marked_delegations(struct nfs_client *clp)
Trond Myklebust515d8612008-12-23 15:21:46 -0500289{
290 struct nfs_delegation *delegation;
291 struct inode *inode;
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500292 int err = 0;
Trond Myklebust515d8612008-12-23 15:21:46 -0500293
294restart:
295 rcu_read_lock();
296 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
297 if (!test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
298 continue;
299 inode = nfs_delegation_grab_inode(delegation);
300 if (inode == NULL)
301 continue;
302 spin_lock(&clp->cl_lock);
303 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
304 spin_unlock(&clp->cl_lock);
305 rcu_read_unlock();
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500306 if (delegation != NULL) {
307 filemap_flush(inode->i_mapping);
308 err = __nfs_inode_return_delegation(inode, delegation, 0);
309 }
Trond Myklebust515d8612008-12-23 15:21:46 -0500310 iput(inode);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500311 if (!err)
312 goto restart;
313 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
314 return err;
Trond Myklebust515d8612008-12-23 15:21:46 -0500315 }
316 rcu_read_unlock();
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500317 return 0;
Trond Myklebust515d8612008-12-23 15:21:46 -0500318}
319
320/*
Trond Myklebuste6f81072008-01-24 18:14:34 -0500321 * This function returns the delegation without reclaiming opens
322 * or protecting against delegation reclaims.
323 * It is therefore really only safe to be called from
324 * nfs4_clear_inode()
325 */
326void nfs_inode_return_delegation_noreclaim(struct inode *inode)
327{
328 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
329 struct nfs_inode *nfsi = NFS_I(inode);
330 struct nfs_delegation *delegation;
331
332 if (rcu_dereference(nfsi->delegation) != NULL) {
333 spin_lock(&clp->cl_lock);
334 delegation = nfs_detach_delegation_locked(nfsi, NULL);
335 spin_unlock(&clp->cl_lock);
336 if (delegation != NULL)
337 nfs_do_return_delegation(inode, delegation, 0);
338 }
339}
340
Trond Myklebust90163022007-07-05 14:55:18 -0400341int nfs_inode_return_delegation(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 int err = 0;
347
Trond Myklebust8383e462007-07-06 15:12:04 -0400348 if (rcu_dereference(nfsi->delegation) != NULL) {
Trond Myklebust90163022007-07-05 14:55:18 -0400349 spin_lock(&clp->cl_lock);
350 delegation = nfs_detach_delegation_locked(nfsi, NULL);
351 spin_unlock(&clp->cl_lock);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500352 if (delegation != NULL) {
353 nfs_msync_inode(inode);
354 err = __nfs_inode_return_delegation(inode, delegation, 1);
355 }
Trond Myklebust90163022007-07-05 14:55:18 -0400356 }
357 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358}
359
Trond Myklebust6411bd42008-12-23 15:21:51 -0500360static void nfs_mark_return_delegation(struct nfs_client *clp, struct nfs_delegation *delegation)
361{
362 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
363 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
364}
365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366/*
367 * Return all delegations associated to a super block
368 */
Trond Myklebust515d8612008-12-23 15:21:46 -0500369void nfs_super_return_all_delegations(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
David Howells7539bba2006-08-22 20:06:09 -0400371 struct nfs_client *clp = NFS_SB(sb)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 struct nfs_delegation *delegation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373
374 if (clp == NULL)
375 return;
Trond Myklebust8383e462007-07-06 15:12:04 -0400376 rcu_read_lock();
377 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Trond Myklebust86e89482008-12-23 15:21:39 -0500378 spin_lock(&delegation->lock);
379 if (delegation->inode != NULL && delegation->inode->i_sb == sb)
Trond Myklebust515d8612008-12-23 15:21:46 -0500380 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
Trond Myklebust86e89482008-12-23 15:21:39 -0500381 spin_unlock(&delegation->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400383 rcu_read_unlock();
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500384 if (nfs_client_return_marked_delegations(clp) != 0)
385 nfs4_schedule_state_manager(clp);
Trond Myklebust515d8612008-12-23 15:21:46 -0500386}
387
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500388static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
Trond Myklebust515d8612008-12-23 15:21:46 -0500389{
390 struct nfs_delegation *delegation;
391
392 rcu_read_lock();
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500393 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Trond Myklebust515d8612008-12-23 15:21:46 -0500394 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500395 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
396 }
Trond Myklebust515d8612008-12-23 15:21:46 -0500397 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398}
399
Trond Myklebustb0d3ded2008-12-23 15:21:50 -0500400static void nfs_delegation_run_state_manager(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +0100401{
Trond Myklebustb0d3ded2008-12-23 15:21:50 -0500402 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
403 nfs4_schedule_state_manager(clp);
Trond Myklebust58d97142006-01-03 09:55:24 +0100404}
405
David Howellsadfa6f92006-08-22 20:06:08 -0400406void nfs_expire_all_delegations(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +0100407{
Trond Myklebustb0d3ded2008-12-23 15:21:50 -0500408 nfs_client_mark_return_all_delegations(clp);
409 nfs_delegation_run_state_manager(clp);
Trond Myklebust58d97142006-01-03 09:55:24 +0100410}
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412/*
413 * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
414 */
David Howellsadfa6f92006-08-22 20:06:08 -0400415void nfs_handle_cb_pathdown(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (clp == NULL)
418 return;
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500419 nfs_client_mark_return_all_delegations(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
Trond Myklebustb7391f42008-12-23 15:21:52 -0500422static void nfs_client_mark_return_unreferenced_delegations(struct nfs_client *clp)
423{
424 struct nfs_delegation *delegation;
425
426 rcu_read_lock();
427 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
428 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
429 continue;
430 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
431 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
432 }
433 rcu_read_unlock();
434}
435
436void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
437{
438 nfs_client_mark_return_unreferenced_delegations(clp);
439 nfs_delegation_run_state_manager(clp);
440}
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442/*
443 * Asynchronous delegation recall!
444 */
445int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid)
446{
Trond Myklebust6411bd42008-12-23 15:21:51 -0500447 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
448 struct nfs_delegation *delegation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Trond Myklebust6411bd42008-12-23 15:21:51 -0500450 rcu_read_lock();
451 delegation = rcu_dereference(NFS_I(inode)->delegation);
452 if (delegation == NULL || memcmp(delegation->stateid.data, stateid->data,
453 sizeof(delegation->stateid.data)) != 0) {
454 rcu_read_unlock();
455 return -ENOENT;
456 }
457 nfs_mark_return_delegation(clp, delegation);
458 rcu_read_unlock();
459 nfs_delegation_run_state_manager(clp);
460 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461}
462
463/*
464 * Retrieve the inode associated with a delegation
465 */
David Howellsadfa6f92006-08-22 20:06:08 -0400466struct inode *nfs_delegation_find_inode(struct nfs_client *clp, const struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
468 struct nfs_delegation *delegation;
469 struct inode *res = NULL;
Trond Myklebust8383e462007-07-06 15:12:04 -0400470 rcu_read_lock();
471 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Trond Myklebust86e89482008-12-23 15:21:39 -0500472 spin_lock(&delegation->lock);
473 if (delegation->inode != NULL &&
474 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 res = igrab(delegation->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 }
Trond Myklebust86e89482008-12-23 15:21:39 -0500477 spin_unlock(&delegation->lock);
478 if (res != NULL)
479 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400481 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return res;
483}
484
485/*
486 * Mark all delegations as needing to be reclaimed
487 */
David Howellsadfa6f92006-08-22 20:06:08 -0400488void nfs_delegation_mark_reclaim(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489{
490 struct nfs_delegation *delegation;
Trond Myklebust8383e462007-07-06 15:12:04 -0400491 rcu_read_lock();
492 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list)
Trond Myklebust15c831b2008-12-23 15:21:39 -0500493 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
Trond Myklebust8383e462007-07-06 15:12:04 -0400494 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
497/*
498 * Reap all unclaimed delegations after reboot recovery is done
499 */
David Howellsadfa6f92006-08-22 20:06:08 -0400500void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501{
Trond Myklebust8383e462007-07-06 15:12:04 -0400502 struct nfs_delegation *delegation;
Trond Myklebust86e89482008-12-23 15:21:39 -0500503 struct inode *inode;
Trond Myklebust8383e462007-07-06 15:12:04 -0400504restart:
505 rcu_read_lock();
506 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Trond Myklebust15c831b2008-12-23 15:21:39 -0500507 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 continue;
Trond Myklebust86e89482008-12-23 15:21:39 -0500509 inode = nfs_delegation_grab_inode(delegation);
510 if (inode == NULL)
511 continue;
Trond Myklebust8383e462007-07-06 15:12:04 -0400512 spin_lock(&clp->cl_lock);
Trond Myklebust86e89482008-12-23 15:21:39 -0500513 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
Trond Myklebust8383e462007-07-06 15:12:04 -0400514 spin_unlock(&clp->cl_lock);
515 rcu_read_unlock();
516 if (delegation != NULL)
Trond Myklebust905f8d12007-08-06 12:18:34 -0400517 nfs_free_delegation(delegation);
Trond Myklebust86e89482008-12-23 15:21:39 -0500518 iput(inode);
Trond Myklebust8383e462007-07-06 15:12:04 -0400519 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400521 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522}
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500523
524int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
525{
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500526 struct nfs_inode *nfsi = NFS_I(inode);
527 struct nfs_delegation *delegation;
Trond Myklebust8383e462007-07-06 15:12:04 -0400528 int ret = 0;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500529
Trond Myklebust8383e462007-07-06 15:12:04 -0400530 rcu_read_lock();
531 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500532 if (delegation != NULL) {
533 memcpy(dst->data, delegation->stateid.data, sizeof(dst->data));
Trond Myklebust8383e462007-07-06 15:12:04 -0400534 ret = 1;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500535 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400536 rcu_read_unlock();
537 return ret;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500538}