blob: 902baea1ecc683be30c75899744d487146b362c7 [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 Myklebust905f8d12007-08-06 12:18:34 -040028static void nfs_free_delegation(struct nfs_delegation *delegation)
29{
NeilBrowna52458b2018-12-03 11:30:31 +110030 put_cred(delegation->cred);
31 delegation->cred = NULL;
Lai Jiangshan26f04dd2011-05-01 06:21:54 -070032 kfree_rcu(delegation, rcu);
Trond Myklebust8383e462007-07-06 15:12:04 -040033}
34
Chuck Leverd3978bb2010-12-24 01:33:04 +000035/**
36 * nfs_mark_delegation_referenced - set delegation's REFERENCED flag
37 * @delegation: delegation to process
38 *
39 */
Trond Myklebustb7391f42008-12-23 15:21:52 -050040void nfs_mark_delegation_referenced(struct nfs_delegation *delegation)
41{
42 set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags);
43}
44
Trond Myklebustaa05c872016-09-22 13:38:54 -040045static bool
46nfs4_is_valid_delegation(const struct nfs_delegation *delegation,
47 fmode_t flags)
48{
49 if (delegation != NULL && (delegation->type & flags) == flags &&
50 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) &&
51 !test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
52 return true;
53 return false;
54}
55
Trond Myklebustbe3df3d2019-10-31 18:40:32 -040056struct nfs_delegation *nfs4_get_valid_delegation(const struct inode *inode)
57{
58 struct nfs_delegation *delegation;
59
60 delegation = rcu_dereference(NFS_I(inode)->delegation);
61 if (nfs4_is_valid_delegation(delegation, 0))
62 return delegation;
63 return NULL;
64}
65
Peng Tao15bb3af2014-07-03 13:05:00 +080066static int
67nfs4_do_check_delegation(struct inode *inode, fmode_t flags, bool mark)
Trond Myklebustb7391f42008-12-23 15:21:52 -050068{
69 struct nfs_delegation *delegation;
70 int ret = 0;
71
72 flags &= FMODE_READ|FMODE_WRITE;
73 rcu_read_lock();
74 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustaa05c872016-09-22 13:38:54 -040075 if (nfs4_is_valid_delegation(delegation, flags)) {
Peng Tao15bb3af2014-07-03 13:05:00 +080076 if (mark)
77 nfs_mark_delegation_referenced(delegation);
Trond Myklebustb7391f42008-12-23 15:21:52 -050078 ret = 1;
79 }
80 rcu_read_unlock();
81 return ret;
82}
Peng Tao15bb3af2014-07-03 13:05:00 +080083/**
84 * nfs_have_delegation - check if inode has a delegation, mark it
85 * NFS_DELEGATION_REFERENCED if there is one.
86 * @inode: inode to check
87 * @flags: delegation types to check for
88 *
89 * Returns one if inode has the indicated delegation, otherwise zero.
90 */
91int nfs4_have_delegation(struct inode *inode, fmode_t flags)
92{
93 return nfs4_do_check_delegation(inode, flags, true);
94}
95
96/*
97 * nfs4_check_delegation - check if inode has a delegation, do not mark
98 * NFS_DELEGATION_REFERENCED if it has one.
99 */
100int nfs4_check_delegation(struct inode *inode, fmode_t flags)
101{
102 return nfs4_do_check_delegation(inode, flags, false);
103}
Trond Myklebustb7391f42008-12-23 15:21:52 -0500104
Olga Kornievskaia44f411c2018-10-04 14:45:00 -0400105static int nfs_delegation_claim_locks(struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -0500106{
107 struct inode *inode = state->inode;
108 struct file_lock *fl;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500109 struct file_lock_context *flctx = inode->i_flctx;
110 struct list_head *list;
Trond Myklebustd5122202009-06-17 13:22:58 -0700111 int status = 0;
Trond Myklebust888e6942005-11-04 15:38:11 -0500112
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500113 if (flctx == NULL)
Trond Myklebust65b62a22013-02-07 10:54:07 -0500114 goto out;
Jeff Layton314d7cc2013-04-10 15:36:48 -0400115
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500116 list = &flctx->flc_posix;
Jeff Layton6109c852015-01-16 15:05:57 -0500117 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500118restart:
119 list_for_each_entry(fl, list, fl_list) {
Olga Kornievskaia44f411c2018-10-04 14:45:00 -0400120 if (nfs_file_open_context(fl->fl_file)->state != state)
Trond Myklebust888e6942005-11-04 15:38:11 -0500121 continue;
Jeff Layton6109c852015-01-16 15:05:57 -0500122 spin_unlock(&flctx->flc_lock);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -0400123 status = nfs4_lock_delegation_recall(fl, state, stateid);
Trond Myklebustd5122202009-06-17 13:22:58 -0700124 if (status < 0)
Trond Myklebust3f09df72009-06-17 13:23:00 -0700125 goto out;
Jeff Layton6109c852015-01-16 15:05:57 -0500126 spin_lock(&flctx->flc_lock);
Trond Myklebust888e6942005-11-04 15:38:11 -0500127 }
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500128 if (list == &flctx->flc_posix) {
129 list = &flctx->flc_flock;
130 goto restart;
Jeff Layton5263e312015-01-16 15:05:55 -0500131 }
Jeff Layton6109c852015-01-16 15:05:57 -0500132 spin_unlock(&flctx->flc_lock);
Trond Myklebust3f09df72009-06-17 13:23:00 -0700133out:
Trond Myklebust888e6942005-11-04 15:38:11 -0500134 return status;
135}
136
Trond Myklebust24311f82015-09-20 10:50:17 -0400137static int nfs_delegation_claim_opens(struct inode *inode,
138 const nfs4_stateid *stateid, fmode_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
140 struct nfs_inode *nfsi = NFS_I(inode);
141 struct nfs_open_context *ctx;
Trond Myklebustd25be542013-02-05 11:43:28 -0500142 struct nfs4_state_owner *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 struct nfs4_state *state;
Trond Myklebustd25be542013-02-05 11:43:28 -0500144 unsigned int seq;
Trond Myklebust888e6942005-11-04 15:38:11 -0500145 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
147again:
Trond Myklebust0de43972018-09-02 15:57:01 -0400148 rcu_read_lock();
149 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 state = ctx->state;
151 if (state == NULL)
152 continue;
153 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
154 continue;
Trond Myklebustf8ebf7a2014-10-17 23:02:52 +0300155 if (!nfs4_valid_open_stateid(state))
156 continue;
Trond Myklebustf597c532012-03-04 18:13:56 -0500157 if (!nfs4_stateid_match(&state->stateid, stateid))
Trond Myklebust90163022007-07-05 14:55:18 -0400158 continue;
Trond Myklebust0de43972018-09-02 15:57:01 -0400159 if (!get_nfs_open_context(ctx))
160 continue;
161 rcu_read_unlock();
Trond Myklebustd25be542013-02-05 11:43:28 -0500162 sp = state->owner;
Trond Myklebust65b62a22013-02-07 10:54:07 -0500163 /* Block nfs4_proc_unlck */
164 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebustd25be542013-02-05 11:43:28 -0500165 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
Trond Myklebust5eb8d182019-07-19 14:08:37 -0400166 err = nfs4_open_delegation_recall(ctx, state, stateid);
Trond Myklebustd25be542013-02-05 11:43:28 -0500167 if (!err)
Olga Kornievskaia44f411c2018-10-04 14:45:00 -0400168 err = nfs_delegation_claim_locks(state, stateid);
Trond Myklebustd25be542013-02-05 11:43:28 -0500169 if (!err && read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
170 err = -EAGAIN;
Trond Myklebust65b62a22013-02-07 10:54:07 -0500171 mutex_unlock(&sp->so_delegreturn_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 put_nfs_open_context(ctx);
Trond Myklebust888e6942005-11-04 15:38:11 -0500173 if (err != 0)
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500174 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 goto again;
176 }
Trond Myklebust0de43972018-09-02 15:57:01 -0400177 rcu_read_unlock();
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500178 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
Chuck Leverd3978bb2010-12-24 01:33:04 +0000181/**
182 * nfs_inode_reclaim_delegation - process a delegation reclaim request
183 * @inode: inode to process
184 * @cred: credential to use for request
Trond Myklebust35156bf2018-03-20 17:03:13 -0400185 * @type: delegation type
186 * @stateid: delegation stateid
187 * @pagemod_limit: write delegation "space_limit"
Chuck Leverd3978bb2010-12-24 01:33:04 +0000188 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 */
NeilBrowna52458b2018-12-03 11:30:31 +1100190void nfs_inode_reclaim_delegation(struct inode *inode, const struct cred *cred,
Trond Myklebust35156bf2018-03-20 17:03:13 -0400191 fmode_t type,
192 const nfs4_stateid *stateid,
193 unsigned long pagemod_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
Trond Myklebust8f649c32010-05-01 12:36:18 -0400195 struct nfs_delegation *delegation;
NeilBrowna52458b2018-12-03 11:30:31 +1100196 const struct cred *oldcred = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Trond Myklebust8f649c32010-05-01 12:36:18 -0400198 rcu_read_lock();
199 delegation = rcu_dereference(NFS_I(inode)->delegation);
200 if (delegation != NULL) {
201 spin_lock(&delegation->lock);
Trond Myklebust1deed572019-10-22 08:52:47 -0400202 if (nfs4_is_valid_delegation(delegation, 0)) {
Trond Myklebust35156bf2018-03-20 17:03:13 -0400203 nfs4_stateid_copy(&delegation->stateid, stateid);
204 delegation->type = type;
205 delegation->pagemod_limit = pagemod_limit;
Trond Myklebust8f649c32010-05-01 12:36:18 -0400206 oldcred = delegation->cred;
NeilBrowna52458b2018-12-03 11:30:31 +1100207 delegation->cred = get_cred(cred);
Trond Myklebust8f649c32010-05-01 12:36:18 -0400208 clear_bit(NFS_DELEGATION_NEED_RECLAIM,
209 &delegation->flags);
Trond Myklebust8f649c32010-05-01 12:36:18 -0400210 spin_unlock(&delegation->lock);
Trond Myklebust8f649c32010-05-01 12:36:18 -0400211 rcu_read_unlock();
NeilBrowna52458b2018-12-03 11:30:31 +1100212 put_cred(oldcred);
Trond Myklebust35156bf2018-03-20 17:03:13 -0400213 trace_nfs4_reclaim_delegation(inode, type);
Trond Myklebustb1a318d2016-09-22 13:39:12 -0400214 return;
Trond Myklebust8f649c32010-05-01 12:36:18 -0400215 }
Trond Myklebustb1a318d2016-09-22 13:39:12 -0400216 /* We appear to have raced with a delegation return. */
217 spin_unlock(&delegation->lock);
Trond Myklebust8f649c32010-05-01 12:36:18 -0400218 }
Trond Myklebustb1a318d2016-09-22 13:39:12 -0400219 rcu_read_unlock();
Trond Myklebust35156bf2018-03-20 17:03:13 -0400220 nfs_inode_set_delegation(inode, cred, type, stateid, pagemod_limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221}
222
Trond Myklebust57bfa892008-01-25 16:38:18 -0500223static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
224{
225 int res = 0;
226
Trond Myklebust869f9df2014-11-10 18:43:56 -0500227 if (!test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
228 res = nfs4_proc_delegreturn(inode,
229 delegation->cred,
230 &delegation->stateid,
231 issync);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500232 nfs_free_delegation(delegation);
233 return res;
234}
235
Trond Myklebust86e89482008-12-23 15:21:39 -0500236static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
237{
238 struct inode *inode = NULL;
239
240 spin_lock(&delegation->lock);
241 if (delegation->inode != NULL)
242 inode = igrab(delegation->inode);
Trond Myklebust6f9449b2019-02-21 14:51:25 -0500243 if (!inode)
244 set_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags);
Trond Myklebust86e89482008-12-23 15:21:39 -0500245 spin_unlock(&delegation->lock);
246 return inode;
247}
248
Chuck Leverdda4b222010-12-24 01:32:54 +0000249static struct nfs_delegation *
Trond Myklebustd25be542013-02-05 11:43:28 -0500250nfs_start_delegation_return_locked(struct nfs_inode *nfsi)
Trond Myklebust57bfa892008-01-25 16:38:18 -0500251{
Trond Myklebustd25be542013-02-05 11:43:28 -0500252 struct nfs_delegation *ret = NULL;
253 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500254
255 if (delegation == NULL)
Trond Myklebustd25be542013-02-05 11:43:28 -0500256 goto out;
257 spin_lock(&delegation->lock);
258 if (!test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
259 ret = delegation;
260 spin_unlock(&delegation->lock);
261out:
262 return ret;
263}
264
265static struct nfs_delegation *
266nfs_start_delegation_return(struct nfs_inode *nfsi)
267{
268 struct nfs_delegation *delegation;
269
270 rcu_read_lock();
271 delegation = nfs_start_delegation_return_locked(nfsi);
272 rcu_read_unlock();
273 return delegation;
274}
275
276static void
277nfs_abort_delegation_return(struct nfs_delegation *delegation,
278 struct nfs_client *clp)
279{
Chuck Leverdda4b222010-12-24 01:32:54 +0000280
Trond Myklebust34310432008-12-23 15:21:38 -0500281 spin_lock(&delegation->lock);
Trond Myklebustd25be542013-02-05 11:43:28 -0500282 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
283 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
284 spin_unlock(&delegation->lock);
285 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
286}
287
288static struct nfs_delegation *
289nfs_detach_delegation_locked(struct nfs_inode *nfsi,
290 struct nfs_delegation *delegation,
291 struct nfs_client *clp)
292{
293 struct nfs_delegation *deleg_cur =
294 rcu_dereference_protected(nfsi->delegation,
295 lockdep_is_held(&clp->cl_lock));
296
297 if (deleg_cur == NULL || delegation != deleg_cur)
298 return NULL;
299
300 spin_lock(&delegation->lock);
Trond Myklebustf9e0cc92019-10-21 14:12:13 -0400301 if (!delegation->inode) {
302 spin_unlock(&delegation->lock);
303 return NULL;
304 }
Trond Myklebustd25be542013-02-05 11:43:28 -0500305 set_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500306 list_del_rcu(&delegation->super_list);
Trond Myklebust86e89482008-12-23 15:21:39 -0500307 delegation->inode = NULL;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500308 rcu_assign_pointer(nfsi->delegation, NULL);
Trond Myklebust34310432008-12-23 15:21:38 -0500309 spin_unlock(&delegation->lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500310 return delegation;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500311}
312
Chuck Leverdda4b222010-12-24 01:32:54 +0000313static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi,
Trond Myklebustd25be542013-02-05 11:43:28 -0500314 struct nfs_delegation *delegation,
315 struct nfs_server *server)
Chuck Leverdda4b222010-12-24 01:32:54 +0000316{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000317 struct nfs_client *clp = server->nfs_client;
Chuck Leverdda4b222010-12-24 01:32:54 +0000318
319 spin_lock(&clp->cl_lock);
Trond Myklebustd25be542013-02-05 11:43:28 -0500320 delegation = nfs_detach_delegation_locked(nfsi, delegation, clp);
Chuck Leverdda4b222010-12-24 01:32:54 +0000321 spin_unlock(&clp->cl_lock);
322 return delegation;
323}
324
Trond Myklebustd25be542013-02-05 11:43:28 -0500325static struct nfs_delegation *
326nfs_inode_detach_delegation(struct inode *inode)
327{
328 struct nfs_inode *nfsi = NFS_I(inode);
329 struct nfs_server *server = NFS_SERVER(inode);
330 struct nfs_delegation *delegation;
331
332 delegation = nfs_start_delegation_return(nfsi);
333 if (delegation == NULL)
334 return NULL;
335 return nfs_detach_delegation(nfsi, delegation, server);
336}
337
Trond Myklebustcf6726e2014-12-19 11:22:28 -0500338static void
339nfs_update_inplace_delegation(struct nfs_delegation *delegation,
340 const struct nfs_delegation *update)
341{
342 if (nfs4_stateid_is_newer(&update->stateid, &delegation->stateid)) {
343 delegation->stateid.seqid = update->stateid.seqid;
344 smp_wmb();
345 delegation->type = update->type;
Trond Myklebustae084a32019-10-21 14:17:34 -0400346 clear_bit(NFS_DELEGATION_REVOKED, &delegation->flags);
Trond Myklebustcf6726e2014-12-19 11:22:28 -0500347 }
348}
349
Chuck Leverd3978bb2010-12-24 01:33:04 +0000350/**
351 * nfs_inode_set_delegation - set up a delegation on an inode
352 * @inode: inode to which delegation applies
353 * @cred: cred to use for subsequent delegation processing
Trond Myklebust35156bf2018-03-20 17:03:13 -0400354 * @type: delegation type
355 * @stateid: delegation stateid
356 * @pagemod_limit: write delegation "space_limit"
Chuck Leverd3978bb2010-12-24 01:33:04 +0000357 *
358 * Returns zero on success, or a negative errno value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 */
NeilBrowna52458b2018-12-03 11:30:31 +1100360int nfs_inode_set_delegation(struct inode *inode, const struct cred *cred,
Trond Myklebust35156bf2018-03-20 17:03:13 -0400361 fmode_t type,
362 const nfs4_stateid *stateid,
363 unsigned long pagemod_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000365 struct nfs_server *server = NFS_SERVER(inode);
366 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 struct nfs_inode *nfsi = NFS_I(inode);
David Howells17d2c0a2010-05-01 12:37:18 -0400368 struct nfs_delegation *delegation, *old_delegation;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500369 struct nfs_delegation *freeme = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 int status = 0;
371
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400372 delegation = kmalloc(sizeof(*delegation), GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 if (delegation == NULL)
374 return -ENOMEM;
Trond Myklebust35156bf2018-03-20 17:03:13 -0400375 nfs4_stateid_copy(&delegation->stateid, stateid);
376 delegation->type = type;
377 delegation->pagemod_limit = pagemod_limit;
Jeff Layton1eb5d982018-01-09 08:21:17 -0500378 delegation->change_attr = inode_peek_iversion_raw(inode);
NeilBrowna52458b2018-12-03 11:30:31 +1100379 delegation->cred = get_cred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 delegation->inode = inode;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500381 delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
Trond Myklebust34310432008-12-23 15:21:38 -0500382 spin_lock_init(&delegation->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383
384 spin_lock(&clp->cl_lock);
David Howells17d2c0a2010-05-01 12:37:18 -0400385 old_delegation = rcu_dereference_protected(nfsi->delegation,
Chuck Leverd3978bb2010-12-24 01:33:04 +0000386 lockdep_is_held(&clp->cl_lock));
David Howells17d2c0a2010-05-01 12:37:18 -0400387 if (old_delegation != NULL) {
Trond Myklebustcf6726e2014-12-19 11:22:28 -0500388 /* Is this an update of the existing delegation? */
389 if (nfs4_stateid_match_other(&old_delegation->stateid,
390 &delegation->stateid)) {
Trond Myklebuste0f07892019-10-21 14:11:00 -0400391 spin_lock(&old_delegation->lock);
Trond Myklebustcf6726e2014-12-19 11:22:28 -0500392 nfs_update_inplace_delegation(old_delegation,
393 delegation);
Trond Myklebuste0f07892019-10-21 14:11:00 -0400394 spin_unlock(&old_delegation->lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500395 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
Trond Myklebust57bfa892008-01-25 16:38:18 -0500397 /*
398 * Deal with broken servers that hand out two
399 * delegations for the same file.
Trond Myklebust17280172012-03-11 13:11:00 -0400400 * Allow for upgrades to a WRITE delegation, but
401 * nothing else.
Trond Myklebust57bfa892008-01-25 16:38:18 -0500402 */
403 dfprintk(FILE, "%s: server %s handed out "
404 "a duplicate delegation!\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700405 __func__, clp->cl_hostname);
Trond Myklebust17280172012-03-11 13:11:00 -0400406 if (delegation->type == old_delegation->type ||
407 !(delegation->type & FMODE_WRITE)) {
Trond Myklebust57bfa892008-01-25 16:38:18 -0500408 freeme = delegation;
409 delegation = NULL;
410 goto out;
411 }
Trond Myklebustade04642015-02-27 14:25:50 -0500412 if (test_and_set_bit(NFS_DELEGATION_RETURNING,
413 &old_delegation->flags))
414 goto out;
415 freeme = nfs_detach_delegation_locked(nfsi,
Trond Myklebustd25be542013-02-05 11:43:28 -0500416 old_delegation, clp);
417 if (freeme == NULL)
418 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
Trond Myklebust38942ba2015-03-04 15:59:05 -0500420 list_add_tail_rcu(&delegation->super_list, &server->delegations);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500421 rcu_assign_pointer(nfsi->delegation, delegation);
422 delegation = NULL;
Trond Myklebust412c77c2007-07-03 16:10:55 -0400423
Trond Myklebust35156bf2018-03-20 17:03:13 -0400424 trace_nfs4_set_delegation(inode, type);
Trond Myklebust412c77c2007-07-03 16:10:55 -0400425
Trond Myklebust97c2c172018-04-07 18:43:17 -0400426 spin_lock(&inode->i_lock);
427 if (NFS_I(inode)->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ATIME))
428 NFS_I(inode)->cache_validity |= NFS_INO_REVAL_FORCED;
429 spin_unlock(&inode->i_lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500430out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 spin_unlock(&clp->cl_lock);
Trond Myklebust603c83d2007-10-18 19:59:20 -0400432 if (delegation != NULL)
433 nfs_free_delegation(delegation);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500434 if (freeme != NULL)
435 nfs_do_return_delegation(inode, freeme, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return status;
437}
438
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439/*
440 * Basic procedure for returning a delegation to the server
441 */
Trond Myklebustd25be542013-02-05 11:43:28 -0500442static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Trond Myklebustd25be542013-02-05 11:43:28 -0500444 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust869f9df2014-11-10 18:43:56 -0500446 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Trond Myklebustd25be542013-02-05 11:43:28 -0500448 if (delegation == NULL)
449 return 0;
450 do {
Trond Myklebust869f9df2014-11-10 18:43:56 -0500451 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
452 break;
Trond Myklebust24311f82015-09-20 10:50:17 -0400453 err = nfs_delegation_claim_opens(inode, &delegation->stateid,
454 delegation->type);
Trond Myklebustd25be542013-02-05 11:43:28 -0500455 if (!issync || err != -EAGAIN)
456 break;
457 /*
458 * Guard against state recovery
459 */
460 err = nfs4_wait_clnt_recover(clp);
461 } while (err == 0);
462
463 if (err) {
464 nfs_abort_delegation_return(delegation, clp);
465 goto out;
466 }
467 if (!nfs_detach_delegation(nfsi, delegation, NFS_SERVER(inode)))
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500468 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500470 err = nfs_do_return_delegation(inode, delegation, issync);
471out:
472 return err;
Trond Myklebust90163022007-07-05 14:55:18 -0400473}
474
Trond Myklebustb7571442013-04-03 14:33:49 -0400475static bool nfs_delegation_need_return(struct nfs_delegation *delegation)
476{
477 bool ret = false;
478
479 if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
480 ret = true;
481 if (test_and_clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags) && !ret) {
482 struct inode *inode;
483
484 spin_lock(&delegation->lock);
485 inode = delegation->inode;
486 if (inode && list_empty(&NFS_I(inode)->open_files))
487 ret = true;
488 spin_unlock(&delegation->lock);
489 }
Trond Myklebustaf20b7b2019-10-22 13:40:47 -0400490 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags) ||
491 test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
492 ret = false;
493
Trond Myklebustb7571442013-04-03 14:33:49 -0400494 return ret;
495}
496
Chuck Leverd3978bb2010-12-24 01:33:04 +0000497/**
498 * nfs_client_return_marked_delegations - return previously marked delegations
499 * @clp: nfs_client to process
500 *
Trond Myklebustdc327ed2012-05-06 19:46:30 -0400501 * Note that this function is designed to be called by the state
502 * manager thread. For this reason, it cannot flush the dirty data,
503 * since that could deadlock in case of a state recovery error.
504 *
Chuck Leverd3978bb2010-12-24 01:33:04 +0000505 * Returns zero on success, or a negative errno value.
Trond Myklebust515d8612008-12-23 15:21:46 -0500506 */
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500507int nfs_client_return_marked_delegations(struct nfs_client *clp)
Trond Myklebust515d8612008-12-23 15:21:46 -0500508{
509 struct nfs_delegation *delegation;
NeilBrowne04bbf62018-04-30 14:31:30 +1000510 struct nfs_delegation *prev;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000511 struct nfs_server *server;
Trond Myklebust515d8612008-12-23 15:21:46 -0500512 struct inode *inode;
NeilBrowne04bbf62018-04-30 14:31:30 +1000513 struct inode *place_holder = NULL;
514 struct nfs_delegation *place_holder_deleg = NULL;
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500515 int err = 0;
Trond Myklebust515d8612008-12-23 15:21:46 -0500516
517restart:
NeilBrowne04bbf62018-04-30 14:31:30 +1000518 /*
519 * To avoid quadratic looping we hold a reference
520 * to an inode place_holder. Each time we restart, we
521 * list nfs_servers from the server of that inode, and
522 * delegation in the server from the delegations of that
523 * inode.
524 * prev is an RCU-protected pointer to a delegation which
525 * wasn't marked for return and might be a good choice for
526 * the next place_holder.
527 */
Trond Myklebust515d8612008-12-23 15:21:46 -0500528 rcu_read_lock();
NeilBrowne04bbf62018-04-30 14:31:30 +1000529 prev = NULL;
530 if (place_holder)
531 server = NFS_SERVER(place_holder);
532 else
533 server = list_entry_rcu(clp->cl_superblocks.next,
534 struct nfs_server, client_link);
535 list_for_each_entry_from_rcu(server, &clp->cl_superblocks, client_link) {
536 delegation = NULL;
537 if (place_holder && server == NFS_SERVER(place_holder))
538 delegation = rcu_dereference(NFS_I(place_holder)->delegation);
539 if (!delegation || delegation != place_holder_deleg)
540 delegation = list_entry_rcu(server->delegations.next,
541 struct nfs_delegation, super_list);
542 list_for_each_entry_from_rcu(delegation, &server->delegations, super_list) {
543 struct inode *to_put = NULL;
544
545 if (!nfs_delegation_need_return(delegation)) {
546 prev = delegation;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000547 continue;
NeilBrowne04bbf62018-04-30 14:31:30 +1000548 }
Trond Myklebust9f0f8e12015-02-26 09:57:34 -0500549 if (!nfs_sb_active(server->super))
NeilBrownf38934912018-05-31 15:23:22 +1000550 break; /* continue in outer loop */
NeilBrowne04bbf62018-04-30 14:31:30 +1000551
552 if (prev) {
553 struct inode *tmp;
554
555 tmp = nfs_delegation_grab_inode(prev);
556 if (tmp) {
557 to_put = place_holder;
558 place_holder = tmp;
559 place_holder_deleg = prev;
560 }
561 }
562
Trond Myklebust9f0f8e12015-02-26 09:57:34 -0500563 inode = nfs_delegation_grab_inode(delegation);
564 if (inode == NULL) {
565 rcu_read_unlock();
NeilBrowne04bbf62018-04-30 14:31:30 +1000566 if (to_put)
567 iput(to_put);
Trond Myklebust9f0f8e12015-02-26 09:57:34 -0500568 nfs_sb_deactive(server->super);
569 goto restart;
570 }
Trond Myklebustd25be542013-02-05 11:43:28 -0500571 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
Chuck Leverd3978bb2010-12-24 01:33:04 +0000572 rcu_read_unlock();
573
NeilBrowne04bbf62018-04-30 14:31:30 +1000574 if (to_put)
575 iput(to_put);
576
Trond Myklebustd25be542013-02-05 11:43:28 -0500577 err = nfs_end_delegation_return(inode, delegation, 0);
Chuck Leverd3978bb2010-12-24 01:33:04 +0000578 iput(inode);
Trond Myklebust9f0f8e12015-02-26 09:57:34 -0500579 nfs_sb_deactive(server->super);
NeilBrown3ca951b2018-04-30 14:31:30 +1000580 cond_resched();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000581 if (!err)
582 goto restart;
583 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
NeilBrowne04bbf62018-04-30 14:31:30 +1000584 if (place_holder)
585 iput(place_holder);
Chuck Leverd3978bb2010-12-24 01:33:04 +0000586 return err;
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500587 }
Trond Myklebust515d8612008-12-23 15:21:46 -0500588 }
589 rcu_read_unlock();
NeilBrowne04bbf62018-04-30 14:31:30 +1000590 if (place_holder)
591 iput(place_holder);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500592 return 0;
Trond Myklebust515d8612008-12-23 15:21:46 -0500593}
594
Chuck Leverd3978bb2010-12-24 01:33:04 +0000595/**
Trond Myklebustb47e0e42019-10-21 14:04:00 -0400596 * nfs_inode_evict_delegation - return delegation, don't reclaim opens
Chuck Leverd3978bb2010-12-24 01:33:04 +0000597 * @inode: inode to process
598 *
599 * Does not protect against delegation reclaims, therefore really only safe
Trond Myklebustb47e0e42019-10-21 14:04:00 -0400600 * to be called from nfs4_clear_inode(). Guaranteed to always free
601 * the delegation structure.
Trond Myklebuste6f81072008-01-24 18:14:34 -0500602 */
Trond Myklebustb47e0e42019-10-21 14:04:00 -0400603void nfs_inode_evict_delegation(struct inode *inode)
Trond Myklebuste6f81072008-01-24 18:14:34 -0500604{
Trond Myklebuste6f81072008-01-24 18:14:34 -0500605 struct nfs_delegation *delegation;
606
Trond Myklebustd25be542013-02-05 11:43:28 -0500607 delegation = nfs_inode_detach_delegation(inode);
Trond Myklebustb47e0e42019-10-21 14:04:00 -0400608 if (delegation != NULL) {
609 set_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags);
Trond Myklebust5fcdfac2015-03-25 13:19:42 -0400610 nfs_do_return_delegation(inode, delegation, 1);
Trond Myklebustb47e0e42019-10-21 14:04:00 -0400611 }
Trond Myklebuste6f81072008-01-24 18:14:34 -0500612}
613
Chuck Leverd3978bb2010-12-24 01:33:04 +0000614/**
615 * nfs_inode_return_delegation - synchronously return a delegation
616 * @inode: inode to process
617 *
Trond Myklebustc57d1bc2012-05-06 19:34:17 -0400618 * This routine will always flush any dirty data to disk on the
619 * assumption that if we need to return the delegation, then
620 * we should stop caching.
621 *
Chuck Leverd3978bb2010-12-24 01:33:04 +0000622 * Returns zero on success, or a negative errno value.
623 */
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400624int nfs4_inode_return_delegation(struct inode *inode)
Trond Myklebust90163022007-07-05 14:55:18 -0400625{
Trond Myklebust90163022007-07-05 14:55:18 -0400626 struct nfs_inode *nfsi = NFS_I(inode);
627 struct nfs_delegation *delegation;
628 int err = 0;
629
Trond Myklebustc57d1bc2012-05-06 19:34:17 -0400630 nfs_wb_all(inode);
Trond Myklebustd25be542013-02-05 11:43:28 -0500631 delegation = nfs_start_delegation_return(nfsi);
632 if (delegation != NULL)
633 err = nfs_end_delegation_return(inode, delegation, 1);
Trond Myklebust90163022007-07-05 14:55:18 -0400634 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635}
636
Trond Myklebustc01d3642018-03-20 16:43:20 -0400637/**
638 * nfs4_inode_make_writeable
639 * @inode: pointer to inode
640 *
641 * Make the inode writeable by returning the delegation if necessary
642 *
643 * Returns zero on success, or a negative errno value.
644 */
645int nfs4_inode_make_writeable(struct inode *inode)
646{
Trond Myklebust3887ce12019-10-27 13:48:18 -0400647 struct nfs_delegation *delegation;
648
649 rcu_read_lock();
650 delegation = nfs4_get_valid_delegation(inode);
651 if (delegation == NULL ||
652 (nfs4_has_session(NFS_SERVER(inode)->nfs_client) &&
653 (delegation->type & FMODE_WRITE))) {
654 rcu_read_unlock();
655 return 0;
656 }
657 rcu_read_unlock();
658 return nfs4_inode_return_delegation(inode);
Trond Myklebustc01d3642018-03-20 16:43:20 -0400659}
660
Trond Myklebustb7571442013-04-03 14:33:49 -0400661static void nfs_mark_return_if_closed_delegation(struct nfs_server *server,
662 struct nfs_delegation *delegation)
663{
664 set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
665 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
666}
667
Trond Myklebusted1e62112011-07-25 15:37:29 -0400668static void nfs_mark_return_delegation(struct nfs_server *server,
669 struct nfs_delegation *delegation)
Trond Myklebust6411bd42008-12-23 15:21:51 -0500670{
671 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
Trond Myklebusted1e62112011-07-25 15:37:29 -0400672 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
Trond Myklebust6411bd42008-12-23 15:21:51 -0500673}
674
Trond Myklebust5c31e232013-04-03 19:04:58 -0400675static bool nfs_server_mark_return_all_delegations(struct nfs_server *server)
676{
677 struct nfs_delegation *delegation;
678 bool ret = false;
679
680 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
681 nfs_mark_return_delegation(server, delegation);
682 ret = true;
683 }
684 return ret;
685}
686
Trond Myklebustb02ba0b2013-04-03 19:23:58 -0400687static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
688{
689 struct nfs_server *server;
690
691 rcu_read_lock();
692 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
693 nfs_server_mark_return_all_delegations(server);
694 rcu_read_unlock();
695}
696
697static void nfs_delegation_run_state_manager(struct nfs_client *clp)
698{
699 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
700 nfs4_schedule_state_manager(clp);
701}
702
703/**
704 * nfs_expire_all_delegations
705 * @clp: client to process
706 *
707 */
708void nfs_expire_all_delegations(struct nfs_client *clp)
709{
710 nfs_client_mark_return_all_delegations(clp);
711 nfs_delegation_run_state_manager(clp);
712}
713
Chuck Leverd3978bb2010-12-24 01:33:04 +0000714/**
715 * nfs_super_return_all_delegations - return delegations for one superblock
Trond Myklebust302fad72019-02-18 13:32:38 -0500716 * @server: pointer to nfs_server to process
Chuck Leverd3978bb2010-12-24 01:33:04 +0000717 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 */
Bryan Schumakereeebf912012-06-20 15:53:41 -0400719void nfs_server_return_all_delegations(struct nfs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000721 struct nfs_client *clp = server->nfs_client;
Trond Myklebust5c31e232013-04-03 19:04:58 -0400722 bool need_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 if (clp == NULL)
725 return;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000726
Trond Myklebust8383e462007-07-06 15:12:04 -0400727 rcu_read_lock();
Trond Myklebust5c31e232013-04-03 19:04:58 -0400728 need_wait = nfs_server_mark_return_all_delegations(server);
Trond Myklebust8383e462007-07-06 15:12:04 -0400729 rcu_read_unlock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000730
Trond Myklebust5c31e232013-04-03 19:04:58 -0400731 if (need_wait) {
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500732 nfs4_schedule_state_manager(clp);
Trond Myklebust5c31e232013-04-03 19:04:58 -0400733 nfs4_wait_clnt_recover(clp);
734 }
Trond Myklebust515d8612008-12-23 15:21:46 -0500735}
736
Trond Myklebust826e0012013-04-03 19:27:52 -0400737static void nfs_mark_return_unused_delegation_types(struct nfs_server *server,
Chuck Leverd3978bb2010-12-24 01:33:04 +0000738 fmode_t flags)
Trond Myklebust515d8612008-12-23 15:21:46 -0500739{
740 struct nfs_delegation *delegation;
741
Chuck Leverd3978bb2010-12-24 01:33:04 +0000742 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500743 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
744 continue;
745 if (delegation->type & flags)
Trond Myklebust826e0012013-04-03 19:27:52 -0400746 nfs_mark_return_if_closed_delegation(server, delegation);
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500747 }
Chuck Leverd3978bb2010-12-24 01:33:04 +0000748}
749
Trond Myklebust826e0012013-04-03 19:27:52 -0400750static void nfs_client_mark_return_unused_delegation_types(struct nfs_client *clp,
Chuck Leverd3978bb2010-12-24 01:33:04 +0000751 fmode_t flags)
752{
753 struct nfs_server *server;
754
755 rcu_read_lock();
756 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
Trond Myklebust826e0012013-04-03 19:27:52 -0400757 nfs_mark_return_unused_delegation_types(server, flags);
Trond Myklebust515d8612008-12-23 15:21:46 -0500758 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759}
760
Trond Myklebust41020b62016-09-22 13:38:58 -0400761static void nfs_mark_delegation_revoked(struct nfs_server *server,
762 struct nfs_delegation *delegation)
Trond Myklebust869f9df2014-11-10 18:43:56 -0500763{
Trond Myklebust41020b62016-09-22 13:38:58 -0400764 set_bit(NFS_DELEGATION_REVOKED, &delegation->flags);
Trond Myklebust059b43e2016-09-22 13:39:06 -0400765 delegation->stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust41020b62016-09-22 13:38:58 -0400766 nfs_mark_return_delegation(server, delegation);
Trond Myklebust869f9df2014-11-10 18:43:56 -0500767}
768
Trond Myklebust41020b62016-09-22 13:38:58 -0400769static bool nfs_revoke_delegation(struct inode *inode,
770 const nfs4_stateid *stateid)
771{
772 struct nfs_delegation *delegation;
Trond Myklebust7f048832016-09-22 13:39:14 -0400773 nfs4_stateid tmp;
Trond Myklebust41020b62016-09-22 13:38:58 -0400774 bool ret = false;
775
776 rcu_read_lock();
777 delegation = rcu_dereference(NFS_I(inode)->delegation);
778 if (delegation == NULL)
779 goto out;
Trond Myklebust7f048832016-09-22 13:39:14 -0400780 if (stateid == NULL) {
781 nfs4_stateid_copy(&tmp, &delegation->stateid);
782 stateid = &tmp;
Trond Myklebustf2d47b52019-10-21 14:15:32 -0400783 } else {
784 if (!nfs4_stateid_match_other(stateid, &delegation->stateid))
785 goto out;
786 spin_lock(&delegation->lock);
787 if (stateid->seqid) {
788 if (nfs4_stateid_is_newer(&delegation->stateid, stateid)) {
789 spin_unlock(&delegation->lock);
790 goto out;
791 }
792 delegation->stateid.seqid = stateid->seqid;
793 }
794 spin_unlock(&delegation->lock);
795 }
Trond Myklebust41020b62016-09-22 13:38:58 -0400796 nfs_mark_delegation_revoked(NFS_SERVER(inode), delegation);
797 ret = true;
798out:
799 rcu_read_unlock();
Trond Myklebust7f048832016-09-22 13:39:14 -0400800 if (ret)
801 nfs_inode_find_state_and_recover(inode, stateid);
Trond Myklebust41020b62016-09-22 13:38:58 -0400802 return ret;
803}
804
805void nfs_remove_bad_delegation(struct inode *inode,
806 const nfs4_stateid *stateid)
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500807{
808 struct nfs_delegation *delegation;
809
Trond Myklebust41020b62016-09-22 13:38:58 -0400810 if (!nfs_revoke_delegation(inode, stateid))
811 return;
Trond Myklebustd25be542013-02-05 11:43:28 -0500812 delegation = nfs_inode_detach_delegation(inode);
Trond Myklebust7f048832016-09-22 13:39:14 -0400813 if (delegation)
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500814 nfs_free_delegation(delegation);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500815}
Andy Adamson9cb81962012-03-07 10:49:41 -0500816EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500817
Trond Myklebustd51f91d2019-10-21 14:22:14 -0400818void nfs_delegation_mark_returned(struct inode *inode,
819 const nfs4_stateid *stateid)
820{
821 struct nfs_delegation *delegation;
822
823 if (!inode)
824 return;
825
826 rcu_read_lock();
827 delegation = rcu_dereference(NFS_I(inode)->delegation);
828 if (!delegation)
829 goto out_rcu_unlock;
830
831 spin_lock(&delegation->lock);
832 if (!nfs4_stateid_match_other(stateid, &delegation->stateid))
833 goto out_spin_unlock;
834 if (stateid->seqid) {
835 /* If delegation->stateid is newer, dont mark as returned */
836 if (nfs4_stateid_is_newer(&delegation->stateid, stateid))
837 goto out_clear_returning;
838 if (delegation->stateid.seqid != stateid->seqid)
839 delegation->stateid.seqid = stateid->seqid;
840 }
841
842 set_bit(NFS_DELEGATION_REVOKED, &delegation->flags);
843
844out_clear_returning:
845 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
846out_spin_unlock:
847 spin_unlock(&delegation->lock);
848out_rcu_unlock:
849 rcu_read_unlock();
850
851 nfs_inode_find_state_and_recover(inode, stateid);
852}
853
Chuck Leverd3978bb2010-12-24 01:33:04 +0000854/**
Trond Myklebust826e0012013-04-03 19:27:52 -0400855 * nfs_expire_unused_delegation_types
Chuck Leverd3978bb2010-12-24 01:33:04 +0000856 * @clp: client to process
857 * @flags: delegation types to expire
858 *
859 */
Trond Myklebust826e0012013-04-03 19:27:52 -0400860void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags)
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500861{
Trond Myklebust826e0012013-04-03 19:27:52 -0400862 nfs_client_mark_return_unused_delegation_types(clp, flags);
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500863 nfs_delegation_run_state_manager(clp);
864}
865
Chuck Leverd3978bb2010-12-24 01:33:04 +0000866static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
Trond Myklebustb7391f42008-12-23 15:21:52 -0500867{
868 struct nfs_delegation *delegation;
869
Chuck Leverd3978bb2010-12-24 01:33:04 +0000870 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
Trond Myklebustb7391f42008-12-23 15:21:52 -0500871 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
872 continue;
Trond Myklebustb7571442013-04-03 14:33:49 -0400873 nfs_mark_return_if_closed_delegation(server, delegation);
Trond Myklebustb7391f42008-12-23 15:21:52 -0500874 }
Trond Myklebustb7391f42008-12-23 15:21:52 -0500875}
876
Chuck Leverd3978bb2010-12-24 01:33:04 +0000877/**
878 * nfs_expire_unreferenced_delegations - Eliminate unused delegations
879 * @clp: nfs_client to process
880 *
881 */
Trond Myklebustb7391f42008-12-23 15:21:52 -0500882void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
883{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000884 struct nfs_server *server;
885
886 rcu_read_lock();
887 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
888 nfs_mark_return_unreferenced_delegations(server);
889 rcu_read_unlock();
890
Trond Myklebustb7391f42008-12-23 15:21:52 -0500891 nfs_delegation_run_state_manager(clp);
892}
893
Chuck Leverd3978bb2010-12-24 01:33:04 +0000894/**
895 * nfs_async_inode_return_delegation - asynchronously return a delegation
896 * @inode: inode to process
Trond Myklebust8e663f02012-03-04 18:13:56 -0500897 * @stateid: state ID information
Chuck Leverd3978bb2010-12-24 01:33:04 +0000898 *
899 * Returns zero on success, or a negative errno value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 */
Chuck Leverd3978bb2010-12-24 01:33:04 +0000901int nfs_async_inode_return_delegation(struct inode *inode,
902 const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903{
Trond Myklebusted1e62112011-07-25 15:37:29 -0400904 struct nfs_server *server = NFS_SERVER(inode);
905 struct nfs_client *clp = server->nfs_client;
Trond Myklebust6411bd42008-12-23 15:21:51 -0500906 struct nfs_delegation *delegation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907
Trond Myklebust6411bd42008-12-23 15:21:51 -0500908 rcu_read_lock();
Trond Myklebust457a5042019-10-22 08:46:06 -0400909 delegation = nfs4_get_valid_delegation(inode);
Trond Myklebust755a48a2014-03-02 22:03:12 -0500910 if (delegation == NULL)
911 goto out_enoent;
Trond Myklebust4816fda2015-09-20 14:58:42 -0400912 if (stateid != NULL &&
913 !clp->cl_mvops->match_stateid(&delegation->stateid, stateid))
Trond Myklebust755a48a2014-03-02 22:03:12 -0500914 goto out_enoent;
Trond Myklebusted1e62112011-07-25 15:37:29 -0400915 nfs_mark_return_delegation(server, delegation);
Trond Myklebust6411bd42008-12-23 15:21:51 -0500916 rcu_read_unlock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000917
Trond Myklebust6411bd42008-12-23 15:21:51 -0500918 nfs_delegation_run_state_manager(clp);
919 return 0;
Trond Myklebust755a48a2014-03-02 22:03:12 -0500920out_enoent:
921 rcu_read_unlock();
922 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923}
924
Chuck Leverd3978bb2010-12-24 01:33:04 +0000925static struct inode *
926nfs_delegation_find_inode_server(struct nfs_server *server,
927 const struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928{
929 struct nfs_delegation *delegation;
Trond Myklebuste39d8a12018-11-13 16:37:54 -0500930 struct inode *freeme, *res = NULL;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000931
932 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
Trond Myklebust86e89482008-12-23 15:21:39 -0500933 spin_lock(&delegation->lock);
934 if (delegation->inode != NULL &&
Trond Myklebust457a5042019-10-22 08:46:06 -0400935 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) &&
Trond Myklebust86e89482008-12-23 15:21:39 -0500936 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
Trond Myklebuste39d8a12018-11-13 16:37:54 -0500937 freeme = igrab(delegation->inode);
938 if (freeme && nfs_sb_active(freeme->i_sb))
939 res = freeme;
Trond Myklebust6c342652018-06-07 14:22:00 -0400940 spin_unlock(&delegation->lock);
941 if (res != NULL)
942 return res;
Trond Myklebuste39d8a12018-11-13 16:37:54 -0500943 if (freeme) {
944 rcu_read_unlock();
945 iput(freeme);
946 rcu_read_lock();
947 }
Trond Myklebust6c342652018-06-07 14:22:00 -0400948 return ERR_PTR(-EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
Trond Myklebust86e89482008-12-23 15:21:39 -0500950 spin_unlock(&delegation->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 }
Trond Myklebust6c342652018-06-07 14:22:00 -0400952 return ERR_PTR(-ENOENT);
Chuck Leverd3978bb2010-12-24 01:33:04 +0000953}
954
955/**
956 * nfs_delegation_find_inode - retrieve the inode associated with a delegation
957 * @clp: client state handle
958 * @fhandle: filehandle from a delegation recall
959 *
960 * Returns pointer to inode matching "fhandle," or NULL if a matching inode
961 * cannot be found.
962 */
963struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
964 const struct nfs_fh *fhandle)
965{
966 struct nfs_server *server;
Trond Myklebust6c342652018-06-07 14:22:00 -0400967 struct inode *res;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000968
969 rcu_read_lock();
970 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
971 res = nfs_delegation_find_inode_server(server, fhandle);
Anna Schumakerd5681f52018-06-14 09:39:17 -0400972 if (res != ERR_PTR(-ENOENT)) {
973 rcu_read_unlock();
Trond Myklebust6c342652018-06-07 14:22:00 -0400974 return res;
Anna Schumakerd5681f52018-06-14 09:39:17 -0400975 }
Chuck Leverd3978bb2010-12-24 01:33:04 +0000976 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400977 rcu_read_unlock();
Trond Myklebust6c342652018-06-07 14:22:00 -0400978 return ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979}
980
Chuck Leverd3978bb2010-12-24 01:33:04 +0000981static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
982{
983 struct nfs_delegation *delegation;
984
Trond Myklebust45870d62016-09-22 13:38:59 -0400985 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
986 /*
987 * If the delegation may have been admin revoked, then we
988 * cannot reclaim it.
989 */
990 if (test_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags))
991 continue;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000992 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
Trond Myklebust45870d62016-09-22 13:38:59 -0400993 }
Chuck Leverd3978bb2010-12-24 01:33:04 +0000994}
995
996/**
997 * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
998 * @clp: nfs_client to process
999 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 */
David Howellsadfa6f92006-08-22 20:06:08 -04001001void nfs_delegation_mark_reclaim(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002{
Chuck Leverd3978bb2010-12-24 01:33:04 +00001003 struct nfs_server *server;
1004
Trond Myklebust8383e462007-07-06 15:12:04 -04001005 rcu_read_lock();
Chuck Leverd3978bb2010-12-24 01:33:04 +00001006 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1007 nfs_delegation_mark_reclaim_server(server);
Trond Myklebust8383e462007-07-06 15:12:04 -04001008 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009}
1010
Chuck Leverd3978bb2010-12-24 01:33:04 +00001011/**
1012 * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
1013 * @clp: nfs_client to process
1014 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 */
David Howellsadfa6f92006-08-22 20:06:08 -04001016void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
Trond Myklebust8383e462007-07-06 15:12:04 -04001018 struct nfs_delegation *delegation;
Chuck Leverd3978bb2010-12-24 01:33:04 +00001019 struct nfs_server *server;
Trond Myklebust86e89482008-12-23 15:21:39 -05001020 struct inode *inode;
Chuck Leverd3978bb2010-12-24 01:33:04 +00001021
Trond Myklebust8383e462007-07-06 15:12:04 -04001022restart:
1023 rcu_read_lock();
Chuck Leverd3978bb2010-12-24 01:33:04 +00001024 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1025 list_for_each_entry_rcu(delegation, &server->delegations,
1026 super_list) {
Trond Myklebust6f9449b2019-02-21 14:51:25 -05001027 if (test_bit(NFS_DELEGATION_INODE_FREEING,
1028 &delegation->flags) ||
1029 test_bit(NFS_DELEGATION_RETURNING,
1030 &delegation->flags) ||
1031 test_bit(NFS_DELEGATION_NEED_RECLAIM,
Chuck Leverd3978bb2010-12-24 01:33:04 +00001032 &delegation->flags) == 0)
1033 continue;
Trond Myklebust9f0f8e12015-02-26 09:57:34 -05001034 if (!nfs_sb_active(server->super))
NeilBrownf38934912018-05-31 15:23:22 +10001035 break; /* continue in outer loop */
Trond Myklebust9f0f8e12015-02-26 09:57:34 -05001036 inode = nfs_delegation_grab_inode(delegation);
1037 if (inode == NULL) {
1038 rcu_read_unlock();
1039 nfs_sb_deactive(server->super);
1040 goto restart;
1041 }
Trond Myklebustb04b22f2015-02-26 13:59:38 -05001042 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
Chuck Leverd3978bb2010-12-24 01:33:04 +00001043 rcu_read_unlock();
Trond Myklebustb04b22f2015-02-26 13:59:38 -05001044 if (delegation != NULL) {
1045 delegation = nfs_detach_delegation(NFS_I(inode),
1046 delegation, server);
1047 if (delegation != NULL)
1048 nfs_free_delegation(delegation);
1049 }
Chuck Leverd3978bb2010-12-24 01:33:04 +00001050 iput(inode);
Trond Myklebust9f0f8e12015-02-26 09:57:34 -05001051 nfs_sb_deactive(server->super);
NeilBrown3ca951b2018-04-30 14:31:30 +10001052 cond_resched();
Chuck Leverd3978bb2010-12-24 01:33:04 +00001053 goto restart;
1054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
Trond Myklebust8383e462007-07-06 15:12:04 -04001056 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057}
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001058
Trond Myklebustbb3d1a32016-09-22 13:39:00 -04001059static inline bool nfs4_server_rebooted(const struct nfs_client *clp)
1060{
1061 return (clp->cl_state & (BIT(NFS4CLNT_CHECK_LEASE) |
1062 BIT(NFS4CLNT_LEASE_EXPIRED) |
1063 BIT(NFS4CLNT_SESSION_RESET))) != 0;
1064}
1065
Trond Myklebust45870d62016-09-22 13:38:59 -04001066static void nfs_mark_test_expired_delegation(struct nfs_server *server,
1067 struct nfs_delegation *delegation)
1068{
Trond Myklebust059b43e2016-09-22 13:39:06 -04001069 if (delegation->stateid.type == NFS4_INVALID_STATEID_TYPE)
1070 return;
Trond Myklebust45870d62016-09-22 13:38:59 -04001071 clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
1072 set_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
1073 set_bit(NFS4CLNT_DELEGATION_EXPIRED, &server->nfs_client->cl_state);
1074}
1075
Trond Myklebustbb3d1a32016-09-22 13:39:00 -04001076static void nfs_inode_mark_test_expired_delegation(struct nfs_server *server,
1077 struct inode *inode)
1078{
1079 struct nfs_delegation *delegation;
1080
1081 rcu_read_lock();
1082 delegation = rcu_dereference(NFS_I(inode)->delegation);
1083 if (delegation)
1084 nfs_mark_test_expired_delegation(server, delegation);
1085 rcu_read_unlock();
1086
1087}
1088
Trond Myklebust45870d62016-09-22 13:38:59 -04001089static void nfs_delegation_mark_test_expired_server(struct nfs_server *server)
1090{
1091 struct nfs_delegation *delegation;
1092
1093 list_for_each_entry_rcu(delegation, &server->delegations, super_list)
1094 nfs_mark_test_expired_delegation(server, delegation);
1095}
1096
1097/**
1098 * nfs_mark_test_expired_all_delegations - mark all delegations for testing
1099 * @clp: nfs_client to process
1100 *
1101 * Iterates through all the delegations associated with this server and
1102 * marks them as needing to be checked for validity.
1103 */
1104void nfs_mark_test_expired_all_delegations(struct nfs_client *clp)
1105{
1106 struct nfs_server *server;
1107
1108 rcu_read_lock();
1109 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1110 nfs_delegation_mark_test_expired_server(server);
1111 rcu_read_unlock();
1112}
1113
1114/**
Scott Mayhew8ca017c2019-05-06 11:59:05 -04001115 * nfs_test_expired_all_delegations - test all delegations for a client
1116 * @clp: nfs_client to process
1117 *
1118 * Helper for handling "recallable state revoked" status from server.
1119 */
1120void nfs_test_expired_all_delegations(struct nfs_client *clp)
1121{
1122 nfs_mark_test_expired_all_delegations(clp);
1123 nfs4_schedule_state_manager(clp);
1124}
1125
Trond Myklebustad114082019-07-26 14:40:53 +01001126static void
1127nfs_delegation_test_free_expired(struct inode *inode,
1128 nfs4_stateid *stateid,
1129 const struct cred *cred)
1130{
1131 struct nfs_server *server = NFS_SERVER(inode);
1132 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
1133 int status;
1134
1135 if (!cred)
1136 return;
1137 status = ops->test_and_free_expired(server, stateid, cred);
1138 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
1139 nfs_remove_bad_delegation(inode, stateid);
1140}
1141
Scott Mayhew8ca017c2019-05-06 11:59:05 -04001142/**
Trond Myklebust45870d62016-09-22 13:38:59 -04001143 * nfs_reap_expired_delegations - reap expired delegations
1144 * @clp: nfs_client to process
1145 *
1146 * Iterates through all the delegations associated with this server and
1147 * checks if they have may have been revoked. This function is usually
1148 * expected to be called in cases where the server may have lost its
1149 * lease.
1150 */
1151void nfs_reap_expired_delegations(struct nfs_client *clp)
1152{
Trond Myklebust45870d62016-09-22 13:38:59 -04001153 struct nfs_delegation *delegation;
1154 struct nfs_server *server;
1155 struct inode *inode;
NeilBrowna52458b2018-12-03 11:30:31 +11001156 const struct cred *cred;
Trond Myklebust45870d62016-09-22 13:38:59 -04001157 nfs4_stateid stateid;
1158
1159restart:
1160 rcu_read_lock();
1161 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1162 list_for_each_entry_rcu(delegation, &server->delegations,
1163 super_list) {
Trond Myklebust6f9449b2019-02-21 14:51:25 -05001164 if (test_bit(NFS_DELEGATION_INODE_FREEING,
1165 &delegation->flags) ||
1166 test_bit(NFS_DELEGATION_RETURNING,
1167 &delegation->flags) ||
1168 test_bit(NFS_DELEGATION_TEST_EXPIRED,
Trond Myklebust45870d62016-09-22 13:38:59 -04001169 &delegation->flags) == 0)
1170 continue;
1171 if (!nfs_sb_active(server->super))
NeilBrownf38934912018-05-31 15:23:22 +10001172 break; /* continue in outer loop */
Trond Myklebust45870d62016-09-22 13:38:59 -04001173 inode = nfs_delegation_grab_inode(delegation);
1174 if (inode == NULL) {
1175 rcu_read_unlock();
1176 nfs_sb_deactive(server->super);
1177 goto restart;
1178 }
NeilBrowna52458b2018-12-03 11:30:31 +11001179 cred = get_cred_rcu(delegation->cred);
Trond Myklebust45870d62016-09-22 13:38:59 -04001180 nfs4_stateid_copy(&stateid, &delegation->stateid);
1181 clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
1182 rcu_read_unlock();
Trond Myklebustad114082019-07-26 14:40:53 +01001183 nfs_delegation_test_free_expired(inode, &stateid, cred);
NeilBrowna52458b2018-12-03 11:30:31 +11001184 put_cred(cred);
Trond Myklebustbb3d1a32016-09-22 13:39:00 -04001185 if (nfs4_server_rebooted(clp)) {
1186 nfs_inode_mark_test_expired_delegation(server,inode);
1187 iput(inode);
1188 nfs_sb_deactive(server->super);
1189 return;
1190 }
Trond Myklebust45870d62016-09-22 13:38:59 -04001191 iput(inode);
1192 nfs_sb_deactive(server->super);
NeilBrown3ca951b2018-04-30 14:31:30 +10001193 cond_resched();
Trond Myklebust45870d62016-09-22 13:38:59 -04001194 goto restart;
1195 }
1196 }
1197 rcu_read_unlock();
1198}
1199
Trond Myklebust6c2d8f82016-09-22 13:39:07 -04001200void nfs_inode_find_delegation_state_and_recover(struct inode *inode,
1201 const nfs4_stateid *stateid)
1202{
1203 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1204 struct nfs_delegation *delegation;
1205 bool found = false;
1206
1207 rcu_read_lock();
1208 delegation = rcu_dereference(NFS_I(inode)->delegation);
1209 if (delegation &&
Trond Myklebust42c304c2019-10-26 10:16:15 -04001210 nfs4_stateid_match_or_older(&delegation->stateid, stateid) &&
1211 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
Trond Myklebust6c2d8f82016-09-22 13:39:07 -04001212 nfs_mark_test_expired_delegation(NFS_SERVER(inode), delegation);
1213 found = true;
1214 }
1215 rcu_read_unlock();
1216 if (found)
1217 nfs4_schedule_state_manager(clp);
1218}
1219
Chuck Leverd3978bb2010-12-24 01:33:04 +00001220/**
1221 * nfs_delegations_present - check for existence of delegations
1222 * @clp: client state handle
1223 *
1224 * Returns one if there are any nfs_delegation structures attached
1225 * to this nfs_client.
1226 */
1227int nfs_delegations_present(struct nfs_client *clp)
1228{
1229 struct nfs_server *server;
1230 int ret = 0;
1231
1232 rcu_read_lock();
1233 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1234 if (!list_empty(&server->delegations)) {
1235 ret = 1;
1236 break;
1237 }
1238 rcu_read_unlock();
1239 return ret;
1240}
1241
1242/**
Trond Myklebust12f275c2017-11-06 15:28:05 -05001243 * nfs4_refresh_delegation_stateid - Update delegation stateid seqid
1244 * @dst: stateid to refresh
1245 * @inode: inode to check
1246 *
1247 * Returns "true" and updates "dst->seqid" * if inode had a delegation
1248 * that matches our delegation stateid. Otherwise "false" is returned.
1249 */
1250bool nfs4_refresh_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
1251{
1252 struct nfs_delegation *delegation;
1253 bool ret = false;
1254 if (!inode)
1255 goto out;
1256
1257 rcu_read_lock();
1258 delegation = rcu_dereference(NFS_I(inode)->delegation);
1259 if (delegation != NULL &&
Trond Myklebustb5756202019-10-22 13:34:06 -04001260 nfs4_stateid_match_other(dst, &delegation->stateid) &&
1261 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
Trond Myklebust12f275c2017-11-06 15:28:05 -05001262 dst->seqid = delegation->stateid.seqid;
Trond Myklebust79cc5542019-10-31 18:40:33 -04001263 ret = true;
Trond Myklebust12f275c2017-11-06 15:28:05 -05001264 }
1265 rcu_read_unlock();
1266out:
1267 return ret;
1268}
1269
1270/**
Chuck Leverd3978bb2010-12-24 01:33:04 +00001271 * nfs4_copy_delegation_stateid - Copy inode's state ID information
Chuck Leverd3978bb2010-12-24 01:33:04 +00001272 * @inode: inode to check
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001273 * @flags: delegation type requirement
Trond Myklebustabf4e132016-05-16 17:42:44 -04001274 * @dst: stateid data structure to fill in
1275 * @cred: optional argument to retrieve credential
Chuck Leverd3978bb2010-12-24 01:33:04 +00001276 *
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001277 * Returns "true" and fills in "dst->data" * if inode had a delegation,
1278 * otherwise "false" is returned.
Chuck Leverd3978bb2010-12-24 01:33:04 +00001279 */
Trond Myklebustabf4e132016-05-16 17:42:44 -04001280bool nfs4_copy_delegation_stateid(struct inode *inode, fmode_t flags,
NeilBrowna52458b2018-12-03 11:30:31 +11001281 nfs4_stateid *dst, const struct cred **cred)
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001282{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001283 struct nfs_inode *nfsi = NFS_I(inode);
1284 struct nfs_delegation *delegation;
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001285 bool ret;
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001286
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001287 flags &= FMODE_READ|FMODE_WRITE;
Trond Myklebust8383e462007-07-06 15:12:04 -04001288 rcu_read_lock();
1289 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebustaa05c872016-09-22 13:38:54 -04001290 ret = nfs4_is_valid_delegation(delegation, flags);
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001291 if (ret) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001292 nfs4_stateid_copy(dst, &delegation->stateid);
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001293 nfs_mark_delegation_referenced(delegation);
Trond Myklebustabf4e132016-05-16 17:42:44 -04001294 if (cred)
NeilBrowna52458b2018-12-03 11:30:31 +11001295 *cred = get_cred(delegation->cred);
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001296 }
Trond Myklebust8383e462007-07-06 15:12:04 -04001297 rcu_read_unlock();
1298 return ret;
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001299}
Trond Myklebust5445b1f2015-09-05 19:06:58 -04001300
1301/**
1302 * nfs4_delegation_flush_on_close - Check if we must flush file on close
1303 * @inode: inode to check
1304 *
1305 * This function checks the number of outstanding writes to the file
1306 * against the delegation 'space_limit' field to see if
1307 * the spec requires us to flush the file on close.
1308 */
1309bool nfs4_delegation_flush_on_close(const struct inode *inode)
1310{
1311 struct nfs_inode *nfsi = NFS_I(inode);
1312 struct nfs_delegation *delegation;
1313 bool ret = true;
1314
1315 rcu_read_lock();
1316 delegation = rcu_dereference(nfsi->delegation);
1317 if (delegation == NULL || !(delegation->type & FMODE_WRITE))
1318 goto out;
Trond Myklebusta6b6d5b2017-08-01 15:39:46 -04001319 if (atomic_long_read(&nfsi->nrequests) < delegation->pagemod_limit)
Trond Myklebust5445b1f2015-09-05 19:06:58 -04001320 ret = false;
1321out:
1322 rcu_read_unlock();
1323 return ret;
1324}