blob: e5f473d13e249b6804daa6a7c7e3d423df80c8e2 [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>
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"
Trond Myklebustca8acf82013-08-13 10:36:56 -040023#include "nfs4trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
Trond Myklebust905f8d12007-08-06 12:18:34 -040025static void nfs_free_delegation(struct nfs_delegation *delegation)
26{
Trond Myklebuste00b8a22011-01-27 14:55:39 -050027 if (delegation->cred) {
28 put_rpccred(delegation->cred);
29 delegation->cred = NULL;
30 }
Lai Jiangshan26f04dd2011-05-01 06:21:54 -070031 kfree_rcu(delegation, rcu);
Trond Myklebust8383e462007-07-06 15:12:04 -040032}
33
Chuck Leverd3978bb2010-12-24 01:33:04 +000034/**
35 * nfs_mark_delegation_referenced - set delegation's REFERENCED flag
36 * @delegation: delegation to process
37 *
38 */
Trond Myklebustb7391f42008-12-23 15:21:52 -050039void nfs_mark_delegation_referenced(struct nfs_delegation *delegation)
40{
41 set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags);
42}
43
Peng Tao15bb3af2014-07-03 13:05:00 +080044static int
45nfs4_do_check_delegation(struct inode *inode, fmode_t flags, bool mark)
Trond Myklebustb7391f42008-12-23 15:21:52 -050046{
47 struct nfs_delegation *delegation;
48 int ret = 0;
49
50 flags &= FMODE_READ|FMODE_WRITE;
51 rcu_read_lock();
52 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustd25be542013-02-05 11:43:28 -050053 if (delegation != NULL && (delegation->type & flags) == flags &&
54 !test_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) {
Peng Tao15bb3af2014-07-03 13:05:00 +080055 if (mark)
56 nfs_mark_delegation_referenced(delegation);
Trond Myklebustb7391f42008-12-23 15:21:52 -050057 ret = 1;
58 }
59 rcu_read_unlock();
60 return ret;
61}
Peng Tao15bb3af2014-07-03 13:05:00 +080062/**
63 * nfs_have_delegation - check if inode has a delegation, mark it
64 * NFS_DELEGATION_REFERENCED if there is one.
65 * @inode: inode to check
66 * @flags: delegation types to check for
67 *
68 * Returns one if inode has the indicated delegation, otherwise zero.
69 */
70int nfs4_have_delegation(struct inode *inode, fmode_t flags)
71{
72 return nfs4_do_check_delegation(inode, flags, true);
73}
74
75/*
76 * nfs4_check_delegation - check if inode has a delegation, do not mark
77 * NFS_DELEGATION_REFERENCED if it has one.
78 */
79int nfs4_check_delegation(struct inode *inode, fmode_t flags)
80{
81 return nfs4_do_check_delegation(inode, flags, false);
82}
Trond Myklebustb7391f42008-12-23 15:21:52 -050083
Trond Myklebustdb4f2e62013-04-01 15:56:46 -040084static int nfs_delegation_claim_locks(struct nfs_open_context *ctx, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -050085{
86 struct inode *inode = state->inode;
87 struct file_lock *fl;
Trond Myklebustd5122202009-06-17 13:22:58 -070088 int status = 0;
Trond Myklebust888e6942005-11-04 15:38:11 -050089
Trond Myklebust3f09df72009-06-17 13:23:00 -070090 if (inode->i_flock == NULL)
Trond Myklebust65b62a22013-02-07 10:54:07 -050091 goto out;
Jeff Layton314d7cc2013-04-10 15:36:48 -040092
Jeff Layton1c8c6012013-06-21 08:58:15 -040093 /* Protect inode->i_flock using the i_lock */
94 spin_lock(&inode->i_lock);
Harvey Harrison90dc7d22008-02-20 13:03:05 -080095 for (fl = inode->i_flock; fl != NULL; fl = fl->fl_next) {
Trond Myklebust888e6942005-11-04 15:38:11 -050096 if (!(fl->fl_flags & (FL_POSIX|FL_FLOCK)))
97 continue;
Trond Myklebustcd3758e2007-08-10 17:44:32 -040098 if (nfs_file_open_context(fl->fl_file) != ctx)
Trond Myklebust888e6942005-11-04 15:38:11 -050099 continue;
Jeff Layton1c8c6012013-06-21 08:58:15 -0400100 spin_unlock(&inode->i_lock);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -0400101 status = nfs4_lock_delegation_recall(fl, state, stateid);
Trond Myklebustd5122202009-06-17 13:22:58 -0700102 if (status < 0)
Trond Myklebust3f09df72009-06-17 13:23:00 -0700103 goto out;
Jeff Layton1c8c6012013-06-21 08:58:15 -0400104 spin_lock(&inode->i_lock);
Trond Myklebust888e6942005-11-04 15:38:11 -0500105 }
Jeff Layton1c8c6012013-06-21 08:58:15 -0400106 spin_unlock(&inode->i_lock);
Trond Myklebust3f09df72009-06-17 13:23:00 -0700107out:
Trond Myklebust888e6942005-11-04 15:38:11 -0500108 return status;
109}
110
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500111static int nfs_delegation_claim_opens(struct inode *inode, const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
113 struct nfs_inode *nfsi = NFS_I(inode);
114 struct nfs_open_context *ctx;
Trond Myklebustd25be542013-02-05 11:43:28 -0500115 struct nfs4_state_owner *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 struct nfs4_state *state;
Trond Myklebustd25be542013-02-05 11:43:28 -0500117 unsigned int seq;
Trond Myklebust888e6942005-11-04 15:38:11 -0500118 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120again:
121 spin_lock(&inode->i_lock);
122 list_for_each_entry(ctx, &nfsi->open_files, list) {
123 state = ctx->state;
124 if (state == NULL)
125 continue;
126 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
127 continue;
Trond Myklebustf597c532012-03-04 18:13:56 -0500128 if (!nfs4_stateid_match(&state->stateid, stateid))
Trond Myklebust90163022007-07-05 14:55:18 -0400129 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 get_nfs_open_context(ctx);
131 spin_unlock(&inode->i_lock);
Trond Myklebustd25be542013-02-05 11:43:28 -0500132 sp = state->owner;
Trond Myklebust65b62a22013-02-07 10:54:07 -0500133 /* Block nfs4_proc_unlck */
134 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebustd25be542013-02-05 11:43:28 -0500135 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
Trond Myklebust13437e12007-07-06 15:10:43 -0400136 err = nfs4_open_delegation_recall(ctx, state, stateid);
Trond Myklebustd25be542013-02-05 11:43:28 -0500137 if (!err)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -0400138 err = nfs_delegation_claim_locks(ctx, state, stateid);
Trond Myklebustd25be542013-02-05 11:43:28 -0500139 if (!err && read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
140 err = -EAGAIN;
Trond Myklebust65b62a22013-02-07 10:54:07 -0500141 mutex_unlock(&sp->so_delegreturn_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 put_nfs_open_context(ctx);
Trond Myklebust888e6942005-11-04 15:38:11 -0500143 if (err != 0)
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500144 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 goto again;
146 }
147 spin_unlock(&inode->i_lock);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500148 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
Chuck Leverd3978bb2010-12-24 01:33:04 +0000151/**
152 * nfs_inode_reclaim_delegation - process a delegation reclaim request
153 * @inode: inode to process
154 * @cred: credential to use for request
155 * @res: new delegation state from server
156 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 */
Chuck Leverd3978bb2010-12-24 01:33:04 +0000158void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred,
159 struct nfs_openres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Trond Myklebust8f649c32010-05-01 12:36:18 -0400161 struct nfs_delegation *delegation;
162 struct rpc_cred *oldcred = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Trond Myklebust8f649c32010-05-01 12:36:18 -0400164 rcu_read_lock();
165 delegation = rcu_dereference(NFS_I(inode)->delegation);
166 if (delegation != NULL) {
167 spin_lock(&delegation->lock);
168 if (delegation->inode != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500169 nfs4_stateid_copy(&delegation->stateid, &res->delegation);
Trond Myklebust8f649c32010-05-01 12:36:18 -0400170 delegation->type = res->delegation_type;
171 delegation->maxsize = res->maxsize;
172 oldcred = delegation->cred;
173 delegation->cred = get_rpccred(cred);
174 clear_bit(NFS_DELEGATION_NEED_RECLAIM,
175 &delegation->flags);
176 NFS_I(inode)->delegation_state = delegation->type;
177 spin_unlock(&delegation->lock);
178 put_rpccred(oldcred);
179 rcu_read_unlock();
Trond Myklebustca8acf82013-08-13 10:36:56 -0400180 trace_nfs4_reclaim_delegation(inode, res->delegation_type);
Trond Myklebust8f649c32010-05-01 12:36:18 -0400181 } else {
182 /* We appear to have raced with a delegation return. */
183 spin_unlock(&delegation->lock);
184 rcu_read_unlock();
185 nfs_inode_set_delegation(inode, cred, res);
186 }
187 } else {
188 rcu_read_unlock();
189 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
Trond Myklebust57bfa892008-01-25 16:38:18 -0500192static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
193{
194 int res = 0;
195
Trond Myklebust869f9df2014-11-10 18:43:56 -0500196 if (!test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
197 res = nfs4_proc_delegreturn(inode,
198 delegation->cred,
199 &delegation->stateid,
200 issync);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500201 nfs_free_delegation(delegation);
202 return res;
203}
204
Trond Myklebust86e89482008-12-23 15:21:39 -0500205static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
206{
207 struct inode *inode = NULL;
208
209 spin_lock(&delegation->lock);
210 if (delegation->inode != NULL)
211 inode = igrab(delegation->inode);
212 spin_unlock(&delegation->lock);
213 return inode;
214}
215
Chuck Leverdda4b222010-12-24 01:32:54 +0000216static struct nfs_delegation *
Trond Myklebustd25be542013-02-05 11:43:28 -0500217nfs_start_delegation_return_locked(struct nfs_inode *nfsi)
Trond Myklebust57bfa892008-01-25 16:38:18 -0500218{
Trond Myklebustd25be542013-02-05 11:43:28 -0500219 struct nfs_delegation *ret = NULL;
220 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500221
222 if (delegation == NULL)
Trond Myklebustd25be542013-02-05 11:43:28 -0500223 goto out;
224 spin_lock(&delegation->lock);
225 if (!test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
226 ret = delegation;
227 spin_unlock(&delegation->lock);
228out:
229 return ret;
230}
231
232static struct nfs_delegation *
233nfs_start_delegation_return(struct nfs_inode *nfsi)
234{
235 struct nfs_delegation *delegation;
236
237 rcu_read_lock();
238 delegation = nfs_start_delegation_return_locked(nfsi);
239 rcu_read_unlock();
240 return delegation;
241}
242
243static void
244nfs_abort_delegation_return(struct nfs_delegation *delegation,
245 struct nfs_client *clp)
246{
Chuck Leverdda4b222010-12-24 01:32:54 +0000247
Trond Myklebust34310432008-12-23 15:21:38 -0500248 spin_lock(&delegation->lock);
Trond Myklebustd25be542013-02-05 11:43:28 -0500249 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
250 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
251 spin_unlock(&delegation->lock);
252 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
253}
254
255static struct nfs_delegation *
256nfs_detach_delegation_locked(struct nfs_inode *nfsi,
257 struct nfs_delegation *delegation,
258 struct nfs_client *clp)
259{
260 struct nfs_delegation *deleg_cur =
261 rcu_dereference_protected(nfsi->delegation,
262 lockdep_is_held(&clp->cl_lock));
263
264 if (deleg_cur == NULL || delegation != deleg_cur)
265 return NULL;
266
267 spin_lock(&delegation->lock);
268 set_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500269 list_del_rcu(&delegation->super_list);
Trond Myklebust86e89482008-12-23 15:21:39 -0500270 delegation->inode = NULL;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500271 nfsi->delegation_state = 0;
272 rcu_assign_pointer(nfsi->delegation, NULL);
Trond Myklebust34310432008-12-23 15:21:38 -0500273 spin_unlock(&delegation->lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500274 return delegation;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500275}
276
Chuck Leverdda4b222010-12-24 01:32:54 +0000277static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi,
Trond Myklebustd25be542013-02-05 11:43:28 -0500278 struct nfs_delegation *delegation,
279 struct nfs_server *server)
Chuck Leverdda4b222010-12-24 01:32:54 +0000280{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000281 struct nfs_client *clp = server->nfs_client;
Chuck Leverdda4b222010-12-24 01:32:54 +0000282
283 spin_lock(&clp->cl_lock);
Trond Myklebustd25be542013-02-05 11:43:28 -0500284 delegation = nfs_detach_delegation_locked(nfsi, delegation, clp);
Chuck Leverdda4b222010-12-24 01:32:54 +0000285 spin_unlock(&clp->cl_lock);
286 return delegation;
287}
288
Trond Myklebustd25be542013-02-05 11:43:28 -0500289static struct nfs_delegation *
290nfs_inode_detach_delegation(struct inode *inode)
291{
292 struct nfs_inode *nfsi = NFS_I(inode);
293 struct nfs_server *server = NFS_SERVER(inode);
294 struct nfs_delegation *delegation;
295
296 delegation = nfs_start_delegation_return(nfsi);
297 if (delegation == NULL)
298 return NULL;
299 return nfs_detach_delegation(nfsi, delegation, server);
300}
301
Chuck Leverd3978bb2010-12-24 01:33:04 +0000302/**
303 * nfs_inode_set_delegation - set up a delegation on an inode
304 * @inode: inode to which delegation applies
305 * @cred: cred to use for subsequent delegation processing
306 * @res: new delegation state from server
307 *
308 * Returns zero on success, or a negative errno value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 */
310int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
311{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000312 struct nfs_server *server = NFS_SERVER(inode);
313 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 struct nfs_inode *nfsi = NFS_I(inode);
David Howells17d2c0a2010-05-01 12:37:18 -0400315 struct nfs_delegation *delegation, *old_delegation;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500316 struct nfs_delegation *freeme = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 int status = 0;
318
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400319 delegation = kmalloc(sizeof(*delegation), GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 if (delegation == NULL)
321 return -ENOMEM;
Trond Myklebustf597c532012-03-04 18:13:56 -0500322 nfs4_stateid_copy(&delegation->stateid, &res->delegation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 delegation->type = res->delegation_type;
324 delegation->maxsize = res->maxsize;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700325 delegation->change_attr = inode->i_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 delegation->cred = get_rpccred(cred);
327 delegation->inode = inode;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500328 delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
Trond Myklebust34310432008-12-23 15:21:38 -0500329 spin_lock_init(&delegation->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 spin_lock(&clp->cl_lock);
David Howells17d2c0a2010-05-01 12:37:18 -0400332 old_delegation = rcu_dereference_protected(nfsi->delegation,
Chuck Leverd3978bb2010-12-24 01:33:04 +0000333 lockdep_is_held(&clp->cl_lock));
David Howells17d2c0a2010-05-01 12:37:18 -0400334 if (old_delegation != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500335 if (nfs4_stateid_match(&delegation->stateid,
336 &old_delegation->stateid) &&
David Howells17d2c0a2010-05-01 12:37:18 -0400337 delegation->type == old_delegation->type) {
Trond Myklebust57bfa892008-01-25 16:38:18 -0500338 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
Trond Myklebust57bfa892008-01-25 16:38:18 -0500340 /*
341 * Deal with broken servers that hand out two
342 * delegations for the same file.
Trond Myklebust17280172012-03-11 13:11:00 -0400343 * Allow for upgrades to a WRITE delegation, but
344 * nothing else.
Trond Myklebust57bfa892008-01-25 16:38:18 -0500345 */
346 dfprintk(FILE, "%s: server %s handed out "
347 "a duplicate delegation!\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700348 __func__, clp->cl_hostname);
Trond Myklebust17280172012-03-11 13:11:00 -0400349 if (delegation->type == old_delegation->type ||
350 !(delegation->type & FMODE_WRITE)) {
Trond Myklebust57bfa892008-01-25 16:38:18 -0500351 freeme = delegation;
352 delegation = NULL;
353 goto out;
354 }
Trond Myklebustd25be542013-02-05 11:43:28 -0500355 freeme = nfs_detach_delegation_locked(nfsi,
356 old_delegation, clp);
357 if (freeme == NULL)
358 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 }
Chuck Leverd3978bb2010-12-24 01:33:04 +0000360 list_add_rcu(&delegation->super_list, &server->delegations);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500361 nfsi->delegation_state = delegation->type;
362 rcu_assign_pointer(nfsi->delegation, delegation);
363 delegation = NULL;
Trond Myklebust412c77c2007-07-03 16:10:55 -0400364
365 /* Ensure we revalidate the attributes and page cache! */
366 spin_lock(&inode->i_lock);
367 nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
368 spin_unlock(&inode->i_lock);
Trond Myklebustca8acf82013-08-13 10:36:56 -0400369 trace_nfs4_set_delegation(inode, res->delegation_type);
Trond Myklebust412c77c2007-07-03 16:10:55 -0400370
Trond Myklebust57bfa892008-01-25 16:38:18 -0500371out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 spin_unlock(&clp->cl_lock);
Trond Myklebust603c83d2007-10-18 19:59:20 -0400373 if (delegation != NULL)
374 nfs_free_delegation(delegation);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500375 if (freeme != NULL)
376 nfs_do_return_delegation(inode, freeme, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return status;
378}
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380/*
381 * Basic procedure for returning a delegation to the server
382 */
Trond Myklebustd25be542013-02-05 11:43:28 -0500383static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Trond Myklebustd25be542013-02-05 11:43:28 -0500385 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust869f9df2014-11-10 18:43:56 -0500387 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Trond Myklebustd25be542013-02-05 11:43:28 -0500389 if (delegation == NULL)
390 return 0;
391 do {
Trond Myklebust869f9df2014-11-10 18:43:56 -0500392 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
393 break;
Trond Myklebustd25be542013-02-05 11:43:28 -0500394 err = nfs_delegation_claim_opens(inode, &delegation->stateid);
395 if (!issync || err != -EAGAIN)
396 break;
397 /*
398 * Guard against state recovery
399 */
400 err = nfs4_wait_clnt_recover(clp);
401 } while (err == 0);
402
403 if (err) {
404 nfs_abort_delegation_return(delegation, clp);
405 goto out;
406 }
407 if (!nfs_detach_delegation(nfsi, delegation, NFS_SERVER(inode)))
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500408 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500410 err = nfs_do_return_delegation(inode, delegation, issync);
411out:
412 return err;
Trond Myklebust90163022007-07-05 14:55:18 -0400413}
414
Trond Myklebustb7571442013-04-03 14:33:49 -0400415static bool nfs_delegation_need_return(struct nfs_delegation *delegation)
416{
417 bool ret = false;
418
419 if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
420 ret = true;
421 if (test_and_clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags) && !ret) {
422 struct inode *inode;
423
424 spin_lock(&delegation->lock);
425 inode = delegation->inode;
426 if (inode && list_empty(&NFS_I(inode)->open_files))
427 ret = true;
428 spin_unlock(&delegation->lock);
429 }
430 return ret;
431}
432
Chuck Leverd3978bb2010-12-24 01:33:04 +0000433/**
434 * nfs_client_return_marked_delegations - return previously marked delegations
435 * @clp: nfs_client to process
436 *
Trond Myklebustdc327ed2012-05-06 19:46:30 -0400437 * Note that this function is designed to be called by the state
438 * manager thread. For this reason, it cannot flush the dirty data,
439 * since that could deadlock in case of a state recovery error.
440 *
Chuck Leverd3978bb2010-12-24 01:33:04 +0000441 * Returns zero on success, or a negative errno value.
Trond Myklebust515d8612008-12-23 15:21:46 -0500442 */
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500443int nfs_client_return_marked_delegations(struct nfs_client *clp)
Trond Myklebust515d8612008-12-23 15:21:46 -0500444{
445 struct nfs_delegation *delegation;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000446 struct nfs_server *server;
Trond Myklebust515d8612008-12-23 15:21:46 -0500447 struct inode *inode;
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500448 int err = 0;
Trond Myklebust515d8612008-12-23 15:21:46 -0500449
450restart:
451 rcu_read_lock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000452 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
453 list_for_each_entry_rcu(delegation, &server->delegations,
454 super_list) {
Trond Myklebustb7571442013-04-03 14:33:49 -0400455 if (!nfs_delegation_need_return(delegation))
Chuck Leverd3978bb2010-12-24 01:33:04 +0000456 continue;
457 inode = nfs_delegation_grab_inode(delegation);
458 if (inode == NULL)
459 continue;
Trond Myklebustd25be542013-02-05 11:43:28 -0500460 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
Chuck Leverd3978bb2010-12-24 01:33:04 +0000461 rcu_read_unlock();
462
Trond Myklebustd25be542013-02-05 11:43:28 -0500463 err = nfs_end_delegation_return(inode, delegation, 0);
Chuck Leverd3978bb2010-12-24 01:33:04 +0000464 iput(inode);
465 if (!err)
466 goto restart;
467 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
468 return err;
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500469 }
Trond Myklebust515d8612008-12-23 15:21:46 -0500470 }
471 rcu_read_unlock();
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500472 return 0;
Trond Myklebust515d8612008-12-23 15:21:46 -0500473}
474
Chuck Leverd3978bb2010-12-24 01:33:04 +0000475/**
476 * nfs_inode_return_delegation_noreclaim - return delegation, don't reclaim opens
477 * @inode: inode to process
478 *
479 * Does not protect against delegation reclaims, therefore really only safe
480 * to be called from nfs4_clear_inode().
Trond Myklebuste6f81072008-01-24 18:14:34 -0500481 */
482void nfs_inode_return_delegation_noreclaim(struct inode *inode)
483{
Trond Myklebuste6f81072008-01-24 18:14:34 -0500484 struct nfs_delegation *delegation;
485
Trond Myklebustd25be542013-02-05 11:43:28 -0500486 delegation = nfs_inode_detach_delegation(inode);
487 if (delegation != NULL)
488 nfs_do_return_delegation(inode, delegation, 0);
Trond Myklebuste6f81072008-01-24 18:14:34 -0500489}
490
Chuck Leverd3978bb2010-12-24 01:33:04 +0000491/**
492 * nfs_inode_return_delegation - synchronously return a delegation
493 * @inode: inode to process
494 *
Trond Myklebustc57d1bc2012-05-06 19:34:17 -0400495 * This routine will always flush any dirty data to disk on the
496 * assumption that if we need to return the delegation, then
497 * we should stop caching.
498 *
Chuck Leverd3978bb2010-12-24 01:33:04 +0000499 * Returns zero on success, or a negative errno value.
500 */
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400501int nfs4_inode_return_delegation(struct inode *inode)
Trond Myklebust90163022007-07-05 14:55:18 -0400502{
Trond Myklebust90163022007-07-05 14:55:18 -0400503 struct nfs_inode *nfsi = NFS_I(inode);
504 struct nfs_delegation *delegation;
505 int err = 0;
506
Trond Myklebustc57d1bc2012-05-06 19:34:17 -0400507 nfs_wb_all(inode);
Trond Myklebustd25be542013-02-05 11:43:28 -0500508 delegation = nfs_start_delegation_return(nfsi);
509 if (delegation != NULL)
510 err = nfs_end_delegation_return(inode, delegation, 1);
Trond Myklebust90163022007-07-05 14:55:18 -0400511 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
Trond Myklebustb7571442013-04-03 14:33:49 -0400514static void nfs_mark_return_if_closed_delegation(struct nfs_server *server,
515 struct nfs_delegation *delegation)
516{
517 set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
518 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
519}
520
Trond Myklebusted1e62112011-07-25 15:37:29 -0400521static void nfs_mark_return_delegation(struct nfs_server *server,
522 struct nfs_delegation *delegation)
Trond Myklebust6411bd42008-12-23 15:21:51 -0500523{
524 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
Trond Myklebusted1e62112011-07-25 15:37:29 -0400525 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
Trond Myklebust6411bd42008-12-23 15:21:51 -0500526}
527
Trond Myklebust5c31e232013-04-03 19:04:58 -0400528static bool nfs_server_mark_return_all_delegations(struct nfs_server *server)
529{
530 struct nfs_delegation *delegation;
531 bool ret = false;
532
533 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
534 nfs_mark_return_delegation(server, delegation);
535 ret = true;
536 }
537 return ret;
538}
539
Trond Myklebustb02ba0b2013-04-03 19:23:58 -0400540static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
541{
542 struct nfs_server *server;
543
544 rcu_read_lock();
545 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
546 nfs_server_mark_return_all_delegations(server);
547 rcu_read_unlock();
548}
549
550static void nfs_delegation_run_state_manager(struct nfs_client *clp)
551{
552 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
553 nfs4_schedule_state_manager(clp);
554}
555
556/**
557 * nfs_expire_all_delegations
558 * @clp: client to process
559 *
560 */
561void nfs_expire_all_delegations(struct nfs_client *clp)
562{
563 nfs_client_mark_return_all_delegations(clp);
564 nfs_delegation_run_state_manager(clp);
565}
566
Chuck Leverd3978bb2010-12-24 01:33:04 +0000567/**
568 * nfs_super_return_all_delegations - return delegations for one superblock
569 * @sb: sb to process
570 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 */
Bryan Schumakereeebf912012-06-20 15:53:41 -0400572void nfs_server_return_all_delegations(struct nfs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000574 struct nfs_client *clp = server->nfs_client;
Trond Myklebust5c31e232013-04-03 19:04:58 -0400575 bool need_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 if (clp == NULL)
578 return;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000579
Trond Myklebust8383e462007-07-06 15:12:04 -0400580 rcu_read_lock();
Trond Myklebust5c31e232013-04-03 19:04:58 -0400581 need_wait = nfs_server_mark_return_all_delegations(server);
Trond Myklebust8383e462007-07-06 15:12:04 -0400582 rcu_read_unlock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000583
Trond Myklebust5c31e232013-04-03 19:04:58 -0400584 if (need_wait) {
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500585 nfs4_schedule_state_manager(clp);
Trond Myklebust5c31e232013-04-03 19:04:58 -0400586 nfs4_wait_clnt_recover(clp);
587 }
Trond Myklebust515d8612008-12-23 15:21:46 -0500588}
589
Trond Myklebust826e0012013-04-03 19:27:52 -0400590static void nfs_mark_return_unused_delegation_types(struct nfs_server *server,
Chuck Leverd3978bb2010-12-24 01:33:04 +0000591 fmode_t flags)
Trond Myklebust515d8612008-12-23 15:21:46 -0500592{
593 struct nfs_delegation *delegation;
594
Chuck Leverd3978bb2010-12-24 01:33:04 +0000595 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500596 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
597 continue;
598 if (delegation->type & flags)
Trond Myklebust826e0012013-04-03 19:27:52 -0400599 nfs_mark_return_if_closed_delegation(server, delegation);
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500600 }
Chuck Leverd3978bb2010-12-24 01:33:04 +0000601}
602
Trond Myklebust826e0012013-04-03 19:27:52 -0400603static void nfs_client_mark_return_unused_delegation_types(struct nfs_client *clp,
Chuck Leverd3978bb2010-12-24 01:33:04 +0000604 fmode_t flags)
605{
606 struct nfs_server *server;
607
608 rcu_read_lock();
609 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
Trond Myklebust826e0012013-04-03 19:27:52 -0400610 nfs_mark_return_unused_delegation_types(server, flags);
Trond Myklebust515d8612008-12-23 15:21:46 -0500611 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
613
Trond Myklebust869f9df2014-11-10 18:43:56 -0500614static void nfs_revoke_delegation(struct inode *inode)
615{
616 struct nfs_delegation *delegation;
617 rcu_read_lock();
618 delegation = rcu_dereference(NFS_I(inode)->delegation);
619 if (delegation != NULL) {
620 set_bit(NFS_DELEGATION_REVOKED, &delegation->flags);
621 nfs_mark_return_delegation(NFS_SERVER(inode), delegation);
622 }
623 rcu_read_unlock();
624}
625
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500626void nfs_remove_bad_delegation(struct inode *inode)
627{
628 struct nfs_delegation *delegation;
629
Trond Myklebust869f9df2014-11-10 18:43:56 -0500630 nfs_revoke_delegation(inode);
Trond Myklebustd25be542013-02-05 11:43:28 -0500631 delegation = nfs_inode_detach_delegation(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500632 if (delegation) {
633 nfs_inode_find_state_and_recover(inode, &delegation->stateid);
634 nfs_free_delegation(delegation);
635 }
636}
Andy Adamson9cb81962012-03-07 10:49:41 -0500637EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500638
Chuck Leverd3978bb2010-12-24 01:33:04 +0000639/**
Trond Myklebust826e0012013-04-03 19:27:52 -0400640 * nfs_expire_unused_delegation_types
Chuck Leverd3978bb2010-12-24 01:33:04 +0000641 * @clp: client to process
642 * @flags: delegation types to expire
643 *
644 */
Trond Myklebust826e0012013-04-03 19:27:52 -0400645void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags)
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500646{
Trond Myklebust826e0012013-04-03 19:27:52 -0400647 nfs_client_mark_return_unused_delegation_types(clp, flags);
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500648 nfs_delegation_run_state_manager(clp);
649}
650
Chuck Leverd3978bb2010-12-24 01:33:04 +0000651static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
Trond Myklebustb7391f42008-12-23 15:21:52 -0500652{
653 struct nfs_delegation *delegation;
654
Chuck Leverd3978bb2010-12-24 01:33:04 +0000655 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
Trond Myklebustb7391f42008-12-23 15:21:52 -0500656 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
657 continue;
Trond Myklebustb7571442013-04-03 14:33:49 -0400658 nfs_mark_return_if_closed_delegation(server, delegation);
Trond Myklebustb7391f42008-12-23 15:21:52 -0500659 }
Trond Myklebustb7391f42008-12-23 15:21:52 -0500660}
661
Chuck Leverd3978bb2010-12-24 01:33:04 +0000662/**
663 * nfs_expire_unreferenced_delegations - Eliminate unused delegations
664 * @clp: nfs_client to process
665 *
666 */
Trond Myklebustb7391f42008-12-23 15:21:52 -0500667void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
668{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000669 struct nfs_server *server;
670
671 rcu_read_lock();
672 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
673 nfs_mark_return_unreferenced_delegations(server);
674 rcu_read_unlock();
675
Trond Myklebustb7391f42008-12-23 15:21:52 -0500676 nfs_delegation_run_state_manager(clp);
677}
678
Chuck Leverd3978bb2010-12-24 01:33:04 +0000679/**
680 * nfs_async_inode_return_delegation - asynchronously return a delegation
681 * @inode: inode to process
Trond Myklebust8e663f02012-03-04 18:13:56 -0500682 * @stateid: state ID information
Chuck Leverd3978bb2010-12-24 01:33:04 +0000683 *
684 * Returns zero on success, or a negative errno value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 */
Chuck Leverd3978bb2010-12-24 01:33:04 +0000686int nfs_async_inode_return_delegation(struct inode *inode,
687 const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Trond Myklebusted1e62112011-07-25 15:37:29 -0400689 struct nfs_server *server = NFS_SERVER(inode);
690 struct nfs_client *clp = server->nfs_client;
Trond Myklebust6411bd42008-12-23 15:21:51 -0500691 struct nfs_delegation *delegation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Trond Myklebustdc327ed2012-05-06 19:46:30 -0400693 filemap_flush(inode->i_mapping);
694
Trond Myklebust6411bd42008-12-23 15:21:51 -0500695 rcu_read_lock();
696 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebust755a48a2014-03-02 22:03:12 -0500697 if (delegation == NULL)
698 goto out_enoent;
Alexandros Batsakis25976412009-12-05 13:48:55 -0500699
Trond Myklebust755a48a2014-03-02 22:03:12 -0500700 if (!clp->cl_mvops->match_stateid(&delegation->stateid, stateid))
701 goto out_enoent;
Trond Myklebusted1e62112011-07-25 15:37:29 -0400702 nfs_mark_return_delegation(server, delegation);
Trond Myklebust6411bd42008-12-23 15:21:51 -0500703 rcu_read_unlock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000704
Trond Myklebust6411bd42008-12-23 15:21:51 -0500705 nfs_delegation_run_state_manager(clp);
706 return 0;
Trond Myklebust755a48a2014-03-02 22:03:12 -0500707out_enoent:
708 rcu_read_unlock();
709 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710}
711
Chuck Leverd3978bb2010-12-24 01:33:04 +0000712static struct inode *
713nfs_delegation_find_inode_server(struct nfs_server *server,
714 const struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
716 struct nfs_delegation *delegation;
717 struct inode *res = NULL;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000718
719 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
Trond Myklebust86e89482008-12-23 15:21:39 -0500720 spin_lock(&delegation->lock);
721 if (delegation->inode != NULL &&
722 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 res = igrab(delegation->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 }
Trond Myklebust86e89482008-12-23 15:21:39 -0500725 spin_unlock(&delegation->lock);
726 if (res != NULL)
727 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
Chuck Leverd3978bb2010-12-24 01:33:04 +0000729 return res;
730}
731
732/**
733 * nfs_delegation_find_inode - retrieve the inode associated with a delegation
734 * @clp: client state handle
735 * @fhandle: filehandle from a delegation recall
736 *
737 * Returns pointer to inode matching "fhandle," or NULL if a matching inode
738 * cannot be found.
739 */
740struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
741 const struct nfs_fh *fhandle)
742{
743 struct nfs_server *server;
744 struct inode *res = NULL;
745
746 rcu_read_lock();
747 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
748 res = nfs_delegation_find_inode_server(server, fhandle);
749 if (res != NULL)
750 break;
751 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400752 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 return res;
754}
755
Chuck Leverd3978bb2010-12-24 01:33:04 +0000756static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
757{
758 struct nfs_delegation *delegation;
759
760 list_for_each_entry_rcu(delegation, &server->delegations, super_list)
761 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
762}
763
764/**
765 * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
766 * @clp: nfs_client to process
767 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 */
David Howellsadfa6f92006-08-22 20:06:08 -0400769void nfs_delegation_mark_reclaim(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000771 struct nfs_server *server;
772
Trond Myklebust8383e462007-07-06 15:12:04 -0400773 rcu_read_lock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000774 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
775 nfs_delegation_mark_reclaim_server(server);
Trond Myklebust8383e462007-07-06 15:12:04 -0400776 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777}
778
Chuck Leverd3978bb2010-12-24 01:33:04 +0000779/**
780 * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
781 * @clp: nfs_client to process
782 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 */
David Howellsadfa6f92006-08-22 20:06:08 -0400784void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
Trond Myklebust8383e462007-07-06 15:12:04 -0400786 struct nfs_delegation *delegation;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000787 struct nfs_server *server;
Trond Myklebust86e89482008-12-23 15:21:39 -0500788 struct inode *inode;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000789
Trond Myklebust8383e462007-07-06 15:12:04 -0400790restart:
791 rcu_read_lock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000792 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
793 list_for_each_entry_rcu(delegation, &server->delegations,
794 super_list) {
795 if (test_bit(NFS_DELEGATION_NEED_RECLAIM,
796 &delegation->flags) == 0)
797 continue;
798 inode = nfs_delegation_grab_inode(delegation);
799 if (inode == NULL)
800 continue;
801 delegation = nfs_detach_delegation(NFS_I(inode),
Trond Myklebustd25be542013-02-05 11:43:28 -0500802 delegation, server);
Chuck Leverd3978bb2010-12-24 01:33:04 +0000803 rcu_read_unlock();
804
805 if (delegation != NULL)
806 nfs_free_delegation(delegation);
807 iput(inode);
808 goto restart;
809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400811 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812}
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500813
Chuck Leverd3978bb2010-12-24 01:33:04 +0000814/**
815 * nfs_delegations_present - check for existence of delegations
816 * @clp: client state handle
817 *
818 * Returns one if there are any nfs_delegation structures attached
819 * to this nfs_client.
820 */
821int nfs_delegations_present(struct nfs_client *clp)
822{
823 struct nfs_server *server;
824 int ret = 0;
825
826 rcu_read_lock();
827 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
828 if (!list_empty(&server->delegations)) {
829 ret = 1;
830 break;
831 }
832 rcu_read_unlock();
833 return ret;
834}
835
836/**
837 * nfs4_copy_delegation_stateid - Copy inode's state ID information
838 * @dst: stateid data structure to fill in
839 * @inode: inode to check
Trond Myklebust0032a7a2012-03-08 17:16:12 -0500840 * @flags: delegation type requirement
Chuck Leverd3978bb2010-12-24 01:33:04 +0000841 *
Trond Myklebust0032a7a2012-03-08 17:16:12 -0500842 * Returns "true" and fills in "dst->data" * if inode had a delegation,
843 * otherwise "false" is returned.
Chuck Leverd3978bb2010-12-24 01:33:04 +0000844 */
Trond Myklebust0032a7a2012-03-08 17:16:12 -0500845bool nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode,
846 fmode_t flags)
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500847{
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500848 struct nfs_inode *nfsi = NFS_I(inode);
849 struct nfs_delegation *delegation;
Trond Myklebust0032a7a2012-03-08 17:16:12 -0500850 bool ret;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500851
Trond Myklebust0032a7a2012-03-08 17:16:12 -0500852 flags &= FMODE_READ|FMODE_WRITE;
Trond Myklebust8383e462007-07-06 15:12:04 -0400853 rcu_read_lock();
854 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust0032a7a2012-03-08 17:16:12 -0500855 ret = (delegation != NULL && (delegation->type & flags) == flags);
856 if (ret) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500857 nfs4_stateid_copy(dst, &delegation->stateid);
Trond Myklebust0032a7a2012-03-08 17:16:12 -0500858 nfs_mark_delegation_referenced(delegation);
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500859 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400860 rcu_read_unlock();
861 return ret;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500862}