blob: af549d70ec507e3f8deb920c3a93509bd59a5ed2 [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);
202 if (delegation->inode != NULL) {
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);
301 set_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500302 list_del_rcu(&delegation->super_list);
Trond Myklebust86e89482008-12-23 15:21:39 -0500303 delegation->inode = NULL;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500304 rcu_assign_pointer(nfsi->delegation, NULL);
Trond Myklebust34310432008-12-23 15:21:38 -0500305 spin_unlock(&delegation->lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500306 return delegation;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500307}
308
Chuck Leverdda4b222010-12-24 01:32:54 +0000309static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi,
Trond Myklebustd25be542013-02-05 11:43:28 -0500310 struct nfs_delegation *delegation,
311 struct nfs_server *server)
Chuck Leverdda4b222010-12-24 01:32:54 +0000312{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000313 struct nfs_client *clp = server->nfs_client;
Chuck Leverdda4b222010-12-24 01:32:54 +0000314
315 spin_lock(&clp->cl_lock);
Trond Myklebustd25be542013-02-05 11:43:28 -0500316 delegation = nfs_detach_delegation_locked(nfsi, delegation, clp);
Chuck Leverdda4b222010-12-24 01:32:54 +0000317 spin_unlock(&clp->cl_lock);
318 return delegation;
319}
320
Trond Myklebustd25be542013-02-05 11:43:28 -0500321static struct nfs_delegation *
322nfs_inode_detach_delegation(struct inode *inode)
323{
324 struct nfs_inode *nfsi = NFS_I(inode);
325 struct nfs_server *server = NFS_SERVER(inode);
326 struct nfs_delegation *delegation;
327
328 delegation = nfs_start_delegation_return(nfsi);
329 if (delegation == NULL)
330 return NULL;
331 return nfs_detach_delegation(nfsi, delegation, server);
332}
333
Trond Myklebustcf6726e2014-12-19 11:22:28 -0500334static void
335nfs_update_inplace_delegation(struct nfs_delegation *delegation,
336 const struct nfs_delegation *update)
337{
338 if (nfs4_stateid_is_newer(&update->stateid, &delegation->stateid)) {
339 delegation->stateid.seqid = update->stateid.seqid;
340 smp_wmb();
341 delegation->type = update->type;
342 }
343}
344
Chuck Leverd3978bb2010-12-24 01:33:04 +0000345/**
346 * nfs_inode_set_delegation - set up a delegation on an inode
347 * @inode: inode to which delegation applies
348 * @cred: cred to use for subsequent delegation processing
Trond Myklebust35156bf2018-03-20 17:03:13 -0400349 * @type: delegation type
350 * @stateid: delegation stateid
351 * @pagemod_limit: write delegation "space_limit"
Chuck Leverd3978bb2010-12-24 01:33:04 +0000352 *
353 * Returns zero on success, or a negative errno value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 */
NeilBrowna52458b2018-12-03 11:30:31 +1100355int nfs_inode_set_delegation(struct inode *inode, const struct cred *cred,
Trond Myklebust35156bf2018-03-20 17:03:13 -0400356 fmode_t type,
357 const nfs4_stateid *stateid,
358 unsigned long pagemod_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000360 struct nfs_server *server = NFS_SERVER(inode);
361 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 struct nfs_inode *nfsi = NFS_I(inode);
David Howells17d2c0a2010-05-01 12:37:18 -0400363 struct nfs_delegation *delegation, *old_delegation;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500364 struct nfs_delegation *freeme = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 int status = 0;
366
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400367 delegation = kmalloc(sizeof(*delegation), GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if (delegation == NULL)
369 return -ENOMEM;
Trond Myklebust35156bf2018-03-20 17:03:13 -0400370 nfs4_stateid_copy(&delegation->stateid, stateid);
371 delegation->type = type;
372 delegation->pagemod_limit = pagemod_limit;
Jeff Layton1eb5d982018-01-09 08:21:17 -0500373 delegation->change_attr = inode_peek_iversion_raw(inode);
NeilBrowna52458b2018-12-03 11:30:31 +1100374 delegation->cred = get_cred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 delegation->inode = inode;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500376 delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
Trond Myklebust34310432008-12-23 15:21:38 -0500377 spin_lock_init(&delegation->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 spin_lock(&clp->cl_lock);
David Howells17d2c0a2010-05-01 12:37:18 -0400380 old_delegation = rcu_dereference_protected(nfsi->delegation,
Chuck Leverd3978bb2010-12-24 01:33:04 +0000381 lockdep_is_held(&clp->cl_lock));
David Howells17d2c0a2010-05-01 12:37:18 -0400382 if (old_delegation != NULL) {
Trond Myklebustcf6726e2014-12-19 11:22:28 -0500383 /* Is this an update of the existing delegation? */
384 if (nfs4_stateid_match_other(&old_delegation->stateid,
385 &delegation->stateid)) {
386 nfs_update_inplace_delegation(old_delegation,
387 delegation);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500388 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 }
Trond Myklebust57bfa892008-01-25 16:38:18 -0500390 /*
391 * Deal with broken servers that hand out two
392 * delegations for the same file.
Trond Myklebust17280172012-03-11 13:11:00 -0400393 * Allow for upgrades to a WRITE delegation, but
394 * nothing else.
Trond Myklebust57bfa892008-01-25 16:38:18 -0500395 */
396 dfprintk(FILE, "%s: server %s handed out "
397 "a duplicate delegation!\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700398 __func__, clp->cl_hostname);
Trond Myklebust17280172012-03-11 13:11:00 -0400399 if (delegation->type == old_delegation->type ||
400 !(delegation->type & FMODE_WRITE)) {
Trond Myklebust57bfa892008-01-25 16:38:18 -0500401 freeme = delegation;
402 delegation = NULL;
403 goto out;
404 }
Trond Myklebustade04642015-02-27 14:25:50 -0500405 if (test_and_set_bit(NFS_DELEGATION_RETURNING,
406 &old_delegation->flags))
407 goto out;
408 freeme = nfs_detach_delegation_locked(nfsi,
Trond Myklebustd25be542013-02-05 11:43:28 -0500409 old_delegation, clp);
410 if (freeme == NULL)
411 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 }
Trond Myklebust38942ba2015-03-04 15:59:05 -0500413 list_add_tail_rcu(&delegation->super_list, &server->delegations);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500414 rcu_assign_pointer(nfsi->delegation, delegation);
415 delegation = NULL;
Trond Myklebust412c77c2007-07-03 16:10:55 -0400416
Trond Myklebust35156bf2018-03-20 17:03:13 -0400417 trace_nfs4_set_delegation(inode, type);
Trond Myklebust412c77c2007-07-03 16:10:55 -0400418
Trond Myklebust97c2c172018-04-07 18:43:17 -0400419 spin_lock(&inode->i_lock);
420 if (NFS_I(inode)->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ATIME))
421 NFS_I(inode)->cache_validity |= NFS_INO_REVAL_FORCED;
422 spin_unlock(&inode->i_lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500423out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 spin_unlock(&clp->cl_lock);
Trond Myklebust603c83d2007-10-18 19:59:20 -0400425 if (delegation != NULL)
426 nfs_free_delegation(delegation);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500427 if (freeme != NULL)
428 nfs_do_return_delegation(inode, freeme, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 return status;
430}
431
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432/*
433 * Basic procedure for returning a delegation to the server
434 */
Trond Myklebustd25be542013-02-05 11:43:28 -0500435static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436{
Trond Myklebustd25be542013-02-05 11:43:28 -0500437 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust869f9df2014-11-10 18:43:56 -0500439 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Trond Myklebustd25be542013-02-05 11:43:28 -0500441 if (delegation == NULL)
442 return 0;
443 do {
Trond Myklebust869f9df2014-11-10 18:43:56 -0500444 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
445 break;
Trond Myklebust24311f82015-09-20 10:50:17 -0400446 err = nfs_delegation_claim_opens(inode, &delegation->stateid,
447 delegation->type);
Trond Myklebustd25be542013-02-05 11:43:28 -0500448 if (!issync || err != -EAGAIN)
449 break;
450 /*
451 * Guard against state recovery
452 */
453 err = nfs4_wait_clnt_recover(clp);
454 } while (err == 0);
455
456 if (err) {
457 nfs_abort_delegation_return(delegation, clp);
458 goto out;
459 }
460 if (!nfs_detach_delegation(nfsi, delegation, NFS_SERVER(inode)))
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500461 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500463 err = nfs_do_return_delegation(inode, delegation, issync);
464out:
465 return err;
Trond Myklebust90163022007-07-05 14:55:18 -0400466}
467
Trond Myklebustb7571442013-04-03 14:33:49 -0400468static bool nfs_delegation_need_return(struct nfs_delegation *delegation)
469{
470 bool ret = false;
471
Trond Myklebustec3ca4e52015-02-26 14:05:05 -0500472 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
473 goto out;
Trond Myklebustb7571442013-04-03 14:33:49 -0400474 if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
475 ret = true;
476 if (test_and_clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags) && !ret) {
477 struct inode *inode;
478
479 spin_lock(&delegation->lock);
480 inode = delegation->inode;
481 if (inode && list_empty(&NFS_I(inode)->open_files))
482 ret = true;
483 spin_unlock(&delegation->lock);
484 }
Trond Myklebustec3ca4e52015-02-26 14:05:05 -0500485out:
Trond Myklebustb7571442013-04-03 14:33:49 -0400486 return ret;
487}
488
Chuck Leverd3978bb2010-12-24 01:33:04 +0000489/**
490 * nfs_client_return_marked_delegations - return previously marked delegations
491 * @clp: nfs_client to process
492 *
Trond Myklebustdc327ed2012-05-06 19:46:30 -0400493 * Note that this function is designed to be called by the state
494 * manager thread. For this reason, it cannot flush the dirty data,
495 * since that could deadlock in case of a state recovery error.
496 *
Chuck Leverd3978bb2010-12-24 01:33:04 +0000497 * Returns zero on success, or a negative errno value.
Trond Myklebust515d8612008-12-23 15:21:46 -0500498 */
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500499int nfs_client_return_marked_delegations(struct nfs_client *clp)
Trond Myklebust515d8612008-12-23 15:21:46 -0500500{
501 struct nfs_delegation *delegation;
NeilBrowne04bbf62018-04-30 14:31:30 +1000502 struct nfs_delegation *prev;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000503 struct nfs_server *server;
Trond Myklebust515d8612008-12-23 15:21:46 -0500504 struct inode *inode;
NeilBrowne04bbf62018-04-30 14:31:30 +1000505 struct inode *place_holder = NULL;
506 struct nfs_delegation *place_holder_deleg = NULL;
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500507 int err = 0;
Trond Myklebust515d8612008-12-23 15:21:46 -0500508
509restart:
NeilBrowne04bbf62018-04-30 14:31:30 +1000510 /*
511 * To avoid quadratic looping we hold a reference
512 * to an inode place_holder. Each time we restart, we
513 * list nfs_servers from the server of that inode, and
514 * delegation in the server from the delegations of that
515 * inode.
516 * prev is an RCU-protected pointer to a delegation which
517 * wasn't marked for return and might be a good choice for
518 * the next place_holder.
519 */
Trond Myklebust515d8612008-12-23 15:21:46 -0500520 rcu_read_lock();
NeilBrowne04bbf62018-04-30 14:31:30 +1000521 prev = NULL;
522 if (place_holder)
523 server = NFS_SERVER(place_holder);
524 else
525 server = list_entry_rcu(clp->cl_superblocks.next,
526 struct nfs_server, client_link);
527 list_for_each_entry_from_rcu(server, &clp->cl_superblocks, client_link) {
528 delegation = NULL;
529 if (place_holder && server == NFS_SERVER(place_holder))
530 delegation = rcu_dereference(NFS_I(place_holder)->delegation);
531 if (!delegation || delegation != place_holder_deleg)
532 delegation = list_entry_rcu(server->delegations.next,
533 struct nfs_delegation, super_list);
534 list_for_each_entry_from_rcu(delegation, &server->delegations, super_list) {
535 struct inode *to_put = NULL;
536
537 if (!nfs_delegation_need_return(delegation)) {
538 prev = delegation;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000539 continue;
NeilBrowne04bbf62018-04-30 14:31:30 +1000540 }
Trond Myklebust9f0f8e12015-02-26 09:57:34 -0500541 if (!nfs_sb_active(server->super))
NeilBrownf38934912018-05-31 15:23:22 +1000542 break; /* continue in outer loop */
NeilBrowne04bbf62018-04-30 14:31:30 +1000543
544 if (prev) {
545 struct inode *tmp;
546
547 tmp = nfs_delegation_grab_inode(prev);
548 if (tmp) {
549 to_put = place_holder;
550 place_holder = tmp;
551 place_holder_deleg = prev;
552 }
553 }
554
Trond Myklebust9f0f8e12015-02-26 09:57:34 -0500555 inode = nfs_delegation_grab_inode(delegation);
556 if (inode == NULL) {
557 rcu_read_unlock();
NeilBrowne04bbf62018-04-30 14:31:30 +1000558 if (to_put)
559 iput(to_put);
Trond Myklebust9f0f8e12015-02-26 09:57:34 -0500560 nfs_sb_deactive(server->super);
561 goto restart;
562 }
Trond Myklebustd25be542013-02-05 11:43:28 -0500563 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
Chuck Leverd3978bb2010-12-24 01:33:04 +0000564 rcu_read_unlock();
565
NeilBrowne04bbf62018-04-30 14:31:30 +1000566 if (to_put)
567 iput(to_put);
568
Trond Myklebustd25be542013-02-05 11:43:28 -0500569 err = nfs_end_delegation_return(inode, delegation, 0);
Chuck Leverd3978bb2010-12-24 01:33:04 +0000570 iput(inode);
Trond Myklebust9f0f8e12015-02-26 09:57:34 -0500571 nfs_sb_deactive(server->super);
NeilBrown3ca951b2018-04-30 14:31:30 +1000572 cond_resched();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000573 if (!err)
574 goto restart;
575 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
NeilBrowne04bbf62018-04-30 14:31:30 +1000576 if (place_holder)
577 iput(place_holder);
Chuck Leverd3978bb2010-12-24 01:33:04 +0000578 return err;
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500579 }
Trond Myklebust515d8612008-12-23 15:21:46 -0500580 }
581 rcu_read_unlock();
NeilBrowne04bbf62018-04-30 14:31:30 +1000582 if (place_holder)
583 iput(place_holder);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500584 return 0;
Trond Myklebust515d8612008-12-23 15:21:46 -0500585}
586
Chuck Leverd3978bb2010-12-24 01:33:04 +0000587/**
588 * nfs_inode_return_delegation_noreclaim - return delegation, don't reclaim opens
589 * @inode: inode to process
590 *
591 * Does not protect against delegation reclaims, therefore really only safe
592 * to be called from nfs4_clear_inode().
Trond Myklebuste6f81072008-01-24 18:14:34 -0500593 */
594void nfs_inode_return_delegation_noreclaim(struct inode *inode)
595{
Trond Myklebuste6f81072008-01-24 18:14:34 -0500596 struct nfs_delegation *delegation;
597
Trond Myklebustd25be542013-02-05 11:43:28 -0500598 delegation = nfs_inode_detach_delegation(inode);
599 if (delegation != NULL)
Trond Myklebust5fcdfac2015-03-25 13:19:42 -0400600 nfs_do_return_delegation(inode, delegation, 1);
Trond Myklebuste6f81072008-01-24 18:14:34 -0500601}
602
Chuck Leverd3978bb2010-12-24 01:33:04 +0000603/**
604 * nfs_inode_return_delegation - synchronously return a delegation
605 * @inode: inode to process
606 *
Trond Myklebustc57d1bc2012-05-06 19:34:17 -0400607 * This routine will always flush any dirty data to disk on the
608 * assumption that if we need to return the delegation, then
609 * we should stop caching.
610 *
Chuck Leverd3978bb2010-12-24 01:33:04 +0000611 * Returns zero on success, or a negative errno value.
612 */
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400613int nfs4_inode_return_delegation(struct inode *inode)
Trond Myklebust90163022007-07-05 14:55:18 -0400614{
Trond Myklebust90163022007-07-05 14:55:18 -0400615 struct nfs_inode *nfsi = NFS_I(inode);
616 struct nfs_delegation *delegation;
617 int err = 0;
618
Trond Myklebustc57d1bc2012-05-06 19:34:17 -0400619 nfs_wb_all(inode);
Trond Myklebustd25be542013-02-05 11:43:28 -0500620 delegation = nfs_start_delegation_return(nfsi);
621 if (delegation != NULL)
622 err = nfs_end_delegation_return(inode, delegation, 1);
Trond Myklebust90163022007-07-05 14:55:18 -0400623 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
625
Trond Myklebustc01d3642018-03-20 16:43:20 -0400626/**
627 * nfs4_inode_make_writeable
628 * @inode: pointer to inode
629 *
630 * Make the inode writeable by returning the delegation if necessary
631 *
632 * Returns zero on success, or a negative errno value.
633 */
634int nfs4_inode_make_writeable(struct inode *inode)
635{
636 if (!nfs4_has_session(NFS_SERVER(inode)->nfs_client) ||
637 !nfs4_check_delegation(inode, FMODE_WRITE))
638 return nfs4_inode_return_delegation(inode);
639 return 0;
640}
641
Trond Myklebustb7571442013-04-03 14:33:49 -0400642static void nfs_mark_return_if_closed_delegation(struct nfs_server *server,
643 struct nfs_delegation *delegation)
644{
645 set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
646 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
647}
648
Trond Myklebusted1e62112011-07-25 15:37:29 -0400649static void nfs_mark_return_delegation(struct nfs_server *server,
650 struct nfs_delegation *delegation)
Trond Myklebust6411bd42008-12-23 15:21:51 -0500651{
652 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
Trond Myklebusted1e62112011-07-25 15:37:29 -0400653 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
Trond Myklebust6411bd42008-12-23 15:21:51 -0500654}
655
Trond Myklebust5c31e232013-04-03 19:04:58 -0400656static bool nfs_server_mark_return_all_delegations(struct nfs_server *server)
657{
658 struct nfs_delegation *delegation;
659 bool ret = false;
660
661 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
662 nfs_mark_return_delegation(server, delegation);
663 ret = true;
664 }
665 return ret;
666}
667
Trond Myklebustb02ba0b2013-04-03 19:23:58 -0400668static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
669{
670 struct nfs_server *server;
671
672 rcu_read_lock();
673 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
674 nfs_server_mark_return_all_delegations(server);
675 rcu_read_unlock();
676}
677
678static void nfs_delegation_run_state_manager(struct nfs_client *clp)
679{
680 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
681 nfs4_schedule_state_manager(clp);
682}
683
684/**
685 * nfs_expire_all_delegations
686 * @clp: client to process
687 *
688 */
689void nfs_expire_all_delegations(struct nfs_client *clp)
690{
691 nfs_client_mark_return_all_delegations(clp);
692 nfs_delegation_run_state_manager(clp);
693}
694
Chuck Leverd3978bb2010-12-24 01:33:04 +0000695/**
696 * nfs_super_return_all_delegations - return delegations for one superblock
Trond Myklebust302fad72019-02-18 13:32:38 -0500697 * @server: pointer to nfs_server to process
Chuck Leverd3978bb2010-12-24 01:33:04 +0000698 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 */
Bryan Schumakereeebf912012-06-20 15:53:41 -0400700void nfs_server_return_all_delegations(struct nfs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000702 struct nfs_client *clp = server->nfs_client;
Trond Myklebust5c31e232013-04-03 19:04:58 -0400703 bool need_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 if (clp == NULL)
706 return;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000707
Trond Myklebust8383e462007-07-06 15:12:04 -0400708 rcu_read_lock();
Trond Myklebust5c31e232013-04-03 19:04:58 -0400709 need_wait = nfs_server_mark_return_all_delegations(server);
Trond Myklebust8383e462007-07-06 15:12:04 -0400710 rcu_read_unlock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000711
Trond Myklebust5c31e232013-04-03 19:04:58 -0400712 if (need_wait) {
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500713 nfs4_schedule_state_manager(clp);
Trond Myklebust5c31e232013-04-03 19:04:58 -0400714 nfs4_wait_clnt_recover(clp);
715 }
Trond Myklebust515d8612008-12-23 15:21:46 -0500716}
717
Trond Myklebust826e0012013-04-03 19:27:52 -0400718static void nfs_mark_return_unused_delegation_types(struct nfs_server *server,
Chuck Leverd3978bb2010-12-24 01:33:04 +0000719 fmode_t flags)
Trond Myklebust515d8612008-12-23 15:21:46 -0500720{
721 struct nfs_delegation *delegation;
722
Chuck Leverd3978bb2010-12-24 01:33:04 +0000723 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500724 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
725 continue;
726 if (delegation->type & flags)
Trond Myklebust826e0012013-04-03 19:27:52 -0400727 nfs_mark_return_if_closed_delegation(server, delegation);
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500728 }
Chuck Leverd3978bb2010-12-24 01:33:04 +0000729}
730
Trond Myklebust826e0012013-04-03 19:27:52 -0400731static void nfs_client_mark_return_unused_delegation_types(struct nfs_client *clp,
Chuck Leverd3978bb2010-12-24 01:33:04 +0000732 fmode_t flags)
733{
734 struct nfs_server *server;
735
736 rcu_read_lock();
737 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
Trond Myklebust826e0012013-04-03 19:27:52 -0400738 nfs_mark_return_unused_delegation_types(server, flags);
Trond Myklebust515d8612008-12-23 15:21:46 -0500739 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740}
741
Trond Myklebust41020b62016-09-22 13:38:58 -0400742static void nfs_mark_delegation_revoked(struct nfs_server *server,
743 struct nfs_delegation *delegation)
Trond Myklebust869f9df2014-11-10 18:43:56 -0500744{
Trond Myklebust41020b62016-09-22 13:38:58 -0400745 set_bit(NFS_DELEGATION_REVOKED, &delegation->flags);
Trond Myklebust059b43e2016-09-22 13:39:06 -0400746 delegation->stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust41020b62016-09-22 13:38:58 -0400747 nfs_mark_return_delegation(server, delegation);
Trond Myklebust869f9df2014-11-10 18:43:56 -0500748}
749
Trond Myklebust41020b62016-09-22 13:38:58 -0400750static bool nfs_revoke_delegation(struct inode *inode,
751 const nfs4_stateid *stateid)
752{
753 struct nfs_delegation *delegation;
Trond Myklebust7f048832016-09-22 13:39:14 -0400754 nfs4_stateid tmp;
Trond Myklebust41020b62016-09-22 13:38:58 -0400755 bool ret = false;
756
757 rcu_read_lock();
758 delegation = rcu_dereference(NFS_I(inode)->delegation);
759 if (delegation == NULL)
760 goto out;
Trond Myklebust7f048832016-09-22 13:39:14 -0400761 if (stateid == NULL) {
762 nfs4_stateid_copy(&tmp, &delegation->stateid);
763 stateid = &tmp;
764 } else if (!nfs4_stateid_match(stateid, &delegation->stateid))
Trond Myklebust41020b62016-09-22 13:38:58 -0400765 goto out;
766 nfs_mark_delegation_revoked(NFS_SERVER(inode), delegation);
767 ret = true;
768out:
769 rcu_read_unlock();
Trond Myklebust7f048832016-09-22 13:39:14 -0400770 if (ret)
771 nfs_inode_find_state_and_recover(inode, stateid);
Trond Myklebust41020b62016-09-22 13:38:58 -0400772 return ret;
773}
774
775void nfs_remove_bad_delegation(struct inode *inode,
776 const nfs4_stateid *stateid)
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500777{
778 struct nfs_delegation *delegation;
779
Trond Myklebust41020b62016-09-22 13:38:58 -0400780 if (!nfs_revoke_delegation(inode, stateid))
781 return;
Trond Myklebustd25be542013-02-05 11:43:28 -0500782 delegation = nfs_inode_detach_delegation(inode);
Trond Myklebust7f048832016-09-22 13:39:14 -0400783 if (delegation)
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500784 nfs_free_delegation(delegation);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500785}
Andy Adamson9cb81962012-03-07 10:49:41 -0500786EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500787
Chuck Leverd3978bb2010-12-24 01:33:04 +0000788/**
Trond Myklebust826e0012013-04-03 19:27:52 -0400789 * nfs_expire_unused_delegation_types
Chuck Leverd3978bb2010-12-24 01:33:04 +0000790 * @clp: client to process
791 * @flags: delegation types to expire
792 *
793 */
Trond Myklebust826e0012013-04-03 19:27:52 -0400794void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags)
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500795{
Trond Myklebust826e0012013-04-03 19:27:52 -0400796 nfs_client_mark_return_unused_delegation_types(clp, flags);
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500797 nfs_delegation_run_state_manager(clp);
798}
799
Chuck Leverd3978bb2010-12-24 01:33:04 +0000800static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
Trond Myklebustb7391f42008-12-23 15:21:52 -0500801{
802 struct nfs_delegation *delegation;
803
Chuck Leverd3978bb2010-12-24 01:33:04 +0000804 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
Trond Myklebustb7391f42008-12-23 15:21:52 -0500805 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
806 continue;
Trond Myklebustb7571442013-04-03 14:33:49 -0400807 nfs_mark_return_if_closed_delegation(server, delegation);
Trond Myklebustb7391f42008-12-23 15:21:52 -0500808 }
Trond Myklebustb7391f42008-12-23 15:21:52 -0500809}
810
Chuck Leverd3978bb2010-12-24 01:33:04 +0000811/**
812 * nfs_expire_unreferenced_delegations - Eliminate unused delegations
813 * @clp: nfs_client to process
814 *
815 */
Trond Myklebustb7391f42008-12-23 15:21:52 -0500816void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
817{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000818 struct nfs_server *server;
819
820 rcu_read_lock();
821 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
822 nfs_mark_return_unreferenced_delegations(server);
823 rcu_read_unlock();
824
Trond Myklebustb7391f42008-12-23 15:21:52 -0500825 nfs_delegation_run_state_manager(clp);
826}
827
Chuck Leverd3978bb2010-12-24 01:33:04 +0000828/**
829 * nfs_async_inode_return_delegation - asynchronously return a delegation
830 * @inode: inode to process
Trond Myklebust8e663f02012-03-04 18:13:56 -0500831 * @stateid: state ID information
Chuck Leverd3978bb2010-12-24 01:33:04 +0000832 *
833 * Returns zero on success, or a negative errno value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 */
Chuck Leverd3978bb2010-12-24 01:33:04 +0000835int nfs_async_inode_return_delegation(struct inode *inode,
836 const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
Trond Myklebusted1e62112011-07-25 15:37:29 -0400838 struct nfs_server *server = NFS_SERVER(inode);
839 struct nfs_client *clp = server->nfs_client;
Trond Myklebust6411bd42008-12-23 15:21:51 -0500840 struct nfs_delegation *delegation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
Trond Myklebust6411bd42008-12-23 15:21:51 -0500842 rcu_read_lock();
843 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebust755a48a2014-03-02 22:03:12 -0500844 if (delegation == NULL)
845 goto out_enoent;
Trond Myklebust4816fda2015-09-20 14:58:42 -0400846 if (stateid != NULL &&
847 !clp->cl_mvops->match_stateid(&delegation->stateid, stateid))
Trond Myklebust755a48a2014-03-02 22:03:12 -0500848 goto out_enoent;
Trond Myklebusted1e62112011-07-25 15:37:29 -0400849 nfs_mark_return_delegation(server, delegation);
Trond Myklebust6411bd42008-12-23 15:21:51 -0500850 rcu_read_unlock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000851
Trond Myklebust6411bd42008-12-23 15:21:51 -0500852 nfs_delegation_run_state_manager(clp);
853 return 0;
Trond Myklebust755a48a2014-03-02 22:03:12 -0500854out_enoent:
855 rcu_read_unlock();
856 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858
Chuck Leverd3978bb2010-12-24 01:33:04 +0000859static struct inode *
860nfs_delegation_find_inode_server(struct nfs_server *server,
861 const struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
863 struct nfs_delegation *delegation;
Trond Myklebuste39d8a12018-11-13 16:37:54 -0500864 struct inode *freeme, *res = NULL;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000865
866 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
Trond Myklebust86e89482008-12-23 15:21:39 -0500867 spin_lock(&delegation->lock);
868 if (delegation->inode != NULL &&
869 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
Trond Myklebuste39d8a12018-11-13 16:37:54 -0500870 freeme = igrab(delegation->inode);
871 if (freeme && nfs_sb_active(freeme->i_sb))
872 res = freeme;
Trond Myklebust6c342652018-06-07 14:22:00 -0400873 spin_unlock(&delegation->lock);
874 if (res != NULL)
875 return res;
Trond Myklebuste39d8a12018-11-13 16:37:54 -0500876 if (freeme) {
877 rcu_read_unlock();
878 iput(freeme);
879 rcu_read_lock();
880 }
Trond Myklebust6c342652018-06-07 14:22:00 -0400881 return ERR_PTR(-EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 }
Trond Myklebust86e89482008-12-23 15:21:39 -0500883 spin_unlock(&delegation->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 }
Trond Myklebust6c342652018-06-07 14:22:00 -0400885 return ERR_PTR(-ENOENT);
Chuck Leverd3978bb2010-12-24 01:33:04 +0000886}
887
888/**
889 * nfs_delegation_find_inode - retrieve the inode associated with a delegation
890 * @clp: client state handle
891 * @fhandle: filehandle from a delegation recall
892 *
893 * Returns pointer to inode matching "fhandle," or NULL if a matching inode
894 * cannot be found.
895 */
896struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
897 const struct nfs_fh *fhandle)
898{
899 struct nfs_server *server;
Trond Myklebust6c342652018-06-07 14:22:00 -0400900 struct inode *res;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000901
902 rcu_read_lock();
903 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
904 res = nfs_delegation_find_inode_server(server, fhandle);
Anna Schumakerd5681f52018-06-14 09:39:17 -0400905 if (res != ERR_PTR(-ENOENT)) {
906 rcu_read_unlock();
Trond Myklebust6c342652018-06-07 14:22:00 -0400907 return res;
Anna Schumakerd5681f52018-06-14 09:39:17 -0400908 }
Chuck Leverd3978bb2010-12-24 01:33:04 +0000909 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400910 rcu_read_unlock();
Trond Myklebust6c342652018-06-07 14:22:00 -0400911 return ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912}
913
Chuck Leverd3978bb2010-12-24 01:33:04 +0000914static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
915{
916 struct nfs_delegation *delegation;
917
Trond Myklebust45870d62016-09-22 13:38:59 -0400918 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
919 /*
920 * If the delegation may have been admin revoked, then we
921 * cannot reclaim it.
922 */
923 if (test_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags))
924 continue;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000925 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
Trond Myklebust45870d62016-09-22 13:38:59 -0400926 }
Chuck Leverd3978bb2010-12-24 01:33:04 +0000927}
928
929/**
930 * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
931 * @clp: nfs_client to process
932 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 */
David Howellsadfa6f92006-08-22 20:06:08 -0400934void nfs_delegation_mark_reclaim(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000936 struct nfs_server *server;
937
Trond Myklebust8383e462007-07-06 15:12:04 -0400938 rcu_read_lock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000939 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
940 nfs_delegation_mark_reclaim_server(server);
Trond Myklebust8383e462007-07-06 15:12:04 -0400941 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
Chuck Leverd3978bb2010-12-24 01:33:04 +0000944/**
945 * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
946 * @clp: nfs_client to process
947 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 */
David Howellsadfa6f92006-08-22 20:06:08 -0400949void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950{
Trond Myklebust8383e462007-07-06 15:12:04 -0400951 struct nfs_delegation *delegation;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000952 struct nfs_server *server;
Trond Myklebust86e89482008-12-23 15:21:39 -0500953 struct inode *inode;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000954
Trond Myklebust8383e462007-07-06 15:12:04 -0400955restart:
956 rcu_read_lock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000957 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
958 list_for_each_entry_rcu(delegation, &server->delegations,
959 super_list) {
Trond Myklebust6f9449b2019-02-21 14:51:25 -0500960 if (test_bit(NFS_DELEGATION_INODE_FREEING,
961 &delegation->flags) ||
962 test_bit(NFS_DELEGATION_RETURNING,
963 &delegation->flags) ||
964 test_bit(NFS_DELEGATION_NEED_RECLAIM,
Chuck Leverd3978bb2010-12-24 01:33:04 +0000965 &delegation->flags) == 0)
966 continue;
Trond Myklebust9f0f8e12015-02-26 09:57:34 -0500967 if (!nfs_sb_active(server->super))
NeilBrownf38934912018-05-31 15:23:22 +1000968 break; /* continue in outer loop */
Trond Myklebust9f0f8e12015-02-26 09:57:34 -0500969 inode = nfs_delegation_grab_inode(delegation);
970 if (inode == NULL) {
971 rcu_read_unlock();
972 nfs_sb_deactive(server->super);
973 goto restart;
974 }
Trond Myklebustb04b22f2015-02-26 13:59:38 -0500975 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
Chuck Leverd3978bb2010-12-24 01:33:04 +0000976 rcu_read_unlock();
Trond Myklebustb04b22f2015-02-26 13:59:38 -0500977 if (delegation != NULL) {
978 delegation = nfs_detach_delegation(NFS_I(inode),
979 delegation, server);
980 if (delegation != NULL)
981 nfs_free_delegation(delegation);
982 }
Chuck Leverd3978bb2010-12-24 01:33:04 +0000983 iput(inode);
Trond Myklebust9f0f8e12015-02-26 09:57:34 -0500984 nfs_sb_deactive(server->super);
NeilBrown3ca951b2018-04-30 14:31:30 +1000985 cond_resched();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000986 goto restart;
987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400989 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990}
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500991
Trond Myklebustbb3d1a32016-09-22 13:39:00 -0400992static inline bool nfs4_server_rebooted(const struct nfs_client *clp)
993{
994 return (clp->cl_state & (BIT(NFS4CLNT_CHECK_LEASE) |
995 BIT(NFS4CLNT_LEASE_EXPIRED) |
996 BIT(NFS4CLNT_SESSION_RESET))) != 0;
997}
998
Trond Myklebust45870d62016-09-22 13:38:59 -0400999static void nfs_mark_test_expired_delegation(struct nfs_server *server,
1000 struct nfs_delegation *delegation)
1001{
Trond Myklebust059b43e2016-09-22 13:39:06 -04001002 if (delegation->stateid.type == NFS4_INVALID_STATEID_TYPE)
1003 return;
Trond Myklebust45870d62016-09-22 13:38:59 -04001004 clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
1005 set_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
1006 set_bit(NFS4CLNT_DELEGATION_EXPIRED, &server->nfs_client->cl_state);
1007}
1008
Trond Myklebustbb3d1a32016-09-22 13:39:00 -04001009static void nfs_inode_mark_test_expired_delegation(struct nfs_server *server,
1010 struct inode *inode)
1011{
1012 struct nfs_delegation *delegation;
1013
1014 rcu_read_lock();
1015 delegation = rcu_dereference(NFS_I(inode)->delegation);
1016 if (delegation)
1017 nfs_mark_test_expired_delegation(server, delegation);
1018 rcu_read_unlock();
1019
1020}
1021
Trond Myklebust45870d62016-09-22 13:38:59 -04001022static void nfs_delegation_mark_test_expired_server(struct nfs_server *server)
1023{
1024 struct nfs_delegation *delegation;
1025
1026 list_for_each_entry_rcu(delegation, &server->delegations, super_list)
1027 nfs_mark_test_expired_delegation(server, delegation);
1028}
1029
1030/**
1031 * nfs_mark_test_expired_all_delegations - mark all delegations for testing
1032 * @clp: nfs_client to process
1033 *
1034 * Iterates through all the delegations associated with this server and
1035 * marks them as needing to be checked for validity.
1036 */
1037void nfs_mark_test_expired_all_delegations(struct nfs_client *clp)
1038{
1039 struct nfs_server *server;
1040
1041 rcu_read_lock();
1042 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1043 nfs_delegation_mark_test_expired_server(server);
1044 rcu_read_unlock();
1045}
1046
1047/**
Scott Mayhew8ca017c2019-05-06 11:59:05 -04001048 * nfs_test_expired_all_delegations - test all delegations for a client
1049 * @clp: nfs_client to process
1050 *
1051 * Helper for handling "recallable state revoked" status from server.
1052 */
1053void nfs_test_expired_all_delegations(struct nfs_client *clp)
1054{
1055 nfs_mark_test_expired_all_delegations(clp);
1056 nfs4_schedule_state_manager(clp);
1057}
1058
Trond Myklebustad114082019-07-26 14:40:53 +01001059static void
1060nfs_delegation_test_free_expired(struct inode *inode,
1061 nfs4_stateid *stateid,
1062 const struct cred *cred)
1063{
1064 struct nfs_server *server = NFS_SERVER(inode);
1065 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
1066 int status;
1067
1068 if (!cred)
1069 return;
1070 status = ops->test_and_free_expired(server, stateid, cred);
1071 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
1072 nfs_remove_bad_delegation(inode, stateid);
1073}
1074
Scott Mayhew8ca017c2019-05-06 11:59:05 -04001075/**
Trond Myklebust45870d62016-09-22 13:38:59 -04001076 * nfs_reap_expired_delegations - reap expired delegations
1077 * @clp: nfs_client to process
1078 *
1079 * Iterates through all the delegations associated with this server and
1080 * checks if they have may have been revoked. This function is usually
1081 * expected to be called in cases where the server may have lost its
1082 * lease.
1083 */
1084void nfs_reap_expired_delegations(struct nfs_client *clp)
1085{
Trond Myklebust45870d62016-09-22 13:38:59 -04001086 struct nfs_delegation *delegation;
1087 struct nfs_server *server;
1088 struct inode *inode;
NeilBrowna52458b2018-12-03 11:30:31 +11001089 const struct cred *cred;
Trond Myklebust45870d62016-09-22 13:38:59 -04001090 nfs4_stateid stateid;
1091
1092restart:
1093 rcu_read_lock();
1094 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1095 list_for_each_entry_rcu(delegation, &server->delegations,
1096 super_list) {
Trond Myklebust6f9449b2019-02-21 14:51:25 -05001097 if (test_bit(NFS_DELEGATION_INODE_FREEING,
1098 &delegation->flags) ||
1099 test_bit(NFS_DELEGATION_RETURNING,
1100 &delegation->flags) ||
1101 test_bit(NFS_DELEGATION_TEST_EXPIRED,
Trond Myklebust45870d62016-09-22 13:38:59 -04001102 &delegation->flags) == 0)
1103 continue;
1104 if (!nfs_sb_active(server->super))
NeilBrownf38934912018-05-31 15:23:22 +10001105 break; /* continue in outer loop */
Trond Myklebust45870d62016-09-22 13:38:59 -04001106 inode = nfs_delegation_grab_inode(delegation);
1107 if (inode == NULL) {
1108 rcu_read_unlock();
1109 nfs_sb_deactive(server->super);
1110 goto restart;
1111 }
NeilBrowna52458b2018-12-03 11:30:31 +11001112 cred = get_cred_rcu(delegation->cred);
Trond Myklebust45870d62016-09-22 13:38:59 -04001113 nfs4_stateid_copy(&stateid, &delegation->stateid);
1114 clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
1115 rcu_read_unlock();
Trond Myklebustad114082019-07-26 14:40:53 +01001116 nfs_delegation_test_free_expired(inode, &stateid, cred);
NeilBrowna52458b2018-12-03 11:30:31 +11001117 put_cred(cred);
Trond Myklebustbb3d1a32016-09-22 13:39:00 -04001118 if (nfs4_server_rebooted(clp)) {
1119 nfs_inode_mark_test_expired_delegation(server,inode);
1120 iput(inode);
1121 nfs_sb_deactive(server->super);
1122 return;
1123 }
Trond Myklebust45870d62016-09-22 13:38:59 -04001124 iput(inode);
1125 nfs_sb_deactive(server->super);
NeilBrown3ca951b2018-04-30 14:31:30 +10001126 cond_resched();
Trond Myklebust45870d62016-09-22 13:38:59 -04001127 goto restart;
1128 }
1129 }
1130 rcu_read_unlock();
1131}
1132
Trond Myklebust6c2d8f82016-09-22 13:39:07 -04001133void nfs_inode_find_delegation_state_and_recover(struct inode *inode,
1134 const nfs4_stateid *stateid)
1135{
1136 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1137 struct nfs_delegation *delegation;
1138 bool found = false;
1139
1140 rcu_read_lock();
1141 delegation = rcu_dereference(NFS_I(inode)->delegation);
1142 if (delegation &&
1143 nfs4_stateid_match_other(&delegation->stateid, stateid)) {
1144 nfs_mark_test_expired_delegation(NFS_SERVER(inode), delegation);
1145 found = true;
1146 }
1147 rcu_read_unlock();
1148 if (found)
1149 nfs4_schedule_state_manager(clp);
1150}
1151
Chuck Leverd3978bb2010-12-24 01:33:04 +00001152/**
1153 * nfs_delegations_present - check for existence of delegations
1154 * @clp: client state handle
1155 *
1156 * Returns one if there are any nfs_delegation structures attached
1157 * to this nfs_client.
1158 */
1159int nfs_delegations_present(struct nfs_client *clp)
1160{
1161 struct nfs_server *server;
1162 int ret = 0;
1163
1164 rcu_read_lock();
1165 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1166 if (!list_empty(&server->delegations)) {
1167 ret = 1;
1168 break;
1169 }
1170 rcu_read_unlock();
1171 return ret;
1172}
1173
1174/**
Trond Myklebust12f275c2017-11-06 15:28:05 -05001175 * nfs4_refresh_delegation_stateid - Update delegation stateid seqid
1176 * @dst: stateid to refresh
1177 * @inode: inode to check
1178 *
1179 * Returns "true" and updates "dst->seqid" * if inode had a delegation
1180 * that matches our delegation stateid. Otherwise "false" is returned.
1181 */
1182bool nfs4_refresh_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
1183{
1184 struct nfs_delegation *delegation;
1185 bool ret = false;
1186 if (!inode)
1187 goto out;
1188
1189 rcu_read_lock();
1190 delegation = rcu_dereference(NFS_I(inode)->delegation);
1191 if (delegation != NULL &&
1192 nfs4_stateid_match_other(dst, &delegation->stateid)) {
1193 dst->seqid = delegation->stateid.seqid;
Trond Myklebust79cc5542019-10-31 18:40:33 -04001194 ret = true;
Trond Myklebust12f275c2017-11-06 15:28:05 -05001195 }
1196 rcu_read_unlock();
1197out:
1198 return ret;
1199}
1200
1201/**
Chuck Leverd3978bb2010-12-24 01:33:04 +00001202 * nfs4_copy_delegation_stateid - Copy inode's state ID information
Chuck Leverd3978bb2010-12-24 01:33:04 +00001203 * @inode: inode to check
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001204 * @flags: delegation type requirement
Trond Myklebustabf4e132016-05-16 17:42:44 -04001205 * @dst: stateid data structure to fill in
1206 * @cred: optional argument to retrieve credential
Chuck Leverd3978bb2010-12-24 01:33:04 +00001207 *
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001208 * Returns "true" and fills in "dst->data" * if inode had a delegation,
1209 * otherwise "false" is returned.
Chuck Leverd3978bb2010-12-24 01:33:04 +00001210 */
Trond Myklebustabf4e132016-05-16 17:42:44 -04001211bool nfs4_copy_delegation_stateid(struct inode *inode, fmode_t flags,
NeilBrowna52458b2018-12-03 11:30:31 +11001212 nfs4_stateid *dst, const struct cred **cred)
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001213{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001214 struct nfs_inode *nfsi = NFS_I(inode);
1215 struct nfs_delegation *delegation;
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001216 bool ret;
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001217
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001218 flags &= FMODE_READ|FMODE_WRITE;
Trond Myklebust8383e462007-07-06 15:12:04 -04001219 rcu_read_lock();
1220 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebustaa05c872016-09-22 13:38:54 -04001221 ret = nfs4_is_valid_delegation(delegation, flags);
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001222 if (ret) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001223 nfs4_stateid_copy(dst, &delegation->stateid);
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001224 nfs_mark_delegation_referenced(delegation);
Trond Myklebustabf4e132016-05-16 17:42:44 -04001225 if (cred)
NeilBrowna52458b2018-12-03 11:30:31 +11001226 *cred = get_cred(delegation->cred);
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001227 }
Trond Myklebust8383e462007-07-06 15:12:04 -04001228 rcu_read_unlock();
1229 return ret;
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001230}
Trond Myklebust5445b1f2015-09-05 19:06:58 -04001231
1232/**
1233 * nfs4_delegation_flush_on_close - Check if we must flush file on close
1234 * @inode: inode to check
1235 *
1236 * This function checks the number of outstanding writes to the file
1237 * against the delegation 'space_limit' field to see if
1238 * the spec requires us to flush the file on close.
1239 */
1240bool nfs4_delegation_flush_on_close(const struct inode *inode)
1241{
1242 struct nfs_inode *nfsi = NFS_I(inode);
1243 struct nfs_delegation *delegation;
1244 bool ret = true;
1245
1246 rcu_read_lock();
1247 delegation = rcu_dereference(nfsi->delegation);
1248 if (delegation == NULL || !(delegation->type & FMODE_WRITE))
1249 goto out;
Trond Myklebusta6b6d5b2017-08-01 15:39:46 -04001250 if (atomic_long_read(&nfsi->nrequests) < delegation->pagemod_limit)
Trond Myklebust5445b1f2015-09-05 19:06:58 -04001251 ret = false;
1252out:
1253 rcu_read_unlock();
1254 return ret;
1255}