blob: 7c9eb679dbdbf8a3eb39d3b18b129364ae9e8211 [file] [log] [blame]
Thomas Gleixner457c8992019-05-19 13:08:55 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * linux/fs/nfs/delegation.c
4 *
5 * Copyright (C) 2004 Trond Myklebust
6 *
7 * NFS file delegation management
8 *
9 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/completion.h>
Trond Myklebust58d97142006-01-03 09:55:24 +010011#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/module.h>
13#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/spinlock.h>
Jeff Layton1eb5d982018-01-09 08:21:17 -050016#include <linux/iversion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#include <linux/nfs4.h>
19#include <linux/nfs_fs.h>
20#include <linux/nfs_xdr.h>
21
Trond Myklebust4ce79712005-06-22 17:16:21 +000022#include "nfs4_fs.h"
Trond Myklebustc01d3642018-03-20 16:43:20 -040023#include "nfs4session.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "delegation.h"
David Howells24c8dbb2006-08-22 20:06:10 -040025#include "internal.h"
Trond Myklebustca8acf82013-08-13 10:36:56 -040026#include "nfs4trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Trond Myklebust10717f42020-01-27 09:58:19 -050028#define NFS_DEFAULT_DELEGATION_WATERMARK (5000U)
29
Trond Myklebustd2269ea2020-01-27 09:58:18 -050030static atomic_long_t nfs_active_delegations;
Trond Myklebust10717f42020-01-27 09:58:19 -050031static unsigned nfs_delegation_watermark = NFS_DEFAULT_DELEGATION_WATERMARK;
Trond Myklebustd2269ea2020-01-27 09:58:18 -050032
33static void __nfs_free_delegation(struct nfs_delegation *delegation)
Trond Myklebust905f8d12007-08-06 12:18:34 -040034{
NeilBrowna52458b2018-12-03 11:30:31 +110035 put_cred(delegation->cred);
36 delegation->cred = NULL;
Lai Jiangshan26f04dd2011-05-01 06:21:54 -070037 kfree_rcu(delegation, rcu);
Trond Myklebust8383e462007-07-06 15:12:04 -040038}
39
Trond Myklebustd2269ea2020-01-27 09:58:18 -050040static void nfs_mark_delegation_revoked(struct nfs_delegation *delegation)
41{
42 if (!test_and_set_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
43 delegation->stateid.type = NFS4_INVALID_STATEID_TYPE;
44 atomic_long_dec(&nfs_active_delegations);
Trond Myklebustefeda802020-02-05 09:01:54 -050045 if (!test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
46 nfs_clear_verifier_delegated(delegation->inode);
Trond Myklebustd2269ea2020-01-27 09:58:18 -050047 }
48}
49
Trond Myklebust8c755932020-02-13 14:51:06 -050050static struct nfs_delegation *nfs_get_delegation(struct nfs_delegation *delegation)
51{
52 refcount_inc(&delegation->refcount);
53 return delegation;
54}
55
56static void nfs_put_delegation(struct nfs_delegation *delegation)
57{
58 if (refcount_dec_and_test(&delegation->refcount))
59 __nfs_free_delegation(delegation);
60}
61
Trond Myklebustd2269ea2020-01-27 09:58:18 -050062static void nfs_free_delegation(struct nfs_delegation *delegation)
63{
64 nfs_mark_delegation_revoked(delegation);
Trond Myklebust8c755932020-02-13 14:51:06 -050065 nfs_put_delegation(delegation);
Trond Myklebustd2269ea2020-01-27 09:58:18 -050066}
67
Chuck Leverd3978bb2010-12-24 01:33:04 +000068/**
69 * nfs_mark_delegation_referenced - set delegation's REFERENCED flag
70 * @delegation: delegation to process
71 *
72 */
Trond Myklebustb7391f42008-12-23 15:21:52 -050073void nfs_mark_delegation_referenced(struct nfs_delegation *delegation)
74{
75 set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags);
76}
77
Trond Myklebustbe200372021-05-08 10:01:32 -040078static void nfs_mark_return_delegation(struct nfs_server *server,
79 struct nfs_delegation *delegation)
80{
81 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
82 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
83}
84
Trond Myklebustaa05c872016-09-22 13:38:54 -040085static bool
86nfs4_is_valid_delegation(const struct nfs_delegation *delegation,
87 fmode_t flags)
88{
89 if (delegation != NULL && (delegation->type & flags) == flags &&
90 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) &&
91 !test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
92 return true;
93 return false;
94}
95
Trond Myklebustbe3df3d2019-10-31 18:40:32 -040096struct nfs_delegation *nfs4_get_valid_delegation(const struct inode *inode)
97{
98 struct nfs_delegation *delegation;
99
100 delegation = rcu_dereference(NFS_I(inode)->delegation);
101 if (nfs4_is_valid_delegation(delegation, 0))
102 return delegation;
103 return NULL;
104}
105
Peng Tao15bb3af2014-07-03 13:05:00 +0800106static int
107nfs4_do_check_delegation(struct inode *inode, fmode_t flags, bool mark)
Trond Myklebustb7391f42008-12-23 15:21:52 -0500108{
109 struct nfs_delegation *delegation;
110 int ret = 0;
111
112 flags &= FMODE_READ|FMODE_WRITE;
113 rcu_read_lock();
114 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustaa05c872016-09-22 13:38:54 -0400115 if (nfs4_is_valid_delegation(delegation, flags)) {
Peng Tao15bb3af2014-07-03 13:05:00 +0800116 if (mark)
117 nfs_mark_delegation_referenced(delegation);
Trond Myklebustb7391f42008-12-23 15:21:52 -0500118 ret = 1;
119 }
120 rcu_read_unlock();
121 return ret;
122}
Peng Tao15bb3af2014-07-03 13:05:00 +0800123/**
Trond Myklebust6453bcd2021-03-04 20:29:50 -0500124 * nfs4_have_delegation - check if inode has a delegation, mark it
Peng Tao15bb3af2014-07-03 13:05:00 +0800125 * NFS_DELEGATION_REFERENCED if there is one.
126 * @inode: inode to check
127 * @flags: delegation types to check for
128 *
129 * Returns one if inode has the indicated delegation, otherwise zero.
130 */
131int nfs4_have_delegation(struct inode *inode, fmode_t flags)
132{
133 return nfs4_do_check_delegation(inode, flags, true);
134}
135
136/*
137 * nfs4_check_delegation - check if inode has a delegation, do not mark
138 * NFS_DELEGATION_REFERENCED if it has one.
139 */
140int nfs4_check_delegation(struct inode *inode, fmode_t flags)
141{
142 return nfs4_do_check_delegation(inode, flags, false);
143}
Trond Myklebustb7391f42008-12-23 15:21:52 -0500144
Olga Kornievskaia44f411c2018-10-04 14:45:00 -0400145static int nfs_delegation_claim_locks(struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -0500146{
147 struct inode *inode = state->inode;
148 struct file_lock *fl;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500149 struct file_lock_context *flctx = inode->i_flctx;
150 struct list_head *list;
Trond Myklebustd5122202009-06-17 13:22:58 -0700151 int status = 0;
Trond Myklebust888e6942005-11-04 15:38:11 -0500152
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500153 if (flctx == NULL)
Trond Myklebust65b62a22013-02-07 10:54:07 -0500154 goto out;
Jeff Layton314d7cc2013-04-10 15:36:48 -0400155
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500156 list = &flctx->flc_posix;
Jeff Layton6109c852015-01-16 15:05:57 -0500157 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500158restart:
159 list_for_each_entry(fl, list, fl_list) {
Olga Kornievskaia44f411c2018-10-04 14:45:00 -0400160 if (nfs_file_open_context(fl->fl_file)->state != state)
Trond Myklebust888e6942005-11-04 15:38:11 -0500161 continue;
Jeff Layton6109c852015-01-16 15:05:57 -0500162 spin_unlock(&flctx->flc_lock);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -0400163 status = nfs4_lock_delegation_recall(fl, state, stateid);
Trond Myklebustd5122202009-06-17 13:22:58 -0700164 if (status < 0)
Trond Myklebust3f09df72009-06-17 13:23:00 -0700165 goto out;
Jeff Layton6109c852015-01-16 15:05:57 -0500166 spin_lock(&flctx->flc_lock);
Trond Myklebust888e6942005-11-04 15:38:11 -0500167 }
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500168 if (list == &flctx->flc_posix) {
169 list = &flctx->flc_flock;
170 goto restart;
Jeff Layton5263e312015-01-16 15:05:55 -0500171 }
Jeff Layton6109c852015-01-16 15:05:57 -0500172 spin_unlock(&flctx->flc_lock);
Trond Myklebust3f09df72009-06-17 13:23:00 -0700173out:
Trond Myklebust888e6942005-11-04 15:38:11 -0500174 return status;
175}
176
Trond Myklebust24311f82015-09-20 10:50:17 -0400177static int nfs_delegation_claim_opens(struct inode *inode,
178 const nfs4_stateid *stateid, fmode_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 struct nfs_inode *nfsi = NFS_I(inode);
181 struct nfs_open_context *ctx;
Trond Myklebustd25be542013-02-05 11:43:28 -0500182 struct nfs4_state_owner *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 struct nfs4_state *state;
Trond Myklebustd25be542013-02-05 11:43:28 -0500184 unsigned int seq;
Trond Myklebust888e6942005-11-04 15:38:11 -0500185 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187again:
Trond Myklebust0de43972018-09-02 15:57:01 -0400188 rcu_read_lock();
189 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 state = ctx->state;
191 if (state == NULL)
192 continue;
193 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
194 continue;
Trond Myklebustf8ebf7a2014-10-17 23:02:52 +0300195 if (!nfs4_valid_open_stateid(state))
196 continue;
Trond Myklebustf597c532012-03-04 18:13:56 -0500197 if (!nfs4_stateid_match(&state->stateid, stateid))
Trond Myklebust90163022007-07-05 14:55:18 -0400198 continue;
Trond Myklebust0de43972018-09-02 15:57:01 -0400199 if (!get_nfs_open_context(ctx))
200 continue;
201 rcu_read_unlock();
Trond Myklebustd25be542013-02-05 11:43:28 -0500202 sp = state->owner;
Trond Myklebust65b62a22013-02-07 10:54:07 -0500203 /* Block nfs4_proc_unlck */
204 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebustd25be542013-02-05 11:43:28 -0500205 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
Trond Myklebust5eb8d182019-07-19 14:08:37 -0400206 err = nfs4_open_delegation_recall(ctx, state, stateid);
Trond Myklebustd25be542013-02-05 11:43:28 -0500207 if (!err)
Olga Kornievskaia44f411c2018-10-04 14:45:00 -0400208 err = nfs_delegation_claim_locks(state, stateid);
Trond Myklebustd25be542013-02-05 11:43:28 -0500209 if (!err && read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
210 err = -EAGAIN;
Trond Myklebust65b62a22013-02-07 10:54:07 -0500211 mutex_unlock(&sp->so_delegreturn_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 put_nfs_open_context(ctx);
Trond Myklebust888e6942005-11-04 15:38:11 -0500213 if (err != 0)
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500214 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 goto again;
216 }
Trond Myklebust0de43972018-09-02 15:57:01 -0400217 rcu_read_unlock();
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500218 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
Chuck Leverd3978bb2010-12-24 01:33:04 +0000221/**
222 * nfs_inode_reclaim_delegation - process a delegation reclaim request
223 * @inode: inode to process
224 * @cred: credential to use for request
Trond Myklebust35156bf2018-03-20 17:03:13 -0400225 * @type: delegation type
226 * @stateid: delegation stateid
227 * @pagemod_limit: write delegation "space_limit"
Chuck Leverd3978bb2010-12-24 01:33:04 +0000228 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 */
NeilBrowna52458b2018-12-03 11:30:31 +1100230void nfs_inode_reclaim_delegation(struct inode *inode, const struct cred *cred,
Trond Myklebust35156bf2018-03-20 17:03:13 -0400231 fmode_t type,
232 const nfs4_stateid *stateid,
233 unsigned long pagemod_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Trond Myklebust8f649c32010-05-01 12:36:18 -0400235 struct nfs_delegation *delegation;
NeilBrowna52458b2018-12-03 11:30:31 +1100236 const struct cred *oldcred = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Trond Myklebust8f649c32010-05-01 12:36:18 -0400238 rcu_read_lock();
239 delegation = rcu_dereference(NFS_I(inode)->delegation);
240 if (delegation != NULL) {
241 spin_lock(&delegation->lock);
Trond Myklebust1deed572019-10-22 08:52:47 -0400242 if (nfs4_is_valid_delegation(delegation, 0)) {
Trond Myklebust35156bf2018-03-20 17:03:13 -0400243 nfs4_stateid_copy(&delegation->stateid, stateid);
244 delegation->type = type;
245 delegation->pagemod_limit = pagemod_limit;
Trond Myklebust8f649c32010-05-01 12:36:18 -0400246 oldcred = delegation->cred;
NeilBrowna52458b2018-12-03 11:30:31 +1100247 delegation->cred = get_cred(cred);
Trond Myklebust8f649c32010-05-01 12:36:18 -0400248 clear_bit(NFS_DELEGATION_NEED_RECLAIM,
249 &delegation->flags);
Trond Myklebust8f649c32010-05-01 12:36:18 -0400250 spin_unlock(&delegation->lock);
Trond Myklebust8f649c32010-05-01 12:36:18 -0400251 rcu_read_unlock();
NeilBrowna52458b2018-12-03 11:30:31 +1100252 put_cred(oldcred);
Trond Myklebust35156bf2018-03-20 17:03:13 -0400253 trace_nfs4_reclaim_delegation(inode, type);
Trond Myklebustb1a318d2016-09-22 13:39:12 -0400254 return;
Trond Myklebust8f649c32010-05-01 12:36:18 -0400255 }
Trond Myklebustb1a318d2016-09-22 13:39:12 -0400256 /* We appear to have raced with a delegation return. */
257 spin_unlock(&delegation->lock);
Trond Myklebust8f649c32010-05-01 12:36:18 -0400258 }
Trond Myklebustb1a318d2016-09-22 13:39:12 -0400259 rcu_read_unlock();
Trond Myklebust35156bf2018-03-20 17:03:13 -0400260 nfs_inode_set_delegation(inode, cred, type, stateid, pagemod_limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
Trond Myklebust57bfa892008-01-25 16:38:18 -0500263static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
264{
Trond Myklebust5d639442020-02-13 14:51:07 -0500265 const struct cred *cred;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500266 int res = 0;
267
Trond Myklebust5d639442020-02-13 14:51:07 -0500268 if (!test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
269 spin_lock(&delegation->lock);
270 cred = get_cred(delegation->cred);
271 spin_unlock(&delegation->lock);
272 res = nfs4_proc_delegreturn(inode, cred,
Trond Myklebust869f9df2014-11-10 18:43:56 -0500273 &delegation->stateid,
274 issync);
Trond Myklebust5d639442020-02-13 14:51:07 -0500275 put_cred(cred);
276 }
Trond Myklebust57bfa892008-01-25 16:38:18 -0500277 return res;
278}
279
Trond Myklebust86e89482008-12-23 15:21:39 -0500280static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
281{
282 struct inode *inode = NULL;
283
284 spin_lock(&delegation->lock);
285 if (delegation->inode != NULL)
286 inode = igrab(delegation->inode);
Trond Myklebust6f9449b2019-02-21 14:51:25 -0500287 if (!inode)
288 set_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags);
Trond Myklebust86e89482008-12-23 15:21:39 -0500289 spin_unlock(&delegation->lock);
290 return inode;
291}
292
Chuck Leverdda4b222010-12-24 01:32:54 +0000293static struct nfs_delegation *
Trond Myklebustd25be542013-02-05 11:43:28 -0500294nfs_start_delegation_return_locked(struct nfs_inode *nfsi)
Trond Myklebust57bfa892008-01-25 16:38:18 -0500295{
Trond Myklebustd25be542013-02-05 11:43:28 -0500296 struct nfs_delegation *ret = NULL;
297 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500298
299 if (delegation == NULL)
Trond Myklebustd25be542013-02-05 11:43:28 -0500300 goto out;
301 spin_lock(&delegation->lock);
Trond Myklebust8c755932020-02-13 14:51:06 -0500302 if (!test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) {
Trond Myklebustbe200372021-05-08 10:01:32 -0400303 clear_bit(NFS_DELEGATION_RETURN_DELAYED, &delegation->flags);
Trond Myklebust8c755932020-02-13 14:51:06 -0500304 /* Refcount matched in nfs_end_delegation_return() */
305 ret = nfs_get_delegation(delegation);
306 }
Trond Myklebustd25be542013-02-05 11:43:28 -0500307 spin_unlock(&delegation->lock);
Trond Myklebustefeda802020-02-05 09:01:54 -0500308 if (ret)
309 nfs_clear_verifier_delegated(&nfsi->vfs_inode);
Trond Myklebustd25be542013-02-05 11:43:28 -0500310out:
311 return ret;
312}
313
314static struct nfs_delegation *
315nfs_start_delegation_return(struct nfs_inode *nfsi)
316{
317 struct nfs_delegation *delegation;
318
319 rcu_read_lock();
320 delegation = nfs_start_delegation_return_locked(nfsi);
321 rcu_read_unlock();
322 return delegation;
323}
324
Trond Myklebustbe200372021-05-08 10:01:32 -0400325static void nfs_abort_delegation_return(struct nfs_delegation *delegation,
326 struct nfs_client *clp, int err)
Trond Myklebustd25be542013-02-05 11:43:28 -0500327{
Chuck Leverdda4b222010-12-24 01:32:54 +0000328
Trond Myklebust34310432008-12-23 15:21:38 -0500329 spin_lock(&delegation->lock);
Trond Myklebustd25be542013-02-05 11:43:28 -0500330 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
Trond Myklebustbe200372021-05-08 10:01:32 -0400331 if (err == -EAGAIN) {
332 set_bit(NFS_DELEGATION_RETURN_DELAYED, &delegation->flags);
333 set_bit(NFS4CLNT_DELEGRETURN_DELAYED, &clp->cl_state);
334 }
Trond Myklebustd25be542013-02-05 11:43:28 -0500335 spin_unlock(&delegation->lock);
Trond Myklebustd25be542013-02-05 11:43:28 -0500336}
337
338static struct nfs_delegation *
339nfs_detach_delegation_locked(struct nfs_inode *nfsi,
340 struct nfs_delegation *delegation,
341 struct nfs_client *clp)
342{
343 struct nfs_delegation *deleg_cur =
344 rcu_dereference_protected(nfsi->delegation,
345 lockdep_is_held(&clp->cl_lock));
346
347 if (deleg_cur == NULL || delegation != deleg_cur)
348 return NULL;
349
350 spin_lock(&delegation->lock);
Trond Myklebustf9e0cc92019-10-21 14:12:13 -0400351 if (!delegation->inode) {
352 spin_unlock(&delegation->lock);
353 return NULL;
354 }
Trond Myklebust57bfa892008-01-25 16:38:18 -0500355 list_del_rcu(&delegation->super_list);
Trond Myklebust86e89482008-12-23 15:21:39 -0500356 delegation->inode = NULL;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500357 rcu_assign_pointer(nfsi->delegation, NULL);
Trond Myklebust34310432008-12-23 15:21:38 -0500358 spin_unlock(&delegation->lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500359 return delegation;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500360}
361
Chuck Leverdda4b222010-12-24 01:32:54 +0000362static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi,
Trond Myklebustd25be542013-02-05 11:43:28 -0500363 struct nfs_delegation *delegation,
364 struct nfs_server *server)
Chuck Leverdda4b222010-12-24 01:32:54 +0000365{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000366 struct nfs_client *clp = server->nfs_client;
Chuck Leverdda4b222010-12-24 01:32:54 +0000367
368 spin_lock(&clp->cl_lock);
Trond Myklebustd25be542013-02-05 11:43:28 -0500369 delegation = nfs_detach_delegation_locked(nfsi, delegation, clp);
Chuck Leverdda4b222010-12-24 01:32:54 +0000370 spin_unlock(&clp->cl_lock);
371 return delegation;
372}
373
Trond Myklebustd25be542013-02-05 11:43:28 -0500374static struct nfs_delegation *
375nfs_inode_detach_delegation(struct inode *inode)
376{
377 struct nfs_inode *nfsi = NFS_I(inode);
378 struct nfs_server *server = NFS_SERVER(inode);
379 struct nfs_delegation *delegation;
380
Trond Myklebustee05f452019-10-21 13:56:59 -0400381 rcu_read_lock();
382 delegation = rcu_dereference(nfsi->delegation);
383 if (delegation != NULL)
384 delegation = nfs_detach_delegation(nfsi, delegation, server);
385 rcu_read_unlock();
386 return delegation;
Trond Myklebustd25be542013-02-05 11:43:28 -0500387}
388
Trond Myklebustcf6726e2014-12-19 11:22:28 -0500389static void
Trond Myklebust57f188e2020-01-27 12:44:41 -0500390nfs_update_delegation_cred(struct nfs_delegation *delegation,
391 const struct cred *cred)
392{
393 const struct cred *old;
394
395 if (cred_fscmp(delegation->cred, cred) != 0) {
396 old = xchg(&delegation->cred, get_cred(cred));
397 put_cred(old);
398 }
399}
400
401static void
Trond Myklebustcf6726e2014-12-19 11:22:28 -0500402nfs_update_inplace_delegation(struct nfs_delegation *delegation,
403 const struct nfs_delegation *update)
404{
405 if (nfs4_stateid_is_newer(&update->stateid, &delegation->stateid)) {
406 delegation->stateid.seqid = update->stateid.seqid;
407 smp_wmb();
408 delegation->type = update->type;
Trond Myklebust57f188e2020-01-27 12:44:41 -0500409 delegation->pagemod_limit = update->pagemod_limit;
410 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
411 delegation->change_attr = update->change_attr;
412 nfs_update_delegation_cred(delegation, update->cred);
413 /* smp_mb__before_atomic() is implicit due to xchg() */
414 clear_bit(NFS_DELEGATION_REVOKED, &delegation->flags);
Trond Myklebustd2269ea2020-01-27 09:58:18 -0500415 atomic_long_inc(&nfs_active_delegations);
Trond Myklebust57f188e2020-01-27 12:44:41 -0500416 }
Trond Myklebustcf6726e2014-12-19 11:22:28 -0500417 }
418}
419
Chuck Leverd3978bb2010-12-24 01:33:04 +0000420/**
421 * nfs_inode_set_delegation - set up a delegation on an inode
422 * @inode: inode to which delegation applies
423 * @cred: cred to use for subsequent delegation processing
Trond Myklebust35156bf2018-03-20 17:03:13 -0400424 * @type: delegation type
425 * @stateid: delegation stateid
426 * @pagemod_limit: write delegation "space_limit"
Chuck Leverd3978bb2010-12-24 01:33:04 +0000427 *
428 * Returns zero on success, or a negative errno value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 */
NeilBrowna52458b2018-12-03 11:30:31 +1100430int nfs_inode_set_delegation(struct inode *inode, const struct cred *cred,
Trond Myklebust35156bf2018-03-20 17:03:13 -0400431 fmode_t type,
432 const nfs4_stateid *stateid,
433 unsigned long pagemod_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000435 struct nfs_server *server = NFS_SERVER(inode);
436 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 struct nfs_inode *nfsi = NFS_I(inode);
David Howells17d2c0a2010-05-01 12:37:18 -0400438 struct nfs_delegation *delegation, *old_delegation;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500439 struct nfs_delegation *freeme = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 int status = 0;
441
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400442 delegation = kmalloc(sizeof(*delegation), GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (delegation == NULL)
444 return -ENOMEM;
Trond Myklebust35156bf2018-03-20 17:03:13 -0400445 nfs4_stateid_copy(&delegation->stateid, stateid);
Trond Myklebust8c755932020-02-13 14:51:06 -0500446 refcount_set(&delegation->refcount, 1);
Trond Myklebust35156bf2018-03-20 17:03:13 -0400447 delegation->type = type;
448 delegation->pagemod_limit = pagemod_limit;
Jeff Layton1eb5d982018-01-09 08:21:17 -0500449 delegation->change_attr = inode_peek_iversion_raw(inode);
NeilBrowna52458b2018-12-03 11:30:31 +1100450 delegation->cred = get_cred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 delegation->inode = inode;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500452 delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
Trond Myklebust34310432008-12-23 15:21:38 -0500453 spin_lock_init(&delegation->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 spin_lock(&clp->cl_lock);
David Howells17d2c0a2010-05-01 12:37:18 -0400456 old_delegation = rcu_dereference_protected(nfsi->delegation,
Chuck Leverd3978bb2010-12-24 01:33:04 +0000457 lockdep_is_held(&clp->cl_lock));
Trond Myklebustee05f452019-10-21 13:56:59 -0400458 if (old_delegation == NULL)
459 goto add_new;
460 /* Is this an update of the existing delegation? */
461 if (nfs4_stateid_match_other(&old_delegation->stateid,
462 &delegation->stateid)) {
463 spin_lock(&old_delegation->lock);
464 nfs_update_inplace_delegation(old_delegation,
465 delegation);
466 spin_unlock(&old_delegation->lock);
467 goto out;
468 }
469 if (!test_bit(NFS_DELEGATION_REVOKED, &old_delegation->flags)) {
Trond Myklebust57bfa892008-01-25 16:38:18 -0500470 /*
471 * Deal with broken servers that hand out two
472 * delegations for the same file.
Trond Myklebust17280172012-03-11 13:11:00 -0400473 * Allow for upgrades to a WRITE delegation, but
474 * nothing else.
Trond Myklebust57bfa892008-01-25 16:38:18 -0500475 */
476 dfprintk(FILE, "%s: server %s handed out "
477 "a duplicate delegation!\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700478 __func__, clp->cl_hostname);
Trond Myklebust17280172012-03-11 13:11:00 -0400479 if (delegation->type == old_delegation->type ||
480 !(delegation->type & FMODE_WRITE)) {
Trond Myklebust57bfa892008-01-25 16:38:18 -0500481 freeme = delegation;
482 delegation = NULL;
483 goto out;
484 }
Trond Myklebustade04642015-02-27 14:25:50 -0500485 if (test_and_set_bit(NFS_DELEGATION_RETURNING,
486 &old_delegation->flags))
487 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
Trond Myklebustee05f452019-10-21 13:56:59 -0400489 freeme = nfs_detach_delegation_locked(nfsi, old_delegation, clp);
490 if (freeme == NULL)
491 goto out;
492add_new:
Trond Myklebustcc7f2da2021-04-11 14:31:24 -0400493 /*
494 * If we didn't revalidate the change attribute before setting
495 * the delegation, then pre-emptively ask for a full attribute
496 * cache revalidation.
497 */
498 spin_lock(&inode->i_lock);
499 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_CHANGE)
500 nfs_set_cache_invalid(inode,
501 NFS_INO_INVALID_ATIME | NFS_INO_INVALID_CTIME |
502 NFS_INO_INVALID_MTIME | NFS_INO_INVALID_SIZE |
503 NFS_INO_INVALID_BLOCKS | NFS_INO_INVALID_NLINK |
504 NFS_INO_INVALID_OTHER | NFS_INO_INVALID_DATA |
505 NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL |
506 NFS_INO_INVALID_XATTR);
507 spin_unlock(&inode->i_lock);
508
Trond Myklebust38942ba2015-03-04 15:59:05 -0500509 list_add_tail_rcu(&delegation->super_list, &server->delegations);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500510 rcu_assign_pointer(nfsi->delegation, delegation);
511 delegation = NULL;
Trond Myklebust412c77c2007-07-03 16:10:55 -0400512
Trond Myklebustd2269ea2020-01-27 09:58:18 -0500513 atomic_long_inc(&nfs_active_delegations);
514
Trond Myklebust35156bf2018-03-20 17:03:13 -0400515 trace_nfs4_set_delegation(inode, type);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500516out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 spin_unlock(&clp->cl_lock);
Trond Myklebust603c83d2007-10-18 19:59:20 -0400518 if (delegation != NULL)
Trond Myklebustd2269ea2020-01-27 09:58:18 -0500519 __nfs_free_delegation(delegation);
Trond Myklebustee05f452019-10-21 13:56:59 -0400520 if (freeme != NULL) {
Trond Myklebust57bfa892008-01-25 16:38:18 -0500521 nfs_do_return_delegation(inode, freeme, 0);
Trond Myklebustee05f452019-10-21 13:56:59 -0400522 nfs_free_delegation(freeme);
523 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 return status;
525}
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527/*
528 * Basic procedure for returning a delegation to the server
529 */
Trond Myklebustd25be542013-02-05 11:43:28 -0500530static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531{
Trond Myklebustd25be542013-02-05 11:43:28 -0500532 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Trond Myklebust6b4befc2021-05-07 09:14:37 -0400533 unsigned int mode = O_WRONLY | O_RDWR;
Trond Myklebust869f9df2014-11-10 18:43:56 -0500534 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Trond Myklebustd25be542013-02-05 11:43:28 -0500536 if (delegation == NULL)
537 return 0;
Trond Myklebust6b4befc2021-05-07 09:14:37 -0400538
539 if (!issync)
540 mode |= O_NONBLOCK;
541 /* Recall of any remaining application leases */
542 err = break_lease(inode, mode);
543
544 while (err == 0) {
Trond Myklebust869f9df2014-11-10 18:43:56 -0500545 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
546 break;
Trond Myklebust24311f82015-09-20 10:50:17 -0400547 err = nfs_delegation_claim_opens(inode, &delegation->stateid,
548 delegation->type);
Trond Myklebustd25be542013-02-05 11:43:28 -0500549 if (!issync || err != -EAGAIN)
550 break;
551 /*
552 * Guard against state recovery
553 */
554 err = nfs4_wait_clnt_recover(clp);
Trond Myklebust6b4befc2021-05-07 09:14:37 -0400555 }
Trond Myklebustd25be542013-02-05 11:43:28 -0500556
557 if (err) {
Trond Myklebustbe200372021-05-08 10:01:32 -0400558 nfs_abort_delegation_return(delegation, clp, err);
Trond Myklebustd25be542013-02-05 11:43:28 -0500559 goto out;
560 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500562 err = nfs_do_return_delegation(inode, delegation, issync);
563out:
Trond Myklebust8c755932020-02-13 14:51:06 -0500564 /* Refcount matched in nfs_start_delegation_return_locked() */
565 nfs_put_delegation(delegation);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500566 return err;
Trond Myklebust90163022007-07-05 14:55:18 -0400567}
568
Trond Myklebustb7571442013-04-03 14:33:49 -0400569static bool nfs_delegation_need_return(struct nfs_delegation *delegation)
570{
571 bool ret = false;
572
573 if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
574 ret = true;
Trond Myklebust0d104162020-01-27 09:58:16 -0500575 else if (test_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags)) {
Trond Myklebustb7571442013-04-03 14:33:49 -0400576 struct inode *inode;
577
578 spin_lock(&delegation->lock);
579 inode = delegation->inode;
580 if (inode && list_empty(&NFS_I(inode)->open_files))
581 ret = true;
582 spin_unlock(&delegation->lock);
583 }
Trond Myklebust0d104162020-01-27 09:58:16 -0500584 if (ret)
585 clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
Trond Myklebustaf20b7b2019-10-22 13:40:47 -0400586 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags) ||
Trond Myklebustbe200372021-05-08 10:01:32 -0400587 test_bit(NFS_DELEGATION_RETURN_DELAYED, &delegation->flags) ||
Trond Myklebustaf20b7b2019-10-22 13:40:47 -0400588 test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
589 ret = false;
590
Trond Myklebustb7571442013-04-03 14:33:49 -0400591 return ret;
592}
593
Trond Myklebustaf3b61b2020-02-27 08:29:02 -0500594static int nfs_server_return_marked_delegations(struct nfs_server *server,
595 void __always_unused *data)
596{
597 struct nfs_delegation *delegation;
598 struct nfs_delegation *prev;
599 struct inode *inode;
600 struct inode *place_holder = NULL;
601 struct nfs_delegation *place_holder_deleg = NULL;
602 int err = 0;
603
604restart:
605 /*
606 * To avoid quadratic looping we hold a reference
607 * to an inode place_holder. Each time we restart, we
608 * list delegation in the server from the delegations
609 * of that inode.
610 * prev is an RCU-protected pointer to a delegation which
611 * wasn't marked for return and might be a good choice for
612 * the next place_holder.
613 */
614 prev = NULL;
615 delegation = NULL;
616 rcu_read_lock();
617 if (place_holder)
618 delegation = rcu_dereference(NFS_I(place_holder)->delegation);
619 if (!delegation || delegation != place_holder_deleg)
620 delegation = list_entry_rcu(server->delegations.next,
621 struct nfs_delegation, super_list);
622 list_for_each_entry_from_rcu(delegation, &server->delegations, super_list) {
623 struct inode *to_put = NULL;
624
625 if (test_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags))
626 continue;
627 if (!nfs_delegation_need_return(delegation)) {
628 if (nfs4_is_valid_delegation(delegation, 0))
629 prev = delegation;
630 continue;
631 }
632
633 if (prev) {
634 struct inode *tmp = nfs_delegation_grab_inode(prev);
635 if (tmp) {
636 to_put = place_holder;
637 place_holder = tmp;
638 place_holder_deleg = prev;
639 }
640 }
641
642 inode = nfs_delegation_grab_inode(delegation);
643 if (inode == NULL) {
644 rcu_read_unlock();
645 iput(to_put);
646 goto restart;
647 }
648 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
649 rcu_read_unlock();
650
651 iput(to_put);
652
653 err = nfs_end_delegation_return(inode, delegation, 0);
654 iput(inode);
655 cond_resched();
656 if (!err)
657 goto restart;
658 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
659 goto out;
660 }
661 rcu_read_unlock();
662out:
663 iput(place_holder);
664 return err;
665}
666
Trond Myklebustbe200372021-05-08 10:01:32 -0400667static bool nfs_server_clear_delayed_delegations(struct nfs_server *server)
668{
669 struct nfs_delegation *d;
670 bool ret = false;
671
672 list_for_each_entry_rcu (d, &server->delegations, super_list) {
673 if (!test_bit(NFS_DELEGATION_RETURN_DELAYED, &d->flags))
674 continue;
675 nfs_mark_return_delegation(server, d);
676 clear_bit(NFS_DELEGATION_RETURN_DELAYED, &d->flags);
677 ret = true;
678 }
679 return ret;
680}
681
682static bool nfs_client_clear_delayed_delegations(struct nfs_client *clp)
683{
684 struct nfs_server *server;
685 bool ret = false;
686
687 if (!test_and_clear_bit(NFS4CLNT_DELEGRETURN_DELAYED, &clp->cl_state))
688 goto out;
689 rcu_read_lock();
690 list_for_each_entry_rcu (server, &clp->cl_superblocks, client_link) {
691 if (nfs_server_clear_delayed_delegations(server))
692 ret = true;
693 }
694 rcu_read_unlock();
695out:
696 return ret;
697}
698
Chuck Leverd3978bb2010-12-24 01:33:04 +0000699/**
700 * nfs_client_return_marked_delegations - return previously marked delegations
701 * @clp: nfs_client to process
702 *
Trond Myklebustdc327ed2012-05-06 19:46:30 -0400703 * Note that this function is designed to be called by the state
704 * manager thread. For this reason, it cannot flush the dirty data,
705 * since that could deadlock in case of a state recovery error.
706 *
Chuck Leverd3978bb2010-12-24 01:33:04 +0000707 * Returns zero on success, or a negative errno value.
Trond Myklebust515d8612008-12-23 15:21:46 -0500708 */
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500709int nfs_client_return_marked_delegations(struct nfs_client *clp)
Trond Myklebust515d8612008-12-23 15:21:46 -0500710{
Trond Myklebustbe200372021-05-08 10:01:32 -0400711 int err = nfs_client_for_each_server(
712 clp, nfs_server_return_marked_delegations, NULL);
713 if (err)
714 return err;
715 /* If a return was delayed, sleep to prevent hard looping */
716 if (nfs_client_clear_delayed_delegations(clp))
717 ssleep(1);
718 return 0;
Trond Myklebust515d8612008-12-23 15:21:46 -0500719}
720
Chuck Leverd3978bb2010-12-24 01:33:04 +0000721/**
Trond Myklebustb47e0e42019-10-21 14:04:00 -0400722 * nfs_inode_evict_delegation - return delegation, don't reclaim opens
Chuck Leverd3978bb2010-12-24 01:33:04 +0000723 * @inode: inode to process
724 *
725 * Does not protect against delegation reclaims, therefore really only safe
Trond Myklebustb47e0e42019-10-21 14:04:00 -0400726 * to be called from nfs4_clear_inode(). Guaranteed to always free
727 * the delegation structure.
Trond Myklebuste6f81072008-01-24 18:14:34 -0500728 */
Trond Myklebustb47e0e42019-10-21 14:04:00 -0400729void nfs_inode_evict_delegation(struct inode *inode)
Trond Myklebuste6f81072008-01-24 18:14:34 -0500730{
Trond Myklebuste6f81072008-01-24 18:14:34 -0500731 struct nfs_delegation *delegation;
732
Trond Myklebustd25be542013-02-05 11:43:28 -0500733 delegation = nfs_inode_detach_delegation(inode);
Trond Myklebustb47e0e42019-10-21 14:04:00 -0400734 if (delegation != NULL) {
Trond Myklebustf885ea62020-01-27 09:58:15 -0500735 set_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
Trond Myklebustb47e0e42019-10-21 14:04:00 -0400736 set_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags);
Trond Myklebust5fcdfac2015-03-25 13:19:42 -0400737 nfs_do_return_delegation(inode, delegation, 1);
Trond Myklebustee05f452019-10-21 13:56:59 -0400738 nfs_free_delegation(delegation);
Trond Myklebustb47e0e42019-10-21 14:04:00 -0400739 }
Trond Myklebuste6f81072008-01-24 18:14:34 -0500740}
741
Chuck Leverd3978bb2010-12-24 01:33:04 +0000742/**
Trond Myklebust6453bcd2021-03-04 20:29:50 -0500743 * nfs4_inode_return_delegation - synchronously return a delegation
Chuck Leverd3978bb2010-12-24 01:33:04 +0000744 * @inode: inode to process
745 *
Trond Myklebustc57d1bc2012-05-06 19:34:17 -0400746 * This routine will always flush any dirty data to disk on the
747 * assumption that if we need to return the delegation, then
748 * we should stop caching.
749 *
Chuck Leverd3978bb2010-12-24 01:33:04 +0000750 * Returns zero on success, or a negative errno value.
751 */
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400752int nfs4_inode_return_delegation(struct inode *inode)
Trond Myklebust90163022007-07-05 14:55:18 -0400753{
Trond Myklebust90163022007-07-05 14:55:18 -0400754 struct nfs_inode *nfsi = NFS_I(inode);
755 struct nfs_delegation *delegation;
Trond Myklebust90163022007-07-05 14:55:18 -0400756
Trond Myklebustd25be542013-02-05 11:43:28 -0500757 delegation = nfs_start_delegation_return(nfsi);
Trond Myklebust6e176d42021-10-10 10:58:12 +0200758 if (delegation != NULL) {
759 /* Synchronous recall of any application leases */
760 break_lease(inode, O_WRONLY | O_RDWR);
761 if (S_ISREG(inode->i_mode))
762 nfs_wb_all(inode);
Trond Myklebust6b4befc2021-05-07 09:14:37 -0400763 return nfs_end_delegation_return(inode, delegation, 1);
Trond Myklebust6e176d42021-10-10 10:58:12 +0200764 }
Trond Myklebust6b4befc2021-05-07 09:14:37 -0400765 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
Trond Myklebustc01d3642018-03-20 16:43:20 -0400768/**
Trond Myklebust6453bcd2021-03-04 20:29:50 -0500769 * nfs4_inode_return_delegation_on_close - asynchronously return a delegation
Trond Myklebustb7b7dac2020-01-27 09:58:17 -0500770 * @inode: inode to process
771 *
772 * This routine is called on file close in order to determine if the
773 * inode delegation needs to be returned immediately.
774 */
775void nfs4_inode_return_delegation_on_close(struct inode *inode)
776{
777 struct nfs_delegation *delegation;
778 struct nfs_delegation *ret = NULL;
779
780 if (!inode)
781 return;
782 rcu_read_lock();
783 delegation = nfs4_get_valid_delegation(inode);
784 if (!delegation)
785 goto out;
Trond Myklebust10717f42020-01-27 09:58:19 -0500786 if (test_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags) ||
787 atomic_long_read(&nfs_active_delegations) >= nfs_delegation_watermark) {
Trond Myklebustb7b7dac2020-01-27 09:58:17 -0500788 spin_lock(&delegation->lock);
789 if (delegation->inode &&
790 list_empty(&NFS_I(inode)->open_files) &&
791 !test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) {
792 clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
Trond Myklebust8c755932020-02-13 14:51:06 -0500793 /* Refcount matched in nfs_end_delegation_return() */
794 ret = nfs_get_delegation(delegation);
Trond Myklebustb7b7dac2020-01-27 09:58:17 -0500795 }
796 spin_unlock(&delegation->lock);
Trond Myklebustefeda802020-02-05 09:01:54 -0500797 if (ret)
798 nfs_clear_verifier_delegated(inode);
Trond Myklebustb7b7dac2020-01-27 09:58:17 -0500799 }
800out:
801 rcu_read_unlock();
802 nfs_end_delegation_return(inode, ret, 0);
803}
804
805/**
Trond Myklebustc01d3642018-03-20 16:43:20 -0400806 * nfs4_inode_make_writeable
807 * @inode: pointer to inode
808 *
809 * Make the inode writeable by returning the delegation if necessary
810 *
811 * Returns zero on success, or a negative errno value.
812 */
813int nfs4_inode_make_writeable(struct inode *inode)
814{
Trond Myklebust3887ce12019-10-27 13:48:18 -0400815 struct nfs_delegation *delegation;
816
817 rcu_read_lock();
818 delegation = nfs4_get_valid_delegation(inode);
819 if (delegation == NULL ||
820 (nfs4_has_session(NFS_SERVER(inode)->nfs_client) &&
821 (delegation->type & FMODE_WRITE))) {
822 rcu_read_unlock();
823 return 0;
824 }
825 rcu_read_unlock();
826 return nfs4_inode_return_delegation(inode);
Trond Myklebustc01d3642018-03-20 16:43:20 -0400827}
828
Trond Myklebustb7571442013-04-03 14:33:49 -0400829static void nfs_mark_return_if_closed_delegation(struct nfs_server *server,
830 struct nfs_delegation *delegation)
831{
832 set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
833 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
834}
835
Trond Myklebust5c31e232013-04-03 19:04:58 -0400836static bool nfs_server_mark_return_all_delegations(struct nfs_server *server)
837{
838 struct nfs_delegation *delegation;
839 bool ret = false;
840
841 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
842 nfs_mark_return_delegation(server, delegation);
843 ret = true;
844 }
845 return ret;
846}
847
Trond Myklebustb02ba0b2013-04-03 19:23:58 -0400848static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
849{
850 struct nfs_server *server;
851
852 rcu_read_lock();
853 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
854 nfs_server_mark_return_all_delegations(server);
855 rcu_read_unlock();
856}
857
858static void nfs_delegation_run_state_manager(struct nfs_client *clp)
859{
860 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
861 nfs4_schedule_state_manager(clp);
862}
863
864/**
865 * nfs_expire_all_delegations
866 * @clp: client to process
867 *
868 */
869void nfs_expire_all_delegations(struct nfs_client *clp)
870{
871 nfs_client_mark_return_all_delegations(clp);
872 nfs_delegation_run_state_manager(clp);
873}
874
Chuck Leverd3978bb2010-12-24 01:33:04 +0000875/**
Trond Myklebust6453bcd2021-03-04 20:29:50 -0500876 * nfs_server_return_all_delegations - return delegations for one superblock
Trond Myklebust302fad72019-02-18 13:32:38 -0500877 * @server: pointer to nfs_server to process
Chuck Leverd3978bb2010-12-24 01:33:04 +0000878 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 */
Bryan Schumakereeebf912012-06-20 15:53:41 -0400880void nfs_server_return_all_delegations(struct nfs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000882 struct nfs_client *clp = server->nfs_client;
Trond Myklebust5c31e232013-04-03 19:04:58 -0400883 bool need_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 if (clp == NULL)
886 return;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000887
Trond Myklebust8383e462007-07-06 15:12:04 -0400888 rcu_read_lock();
Trond Myklebust5c31e232013-04-03 19:04:58 -0400889 need_wait = nfs_server_mark_return_all_delegations(server);
Trond Myklebust8383e462007-07-06 15:12:04 -0400890 rcu_read_unlock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000891
Trond Myklebust5c31e232013-04-03 19:04:58 -0400892 if (need_wait) {
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500893 nfs4_schedule_state_manager(clp);
Trond Myklebust5c31e232013-04-03 19:04:58 -0400894 nfs4_wait_clnt_recover(clp);
895 }
Trond Myklebust515d8612008-12-23 15:21:46 -0500896}
897
Trond Myklebust826e0012013-04-03 19:27:52 -0400898static void nfs_mark_return_unused_delegation_types(struct nfs_server *server,
Chuck Leverd3978bb2010-12-24 01:33:04 +0000899 fmode_t flags)
Trond Myklebust515d8612008-12-23 15:21:46 -0500900{
901 struct nfs_delegation *delegation;
902
Chuck Leverd3978bb2010-12-24 01:33:04 +0000903 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500904 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
905 continue;
906 if (delegation->type & flags)
Trond Myklebust826e0012013-04-03 19:27:52 -0400907 nfs_mark_return_if_closed_delegation(server, delegation);
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500908 }
Chuck Leverd3978bb2010-12-24 01:33:04 +0000909}
910
Trond Myklebust826e0012013-04-03 19:27:52 -0400911static void nfs_client_mark_return_unused_delegation_types(struct nfs_client *clp,
Chuck Leverd3978bb2010-12-24 01:33:04 +0000912 fmode_t flags)
913{
914 struct nfs_server *server;
915
916 rcu_read_lock();
917 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
Trond Myklebust826e0012013-04-03 19:27:52 -0400918 nfs_mark_return_unused_delegation_types(server, flags);
Trond Myklebust515d8612008-12-23 15:21:46 -0500919 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920}
921
Trond Myklebustee05f452019-10-21 13:56:59 -0400922static void nfs_revoke_delegation(struct inode *inode,
Trond Myklebust41020b62016-09-22 13:38:58 -0400923 const nfs4_stateid *stateid)
924{
925 struct nfs_delegation *delegation;
Trond Myklebust7f048832016-09-22 13:39:14 -0400926 nfs4_stateid tmp;
Trond Myklebust41020b62016-09-22 13:38:58 -0400927 bool ret = false;
928
929 rcu_read_lock();
930 delegation = rcu_dereference(NFS_I(inode)->delegation);
931 if (delegation == NULL)
932 goto out;
Trond Myklebust7f048832016-09-22 13:39:14 -0400933 if (stateid == NULL) {
934 nfs4_stateid_copy(&tmp, &delegation->stateid);
935 stateid = &tmp;
Trond Myklebustf2d47b52019-10-21 14:15:32 -0400936 } else {
937 if (!nfs4_stateid_match_other(stateid, &delegation->stateid))
938 goto out;
939 spin_lock(&delegation->lock);
940 if (stateid->seqid) {
941 if (nfs4_stateid_is_newer(&delegation->stateid, stateid)) {
942 spin_unlock(&delegation->lock);
943 goto out;
944 }
945 delegation->stateid.seqid = stateid->seqid;
946 }
947 spin_unlock(&delegation->lock);
948 }
Trond Myklebustd2269ea2020-01-27 09:58:18 -0500949 nfs_mark_delegation_revoked(delegation);
Trond Myklebust41020b62016-09-22 13:38:58 -0400950 ret = true;
951out:
952 rcu_read_unlock();
Trond Myklebust7f048832016-09-22 13:39:14 -0400953 if (ret)
954 nfs_inode_find_state_and_recover(inode, stateid);
Trond Myklebust41020b62016-09-22 13:38:58 -0400955}
956
957void nfs_remove_bad_delegation(struct inode *inode,
958 const nfs4_stateid *stateid)
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500959{
Trond Myklebustee05f452019-10-21 13:56:59 -0400960 nfs_revoke_delegation(inode, stateid);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500961}
Andy Adamson9cb81962012-03-07 10:49:41 -0500962EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500963
Trond Myklebustd51f91d2019-10-21 14:22:14 -0400964void nfs_delegation_mark_returned(struct inode *inode,
965 const nfs4_stateid *stateid)
966{
967 struct nfs_delegation *delegation;
968
969 if (!inode)
970 return;
971
972 rcu_read_lock();
973 delegation = rcu_dereference(NFS_I(inode)->delegation);
974 if (!delegation)
975 goto out_rcu_unlock;
976
977 spin_lock(&delegation->lock);
978 if (!nfs4_stateid_match_other(stateid, &delegation->stateid))
979 goto out_spin_unlock;
980 if (stateid->seqid) {
981 /* If delegation->stateid is newer, dont mark as returned */
982 if (nfs4_stateid_is_newer(&delegation->stateid, stateid))
983 goto out_clear_returning;
984 if (delegation->stateid.seqid != stateid->seqid)
985 delegation->stateid.seqid = stateid->seqid;
986 }
987
Trond Myklebustd2269ea2020-01-27 09:58:18 -0500988 nfs_mark_delegation_revoked(delegation);
Trond Myklebustd51f91d2019-10-21 14:22:14 -0400989
990out_clear_returning:
991 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
992out_spin_unlock:
993 spin_unlock(&delegation->lock);
994out_rcu_unlock:
995 rcu_read_unlock();
996
997 nfs_inode_find_state_and_recover(inode, stateid);
998}
999
Chuck Leverd3978bb2010-12-24 01:33:04 +00001000/**
Trond Myklebust826e0012013-04-03 19:27:52 -04001001 * nfs_expire_unused_delegation_types
Chuck Leverd3978bb2010-12-24 01:33:04 +00001002 * @clp: client to process
1003 * @flags: delegation types to expire
1004 *
1005 */
Trond Myklebust826e0012013-04-03 19:27:52 -04001006void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags)
Alexandros Batsakisc79571a2009-12-05 13:20:52 -05001007{
Trond Myklebust826e0012013-04-03 19:27:52 -04001008 nfs_client_mark_return_unused_delegation_types(clp, flags);
Alexandros Batsakisc79571a2009-12-05 13:20:52 -05001009 nfs_delegation_run_state_manager(clp);
1010}
1011
Chuck Leverd3978bb2010-12-24 01:33:04 +00001012static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
Trond Myklebustb7391f42008-12-23 15:21:52 -05001013{
1014 struct nfs_delegation *delegation;
1015
Chuck Leverd3978bb2010-12-24 01:33:04 +00001016 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
Trond Myklebustb7391f42008-12-23 15:21:52 -05001017 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
1018 continue;
Trond Myklebustb7571442013-04-03 14:33:49 -04001019 nfs_mark_return_if_closed_delegation(server, delegation);
Trond Myklebustb7391f42008-12-23 15:21:52 -05001020 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001021}
1022
Chuck Leverd3978bb2010-12-24 01:33:04 +00001023/**
1024 * nfs_expire_unreferenced_delegations - Eliminate unused delegations
1025 * @clp: nfs_client to process
1026 *
1027 */
Trond Myklebustb7391f42008-12-23 15:21:52 -05001028void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
1029{
Chuck Leverd3978bb2010-12-24 01:33:04 +00001030 struct nfs_server *server;
1031
1032 rcu_read_lock();
1033 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1034 nfs_mark_return_unreferenced_delegations(server);
1035 rcu_read_unlock();
1036
Trond Myklebustb7391f42008-12-23 15:21:52 -05001037 nfs_delegation_run_state_manager(clp);
1038}
1039
Chuck Leverd3978bb2010-12-24 01:33:04 +00001040/**
1041 * nfs_async_inode_return_delegation - asynchronously return a delegation
1042 * @inode: inode to process
Trond Myklebust8e663f02012-03-04 18:13:56 -05001043 * @stateid: state ID information
Chuck Leverd3978bb2010-12-24 01:33:04 +00001044 *
1045 * Returns zero on success, or a negative errno value.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 */
Chuck Leverd3978bb2010-12-24 01:33:04 +00001047int nfs_async_inode_return_delegation(struct inode *inode,
1048 const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
Trond Myklebusted1e62112011-07-25 15:37:29 -04001050 struct nfs_server *server = NFS_SERVER(inode);
1051 struct nfs_client *clp = server->nfs_client;
Trond Myklebust6411bd42008-12-23 15:21:51 -05001052 struct nfs_delegation *delegation;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Trond Myklebust6411bd42008-12-23 15:21:51 -05001054 rcu_read_lock();
Trond Myklebust457a5042019-10-22 08:46:06 -04001055 delegation = nfs4_get_valid_delegation(inode);
Trond Myklebust755a48a2014-03-02 22:03:12 -05001056 if (delegation == NULL)
1057 goto out_enoent;
Trond Myklebust4816fda2015-09-20 14:58:42 -04001058 if (stateid != NULL &&
1059 !clp->cl_mvops->match_stateid(&delegation->stateid, stateid))
Trond Myklebust755a48a2014-03-02 22:03:12 -05001060 goto out_enoent;
Trond Myklebusted1e62112011-07-25 15:37:29 -04001061 nfs_mark_return_delegation(server, delegation);
Trond Myklebust6411bd42008-12-23 15:21:51 -05001062 rcu_read_unlock();
Chuck Leverd3978bb2010-12-24 01:33:04 +00001063
Trond Myklebust6b4befc2021-05-07 09:14:37 -04001064 /* If there are any application leases or delegations, recall them */
1065 break_lease(inode, O_WRONLY | O_RDWR | O_NONBLOCK);
1066
Trond Myklebust6411bd42008-12-23 15:21:51 -05001067 nfs_delegation_run_state_manager(clp);
1068 return 0;
Trond Myklebust755a48a2014-03-02 22:03:12 -05001069out_enoent:
1070 rcu_read_unlock();
1071 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072}
1073
Chuck Leverd3978bb2010-12-24 01:33:04 +00001074static struct inode *
1075nfs_delegation_find_inode_server(struct nfs_server *server,
1076 const struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
1078 struct nfs_delegation *delegation;
Trond Myklebust113aac62021-01-10 15:46:06 -05001079 struct super_block *freeme = NULL;
1080 struct inode *res = NULL;
Chuck Leverd3978bb2010-12-24 01:33:04 +00001081
1082 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
Trond Myklebust86e89482008-12-23 15:21:39 -05001083 spin_lock(&delegation->lock);
1084 if (delegation->inode != NULL &&
Trond Myklebust457a5042019-10-22 08:46:06 -04001085 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) &&
Trond Myklebust86e89482008-12-23 15:21:39 -05001086 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
Trond Myklebust113aac62021-01-10 15:46:06 -05001087 if (nfs_sb_active(server->super)) {
1088 freeme = server->super;
1089 res = igrab(delegation->inode);
1090 }
Trond Myklebust6c342652018-06-07 14:22:00 -04001091 spin_unlock(&delegation->lock);
1092 if (res != NULL)
1093 return res;
Trond Myklebuste39d8a12018-11-13 16:37:54 -05001094 if (freeme) {
1095 rcu_read_unlock();
Trond Myklebust113aac62021-01-10 15:46:06 -05001096 nfs_sb_deactive(freeme);
Trond Myklebuste39d8a12018-11-13 16:37:54 -05001097 rcu_read_lock();
1098 }
Trond Myklebust6c342652018-06-07 14:22:00 -04001099 return ERR_PTR(-EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 }
Trond Myklebust86e89482008-12-23 15:21:39 -05001101 spin_unlock(&delegation->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 }
Trond Myklebust6c342652018-06-07 14:22:00 -04001103 return ERR_PTR(-ENOENT);
Chuck Leverd3978bb2010-12-24 01:33:04 +00001104}
1105
1106/**
1107 * nfs_delegation_find_inode - retrieve the inode associated with a delegation
1108 * @clp: client state handle
1109 * @fhandle: filehandle from a delegation recall
1110 *
1111 * Returns pointer to inode matching "fhandle," or NULL if a matching inode
1112 * cannot be found.
1113 */
1114struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
1115 const struct nfs_fh *fhandle)
1116{
1117 struct nfs_server *server;
Trond Myklebust6c342652018-06-07 14:22:00 -04001118 struct inode *res;
Chuck Leverd3978bb2010-12-24 01:33:04 +00001119
1120 rcu_read_lock();
1121 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1122 res = nfs_delegation_find_inode_server(server, fhandle);
Anna Schumakerd5681f52018-06-14 09:39:17 -04001123 if (res != ERR_PTR(-ENOENT)) {
1124 rcu_read_unlock();
Trond Myklebust6c342652018-06-07 14:22:00 -04001125 return res;
Anna Schumakerd5681f52018-06-14 09:39:17 -04001126 }
Chuck Leverd3978bb2010-12-24 01:33:04 +00001127 }
Trond Myklebust8383e462007-07-06 15:12:04 -04001128 rcu_read_unlock();
Trond Myklebust6c342652018-06-07 14:22:00 -04001129 return ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130}
1131
Chuck Leverd3978bb2010-12-24 01:33:04 +00001132static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
1133{
1134 struct nfs_delegation *delegation;
1135
Trond Myklebust45870d62016-09-22 13:38:59 -04001136 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
1137 /*
1138 * If the delegation may have been admin revoked, then we
1139 * cannot reclaim it.
1140 */
1141 if (test_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags))
1142 continue;
Chuck Leverd3978bb2010-12-24 01:33:04 +00001143 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
Trond Myklebust45870d62016-09-22 13:38:59 -04001144 }
Chuck Leverd3978bb2010-12-24 01:33:04 +00001145}
1146
1147/**
1148 * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
1149 * @clp: nfs_client to process
1150 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 */
David Howellsadfa6f92006-08-22 20:06:08 -04001152void nfs_delegation_mark_reclaim(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153{
Chuck Leverd3978bb2010-12-24 01:33:04 +00001154 struct nfs_server *server;
1155
Trond Myklebust8383e462007-07-06 15:12:04 -04001156 rcu_read_lock();
Chuck Leverd3978bb2010-12-24 01:33:04 +00001157 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1158 nfs_delegation_mark_reclaim_server(server);
Trond Myklebust8383e462007-07-06 15:12:04 -04001159 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160}
1161
Trond Myklebust1bba38b2020-02-27 09:08:25 -05001162static int nfs_server_reap_unclaimed_delegations(struct nfs_server *server,
1163 void __always_unused *data)
1164{
1165 struct nfs_delegation *delegation;
1166 struct inode *inode;
1167restart:
1168 rcu_read_lock();
1169restart_locked:
1170 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
1171 if (test_bit(NFS_DELEGATION_INODE_FREEING,
1172 &delegation->flags) ||
1173 test_bit(NFS_DELEGATION_RETURNING,
1174 &delegation->flags) ||
1175 test_bit(NFS_DELEGATION_NEED_RECLAIM,
1176 &delegation->flags) == 0)
1177 continue;
1178 inode = nfs_delegation_grab_inode(delegation);
1179 if (inode == NULL)
1180 goto restart_locked;
1181 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
1182 rcu_read_unlock();
1183 if (delegation != NULL) {
1184 if (nfs_detach_delegation(NFS_I(inode), delegation,
1185 server) != NULL)
1186 nfs_free_delegation(delegation);
1187 /* Match nfs_start_delegation_return_locked */
1188 nfs_put_delegation(delegation);
1189 }
1190 iput(inode);
1191 cond_resched();
1192 goto restart;
1193 }
1194 rcu_read_unlock();
1195 return 0;
1196}
1197
Chuck Leverd3978bb2010-12-24 01:33:04 +00001198/**
1199 * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
1200 * @clp: nfs_client to process
1201 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 */
David Howellsadfa6f92006-08-22 20:06:08 -04001203void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204{
Trond Myklebust1bba38b2020-02-27 09:08:25 -05001205 nfs_client_for_each_server(clp, nfs_server_reap_unclaimed_delegations,
1206 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207}
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001208
Trond Myklebustbb3d1a32016-09-22 13:39:00 -04001209static inline bool nfs4_server_rebooted(const struct nfs_client *clp)
1210{
1211 return (clp->cl_state & (BIT(NFS4CLNT_CHECK_LEASE) |
1212 BIT(NFS4CLNT_LEASE_EXPIRED) |
1213 BIT(NFS4CLNT_SESSION_RESET))) != 0;
1214}
1215
Trond Myklebust45870d62016-09-22 13:38:59 -04001216static void nfs_mark_test_expired_delegation(struct nfs_server *server,
1217 struct nfs_delegation *delegation)
1218{
Trond Myklebust059b43e2016-09-22 13:39:06 -04001219 if (delegation->stateid.type == NFS4_INVALID_STATEID_TYPE)
1220 return;
Trond Myklebust45870d62016-09-22 13:38:59 -04001221 clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
1222 set_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
1223 set_bit(NFS4CLNT_DELEGATION_EXPIRED, &server->nfs_client->cl_state);
1224}
1225
Trond Myklebustbb3d1a32016-09-22 13:39:00 -04001226static void nfs_inode_mark_test_expired_delegation(struct nfs_server *server,
1227 struct inode *inode)
1228{
1229 struct nfs_delegation *delegation;
1230
1231 rcu_read_lock();
1232 delegation = rcu_dereference(NFS_I(inode)->delegation);
1233 if (delegation)
1234 nfs_mark_test_expired_delegation(server, delegation);
1235 rcu_read_unlock();
1236
1237}
1238
Trond Myklebust45870d62016-09-22 13:38:59 -04001239static void nfs_delegation_mark_test_expired_server(struct nfs_server *server)
1240{
1241 struct nfs_delegation *delegation;
1242
1243 list_for_each_entry_rcu(delegation, &server->delegations, super_list)
1244 nfs_mark_test_expired_delegation(server, delegation);
1245}
1246
1247/**
1248 * nfs_mark_test_expired_all_delegations - mark all delegations for testing
1249 * @clp: nfs_client to process
1250 *
1251 * Iterates through all the delegations associated with this server and
1252 * marks them as needing to be checked for validity.
1253 */
1254void nfs_mark_test_expired_all_delegations(struct nfs_client *clp)
1255{
1256 struct nfs_server *server;
1257
1258 rcu_read_lock();
1259 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1260 nfs_delegation_mark_test_expired_server(server);
1261 rcu_read_unlock();
1262}
1263
1264/**
Scott Mayhew8ca017c2019-05-06 11:59:05 -04001265 * nfs_test_expired_all_delegations - test all delegations for a client
1266 * @clp: nfs_client to process
1267 *
1268 * Helper for handling "recallable state revoked" status from server.
1269 */
1270void nfs_test_expired_all_delegations(struct nfs_client *clp)
1271{
1272 nfs_mark_test_expired_all_delegations(clp);
1273 nfs4_schedule_state_manager(clp);
1274}
1275
Trond Myklebustad114082019-07-26 14:40:53 +01001276static void
1277nfs_delegation_test_free_expired(struct inode *inode,
1278 nfs4_stateid *stateid,
1279 const struct cred *cred)
1280{
1281 struct nfs_server *server = NFS_SERVER(inode);
1282 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
1283 int status;
1284
1285 if (!cred)
1286 return;
1287 status = ops->test_and_free_expired(server, stateid, cred);
1288 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
1289 nfs_remove_bad_delegation(inode, stateid);
1290}
1291
Trond Myklebust7f156ef2020-02-27 09:15:19 -05001292static int nfs_server_reap_expired_delegations(struct nfs_server *server,
1293 void __always_unused *data)
1294{
1295 struct nfs_delegation *delegation;
1296 struct inode *inode;
1297 const struct cred *cred;
1298 nfs4_stateid stateid;
1299restart:
1300 rcu_read_lock();
1301restart_locked:
1302 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
1303 if (test_bit(NFS_DELEGATION_INODE_FREEING,
1304 &delegation->flags) ||
1305 test_bit(NFS_DELEGATION_RETURNING,
1306 &delegation->flags) ||
1307 test_bit(NFS_DELEGATION_TEST_EXPIRED,
1308 &delegation->flags) == 0)
1309 continue;
1310 inode = nfs_delegation_grab_inode(delegation);
1311 if (inode == NULL)
1312 goto restart_locked;
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04001313 spin_lock(&delegation->lock);
Trond Myklebust7f156ef2020-02-27 09:15:19 -05001314 cred = get_cred_rcu(delegation->cred);
1315 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04001316 spin_unlock(&delegation->lock);
Trond Myklebust7f156ef2020-02-27 09:15:19 -05001317 clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
1318 rcu_read_unlock();
1319 nfs_delegation_test_free_expired(inode, &stateid, cred);
1320 put_cred(cred);
1321 if (!nfs4_server_rebooted(server->nfs_client)) {
1322 iput(inode);
1323 cond_resched();
1324 goto restart;
1325 }
1326 nfs_inode_mark_test_expired_delegation(server,inode);
1327 iput(inode);
1328 return -EAGAIN;
1329 }
1330 rcu_read_unlock();
1331 return 0;
1332}
1333
Scott Mayhew8ca017c2019-05-06 11:59:05 -04001334/**
Trond Myklebust45870d62016-09-22 13:38:59 -04001335 * nfs_reap_expired_delegations - reap expired delegations
1336 * @clp: nfs_client to process
1337 *
1338 * Iterates through all the delegations associated with this server and
1339 * checks if they have may have been revoked. This function is usually
1340 * expected to be called in cases where the server may have lost its
1341 * lease.
1342 */
1343void nfs_reap_expired_delegations(struct nfs_client *clp)
1344{
Trond Myklebust7f156ef2020-02-27 09:15:19 -05001345 nfs_client_for_each_server(clp, nfs_server_reap_expired_delegations,
1346 NULL);
Trond Myklebust45870d62016-09-22 13:38:59 -04001347}
1348
Trond Myklebust6c2d8f82016-09-22 13:39:07 -04001349void nfs_inode_find_delegation_state_and_recover(struct inode *inode,
1350 const nfs4_stateid *stateid)
1351{
1352 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1353 struct nfs_delegation *delegation;
1354 bool found = false;
1355
1356 rcu_read_lock();
1357 delegation = rcu_dereference(NFS_I(inode)->delegation);
1358 if (delegation &&
Trond Myklebust42c304c2019-10-26 10:16:15 -04001359 nfs4_stateid_match_or_older(&delegation->stateid, stateid) &&
1360 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
Trond Myklebust6c2d8f82016-09-22 13:39:07 -04001361 nfs_mark_test_expired_delegation(NFS_SERVER(inode), delegation);
1362 found = true;
1363 }
1364 rcu_read_unlock();
1365 if (found)
1366 nfs4_schedule_state_manager(clp);
1367}
1368
Chuck Leverd3978bb2010-12-24 01:33:04 +00001369/**
1370 * nfs_delegations_present - check for existence of delegations
1371 * @clp: client state handle
1372 *
1373 * Returns one if there are any nfs_delegation structures attached
1374 * to this nfs_client.
1375 */
1376int nfs_delegations_present(struct nfs_client *clp)
1377{
1378 struct nfs_server *server;
1379 int ret = 0;
1380
1381 rcu_read_lock();
1382 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1383 if (!list_empty(&server->delegations)) {
1384 ret = 1;
1385 break;
1386 }
1387 rcu_read_unlock();
1388 return ret;
1389}
1390
1391/**
Trond Myklebust12f275c2017-11-06 15:28:05 -05001392 * nfs4_refresh_delegation_stateid - Update delegation stateid seqid
1393 * @dst: stateid to refresh
1394 * @inode: inode to check
1395 *
1396 * Returns "true" and updates "dst->seqid" * if inode had a delegation
1397 * that matches our delegation stateid. Otherwise "false" is returned.
1398 */
1399bool nfs4_refresh_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
1400{
1401 struct nfs_delegation *delegation;
1402 bool ret = false;
1403 if (!inode)
1404 goto out;
1405
1406 rcu_read_lock();
1407 delegation = rcu_dereference(NFS_I(inode)->delegation);
1408 if (delegation != NULL &&
Trond Myklebustb5756202019-10-22 13:34:06 -04001409 nfs4_stateid_match_other(dst, &delegation->stateid) &&
Trond Myklebust246afc0a2019-10-24 18:00:35 -04001410 nfs4_stateid_is_newer(&delegation->stateid, dst) &&
Trond Myklebustb5756202019-10-22 13:34:06 -04001411 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
Trond Myklebust12f275c2017-11-06 15:28:05 -05001412 dst->seqid = delegation->stateid.seqid;
Trond Myklebust79cc5542019-10-31 18:40:33 -04001413 ret = true;
Trond Myklebust12f275c2017-11-06 15:28:05 -05001414 }
1415 rcu_read_unlock();
1416out:
1417 return ret;
1418}
1419
1420/**
Chuck Leverd3978bb2010-12-24 01:33:04 +00001421 * nfs4_copy_delegation_stateid - Copy inode's state ID information
Chuck Leverd3978bb2010-12-24 01:33:04 +00001422 * @inode: inode to check
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001423 * @flags: delegation type requirement
Trond Myklebustabf4e132016-05-16 17:42:44 -04001424 * @dst: stateid data structure to fill in
1425 * @cred: optional argument to retrieve credential
Chuck Leverd3978bb2010-12-24 01:33:04 +00001426 *
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001427 * Returns "true" and fills in "dst->data" * if inode had a delegation,
1428 * otherwise "false" is returned.
Chuck Leverd3978bb2010-12-24 01:33:04 +00001429 */
Trond Myklebustabf4e132016-05-16 17:42:44 -04001430bool nfs4_copy_delegation_stateid(struct inode *inode, fmode_t flags,
NeilBrowna52458b2018-12-03 11:30:31 +11001431 nfs4_stateid *dst, const struct cred **cred)
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001432{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001433 struct nfs_inode *nfsi = NFS_I(inode);
1434 struct nfs_delegation *delegation;
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04001435 bool ret = false;
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001436
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001437 flags &= FMODE_READ|FMODE_WRITE;
Trond Myklebust8383e462007-07-06 15:12:04 -04001438 rcu_read_lock();
1439 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04001440 if (!delegation)
1441 goto out;
1442 spin_lock(&delegation->lock);
Trond Myklebustaa05c872016-09-22 13:38:54 -04001443 ret = nfs4_is_valid_delegation(delegation, flags);
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001444 if (ret) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001445 nfs4_stateid_copy(dst, &delegation->stateid);
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001446 nfs_mark_delegation_referenced(delegation);
Trond Myklebustabf4e132016-05-16 17:42:44 -04001447 if (cred)
NeilBrowna52458b2018-12-03 11:30:31 +11001448 *cred = get_cred(delegation->cred);
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001449 }
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04001450 spin_unlock(&delegation->lock);
1451out:
Trond Myklebust8383e462007-07-06 15:12:04 -04001452 rcu_read_unlock();
1453 return ret;
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001454}
Trond Myklebust5445b1f2015-09-05 19:06:58 -04001455
1456/**
1457 * nfs4_delegation_flush_on_close - Check if we must flush file on close
1458 * @inode: inode to check
1459 *
1460 * This function checks the number of outstanding writes to the file
1461 * against the delegation 'space_limit' field to see if
1462 * the spec requires us to flush the file on close.
1463 */
1464bool nfs4_delegation_flush_on_close(const struct inode *inode)
1465{
1466 struct nfs_inode *nfsi = NFS_I(inode);
1467 struct nfs_delegation *delegation;
1468 bool ret = true;
1469
1470 rcu_read_lock();
1471 delegation = rcu_dereference(nfsi->delegation);
1472 if (delegation == NULL || !(delegation->type & FMODE_WRITE))
1473 goto out;
Trond Myklebusta6b6d5b2017-08-01 15:39:46 -04001474 if (atomic_long_read(&nfsi->nrequests) < delegation->pagemod_limit)
Trond Myklebust5445b1f2015-09-05 19:06:58 -04001475 ret = false;
1476out:
1477 rcu_read_unlock();
1478 return ret;
1479}
Trond Myklebust10717f42020-01-27 09:58:19 -05001480
1481module_param_named(delegation_watermark, nfs_delegation_watermark, uint, 0644);