blob: a777b3d0e7204903ffd431ce997976ea3bb3ec6e [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 Myklebustd2269ea2020-01-27 09:58:18 -050028static atomic_long_t nfs_active_delegations;
29
30static void __nfs_free_delegation(struct nfs_delegation *delegation)
Trond Myklebust905f8d12007-08-06 12:18:34 -040031{
NeilBrowna52458b2018-12-03 11:30:31 +110032 put_cred(delegation->cred);
33 delegation->cred = NULL;
Lai Jiangshan26f04dd2011-05-01 06:21:54 -070034 kfree_rcu(delegation, rcu);
Trond Myklebust8383e462007-07-06 15:12:04 -040035}
36
Trond Myklebustd2269ea2020-01-27 09:58:18 -050037static void nfs_mark_delegation_revoked(struct nfs_delegation *delegation)
38{
39 if (!test_and_set_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
40 delegation->stateid.type = NFS4_INVALID_STATEID_TYPE;
41 atomic_long_dec(&nfs_active_delegations);
42 }
43}
44
45static void nfs_free_delegation(struct nfs_delegation *delegation)
46{
47 nfs_mark_delegation_revoked(delegation);
48 __nfs_free_delegation(delegation);
49}
50
Chuck Leverd3978bb2010-12-24 01:33:04 +000051/**
52 * nfs_mark_delegation_referenced - set delegation's REFERENCED flag
53 * @delegation: delegation to process
54 *
55 */
Trond Myklebustb7391f42008-12-23 15:21:52 -050056void nfs_mark_delegation_referenced(struct nfs_delegation *delegation)
57{
58 set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags);
59}
60
Trond Myklebustaa05c872016-09-22 13:38:54 -040061static bool
62nfs4_is_valid_delegation(const struct nfs_delegation *delegation,
63 fmode_t flags)
64{
65 if (delegation != NULL && (delegation->type & flags) == flags &&
66 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) &&
67 !test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
68 return true;
69 return false;
70}
71
Trond Myklebustbe3df3d2019-10-31 18:40:32 -040072struct nfs_delegation *nfs4_get_valid_delegation(const struct inode *inode)
73{
74 struct nfs_delegation *delegation;
75
76 delegation = rcu_dereference(NFS_I(inode)->delegation);
77 if (nfs4_is_valid_delegation(delegation, 0))
78 return delegation;
79 return NULL;
80}
81
Peng Tao15bb3af2014-07-03 13:05:00 +080082static int
83nfs4_do_check_delegation(struct inode *inode, fmode_t flags, bool mark)
Trond Myklebustb7391f42008-12-23 15:21:52 -050084{
85 struct nfs_delegation *delegation;
86 int ret = 0;
87
88 flags &= FMODE_READ|FMODE_WRITE;
89 rcu_read_lock();
90 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustaa05c872016-09-22 13:38:54 -040091 if (nfs4_is_valid_delegation(delegation, flags)) {
Peng Tao15bb3af2014-07-03 13:05:00 +080092 if (mark)
93 nfs_mark_delegation_referenced(delegation);
Trond Myklebustb7391f42008-12-23 15:21:52 -050094 ret = 1;
95 }
96 rcu_read_unlock();
97 return ret;
98}
Peng Tao15bb3af2014-07-03 13:05:00 +080099/**
100 * nfs_have_delegation - check if inode has a delegation, mark it
101 * NFS_DELEGATION_REFERENCED if there is one.
102 * @inode: inode to check
103 * @flags: delegation types to check for
104 *
105 * Returns one if inode has the indicated delegation, otherwise zero.
106 */
107int nfs4_have_delegation(struct inode *inode, fmode_t flags)
108{
109 return nfs4_do_check_delegation(inode, flags, true);
110}
111
112/*
113 * nfs4_check_delegation - check if inode has a delegation, do not mark
114 * NFS_DELEGATION_REFERENCED if it has one.
115 */
116int nfs4_check_delegation(struct inode *inode, fmode_t flags)
117{
118 return nfs4_do_check_delegation(inode, flags, false);
119}
Trond Myklebustb7391f42008-12-23 15:21:52 -0500120
Olga Kornievskaia44f411c2018-10-04 14:45:00 -0400121static int nfs_delegation_claim_locks(struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -0500122{
123 struct inode *inode = state->inode;
124 struct file_lock *fl;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500125 struct file_lock_context *flctx = inode->i_flctx;
126 struct list_head *list;
Trond Myklebustd5122202009-06-17 13:22:58 -0700127 int status = 0;
Trond Myklebust888e6942005-11-04 15:38:11 -0500128
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500129 if (flctx == NULL)
Trond Myklebust65b62a22013-02-07 10:54:07 -0500130 goto out;
Jeff Layton314d7cc2013-04-10 15:36:48 -0400131
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500132 list = &flctx->flc_posix;
Jeff Layton6109c852015-01-16 15:05:57 -0500133 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500134restart:
135 list_for_each_entry(fl, list, fl_list) {
Olga Kornievskaia44f411c2018-10-04 14:45:00 -0400136 if (nfs_file_open_context(fl->fl_file)->state != state)
Trond Myklebust888e6942005-11-04 15:38:11 -0500137 continue;
Jeff Layton6109c852015-01-16 15:05:57 -0500138 spin_unlock(&flctx->flc_lock);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -0400139 status = nfs4_lock_delegation_recall(fl, state, stateid);
Trond Myklebustd5122202009-06-17 13:22:58 -0700140 if (status < 0)
Trond Myklebust3f09df72009-06-17 13:23:00 -0700141 goto out;
Jeff Layton6109c852015-01-16 15:05:57 -0500142 spin_lock(&flctx->flc_lock);
Trond Myklebust888e6942005-11-04 15:38:11 -0500143 }
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500144 if (list == &flctx->flc_posix) {
145 list = &flctx->flc_flock;
146 goto restart;
Jeff Layton5263e312015-01-16 15:05:55 -0500147 }
Jeff Layton6109c852015-01-16 15:05:57 -0500148 spin_unlock(&flctx->flc_lock);
Trond Myklebust3f09df72009-06-17 13:23:00 -0700149out:
Trond Myklebust888e6942005-11-04 15:38:11 -0500150 return status;
151}
152
Trond Myklebust24311f82015-09-20 10:50:17 -0400153static int nfs_delegation_claim_opens(struct inode *inode,
154 const nfs4_stateid *stateid, fmode_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
156 struct nfs_inode *nfsi = NFS_I(inode);
157 struct nfs_open_context *ctx;
Trond Myklebustd25be542013-02-05 11:43:28 -0500158 struct nfs4_state_owner *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 struct nfs4_state *state;
Trond Myklebustd25be542013-02-05 11:43:28 -0500160 unsigned int seq;
Trond Myklebust888e6942005-11-04 15:38:11 -0500161 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163again:
Trond Myklebust0de43972018-09-02 15:57:01 -0400164 rcu_read_lock();
165 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 state = ctx->state;
167 if (state == NULL)
168 continue;
169 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
170 continue;
Trond Myklebustf8ebf7a2014-10-17 23:02:52 +0300171 if (!nfs4_valid_open_stateid(state))
172 continue;
Trond Myklebustf597c532012-03-04 18:13:56 -0500173 if (!nfs4_stateid_match(&state->stateid, stateid))
Trond Myklebust90163022007-07-05 14:55:18 -0400174 continue;
Trond Myklebust0de43972018-09-02 15:57:01 -0400175 if (!get_nfs_open_context(ctx))
176 continue;
177 rcu_read_unlock();
Trond Myklebustd25be542013-02-05 11:43:28 -0500178 sp = state->owner;
Trond Myklebust65b62a22013-02-07 10:54:07 -0500179 /* Block nfs4_proc_unlck */
180 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebustd25be542013-02-05 11:43:28 -0500181 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
Trond Myklebust5eb8d182019-07-19 14:08:37 -0400182 err = nfs4_open_delegation_recall(ctx, state, stateid);
Trond Myklebustd25be542013-02-05 11:43:28 -0500183 if (!err)
Olga Kornievskaia44f411c2018-10-04 14:45:00 -0400184 err = nfs_delegation_claim_locks(state, stateid);
Trond Myklebustd25be542013-02-05 11:43:28 -0500185 if (!err && read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
186 err = -EAGAIN;
Trond Myklebust65b62a22013-02-07 10:54:07 -0500187 mutex_unlock(&sp->so_delegreturn_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 put_nfs_open_context(ctx);
Trond Myklebust888e6942005-11-04 15:38:11 -0500189 if (err != 0)
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500190 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 goto again;
192 }
Trond Myklebust0de43972018-09-02 15:57:01 -0400193 rcu_read_unlock();
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500194 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
Chuck Leverd3978bb2010-12-24 01:33:04 +0000197/**
198 * nfs_inode_reclaim_delegation - process a delegation reclaim request
199 * @inode: inode to process
200 * @cred: credential to use for request
Trond Myklebust35156bf2018-03-20 17:03:13 -0400201 * @type: delegation type
202 * @stateid: delegation stateid
203 * @pagemod_limit: write delegation "space_limit"
Chuck Leverd3978bb2010-12-24 01:33:04 +0000204 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 */
NeilBrowna52458b2018-12-03 11:30:31 +1100206void nfs_inode_reclaim_delegation(struct inode *inode, const struct cred *cred,
Trond Myklebust35156bf2018-03-20 17:03:13 -0400207 fmode_t type,
208 const nfs4_stateid *stateid,
209 unsigned long pagemod_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Trond Myklebust8f649c32010-05-01 12:36:18 -0400211 struct nfs_delegation *delegation;
NeilBrowna52458b2018-12-03 11:30:31 +1100212 const struct cred *oldcred = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
Trond Myklebust8f649c32010-05-01 12:36:18 -0400214 rcu_read_lock();
215 delegation = rcu_dereference(NFS_I(inode)->delegation);
216 if (delegation != NULL) {
217 spin_lock(&delegation->lock);
Trond Myklebust1deed572019-10-22 08:52:47 -0400218 if (nfs4_is_valid_delegation(delegation, 0)) {
Trond Myklebust35156bf2018-03-20 17:03:13 -0400219 nfs4_stateid_copy(&delegation->stateid, stateid);
220 delegation->type = type;
221 delegation->pagemod_limit = pagemod_limit;
Trond Myklebust8f649c32010-05-01 12:36:18 -0400222 oldcred = delegation->cred;
NeilBrowna52458b2018-12-03 11:30:31 +1100223 delegation->cred = get_cred(cred);
Trond Myklebust8f649c32010-05-01 12:36:18 -0400224 clear_bit(NFS_DELEGATION_NEED_RECLAIM,
225 &delegation->flags);
Trond Myklebust8f649c32010-05-01 12:36:18 -0400226 spin_unlock(&delegation->lock);
Trond Myklebust8f649c32010-05-01 12:36:18 -0400227 rcu_read_unlock();
NeilBrowna52458b2018-12-03 11:30:31 +1100228 put_cred(oldcred);
Trond Myklebust35156bf2018-03-20 17:03:13 -0400229 trace_nfs4_reclaim_delegation(inode, type);
Trond Myklebustb1a318d2016-09-22 13:39:12 -0400230 return;
Trond Myklebust8f649c32010-05-01 12:36:18 -0400231 }
Trond Myklebustb1a318d2016-09-22 13:39:12 -0400232 /* We appear to have raced with a delegation return. */
233 spin_unlock(&delegation->lock);
Trond Myklebust8f649c32010-05-01 12:36:18 -0400234 }
Trond Myklebustb1a318d2016-09-22 13:39:12 -0400235 rcu_read_unlock();
Trond Myklebust35156bf2018-03-20 17:03:13 -0400236 nfs_inode_set_delegation(inode, cred, type, stateid, pagemod_limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237}
238
Trond Myklebust57bfa892008-01-25 16:38:18 -0500239static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
240{
241 int res = 0;
242
Trond Myklebust869f9df2014-11-10 18:43:56 -0500243 if (!test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
244 res = nfs4_proc_delegreturn(inode,
245 delegation->cred,
246 &delegation->stateid,
247 issync);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500248 return res;
249}
250
Trond Myklebust86e89482008-12-23 15:21:39 -0500251static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
252{
253 struct inode *inode = NULL;
254
255 spin_lock(&delegation->lock);
256 if (delegation->inode != NULL)
257 inode = igrab(delegation->inode);
Trond Myklebust6f9449b2019-02-21 14:51:25 -0500258 if (!inode)
259 set_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags);
Trond Myklebust86e89482008-12-23 15:21:39 -0500260 spin_unlock(&delegation->lock);
261 return inode;
262}
263
Chuck Leverdda4b222010-12-24 01:32:54 +0000264static struct nfs_delegation *
Trond Myklebustd25be542013-02-05 11:43:28 -0500265nfs_start_delegation_return_locked(struct nfs_inode *nfsi)
Trond Myklebust57bfa892008-01-25 16:38:18 -0500266{
Trond Myklebustd25be542013-02-05 11:43:28 -0500267 struct nfs_delegation *ret = NULL;
268 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500269
270 if (delegation == NULL)
Trond Myklebustd25be542013-02-05 11:43:28 -0500271 goto out;
272 spin_lock(&delegation->lock);
273 if (!test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
274 ret = delegation;
275 spin_unlock(&delegation->lock);
276out:
277 return ret;
278}
279
280static struct nfs_delegation *
281nfs_start_delegation_return(struct nfs_inode *nfsi)
282{
283 struct nfs_delegation *delegation;
284
285 rcu_read_lock();
286 delegation = nfs_start_delegation_return_locked(nfsi);
287 rcu_read_unlock();
288 return delegation;
289}
290
291static void
292nfs_abort_delegation_return(struct nfs_delegation *delegation,
293 struct nfs_client *clp)
294{
Chuck Leverdda4b222010-12-24 01:32:54 +0000295
Trond Myklebust34310432008-12-23 15:21:38 -0500296 spin_lock(&delegation->lock);
Trond Myklebustd25be542013-02-05 11:43:28 -0500297 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
298 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
299 spin_unlock(&delegation->lock);
300 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
301}
302
303static struct nfs_delegation *
304nfs_detach_delegation_locked(struct nfs_inode *nfsi,
305 struct nfs_delegation *delegation,
306 struct nfs_client *clp)
307{
308 struct nfs_delegation *deleg_cur =
309 rcu_dereference_protected(nfsi->delegation,
310 lockdep_is_held(&clp->cl_lock));
311
312 if (deleg_cur == NULL || delegation != deleg_cur)
313 return NULL;
314
315 spin_lock(&delegation->lock);
Trond Myklebustf9e0cc92019-10-21 14:12:13 -0400316 if (!delegation->inode) {
317 spin_unlock(&delegation->lock);
318 return NULL;
319 }
Trond Myklebust57bfa892008-01-25 16:38:18 -0500320 list_del_rcu(&delegation->super_list);
Trond Myklebust86e89482008-12-23 15:21:39 -0500321 delegation->inode = NULL;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500322 rcu_assign_pointer(nfsi->delegation, NULL);
Trond Myklebust34310432008-12-23 15:21:38 -0500323 spin_unlock(&delegation->lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500324 return delegation;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500325}
326
Chuck Leverdda4b222010-12-24 01:32:54 +0000327static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi,
Trond Myklebustd25be542013-02-05 11:43:28 -0500328 struct nfs_delegation *delegation,
329 struct nfs_server *server)
Chuck Leverdda4b222010-12-24 01:32:54 +0000330{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000331 struct nfs_client *clp = server->nfs_client;
Chuck Leverdda4b222010-12-24 01:32:54 +0000332
333 spin_lock(&clp->cl_lock);
Trond Myklebustd25be542013-02-05 11:43:28 -0500334 delegation = nfs_detach_delegation_locked(nfsi, delegation, clp);
Chuck Leverdda4b222010-12-24 01:32:54 +0000335 spin_unlock(&clp->cl_lock);
336 return delegation;
337}
338
Trond Myklebustd25be542013-02-05 11:43:28 -0500339static struct nfs_delegation *
340nfs_inode_detach_delegation(struct inode *inode)
341{
342 struct nfs_inode *nfsi = NFS_I(inode);
343 struct nfs_server *server = NFS_SERVER(inode);
344 struct nfs_delegation *delegation;
345
Trond Myklebustee05f452019-10-21 13:56:59 -0400346 rcu_read_lock();
347 delegation = rcu_dereference(nfsi->delegation);
348 if (delegation != NULL)
349 delegation = nfs_detach_delegation(nfsi, delegation, server);
350 rcu_read_unlock();
351 return delegation;
Trond Myklebustd25be542013-02-05 11:43:28 -0500352}
353
Trond Myklebustcf6726e2014-12-19 11:22:28 -0500354static void
355nfs_update_inplace_delegation(struct nfs_delegation *delegation,
356 const struct nfs_delegation *update)
357{
358 if (nfs4_stateid_is_newer(&update->stateid, &delegation->stateid)) {
359 delegation->stateid.seqid = update->stateid.seqid;
360 smp_wmb();
361 delegation->type = update->type;
Trond Myklebustd2269ea2020-01-27 09:58:18 -0500362 if (test_and_clear_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
363 atomic_long_inc(&nfs_active_delegations);
Trond Myklebustcf6726e2014-12-19 11:22:28 -0500364 }
365}
366
Chuck Leverd3978bb2010-12-24 01:33:04 +0000367/**
368 * nfs_inode_set_delegation - set up a delegation on an inode
369 * @inode: inode to which delegation applies
370 * @cred: cred to use for subsequent delegation processing
Trond Myklebust35156bf2018-03-20 17:03:13 -0400371 * @type: delegation type
372 * @stateid: delegation stateid
373 * @pagemod_limit: write delegation "space_limit"
Chuck Leverd3978bb2010-12-24 01:33:04 +0000374 *
375 * Returns zero on success, or a negative errno value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 */
NeilBrowna52458b2018-12-03 11:30:31 +1100377int nfs_inode_set_delegation(struct inode *inode, const struct cred *cred,
Trond Myklebust35156bf2018-03-20 17:03:13 -0400378 fmode_t type,
379 const nfs4_stateid *stateid,
380 unsigned long pagemod_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000382 struct nfs_server *server = NFS_SERVER(inode);
383 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 struct nfs_inode *nfsi = NFS_I(inode);
David Howells17d2c0a2010-05-01 12:37:18 -0400385 struct nfs_delegation *delegation, *old_delegation;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500386 struct nfs_delegation *freeme = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 int status = 0;
388
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400389 delegation = kmalloc(sizeof(*delegation), GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (delegation == NULL)
391 return -ENOMEM;
Trond Myklebust35156bf2018-03-20 17:03:13 -0400392 nfs4_stateid_copy(&delegation->stateid, stateid);
393 delegation->type = type;
394 delegation->pagemod_limit = pagemod_limit;
Jeff Layton1eb5d982018-01-09 08:21:17 -0500395 delegation->change_attr = inode_peek_iversion_raw(inode);
NeilBrowna52458b2018-12-03 11:30:31 +1100396 delegation->cred = get_cred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 delegation->inode = inode;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500398 delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
Trond Myklebust34310432008-12-23 15:21:38 -0500399 spin_lock_init(&delegation->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 spin_lock(&clp->cl_lock);
David Howells17d2c0a2010-05-01 12:37:18 -0400402 old_delegation = rcu_dereference_protected(nfsi->delegation,
Chuck Leverd3978bb2010-12-24 01:33:04 +0000403 lockdep_is_held(&clp->cl_lock));
Trond Myklebustee05f452019-10-21 13:56:59 -0400404 if (old_delegation == NULL)
405 goto add_new;
406 /* Is this an update of the existing delegation? */
407 if (nfs4_stateid_match_other(&old_delegation->stateid,
408 &delegation->stateid)) {
409 spin_lock(&old_delegation->lock);
410 nfs_update_inplace_delegation(old_delegation,
411 delegation);
412 spin_unlock(&old_delegation->lock);
413 goto out;
414 }
415 if (!test_bit(NFS_DELEGATION_REVOKED, &old_delegation->flags)) {
Trond Myklebust57bfa892008-01-25 16:38:18 -0500416 /*
417 * Deal with broken servers that hand out two
418 * delegations for the same file.
Trond Myklebust17280172012-03-11 13:11:00 -0400419 * Allow for upgrades to a WRITE delegation, but
420 * nothing else.
Trond Myklebust57bfa892008-01-25 16:38:18 -0500421 */
422 dfprintk(FILE, "%s: server %s handed out "
423 "a duplicate delegation!\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700424 __func__, clp->cl_hostname);
Trond Myklebust17280172012-03-11 13:11:00 -0400425 if (delegation->type == old_delegation->type ||
426 !(delegation->type & FMODE_WRITE)) {
Trond Myklebust57bfa892008-01-25 16:38:18 -0500427 freeme = delegation;
428 delegation = NULL;
429 goto out;
430 }
Trond Myklebustade04642015-02-27 14:25:50 -0500431 if (test_and_set_bit(NFS_DELEGATION_RETURNING,
432 &old_delegation->flags))
433 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
Trond Myklebustee05f452019-10-21 13:56:59 -0400435 freeme = nfs_detach_delegation_locked(nfsi, old_delegation, clp);
436 if (freeme == NULL)
437 goto out;
438add_new:
Trond Myklebust38942ba2015-03-04 15:59:05 -0500439 list_add_tail_rcu(&delegation->super_list, &server->delegations);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500440 rcu_assign_pointer(nfsi->delegation, delegation);
441 delegation = NULL;
Trond Myklebust412c77c2007-07-03 16:10:55 -0400442
Trond Myklebustd2269ea2020-01-27 09:58:18 -0500443 atomic_long_inc(&nfs_active_delegations);
444
Trond Myklebust35156bf2018-03-20 17:03:13 -0400445 trace_nfs4_set_delegation(inode, type);
Trond Myklebust412c77c2007-07-03 16:10:55 -0400446
Trond Myklebust97c2c172018-04-07 18:43:17 -0400447 spin_lock(&inode->i_lock);
448 if (NFS_I(inode)->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ATIME))
449 NFS_I(inode)->cache_validity |= NFS_INO_REVAL_FORCED;
450 spin_unlock(&inode->i_lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500451out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 spin_unlock(&clp->cl_lock);
Trond Myklebust603c83d2007-10-18 19:59:20 -0400453 if (delegation != NULL)
Trond Myklebustd2269ea2020-01-27 09:58:18 -0500454 __nfs_free_delegation(delegation);
Trond Myklebustee05f452019-10-21 13:56:59 -0400455 if (freeme != NULL) {
Trond Myklebust57bfa892008-01-25 16:38:18 -0500456 nfs_do_return_delegation(inode, freeme, 0);
Trond Myklebustee05f452019-10-21 13:56:59 -0400457 nfs_free_delegation(freeme);
458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 return status;
460}
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462/*
463 * Basic procedure for returning a delegation to the server
464 */
Trond Myklebustd25be542013-02-05 11:43:28 -0500465static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Trond Myklebustd25be542013-02-05 11:43:28 -0500467 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Trond Myklebust869f9df2014-11-10 18:43:56 -0500468 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469
Trond Myklebustd25be542013-02-05 11:43:28 -0500470 if (delegation == NULL)
471 return 0;
472 do {
Trond Myklebust869f9df2014-11-10 18:43:56 -0500473 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
474 break;
Trond Myklebust24311f82015-09-20 10:50:17 -0400475 err = nfs_delegation_claim_opens(inode, &delegation->stateid,
476 delegation->type);
Trond Myklebustd25be542013-02-05 11:43:28 -0500477 if (!issync || err != -EAGAIN)
478 break;
479 /*
480 * Guard against state recovery
481 */
482 err = nfs4_wait_clnt_recover(clp);
483 } while (err == 0);
484
485 if (err) {
486 nfs_abort_delegation_return(delegation, clp);
487 goto out;
488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500490 err = nfs_do_return_delegation(inode, delegation, issync);
491out:
492 return err;
Trond Myklebust90163022007-07-05 14:55:18 -0400493}
494
Trond Myklebustb7571442013-04-03 14:33:49 -0400495static bool nfs_delegation_need_return(struct nfs_delegation *delegation)
496{
497 bool ret = false;
498
499 if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
500 ret = true;
Trond Myklebust0d104162020-01-27 09:58:16 -0500501 else if (test_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags)) {
Trond Myklebustb7571442013-04-03 14:33:49 -0400502 struct inode *inode;
503
504 spin_lock(&delegation->lock);
505 inode = delegation->inode;
506 if (inode && list_empty(&NFS_I(inode)->open_files))
507 ret = true;
508 spin_unlock(&delegation->lock);
509 }
Trond Myklebust0d104162020-01-27 09:58:16 -0500510 if (ret)
511 clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
Trond Myklebustaf20b7b2019-10-22 13:40:47 -0400512 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags) ||
513 test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
514 ret = false;
515
Trond Myklebustb7571442013-04-03 14:33:49 -0400516 return ret;
517}
518
Chuck Leverd3978bb2010-12-24 01:33:04 +0000519/**
520 * nfs_client_return_marked_delegations - return previously marked delegations
521 * @clp: nfs_client to process
522 *
Trond Myklebustdc327ed2012-05-06 19:46:30 -0400523 * Note that this function is designed to be called by the state
524 * manager thread. For this reason, it cannot flush the dirty data,
525 * since that could deadlock in case of a state recovery error.
526 *
Chuck Leverd3978bb2010-12-24 01:33:04 +0000527 * Returns zero on success, or a negative errno value.
Trond Myklebust515d8612008-12-23 15:21:46 -0500528 */
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500529int nfs_client_return_marked_delegations(struct nfs_client *clp)
Trond Myklebust515d8612008-12-23 15:21:46 -0500530{
531 struct nfs_delegation *delegation;
NeilBrowne04bbf62018-04-30 14:31:30 +1000532 struct nfs_delegation *prev;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000533 struct nfs_server *server;
Trond Myklebust515d8612008-12-23 15:21:46 -0500534 struct inode *inode;
NeilBrowne04bbf62018-04-30 14:31:30 +1000535 struct inode *place_holder = NULL;
536 struct nfs_delegation *place_holder_deleg = NULL;
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500537 int err = 0;
Trond Myklebust515d8612008-12-23 15:21:46 -0500538
539restart:
NeilBrowne04bbf62018-04-30 14:31:30 +1000540 /*
541 * To avoid quadratic looping we hold a reference
542 * to an inode place_holder. Each time we restart, we
543 * list nfs_servers from the server of that inode, and
544 * delegation in the server from the delegations of that
545 * inode.
546 * prev is an RCU-protected pointer to a delegation which
547 * wasn't marked for return and might be a good choice for
548 * the next place_holder.
549 */
Trond Myklebust515d8612008-12-23 15:21:46 -0500550 rcu_read_lock();
NeilBrowne04bbf62018-04-30 14:31:30 +1000551 prev = NULL;
552 if (place_holder)
553 server = NFS_SERVER(place_holder);
554 else
555 server = list_entry_rcu(clp->cl_superblocks.next,
556 struct nfs_server, client_link);
557 list_for_each_entry_from_rcu(server, &clp->cl_superblocks, client_link) {
558 delegation = NULL;
559 if (place_holder && server == NFS_SERVER(place_holder))
560 delegation = rcu_dereference(NFS_I(place_holder)->delegation);
561 if (!delegation || delegation != place_holder_deleg)
562 delegation = list_entry_rcu(server->delegations.next,
563 struct nfs_delegation, super_list);
564 list_for_each_entry_from_rcu(delegation, &server->delegations, super_list) {
565 struct inode *to_put = NULL;
566
567 if (!nfs_delegation_need_return(delegation)) {
568 prev = delegation;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000569 continue;
NeilBrowne04bbf62018-04-30 14:31:30 +1000570 }
Trond Myklebust9f0f8e12015-02-26 09:57:34 -0500571 if (!nfs_sb_active(server->super))
NeilBrownf38934912018-05-31 15:23:22 +1000572 break; /* continue in outer loop */
NeilBrowne04bbf62018-04-30 14:31:30 +1000573
574 if (prev) {
575 struct inode *tmp;
576
577 tmp = nfs_delegation_grab_inode(prev);
578 if (tmp) {
579 to_put = place_holder;
580 place_holder = tmp;
581 place_holder_deleg = prev;
582 }
583 }
584
Trond Myklebust9f0f8e12015-02-26 09:57:34 -0500585 inode = nfs_delegation_grab_inode(delegation);
586 if (inode == NULL) {
587 rcu_read_unlock();
NeilBrowne04bbf62018-04-30 14:31:30 +1000588 if (to_put)
589 iput(to_put);
Trond Myklebust9f0f8e12015-02-26 09:57:34 -0500590 nfs_sb_deactive(server->super);
591 goto restart;
592 }
Trond Myklebustd25be542013-02-05 11:43:28 -0500593 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
Chuck Leverd3978bb2010-12-24 01:33:04 +0000594 rcu_read_unlock();
595
NeilBrowne04bbf62018-04-30 14:31:30 +1000596 if (to_put)
597 iput(to_put);
598
Trond Myklebustd25be542013-02-05 11:43:28 -0500599 err = nfs_end_delegation_return(inode, delegation, 0);
Chuck Leverd3978bb2010-12-24 01:33:04 +0000600 iput(inode);
Trond Myklebust9f0f8e12015-02-26 09:57:34 -0500601 nfs_sb_deactive(server->super);
NeilBrown3ca951b2018-04-30 14:31:30 +1000602 cond_resched();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000603 if (!err)
604 goto restart;
605 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
NeilBrowne04bbf62018-04-30 14:31:30 +1000606 if (place_holder)
607 iput(place_holder);
Chuck Leverd3978bb2010-12-24 01:33:04 +0000608 return err;
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500609 }
Trond Myklebust515d8612008-12-23 15:21:46 -0500610 }
611 rcu_read_unlock();
NeilBrowne04bbf62018-04-30 14:31:30 +1000612 if (place_holder)
613 iput(place_holder);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500614 return 0;
Trond Myklebust515d8612008-12-23 15:21:46 -0500615}
616
Chuck Leverd3978bb2010-12-24 01:33:04 +0000617/**
Trond Myklebustb47e0e42019-10-21 14:04:00 -0400618 * nfs_inode_evict_delegation - return delegation, don't reclaim opens
Chuck Leverd3978bb2010-12-24 01:33:04 +0000619 * @inode: inode to process
620 *
621 * Does not protect against delegation reclaims, therefore really only safe
Trond Myklebustb47e0e42019-10-21 14:04:00 -0400622 * to be called from nfs4_clear_inode(). Guaranteed to always free
623 * the delegation structure.
Trond Myklebuste6f81072008-01-24 18:14:34 -0500624 */
Trond Myklebustb47e0e42019-10-21 14:04:00 -0400625void nfs_inode_evict_delegation(struct inode *inode)
Trond Myklebuste6f81072008-01-24 18:14:34 -0500626{
Trond Myklebuste6f81072008-01-24 18:14:34 -0500627 struct nfs_delegation *delegation;
628
Trond Myklebustd25be542013-02-05 11:43:28 -0500629 delegation = nfs_inode_detach_delegation(inode);
Trond Myklebustb47e0e42019-10-21 14:04:00 -0400630 if (delegation != NULL) {
Trond Myklebustf885ea62020-01-27 09:58:15 -0500631 set_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
Trond Myklebustb47e0e42019-10-21 14:04:00 -0400632 set_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags);
Trond Myklebust5fcdfac2015-03-25 13:19:42 -0400633 nfs_do_return_delegation(inode, delegation, 1);
Trond Myklebustee05f452019-10-21 13:56:59 -0400634 nfs_free_delegation(delegation);
Trond Myklebustb47e0e42019-10-21 14:04:00 -0400635 }
Trond Myklebuste6f81072008-01-24 18:14:34 -0500636}
637
Chuck Leverd3978bb2010-12-24 01:33:04 +0000638/**
639 * nfs_inode_return_delegation - synchronously return a delegation
640 * @inode: inode to process
641 *
Trond Myklebustc57d1bc2012-05-06 19:34:17 -0400642 * This routine will always flush any dirty data to disk on the
643 * assumption that if we need to return the delegation, then
644 * we should stop caching.
645 *
Chuck Leverd3978bb2010-12-24 01:33:04 +0000646 * Returns zero on success, or a negative errno value.
647 */
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400648int nfs4_inode_return_delegation(struct inode *inode)
Trond Myklebust90163022007-07-05 14:55:18 -0400649{
Trond Myklebust90163022007-07-05 14:55:18 -0400650 struct nfs_inode *nfsi = NFS_I(inode);
651 struct nfs_delegation *delegation;
652 int err = 0;
653
Trond Myklebustc57d1bc2012-05-06 19:34:17 -0400654 nfs_wb_all(inode);
Trond Myklebustd25be542013-02-05 11:43:28 -0500655 delegation = nfs_start_delegation_return(nfsi);
656 if (delegation != NULL)
657 err = nfs_end_delegation_return(inode, delegation, 1);
Trond Myklebust90163022007-07-05 14:55:18 -0400658 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
Trond Myklebustc01d3642018-03-20 16:43:20 -0400661/**
Trond Myklebustb7b7dac2020-01-27 09:58:17 -0500662 * nfs_inode_return_delegation_on_close - asynchronously return a delegation
663 * @inode: inode to process
664 *
665 * This routine is called on file close in order to determine if the
666 * inode delegation needs to be returned immediately.
667 */
668void nfs4_inode_return_delegation_on_close(struct inode *inode)
669{
670 struct nfs_delegation *delegation;
671 struct nfs_delegation *ret = NULL;
672
673 if (!inode)
674 return;
675 rcu_read_lock();
676 delegation = nfs4_get_valid_delegation(inode);
677 if (!delegation)
678 goto out;
679 if (test_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags)) {
680 spin_lock(&delegation->lock);
681 if (delegation->inode &&
682 list_empty(&NFS_I(inode)->open_files) &&
683 !test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) {
684 clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
685 ret = delegation;
686 }
687 spin_unlock(&delegation->lock);
688 }
689out:
690 rcu_read_unlock();
691 nfs_end_delegation_return(inode, ret, 0);
692}
693
694/**
Trond Myklebustc01d3642018-03-20 16:43:20 -0400695 * nfs4_inode_make_writeable
696 * @inode: pointer to inode
697 *
698 * Make the inode writeable by returning the delegation if necessary
699 *
700 * Returns zero on success, or a negative errno value.
701 */
702int nfs4_inode_make_writeable(struct inode *inode)
703{
Trond Myklebust3887ce12019-10-27 13:48:18 -0400704 struct nfs_delegation *delegation;
705
706 rcu_read_lock();
707 delegation = nfs4_get_valid_delegation(inode);
708 if (delegation == NULL ||
709 (nfs4_has_session(NFS_SERVER(inode)->nfs_client) &&
710 (delegation->type & FMODE_WRITE))) {
711 rcu_read_unlock();
712 return 0;
713 }
714 rcu_read_unlock();
715 return nfs4_inode_return_delegation(inode);
Trond Myklebustc01d3642018-03-20 16:43:20 -0400716}
717
Trond Myklebustb7571442013-04-03 14:33:49 -0400718static void nfs_mark_return_if_closed_delegation(struct nfs_server *server,
719 struct nfs_delegation *delegation)
720{
721 set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
722 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
723}
724
Trond Myklebusted1e62112011-07-25 15:37:29 -0400725static void nfs_mark_return_delegation(struct nfs_server *server,
726 struct nfs_delegation *delegation)
Trond Myklebust6411bd42008-12-23 15:21:51 -0500727{
728 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
Trond Myklebusted1e62112011-07-25 15:37:29 -0400729 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
Trond Myklebust6411bd42008-12-23 15:21:51 -0500730}
731
Trond Myklebust5c31e232013-04-03 19:04:58 -0400732static bool nfs_server_mark_return_all_delegations(struct nfs_server *server)
733{
734 struct nfs_delegation *delegation;
735 bool ret = false;
736
737 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
738 nfs_mark_return_delegation(server, delegation);
739 ret = true;
740 }
741 return ret;
742}
743
Trond Myklebustb02ba0b2013-04-03 19:23:58 -0400744static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
745{
746 struct nfs_server *server;
747
748 rcu_read_lock();
749 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
750 nfs_server_mark_return_all_delegations(server);
751 rcu_read_unlock();
752}
753
754static void nfs_delegation_run_state_manager(struct nfs_client *clp)
755{
756 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
757 nfs4_schedule_state_manager(clp);
758}
759
760/**
761 * nfs_expire_all_delegations
762 * @clp: client to process
763 *
764 */
765void nfs_expire_all_delegations(struct nfs_client *clp)
766{
767 nfs_client_mark_return_all_delegations(clp);
768 nfs_delegation_run_state_manager(clp);
769}
770
Chuck Leverd3978bb2010-12-24 01:33:04 +0000771/**
772 * nfs_super_return_all_delegations - return delegations for one superblock
Trond Myklebust302fad72019-02-18 13:32:38 -0500773 * @server: pointer to nfs_server to process
Chuck Leverd3978bb2010-12-24 01:33:04 +0000774 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 */
Bryan Schumakereeebf912012-06-20 15:53:41 -0400776void nfs_server_return_all_delegations(struct nfs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000778 struct nfs_client *clp = server->nfs_client;
Trond Myklebust5c31e232013-04-03 19:04:58 -0400779 bool need_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 if (clp == NULL)
782 return;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000783
Trond Myklebust8383e462007-07-06 15:12:04 -0400784 rcu_read_lock();
Trond Myklebust5c31e232013-04-03 19:04:58 -0400785 need_wait = nfs_server_mark_return_all_delegations(server);
Trond Myklebust8383e462007-07-06 15:12:04 -0400786 rcu_read_unlock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000787
Trond Myklebust5c31e232013-04-03 19:04:58 -0400788 if (need_wait) {
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500789 nfs4_schedule_state_manager(clp);
Trond Myklebust5c31e232013-04-03 19:04:58 -0400790 nfs4_wait_clnt_recover(clp);
791 }
Trond Myklebust515d8612008-12-23 15:21:46 -0500792}
793
Trond Myklebust826e0012013-04-03 19:27:52 -0400794static void nfs_mark_return_unused_delegation_types(struct nfs_server *server,
Chuck Leverd3978bb2010-12-24 01:33:04 +0000795 fmode_t flags)
Trond Myklebust515d8612008-12-23 15:21:46 -0500796{
797 struct nfs_delegation *delegation;
798
Chuck Leverd3978bb2010-12-24 01:33:04 +0000799 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500800 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
801 continue;
802 if (delegation->type & flags)
Trond Myklebust826e0012013-04-03 19:27:52 -0400803 nfs_mark_return_if_closed_delegation(server, delegation);
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500804 }
Chuck Leverd3978bb2010-12-24 01:33:04 +0000805}
806
Trond Myklebust826e0012013-04-03 19:27:52 -0400807static void nfs_client_mark_return_unused_delegation_types(struct nfs_client *clp,
Chuck Leverd3978bb2010-12-24 01:33:04 +0000808 fmode_t flags)
809{
810 struct nfs_server *server;
811
812 rcu_read_lock();
813 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
Trond Myklebust826e0012013-04-03 19:27:52 -0400814 nfs_mark_return_unused_delegation_types(server, flags);
Trond Myklebust515d8612008-12-23 15:21:46 -0500815 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816}
817
Trond Myklebustee05f452019-10-21 13:56:59 -0400818static void nfs_revoke_delegation(struct inode *inode,
Trond Myklebust41020b62016-09-22 13:38:58 -0400819 const nfs4_stateid *stateid)
820{
821 struct nfs_delegation *delegation;
Trond Myklebust7f048832016-09-22 13:39:14 -0400822 nfs4_stateid tmp;
Trond Myklebust41020b62016-09-22 13:38:58 -0400823 bool ret = false;
824
825 rcu_read_lock();
826 delegation = rcu_dereference(NFS_I(inode)->delegation);
827 if (delegation == NULL)
828 goto out;
Trond Myklebust7f048832016-09-22 13:39:14 -0400829 if (stateid == NULL) {
830 nfs4_stateid_copy(&tmp, &delegation->stateid);
831 stateid = &tmp;
Trond Myklebustf2d47b52019-10-21 14:15:32 -0400832 } else {
833 if (!nfs4_stateid_match_other(stateid, &delegation->stateid))
834 goto out;
835 spin_lock(&delegation->lock);
836 if (stateid->seqid) {
837 if (nfs4_stateid_is_newer(&delegation->stateid, stateid)) {
838 spin_unlock(&delegation->lock);
839 goto out;
840 }
841 delegation->stateid.seqid = stateid->seqid;
842 }
843 spin_unlock(&delegation->lock);
844 }
Trond Myklebustd2269ea2020-01-27 09:58:18 -0500845 nfs_mark_delegation_revoked(delegation);
Trond Myklebust41020b62016-09-22 13:38:58 -0400846 ret = true;
847out:
848 rcu_read_unlock();
Trond Myklebust7f048832016-09-22 13:39:14 -0400849 if (ret)
850 nfs_inode_find_state_and_recover(inode, stateid);
Trond Myklebust41020b62016-09-22 13:38:58 -0400851}
852
853void nfs_remove_bad_delegation(struct inode *inode,
854 const nfs4_stateid *stateid)
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500855{
Trond Myklebustee05f452019-10-21 13:56:59 -0400856 nfs_revoke_delegation(inode, stateid);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500857}
Andy Adamson9cb81962012-03-07 10:49:41 -0500858EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500859
Trond Myklebustd51f91d2019-10-21 14:22:14 -0400860void nfs_delegation_mark_returned(struct inode *inode,
861 const nfs4_stateid *stateid)
862{
863 struct nfs_delegation *delegation;
864
865 if (!inode)
866 return;
867
868 rcu_read_lock();
869 delegation = rcu_dereference(NFS_I(inode)->delegation);
870 if (!delegation)
871 goto out_rcu_unlock;
872
873 spin_lock(&delegation->lock);
874 if (!nfs4_stateid_match_other(stateid, &delegation->stateid))
875 goto out_spin_unlock;
876 if (stateid->seqid) {
877 /* If delegation->stateid is newer, dont mark as returned */
878 if (nfs4_stateid_is_newer(&delegation->stateid, stateid))
879 goto out_clear_returning;
880 if (delegation->stateid.seqid != stateid->seqid)
881 delegation->stateid.seqid = stateid->seqid;
882 }
883
Trond Myklebustd2269ea2020-01-27 09:58:18 -0500884 nfs_mark_delegation_revoked(delegation);
Trond Myklebustd51f91d2019-10-21 14:22:14 -0400885
886out_clear_returning:
887 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
888out_spin_unlock:
889 spin_unlock(&delegation->lock);
890out_rcu_unlock:
891 rcu_read_unlock();
892
893 nfs_inode_find_state_and_recover(inode, stateid);
894}
895
Chuck Leverd3978bb2010-12-24 01:33:04 +0000896/**
Trond Myklebust826e0012013-04-03 19:27:52 -0400897 * nfs_expire_unused_delegation_types
Chuck Leverd3978bb2010-12-24 01:33:04 +0000898 * @clp: client to process
899 * @flags: delegation types to expire
900 *
901 */
Trond Myklebust826e0012013-04-03 19:27:52 -0400902void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags)
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500903{
Trond Myklebust826e0012013-04-03 19:27:52 -0400904 nfs_client_mark_return_unused_delegation_types(clp, flags);
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500905 nfs_delegation_run_state_manager(clp);
906}
907
Chuck Leverd3978bb2010-12-24 01:33:04 +0000908static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
Trond Myklebustb7391f42008-12-23 15:21:52 -0500909{
910 struct nfs_delegation *delegation;
911
Chuck Leverd3978bb2010-12-24 01:33:04 +0000912 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
Trond Myklebustb7391f42008-12-23 15:21:52 -0500913 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
914 continue;
Trond Myklebustb7571442013-04-03 14:33:49 -0400915 nfs_mark_return_if_closed_delegation(server, delegation);
Trond Myklebustb7391f42008-12-23 15:21:52 -0500916 }
Trond Myklebustb7391f42008-12-23 15:21:52 -0500917}
918
Chuck Leverd3978bb2010-12-24 01:33:04 +0000919/**
920 * nfs_expire_unreferenced_delegations - Eliminate unused delegations
921 * @clp: nfs_client to process
922 *
923 */
Trond Myklebustb7391f42008-12-23 15:21:52 -0500924void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
925{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000926 struct nfs_server *server;
927
928 rcu_read_lock();
929 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
930 nfs_mark_return_unreferenced_delegations(server);
931 rcu_read_unlock();
932
Trond Myklebustb7391f42008-12-23 15:21:52 -0500933 nfs_delegation_run_state_manager(clp);
934}
935
Chuck Leverd3978bb2010-12-24 01:33:04 +0000936/**
937 * nfs_async_inode_return_delegation - asynchronously return a delegation
938 * @inode: inode to process
Trond Myklebust8e663f02012-03-04 18:13:56 -0500939 * @stateid: state ID information
Chuck Leverd3978bb2010-12-24 01:33:04 +0000940 *
941 * Returns zero on success, or a negative errno value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 */
Chuck Leverd3978bb2010-12-24 01:33:04 +0000943int nfs_async_inode_return_delegation(struct inode *inode,
944 const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Trond Myklebusted1e62112011-07-25 15:37:29 -0400946 struct nfs_server *server = NFS_SERVER(inode);
947 struct nfs_client *clp = server->nfs_client;
Trond Myklebust6411bd42008-12-23 15:21:51 -0500948 struct nfs_delegation *delegation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949
Trond Myklebust6411bd42008-12-23 15:21:51 -0500950 rcu_read_lock();
Trond Myklebust457a5042019-10-22 08:46:06 -0400951 delegation = nfs4_get_valid_delegation(inode);
Trond Myklebust755a48a2014-03-02 22:03:12 -0500952 if (delegation == NULL)
953 goto out_enoent;
Trond Myklebust4816fda2015-09-20 14:58:42 -0400954 if (stateid != NULL &&
955 !clp->cl_mvops->match_stateid(&delegation->stateid, stateid))
Trond Myklebust755a48a2014-03-02 22:03:12 -0500956 goto out_enoent;
Trond Myklebusted1e62112011-07-25 15:37:29 -0400957 nfs_mark_return_delegation(server, delegation);
Trond Myklebust6411bd42008-12-23 15:21:51 -0500958 rcu_read_unlock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000959
Trond Myklebust6411bd42008-12-23 15:21:51 -0500960 nfs_delegation_run_state_manager(clp);
961 return 0;
Trond Myklebust755a48a2014-03-02 22:03:12 -0500962out_enoent:
963 rcu_read_unlock();
964 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965}
966
Chuck Leverd3978bb2010-12-24 01:33:04 +0000967static struct inode *
968nfs_delegation_find_inode_server(struct nfs_server *server,
969 const struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
971 struct nfs_delegation *delegation;
Trond Myklebuste39d8a12018-11-13 16:37:54 -0500972 struct inode *freeme, *res = NULL;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000973
974 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
Trond Myklebust86e89482008-12-23 15:21:39 -0500975 spin_lock(&delegation->lock);
976 if (delegation->inode != NULL &&
Trond Myklebust457a5042019-10-22 08:46:06 -0400977 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) &&
Trond Myklebust86e89482008-12-23 15:21:39 -0500978 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
Trond Myklebuste39d8a12018-11-13 16:37:54 -0500979 freeme = igrab(delegation->inode);
980 if (freeme && nfs_sb_active(freeme->i_sb))
981 res = freeme;
Trond Myklebust6c342652018-06-07 14:22:00 -0400982 spin_unlock(&delegation->lock);
983 if (res != NULL)
984 return res;
Trond Myklebuste39d8a12018-11-13 16:37:54 -0500985 if (freeme) {
986 rcu_read_unlock();
987 iput(freeme);
988 rcu_read_lock();
989 }
Trond Myklebust6c342652018-06-07 14:22:00 -0400990 return ERR_PTR(-EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 }
Trond Myklebust86e89482008-12-23 15:21:39 -0500992 spin_unlock(&delegation->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 }
Trond Myklebust6c342652018-06-07 14:22:00 -0400994 return ERR_PTR(-ENOENT);
Chuck Leverd3978bb2010-12-24 01:33:04 +0000995}
996
997/**
998 * nfs_delegation_find_inode - retrieve the inode associated with a delegation
999 * @clp: client state handle
1000 * @fhandle: filehandle from a delegation recall
1001 *
1002 * Returns pointer to inode matching "fhandle," or NULL if a matching inode
1003 * cannot be found.
1004 */
1005struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
1006 const struct nfs_fh *fhandle)
1007{
1008 struct nfs_server *server;
Trond Myklebust6c342652018-06-07 14:22:00 -04001009 struct inode *res;
Chuck Leverd3978bb2010-12-24 01:33:04 +00001010
1011 rcu_read_lock();
1012 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1013 res = nfs_delegation_find_inode_server(server, fhandle);
Anna Schumakerd5681f52018-06-14 09:39:17 -04001014 if (res != ERR_PTR(-ENOENT)) {
1015 rcu_read_unlock();
Trond Myklebust6c342652018-06-07 14:22:00 -04001016 return res;
Anna Schumakerd5681f52018-06-14 09:39:17 -04001017 }
Chuck Leverd3978bb2010-12-24 01:33:04 +00001018 }
Trond Myklebust8383e462007-07-06 15:12:04 -04001019 rcu_read_unlock();
Trond Myklebust6c342652018-06-07 14:22:00 -04001020 return ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021}
1022
Chuck Leverd3978bb2010-12-24 01:33:04 +00001023static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
1024{
1025 struct nfs_delegation *delegation;
1026
Trond Myklebust45870d62016-09-22 13:38:59 -04001027 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
1028 /*
1029 * If the delegation may have been admin revoked, then we
1030 * cannot reclaim it.
1031 */
1032 if (test_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags))
1033 continue;
Chuck Leverd3978bb2010-12-24 01:33:04 +00001034 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
Trond Myklebust45870d62016-09-22 13:38:59 -04001035 }
Chuck Leverd3978bb2010-12-24 01:33:04 +00001036}
1037
1038/**
1039 * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
1040 * @clp: nfs_client to process
1041 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 */
David Howellsadfa6f92006-08-22 20:06:08 -04001043void nfs_delegation_mark_reclaim(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
Chuck Leverd3978bb2010-12-24 01:33:04 +00001045 struct nfs_server *server;
1046
Trond Myklebust8383e462007-07-06 15:12:04 -04001047 rcu_read_lock();
Chuck Leverd3978bb2010-12-24 01:33:04 +00001048 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1049 nfs_delegation_mark_reclaim_server(server);
Trond Myklebust8383e462007-07-06 15:12:04 -04001050 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051}
1052
Chuck Leverd3978bb2010-12-24 01:33:04 +00001053/**
1054 * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
1055 * @clp: nfs_client to process
1056 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 */
David Howellsadfa6f92006-08-22 20:06:08 -04001058void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059{
Trond Myklebust8383e462007-07-06 15:12:04 -04001060 struct nfs_delegation *delegation;
Chuck Leverd3978bb2010-12-24 01:33:04 +00001061 struct nfs_server *server;
Trond Myklebust86e89482008-12-23 15:21:39 -05001062 struct inode *inode;
Chuck Leverd3978bb2010-12-24 01:33:04 +00001063
Trond Myklebust8383e462007-07-06 15:12:04 -04001064restart:
1065 rcu_read_lock();
Chuck Leverd3978bb2010-12-24 01:33:04 +00001066 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1067 list_for_each_entry_rcu(delegation, &server->delegations,
1068 super_list) {
Trond Myklebust6f9449b2019-02-21 14:51:25 -05001069 if (test_bit(NFS_DELEGATION_INODE_FREEING,
1070 &delegation->flags) ||
1071 test_bit(NFS_DELEGATION_RETURNING,
1072 &delegation->flags) ||
1073 test_bit(NFS_DELEGATION_NEED_RECLAIM,
Chuck Leverd3978bb2010-12-24 01:33:04 +00001074 &delegation->flags) == 0)
1075 continue;
Trond Myklebust9f0f8e12015-02-26 09:57:34 -05001076 if (!nfs_sb_active(server->super))
NeilBrownf38934912018-05-31 15:23:22 +10001077 break; /* continue in outer loop */
Trond Myklebust9f0f8e12015-02-26 09:57:34 -05001078 inode = nfs_delegation_grab_inode(delegation);
1079 if (inode == NULL) {
1080 rcu_read_unlock();
1081 nfs_sb_deactive(server->super);
1082 goto restart;
1083 }
Trond Myklebustb04b22f2015-02-26 13:59:38 -05001084 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
Chuck Leverd3978bb2010-12-24 01:33:04 +00001085 rcu_read_unlock();
Trond Myklebustb04b22f2015-02-26 13:59:38 -05001086 if (delegation != NULL) {
1087 delegation = nfs_detach_delegation(NFS_I(inode),
1088 delegation, server);
1089 if (delegation != NULL)
1090 nfs_free_delegation(delegation);
1091 }
Chuck Leverd3978bb2010-12-24 01:33:04 +00001092 iput(inode);
Trond Myklebust9f0f8e12015-02-26 09:57:34 -05001093 nfs_sb_deactive(server->super);
NeilBrown3ca951b2018-04-30 14:31:30 +10001094 cond_resched();
Chuck Leverd3978bb2010-12-24 01:33:04 +00001095 goto restart;
1096 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 }
Trond Myklebust8383e462007-07-06 15:12:04 -04001098 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099}
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001100
Trond Myklebustbb3d1a32016-09-22 13:39:00 -04001101static inline bool nfs4_server_rebooted(const struct nfs_client *clp)
1102{
1103 return (clp->cl_state & (BIT(NFS4CLNT_CHECK_LEASE) |
1104 BIT(NFS4CLNT_LEASE_EXPIRED) |
1105 BIT(NFS4CLNT_SESSION_RESET))) != 0;
1106}
1107
Trond Myklebust45870d62016-09-22 13:38:59 -04001108static void nfs_mark_test_expired_delegation(struct nfs_server *server,
1109 struct nfs_delegation *delegation)
1110{
Trond Myklebust059b43e2016-09-22 13:39:06 -04001111 if (delegation->stateid.type == NFS4_INVALID_STATEID_TYPE)
1112 return;
Trond Myklebust45870d62016-09-22 13:38:59 -04001113 clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
1114 set_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
1115 set_bit(NFS4CLNT_DELEGATION_EXPIRED, &server->nfs_client->cl_state);
1116}
1117
Trond Myklebustbb3d1a32016-09-22 13:39:00 -04001118static void nfs_inode_mark_test_expired_delegation(struct nfs_server *server,
1119 struct inode *inode)
1120{
1121 struct nfs_delegation *delegation;
1122
1123 rcu_read_lock();
1124 delegation = rcu_dereference(NFS_I(inode)->delegation);
1125 if (delegation)
1126 nfs_mark_test_expired_delegation(server, delegation);
1127 rcu_read_unlock();
1128
1129}
1130
Trond Myklebust45870d62016-09-22 13:38:59 -04001131static void nfs_delegation_mark_test_expired_server(struct nfs_server *server)
1132{
1133 struct nfs_delegation *delegation;
1134
1135 list_for_each_entry_rcu(delegation, &server->delegations, super_list)
1136 nfs_mark_test_expired_delegation(server, delegation);
1137}
1138
1139/**
1140 * nfs_mark_test_expired_all_delegations - mark all delegations for testing
1141 * @clp: nfs_client to process
1142 *
1143 * Iterates through all the delegations associated with this server and
1144 * marks them as needing to be checked for validity.
1145 */
1146void nfs_mark_test_expired_all_delegations(struct nfs_client *clp)
1147{
1148 struct nfs_server *server;
1149
1150 rcu_read_lock();
1151 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1152 nfs_delegation_mark_test_expired_server(server);
1153 rcu_read_unlock();
1154}
1155
1156/**
Scott Mayhew8ca017c2019-05-06 11:59:05 -04001157 * nfs_test_expired_all_delegations - test all delegations for a client
1158 * @clp: nfs_client to process
1159 *
1160 * Helper for handling "recallable state revoked" status from server.
1161 */
1162void nfs_test_expired_all_delegations(struct nfs_client *clp)
1163{
1164 nfs_mark_test_expired_all_delegations(clp);
1165 nfs4_schedule_state_manager(clp);
1166}
1167
Trond Myklebustad114082019-07-26 14:40:53 +01001168static void
1169nfs_delegation_test_free_expired(struct inode *inode,
1170 nfs4_stateid *stateid,
1171 const struct cred *cred)
1172{
1173 struct nfs_server *server = NFS_SERVER(inode);
1174 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
1175 int status;
1176
1177 if (!cred)
1178 return;
1179 status = ops->test_and_free_expired(server, stateid, cred);
1180 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
1181 nfs_remove_bad_delegation(inode, stateid);
1182}
1183
Scott Mayhew8ca017c2019-05-06 11:59:05 -04001184/**
Trond Myklebust45870d62016-09-22 13:38:59 -04001185 * nfs_reap_expired_delegations - reap expired delegations
1186 * @clp: nfs_client to process
1187 *
1188 * Iterates through all the delegations associated with this server and
1189 * checks if they have may have been revoked. This function is usually
1190 * expected to be called in cases where the server may have lost its
1191 * lease.
1192 */
1193void nfs_reap_expired_delegations(struct nfs_client *clp)
1194{
Trond Myklebust45870d62016-09-22 13:38:59 -04001195 struct nfs_delegation *delegation;
1196 struct nfs_server *server;
1197 struct inode *inode;
NeilBrowna52458b2018-12-03 11:30:31 +11001198 const struct cred *cred;
Trond Myklebust45870d62016-09-22 13:38:59 -04001199 nfs4_stateid stateid;
1200
1201restart:
1202 rcu_read_lock();
1203 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1204 list_for_each_entry_rcu(delegation, &server->delegations,
1205 super_list) {
Trond Myklebust6f9449b2019-02-21 14:51:25 -05001206 if (test_bit(NFS_DELEGATION_INODE_FREEING,
1207 &delegation->flags) ||
1208 test_bit(NFS_DELEGATION_RETURNING,
1209 &delegation->flags) ||
1210 test_bit(NFS_DELEGATION_TEST_EXPIRED,
Trond Myklebust45870d62016-09-22 13:38:59 -04001211 &delegation->flags) == 0)
1212 continue;
1213 if (!nfs_sb_active(server->super))
NeilBrownf38934912018-05-31 15:23:22 +10001214 break; /* continue in outer loop */
Trond Myklebust45870d62016-09-22 13:38:59 -04001215 inode = nfs_delegation_grab_inode(delegation);
1216 if (inode == NULL) {
1217 rcu_read_unlock();
1218 nfs_sb_deactive(server->super);
1219 goto restart;
1220 }
NeilBrowna52458b2018-12-03 11:30:31 +11001221 cred = get_cred_rcu(delegation->cred);
Trond Myklebust45870d62016-09-22 13:38:59 -04001222 nfs4_stateid_copy(&stateid, &delegation->stateid);
1223 clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
1224 rcu_read_unlock();
Trond Myklebustad114082019-07-26 14:40:53 +01001225 nfs_delegation_test_free_expired(inode, &stateid, cred);
NeilBrowna52458b2018-12-03 11:30:31 +11001226 put_cred(cred);
Trond Myklebustbb3d1a32016-09-22 13:39:00 -04001227 if (nfs4_server_rebooted(clp)) {
1228 nfs_inode_mark_test_expired_delegation(server,inode);
1229 iput(inode);
1230 nfs_sb_deactive(server->super);
1231 return;
1232 }
Trond Myklebust45870d62016-09-22 13:38:59 -04001233 iput(inode);
1234 nfs_sb_deactive(server->super);
NeilBrown3ca951b2018-04-30 14:31:30 +10001235 cond_resched();
Trond Myklebust45870d62016-09-22 13:38:59 -04001236 goto restart;
1237 }
1238 }
1239 rcu_read_unlock();
1240}
1241
Trond Myklebust6c2d8f82016-09-22 13:39:07 -04001242void nfs_inode_find_delegation_state_and_recover(struct inode *inode,
1243 const nfs4_stateid *stateid)
1244{
1245 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1246 struct nfs_delegation *delegation;
1247 bool found = false;
1248
1249 rcu_read_lock();
1250 delegation = rcu_dereference(NFS_I(inode)->delegation);
1251 if (delegation &&
Trond Myklebust42c304c2019-10-26 10:16:15 -04001252 nfs4_stateid_match_or_older(&delegation->stateid, stateid) &&
1253 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
Trond Myklebust6c2d8f82016-09-22 13:39:07 -04001254 nfs_mark_test_expired_delegation(NFS_SERVER(inode), delegation);
1255 found = true;
1256 }
1257 rcu_read_unlock();
1258 if (found)
1259 nfs4_schedule_state_manager(clp);
1260}
1261
Chuck Leverd3978bb2010-12-24 01:33:04 +00001262/**
1263 * nfs_delegations_present - check for existence of delegations
1264 * @clp: client state handle
1265 *
1266 * Returns one if there are any nfs_delegation structures attached
1267 * to this nfs_client.
1268 */
1269int nfs_delegations_present(struct nfs_client *clp)
1270{
1271 struct nfs_server *server;
1272 int ret = 0;
1273
1274 rcu_read_lock();
1275 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1276 if (!list_empty(&server->delegations)) {
1277 ret = 1;
1278 break;
1279 }
1280 rcu_read_unlock();
1281 return ret;
1282}
1283
1284/**
Trond Myklebust12f275c2017-11-06 15:28:05 -05001285 * nfs4_refresh_delegation_stateid - Update delegation stateid seqid
1286 * @dst: stateid to refresh
1287 * @inode: inode to check
1288 *
1289 * Returns "true" and updates "dst->seqid" * if inode had a delegation
1290 * that matches our delegation stateid. Otherwise "false" is returned.
1291 */
1292bool nfs4_refresh_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
1293{
1294 struct nfs_delegation *delegation;
1295 bool ret = false;
1296 if (!inode)
1297 goto out;
1298
1299 rcu_read_lock();
1300 delegation = rcu_dereference(NFS_I(inode)->delegation);
1301 if (delegation != NULL &&
Trond Myklebustb5756202019-10-22 13:34:06 -04001302 nfs4_stateid_match_other(dst, &delegation->stateid) &&
Trond Myklebust246afc0a2019-10-24 18:00:35 -04001303 nfs4_stateid_is_newer(&delegation->stateid, dst) &&
Trond Myklebustb5756202019-10-22 13:34:06 -04001304 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
Trond Myklebust12f275c2017-11-06 15:28:05 -05001305 dst->seqid = delegation->stateid.seqid;
Trond Myklebust79cc5542019-10-31 18:40:33 -04001306 ret = true;
Trond Myklebust12f275c2017-11-06 15:28:05 -05001307 }
1308 rcu_read_unlock();
1309out:
1310 return ret;
1311}
1312
1313/**
Chuck Leverd3978bb2010-12-24 01:33:04 +00001314 * nfs4_copy_delegation_stateid - Copy inode's state ID information
Chuck Leverd3978bb2010-12-24 01:33:04 +00001315 * @inode: inode to check
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001316 * @flags: delegation type requirement
Trond Myklebustabf4e132016-05-16 17:42:44 -04001317 * @dst: stateid data structure to fill in
1318 * @cred: optional argument to retrieve credential
Chuck Leverd3978bb2010-12-24 01:33:04 +00001319 *
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001320 * Returns "true" and fills in "dst->data" * if inode had a delegation,
1321 * otherwise "false" is returned.
Chuck Leverd3978bb2010-12-24 01:33:04 +00001322 */
Trond Myklebustabf4e132016-05-16 17:42:44 -04001323bool nfs4_copy_delegation_stateid(struct inode *inode, fmode_t flags,
NeilBrowna52458b2018-12-03 11:30:31 +11001324 nfs4_stateid *dst, const struct cred **cred)
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001325{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001326 struct nfs_inode *nfsi = NFS_I(inode);
1327 struct nfs_delegation *delegation;
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001328 bool ret;
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001329
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001330 flags &= FMODE_READ|FMODE_WRITE;
Trond Myklebust8383e462007-07-06 15:12:04 -04001331 rcu_read_lock();
1332 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebustaa05c872016-09-22 13:38:54 -04001333 ret = nfs4_is_valid_delegation(delegation, flags);
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001334 if (ret) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001335 nfs4_stateid_copy(dst, &delegation->stateid);
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001336 nfs_mark_delegation_referenced(delegation);
Trond Myklebustabf4e132016-05-16 17:42:44 -04001337 if (cred)
NeilBrowna52458b2018-12-03 11:30:31 +11001338 *cred = get_cred(delegation->cred);
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001339 }
Trond Myklebust8383e462007-07-06 15:12:04 -04001340 rcu_read_unlock();
1341 return ret;
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001342}
Trond Myklebust5445b1f2015-09-05 19:06:58 -04001343
1344/**
1345 * nfs4_delegation_flush_on_close - Check if we must flush file on close
1346 * @inode: inode to check
1347 *
1348 * This function checks the number of outstanding writes to the file
1349 * against the delegation 'space_limit' field to see if
1350 * the spec requires us to flush the file on close.
1351 */
1352bool nfs4_delegation_flush_on_close(const struct inode *inode)
1353{
1354 struct nfs_inode *nfsi = NFS_I(inode);
1355 struct nfs_delegation *delegation;
1356 bool ret = true;
1357
1358 rcu_read_lock();
1359 delegation = rcu_dereference(nfsi->delegation);
1360 if (delegation == NULL || !(delegation->type & FMODE_WRITE))
1361 goto out;
Trond Myklebusta6b6d5b2017-08-01 15:39:46 -04001362 if (atomic_long_read(&nfsi->nrequests) < delegation->pagemod_limit)
Trond Myklebust5445b1f2015-09-05 19:06:58 -04001363 ret = false;
1364out:
1365 rcu_read_unlock();
1366 return ret;
1367}