blob: 16b754ee0d0909a500b8e61a9967a39d08a14034 [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 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 Myklebustdb4f2e62013-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
Trond Myklebustcf6726e2014-12-19 11:22:28 -0500304static void
305nfs_update_inplace_delegation(struct nfs_delegation *delegation,
306 const struct nfs_delegation *update)
307{
308 if (nfs4_stateid_is_newer(&update->stateid, &delegation->stateid)) {
309 delegation->stateid.seqid = update->stateid.seqid;
310 smp_wmb();
311 delegation->type = update->type;
312 }
313}
314
Chuck Leverd3978bb2010-12-24 01:33:04 +0000315/**
316 * nfs_inode_set_delegation - set up a delegation on an inode
317 * @inode: inode to which delegation applies
318 * @cred: cred to use for subsequent delegation processing
319 * @res: new delegation state from server
320 *
321 * Returns zero on success, or a negative errno value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 */
323int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred, struct nfs_openres *res)
324{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000325 struct nfs_server *server = NFS_SERVER(inode);
326 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 struct nfs_inode *nfsi = NFS_I(inode);
David Howells17d2c0a2010-05-01 12:37:18 -0400328 struct nfs_delegation *delegation, *old_delegation;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500329 struct nfs_delegation *freeme = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 int status = 0;
331
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400332 delegation = kmalloc(sizeof(*delegation), GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 if (delegation == NULL)
334 return -ENOMEM;
Trond Myklebustf597c532012-03-04 18:13:56 -0500335 nfs4_stateid_copy(&delegation->stateid, &res->delegation);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 delegation->type = res->delegation_type;
337 delegation->maxsize = res->maxsize;
Trond Myklebusta9a4a872011-10-17 16:08:46 -0700338 delegation->change_attr = inode->i_version;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 delegation->cred = get_rpccred(cred);
340 delegation->inode = inode;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500341 delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
Trond Myklebust34310432008-12-23 15:21:38 -0500342 spin_lock_init(&delegation->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 spin_lock(&clp->cl_lock);
David Howells17d2c0a2010-05-01 12:37:18 -0400345 old_delegation = rcu_dereference_protected(nfsi->delegation,
Chuck Leverd3978bb2010-12-24 01:33:04 +0000346 lockdep_is_held(&clp->cl_lock));
David Howells17d2c0a2010-05-01 12:37:18 -0400347 if (old_delegation != NULL) {
Trond Myklebustcf6726e2014-12-19 11:22:28 -0500348 /* Is this an update of the existing delegation? */
349 if (nfs4_stateid_match_other(&old_delegation->stateid,
350 &delegation->stateid)) {
351 nfs_update_inplace_delegation(old_delegation,
352 delegation);
353 nfsi->delegation_state = old_delegation->type;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500354 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
Trond Myklebust57bfa892008-01-25 16:38:18 -0500356 /*
357 * Deal with broken servers that hand out two
358 * delegations for the same file.
Trond Myklebust17280172012-03-11 13:11:00 -0400359 * Allow for upgrades to a WRITE delegation, but
360 * nothing else.
Trond Myklebust57bfa892008-01-25 16:38:18 -0500361 */
362 dfprintk(FILE, "%s: server %s handed out "
363 "a duplicate delegation!\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700364 __func__, clp->cl_hostname);
Trond Myklebust17280172012-03-11 13:11:00 -0400365 if (delegation->type == old_delegation->type ||
366 !(delegation->type & FMODE_WRITE)) {
Trond Myklebust57bfa892008-01-25 16:38:18 -0500367 freeme = delegation;
368 delegation = NULL;
369 goto out;
370 }
Trond Myklebustd25be542013-02-05 11:43:28 -0500371 freeme = nfs_detach_delegation_locked(nfsi,
372 old_delegation, clp);
373 if (freeme == NULL)
374 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 }
Chuck Leverd3978bb2010-12-24 01:33:04 +0000376 list_add_rcu(&delegation->super_list, &server->delegations);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500377 nfsi->delegation_state = delegation->type;
378 rcu_assign_pointer(nfsi->delegation, delegation);
379 delegation = NULL;
Trond Myklebust412c77c2007-07-03 16:10:55 -0400380
381 /* Ensure we revalidate the attributes and page cache! */
382 spin_lock(&inode->i_lock);
383 nfsi->cache_validity |= NFS_INO_REVAL_FORCED;
384 spin_unlock(&inode->i_lock);
Trond Myklebustca8acf82013-08-13 10:36:56 -0400385 trace_nfs4_set_delegation(inode, res->delegation_type);
Trond Myklebust412c77c2007-07-03 16:10:55 -0400386
Trond Myklebust57bfa892008-01-25 16:38:18 -0500387out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 spin_unlock(&clp->cl_lock);
Trond Myklebust603c83d2007-10-18 19:59:20 -0400389 if (delegation != NULL)
390 nfs_free_delegation(delegation);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500391 if (freeme != NULL)
392 nfs_do_return_delegation(inode, freeme, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 return status;
394}
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396/*
397 * Basic procedure for returning a delegation to the server
398 */
Trond Myklebustd25be542013-02-05 11:43:28 -0500399static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
Trond Myklebustd25be542013-02-05 11:43:28 -0500401 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust869f9df2014-11-10 18:43:56 -0500403 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Trond Myklebustd25be542013-02-05 11:43:28 -0500405 if (delegation == NULL)
406 return 0;
407 do {
Trond Myklebust869f9df2014-11-10 18:43:56 -0500408 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
409 break;
Trond Myklebustd25be542013-02-05 11:43:28 -0500410 err = nfs_delegation_claim_opens(inode, &delegation->stateid);
411 if (!issync || err != -EAGAIN)
412 break;
413 /*
414 * Guard against state recovery
415 */
416 err = nfs4_wait_clnt_recover(clp);
417 } while (err == 0);
418
419 if (err) {
420 nfs_abort_delegation_return(delegation, clp);
421 goto out;
422 }
423 if (!nfs_detach_delegation(nfsi, delegation, NFS_SERVER(inode)))
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500424 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500426 err = nfs_do_return_delegation(inode, delegation, issync);
427out:
428 return err;
Trond Myklebust90163022007-07-05 14:55:18 -0400429}
430
Trond Myklebustb7571442013-04-03 14:33:49 -0400431static bool nfs_delegation_need_return(struct nfs_delegation *delegation)
432{
433 bool ret = false;
434
435 if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
436 ret = true;
437 if (test_and_clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags) && !ret) {
438 struct inode *inode;
439
440 spin_lock(&delegation->lock);
441 inode = delegation->inode;
442 if (inode && list_empty(&NFS_I(inode)->open_files))
443 ret = true;
444 spin_unlock(&delegation->lock);
445 }
446 return ret;
447}
448
Chuck Leverd3978bb2010-12-24 01:33:04 +0000449/**
450 * nfs_client_return_marked_delegations - return previously marked delegations
451 * @clp: nfs_client to process
452 *
Trond Myklebustdc327ed2012-05-06 19:46:30 -0400453 * Note that this function is designed to be called by the state
454 * manager thread. For this reason, it cannot flush the dirty data,
455 * since that could deadlock in case of a state recovery error.
456 *
Chuck Leverd3978bb2010-12-24 01:33:04 +0000457 * Returns zero on success, or a negative errno value.
Trond Myklebust515d8612008-12-23 15:21:46 -0500458 */
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500459int nfs_client_return_marked_delegations(struct nfs_client *clp)
Trond Myklebust515d8612008-12-23 15:21:46 -0500460{
461 struct nfs_delegation *delegation;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000462 struct nfs_server *server;
Trond Myklebust515d8612008-12-23 15:21:46 -0500463 struct inode *inode;
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500464 int err = 0;
Trond Myklebust515d8612008-12-23 15:21:46 -0500465
466restart:
467 rcu_read_lock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000468 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
469 list_for_each_entry_rcu(delegation, &server->delegations,
470 super_list) {
Trond Myklebustb7571442013-04-03 14:33:49 -0400471 if (!nfs_delegation_need_return(delegation))
Chuck Leverd3978bb2010-12-24 01:33:04 +0000472 continue;
473 inode = nfs_delegation_grab_inode(delegation);
474 if (inode == NULL)
475 continue;
Trond Myklebustd25be542013-02-05 11:43:28 -0500476 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
Chuck Leverd3978bb2010-12-24 01:33:04 +0000477 rcu_read_unlock();
478
Trond Myklebustd25be542013-02-05 11:43:28 -0500479 err = nfs_end_delegation_return(inode, delegation, 0);
Chuck Leverd3978bb2010-12-24 01:33:04 +0000480 iput(inode);
481 if (!err)
482 goto restart;
483 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
484 return err;
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500485 }
Trond Myklebust515d8612008-12-23 15:21:46 -0500486 }
487 rcu_read_unlock();
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500488 return 0;
Trond Myklebust515d8612008-12-23 15:21:46 -0500489}
490
Chuck Leverd3978bb2010-12-24 01:33:04 +0000491/**
492 * nfs_inode_return_delegation_noreclaim - return delegation, don't reclaim opens
493 * @inode: inode to process
494 *
495 * Does not protect against delegation reclaims, therefore really only safe
496 * to be called from nfs4_clear_inode().
Trond Myklebuste6f81072008-01-24 18:14:34 -0500497 */
498void nfs_inode_return_delegation_noreclaim(struct inode *inode)
499{
Trond Myklebuste6f81072008-01-24 18:14:34 -0500500 struct nfs_delegation *delegation;
501
Trond Myklebustd25be542013-02-05 11:43:28 -0500502 delegation = nfs_inode_detach_delegation(inode);
503 if (delegation != NULL)
504 nfs_do_return_delegation(inode, delegation, 0);
Trond Myklebuste6f81072008-01-24 18:14:34 -0500505}
506
Chuck Leverd3978bb2010-12-24 01:33:04 +0000507/**
508 * nfs_inode_return_delegation - synchronously return a delegation
509 * @inode: inode to process
510 *
Trond Myklebustc57d1bc2012-05-06 19:34:17 -0400511 * This routine will always flush any dirty data to disk on the
512 * assumption that if we need to return the delegation, then
513 * we should stop caching.
514 *
Chuck Leverd3978bb2010-12-24 01:33:04 +0000515 * Returns zero on success, or a negative errno value.
516 */
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400517int nfs4_inode_return_delegation(struct inode *inode)
Trond Myklebust90163022007-07-05 14:55:18 -0400518{
Trond Myklebust90163022007-07-05 14:55:18 -0400519 struct nfs_inode *nfsi = NFS_I(inode);
520 struct nfs_delegation *delegation;
521 int err = 0;
522
Trond Myklebustc57d1bc2012-05-06 19:34:17 -0400523 nfs_wb_all(inode);
Trond Myklebustd25be542013-02-05 11:43:28 -0500524 delegation = nfs_start_delegation_return(nfsi);
525 if (delegation != NULL)
526 err = nfs_end_delegation_return(inode, delegation, 1);
Trond Myklebust90163022007-07-05 14:55:18 -0400527 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528}
529
Trond Myklebustb7571442013-04-03 14:33:49 -0400530static void nfs_mark_return_if_closed_delegation(struct nfs_server *server,
531 struct nfs_delegation *delegation)
532{
533 set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
534 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
535}
536
Trond Myklebusted1e62112011-07-25 15:37:29 -0400537static void nfs_mark_return_delegation(struct nfs_server *server,
538 struct nfs_delegation *delegation)
Trond Myklebust6411bd42008-12-23 15:21:51 -0500539{
540 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
Trond Myklebusted1e62112011-07-25 15:37:29 -0400541 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
Trond Myklebust6411bd42008-12-23 15:21:51 -0500542}
543
Trond Myklebust5c31e232013-04-03 19:04:58 -0400544static bool nfs_server_mark_return_all_delegations(struct nfs_server *server)
545{
546 struct nfs_delegation *delegation;
547 bool ret = false;
548
549 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
550 nfs_mark_return_delegation(server, delegation);
551 ret = true;
552 }
553 return ret;
554}
555
Trond Myklebustb02ba0b2013-04-03 19:23:58 -0400556static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
557{
558 struct nfs_server *server;
559
560 rcu_read_lock();
561 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
562 nfs_server_mark_return_all_delegations(server);
563 rcu_read_unlock();
564}
565
566static void nfs_delegation_run_state_manager(struct nfs_client *clp)
567{
568 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
569 nfs4_schedule_state_manager(clp);
570}
571
572/**
573 * nfs_expire_all_delegations
574 * @clp: client to process
575 *
576 */
577void nfs_expire_all_delegations(struct nfs_client *clp)
578{
579 nfs_client_mark_return_all_delegations(clp);
580 nfs_delegation_run_state_manager(clp);
581}
582
Chuck Leverd3978bb2010-12-24 01:33:04 +0000583/**
584 * nfs_super_return_all_delegations - return delegations for one superblock
585 * @sb: sb to process
586 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 */
Bryan Schumakereeebf912012-06-20 15:53:41 -0400588void nfs_server_return_all_delegations(struct nfs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000590 struct nfs_client *clp = server->nfs_client;
Trond Myklebust5c31e232013-04-03 19:04:58 -0400591 bool need_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 if (clp == NULL)
594 return;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000595
Trond Myklebust8383e462007-07-06 15:12:04 -0400596 rcu_read_lock();
Trond Myklebust5c31e232013-04-03 19:04:58 -0400597 need_wait = nfs_server_mark_return_all_delegations(server);
Trond Myklebust8383e462007-07-06 15:12:04 -0400598 rcu_read_unlock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000599
Trond Myklebust5c31e232013-04-03 19:04:58 -0400600 if (need_wait) {
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500601 nfs4_schedule_state_manager(clp);
Trond Myklebust5c31e232013-04-03 19:04:58 -0400602 nfs4_wait_clnt_recover(clp);
603 }
Trond Myklebust515d8612008-12-23 15:21:46 -0500604}
605
Trond Myklebust826e0012013-04-03 19:27:52 -0400606static void nfs_mark_return_unused_delegation_types(struct nfs_server *server,
Chuck Leverd3978bb2010-12-24 01:33:04 +0000607 fmode_t flags)
Trond Myklebust515d8612008-12-23 15:21:46 -0500608{
609 struct nfs_delegation *delegation;
610
Chuck Leverd3978bb2010-12-24 01:33:04 +0000611 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500612 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
613 continue;
614 if (delegation->type & flags)
Trond Myklebust826e0012013-04-03 19:27:52 -0400615 nfs_mark_return_if_closed_delegation(server, delegation);
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500616 }
Chuck Leverd3978bb2010-12-24 01:33:04 +0000617}
618
Trond Myklebust826e0012013-04-03 19:27:52 -0400619static void nfs_client_mark_return_unused_delegation_types(struct nfs_client *clp,
Chuck Leverd3978bb2010-12-24 01:33:04 +0000620 fmode_t flags)
621{
622 struct nfs_server *server;
623
624 rcu_read_lock();
625 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
Trond Myklebust826e0012013-04-03 19:27:52 -0400626 nfs_mark_return_unused_delegation_types(server, flags);
Trond Myklebust515d8612008-12-23 15:21:46 -0500627 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628}
629
Trond Myklebust869f9df2014-11-10 18:43:56 -0500630static void nfs_revoke_delegation(struct inode *inode)
631{
632 struct nfs_delegation *delegation;
633 rcu_read_lock();
634 delegation = rcu_dereference(NFS_I(inode)->delegation);
635 if (delegation != NULL) {
636 set_bit(NFS_DELEGATION_REVOKED, &delegation->flags);
637 nfs_mark_return_delegation(NFS_SERVER(inode), delegation);
638 }
639 rcu_read_unlock();
640}
641
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500642void nfs_remove_bad_delegation(struct inode *inode)
643{
644 struct nfs_delegation *delegation;
645
Trond Myklebust869f9df2014-11-10 18:43:56 -0500646 nfs_revoke_delegation(inode);
Trond Myklebustd25be542013-02-05 11:43:28 -0500647 delegation = nfs_inode_detach_delegation(inode);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500648 if (delegation) {
649 nfs_inode_find_state_and_recover(inode, &delegation->stateid);
650 nfs_free_delegation(delegation);
651 }
652}
Andy Adamson9cb81962012-03-07 10:49:41 -0500653EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500654
Chuck Leverd3978bb2010-12-24 01:33:04 +0000655/**
Trond Myklebust826e0012013-04-03 19:27:52 -0400656 * nfs_expire_unused_delegation_types
Chuck Leverd3978bb2010-12-24 01:33:04 +0000657 * @clp: client to process
658 * @flags: delegation types to expire
659 *
660 */
Trond Myklebust826e0012013-04-03 19:27:52 -0400661void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags)
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500662{
Trond Myklebust826e0012013-04-03 19:27:52 -0400663 nfs_client_mark_return_unused_delegation_types(clp, flags);
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500664 nfs_delegation_run_state_manager(clp);
665}
666
Chuck Leverd3978bb2010-12-24 01:33:04 +0000667static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
Trond Myklebustb7391f42008-12-23 15:21:52 -0500668{
669 struct nfs_delegation *delegation;
670
Chuck Leverd3978bb2010-12-24 01:33:04 +0000671 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
Trond Myklebustb7391f42008-12-23 15:21:52 -0500672 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
673 continue;
Trond Myklebustb7571442013-04-03 14:33:49 -0400674 nfs_mark_return_if_closed_delegation(server, delegation);
Trond Myklebustb7391f42008-12-23 15:21:52 -0500675 }
Trond Myklebustb7391f42008-12-23 15:21:52 -0500676}
677
Chuck Leverd3978bb2010-12-24 01:33:04 +0000678/**
679 * nfs_expire_unreferenced_delegations - Eliminate unused delegations
680 * @clp: nfs_client to process
681 *
682 */
Trond Myklebustb7391f42008-12-23 15:21:52 -0500683void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
684{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000685 struct nfs_server *server;
686
687 rcu_read_lock();
688 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
689 nfs_mark_return_unreferenced_delegations(server);
690 rcu_read_unlock();
691
Trond Myklebustb7391f42008-12-23 15:21:52 -0500692 nfs_delegation_run_state_manager(clp);
693}
694
Chuck Leverd3978bb2010-12-24 01:33:04 +0000695/**
696 * nfs_async_inode_return_delegation - asynchronously return a delegation
697 * @inode: inode to process
Trond Myklebust8e663f02012-03-04 18:13:56 -0500698 * @stateid: state ID information
Chuck Leverd3978bb2010-12-24 01:33:04 +0000699 *
700 * Returns zero on success, or a negative errno value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 */
Chuck Leverd3978bb2010-12-24 01:33:04 +0000702int nfs_async_inode_return_delegation(struct inode *inode,
703 const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
Trond Myklebusted1e62112011-07-25 15:37:29 -0400705 struct nfs_server *server = NFS_SERVER(inode);
706 struct nfs_client *clp = server->nfs_client;
Trond Myklebust6411bd42008-12-23 15:21:51 -0500707 struct nfs_delegation *delegation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
Trond Myklebustdc327ed2012-05-06 19:46:30 -0400709 filemap_flush(inode->i_mapping);
710
Trond Myklebust6411bd42008-12-23 15:21:51 -0500711 rcu_read_lock();
712 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebust755a48a2014-03-02 22:03:12 -0500713 if (delegation == NULL)
714 goto out_enoent;
Alexandros Batsakis25976412009-12-05 13:48:55 -0500715
Trond Myklebust755a48a2014-03-02 22:03:12 -0500716 if (!clp->cl_mvops->match_stateid(&delegation->stateid, stateid))
717 goto out_enoent;
Trond Myklebusted1e62112011-07-25 15:37:29 -0400718 nfs_mark_return_delegation(server, delegation);
Trond Myklebust6411bd42008-12-23 15:21:51 -0500719 rcu_read_unlock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000720
Trond Myklebust6411bd42008-12-23 15:21:51 -0500721 nfs_delegation_run_state_manager(clp);
722 return 0;
Trond Myklebust755a48a2014-03-02 22:03:12 -0500723out_enoent:
724 rcu_read_unlock();
725 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726}
727
Chuck Leverd3978bb2010-12-24 01:33:04 +0000728static struct inode *
729nfs_delegation_find_inode_server(struct nfs_server *server,
730 const struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
732 struct nfs_delegation *delegation;
733 struct inode *res = NULL;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000734
735 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
Trond Myklebust86e89482008-12-23 15:21:39 -0500736 spin_lock(&delegation->lock);
737 if (delegation->inode != NULL &&
738 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 res = igrab(delegation->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 }
Trond Myklebust86e89482008-12-23 15:21:39 -0500741 spin_unlock(&delegation->lock);
742 if (res != NULL)
743 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 }
Chuck Leverd3978bb2010-12-24 01:33:04 +0000745 return res;
746}
747
748/**
749 * nfs_delegation_find_inode - retrieve the inode associated with a delegation
750 * @clp: client state handle
751 * @fhandle: filehandle from a delegation recall
752 *
753 * Returns pointer to inode matching "fhandle," or NULL if a matching inode
754 * cannot be found.
755 */
756struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
757 const struct nfs_fh *fhandle)
758{
759 struct nfs_server *server;
760 struct inode *res = NULL;
761
762 rcu_read_lock();
763 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
764 res = nfs_delegation_find_inode_server(server, fhandle);
765 if (res != NULL)
766 break;
767 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400768 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 return res;
770}
771
Chuck Leverd3978bb2010-12-24 01:33:04 +0000772static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
773{
774 struct nfs_delegation *delegation;
775
776 list_for_each_entry_rcu(delegation, &server->delegations, super_list)
777 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
778}
779
780/**
781 * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
782 * @clp: nfs_client to process
783 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 */
David Howellsadfa6f92006-08-22 20:06:08 -0400785void nfs_delegation_mark_reclaim(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000787 struct nfs_server *server;
788
Trond Myklebust8383e462007-07-06 15:12:04 -0400789 rcu_read_lock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000790 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
791 nfs_delegation_mark_reclaim_server(server);
Trond Myklebust8383e462007-07-06 15:12:04 -0400792 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793}
794
Chuck Leverd3978bb2010-12-24 01:33:04 +0000795/**
796 * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
797 * @clp: nfs_client to process
798 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 */
David Howellsadfa6f92006-08-22 20:06:08 -0400800void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801{
Trond Myklebust8383e462007-07-06 15:12:04 -0400802 struct nfs_delegation *delegation;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000803 struct nfs_server *server;
Trond Myklebust86e89482008-12-23 15:21:39 -0500804 struct inode *inode;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000805
Trond Myklebust8383e462007-07-06 15:12:04 -0400806restart:
807 rcu_read_lock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000808 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
809 list_for_each_entry_rcu(delegation, &server->delegations,
810 super_list) {
811 if (test_bit(NFS_DELEGATION_NEED_RECLAIM,
812 &delegation->flags) == 0)
813 continue;
814 inode = nfs_delegation_grab_inode(delegation);
815 if (inode == NULL)
816 continue;
817 delegation = nfs_detach_delegation(NFS_I(inode),
Trond Myklebustd25be542013-02-05 11:43:28 -0500818 delegation, server);
Chuck Leverd3978bb2010-12-24 01:33:04 +0000819 rcu_read_unlock();
820
821 if (delegation != NULL)
822 nfs_free_delegation(delegation);
823 iput(inode);
824 goto restart;
825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400827 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828}
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500829
Chuck Leverd3978bb2010-12-24 01:33:04 +0000830/**
831 * nfs_delegations_present - check for existence of delegations
832 * @clp: client state handle
833 *
834 * Returns one if there are any nfs_delegation structures attached
835 * to this nfs_client.
836 */
837int nfs_delegations_present(struct nfs_client *clp)
838{
839 struct nfs_server *server;
840 int ret = 0;
841
842 rcu_read_lock();
843 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
844 if (!list_empty(&server->delegations)) {
845 ret = 1;
846 break;
847 }
848 rcu_read_unlock();
849 return ret;
850}
851
852/**
853 * nfs4_copy_delegation_stateid - Copy inode's state ID information
854 * @dst: stateid data structure to fill in
855 * @inode: inode to check
Trond Myklebust0032a7a2012-03-08 17:16:12 -0500856 * @flags: delegation type requirement
Chuck Leverd3978bb2010-12-24 01:33:04 +0000857 *
Trond Myklebust0032a7a2012-03-08 17:16:12 -0500858 * Returns "true" and fills in "dst->data" * if inode had a delegation,
859 * otherwise "false" is returned.
Chuck Leverd3978bb2010-12-24 01:33:04 +0000860 */
Trond Myklebust0032a7a2012-03-08 17:16:12 -0500861bool nfs4_copy_delegation_stateid(nfs4_stateid *dst, struct inode *inode,
862 fmode_t flags)
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500863{
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500864 struct nfs_inode *nfsi = NFS_I(inode);
865 struct nfs_delegation *delegation;
Trond Myklebust0032a7a2012-03-08 17:16:12 -0500866 bool ret;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500867
Trond Myklebust0032a7a2012-03-08 17:16:12 -0500868 flags &= FMODE_READ|FMODE_WRITE;
Trond Myklebust8383e462007-07-06 15:12:04 -0400869 rcu_read_lock();
870 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust0032a7a2012-03-08 17:16:12 -0500871 ret = (delegation != NULL && (delegation->type & flags) == flags);
872 if (ret) {
Trond Myklebustf597c532012-03-04 18:13:56 -0500873 nfs4_stateid_copy(dst, &delegation->stateid);
Trond Myklebust0032a7a2012-03-08 17:16:12 -0500874 nfs_mark_delegation_referenced(delegation);
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500875 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400876 rcu_read_unlock();
877 return ret;
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500878}