blob: 7f3f60641344437ab4f493eebf6093e39957c3be [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 Myklebustdb4f2e632013-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 Myklebustdb4f2e632013-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 Myklebustf8ebf7a2014-10-17 23:02:52 +0300128 if (!nfs4_valid_open_stateid(state))
129 continue;
Trond Myklebustf597c532012-03-04 18:13:56 -0500130 if (!nfs4_stateid_match(&state->stateid, stateid))
Trond Myklebust90163022007-07-05 14:55:18 -0400131 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 get_nfs_open_context(ctx);
133 spin_unlock(&inode->i_lock);
Trond Myklebustd25be542013-02-05 11:43:28 -0500134 sp = state->owner;
Trond Myklebust65b62a22013-02-07 10:54:07 -0500135 /* Block nfs4_proc_unlck */
136 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebustd25be542013-02-05 11:43:28 -0500137 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
Trond Myklebust13437e12007-07-06 15:10:43 -0400138 err = nfs4_open_delegation_recall(ctx, state, stateid);
Trond Myklebustd25be542013-02-05 11:43:28 -0500139 if (!err)
Trond Myklebustdb4f2e632013-04-01 15:56:46 -0400140 err = nfs_delegation_claim_locks(ctx, state, stateid);
Trond Myklebustd25be542013-02-05 11:43:28 -0500141 if (!err && read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
142 err = -EAGAIN;
Trond Myklebust65b62a22013-02-07 10:54:07 -0500143 mutex_unlock(&sp->so_delegreturn_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 put_nfs_open_context(ctx);
Trond Myklebust888e6942005-11-04 15:38:11 -0500145 if (err != 0)
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500146 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 goto again;
148 }
149 spin_unlock(&inode->i_lock);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500150 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
Chuck Leverd3978bb2010-12-24 01:33:04 +0000153/**
154 * nfs_inode_reclaim_delegation - process a delegation reclaim request
155 * @inode: inode to process
156 * @cred: credential to use for request
157 * @res: new delegation state from server
158 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 */
Chuck Leverd3978bb2010-12-24 01:33:04 +0000160void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred,
161 struct nfs_openres *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Trond Myklebust8f649c32010-05-01 12:36:18 -0400163 struct nfs_delegation *delegation;
164 struct rpc_cred *oldcred = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
Trond Myklebust8f649c32010-05-01 12:36:18 -0400166 rcu_read_lock();
167 delegation = rcu_dereference(NFS_I(inode)->delegation);
168 if (delegation != NULL) {
169 spin_lock(&delegation->lock);
170 if (delegation->inode != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500171 nfs4_stateid_copy(&delegation->stateid, &res->delegation);
Trond Myklebust8f649c32010-05-01 12:36:18 -0400172 delegation->type = res->delegation_type;
173 delegation->maxsize = res->maxsize;
174 oldcred = delegation->cred;
175 delegation->cred = get_rpccred(cred);
176 clear_bit(NFS_DELEGATION_NEED_RECLAIM,
177 &delegation->flags);
178 NFS_I(inode)->delegation_state = delegation->type;
179 spin_unlock(&delegation->lock);
180 put_rpccred(oldcred);
181 rcu_read_unlock();
Trond Myklebustca8acf82013-08-13 10:36:56 -0400182 trace_nfs4_reclaim_delegation(inode, res->delegation_type);
Trond Myklebust8f649c32010-05-01 12:36:18 -0400183 } else {
184 /* We appear to have raced with a delegation return. */
185 spin_unlock(&delegation->lock);
186 rcu_read_unlock();
187 nfs_inode_set_delegation(inode, cred, res);
188 }
189 } else {
190 rcu_read_unlock();
191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
Trond Myklebust57bfa892008-01-25 16:38:18 -0500194static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
195{
196 int res = 0;
197
Trond Myklebust869f9df2014-11-10 18:43:56 -0500198 if (!test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
199 res = nfs4_proc_delegreturn(inode,
200 delegation->cred,
201 &delegation->stateid,
202 issync);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500203 nfs_free_delegation(delegation);
204 return res;
205}
206
Trond Myklebust86e89482008-12-23 15:21:39 -0500207static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
208{
209 struct inode *inode = NULL;
210
211 spin_lock(&delegation->lock);
212 if (delegation->inode != NULL)
213 inode = igrab(delegation->inode);
214 spin_unlock(&delegation->lock);
215 return inode;
216}
217
Chuck Leverdda4b222010-12-24 01:32:54 +0000218static struct nfs_delegation *
Trond Myklebustd25be542013-02-05 11:43:28 -0500219nfs_start_delegation_return_locked(struct nfs_inode *nfsi)
Trond Myklebust57bfa892008-01-25 16:38:18 -0500220{
Trond Myklebustd25be542013-02-05 11:43:28 -0500221 struct nfs_delegation *ret = NULL;
222 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500223
224 if (delegation == NULL)
Trond Myklebustd25be542013-02-05 11:43:28 -0500225 goto out;
226 spin_lock(&delegation->lock);
227 if (!test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
228 ret = delegation;
229 spin_unlock(&delegation->lock);
230out:
231 return ret;
232}
233
234static struct nfs_delegation *
235nfs_start_delegation_return(struct nfs_inode *nfsi)
236{
237 struct nfs_delegation *delegation;
238
239 rcu_read_lock();
240 delegation = nfs_start_delegation_return_locked(nfsi);
241 rcu_read_unlock();
242 return delegation;
243}
244
245static void
246nfs_abort_delegation_return(struct nfs_delegation *delegation,
247 struct nfs_client *clp)
248{
Chuck Leverdda4b222010-12-24 01:32:54 +0000249
Trond Myklebust34310432008-12-23 15:21:38 -0500250 spin_lock(&delegation->lock);
Trond Myklebustd25be542013-02-05 11:43:28 -0500251 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
252 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
253 spin_unlock(&delegation->lock);
254 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
255}
256
257static struct nfs_delegation *
258nfs_detach_delegation_locked(struct nfs_inode *nfsi,
259 struct nfs_delegation *delegation,
260 struct nfs_client *clp)
261{
262 struct nfs_delegation *deleg_cur =
263 rcu_dereference_protected(nfsi->delegation,
264 lockdep_is_held(&clp->cl_lock));
265
266 if (deleg_cur == NULL || delegation != deleg_cur)
267 return NULL;
268
269 spin_lock(&delegation->lock);
270 set_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500271 list_del_rcu(&delegation->super_list);
Trond Myklebust86e89482008-12-23 15:21:39 -0500272 delegation->inode = NULL;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500273 nfsi->delegation_state = 0;
274 rcu_assign_pointer(nfsi->delegation, NULL);
Trond Myklebust34310432008-12-23 15:21:38 -0500275 spin_unlock(&delegation->lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500276 return delegation;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500277}
278
Chuck Leverdda4b222010-12-24 01:32:54 +0000279static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi,
Trond Myklebustd25be542013-02-05 11:43:28 -0500280 struct nfs_delegation *delegation,
281 struct nfs_server *server)
Chuck Leverdda4b222010-12-24 01:32:54 +0000282{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000283 struct nfs_client *clp = server->nfs_client;
Chuck Leverdda4b222010-12-24 01:32:54 +0000284
285 spin_lock(&clp->cl_lock);
Trond Myklebustd25be542013-02-05 11:43:28 -0500286 delegation = nfs_detach_delegation_locked(nfsi, delegation, clp);
Chuck Leverdda4b222010-12-24 01:32:54 +0000287 spin_unlock(&clp->cl_lock);
288 return delegation;
289}
290
Trond Myklebustd25be542013-02-05 11:43:28 -0500291static struct nfs_delegation *
292nfs_inode_detach_delegation(struct inode *inode)
293{
294 struct nfs_inode *nfsi = NFS_I(inode);
295 struct nfs_server *server = NFS_SERVER(inode);
296 struct nfs_delegation *delegation;
297
298 delegation = nfs_start_delegation_return(nfsi);
299 if (delegation == NULL)
300 return NULL;
301 return nfs_detach_delegation(nfsi, delegation, server);
302}
303
Chuck Leverd3978bb2010-12-24 01:33:04 +0000304/**
305 * nfs_inode_set_delegation - set up a delegation on an inode
306 * @inode: inode to which delegation applies
307 * @cred: cred to use for subsequent delegation processing
308 * @res: new delegation state from server
309 *
310 * Returns zero on success, or a negative errno value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 */
312int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
313{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000314 struct nfs_server *server = NFS_SERVER(inode);
315 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 struct nfs_inode *nfsi = NFS_I(inode);
David Howells17d2c0a2010-05-01 12:37:18 -0400317 struct nfs_delegation *delegation, *old_delegation;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500318 struct nfs_delegation *freeme = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 int status = 0;
320
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400321 delegation = kmalloc(sizeof(*delegation), GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (delegation == NULL)
323 return -ENOMEM;
Trond Myklebustf597c532012-03-04 18:13:56 -0500324 nfs4_stateid_copy(&delegation->stateid, &res->delegation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 delegation->type = res->delegation_type;
326 delegation->maxsize = res->maxsize;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700327 delegation->change_attr = inode->i_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 delegation->cred = get_rpccred(cred);
329 delegation->inode = inode;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500330 delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
Trond Myklebust34310432008-12-23 15:21:38 -0500331 spin_lock_init(&delegation->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
333 spin_lock(&clp->cl_lock);
David Howells17d2c0a2010-05-01 12:37:18 -0400334 old_delegation = rcu_dereference_protected(nfsi->delegation,
Chuck Leverd3978bb2010-12-24 01:33:04 +0000335 lockdep_is_held(&clp->cl_lock));
David Howells17d2c0a2010-05-01 12:37:18 -0400336 if (old_delegation != NULL) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500337 if (nfs4_stateid_match(&delegation->stateid,
338 &old_delegation->stateid) &&
David Howells17d2c0a2010-05-01 12:37:18 -0400339 delegation->type == old_delegation->type) {
Trond Myklebust57bfa892008-01-25 16:38:18 -0500340 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
Trond Myklebust57bfa892008-01-25 16:38:18 -0500342 /*
343 * Deal with broken servers that hand out two
344 * delegations for the same file.
Trond Myklebust17280172012-03-11 13:11:00 -0400345 * Allow for upgrades to a WRITE delegation, but
346 * nothing else.
Trond Myklebust57bfa892008-01-25 16:38:18 -0500347 */
348 dfprintk(FILE, "%s: server %s handed out "
349 "a duplicate delegation!\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700350 __func__, clp->cl_hostname);
Trond Myklebust17280172012-03-11 13:11:00 -0400351 if (delegation->type == old_delegation->type ||
352 !(delegation->type & FMODE_WRITE)) {
Trond Myklebust57bfa892008-01-25 16:38:18 -0500353 freeme = delegation;
354 delegation = NULL;
355 goto out;
356 }
Trond Myklebustd25be542013-02-05 11:43:28 -0500357 freeme = nfs_detach_delegation_locked(nfsi,
358 old_delegation, clp);
359 if (freeme == NULL)
360 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 }
Chuck Leverd3978bb2010-12-24 01:33:04 +0000362 list_add_rcu(&delegation->super_list, &server->delegations);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500363 nfsi->delegation_state = delegation->type;
364 rcu_assign_pointer(nfsi->delegation, delegation);
365 delegation = NULL;
Trond Myklebust412c77c2007-07-03 16:10:55 -0400366
367 /* Ensure we revalidate the attributes and page cache! */
368 spin_lock(&inode->i_lock);
369 nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
370 spin_unlock(&inode->i_lock);
Trond Myklebustca8acf82013-08-13 10:36:56 -0400371 trace_nfs4_set_delegation(inode, res->delegation_type);
Trond Myklebust412c77c2007-07-03 16:10:55 -0400372
Trond Myklebust57bfa892008-01-25 16:38:18 -0500373out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 spin_unlock(&clp->cl_lock);
Trond Myklebust603c83d2007-10-18 19:59:20 -0400375 if (delegation != NULL)
376 nfs_free_delegation(delegation);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500377 if (freeme != NULL)
378 nfs_do_return_delegation(inode, freeme, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return status;
380}
381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382/*
383 * Basic procedure for returning a delegation to the server
384 */
Trond Myklebustd25be542013-02-05 11:43:28 -0500385static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Trond Myklebustd25be542013-02-05 11:43:28 -0500387 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust869f9df2014-11-10 18:43:56 -0500389 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Trond Myklebustd25be542013-02-05 11:43:28 -0500391 if (delegation == NULL)
392 return 0;
393 do {
Trond Myklebust869f9df2014-11-10 18:43:56 -0500394 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
395 break;
Trond Myklebustd25be542013-02-05 11:43:28 -0500396 err = nfs_delegation_claim_opens(inode, &delegation->stateid);
397 if (!issync || err != -EAGAIN)
398 break;
399 /*
400 * Guard against state recovery
401 */
402 err = nfs4_wait_clnt_recover(clp);
403 } while (err == 0);
404
405 if (err) {
406 nfs_abort_delegation_return(delegation, clp);
407 goto out;
408 }
409 if (!nfs_detach_delegation(nfsi, delegation, NFS_SERVER(inode)))
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500410 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500412 err = nfs_do_return_delegation(inode, delegation, issync);
413out:
414 return err;
Trond Myklebust90163022007-07-05 14:55:18 -0400415}
416
Trond Myklebustb7571442013-04-03 14:33:49 -0400417static bool nfs_delegation_need_return(struct nfs_delegation *delegation)
418{
419 bool ret = false;
420
421 if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
422 ret = true;
423 if (test_and_clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags) && !ret) {
424 struct inode *inode;
425
426 spin_lock(&delegation->lock);
427 inode = delegation->inode;
428 if (inode && list_empty(&NFS_I(inode)->open_files))
429 ret = true;
430 spin_unlock(&delegation->lock);
431 }
432 return ret;
433}
434
Chuck Leverd3978bb2010-12-24 01:33:04 +0000435/**
436 * nfs_client_return_marked_delegations - return previously marked delegations
437 * @clp: nfs_client to process
438 *
Trond Myklebustdc327ed2012-05-06 19:46:30 -0400439 * Note that this function is designed to be called by the state
440 * manager thread. For this reason, it cannot flush the dirty data,
441 * since that could deadlock in case of a state recovery error.
442 *
Chuck Leverd3978bb2010-12-24 01:33:04 +0000443 * Returns zero on success, or a negative errno value.
Trond Myklebust515d8612008-12-23 15:21:46 -0500444 */
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500445int nfs_client_return_marked_delegations(struct nfs_client *clp)
Trond Myklebust515d8612008-12-23 15:21:46 -0500446{
447 struct nfs_delegation *delegation;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000448 struct nfs_server *server;
Trond Myklebust515d8612008-12-23 15:21:46 -0500449 struct inode *inode;
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500450 int err = 0;
Trond Myklebust515d8612008-12-23 15:21:46 -0500451
452restart:
453 rcu_read_lock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000454 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
455 list_for_each_entry_rcu(delegation, &server->delegations,
456 super_list) {
Trond Myklebustb7571442013-04-03 14:33:49 -0400457 if (!nfs_delegation_need_return(delegation))
Chuck Leverd3978bb2010-12-24 01:33:04 +0000458 continue;
459 inode = nfs_delegation_grab_inode(delegation);
460 if (inode == NULL)
461 continue;
Trond Myklebustd25be542013-02-05 11:43:28 -0500462 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
Chuck Leverd3978bb2010-12-24 01:33:04 +0000463 rcu_read_unlock();
464
Trond Myklebustd25be542013-02-05 11:43:28 -0500465 err = nfs_end_delegation_return(inode, delegation, 0);
Chuck Leverd3978bb2010-12-24 01:33:04 +0000466 iput(inode);
467 if (!err)
468 goto restart;
469 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
470 return err;
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500471 }
Trond Myklebust515d8612008-12-23 15:21:46 -0500472 }
473 rcu_read_unlock();
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500474 return 0;
Trond Myklebust515d8612008-12-23 15:21:46 -0500475}
476
Chuck Leverd3978bb2010-12-24 01:33:04 +0000477/**
478 * nfs_inode_return_delegation_noreclaim - return delegation, don't reclaim opens
479 * @inode: inode to process
480 *
481 * Does not protect against delegation reclaims, therefore really only safe
482 * to be called from nfs4_clear_inode().
Trond Myklebuste6f81072008-01-24 18:14:34 -0500483 */
484void nfs_inode_return_delegation_noreclaim(struct inode *inode)
485{
Trond Myklebuste6f81072008-01-24 18:14:34 -0500486 struct nfs_delegation *delegation;
487
Trond Myklebustd25be542013-02-05 11:43:28 -0500488 delegation = nfs_inode_detach_delegation(inode);
489 if (delegation != NULL)
490 nfs_do_return_delegation(inode, delegation, 0);
Trond Myklebuste6f81072008-01-24 18:14:34 -0500491}
492
Chuck Leverd3978bb2010-12-24 01:33:04 +0000493/**
494 * nfs_inode_return_delegation - synchronously return a delegation
495 * @inode: inode to process
496 *
Trond Myklebustc57d1bc2012-05-06 19:34:17 -0400497 * This routine will always flush any dirty data to disk on the
498 * assumption that if we need to return the delegation, then
499 * we should stop caching.
500 *
Chuck Leverd3978bb2010-12-24 01:33:04 +0000501 * Returns zero on success, or a negative errno value.
502 */
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400503int nfs4_inode_return_delegation(struct inode *inode)
Trond Myklebust90163022007-07-05 14:55:18 -0400504{
Trond Myklebust90163022007-07-05 14:55:18 -0400505 struct nfs_inode *nfsi = NFS_I(inode);
506 struct nfs_delegation *delegation;
507 int err = 0;
508
Trond Myklebustc57d1bc2012-05-06 19:34:17 -0400509 nfs_wb_all(inode);
Trond Myklebustd25be542013-02-05 11:43:28 -0500510 delegation = nfs_start_delegation_return(nfsi);
511 if (delegation != NULL)
512 err = nfs_end_delegation_return(inode, delegation, 1);
Trond Myklebust90163022007-07-05 14:55:18 -0400513 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514}
515
Trond Myklebustb7571442013-04-03 14:33:49 -0400516static void nfs_mark_return_if_closed_delegation(struct nfs_server *server,
517 struct nfs_delegation *delegation)
518{
519 set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
520 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
521}
522
Trond Myklebusted1e6212011-07-25 15:37:29 -0400523static void nfs_mark_return_delegation(struct nfs_server *server,
524 struct nfs_delegation *delegation)
Trond Myklebust6411bd42008-12-23 15:21:51 -0500525{
526 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
Trond Myklebusted1e6212011-07-25 15:37:29 -0400527 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
Trond Myklebust6411bd42008-12-23 15:21:51 -0500528}
529
Trond Myklebust5c31e232013-04-03 19:04:58 -0400530static bool nfs_server_mark_return_all_delegations(struct nfs_server *server)
531{
532 struct nfs_delegation *delegation;
533 bool ret = false;
534
535 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
536 nfs_mark_return_delegation(server, delegation);
537 ret = true;
538 }
539 return ret;
540}
541
Trond Myklebustb02ba0b2013-04-03 19:23:58 -0400542static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
543{
544 struct nfs_server *server;
545
546 rcu_read_lock();
547 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
548 nfs_server_mark_return_all_delegations(server);
549 rcu_read_unlock();
550}
551
552static void nfs_delegation_run_state_manager(struct nfs_client *clp)
553{
554 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
555 nfs4_schedule_state_manager(clp);
556}
557
558/**
559 * nfs_expire_all_delegations
560 * @clp: client to process
561 *
562 */
563void nfs_expire_all_delegations(struct nfs_client *clp)
564{
565 nfs_client_mark_return_all_delegations(clp);
566 nfs_delegation_run_state_manager(clp);
567}
568
Chuck Leverd3978bb2010-12-24 01:33:04 +0000569/**
570 * nfs_super_return_all_delegations - return delegations for one superblock
571 * @sb: sb to process
572 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 */
Bryan Schumakereeebf912012-06-20 15:53:41 -0400574void nfs_server_return_all_delegations(struct nfs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000576 struct nfs_client *clp = server->nfs_client;
Trond Myklebust5c31e232013-04-03 19:04:58 -0400577 bool need_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 if (clp == NULL)
580 return;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000581
Trond Myklebust8383e462007-07-06 15:12:04 -0400582 rcu_read_lock();
Trond Myklebust5c31e232013-04-03 19:04:58 -0400583 need_wait = nfs_server_mark_return_all_delegations(server);
Trond Myklebust8383e462007-07-06 15:12:04 -0400584 rcu_read_unlock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000585
Trond Myklebust5c31e232013-04-03 19:04:58 -0400586 if (need_wait) {
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500587 nfs4_schedule_state_manager(clp);
Trond Myklebust5c31e232013-04-03 19:04:58 -0400588 nfs4_wait_clnt_recover(clp);
589 }
Trond Myklebust515d8612008-12-23 15:21:46 -0500590}
591
Trond Myklebust826e0012013-04-03 19:27:52 -0400592static void nfs_mark_return_unused_delegation_types(struct nfs_server *server,
Chuck Leverd3978bb2010-12-24 01:33:04 +0000593 fmode_t flags)
Trond Myklebust515d8612008-12-23 15:21:46 -0500594{
595 struct nfs_delegation *delegation;
596
Chuck Leverd3978bb2010-12-24 01:33:04 +0000597 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500598 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
599 continue;
600 if (delegation->type & flags)
Trond Myklebust826e0012013-04-03 19:27:52 -0400601 nfs_mark_return_if_closed_delegation(server, delegation);
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500602 }
Chuck Leverd3978bb2010-12-24 01:33:04 +0000603}
604
Trond Myklebust826e0012013-04-03 19:27:52 -0400605static void nfs_client_mark_return_unused_delegation_types(struct nfs_client *clp,
Chuck Leverd3978bb2010-12-24 01:33:04 +0000606 fmode_t flags)
607{
608 struct nfs_server *server;
609
610 rcu_read_lock();
611 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
Trond Myklebust826e0012013-04-03 19:27:52 -0400612 nfs_mark_return_unused_delegation_types(server, flags);
Trond Myklebust515d8612008-12-23 15:21:46 -0500613 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
Trond Myklebust869f9df2014-11-10 18:43:56 -0500616static void nfs_revoke_delegation(struct inode *inode)
617{
618 struct nfs_delegation *delegation;
619 rcu_read_lock();
620 delegation = rcu_dereference(NFS_I(inode)->delegation);
621 if (delegation != NULL) {
622 set_bit(NFS_DELEGATION_REVOKED, &delegation->flags);
623 nfs_mark_return_delegation(NFS_SERVER(inode), delegation);
624 }
625 rcu_read_unlock();
626}
627
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500628void nfs_remove_bad_delegation(struct inode *inode)
629{
630 struct nfs_delegation *delegation;
631
Trond Myklebust869f9df2014-11-10 18:43:56 -0500632 nfs_revoke_delegation(inode);
Trond Myklebustd25be542013-02-05 11:43:28 -0500633 delegation = nfs_inode_detach_delegation(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500634 if (delegation) {
635 nfs_inode_find_state_and_recover(inode, &delegation->stateid);
636 nfs_free_delegation(delegation);
637 }
638}
Andy Adamson9cb81962012-03-07 10:49:41 -0500639EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500640
Chuck Leverd3978bb2010-12-24 01:33:04 +0000641/**
Trond Myklebust826e0012013-04-03 19:27:52 -0400642 * nfs_expire_unused_delegation_types
Chuck Leverd3978bb2010-12-24 01:33:04 +0000643 * @clp: client to process
644 * @flags: delegation types to expire
645 *
646 */
Trond Myklebust826e0012013-04-03 19:27:52 -0400647void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags)
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500648{
Trond Myklebust826e0012013-04-03 19:27:52 -0400649 nfs_client_mark_return_unused_delegation_types(clp, flags);
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500650 nfs_delegation_run_state_manager(clp);
651}
652
Chuck Leverd3978bb2010-12-24 01:33:04 +0000653static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
Trond Myklebustb7391f42008-12-23 15:21:52 -0500654{
655 struct nfs_delegation *delegation;
656
Chuck Leverd3978bb2010-12-24 01:33:04 +0000657 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
Trond Myklebustb7391f42008-12-23 15:21:52 -0500658 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
659 continue;
Trond Myklebustb7571442013-04-03 14:33:49 -0400660 nfs_mark_return_if_closed_delegation(server, delegation);
Trond Myklebustb7391f42008-12-23 15:21:52 -0500661 }
Trond Myklebustb7391f42008-12-23 15:21:52 -0500662}
663
Chuck Leverd3978bb2010-12-24 01:33:04 +0000664/**
665 * nfs_expire_unreferenced_delegations - Eliminate unused delegations
666 * @clp: nfs_client to process
667 *
668 */
Trond Myklebustb7391f42008-12-23 15:21:52 -0500669void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
670{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000671 struct nfs_server *server;
672
673 rcu_read_lock();
674 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
675 nfs_mark_return_unreferenced_delegations(server);
676 rcu_read_unlock();
677
Trond Myklebustb7391f42008-12-23 15:21:52 -0500678 nfs_delegation_run_state_manager(clp);
679}
680
Chuck Leverd3978bb2010-12-24 01:33:04 +0000681/**
682 * nfs_async_inode_return_delegation - asynchronously return a delegation
683 * @inode: inode to process
Trond Myklebust8e663f02012-03-04 18:13:56 -0500684 * @stateid: state ID information
Chuck Leverd3978bb2010-12-24 01:33:04 +0000685 *
686 * Returns zero on success, or a negative errno value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 */
Chuck Leverd3978bb2010-12-24 01:33:04 +0000688int nfs_async_inode_return_delegation(struct inode *inode,
689 const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Trond Myklebusted1e6212011-07-25 15:37:29 -0400691 struct nfs_server *server = NFS_SERVER(inode);
692 struct nfs_client *clp = server->nfs_client;
Trond Myklebust6411bd42008-12-23 15:21:51 -0500693 struct nfs_delegation *delegation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Trond Myklebustdc327ed2012-05-06 19:46:30 -0400695 filemap_flush(inode->i_mapping);
696
Trond Myklebust6411bd42008-12-23 15:21:51 -0500697 rcu_read_lock();
698 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebust755a48a2014-03-02 22:03:12 -0500699 if (delegation == NULL)
700 goto out_enoent;
Alexandros Batsakis25976412009-12-05 13:48:55 -0500701
Trond Myklebust755a48a2014-03-02 22:03:12 -0500702 if (!clp->cl_mvops->match_stateid(&delegation->stateid, stateid))
703 goto out_enoent;
Trond Myklebusted1e6212011-07-25 15:37:29 -0400704 nfs_mark_return_delegation(server, delegation);
Trond Myklebust6411bd42008-12-23 15:21:51 -0500705 rcu_read_unlock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000706
Trond Myklebust6411bd42008-12-23 15:21:51 -0500707 nfs_delegation_run_state_manager(clp);
708 return 0;
Trond Myklebust755a48a2014-03-02 22:03:12 -0500709out_enoent:
710 rcu_read_unlock();
711 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
Chuck Leverd3978bb2010-12-24 01:33:04 +0000714static struct inode *
715nfs_delegation_find_inode_server(struct nfs_server *server,
716 const struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717{
718 struct nfs_delegation *delegation;
719 struct inode *res = NULL;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000720
721 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
Trond Myklebust86e89482008-12-23 15:21:39 -0500722 spin_lock(&delegation->lock);
723 if (delegation->inode != NULL &&
724 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 res = igrab(delegation->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
Trond Myklebust86e89482008-12-23 15:21:39 -0500727 spin_unlock(&delegation->lock);
728 if (res != NULL)
729 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 }
Chuck Leverd3978bb2010-12-24 01:33:04 +0000731 return res;
732}
733
734/**
735 * nfs_delegation_find_inode - retrieve the inode associated with a delegation
736 * @clp: client state handle
737 * @fhandle: filehandle from a delegation recall
738 *
739 * Returns pointer to inode matching "fhandle," or NULL if a matching inode
740 * cannot be found.
741 */
742struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
743 const struct nfs_fh *fhandle)
744{
745 struct nfs_server *server;
746 struct inode *res = NULL;
747
748 rcu_read_lock();
749 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
750 res = nfs_delegation_find_inode_server(server, fhandle);
751 if (res != NULL)
752 break;
753 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400754 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 return res;
756}
757
Chuck Leverd3978bb2010-12-24 01:33:04 +0000758static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
759{
760 struct nfs_delegation *delegation;
761
762 list_for_each_entry_rcu(delegation, &server->delegations, super_list)
763 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
764}
765
766/**
767 * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
768 * @clp: nfs_client to process
769 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 */
David Howellsadfa6f92006-08-22 20:06:08 -0400771void nfs_delegation_mark_reclaim(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000773 struct nfs_server *server;
774
Trond Myklebust8383e462007-07-06 15:12:04 -0400775 rcu_read_lock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000776 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
777 nfs_delegation_mark_reclaim_server(server);
Trond Myklebust8383e462007-07-06 15:12:04 -0400778 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779}
780
Chuck Leverd3978bb2010-12-24 01:33:04 +0000781/**
782 * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
783 * @clp: nfs_client to process
784 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 */
David Howellsadfa6f92006-08-22 20:06:08 -0400786void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Trond Myklebust8383e462007-07-06 15:12:04 -0400788 struct nfs_delegation *delegation;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000789 struct nfs_server *server;
Trond Myklebust86e89482008-12-23 15:21:39 -0500790 struct inode *inode;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000791
Trond Myklebust8383e462007-07-06 15:12:04 -0400792restart:
793 rcu_read_lock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000794 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
795 list_for_each_entry_rcu(delegation, &server->delegations,
796 super_list) {
797 if (test_bit(NFS_DELEGATION_NEED_RECLAIM,
798 &delegation->flags) == 0)
799 continue;
800 inode = nfs_delegation_grab_inode(delegation);
801 if (inode == NULL)
802 continue;
803 delegation = nfs_detach_delegation(NFS_I(inode),
Trond Myklebustd25be542013-02-05 11:43:28 -0500804 delegation, server);
Chuck Leverd3978bb2010-12-24 01:33:04 +0000805 rcu_read_unlock();
806
807 if (delegation != NULL)
808 nfs_free_delegation(delegation);
809 iput(inode);
810 goto restart;
811 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400813 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500815
Chuck Leverd3978bb2010-12-24 01:33:04 +0000816/**
817 * nfs_delegations_present - check for existence of delegations
818 * @clp: client state handle
819 *
820 * Returns one if there are any nfs_delegation structures attached
821 * to this nfs_client.
822 */
823int nfs_delegations_present(struct nfs_client *clp)
824{
825 struct nfs_server *server;
826 int ret = 0;
827
828 rcu_read_lock();
829 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
830 if (!list_empty(&server->delegations)) {
831 ret = 1;
832 break;
833 }
834 rcu_read_unlock();
835 return ret;
836}
837
838/**
839 * nfs4_copy_delegation_stateid - Copy inode's state ID information
840 * @dst: stateid data structure to fill in
841 * @inode: inode to check
Trond Myklebust0032a7a2012-03-08 17:16:12 -0500842 * @flags: delegation type requirement
Chuck Leverd3978bb2010-12-24 01:33:04 +0000843 *
Trond Myklebust0032a7a2012-03-08 17:16:12 -0500844 * Returns "true" and fills in "dst->data" * if inode had a delegation,
845 * otherwise "false" is returned.
Chuck Leverd3978bb2010-12-24 01:33:04 +0000846 */
Trond Myklebust0032a7a2012-03-08 17:16:12 -0500847bool nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode,
848 fmode_t flags)
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500849{
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500850 struct nfs_inode *nfsi = NFS_I(inode);
851 struct nfs_delegation *delegation;
Trond Myklebust0032a7a2012-03-08 17:16:12 -0500852 bool ret;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500853
Trond Myklebust0032a7a2012-03-08 17:16:12 -0500854 flags &= FMODE_READ|FMODE_WRITE;
Trond Myklebust8383e462007-07-06 15:12:04 -0400855 rcu_read_lock();
856 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust0032a7a2012-03-08 17:16:12 -0500857 ret = (delegation != NULL && (delegation->type & flags) == flags);
858 if (ret) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500859 nfs4_stateid_copy(dst, &delegation->stateid);
Trond Myklebust0032a7a2012-03-08 17:16:12 -0500860 nfs_mark_delegation_referenced(delegation);
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500861 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400862 rcu_read_unlock();
863 return ret;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500864}