blob: af05b918cb5bcce6655aee1d16adaf9000fe9d25 [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>
13#include <linux/spinlock.h>
14
15#include <linux/nfs4.h>
16#include <linux/nfs_fs.h>
17#include <linux/nfs_xdr.h>
18
Trond Myklebust4ce79712005-06-22 17:16:21 +000019#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include "delegation.h"
David Howells24c8dbb2006-08-22 20:06:10 -040021#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Trond Myklebust905f8d12007-08-06 12:18:34 -040023static void nfs_do_free_delegation(struct nfs_delegation *delegation)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024{
Linus Torvalds1da177e2005-04-16 15:20:36 -070025 kfree(delegation);
26}
27
Trond Myklebust8383e462007-07-06 15:12:04 -040028static void nfs_free_delegation_callback(struct rcu_head *head)
29{
30 struct nfs_delegation *delegation = container_of(head, struct nfs_delegation, rcu);
31
Trond Myklebust905f8d12007-08-06 12:18:34 -040032 nfs_do_free_delegation(delegation);
33}
34
35static void nfs_free_delegation(struct nfs_delegation *delegation)
36{
37 struct rpc_cred *cred;
38
39 cred = rcu_dereference(delegation->cred);
40 rcu_assign_pointer(delegation->cred, NULL);
41 call_rcu(&delegation->rcu, nfs_free_delegation_callback);
42 if (cred)
43 put_rpccred(cred);
Trond Myklebust8383e462007-07-06 15:12:04 -040044}
45
Trond Myklebustb7391f42008-12-23 15:21:52 -050046void nfs_mark_delegation_referenced(struct nfs_delegation *delegation)
47{
48 set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags);
49}
50
Trond Myklebustbd7bf9d2008-12-23 15:21:53 -050051int nfs_have_delegation(struct inode *inode, fmode_t flags)
Trond Myklebustb7391f42008-12-23 15:21:52 -050052{
53 struct nfs_delegation *delegation;
54 int ret = 0;
55
56 flags &= FMODE_READ|FMODE_WRITE;
57 rcu_read_lock();
58 delegation = rcu_dereference(NFS_I(inode)->delegation);
59 if (delegation != NULL && (delegation->type & flags) == flags) {
60 nfs_mark_delegation_referenced(delegation);
61 ret = 1;
62 }
63 rcu_read_unlock();
64 return ret;
65}
66
Trond Myklebust888e6942005-11-04 15:38:11 -050067static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state)
68{
69 struct inode *inode = state->inode;
70 struct file_lock *fl;
Trond Myklebustd5122202009-06-17 13:22:58 -070071 int status = 0;
Trond Myklebust888e6942005-11-04 15:38:11 -050072
Trond Myklebust3f09df72009-06-17 13:23:00 -070073 if (inode->i_flock == NULL)
74 goto out;
75
76 /* Protect inode->i_flock using the BKL */
77 lock_kernel();
Harvey Harrison90dc7d22008-02-20 13:03:05 -080078 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
Trond Myklebust888e6942005-11-04 15:38:11 -050079 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
80 continue;
Trond Myklebustcd3758e2007-08-10 17:44:32 -040081 if (nfs_file_open_context(fl->fl_file) != ctx)
Trond Myklebust888e6942005-11-04 15:38:11 -050082 continue;
Trond Myklebust3f09df72009-06-17 13:23:00 -070083 unlock_kernel();
Trond Myklebust888e6942005-11-04 15:38:11 -050084 status = nfs4_lock_delegation_recall(state, fl);
Trond Myklebustd5122202009-06-17 13:22:58 -070085 if (status < 0)
Trond Myklebust3f09df72009-06-17 13:23:00 -070086 goto out;
87 lock_kernel();
Trond Myklebust888e6942005-11-04 15:38:11 -050088 }
Trond Myklebust3f09df72009-06-17 13:23:00 -070089 unlock_kernel();
90out:
Trond Myklebust888e6942005-11-04 15:38:11 -050091 return status;
92}
93
Trond Myklebust90163022007-07-05 14:55:18 -040094static void nfs_delegation_claim_opens(struct inode *inode, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 struct nfs_inode *nfsi = NFS_I(inode);
97 struct nfs_open_context *ctx;
98 struct nfs4_state *state;
Trond Myklebust888e6942005-11-04 15:38:11 -050099 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
101again:
102 spin_lock(&inode->i_lock);
103 list_for_each_entry(ctx, &nfsi->open_files, list) {
104 state = ctx->state;
105 if (state == NULL)
106 continue;
107 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
108 continue;
Trond Myklebust90163022007-07-05 14:55:18 -0400109 if (memcmp(state->stateid.data, stateid->data, sizeof(state->stateid.data)) != 0)
110 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 get_nfs_open_context(ctx);
112 spin_unlock(&inode->i_lock);
Trond Myklebust13437e12007-07-06 15:10:43 -0400113 err = nfs4_open_delegation_recall(ctx, state, stateid);
Trond Myklebust888e6942005-11-04 15:38:11 -0500114 if (err >= 0)
115 err = nfs_delegation_claim_locks(ctx, state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 put_nfs_open_context(ctx);
Trond Myklebust888e6942005-11-04 15:38:11 -0500117 if (err != 0)
118 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 goto again;
120 }
121 spin_unlock(&inode->i_lock);
122}
123
124/*
125 * Set up a delegation on an inode
126 */
127void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
128{
129 struct nfs_delegation *delegation = NFS_I(inode)->delegation;
Trond Myklebust05c88ba2007-10-11 15:11:51 -0400130 struct rpc_cred *oldcred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 if (delegation == NULL)
133 return;
134 memcpy(delegation->stateid.data, res->delegation.data,
135 sizeof(delegation->stateid.data));
136 delegation->type = res->delegation_type;
137 delegation->maxsize = res->maxsize;
Trond Myklebust05c88ba2007-10-11 15:11:51 -0400138 oldcred = delegation->cred;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 delegation->cred = get_rpccred(cred);
Trond Myklebust15c831b2008-12-23 15:21:39 -0500140 clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 NFS_I(inode)->delegation_state = delegation->type;
142 smp_wmb();
Trond Myklebust05c88ba2007-10-11 15:11:51 -0400143 put_rpccred(oldcred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
Trond Myklebust57bfa892008-01-25 16:38:18 -0500146static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
147{
148 int res = 0;
149
150 res = nfs4_proc_delegreturn(inode, delegation->cred, &delegation->stateid, issync);
151 nfs_free_delegation(delegation);
152 return res;
153}
154
Trond Myklebust86e89482008-12-23 15:21:39 -0500155static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
156{
157 struct inode *inode = NULL;
158
159 spin_lock(&delegation->lock);
160 if (delegation->inode != NULL)
161 inode = igrab(delegation->inode);
162 spin_unlock(&delegation->lock);
163 return inode;
164}
165
Trond Myklebust57bfa892008-01-25 16:38:18 -0500166static struct nfs_delegation *nfs_detach_delegation_locked(struct nfs_inode *nfsi, const nfs4_stateid *stateid)
167{
168 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
169
170 if (delegation == NULL)
171 goto nomatch;
Trond Myklebust34310432008-12-23 15:21:38 -0500172 spin_lock(&delegation->lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500173 if (stateid != NULL && memcmp(delegation->stateid.data, stateid->data,
174 sizeof(delegation->stateid.data)) != 0)
Trond Myklebust34310432008-12-23 15:21:38 -0500175 goto nomatch_unlock;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500176 list_del_rcu(&delegation->super_list);
Trond Myklebust86e89482008-12-23 15:21:39 -0500177 delegation->inode = NULL;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500178 nfsi->delegation_state = 0;
179 rcu_assign_pointer(nfsi->delegation, NULL);
Trond Myklebust34310432008-12-23 15:21:38 -0500180 spin_unlock(&delegation->lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500181 return delegation;
Trond Myklebust34310432008-12-23 15:21:38 -0500182nomatch_unlock:
183 spin_unlock(&delegation->lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500184nomatch:
185 return NULL;
186}
187
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188/*
189 * Set up a delegation on an inode
190 */
191int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
192{
David Howells7539bba2006-08-22 20:06:09 -0400193 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 struct nfs_inode *nfsi = NFS_I(inode);
195 struct nfs_delegation *delegation;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500196 struct nfs_delegation *freeme = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 int status = 0;
198
Panagiotis Issarisf52720c2006-09-27 01:49:39 -0700199 delegation = kmalloc(sizeof(*delegation), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (delegation == NULL)
201 return -ENOMEM;
202 memcpy(delegation->stateid.data, res->delegation.data,
203 sizeof(delegation->stateid.data));
204 delegation->type = res->delegation_type;
205 delegation->maxsize = res->maxsize;
Trond Myklebustbeb2a5e2006-01-03 09:55:37 +0100206 delegation->change_attr = nfsi->change_attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 delegation->cred = get_rpccred(cred);
208 delegation->inode = inode;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500209 delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
Trond Myklebust34310432008-12-23 15:21:38 -0500210 spin_lock_init(&delegation->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
212 spin_lock(&clp->cl_lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500213 if (rcu_dereference(nfsi->delegation) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 if (memcmp(&delegation->stateid, &nfsi->delegation->stateid,
Trond Myklebust57bfa892008-01-25 16:38:18 -0500215 sizeof(delegation->stateid)) == 0 &&
216 delegation->type == nfsi->delegation->type) {
217 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 }
Trond Myklebust57bfa892008-01-25 16:38:18 -0500219 /*
220 * Deal with broken servers that hand out two
221 * delegations for the same file.
222 */
223 dfprintk(FILE, "%s: server %s handed out "
224 "a duplicate delegation!\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700225 __func__, clp->cl_hostname);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500226 if (delegation->type <= nfsi->delegation->type) {
227 freeme = delegation;
228 delegation = NULL;
229 goto out;
230 }
231 freeme = nfs_detach_delegation_locked(nfsi, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
Trond Myklebust57bfa892008-01-25 16:38:18 -0500233 list_add_rcu(&delegation->super_list, &clp->cl_delegations);
234 nfsi->delegation_state = delegation->type;
235 rcu_assign_pointer(nfsi->delegation, delegation);
236 delegation = NULL;
Trond Myklebust412c77c2007-07-03 16:10:55 -0400237
238 /* Ensure we revalidate the attributes and page cache! */
239 spin_lock(&inode->i_lock);
240 nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
241 spin_unlock(&inode->i_lock);
242
Trond Myklebust57bfa892008-01-25 16:38:18 -0500243out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 spin_unlock(&clp->cl_lock);
Trond Myklebust603c83d2007-10-18 19:59:20 -0400245 if (delegation != NULL)
246 nfs_free_delegation(delegation);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500247 if (freeme != NULL)
248 nfs_do_return_delegation(inode, freeme, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return status;
250}
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252/* Sync all data to disk upon delegation return */
253static void nfs_msync_inode(struct inode *inode)
254{
255 filemap_fdatawrite(inode->i_mapping);
256 nfs_wb_all(inode);
257 filemap_fdatawait(inode->i_mapping);
258}
259
260/*
261 * Basic procedure for returning a delegation to the server
262 */
Trond Myklebust90163022007-07-05 14:55:18 -0400263static int __nfs_inode_return_delegation(struct inode *inode, struct nfs_delegation *delegation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 struct nfs_inode *nfsi = NFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 nfs_msync_inode(inode);
Trond Myklebust3f09df72009-06-17 13:23:00 -0700268 /*
269 * Guard against new delegated open/lock/unlock calls and against
270 * state recovery
271 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 down_write(&nfsi->rwsem);
Trond Myklebust90163022007-07-05 14:55:18 -0400273 nfs_delegation_claim_opens(inode, &delegation->stateid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 up_write(&nfsi->rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 nfs_msync_inode(inode);
276
Trond Myklebuste6f81072008-01-24 18:14:34 -0500277 return nfs_do_return_delegation(inode, delegation, 1);
Trond Myklebust90163022007-07-05 14:55:18 -0400278}
279
Trond Myklebuste6f81072008-01-24 18:14:34 -0500280/*
Trond Myklebust515d8612008-12-23 15:21:46 -0500281 * Return all delegations that have been marked for return
282 */
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500283void nfs_client_return_marked_delegations(struct nfs_client *clp)
Trond Myklebust515d8612008-12-23 15:21:46 -0500284{
285 struct nfs_delegation *delegation;
286 struct inode *inode;
287
288restart:
289 rcu_read_lock();
290 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
291 if (!test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
292 continue;
293 inode = nfs_delegation_grab_inode(delegation);
294 if (inode == NULL)
295 continue;
296 spin_lock(&clp->cl_lock);
297 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
298 spin_unlock(&clp->cl_lock);
299 rcu_read_unlock();
300 if (delegation != NULL)
301 __nfs_inode_return_delegation(inode, delegation);
302 iput(inode);
303 goto restart;
304 }
305 rcu_read_unlock();
306}
307
308/*
Trond Myklebuste6f81072008-01-24 18:14:34 -0500309 * This function returns the delegation without reclaiming opens
310 * or protecting against delegation reclaims.
311 * It is therefore really only safe to be called from
312 * nfs4_clear_inode()
313 */
314void nfs_inode_return_delegation_noreclaim(struct inode *inode)
315{
316 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
317 struct nfs_inode *nfsi = NFS_I(inode);
318 struct nfs_delegation *delegation;
319
320 if (rcu_dereference(nfsi->delegation) != NULL) {
321 spin_lock(&clp->cl_lock);
322 delegation = nfs_detach_delegation_locked(nfsi, NULL);
323 spin_unlock(&clp->cl_lock);
324 if (delegation != NULL)
325 nfs_do_return_delegation(inode, delegation, 0);
326 }
327}
328
Trond Myklebust90163022007-07-05 14:55:18 -0400329int nfs_inode_return_delegation(struct inode *inode)
330{
331 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
332 struct nfs_inode *nfsi = NFS_I(inode);
333 struct nfs_delegation *delegation;
334 int err = 0;
335
Trond Myklebust8383e462007-07-06 15:12:04 -0400336 if (rcu_dereference(nfsi->delegation) != NULL) {
Trond Myklebust90163022007-07-05 14:55:18 -0400337 spin_lock(&clp->cl_lock);
338 delegation = nfs_detach_delegation_locked(nfsi, NULL);
339 spin_unlock(&clp->cl_lock);
340 if (delegation != NULL)
341 err = __nfs_inode_return_delegation(inode, delegation);
342 }
343 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
Trond Myklebust6411bd42008-12-23 15:21:51 -0500346static void nfs_mark_return_delegation(struct nfs_client *clp, struct nfs_delegation *delegation)
347{
348 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
349 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
350}
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352/*
353 * Return all delegations associated to a super block
354 */
Trond Myklebust515d8612008-12-23 15:21:46 -0500355void nfs_super_return_all_delegations(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
David Howells7539bba2006-08-22 20:06:09 -0400357 struct nfs_client *clp = NFS_SB(sb)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 struct nfs_delegation *delegation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
360 if (clp == NULL)
361 return;
Trond Myklebust8383e462007-07-06 15:12:04 -0400362 rcu_read_lock();
363 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Trond Myklebust86e89482008-12-23 15:21:39 -0500364 spin_lock(&delegation->lock);
365 if (delegation->inode != NULL && delegation->inode->i_sb == sb)
Trond Myklebust515d8612008-12-23 15:21:46 -0500366 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
Trond Myklebust86e89482008-12-23 15:21:39 -0500367 spin_unlock(&delegation->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400369 rcu_read_unlock();
Trond Myklebust515d8612008-12-23 15:21:46 -0500370 nfs_client_return_marked_delegations(clp);
371}
372
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500373static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
Trond Myklebust515d8612008-12-23 15:21:46 -0500374{
375 struct nfs_delegation *delegation;
376
377 rcu_read_lock();
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500378 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Trond Myklebust515d8612008-12-23 15:21:46 -0500379 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500380 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
381 }
Trond Myklebust515d8612008-12-23 15:21:46 -0500382 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
384
Trond Myklebustb0d3ded2008-12-23 15:21:50 -0500385static void nfs_delegation_run_state_manager(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +0100386{
Trond Myklebustb0d3ded2008-12-23 15:21:50 -0500387 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
388 nfs4_schedule_state_manager(clp);
Trond Myklebust58d97142006-01-03 09:55:24 +0100389}
390
David Howellsadfa6f92006-08-22 20:06:08 -0400391void nfs_expire_all_delegations(struct nfs_client *clp)
Trond Myklebust58d97142006-01-03 09:55:24 +0100392{
Trond Myklebustb0d3ded2008-12-23 15:21:50 -0500393 nfs_client_mark_return_all_delegations(clp);
394 nfs_delegation_run_state_manager(clp);
Trond Myklebust58d97142006-01-03 09:55:24 +0100395}
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397/*
398 * Return all delegations following an NFS4ERR_CB_PATH_DOWN error.
399 */
David Howellsadfa6f92006-08-22 20:06:08 -0400400void nfs_handle_cb_pathdown(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 if (clp == NULL)
403 return;
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500404 nfs_client_mark_return_all_delegations(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405}
406
Trond Myklebustb7391f42008-12-23 15:21:52 -0500407static void nfs_client_mark_return_unreferenced_delegations(struct nfs_client *clp)
408{
409 struct nfs_delegation *delegation;
410
411 rcu_read_lock();
412 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
413 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
414 continue;
415 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
416 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
417 }
418 rcu_read_unlock();
419}
420
421void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
422{
423 nfs_client_mark_return_unreferenced_delegations(clp);
424 nfs_delegation_run_state_manager(clp);
425}
426
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427/*
428 * Asynchronous delegation recall!
429 */
430int nfs_async_inode_return_delegation(struct inode *inode, const nfs4_stateid *stateid)
431{
Trond Myklebust6411bd42008-12-23 15:21:51 -0500432 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
433 struct nfs_delegation *delegation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Trond Myklebust6411bd42008-12-23 15:21:51 -0500435 rcu_read_lock();
436 delegation = rcu_dereference(NFS_I(inode)->delegation);
437 if (delegation == NULL || memcmp(delegation->stateid.data, stateid->data,
438 sizeof(delegation->stateid.data)) != 0) {
439 rcu_read_unlock();
440 return -ENOENT;
441 }
442 nfs_mark_return_delegation(clp, delegation);
443 rcu_read_unlock();
444 nfs_delegation_run_state_manager(clp);
445 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
448/*
449 * Retrieve the inode associated with a delegation
450 */
David Howellsadfa6f92006-08-22 20:06:08 -0400451struct inode *nfs_delegation_find_inode(struct nfs_client *clp, const struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 struct nfs_delegation *delegation;
454 struct inode *res = NULL;
Trond Myklebust8383e462007-07-06 15:12:04 -0400455 rcu_read_lock();
456 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Trond Myklebust86e89482008-12-23 15:21:39 -0500457 spin_lock(&delegation->lock);
458 if (delegation->inode != NULL &&
459 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 res = igrab(delegation->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 }
Trond Myklebust86e89482008-12-23 15:21:39 -0500462 spin_unlock(&delegation->lock);
463 if (res != NULL)
464 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400466 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 return res;
468}
469
470/*
471 * Mark all delegations as needing to be reclaimed
472 */
David Howellsadfa6f92006-08-22 20:06:08 -0400473void nfs_delegation_mark_reclaim(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
475 struct nfs_delegation *delegation;
Trond Myklebust8383e462007-07-06 15:12:04 -0400476 rcu_read_lock();
477 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list)
Trond Myklebust15c831b2008-12-23 15:21:39 -0500478 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
Trond Myklebust8383e462007-07-06 15:12:04 -0400479 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480}
481
482/*
483 * Reap all unclaimed delegations after reboot recovery is done
484 */
David Howellsadfa6f92006-08-22 20:06:08 -0400485void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486{
Trond Myklebust8383e462007-07-06 15:12:04 -0400487 struct nfs_delegation *delegation;
Trond Myklebust86e89482008-12-23 15:21:39 -0500488 struct inode *inode;
Trond Myklebust8383e462007-07-06 15:12:04 -0400489restart:
490 rcu_read_lock();
491 list_for_each_entry_rcu(delegation, &clp->cl_delegations, super_list) {
Trond Myklebust15c831b2008-12-23 15:21:39 -0500492 if (test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 continue;
Trond Myklebust86e89482008-12-23 15:21:39 -0500494 inode = nfs_delegation_grab_inode(delegation);
495 if (inode == NULL)
496 continue;
Trond Myklebust8383e462007-07-06 15:12:04 -0400497 spin_lock(&clp->cl_lock);
Trond Myklebust86e89482008-12-23 15:21:39 -0500498 delegation = nfs_detach_delegation_locked(NFS_I(inode), NULL);
Trond Myklebust8383e462007-07-06 15:12:04 -0400499 spin_unlock(&clp->cl_lock);
500 rcu_read_unlock();
501 if (delegation != NULL)
Trond Myklebust905f8d12007-08-06 12:18:34 -0400502 nfs_free_delegation(delegation);
Trond Myklebust86e89482008-12-23 15:21:39 -0500503 iput(inode);
Trond Myklebust8383e462007-07-06 15:12:04 -0400504 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400506 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500508
509int nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
510{
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500511 struct nfs_inode *nfsi = NFS_I(inode);
512 struct nfs_delegation *delegation;
Trond Myklebust8383e462007-07-06 15:12:04 -0400513 int ret = 0;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500514
Trond Myklebust8383e462007-07-06 15:12:04 -0400515 rcu_read_lock();
516 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500517 if (delegation != NULL) {
518 memcpy(dst->data, delegation->stateid.data, sizeof(dst->data));
Trond Myklebust8383e462007-07-06 15:12:04 -0400519 ret = 1;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500520 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400521 rcu_read_unlock();
522 return ret;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500523}