blob: 76d205d1c7bccad2fdd39300ed92e27f58bece0d [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>
Jeff Layton1eb5d982018-01-09 08:21:17 -050015#include <linux/iversion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17#include <linux/nfs4.h>
18#include <linux/nfs_fs.h>
19#include <linux/nfs_xdr.h>
20
Trond Myklebust4ce79712005-06-22 17:16:21 +000021#include "nfs4_fs.h"
Trond Myklebustc01d3642018-03-20 16:43:20 -040022#include "nfs4session.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "delegation.h"
David Howells24c8dbb2006-08-22 20:06:10 -040024#include "internal.h"
Trond Myklebustca8acf82013-08-13 10:36:56 -040025#include "nfs4trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Trond Myklebust905f8d12007-08-06 12:18:34 -040027static void nfs_free_delegation(struct nfs_delegation *delegation)
28{
Trond Myklebuste00b8a22011-01-27 14:55:39 -050029 if (delegation->cred) {
30 put_rpccred(delegation->cred);
31 delegation->cred = NULL;
32 }
Lai Jiangshan26f04dd2011-05-01 06:21:54 -070033 kfree_rcu(delegation, rcu);
Trond Myklebust8383e462007-07-06 15:12:04 -040034}
35
Chuck Leverd3978bb2010-12-24 01:33:04 +000036/**
37 * nfs_mark_delegation_referenced - set delegation's REFERENCED flag
38 * @delegation: delegation to process
39 *
40 */
Trond Myklebustb7391f42008-12-23 15:21:52 -050041void nfs_mark_delegation_referenced(struct nfs_delegation *delegation)
42{
43 set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags);
44}
45
Trond Myklebustaa05c872016-09-22 13:38:54 -040046static bool
47nfs4_is_valid_delegation(const struct nfs_delegation *delegation,
48 fmode_t flags)
49{
50 if (delegation != NULL && (delegation->type & flags) == flags &&
51 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) &&
52 !test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
53 return true;
54 return false;
55}
56
Peng Tao15bb3af2014-07-03 13:05:00 +080057static int
58nfs4_do_check_delegation(struct inode *inode, fmode_t flags, bool mark)
Trond Myklebustb7391f42008-12-23 15:21:52 -050059{
60 struct nfs_delegation *delegation;
61 int ret = 0;
62
63 flags &= FMODE_READ|FMODE_WRITE;
64 rcu_read_lock();
65 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustaa05c872016-09-22 13:38:54 -040066 if (nfs4_is_valid_delegation(delegation, flags)) {
Peng Tao15bb3af2014-07-03 13:05:00 +080067 if (mark)
68 nfs_mark_delegation_referenced(delegation);
Trond Myklebustb7391f42008-12-23 15:21:52 -050069 ret = 1;
70 }
71 rcu_read_unlock();
72 return ret;
73}
Peng Tao15bb3af2014-07-03 13:05:00 +080074/**
75 * nfs_have_delegation - check if inode has a delegation, mark it
76 * NFS_DELEGATION_REFERENCED if there is one.
77 * @inode: inode to check
78 * @flags: delegation types to check for
79 *
80 * Returns one if inode has the indicated delegation, otherwise zero.
81 */
82int nfs4_have_delegation(struct inode *inode, fmode_t flags)
83{
84 return nfs4_do_check_delegation(inode, flags, true);
85}
86
87/*
88 * nfs4_check_delegation - check if inode has a delegation, do not mark
89 * NFS_DELEGATION_REFERENCED if it has one.
90 */
91int nfs4_check_delegation(struct inode *inode, fmode_t flags)
92{
93 return nfs4_do_check_delegation(inode, flags, false);
94}
Trond Myklebustb7391f42008-12-23 15:21:52 -050095
Trond Myklebustdb4f2e62013-04-01 15:56:46 -040096static 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 -050097{
98 struct inode *inode = state->inode;
99 struct file_lock *fl;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500100 struct file_lock_context *flctx = inode->i_flctx;
101 struct list_head *list;
Trond Myklebustd5122202009-06-17 13:22:58 -0700102 int status = 0;
Trond Myklebust888e6942005-11-04 15:38:11 -0500103
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500104 if (flctx == NULL)
Trond Myklebust65b62a22013-02-07 10:54:07 -0500105 goto out;
Jeff Layton314d7cc2013-04-10 15:36:48 -0400106
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500107 list = &flctx->flc_posix;
Jeff Layton6109c852015-01-16 15:05:57 -0500108 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500109restart:
110 list_for_each_entry(fl, list, fl_list) {
Trond Myklebustcd3758e2007-08-10 17:44:32 -0400111 if (nfs_file_open_context(fl->fl_file) != ctx)
Trond Myklebust888e6942005-11-04 15:38:11 -0500112 continue;
Jeff Layton6109c852015-01-16 15:05:57 -0500113 spin_unlock(&flctx->flc_lock);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -0400114 status = nfs4_lock_delegation_recall(fl, state, stateid);
Trond Myklebustd5122202009-06-17 13:22:58 -0700115 if (status < 0)
Trond Myklebust3f09df72009-06-17 13:23:00 -0700116 goto out;
Jeff Layton6109c852015-01-16 15:05:57 -0500117 spin_lock(&flctx->flc_lock);
Trond Myklebust888e6942005-11-04 15:38:11 -0500118 }
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500119 if (list == &flctx->flc_posix) {
120 list = &flctx->flc_flock;
121 goto restart;
Jeff Layton5263e312015-01-16 15:05:55 -0500122 }
Jeff Layton6109c852015-01-16 15:05:57 -0500123 spin_unlock(&flctx->flc_lock);
Trond Myklebust3f09df72009-06-17 13:23:00 -0700124out:
Trond Myklebust888e6942005-11-04 15:38:11 -0500125 return status;
126}
127
Trond Myklebust24311f82015-09-20 10:50:17 -0400128static int nfs_delegation_claim_opens(struct inode *inode,
129 const nfs4_stateid *stateid, fmode_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 struct nfs_inode *nfsi = NFS_I(inode);
132 struct nfs_open_context *ctx;
Trond Myklebustd25be542013-02-05 11:43:28 -0500133 struct nfs4_state_owner *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 struct nfs4_state *state;
Trond Myklebustd25be542013-02-05 11:43:28 -0500135 unsigned int seq;
Trond Myklebust888e6942005-11-04 15:38:11 -0500136 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138again:
Trond Myklebust0de43972018-09-02 15:57:01 -0400139 rcu_read_lock();
140 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 state = ctx->state;
142 if (state == NULL)
143 continue;
144 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
145 continue;
Trond Myklebustf8ebf7a2014-10-17 23:02:52 +0300146 if (!nfs4_valid_open_stateid(state))
147 continue;
Trond Myklebustf597c532012-03-04 18:13:56 -0500148 if (!nfs4_stateid_match(&state->stateid, stateid))
Trond Myklebust90163022007-07-05 14:55:18 -0400149 continue;
Trond Myklebust0de43972018-09-02 15:57:01 -0400150 if (!get_nfs_open_context(ctx))
151 continue;
152 rcu_read_unlock();
Trond Myklebustd25be542013-02-05 11:43:28 -0500153 sp = state->owner;
Trond Myklebust65b62a22013-02-07 10:54:07 -0500154 /* Block nfs4_proc_unlck */
155 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebustd25be542013-02-05 11:43:28 -0500156 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
Trond Myklebust24311f82015-09-20 10:50:17 -0400157 err = nfs4_open_delegation_recall(ctx, state, stateid, type);
Trond Myklebustd25be542013-02-05 11:43:28 -0500158 if (!err)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -0400159 err = nfs_delegation_claim_locks(ctx, state, stateid);
Trond Myklebustd25be542013-02-05 11:43:28 -0500160 if (!err && read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
161 err = -EAGAIN;
Trond Myklebust65b62a22013-02-07 10:54:07 -0500162 mutex_unlock(&sp->so_delegreturn_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 put_nfs_open_context(ctx);
Trond Myklebust888e6942005-11-04 15:38:11 -0500164 if (err != 0)
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500165 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 goto again;
167 }
Trond Myklebust0de43972018-09-02 15:57:01 -0400168 rcu_read_unlock();
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500169 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
Chuck Leverd3978bb2010-12-24 01:33:04 +0000172/**
173 * nfs_inode_reclaim_delegation - process a delegation reclaim request
174 * @inode: inode to process
175 * @cred: credential to use for request
Trond Myklebust35156bf2018-03-20 17:03:13 -0400176 * @type: delegation type
177 * @stateid: delegation stateid
178 * @pagemod_limit: write delegation "space_limit"
Chuck Leverd3978bb2010-12-24 01:33:04 +0000179 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 */
Chuck Leverd3978bb2010-12-24 01:33:04 +0000181void nfs_inode_reclaim_delegation(struct inode *inode, struct rpc_cred *cred,
Trond Myklebust35156bf2018-03-20 17:03:13 -0400182 fmode_t type,
183 const nfs4_stateid *stateid,
184 unsigned long pagemod_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Trond Myklebust8f649c32010-05-01 12:36:18 -0400186 struct nfs_delegation *delegation;
187 struct rpc_cred *oldcred = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Trond Myklebust8f649c32010-05-01 12:36:18 -0400189 rcu_read_lock();
190 delegation = rcu_dereference(NFS_I(inode)->delegation);
191 if (delegation != NULL) {
192 spin_lock(&delegation->lock);
193 if (delegation->inode != NULL) {
Trond Myklebust35156bf2018-03-20 17:03:13 -0400194 nfs4_stateid_copy(&delegation->stateid, stateid);
195 delegation->type = type;
196 delegation->pagemod_limit = pagemod_limit;
Trond Myklebust8f649c32010-05-01 12:36:18 -0400197 oldcred = delegation->cred;
198 delegation->cred = get_rpccred(cred);
199 clear_bit(NFS_DELEGATION_NEED_RECLAIM,
200 &delegation->flags);
Trond Myklebust8f649c32010-05-01 12:36:18 -0400201 spin_unlock(&delegation->lock);
Trond Myklebust8f649c32010-05-01 12:36:18 -0400202 rcu_read_unlock();
Trond Myklebust7c0af9f2015-02-26 12:54:46 -0500203 put_rpccred(oldcred);
Trond Myklebust35156bf2018-03-20 17:03:13 -0400204 trace_nfs4_reclaim_delegation(inode, type);
Trond Myklebustb1a318d2016-09-22 13:39:12 -0400205 return;
Trond Myklebust8f649c32010-05-01 12:36:18 -0400206 }
Trond Myklebustb1a318d2016-09-22 13:39:12 -0400207 /* We appear to have raced with a delegation return. */
208 spin_unlock(&delegation->lock);
Trond Myklebust8f649c32010-05-01 12:36:18 -0400209 }
Trond Myklebustb1a318d2016-09-22 13:39:12 -0400210 rcu_read_unlock();
Trond Myklebust35156bf2018-03-20 17:03:13 -0400211 nfs_inode_set_delegation(inode, cred, type, stateid, pagemod_limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
Trond Myklebust57bfa892008-01-25 16:38:18 -0500214static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
215{
216 int res = 0;
217
Trond Myklebust869f9df2014-11-10 18:43:56 -0500218 if (!test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
219 res = nfs4_proc_delegreturn(inode,
220 delegation->cred,
221 &delegation->stateid,
222 issync);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500223 nfs_free_delegation(delegation);
224 return res;
225}
226
Trond Myklebust86e89482008-12-23 15:21:39 -0500227static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
228{
229 struct inode *inode = NULL;
230
231 spin_lock(&delegation->lock);
232 if (delegation->inode != NULL)
233 inode = igrab(delegation->inode);
234 spin_unlock(&delegation->lock);
235 return inode;
236}
237
Chuck Leverdda4b222010-12-24 01:32:54 +0000238static struct nfs_delegation *
Trond Myklebustd25be542013-02-05 11:43:28 -0500239nfs_start_delegation_return_locked(struct nfs_inode *nfsi)
Trond Myklebust57bfa892008-01-25 16:38:18 -0500240{
Trond Myklebustd25be542013-02-05 11:43:28 -0500241 struct nfs_delegation *ret = NULL;
242 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500243
244 if (delegation == NULL)
Trond Myklebustd25be542013-02-05 11:43:28 -0500245 goto out;
246 spin_lock(&delegation->lock);
247 if (!test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
248 ret = delegation;
249 spin_unlock(&delegation->lock);
250out:
251 return ret;
252}
253
254static struct nfs_delegation *
255nfs_start_delegation_return(struct nfs_inode *nfsi)
256{
257 struct nfs_delegation *delegation;
258
259 rcu_read_lock();
260 delegation = nfs_start_delegation_return_locked(nfsi);
261 rcu_read_unlock();
262 return delegation;
263}
264
265static void
266nfs_abort_delegation_return(struct nfs_delegation *delegation,
267 struct nfs_client *clp)
268{
Chuck Leverdda4b222010-12-24 01:32:54 +0000269
Trond Myklebust34310432008-12-23 15:21:38 -0500270 spin_lock(&delegation->lock);
Trond Myklebustd25be542013-02-05 11:43:28 -0500271 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
272 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
273 spin_unlock(&delegation->lock);
274 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
275}
276
277static struct nfs_delegation *
278nfs_detach_delegation_locked(struct nfs_inode *nfsi,
279 struct nfs_delegation *delegation,
280 struct nfs_client *clp)
281{
282 struct nfs_delegation *deleg_cur =
283 rcu_dereference_protected(nfsi->delegation,
284 lockdep_is_held(&clp->cl_lock));
285
286 if (deleg_cur == NULL || delegation != deleg_cur)
287 return NULL;
288
289 spin_lock(&delegation->lock);
290 set_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500291 list_del_rcu(&delegation->super_list);
Trond Myklebust86e89482008-12-23 15:21:39 -0500292 delegation->inode = NULL;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500293 rcu_assign_pointer(nfsi->delegation, NULL);
Trond Myklebust34310432008-12-23 15:21:38 -0500294 spin_unlock(&delegation->lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500295 return delegation;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500296}
297
Chuck Leverdda4b222010-12-24 01:32:54 +0000298static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi,
Trond Myklebustd25be542013-02-05 11:43:28 -0500299 struct nfs_delegation *delegation,
300 struct nfs_server *server)
Chuck Leverdda4b222010-12-24 01:32:54 +0000301{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000302 struct nfs_client *clp = server->nfs_client;
Chuck Leverdda4b222010-12-24 01:32:54 +0000303
304 spin_lock(&clp->cl_lock);
Trond Myklebustd25be542013-02-05 11:43:28 -0500305 delegation = nfs_detach_delegation_locked(nfsi, delegation, clp);
Chuck Leverdda4b222010-12-24 01:32:54 +0000306 spin_unlock(&clp->cl_lock);
307 return delegation;
308}
309
Trond Myklebustd25be542013-02-05 11:43:28 -0500310static struct nfs_delegation *
311nfs_inode_detach_delegation(struct inode *inode)
312{
313 struct nfs_inode *nfsi = NFS_I(inode);
314 struct nfs_server *server = NFS_SERVER(inode);
315 struct nfs_delegation *delegation;
316
317 delegation = nfs_start_delegation_return(nfsi);
318 if (delegation == NULL)
319 return NULL;
320 return nfs_detach_delegation(nfsi, delegation, server);
321}
322
Trond Myklebustcf6726e2014-12-19 11:22:28 -0500323static void
324nfs_update_inplace_delegation(struct nfs_delegation *delegation,
325 const struct nfs_delegation *update)
326{
327 if (nfs4_stateid_is_newer(&update->stateid, &delegation->stateid)) {
328 delegation->stateid.seqid = update->stateid.seqid;
329 smp_wmb();
330 delegation->type = update->type;
331 }
332}
333
Chuck Leverd3978bb2010-12-24 01:33:04 +0000334/**
335 * nfs_inode_set_delegation - set up a delegation on an inode
336 * @inode: inode to which delegation applies
337 * @cred: cred to use for subsequent delegation processing
Trond Myklebust35156bf2018-03-20 17:03:13 -0400338 * @type: delegation type
339 * @stateid: delegation stateid
340 * @pagemod_limit: write delegation "space_limit"
Chuck Leverd3978bb2010-12-24 01:33:04 +0000341 *
342 * Returns zero on success, or a negative errno value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 */
Trond Myklebust35156bf2018-03-20 17:03:13 -0400344int nfs_inode_set_delegation(struct inode *inode, struct rpc_cred *cred,
345 fmode_t type,
346 const nfs4_stateid *stateid,
347 unsigned long pagemod_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000349 struct nfs_server *server = NFS_SERVER(inode);
350 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 struct nfs_inode *nfsi = NFS_I(inode);
David Howells17d2c0a2010-05-01 12:37:18 -0400352 struct nfs_delegation *delegation, *old_delegation;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500353 struct nfs_delegation *freeme = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 int status = 0;
355
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400356 delegation = kmalloc(sizeof(*delegation), GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 if (delegation == NULL)
358 return -ENOMEM;
Trond Myklebust35156bf2018-03-20 17:03:13 -0400359 nfs4_stateid_copy(&delegation->stateid, stateid);
360 delegation->type = type;
361 delegation->pagemod_limit = pagemod_limit;
Jeff Layton1eb5d982018-01-09 08:21:17 -0500362 delegation->change_attr = inode_peek_iversion_raw(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 delegation->cred = get_rpccred(cred);
364 delegation->inode = inode;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500365 delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
Trond Myklebust34310432008-12-23 15:21:38 -0500366 spin_lock_init(&delegation->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
368 spin_lock(&clp->cl_lock);
David Howells17d2c0a2010-05-01 12:37:18 -0400369 old_delegation = rcu_dereference_protected(nfsi->delegation,
Chuck Leverd3978bb2010-12-24 01:33:04 +0000370 lockdep_is_held(&clp->cl_lock));
David Howells17d2c0a2010-05-01 12:37:18 -0400371 if (old_delegation != NULL) {
Trond Myklebustcf6726e2014-12-19 11:22:28 -0500372 /* Is this an update of the existing delegation? */
373 if (nfs4_stateid_match_other(&old_delegation->stateid,
374 &delegation->stateid)) {
375 nfs_update_inplace_delegation(old_delegation,
376 delegation);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500377 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
Trond Myklebust57bfa892008-01-25 16:38:18 -0500379 /*
380 * Deal with broken servers that hand out two
381 * delegations for the same file.
Trond Myklebust17280172012-03-11 13:11:00 -0400382 * Allow for upgrades to a WRITE delegation, but
383 * nothing else.
Trond Myklebust57bfa892008-01-25 16:38:18 -0500384 */
385 dfprintk(FILE, "%s: server %s handed out "
386 "a duplicate delegation!\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700387 __func__, clp->cl_hostname);
Trond Myklebust17280172012-03-11 13:11:00 -0400388 if (delegation->type == old_delegation->type ||
389 !(delegation->type & FMODE_WRITE)) {
Trond Myklebust57bfa892008-01-25 16:38:18 -0500390 freeme = delegation;
391 delegation = NULL;
392 goto out;
393 }
Trond Myklebustade04642015-02-27 14:25:50 -0500394 if (test_and_set_bit(NFS_DELEGATION_RETURNING,
395 &old_delegation->flags))
396 goto out;
397 freeme = nfs_detach_delegation_locked(nfsi,
Trond Myklebustd25be542013-02-05 11:43:28 -0500398 old_delegation, clp);
399 if (freeme == NULL)
400 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
Trond Myklebust38942ba2015-03-04 15:59:05 -0500402 list_add_tail_rcu(&delegation->super_list, &server->delegations);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500403 rcu_assign_pointer(nfsi->delegation, delegation);
404 delegation = NULL;
Trond Myklebust412c77c2007-07-03 16:10:55 -0400405
Trond Myklebust35156bf2018-03-20 17:03:13 -0400406 trace_nfs4_set_delegation(inode, type);
Trond Myklebust412c77c2007-07-03 16:10:55 -0400407
Trond Myklebust97c2c172018-04-07 18:43:17 -0400408 spin_lock(&inode->i_lock);
409 if (NFS_I(inode)->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ATIME))
410 NFS_I(inode)->cache_validity |= NFS_INO_REVAL_FORCED;
411 spin_unlock(&inode->i_lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500412out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 spin_unlock(&clp->cl_lock);
Trond Myklebust603c83d2007-10-18 19:59:20 -0400414 if (delegation != NULL)
415 nfs_free_delegation(delegation);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500416 if (freeme != NULL)
417 nfs_do_return_delegation(inode, freeme, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 return status;
419}
420
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421/*
422 * Basic procedure for returning a delegation to the server
423 */
Trond Myklebustd25be542013-02-05 11:43:28 -0500424static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425{
Trond Myklebustd25be542013-02-05 11:43:28 -0500426 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust869f9df2014-11-10 18:43:56 -0500428 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Trond Myklebustd25be542013-02-05 11:43:28 -0500430 if (delegation == NULL)
431 return 0;
432 do {
Trond Myklebust869f9df2014-11-10 18:43:56 -0500433 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
434 break;
Trond Myklebust24311f82015-09-20 10:50:17 -0400435 err = nfs_delegation_claim_opens(inode, &delegation->stateid,
436 delegation->type);
Trond Myklebustd25be542013-02-05 11:43:28 -0500437 if (!issync || err != -EAGAIN)
438 break;
439 /*
440 * Guard against state recovery
441 */
442 err = nfs4_wait_clnt_recover(clp);
443 } while (err == 0);
444
445 if (err) {
446 nfs_abort_delegation_return(delegation, clp);
447 goto out;
448 }
449 if (!nfs_detach_delegation(nfsi, delegation, NFS_SERVER(inode)))
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500450 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500452 err = nfs_do_return_delegation(inode, delegation, issync);
453out:
454 return err;
Trond Myklebust90163022007-07-05 14:55:18 -0400455}
456
Trond Myklebustb7571442013-04-03 14:33:49 -0400457static bool nfs_delegation_need_return(struct nfs_delegation *delegation)
458{
459 bool ret = false;
460
Trond Myklebustec3ca4e52015-02-26 14:05:05 -0500461 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
462 goto out;
Trond Myklebustb7571442013-04-03 14:33:49 -0400463 if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
464 ret = true;
465 if (test_and_clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags) && !ret) {
466 struct inode *inode;
467
468 spin_lock(&delegation->lock);
469 inode = delegation->inode;
470 if (inode && list_empty(&NFS_I(inode)->open_files))
471 ret = true;
472 spin_unlock(&delegation->lock);
473 }
Trond Myklebustec3ca4e52015-02-26 14:05:05 -0500474out:
Trond Myklebustb7571442013-04-03 14:33:49 -0400475 return ret;
476}
477
Chuck Leverd3978bb2010-12-24 01:33:04 +0000478/**
479 * nfs_client_return_marked_delegations - return previously marked delegations
480 * @clp: nfs_client to process
481 *
Trond Myklebustdc327ed2012-05-06 19:46:30 -0400482 * Note that this function is designed to be called by the state
483 * manager thread. For this reason, it cannot flush the dirty data,
484 * since that could deadlock in case of a state recovery error.
485 *
Chuck Leverd3978bb2010-12-24 01:33:04 +0000486 * Returns zero on success, or a negative errno value.
Trond Myklebust515d8612008-12-23 15:21:46 -0500487 */
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500488int nfs_client_return_marked_delegations(struct nfs_client *clp)
Trond Myklebust515d8612008-12-23 15:21:46 -0500489{
490 struct nfs_delegation *delegation;
NeilBrowne04bbf62018-04-30 14:31:30 +1000491 struct nfs_delegation *prev;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000492 struct nfs_server *server;
Trond Myklebust515d8612008-12-23 15:21:46 -0500493 struct inode *inode;
NeilBrowne04bbf62018-04-30 14:31:30 +1000494 struct inode *place_holder = NULL;
495 struct nfs_delegation *place_holder_deleg = NULL;
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500496 int err = 0;
Trond Myklebust515d8612008-12-23 15:21:46 -0500497
498restart:
NeilBrowne04bbf62018-04-30 14:31:30 +1000499 /*
500 * To avoid quadratic looping we hold a reference
501 * to an inode place_holder. Each time we restart, we
502 * list nfs_servers from the server of that inode, and
503 * delegation in the server from the delegations of that
504 * inode.
505 * prev is an RCU-protected pointer to a delegation which
506 * wasn't marked for return and might be a good choice for
507 * the next place_holder.
508 */
Trond Myklebust515d8612008-12-23 15:21:46 -0500509 rcu_read_lock();
NeilBrowne04bbf62018-04-30 14:31:30 +1000510 prev = NULL;
511 if (place_holder)
512 server = NFS_SERVER(place_holder);
513 else
514 server = list_entry_rcu(clp->cl_superblocks.next,
515 struct nfs_server, client_link);
516 list_for_each_entry_from_rcu(server, &clp->cl_superblocks, client_link) {
517 delegation = NULL;
518 if (place_holder && server == NFS_SERVER(place_holder))
519 delegation = rcu_dereference(NFS_I(place_holder)->delegation);
520 if (!delegation || delegation != place_holder_deleg)
521 delegation = list_entry_rcu(server->delegations.next,
522 struct nfs_delegation, super_list);
523 list_for_each_entry_from_rcu(delegation, &server->delegations, super_list) {
524 struct inode *to_put = NULL;
525
526 if (!nfs_delegation_need_return(delegation)) {
527 prev = delegation;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000528 continue;
NeilBrowne04bbf62018-04-30 14:31:30 +1000529 }
Trond Myklebust9f0f8e12015-02-26 09:57:34 -0500530 if (!nfs_sb_active(server->super))
NeilBrownf38934912018-05-31 15:23:22 +1000531 break; /* continue in outer loop */
NeilBrowne04bbf62018-04-30 14:31:30 +1000532
533 if (prev) {
534 struct inode *tmp;
535
536 tmp = nfs_delegation_grab_inode(prev);
537 if (tmp) {
538 to_put = place_holder;
539 place_holder = tmp;
540 place_holder_deleg = prev;
541 }
542 }
543
Trond Myklebust9f0f8e12015-02-26 09:57:34 -0500544 inode = nfs_delegation_grab_inode(delegation);
545 if (inode == NULL) {
546 rcu_read_unlock();
NeilBrowne04bbf62018-04-30 14:31:30 +1000547 if (to_put)
548 iput(to_put);
Trond Myklebust9f0f8e12015-02-26 09:57:34 -0500549 nfs_sb_deactive(server->super);
550 goto restart;
551 }
Trond Myklebustd25be542013-02-05 11:43:28 -0500552 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
Chuck Leverd3978bb2010-12-24 01:33:04 +0000553 rcu_read_unlock();
554
NeilBrowne04bbf62018-04-30 14:31:30 +1000555 if (to_put)
556 iput(to_put);
557
Trond Myklebustd25be542013-02-05 11:43:28 -0500558 err = nfs_end_delegation_return(inode, delegation, 0);
Chuck Leverd3978bb2010-12-24 01:33:04 +0000559 iput(inode);
Trond Myklebust9f0f8e12015-02-26 09:57:34 -0500560 nfs_sb_deactive(server->super);
NeilBrown3ca951b2018-04-30 14:31:30 +1000561 cond_resched();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000562 if (!err)
563 goto restart;
564 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
NeilBrowne04bbf62018-04-30 14:31:30 +1000565 if (place_holder)
566 iput(place_holder);
Chuck Leverd3978bb2010-12-24 01:33:04 +0000567 return err;
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500568 }
Trond Myklebust515d8612008-12-23 15:21:46 -0500569 }
570 rcu_read_unlock();
NeilBrowne04bbf62018-04-30 14:31:30 +1000571 if (place_holder)
572 iput(place_holder);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500573 return 0;
Trond Myklebust515d8612008-12-23 15:21:46 -0500574}
575
Chuck Leverd3978bb2010-12-24 01:33:04 +0000576/**
577 * nfs_inode_return_delegation_noreclaim - return delegation, don't reclaim opens
578 * @inode: inode to process
579 *
580 * Does not protect against delegation reclaims, therefore really only safe
581 * to be called from nfs4_clear_inode().
Trond Myklebuste6f81072008-01-24 18:14:34 -0500582 */
583void nfs_inode_return_delegation_noreclaim(struct inode *inode)
584{
Trond Myklebuste6f81072008-01-24 18:14:34 -0500585 struct nfs_delegation *delegation;
586
Trond Myklebustd25be542013-02-05 11:43:28 -0500587 delegation = nfs_inode_detach_delegation(inode);
588 if (delegation != NULL)
Trond Myklebust5fcdfac2015-03-25 13:19:42 -0400589 nfs_do_return_delegation(inode, delegation, 1);
Trond Myklebuste6f81072008-01-24 18:14:34 -0500590}
591
Chuck Leverd3978bb2010-12-24 01:33:04 +0000592/**
593 * nfs_inode_return_delegation - synchronously return a delegation
594 * @inode: inode to process
595 *
Trond Myklebustc57d1bc2012-05-06 19:34:17 -0400596 * This routine will always flush any dirty data to disk on the
597 * assumption that if we need to return the delegation, then
598 * we should stop caching.
599 *
Chuck Leverd3978bb2010-12-24 01:33:04 +0000600 * Returns zero on success, or a negative errno value.
601 */
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400602int nfs4_inode_return_delegation(struct inode *inode)
Trond Myklebust90163022007-07-05 14:55:18 -0400603{
Trond Myklebust90163022007-07-05 14:55:18 -0400604 struct nfs_inode *nfsi = NFS_I(inode);
605 struct nfs_delegation *delegation;
606 int err = 0;
607
Trond Myklebustc57d1bc2012-05-06 19:34:17 -0400608 nfs_wb_all(inode);
Trond Myklebustd25be542013-02-05 11:43:28 -0500609 delegation = nfs_start_delegation_return(nfsi);
610 if (delegation != NULL)
611 err = nfs_end_delegation_return(inode, delegation, 1);
Trond Myklebust90163022007-07-05 14:55:18 -0400612 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613}
614
Trond Myklebustc01d3642018-03-20 16:43:20 -0400615/**
616 * nfs4_inode_make_writeable
617 * @inode: pointer to inode
618 *
619 * Make the inode writeable by returning the delegation if necessary
620 *
621 * Returns zero on success, or a negative errno value.
622 */
623int nfs4_inode_make_writeable(struct inode *inode)
624{
625 if (!nfs4_has_session(NFS_SERVER(inode)->nfs_client) ||
626 !nfs4_check_delegation(inode, FMODE_WRITE))
627 return nfs4_inode_return_delegation(inode);
628 return 0;
629}
630
Trond Myklebustb7571442013-04-03 14:33:49 -0400631static void nfs_mark_return_if_closed_delegation(struct nfs_server *server,
632 struct nfs_delegation *delegation)
633{
634 set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
635 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
636}
637
Trond Myklebusted1e62112011-07-25 15:37:29 -0400638static void nfs_mark_return_delegation(struct nfs_server *server,
639 struct nfs_delegation *delegation)
Trond Myklebust6411bd42008-12-23 15:21:51 -0500640{
641 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
Trond Myklebusted1e62112011-07-25 15:37:29 -0400642 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
Trond Myklebust6411bd42008-12-23 15:21:51 -0500643}
644
Trond Myklebust5c31e232013-04-03 19:04:58 -0400645static bool nfs_server_mark_return_all_delegations(struct nfs_server *server)
646{
647 struct nfs_delegation *delegation;
648 bool ret = false;
649
650 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
651 nfs_mark_return_delegation(server, delegation);
652 ret = true;
653 }
654 return ret;
655}
656
Trond Myklebustb02ba0b2013-04-03 19:23:58 -0400657static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
658{
659 struct nfs_server *server;
660
661 rcu_read_lock();
662 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
663 nfs_server_mark_return_all_delegations(server);
664 rcu_read_unlock();
665}
666
667static void nfs_delegation_run_state_manager(struct nfs_client *clp)
668{
669 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
670 nfs4_schedule_state_manager(clp);
671}
672
673/**
674 * nfs_expire_all_delegations
675 * @clp: client to process
676 *
677 */
678void nfs_expire_all_delegations(struct nfs_client *clp)
679{
680 nfs_client_mark_return_all_delegations(clp);
681 nfs_delegation_run_state_manager(clp);
682}
683
Chuck Leverd3978bb2010-12-24 01:33:04 +0000684/**
685 * nfs_super_return_all_delegations - return delegations for one superblock
686 * @sb: sb to process
687 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 */
Bryan Schumakereeebf912012-06-20 15:53:41 -0400689void nfs_server_return_all_delegations(struct nfs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000691 struct nfs_client *clp = server->nfs_client;
Trond Myklebust5c31e232013-04-03 19:04:58 -0400692 bool need_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 if (clp == NULL)
695 return;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000696
Trond Myklebust8383e462007-07-06 15:12:04 -0400697 rcu_read_lock();
Trond Myklebust5c31e232013-04-03 19:04:58 -0400698 need_wait = nfs_server_mark_return_all_delegations(server);
Trond Myklebust8383e462007-07-06 15:12:04 -0400699 rcu_read_unlock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000700
Trond Myklebust5c31e232013-04-03 19:04:58 -0400701 if (need_wait) {
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500702 nfs4_schedule_state_manager(clp);
Trond Myklebust5c31e232013-04-03 19:04:58 -0400703 nfs4_wait_clnt_recover(clp);
704 }
Trond Myklebust515d8612008-12-23 15:21:46 -0500705}
706
Trond Myklebust826e0012013-04-03 19:27:52 -0400707static void nfs_mark_return_unused_delegation_types(struct nfs_server *server,
Chuck Leverd3978bb2010-12-24 01:33:04 +0000708 fmode_t flags)
Trond Myklebust515d8612008-12-23 15:21:46 -0500709{
710 struct nfs_delegation *delegation;
711
Chuck Leverd3978bb2010-12-24 01:33:04 +0000712 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500713 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
714 continue;
715 if (delegation->type & flags)
Trond Myklebust826e0012013-04-03 19:27:52 -0400716 nfs_mark_return_if_closed_delegation(server, delegation);
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500717 }
Chuck Leverd3978bb2010-12-24 01:33:04 +0000718}
719
Trond Myklebust826e0012013-04-03 19:27:52 -0400720static void nfs_client_mark_return_unused_delegation_types(struct nfs_client *clp,
Chuck Leverd3978bb2010-12-24 01:33:04 +0000721 fmode_t flags)
722{
723 struct nfs_server *server;
724
725 rcu_read_lock();
726 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
Trond Myklebust826e0012013-04-03 19:27:52 -0400727 nfs_mark_return_unused_delegation_types(server, flags);
Trond Myklebust515d8612008-12-23 15:21:46 -0500728 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
730
Trond Myklebust41020b62016-09-22 13:38:58 -0400731static void nfs_mark_delegation_revoked(struct nfs_server *server,
732 struct nfs_delegation *delegation)
Trond Myklebust869f9df2014-11-10 18:43:56 -0500733{
Trond Myklebust41020b62016-09-22 13:38:58 -0400734 set_bit(NFS_DELEGATION_REVOKED, &delegation->flags);
Trond Myklebust059b43e2016-09-22 13:39:06 -0400735 delegation->stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust41020b62016-09-22 13:38:58 -0400736 nfs_mark_return_delegation(server, delegation);
Trond Myklebust869f9df2014-11-10 18:43:56 -0500737}
738
Trond Myklebust41020b62016-09-22 13:38:58 -0400739static bool nfs_revoke_delegation(struct inode *inode,
740 const nfs4_stateid *stateid)
741{
742 struct nfs_delegation *delegation;
Trond Myklebust7f048832016-09-22 13:39:14 -0400743 nfs4_stateid tmp;
Trond Myklebust41020b62016-09-22 13:38:58 -0400744 bool ret = false;
745
746 rcu_read_lock();
747 delegation = rcu_dereference(NFS_I(inode)->delegation);
748 if (delegation == NULL)
749 goto out;
Trond Myklebust7f048832016-09-22 13:39:14 -0400750 if (stateid == NULL) {
751 nfs4_stateid_copy(&tmp, &delegation->stateid);
752 stateid = &tmp;
753 } else if (!nfs4_stateid_match(stateid, &delegation->stateid))
Trond Myklebust41020b62016-09-22 13:38:58 -0400754 goto out;
755 nfs_mark_delegation_revoked(NFS_SERVER(inode), delegation);
756 ret = true;
757out:
758 rcu_read_unlock();
Trond Myklebust7f048832016-09-22 13:39:14 -0400759 if (ret)
760 nfs_inode_find_state_and_recover(inode, stateid);
Trond Myklebust41020b62016-09-22 13:38:58 -0400761 return ret;
762}
763
764void nfs_remove_bad_delegation(struct inode *inode,
765 const nfs4_stateid *stateid)
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500766{
767 struct nfs_delegation *delegation;
768
Trond Myklebust41020b62016-09-22 13:38:58 -0400769 if (!nfs_revoke_delegation(inode, stateid))
770 return;
Trond Myklebustd25be542013-02-05 11:43:28 -0500771 delegation = nfs_inode_detach_delegation(inode);
Trond Myklebust7f048832016-09-22 13:39:14 -0400772 if (delegation)
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500773 nfs_free_delegation(delegation);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500774}
Andy Adamson9cb81962012-03-07 10:49:41 -0500775EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500776
Chuck Leverd3978bb2010-12-24 01:33:04 +0000777/**
Trond Myklebust826e0012013-04-03 19:27:52 -0400778 * nfs_expire_unused_delegation_types
Chuck Leverd3978bb2010-12-24 01:33:04 +0000779 * @clp: client to process
780 * @flags: delegation types to expire
781 *
782 */
Trond Myklebust826e0012013-04-03 19:27:52 -0400783void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags)
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500784{
Trond Myklebust826e0012013-04-03 19:27:52 -0400785 nfs_client_mark_return_unused_delegation_types(clp, flags);
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500786 nfs_delegation_run_state_manager(clp);
787}
788
Chuck Leverd3978bb2010-12-24 01:33:04 +0000789static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
Trond Myklebustb7391f42008-12-23 15:21:52 -0500790{
791 struct nfs_delegation *delegation;
792
Chuck Leverd3978bb2010-12-24 01:33:04 +0000793 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
Trond Myklebustb7391f42008-12-23 15:21:52 -0500794 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
795 continue;
Trond Myklebustb7571442013-04-03 14:33:49 -0400796 nfs_mark_return_if_closed_delegation(server, delegation);
Trond Myklebustb7391f42008-12-23 15:21:52 -0500797 }
Trond Myklebustb7391f42008-12-23 15:21:52 -0500798}
799
Chuck Leverd3978bb2010-12-24 01:33:04 +0000800/**
801 * nfs_expire_unreferenced_delegations - Eliminate unused delegations
802 * @clp: nfs_client to process
803 *
804 */
Trond Myklebustb7391f42008-12-23 15:21:52 -0500805void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
806{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000807 struct nfs_server *server;
808
809 rcu_read_lock();
810 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
811 nfs_mark_return_unreferenced_delegations(server);
812 rcu_read_unlock();
813
Trond Myklebustb7391f42008-12-23 15:21:52 -0500814 nfs_delegation_run_state_manager(clp);
815}
816
Chuck Leverd3978bb2010-12-24 01:33:04 +0000817/**
818 * nfs_async_inode_return_delegation - asynchronously return a delegation
819 * @inode: inode to process
Trond Myklebust8e663f02012-03-04 18:13:56 -0500820 * @stateid: state ID information
Chuck Leverd3978bb2010-12-24 01:33:04 +0000821 *
822 * Returns zero on success, or a negative errno value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 */
Chuck Leverd3978bb2010-12-24 01:33:04 +0000824int nfs_async_inode_return_delegation(struct inode *inode,
825 const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Trond Myklebusted1e62112011-07-25 15:37:29 -0400827 struct nfs_server *server = NFS_SERVER(inode);
828 struct nfs_client *clp = server->nfs_client;
Trond Myklebust6411bd42008-12-23 15:21:51 -0500829 struct nfs_delegation *delegation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
Trond Myklebust6411bd42008-12-23 15:21:51 -0500831 rcu_read_lock();
832 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebust755a48a2014-03-02 22:03:12 -0500833 if (delegation == NULL)
834 goto out_enoent;
Trond Myklebust4816fda2015-09-20 14:58:42 -0400835 if (stateid != NULL &&
836 !clp->cl_mvops->match_stateid(&delegation->stateid, stateid))
Trond Myklebust755a48a2014-03-02 22:03:12 -0500837 goto out_enoent;
Trond Myklebusted1e62112011-07-25 15:37:29 -0400838 nfs_mark_return_delegation(server, delegation);
Trond Myklebust6411bd42008-12-23 15:21:51 -0500839 rcu_read_unlock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000840
Trond Myklebust6411bd42008-12-23 15:21:51 -0500841 nfs_delegation_run_state_manager(clp);
842 return 0;
Trond Myklebust755a48a2014-03-02 22:03:12 -0500843out_enoent:
844 rcu_read_unlock();
845 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
Chuck Leverd3978bb2010-12-24 01:33:04 +0000848static struct inode *
849nfs_delegation_find_inode_server(struct nfs_server *server,
850 const struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
852 struct nfs_delegation *delegation;
853 struct inode *res = NULL;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000854
855 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
Trond Myklebust86e89482008-12-23 15:21:39 -0500856 spin_lock(&delegation->lock);
857 if (delegation->inode != NULL &&
858 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 res = igrab(delegation->inode);
Trond Myklebust6c342652018-06-07 14:22:00 -0400860 spin_unlock(&delegation->lock);
861 if (res != NULL)
862 return res;
863 return ERR_PTR(-EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 }
Trond Myklebust86e89482008-12-23 15:21:39 -0500865 spin_unlock(&delegation->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 }
Trond Myklebust6c342652018-06-07 14:22:00 -0400867 return ERR_PTR(-ENOENT);
Chuck Leverd3978bb2010-12-24 01:33:04 +0000868}
869
870/**
871 * nfs_delegation_find_inode - retrieve the inode associated with a delegation
872 * @clp: client state handle
873 * @fhandle: filehandle from a delegation recall
874 *
875 * Returns pointer to inode matching "fhandle," or NULL if a matching inode
876 * cannot be found.
877 */
878struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
879 const struct nfs_fh *fhandle)
880{
881 struct nfs_server *server;
Trond Myklebust6c342652018-06-07 14:22:00 -0400882 struct inode *res;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000883
884 rcu_read_lock();
885 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
886 res = nfs_delegation_find_inode_server(server, fhandle);
Anna Schumakerd5681f52018-06-14 09:39:17 -0400887 if (res != ERR_PTR(-ENOENT)) {
888 rcu_read_unlock();
Trond Myklebust6c342652018-06-07 14:22:00 -0400889 return res;
Anna Schumakerd5681f52018-06-14 09:39:17 -0400890 }
Chuck Leverd3978bb2010-12-24 01:33:04 +0000891 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400892 rcu_read_unlock();
Trond Myklebust6c342652018-06-07 14:22:00 -0400893 return ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894}
895
Chuck Leverd3978bb2010-12-24 01:33:04 +0000896static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
897{
898 struct nfs_delegation *delegation;
899
Trond Myklebust45870d62016-09-22 13:38:59 -0400900 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
901 /*
902 * If the delegation may have been admin revoked, then we
903 * cannot reclaim it.
904 */
905 if (test_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags))
906 continue;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000907 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
Trond Myklebust45870d62016-09-22 13:38:59 -0400908 }
Chuck Leverd3978bb2010-12-24 01:33:04 +0000909}
910
911/**
912 * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
913 * @clp: nfs_client to process
914 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 */
David Howellsadfa6f92006-08-22 20:06:08 -0400916void nfs_delegation_mark_reclaim(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000918 struct nfs_server *server;
919
Trond Myklebust8383e462007-07-06 15:12:04 -0400920 rcu_read_lock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000921 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
922 nfs_delegation_mark_reclaim_server(server);
Trond Myklebust8383e462007-07-06 15:12:04 -0400923 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924}
925
Chuck Leverd3978bb2010-12-24 01:33:04 +0000926/**
927 * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
928 * @clp: nfs_client to process
929 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 */
David Howellsadfa6f92006-08-22 20:06:08 -0400931void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932{
Trond Myklebust8383e462007-07-06 15:12:04 -0400933 struct nfs_delegation *delegation;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000934 struct nfs_server *server;
Trond Myklebust86e89482008-12-23 15:21:39 -0500935 struct inode *inode;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000936
Trond Myklebust8383e462007-07-06 15:12:04 -0400937restart:
938 rcu_read_lock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000939 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
940 list_for_each_entry_rcu(delegation, &server->delegations,
941 super_list) {
Trond Myklebustec3ca4e52015-02-26 14:05:05 -0500942 if (test_bit(NFS_DELEGATION_RETURNING,
943 &delegation->flags))
944 continue;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000945 if (test_bit(NFS_DELEGATION_NEED_RECLAIM,
946 &delegation->flags) == 0)
947 continue;
Trond Myklebust9f0f8e12015-02-26 09:57:34 -0500948 if (!nfs_sb_active(server->super))
NeilBrownf38934912018-05-31 15:23:22 +1000949 break; /* continue in outer loop */
Trond Myklebust9f0f8e12015-02-26 09:57:34 -0500950 inode = nfs_delegation_grab_inode(delegation);
951 if (inode == NULL) {
952 rcu_read_unlock();
953 nfs_sb_deactive(server->super);
954 goto restart;
955 }
Trond Myklebustb04b22f2015-02-26 13:59:38 -0500956 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
Chuck Leverd3978bb2010-12-24 01:33:04 +0000957 rcu_read_unlock();
Trond Myklebustb04b22f2015-02-26 13:59:38 -0500958 if (delegation != NULL) {
959 delegation = nfs_detach_delegation(NFS_I(inode),
960 delegation, server);
961 if (delegation != NULL)
962 nfs_free_delegation(delegation);
963 }
Chuck Leverd3978bb2010-12-24 01:33:04 +0000964 iput(inode);
Trond Myklebust9f0f8e12015-02-26 09:57:34 -0500965 nfs_sb_deactive(server->super);
NeilBrown3ca951b2018-04-30 14:31:30 +1000966 cond_resched();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000967 goto restart;
968 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400970 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971}
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500972
Trond Myklebustbb3d1a32016-09-22 13:39:00 -0400973static inline bool nfs4_server_rebooted(const struct nfs_client *clp)
974{
975 return (clp->cl_state & (BIT(NFS4CLNT_CHECK_LEASE) |
976 BIT(NFS4CLNT_LEASE_EXPIRED) |
977 BIT(NFS4CLNT_SESSION_RESET))) != 0;
978}
979
Trond Myklebust45870d62016-09-22 13:38:59 -0400980static void nfs_mark_test_expired_delegation(struct nfs_server *server,
981 struct nfs_delegation *delegation)
982{
Trond Myklebust059b43e2016-09-22 13:39:06 -0400983 if (delegation->stateid.type == NFS4_INVALID_STATEID_TYPE)
984 return;
Trond Myklebust45870d62016-09-22 13:38:59 -0400985 clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
986 set_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
987 set_bit(NFS4CLNT_DELEGATION_EXPIRED, &server->nfs_client->cl_state);
988}
989
Trond Myklebustbb3d1a32016-09-22 13:39:00 -0400990static void nfs_inode_mark_test_expired_delegation(struct nfs_server *server,
991 struct inode *inode)
992{
993 struct nfs_delegation *delegation;
994
995 rcu_read_lock();
996 delegation = rcu_dereference(NFS_I(inode)->delegation);
997 if (delegation)
998 nfs_mark_test_expired_delegation(server, delegation);
999 rcu_read_unlock();
1000
1001}
1002
Trond Myklebust45870d62016-09-22 13:38:59 -04001003static void nfs_delegation_mark_test_expired_server(struct nfs_server *server)
1004{
1005 struct nfs_delegation *delegation;
1006
1007 list_for_each_entry_rcu(delegation, &server->delegations, super_list)
1008 nfs_mark_test_expired_delegation(server, delegation);
1009}
1010
1011/**
1012 * nfs_mark_test_expired_all_delegations - mark all delegations for testing
1013 * @clp: nfs_client to process
1014 *
1015 * Iterates through all the delegations associated with this server and
1016 * marks them as needing to be checked for validity.
1017 */
1018void nfs_mark_test_expired_all_delegations(struct nfs_client *clp)
1019{
1020 struct nfs_server *server;
1021
1022 rcu_read_lock();
1023 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1024 nfs_delegation_mark_test_expired_server(server);
1025 rcu_read_unlock();
1026}
1027
1028/**
1029 * nfs_reap_expired_delegations - reap expired delegations
1030 * @clp: nfs_client to process
1031 *
1032 * Iterates through all the delegations associated with this server and
1033 * checks if they have may have been revoked. This function is usually
1034 * expected to be called in cases where the server may have lost its
1035 * lease.
1036 */
1037void nfs_reap_expired_delegations(struct nfs_client *clp)
1038{
1039 const struct nfs4_minor_version_ops *ops = clp->cl_mvops;
1040 struct nfs_delegation *delegation;
1041 struct nfs_server *server;
1042 struct inode *inode;
1043 struct rpc_cred *cred;
1044 nfs4_stateid stateid;
1045
1046restart:
1047 rcu_read_lock();
1048 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1049 list_for_each_entry_rcu(delegation, &server->delegations,
1050 super_list) {
1051 if (test_bit(NFS_DELEGATION_RETURNING,
1052 &delegation->flags))
1053 continue;
1054 if (test_bit(NFS_DELEGATION_TEST_EXPIRED,
1055 &delegation->flags) == 0)
1056 continue;
1057 if (!nfs_sb_active(server->super))
NeilBrownf38934912018-05-31 15:23:22 +10001058 break; /* continue in outer loop */
Trond Myklebust45870d62016-09-22 13:38:59 -04001059 inode = nfs_delegation_grab_inode(delegation);
1060 if (inode == NULL) {
1061 rcu_read_unlock();
1062 nfs_sb_deactive(server->super);
1063 goto restart;
1064 }
1065 cred = get_rpccred_rcu(delegation->cred);
1066 nfs4_stateid_copy(&stateid, &delegation->stateid);
1067 clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
1068 rcu_read_unlock();
1069 if (cred != NULL &&
1070 ops->test_and_free_expired(server, &stateid, cred) < 0) {
1071 nfs_revoke_delegation(inode, &stateid);
1072 nfs_inode_find_state_and_recover(inode, &stateid);
1073 }
1074 put_rpccred(cred);
Trond Myklebustbb3d1a32016-09-22 13:39:00 -04001075 if (nfs4_server_rebooted(clp)) {
1076 nfs_inode_mark_test_expired_delegation(server,inode);
1077 iput(inode);
1078 nfs_sb_deactive(server->super);
1079 return;
1080 }
Trond Myklebust45870d62016-09-22 13:38:59 -04001081 iput(inode);
1082 nfs_sb_deactive(server->super);
NeilBrown3ca951b2018-04-30 14:31:30 +10001083 cond_resched();
Trond Myklebust45870d62016-09-22 13:38:59 -04001084 goto restart;
1085 }
1086 }
1087 rcu_read_unlock();
1088}
1089
Trond Myklebust6c2d8f82016-09-22 13:39:07 -04001090void nfs_inode_find_delegation_state_and_recover(struct inode *inode,
1091 const nfs4_stateid *stateid)
1092{
1093 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1094 struct nfs_delegation *delegation;
1095 bool found = false;
1096
1097 rcu_read_lock();
1098 delegation = rcu_dereference(NFS_I(inode)->delegation);
1099 if (delegation &&
1100 nfs4_stateid_match_other(&delegation->stateid, stateid)) {
1101 nfs_mark_test_expired_delegation(NFS_SERVER(inode), delegation);
1102 found = true;
1103 }
1104 rcu_read_unlock();
1105 if (found)
1106 nfs4_schedule_state_manager(clp);
1107}
1108
Chuck Leverd3978bb2010-12-24 01:33:04 +00001109/**
1110 * nfs_delegations_present - check for existence of delegations
1111 * @clp: client state handle
1112 *
1113 * Returns one if there are any nfs_delegation structures attached
1114 * to this nfs_client.
1115 */
1116int nfs_delegations_present(struct nfs_client *clp)
1117{
1118 struct nfs_server *server;
1119 int ret = 0;
1120
1121 rcu_read_lock();
1122 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1123 if (!list_empty(&server->delegations)) {
1124 ret = 1;
1125 break;
1126 }
1127 rcu_read_unlock();
1128 return ret;
1129}
1130
1131/**
Trond Myklebust12f275c2017-11-06 15:28:05 -05001132 * nfs4_refresh_delegation_stateid - Update delegation stateid seqid
1133 * @dst: stateid to refresh
1134 * @inode: inode to check
1135 *
1136 * Returns "true" and updates "dst->seqid" * if inode had a delegation
1137 * that matches our delegation stateid. Otherwise "false" is returned.
1138 */
1139bool nfs4_refresh_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
1140{
1141 struct nfs_delegation *delegation;
1142 bool ret = false;
1143 if (!inode)
1144 goto out;
1145
1146 rcu_read_lock();
1147 delegation = rcu_dereference(NFS_I(inode)->delegation);
1148 if (delegation != NULL &&
1149 nfs4_stateid_match_other(dst, &delegation->stateid)) {
1150 dst->seqid = delegation->stateid.seqid;
1151 return ret;
1152 }
1153 rcu_read_unlock();
1154out:
1155 return ret;
1156}
1157
1158/**
Chuck Leverd3978bb2010-12-24 01:33:04 +00001159 * nfs4_copy_delegation_stateid - Copy inode's state ID information
Chuck Leverd3978bb2010-12-24 01:33:04 +00001160 * @inode: inode to check
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001161 * @flags: delegation type requirement
Trond Myklebustabf4e132016-05-16 17:42:44 -04001162 * @dst: stateid data structure to fill in
1163 * @cred: optional argument to retrieve credential
Chuck Leverd3978bb2010-12-24 01:33:04 +00001164 *
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001165 * Returns "true" and fills in "dst->data" * if inode had a delegation,
1166 * otherwise "false" is returned.
Chuck Leverd3978bb2010-12-24 01:33:04 +00001167 */
Trond Myklebustabf4e132016-05-16 17:42:44 -04001168bool nfs4_copy_delegation_stateid(struct inode *inode, fmode_t flags,
1169 nfs4_stateid *dst, struct rpc_cred **cred)
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001170{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001171 struct nfs_inode *nfsi = NFS_I(inode);
1172 struct nfs_delegation *delegation;
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001173 bool ret;
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001174
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001175 flags &= FMODE_READ|FMODE_WRITE;
Trond Myklebust8383e462007-07-06 15:12:04 -04001176 rcu_read_lock();
1177 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebustaa05c872016-09-22 13:38:54 -04001178 ret = nfs4_is_valid_delegation(delegation, flags);
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001179 if (ret) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001180 nfs4_stateid_copy(dst, &delegation->stateid);
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001181 nfs_mark_delegation_referenced(delegation);
Trond Myklebustabf4e132016-05-16 17:42:44 -04001182 if (cred)
1183 *cred = get_rpccred(delegation->cred);
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001184 }
Trond Myklebust8383e462007-07-06 15:12:04 -04001185 rcu_read_unlock();
1186 return ret;
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001187}
Trond Myklebust5445b1f2015-09-05 19:06:58 -04001188
1189/**
1190 * nfs4_delegation_flush_on_close - Check if we must flush file on close
1191 * @inode: inode to check
1192 *
1193 * This function checks the number of outstanding writes to the file
1194 * against the delegation 'space_limit' field to see if
1195 * the spec requires us to flush the file on close.
1196 */
1197bool nfs4_delegation_flush_on_close(const struct inode *inode)
1198{
1199 struct nfs_inode *nfsi = NFS_I(inode);
1200 struct nfs_delegation *delegation;
1201 bool ret = true;
1202
1203 rcu_read_lock();
1204 delegation = rcu_dereference(nfsi->delegation);
1205 if (delegation == NULL || !(delegation->type & FMODE_WRITE))
1206 goto out;
Trond Myklebusta6b6d5b2017-08-01 15:39:46 -04001207 if (atomic_long_read(&nfsi->nrequests) < delegation->pagemod_limit)
Trond Myklebust5445b1f2015-09-05 19:06:58 -04001208 ret = false;
1209out:
1210 rcu_read_unlock();
1211 return ret;
1212}