blob: 0ff3facf81dac61fe99c6eff078e0a23b000d2e4 [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
Peng Tao15bb3af2014-07-03 13:05:00 +080056static int
57nfs4_do_check_delegation(struct inode *inode, fmode_t flags, bool mark)
Trond Myklebustb7391f42008-12-23 15:21:52 -050058{
59 struct nfs_delegation *delegation;
60 int ret = 0;
61
62 flags &= FMODE_READ|FMODE_WRITE;
63 rcu_read_lock();
64 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustaa05c872016-09-22 13:38:54 -040065 if (nfs4_is_valid_delegation(delegation, flags)) {
Peng Tao15bb3af2014-07-03 13:05:00 +080066 if (mark)
67 nfs_mark_delegation_referenced(delegation);
Trond Myklebustb7391f42008-12-23 15:21:52 -050068 ret = 1;
69 }
70 rcu_read_unlock();
71 return ret;
72}
Peng Tao15bb3af2014-07-03 13:05:00 +080073/**
74 * nfs_have_delegation - check if inode has a delegation, mark it
75 * NFS_DELEGATION_REFERENCED if there is one.
76 * @inode: inode to check
77 * @flags: delegation types to check for
78 *
79 * Returns one if inode has the indicated delegation, otherwise zero.
80 */
81int nfs4_have_delegation(struct inode *inode, fmode_t flags)
82{
83 return nfs4_do_check_delegation(inode, flags, true);
84}
85
86/*
87 * nfs4_check_delegation - check if inode has a delegation, do not mark
88 * NFS_DELEGATION_REFERENCED if it has one.
89 */
90int nfs4_check_delegation(struct inode *inode, fmode_t flags)
91{
92 return nfs4_do_check_delegation(inode, flags, false);
93}
Trond Myklebustb7391f42008-12-23 15:21:52 -050094
Olga Kornievskaia44f411c2018-10-04 14:45:00 -040095static int nfs_delegation_claim_locks(struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -050096{
97 struct inode *inode = state->inode;
98 struct file_lock *fl;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -050099 struct file_lock_context *flctx = inode->i_flctx;
100 struct list_head *list;
Trond Myklebustd5122202009-06-17 13:22:58 -0700101 int status = 0;
Trond Myklebust888e6942005-11-04 15:38:11 -0500102
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500103 if (flctx == NULL)
Trond Myklebust65b62a22013-02-07 10:54:07 -0500104 goto out;
Jeff Layton314d7cc2013-04-10 15:36:48 -0400105
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500106 list = &flctx->flc_posix;
Jeff Layton6109c852015-01-16 15:05:57 -0500107 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500108restart:
109 list_for_each_entry(fl, list, fl_list) {
Olga Kornievskaia44f411c2018-10-04 14:45:00 -0400110 if (nfs_file_open_context(fl->fl_file)->state != state)
Trond Myklebust888e6942005-11-04 15:38:11 -0500111 continue;
Jeff Layton6109c852015-01-16 15:05:57 -0500112 spin_unlock(&flctx->flc_lock);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -0400113 status = nfs4_lock_delegation_recall(fl, state, stateid);
Trond Myklebustd5122202009-06-17 13:22:58 -0700114 if (status < 0)
Trond Myklebust3f09df72009-06-17 13:23:00 -0700115 goto out;
Jeff Layton6109c852015-01-16 15:05:57 -0500116 spin_lock(&flctx->flc_lock);
Trond Myklebust888e6942005-11-04 15:38:11 -0500117 }
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500118 if (list == &flctx->flc_posix) {
119 list = &flctx->flc_flock;
120 goto restart;
Jeff Layton5263e312015-01-16 15:05:55 -0500121 }
Jeff Layton6109c852015-01-16 15:05:57 -0500122 spin_unlock(&flctx->flc_lock);
Trond Myklebust3f09df72009-06-17 13:23:00 -0700123out:
Trond Myklebust888e6942005-11-04 15:38:11 -0500124 return status;
125}
126
Trond Myklebust24311f82015-09-20 10:50:17 -0400127static int nfs_delegation_claim_opens(struct inode *inode,
128 const nfs4_stateid *stateid, fmode_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
130 struct nfs_inode *nfsi = NFS_I(inode);
131 struct nfs_open_context *ctx;
Trond Myklebustd25be542013-02-05 11:43:28 -0500132 struct nfs4_state_owner *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 struct nfs4_state *state;
Trond Myklebustd25be542013-02-05 11:43:28 -0500134 unsigned int seq;
Trond Myklebust888e6942005-11-04 15:38:11 -0500135 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
137again:
Trond Myklebust0de43972018-09-02 15:57:01 -0400138 rcu_read_lock();
139 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 state = ctx->state;
141 if (state == NULL)
142 continue;
143 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
144 continue;
Trond Myklebustf8ebf7a2014-10-17 23:02:52 +0300145 if (!nfs4_valid_open_stateid(state))
146 continue;
Trond Myklebustf597c532012-03-04 18:13:56 -0500147 if (!nfs4_stateid_match(&state->stateid, stateid))
Trond Myklebust90163022007-07-05 14:55:18 -0400148 continue;
Trond Myklebust0de43972018-09-02 15:57:01 -0400149 if (!get_nfs_open_context(ctx))
150 continue;
151 rcu_read_unlock();
Trond Myklebustd25be542013-02-05 11:43:28 -0500152 sp = state->owner;
Trond Myklebust65b62a22013-02-07 10:54:07 -0500153 /* Block nfs4_proc_unlck */
154 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebustd25be542013-02-05 11:43:28 -0500155 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
Trond Myklebust24311f82015-09-20 10:50:17 -0400156 err = nfs4_open_delegation_recall(ctx, state, stateid, type);
Trond Myklebustd25be542013-02-05 11:43:28 -0500157 if (!err)
Olga Kornievskaia44f411c2018-10-04 14:45:00 -0400158 err = nfs_delegation_claim_locks(state, stateid);
Trond Myklebustd25be542013-02-05 11:43:28 -0500159 if (!err && read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
160 err = -EAGAIN;
Trond Myklebust65b62a22013-02-07 10:54:07 -0500161 mutex_unlock(&sp->so_delegreturn_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 put_nfs_open_context(ctx);
Trond Myklebust888e6942005-11-04 15:38:11 -0500163 if (err != 0)
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500164 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 goto again;
166 }
Trond Myklebust0de43972018-09-02 15:57:01 -0400167 rcu_read_unlock();
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500168 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
Chuck Leverd3978bb2010-12-24 01:33:04 +0000171/**
172 * nfs_inode_reclaim_delegation - process a delegation reclaim request
173 * @inode: inode to process
174 * @cred: credential to use for request
Trond Myklebust35156bf2018-03-20 17:03:13 -0400175 * @type: delegation type
176 * @stateid: delegation stateid
177 * @pagemod_limit: write delegation "space_limit"
Chuck Leverd3978bb2010-12-24 01:33:04 +0000178 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 */
NeilBrowna52458b2018-12-03 11:30:31 +1100180void nfs_inode_reclaim_delegation(struct inode *inode, const struct cred *cred,
Trond Myklebust35156bf2018-03-20 17:03:13 -0400181 fmode_t type,
182 const nfs4_stateid *stateid,
183 unsigned long pagemod_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
Trond Myklebust8f649c32010-05-01 12:36:18 -0400185 struct nfs_delegation *delegation;
NeilBrowna52458b2018-12-03 11:30:31 +1100186 const struct cred *oldcred = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Trond Myklebust8f649c32010-05-01 12:36:18 -0400188 rcu_read_lock();
189 delegation = rcu_dereference(NFS_I(inode)->delegation);
190 if (delegation != NULL) {
191 spin_lock(&delegation->lock);
192 if (delegation->inode != NULL) {
Trond Myklebust35156bf2018-03-20 17:03:13 -0400193 nfs4_stateid_copy(&delegation->stateid, stateid);
194 delegation->type = type;
195 delegation->pagemod_limit = pagemod_limit;
Trond Myklebust8f649c32010-05-01 12:36:18 -0400196 oldcred = delegation->cred;
NeilBrowna52458b2018-12-03 11:30:31 +1100197 delegation->cred = get_cred(cred);
Trond Myklebust8f649c32010-05-01 12:36:18 -0400198 clear_bit(NFS_DELEGATION_NEED_RECLAIM,
199 &delegation->flags);
Trond Myklebust8f649c32010-05-01 12:36:18 -0400200 spin_unlock(&delegation->lock);
Trond Myklebust8f649c32010-05-01 12:36:18 -0400201 rcu_read_unlock();
NeilBrowna52458b2018-12-03 11:30:31 +1100202 put_cred(oldcred);
Trond Myklebust35156bf2018-03-20 17:03:13 -0400203 trace_nfs4_reclaim_delegation(inode, type);
Trond Myklebustb1a318d2016-09-22 13:39:12 -0400204 return;
Trond Myklebust8f649c32010-05-01 12:36:18 -0400205 }
Trond Myklebustb1a318d2016-09-22 13:39:12 -0400206 /* We appear to have raced with a delegation return. */
207 spin_unlock(&delegation->lock);
Trond Myklebust8f649c32010-05-01 12:36:18 -0400208 }
Trond Myklebustb1a318d2016-09-22 13:39:12 -0400209 rcu_read_unlock();
Trond Myklebust35156bf2018-03-20 17:03:13 -0400210 nfs_inode_set_delegation(inode, cred, type, stateid, pagemod_limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
Trond Myklebust57bfa892008-01-25 16:38:18 -0500213static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
214{
215 int res = 0;
216
Trond Myklebust869f9df2014-11-10 18:43:56 -0500217 if (!test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
218 res = nfs4_proc_delegreturn(inode,
219 delegation->cred,
220 &delegation->stateid,
221 issync);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500222 nfs_free_delegation(delegation);
223 return res;
224}
225
Trond Myklebust86e89482008-12-23 15:21:39 -0500226static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
227{
228 struct inode *inode = NULL;
229
230 spin_lock(&delegation->lock);
231 if (delegation->inode != NULL)
232 inode = igrab(delegation->inode);
Trond Myklebust6f9449b2019-02-21 14:51:25 -0500233 if (!inode)
234 set_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags);
Trond Myklebust86e89482008-12-23 15:21:39 -0500235 spin_unlock(&delegation->lock);
236 return inode;
237}
238
Chuck Leverdda4b222010-12-24 01:32:54 +0000239static struct nfs_delegation *
Trond Myklebustd25be542013-02-05 11:43:28 -0500240nfs_start_delegation_return_locked(struct nfs_inode *nfsi)
Trond Myklebust57bfa892008-01-25 16:38:18 -0500241{
Trond Myklebustd25be542013-02-05 11:43:28 -0500242 struct nfs_delegation *ret = NULL;
243 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500244
245 if (delegation == NULL)
Trond Myklebustd25be542013-02-05 11:43:28 -0500246 goto out;
247 spin_lock(&delegation->lock);
248 if (!test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
249 ret = delegation;
250 spin_unlock(&delegation->lock);
251out:
252 return ret;
253}
254
255static struct nfs_delegation *
256nfs_start_delegation_return(struct nfs_inode *nfsi)
257{
258 struct nfs_delegation *delegation;
259
260 rcu_read_lock();
261 delegation = nfs_start_delegation_return_locked(nfsi);
262 rcu_read_unlock();
263 return delegation;
264}
265
266static void
267nfs_abort_delegation_return(struct nfs_delegation *delegation,
268 struct nfs_client *clp)
269{
Chuck Leverdda4b222010-12-24 01:32:54 +0000270
Trond Myklebust34310432008-12-23 15:21:38 -0500271 spin_lock(&delegation->lock);
Trond Myklebustd25be542013-02-05 11:43:28 -0500272 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
273 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
274 spin_unlock(&delegation->lock);
275 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
276}
277
278static struct nfs_delegation *
279nfs_detach_delegation_locked(struct nfs_inode *nfsi,
280 struct nfs_delegation *delegation,
281 struct nfs_client *clp)
282{
283 struct nfs_delegation *deleg_cur =
284 rcu_dereference_protected(nfsi->delegation,
285 lockdep_is_held(&clp->cl_lock));
286
287 if (deleg_cur == NULL || delegation != deleg_cur)
288 return NULL;
289
290 spin_lock(&delegation->lock);
291 set_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500292 list_del_rcu(&delegation->super_list);
Trond Myklebust86e89482008-12-23 15:21:39 -0500293 delegation->inode = NULL;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500294 rcu_assign_pointer(nfsi->delegation, NULL);
Trond Myklebust34310432008-12-23 15:21:38 -0500295 spin_unlock(&delegation->lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500296 return delegation;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500297}
298
Chuck Leverdda4b222010-12-24 01:32:54 +0000299static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi,
Trond Myklebustd25be542013-02-05 11:43:28 -0500300 struct nfs_delegation *delegation,
301 struct nfs_server *server)
Chuck Leverdda4b222010-12-24 01:32:54 +0000302{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000303 struct nfs_client *clp = server->nfs_client;
Chuck Leverdda4b222010-12-24 01:32:54 +0000304
305 spin_lock(&clp->cl_lock);
Trond Myklebustd25be542013-02-05 11:43:28 -0500306 delegation = nfs_detach_delegation_locked(nfsi, delegation, clp);
Chuck Leverdda4b222010-12-24 01:32:54 +0000307 spin_unlock(&clp->cl_lock);
308 return delegation;
309}
310
Trond Myklebustd25be542013-02-05 11:43:28 -0500311static struct nfs_delegation *
312nfs_inode_detach_delegation(struct inode *inode)
313{
314 struct nfs_inode *nfsi = NFS_I(inode);
315 struct nfs_server *server = NFS_SERVER(inode);
316 struct nfs_delegation *delegation;
317
318 delegation = nfs_start_delegation_return(nfsi);
319 if (delegation == NULL)
320 return NULL;
321 return nfs_detach_delegation(nfsi, delegation, server);
322}
323
Trond Myklebustcf6726e2014-12-19 11:22:28 -0500324static void
325nfs_update_inplace_delegation(struct nfs_delegation *delegation,
326 const struct nfs_delegation *update)
327{
328 if (nfs4_stateid_is_newer(&update->stateid, &delegation->stateid)) {
329 delegation->stateid.seqid = update->stateid.seqid;
330 smp_wmb();
331 delegation->type = update->type;
332 }
333}
334
Chuck Leverd3978bb2010-12-24 01:33:04 +0000335/**
336 * nfs_inode_set_delegation - set up a delegation on an inode
337 * @inode: inode to which delegation applies
338 * @cred: cred to use for subsequent delegation processing
Trond Myklebust35156bf2018-03-20 17:03:13 -0400339 * @type: delegation type
340 * @stateid: delegation stateid
341 * @pagemod_limit: write delegation "space_limit"
Chuck Leverd3978bb2010-12-24 01:33:04 +0000342 *
343 * Returns zero on success, or a negative errno value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 */
NeilBrowna52458b2018-12-03 11:30:31 +1100345int nfs_inode_set_delegation(struct inode *inode, const struct cred *cred,
Trond Myklebust35156bf2018-03-20 17:03:13 -0400346 fmode_t type,
347 const nfs4_stateid *stateid,
348 unsigned long pagemod_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000350 struct nfs_server *server = NFS_SERVER(inode);
351 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 struct nfs_inode *nfsi = NFS_I(inode);
David Howells17d2c0a2010-05-01 12:37:18 -0400353 struct nfs_delegation *delegation, *old_delegation;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500354 struct nfs_delegation *freeme = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 int status = 0;
356
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400357 delegation = kmalloc(sizeof(*delegation), GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (delegation == NULL)
359 return -ENOMEM;
Trond Myklebust35156bf2018-03-20 17:03:13 -0400360 nfs4_stateid_copy(&delegation->stateid, stateid);
361 delegation->type = type;
362 delegation->pagemod_limit = pagemod_limit;
Jeff Layton1eb5d982018-01-09 08:21:17 -0500363 delegation->change_attr = inode_peek_iversion_raw(inode);
NeilBrowna52458b2018-12-03 11:30:31 +1100364 delegation->cred = get_cred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 delegation->inode = inode;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500366 delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
Trond Myklebust34310432008-12-23 15:21:38 -0500367 spin_lock_init(&delegation->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 spin_lock(&clp->cl_lock);
David Howells17d2c0a2010-05-01 12:37:18 -0400370 old_delegation = rcu_dereference_protected(nfsi->delegation,
Chuck Leverd3978bb2010-12-24 01:33:04 +0000371 lockdep_is_held(&clp->cl_lock));
David Howells17d2c0a2010-05-01 12:37:18 -0400372 if (old_delegation != NULL) {
Trond Myklebustcf6726e2014-12-19 11:22:28 -0500373 /* Is this an update of the existing delegation? */
374 if (nfs4_stateid_match_other(&old_delegation->stateid,
375 &delegation->stateid)) {
376 nfs_update_inplace_delegation(old_delegation,
377 delegation);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500378 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 }
Trond Myklebust57bfa892008-01-25 16:38:18 -0500380 /*
381 * Deal with broken servers that hand out two
382 * delegations for the same file.
Trond Myklebust17280172012-03-11 13:11:00 -0400383 * Allow for upgrades to a WRITE delegation, but
384 * nothing else.
Trond Myklebust57bfa892008-01-25 16:38:18 -0500385 */
386 dfprintk(FILE, "%s: server %s handed out "
387 "a duplicate delegation!\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700388 __func__, clp->cl_hostname);
Trond Myklebust17280172012-03-11 13:11:00 -0400389 if (delegation->type == old_delegation->type ||
390 !(delegation->type & FMODE_WRITE)) {
Trond Myklebust57bfa892008-01-25 16:38:18 -0500391 freeme = delegation;
392 delegation = NULL;
393 goto out;
394 }
Trond Myklebustade04642015-02-27 14:25:50 -0500395 if (test_and_set_bit(NFS_DELEGATION_RETURNING,
396 &old_delegation->flags))
397 goto out;
398 freeme = nfs_detach_delegation_locked(nfsi,
Trond Myklebustd25be542013-02-05 11:43:28 -0500399 old_delegation, clp);
400 if (freeme == NULL)
401 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
Trond Myklebust38942ba2015-03-04 15:59:05 -0500403 list_add_tail_rcu(&delegation->super_list, &server->delegations);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500404 rcu_assign_pointer(nfsi->delegation, delegation);
405 delegation = NULL;
Trond Myklebust412c77c2007-07-03 16:10:55 -0400406
Trond Myklebust35156bf2018-03-20 17:03:13 -0400407 trace_nfs4_set_delegation(inode, type);
Trond Myklebust412c77c2007-07-03 16:10:55 -0400408
Trond Myklebust97c2c172018-04-07 18:43:17 -0400409 spin_lock(&inode->i_lock);
410 if (NFS_I(inode)->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ATIME))
411 NFS_I(inode)->cache_validity |= NFS_INO_REVAL_FORCED;
412 spin_unlock(&inode->i_lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500413out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 spin_unlock(&clp->cl_lock);
Trond Myklebust603c83d2007-10-18 19:59:20 -0400415 if (delegation != NULL)
416 nfs_free_delegation(delegation);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500417 if (freeme != NULL)
418 nfs_do_return_delegation(inode, freeme, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 return status;
420}
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422/*
423 * Basic procedure for returning a delegation to the server
424 */
Trond Myklebustd25be542013-02-05 11:43:28 -0500425static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Trond Myklebustd25be542013-02-05 11:43:28 -0500427 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust869f9df2014-11-10 18:43:56 -0500429 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
Trond Myklebustd25be542013-02-05 11:43:28 -0500431 if (delegation == NULL)
432 return 0;
433 do {
Trond Myklebust869f9df2014-11-10 18:43:56 -0500434 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
435 break;
Trond Myklebust24311f82015-09-20 10:50:17 -0400436 err = nfs_delegation_claim_opens(inode, &delegation->stateid,
437 delegation->type);
Trond Myklebustd25be542013-02-05 11:43:28 -0500438 if (!issync || err != -EAGAIN)
439 break;
440 /*
441 * Guard against state recovery
442 */
443 err = nfs4_wait_clnt_recover(clp);
444 } while (err == 0);
445
446 if (err) {
447 nfs_abort_delegation_return(delegation, clp);
448 goto out;
449 }
450 if (!nfs_detach_delegation(nfsi, delegation, NFS_SERVER(inode)))
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500451 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500453 err = nfs_do_return_delegation(inode, delegation, issync);
454out:
455 return err;
Trond Myklebust90163022007-07-05 14:55:18 -0400456}
457
Trond Myklebustb7571442013-04-03 14:33:49 -0400458static bool nfs_delegation_need_return(struct nfs_delegation *delegation)
459{
460 bool ret = false;
461
Trond Myklebustec3ca4e52015-02-26 14:05:05 -0500462 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
463 goto out;
Trond Myklebustb7571442013-04-03 14:33:49 -0400464 if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
465 ret = true;
466 if (test_and_clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags) && !ret) {
467 struct inode *inode;
468
469 spin_lock(&delegation->lock);
470 inode = delegation->inode;
471 if (inode && list_empty(&NFS_I(inode)->open_files))
472 ret = true;
473 spin_unlock(&delegation->lock);
474 }
Trond Myklebustec3ca4e52015-02-26 14:05:05 -0500475out:
Trond Myklebustb7571442013-04-03 14:33:49 -0400476 return ret;
477}
478
Chuck Leverd3978bb2010-12-24 01:33:04 +0000479/**
480 * nfs_client_return_marked_delegations - return previously marked delegations
481 * @clp: nfs_client to process
482 *
Trond Myklebustdc327ed2012-05-06 19:46:30 -0400483 * Note that this function is designed to be called by the state
484 * manager thread. For this reason, it cannot flush the dirty data,
485 * since that could deadlock in case of a state recovery error.
486 *
Chuck Leverd3978bb2010-12-24 01:33:04 +0000487 * Returns zero on success, or a negative errno value.
Trond Myklebust515d8612008-12-23 15:21:46 -0500488 */
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500489int nfs_client_return_marked_delegations(struct nfs_client *clp)
Trond Myklebust515d8612008-12-23 15:21:46 -0500490{
491 struct nfs_delegation *delegation;
NeilBrowne04bbf62018-04-30 14:31:30 +1000492 struct nfs_delegation *prev;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000493 struct nfs_server *server;
Trond Myklebust515d8612008-12-23 15:21:46 -0500494 struct inode *inode;
NeilBrowne04bbf62018-04-30 14:31:30 +1000495 struct inode *place_holder = NULL;
496 struct nfs_delegation *place_holder_deleg = NULL;
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500497 int err = 0;
Trond Myklebust515d8612008-12-23 15:21:46 -0500498
499restart:
NeilBrowne04bbf62018-04-30 14:31:30 +1000500 /*
501 * To avoid quadratic looping we hold a reference
502 * to an inode place_holder. Each time we restart, we
503 * list nfs_servers from the server of that inode, and
504 * delegation in the server from the delegations of that
505 * inode.
506 * prev is an RCU-protected pointer to a delegation which
507 * wasn't marked for return and might be a good choice for
508 * the next place_holder.
509 */
Trond Myklebust515d8612008-12-23 15:21:46 -0500510 rcu_read_lock();
NeilBrowne04bbf62018-04-30 14:31:30 +1000511 prev = NULL;
512 if (place_holder)
513 server = NFS_SERVER(place_holder);
514 else
515 server = list_entry_rcu(clp->cl_superblocks.next,
516 struct nfs_server, client_link);
517 list_for_each_entry_from_rcu(server, &clp->cl_superblocks, client_link) {
518 delegation = NULL;
519 if (place_holder && server == NFS_SERVER(place_holder))
520 delegation = rcu_dereference(NFS_I(place_holder)->delegation);
521 if (!delegation || delegation != place_holder_deleg)
522 delegation = list_entry_rcu(server->delegations.next,
523 struct nfs_delegation, super_list);
524 list_for_each_entry_from_rcu(delegation, &server->delegations, super_list) {
525 struct inode *to_put = NULL;
526
527 if (!nfs_delegation_need_return(delegation)) {
528 prev = delegation;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000529 continue;
NeilBrowne04bbf62018-04-30 14:31:30 +1000530 }
Trond Myklebust9f0f8e12015-02-26 09:57:34 -0500531 if (!nfs_sb_active(server->super))
NeilBrownf38934912018-05-31 15:23:22 +1000532 break; /* continue in outer loop */
NeilBrowne04bbf62018-04-30 14:31:30 +1000533
534 if (prev) {
535 struct inode *tmp;
536
537 tmp = nfs_delegation_grab_inode(prev);
538 if (tmp) {
539 to_put = place_holder;
540 place_holder = tmp;
541 place_holder_deleg = prev;
542 }
543 }
544
Trond Myklebust9f0f8e12015-02-26 09:57:34 -0500545 inode = nfs_delegation_grab_inode(delegation);
546 if (inode == NULL) {
547 rcu_read_unlock();
NeilBrowne04bbf62018-04-30 14:31:30 +1000548 if (to_put)
549 iput(to_put);
Trond Myklebust9f0f8e12015-02-26 09:57:34 -0500550 nfs_sb_deactive(server->super);
551 goto restart;
552 }
Trond Myklebustd25be542013-02-05 11:43:28 -0500553 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
Chuck Leverd3978bb2010-12-24 01:33:04 +0000554 rcu_read_unlock();
555
NeilBrowne04bbf62018-04-30 14:31:30 +1000556 if (to_put)
557 iput(to_put);
558
Trond Myklebustd25be542013-02-05 11:43:28 -0500559 err = nfs_end_delegation_return(inode, delegation, 0);
Chuck Leverd3978bb2010-12-24 01:33:04 +0000560 iput(inode);
Trond Myklebust9f0f8e12015-02-26 09:57:34 -0500561 nfs_sb_deactive(server->super);
NeilBrown3ca951b2018-04-30 14:31:30 +1000562 cond_resched();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000563 if (!err)
564 goto restart;
565 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
NeilBrowne04bbf62018-04-30 14:31:30 +1000566 if (place_holder)
567 iput(place_holder);
Chuck Leverd3978bb2010-12-24 01:33:04 +0000568 return err;
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500569 }
Trond Myklebust515d8612008-12-23 15:21:46 -0500570 }
571 rcu_read_unlock();
NeilBrowne04bbf62018-04-30 14:31:30 +1000572 if (place_holder)
573 iput(place_holder);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500574 return 0;
Trond Myklebust515d8612008-12-23 15:21:46 -0500575}
576
Chuck Leverd3978bb2010-12-24 01:33:04 +0000577/**
578 * nfs_inode_return_delegation_noreclaim - return delegation, don't reclaim opens
579 * @inode: inode to process
580 *
581 * Does not protect against delegation reclaims, therefore really only safe
582 * to be called from nfs4_clear_inode().
Trond Myklebuste6f81072008-01-24 18:14:34 -0500583 */
584void nfs_inode_return_delegation_noreclaim(struct inode *inode)
585{
Trond Myklebuste6f81072008-01-24 18:14:34 -0500586 struct nfs_delegation *delegation;
587
Trond Myklebustd25be542013-02-05 11:43:28 -0500588 delegation = nfs_inode_detach_delegation(inode);
589 if (delegation != NULL)
Trond Myklebust5fcdfac2015-03-25 13:19:42 -0400590 nfs_do_return_delegation(inode, delegation, 1);
Trond Myklebuste6f81072008-01-24 18:14:34 -0500591}
592
Chuck Leverd3978bb2010-12-24 01:33:04 +0000593/**
594 * nfs_inode_return_delegation - synchronously return a delegation
595 * @inode: inode to process
596 *
Trond Myklebustc57d1bc2012-05-06 19:34:17 -0400597 * This routine will always flush any dirty data to disk on the
598 * assumption that if we need to return the delegation, then
599 * we should stop caching.
600 *
Chuck Leverd3978bb2010-12-24 01:33:04 +0000601 * Returns zero on success, or a negative errno value.
602 */
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400603int nfs4_inode_return_delegation(struct inode *inode)
Trond Myklebust90163022007-07-05 14:55:18 -0400604{
Trond Myklebust90163022007-07-05 14:55:18 -0400605 struct nfs_inode *nfsi = NFS_I(inode);
606 struct nfs_delegation *delegation;
607 int err = 0;
608
Trond Myklebustc57d1bc2012-05-06 19:34:17 -0400609 nfs_wb_all(inode);
Trond Myklebustd25be542013-02-05 11:43:28 -0500610 delegation = nfs_start_delegation_return(nfsi);
611 if (delegation != NULL)
612 err = nfs_end_delegation_return(inode, delegation, 1);
Trond Myklebust90163022007-07-05 14:55:18 -0400613 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614}
615
Trond Myklebustc01d3642018-03-20 16:43:20 -0400616/**
617 * nfs4_inode_make_writeable
618 * @inode: pointer to inode
619 *
620 * Make the inode writeable by returning the delegation if necessary
621 *
622 * Returns zero on success, or a negative errno value.
623 */
624int nfs4_inode_make_writeable(struct inode *inode)
625{
626 if (!nfs4_has_session(NFS_SERVER(inode)->nfs_client) ||
627 !nfs4_check_delegation(inode, FMODE_WRITE))
628 return nfs4_inode_return_delegation(inode);
629 return 0;
630}
631
Trond Myklebustb7571442013-04-03 14:33:49 -0400632static void nfs_mark_return_if_closed_delegation(struct nfs_server *server,
633 struct nfs_delegation *delegation)
634{
635 set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
636 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
637}
638
Trond Myklebusted1e62112011-07-25 15:37:29 -0400639static void nfs_mark_return_delegation(struct nfs_server *server,
640 struct nfs_delegation *delegation)
Trond Myklebust6411bd42008-12-23 15:21:51 -0500641{
642 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
Trond Myklebusted1e62112011-07-25 15:37:29 -0400643 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
Trond Myklebust6411bd42008-12-23 15:21:51 -0500644}
645
Trond Myklebust5c31e232013-04-03 19:04:58 -0400646static bool nfs_server_mark_return_all_delegations(struct nfs_server *server)
647{
648 struct nfs_delegation *delegation;
649 bool ret = false;
650
651 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
652 nfs_mark_return_delegation(server, delegation);
653 ret = true;
654 }
655 return ret;
656}
657
Trond Myklebustb02ba0b2013-04-03 19:23:58 -0400658static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
659{
660 struct nfs_server *server;
661
662 rcu_read_lock();
663 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
664 nfs_server_mark_return_all_delegations(server);
665 rcu_read_unlock();
666}
667
668static void nfs_delegation_run_state_manager(struct nfs_client *clp)
669{
670 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
671 nfs4_schedule_state_manager(clp);
672}
673
674/**
675 * nfs_expire_all_delegations
676 * @clp: client to process
677 *
678 */
679void nfs_expire_all_delegations(struct nfs_client *clp)
680{
681 nfs_client_mark_return_all_delegations(clp);
682 nfs_delegation_run_state_manager(clp);
683}
684
Chuck Leverd3978bb2010-12-24 01:33:04 +0000685/**
686 * nfs_super_return_all_delegations - return delegations for one superblock
Trond Myklebust302fad72019-02-18 13:32:38 -0500687 * @server: pointer to nfs_server to process
Chuck Leverd3978bb2010-12-24 01:33:04 +0000688 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 */
Bryan Schumakereeebf912012-06-20 15:53:41 -0400690void nfs_server_return_all_delegations(struct nfs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000692 struct nfs_client *clp = server->nfs_client;
Trond Myklebust5c31e232013-04-03 19:04:58 -0400693 bool need_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
695 if (clp == NULL)
696 return;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000697
Trond Myklebust8383e462007-07-06 15:12:04 -0400698 rcu_read_lock();
Trond Myklebust5c31e232013-04-03 19:04:58 -0400699 need_wait = nfs_server_mark_return_all_delegations(server);
Trond Myklebust8383e462007-07-06 15:12:04 -0400700 rcu_read_unlock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000701
Trond Myklebust5c31e232013-04-03 19:04:58 -0400702 if (need_wait) {
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500703 nfs4_schedule_state_manager(clp);
Trond Myklebust5c31e232013-04-03 19:04:58 -0400704 nfs4_wait_clnt_recover(clp);
705 }
Trond Myklebust515d8612008-12-23 15:21:46 -0500706}
707
Trond Myklebust826e0012013-04-03 19:27:52 -0400708static void nfs_mark_return_unused_delegation_types(struct nfs_server *server,
Chuck Leverd3978bb2010-12-24 01:33:04 +0000709 fmode_t flags)
Trond Myklebust515d8612008-12-23 15:21:46 -0500710{
711 struct nfs_delegation *delegation;
712
Chuck Leverd3978bb2010-12-24 01:33:04 +0000713 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500714 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
715 continue;
716 if (delegation->type & flags)
Trond Myklebust826e0012013-04-03 19:27:52 -0400717 nfs_mark_return_if_closed_delegation(server, delegation);
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500718 }
Chuck Leverd3978bb2010-12-24 01:33:04 +0000719}
720
Trond Myklebust826e0012013-04-03 19:27:52 -0400721static void nfs_client_mark_return_unused_delegation_types(struct nfs_client *clp,
Chuck Leverd3978bb2010-12-24 01:33:04 +0000722 fmode_t flags)
723{
724 struct nfs_server *server;
725
726 rcu_read_lock();
727 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
Trond Myklebust826e0012013-04-03 19:27:52 -0400728 nfs_mark_return_unused_delegation_types(server, flags);
Trond Myklebust515d8612008-12-23 15:21:46 -0500729 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
731
Trond Myklebust41020b62016-09-22 13:38:58 -0400732static void nfs_mark_delegation_revoked(struct nfs_server *server,
733 struct nfs_delegation *delegation)
Trond Myklebust869f9df2014-11-10 18:43:56 -0500734{
Trond Myklebust41020b62016-09-22 13:38:58 -0400735 set_bit(NFS_DELEGATION_REVOKED, &delegation->flags);
Trond Myklebust059b43e2016-09-22 13:39:06 -0400736 delegation->stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust41020b62016-09-22 13:38:58 -0400737 nfs_mark_return_delegation(server, delegation);
Trond Myklebust869f9df2014-11-10 18:43:56 -0500738}
739
Trond Myklebust41020b62016-09-22 13:38:58 -0400740static bool nfs_revoke_delegation(struct inode *inode,
741 const nfs4_stateid *stateid)
742{
743 struct nfs_delegation *delegation;
Trond Myklebust7f048832016-09-22 13:39:14 -0400744 nfs4_stateid tmp;
Trond Myklebust41020b62016-09-22 13:38:58 -0400745 bool ret = false;
746
747 rcu_read_lock();
748 delegation = rcu_dereference(NFS_I(inode)->delegation);
749 if (delegation == NULL)
750 goto out;
Trond Myklebust7f048832016-09-22 13:39:14 -0400751 if (stateid == NULL) {
752 nfs4_stateid_copy(&tmp, &delegation->stateid);
753 stateid = &tmp;
754 } else if (!nfs4_stateid_match(stateid, &delegation->stateid))
Trond Myklebust41020b62016-09-22 13:38:58 -0400755 goto out;
756 nfs_mark_delegation_revoked(NFS_SERVER(inode), delegation);
757 ret = true;
758out:
759 rcu_read_unlock();
Trond Myklebust7f048832016-09-22 13:39:14 -0400760 if (ret)
761 nfs_inode_find_state_and_recover(inode, stateid);
Trond Myklebust41020b62016-09-22 13:38:58 -0400762 return ret;
763}
764
765void nfs_remove_bad_delegation(struct inode *inode,
766 const nfs4_stateid *stateid)
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500767{
768 struct nfs_delegation *delegation;
769
Trond Myklebust41020b62016-09-22 13:38:58 -0400770 if (!nfs_revoke_delegation(inode, stateid))
771 return;
Trond Myklebustd25be542013-02-05 11:43:28 -0500772 delegation = nfs_inode_detach_delegation(inode);
Trond Myklebust7f048832016-09-22 13:39:14 -0400773 if (delegation)
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500774 nfs_free_delegation(delegation);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500775}
Andy Adamson9cb81962012-03-07 10:49:41 -0500776EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500777
Chuck Leverd3978bb2010-12-24 01:33:04 +0000778/**
Trond Myklebust826e0012013-04-03 19:27:52 -0400779 * nfs_expire_unused_delegation_types
Chuck Leverd3978bb2010-12-24 01:33:04 +0000780 * @clp: client to process
781 * @flags: delegation types to expire
782 *
783 */
Trond Myklebust826e0012013-04-03 19:27:52 -0400784void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags)
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500785{
Trond Myklebust826e0012013-04-03 19:27:52 -0400786 nfs_client_mark_return_unused_delegation_types(clp, flags);
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500787 nfs_delegation_run_state_manager(clp);
788}
789
Chuck Leverd3978bb2010-12-24 01:33:04 +0000790static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
Trond Myklebustb7391f42008-12-23 15:21:52 -0500791{
792 struct nfs_delegation *delegation;
793
Chuck Leverd3978bb2010-12-24 01:33:04 +0000794 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
Trond Myklebustb7391f42008-12-23 15:21:52 -0500795 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
796 continue;
Trond Myklebustb7571442013-04-03 14:33:49 -0400797 nfs_mark_return_if_closed_delegation(server, delegation);
Trond Myklebustb7391f42008-12-23 15:21:52 -0500798 }
Trond Myklebustb7391f42008-12-23 15:21:52 -0500799}
800
Chuck Leverd3978bb2010-12-24 01:33:04 +0000801/**
802 * nfs_expire_unreferenced_delegations - Eliminate unused delegations
803 * @clp: nfs_client to process
804 *
805 */
Trond Myklebustb7391f42008-12-23 15:21:52 -0500806void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
807{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000808 struct nfs_server *server;
809
810 rcu_read_lock();
811 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
812 nfs_mark_return_unreferenced_delegations(server);
813 rcu_read_unlock();
814
Trond Myklebustb7391f42008-12-23 15:21:52 -0500815 nfs_delegation_run_state_manager(clp);
816}
817
Chuck Leverd3978bb2010-12-24 01:33:04 +0000818/**
819 * nfs_async_inode_return_delegation - asynchronously return a delegation
820 * @inode: inode to process
Trond Myklebust8e663f02012-03-04 18:13:56 -0500821 * @stateid: state ID information
Chuck Leverd3978bb2010-12-24 01:33:04 +0000822 *
823 * Returns zero on success, or a negative errno value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 */
Chuck Leverd3978bb2010-12-24 01:33:04 +0000825int nfs_async_inode_return_delegation(struct inode *inode,
826 const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827{
Trond Myklebusted1e62112011-07-25 15:37:29 -0400828 struct nfs_server *server = NFS_SERVER(inode);
829 struct nfs_client *clp = server->nfs_client;
Trond Myklebust6411bd42008-12-23 15:21:51 -0500830 struct nfs_delegation *delegation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Trond Myklebust6411bd42008-12-23 15:21:51 -0500832 rcu_read_lock();
833 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebust755a48a2014-03-02 22:03:12 -0500834 if (delegation == NULL)
835 goto out_enoent;
Trond Myklebust4816fda2015-09-20 14:58:42 -0400836 if (stateid != NULL &&
837 !clp->cl_mvops->match_stateid(&delegation->stateid, stateid))
Trond Myklebust755a48a2014-03-02 22:03:12 -0500838 goto out_enoent;
Trond Myklebusted1e62112011-07-25 15:37:29 -0400839 nfs_mark_return_delegation(server, delegation);
Trond Myklebust6411bd42008-12-23 15:21:51 -0500840 rcu_read_unlock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000841
Trond Myklebust6411bd42008-12-23 15:21:51 -0500842 nfs_delegation_run_state_manager(clp);
843 return 0;
Trond Myklebust755a48a2014-03-02 22:03:12 -0500844out_enoent:
845 rcu_read_unlock();
846 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
848
Chuck Leverd3978bb2010-12-24 01:33:04 +0000849static struct inode *
850nfs_delegation_find_inode_server(struct nfs_server *server,
851 const struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852{
853 struct nfs_delegation *delegation;
Trond Myklebuste39d8a12018-11-13 16:37:54 -0500854 struct inode *freeme, *res = NULL;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000855
856 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
Trond Myklebust86e89482008-12-23 15:21:39 -0500857 spin_lock(&delegation->lock);
858 if (delegation->inode != NULL &&
859 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
Trond Myklebuste39d8a12018-11-13 16:37:54 -0500860 freeme = igrab(delegation->inode);
861 if (freeme && nfs_sb_active(freeme->i_sb))
862 res = freeme;
Trond Myklebust6c342652018-06-07 14:22:00 -0400863 spin_unlock(&delegation->lock);
864 if (res != NULL)
865 return res;
Trond Myklebuste39d8a12018-11-13 16:37:54 -0500866 if (freeme) {
867 rcu_read_unlock();
868 iput(freeme);
869 rcu_read_lock();
870 }
Trond Myklebust6c342652018-06-07 14:22:00 -0400871 return ERR_PTR(-EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 }
Trond Myklebust86e89482008-12-23 15:21:39 -0500873 spin_unlock(&delegation->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
Trond Myklebust6c342652018-06-07 14:22:00 -0400875 return ERR_PTR(-ENOENT);
Chuck Leverd3978bb2010-12-24 01:33:04 +0000876}
877
878/**
879 * nfs_delegation_find_inode - retrieve the inode associated with a delegation
880 * @clp: client state handle
881 * @fhandle: filehandle from a delegation recall
882 *
883 * Returns pointer to inode matching "fhandle," or NULL if a matching inode
884 * cannot be found.
885 */
886struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
887 const struct nfs_fh *fhandle)
888{
889 struct nfs_server *server;
Trond Myklebust6c342652018-06-07 14:22:00 -0400890 struct inode *res;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000891
892 rcu_read_lock();
893 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
894 res = nfs_delegation_find_inode_server(server, fhandle);
Anna Schumakerd5681f52018-06-14 09:39:17 -0400895 if (res != ERR_PTR(-ENOENT)) {
896 rcu_read_unlock();
Trond Myklebust6c342652018-06-07 14:22:00 -0400897 return res;
Anna Schumakerd5681f52018-06-14 09:39:17 -0400898 }
Chuck Leverd3978bb2010-12-24 01:33:04 +0000899 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400900 rcu_read_unlock();
Trond Myklebust6c342652018-06-07 14:22:00 -0400901 return ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902}
903
Chuck Leverd3978bb2010-12-24 01:33:04 +0000904static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
905{
906 struct nfs_delegation *delegation;
907
Trond Myklebust45870d62016-09-22 13:38:59 -0400908 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
909 /*
910 * If the delegation may have been admin revoked, then we
911 * cannot reclaim it.
912 */
913 if (test_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags))
914 continue;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000915 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
Trond Myklebust45870d62016-09-22 13:38:59 -0400916 }
Chuck Leverd3978bb2010-12-24 01:33:04 +0000917}
918
919/**
920 * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
921 * @clp: nfs_client to process
922 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 */
David Howellsadfa6f92006-08-22 20:06:08 -0400924void nfs_delegation_mark_reclaim(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000926 struct nfs_server *server;
927
Trond Myklebust8383e462007-07-06 15:12:04 -0400928 rcu_read_lock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000929 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
930 nfs_delegation_mark_reclaim_server(server);
Trond Myklebust8383e462007-07-06 15:12:04 -0400931 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932}
933
Chuck Leverd3978bb2010-12-24 01:33:04 +0000934/**
935 * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
936 * @clp: nfs_client to process
937 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 */
David Howellsadfa6f92006-08-22 20:06:08 -0400939void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
Trond Myklebust8383e462007-07-06 15:12:04 -0400941 struct nfs_delegation *delegation;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000942 struct nfs_server *server;
Trond Myklebust86e89482008-12-23 15:21:39 -0500943 struct inode *inode;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000944
Trond Myklebust8383e462007-07-06 15:12:04 -0400945restart:
946 rcu_read_lock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000947 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
948 list_for_each_entry_rcu(delegation, &server->delegations,
949 super_list) {
Trond Myklebust6f9449b2019-02-21 14:51:25 -0500950 if (test_bit(NFS_DELEGATION_INODE_FREEING,
951 &delegation->flags) ||
952 test_bit(NFS_DELEGATION_RETURNING,
953 &delegation->flags) ||
954 test_bit(NFS_DELEGATION_NEED_RECLAIM,
Chuck Leverd3978bb2010-12-24 01:33:04 +0000955 &delegation->flags) == 0)
956 continue;
Trond Myklebust9f0f8e12015-02-26 09:57:34 -0500957 if (!nfs_sb_active(server->super))
NeilBrownf38934912018-05-31 15:23:22 +1000958 break; /* continue in outer loop */
Trond Myklebust9f0f8e12015-02-26 09:57:34 -0500959 inode = nfs_delegation_grab_inode(delegation);
960 if (inode == NULL) {
961 rcu_read_unlock();
962 nfs_sb_deactive(server->super);
963 goto restart;
964 }
Trond Myklebustb04b22f2015-02-26 13:59:38 -0500965 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
Chuck Leverd3978bb2010-12-24 01:33:04 +0000966 rcu_read_unlock();
Trond Myklebustb04b22f2015-02-26 13:59:38 -0500967 if (delegation != NULL) {
968 delegation = nfs_detach_delegation(NFS_I(inode),
969 delegation, server);
970 if (delegation != NULL)
971 nfs_free_delegation(delegation);
972 }
Chuck Leverd3978bb2010-12-24 01:33:04 +0000973 iput(inode);
Trond Myklebust9f0f8e12015-02-26 09:57:34 -0500974 nfs_sb_deactive(server->super);
NeilBrown3ca951b2018-04-30 14:31:30 +1000975 cond_resched();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000976 goto restart;
977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400979 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980}
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500981
Trond Myklebustbb3d1a32016-09-22 13:39:00 -0400982static inline bool nfs4_server_rebooted(const struct nfs_client *clp)
983{
984 return (clp->cl_state & (BIT(NFS4CLNT_CHECK_LEASE) |
985 BIT(NFS4CLNT_LEASE_EXPIRED) |
986 BIT(NFS4CLNT_SESSION_RESET))) != 0;
987}
988
Trond Myklebust45870d62016-09-22 13:38:59 -0400989static void nfs_mark_test_expired_delegation(struct nfs_server *server,
990 struct nfs_delegation *delegation)
991{
Trond Myklebust059b43e2016-09-22 13:39:06 -0400992 if (delegation->stateid.type == NFS4_INVALID_STATEID_TYPE)
993 return;
Trond Myklebust45870d62016-09-22 13:38:59 -0400994 clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
995 set_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
996 set_bit(NFS4CLNT_DELEGATION_EXPIRED, &server->nfs_client->cl_state);
997}
998
Trond Myklebustbb3d1a32016-09-22 13:39:00 -0400999static void nfs_inode_mark_test_expired_delegation(struct nfs_server *server,
1000 struct inode *inode)
1001{
1002 struct nfs_delegation *delegation;
1003
1004 rcu_read_lock();
1005 delegation = rcu_dereference(NFS_I(inode)->delegation);
1006 if (delegation)
1007 nfs_mark_test_expired_delegation(server, delegation);
1008 rcu_read_unlock();
1009
1010}
1011
Trond Myklebust45870d62016-09-22 13:38:59 -04001012static void nfs_delegation_mark_test_expired_server(struct nfs_server *server)
1013{
1014 struct nfs_delegation *delegation;
1015
1016 list_for_each_entry_rcu(delegation, &server->delegations, super_list)
1017 nfs_mark_test_expired_delegation(server, delegation);
1018}
1019
1020/**
1021 * nfs_mark_test_expired_all_delegations - mark all delegations for testing
1022 * @clp: nfs_client to process
1023 *
1024 * Iterates through all the delegations associated with this server and
1025 * marks them as needing to be checked for validity.
1026 */
1027void nfs_mark_test_expired_all_delegations(struct nfs_client *clp)
1028{
1029 struct nfs_server *server;
1030
1031 rcu_read_lock();
1032 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1033 nfs_delegation_mark_test_expired_server(server);
1034 rcu_read_unlock();
1035}
1036
1037/**
Scott Mayhew8ca017c2019-05-06 11:59:05 -04001038 * nfs_test_expired_all_delegations - test all delegations for a client
1039 * @clp: nfs_client to process
1040 *
1041 * Helper for handling "recallable state revoked" status from server.
1042 */
1043void nfs_test_expired_all_delegations(struct nfs_client *clp)
1044{
1045 nfs_mark_test_expired_all_delegations(clp);
1046 nfs4_schedule_state_manager(clp);
1047}
1048
1049/**
Trond Myklebust45870d62016-09-22 13:38:59 -04001050 * nfs_reap_expired_delegations - reap expired delegations
1051 * @clp: nfs_client to process
1052 *
1053 * Iterates through all the delegations associated with this server and
1054 * checks if they have may have been revoked. This function is usually
1055 * expected to be called in cases where the server may have lost its
1056 * lease.
1057 */
1058void nfs_reap_expired_delegations(struct nfs_client *clp)
1059{
1060 const struct nfs4_minor_version_ops *ops = clp->cl_mvops;
1061 struct nfs_delegation *delegation;
1062 struct nfs_server *server;
1063 struct inode *inode;
NeilBrowna52458b2018-12-03 11:30:31 +11001064 const struct cred *cred;
Trond Myklebust45870d62016-09-22 13:38:59 -04001065 nfs4_stateid stateid;
1066
1067restart:
1068 rcu_read_lock();
1069 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1070 list_for_each_entry_rcu(delegation, &server->delegations,
1071 super_list) {
Trond Myklebust6f9449b2019-02-21 14:51:25 -05001072 if (test_bit(NFS_DELEGATION_INODE_FREEING,
1073 &delegation->flags) ||
1074 test_bit(NFS_DELEGATION_RETURNING,
1075 &delegation->flags) ||
1076 test_bit(NFS_DELEGATION_TEST_EXPIRED,
Trond Myklebust45870d62016-09-22 13:38:59 -04001077 &delegation->flags) == 0)
1078 continue;
1079 if (!nfs_sb_active(server->super))
NeilBrownf38934912018-05-31 15:23:22 +10001080 break; /* continue in outer loop */
Trond Myklebust45870d62016-09-22 13:38:59 -04001081 inode = nfs_delegation_grab_inode(delegation);
1082 if (inode == NULL) {
1083 rcu_read_unlock();
1084 nfs_sb_deactive(server->super);
1085 goto restart;
1086 }
NeilBrowna52458b2018-12-03 11:30:31 +11001087 cred = get_cred_rcu(delegation->cred);
Trond Myklebust45870d62016-09-22 13:38:59 -04001088 nfs4_stateid_copy(&stateid, &delegation->stateid);
1089 clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
1090 rcu_read_unlock();
1091 if (cred != NULL &&
1092 ops->test_and_free_expired(server, &stateid, cred) < 0) {
1093 nfs_revoke_delegation(inode, &stateid);
1094 nfs_inode_find_state_and_recover(inode, &stateid);
1095 }
NeilBrowna52458b2018-12-03 11:30:31 +11001096 put_cred(cred);
Trond Myklebustbb3d1a32016-09-22 13:39:00 -04001097 if (nfs4_server_rebooted(clp)) {
1098 nfs_inode_mark_test_expired_delegation(server,inode);
1099 iput(inode);
1100 nfs_sb_deactive(server->super);
1101 return;
1102 }
Trond Myklebust45870d62016-09-22 13:38:59 -04001103 iput(inode);
1104 nfs_sb_deactive(server->super);
NeilBrown3ca951b2018-04-30 14:31:30 +10001105 cond_resched();
Trond Myklebust45870d62016-09-22 13:38:59 -04001106 goto restart;
1107 }
1108 }
1109 rcu_read_unlock();
1110}
1111
Trond Myklebust6c2d8f82016-09-22 13:39:07 -04001112void nfs_inode_find_delegation_state_and_recover(struct inode *inode,
1113 const nfs4_stateid *stateid)
1114{
1115 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1116 struct nfs_delegation *delegation;
1117 bool found = false;
1118
1119 rcu_read_lock();
1120 delegation = rcu_dereference(NFS_I(inode)->delegation);
1121 if (delegation &&
1122 nfs4_stateid_match_other(&delegation->stateid, stateid)) {
1123 nfs_mark_test_expired_delegation(NFS_SERVER(inode), delegation);
1124 found = true;
1125 }
1126 rcu_read_unlock();
1127 if (found)
1128 nfs4_schedule_state_manager(clp);
1129}
1130
Chuck Leverd3978bb2010-12-24 01:33:04 +00001131/**
1132 * nfs_delegations_present - check for existence of delegations
1133 * @clp: client state handle
1134 *
1135 * Returns one if there are any nfs_delegation structures attached
1136 * to this nfs_client.
1137 */
1138int nfs_delegations_present(struct nfs_client *clp)
1139{
1140 struct nfs_server *server;
1141 int ret = 0;
1142
1143 rcu_read_lock();
1144 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1145 if (!list_empty(&server->delegations)) {
1146 ret = 1;
1147 break;
1148 }
1149 rcu_read_unlock();
1150 return ret;
1151}
1152
1153/**
Trond Myklebust12f275c2017-11-06 15:28:05 -05001154 * nfs4_refresh_delegation_stateid - Update delegation stateid seqid
1155 * @dst: stateid to refresh
1156 * @inode: inode to check
1157 *
1158 * Returns "true" and updates "dst->seqid" * if inode had a delegation
1159 * that matches our delegation stateid. Otherwise "false" is returned.
1160 */
1161bool nfs4_refresh_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
1162{
1163 struct nfs_delegation *delegation;
1164 bool ret = false;
1165 if (!inode)
1166 goto out;
1167
1168 rcu_read_lock();
1169 delegation = rcu_dereference(NFS_I(inode)->delegation);
1170 if (delegation != NULL &&
1171 nfs4_stateid_match_other(dst, &delegation->stateid)) {
1172 dst->seqid = delegation->stateid.seqid;
1173 return ret;
1174 }
1175 rcu_read_unlock();
1176out:
1177 return ret;
1178}
1179
1180/**
Chuck Leverd3978bb2010-12-24 01:33:04 +00001181 * nfs4_copy_delegation_stateid - Copy inode's state ID information
Chuck Leverd3978bb2010-12-24 01:33:04 +00001182 * @inode: inode to check
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001183 * @flags: delegation type requirement
Trond Myklebustabf4e132016-05-16 17:42:44 -04001184 * @dst: stateid data structure to fill in
1185 * @cred: optional argument to retrieve credential
Chuck Leverd3978bb2010-12-24 01:33:04 +00001186 *
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001187 * Returns "true" and fills in "dst->data" * if inode had a delegation,
1188 * otherwise "false" is returned.
Chuck Leverd3978bb2010-12-24 01:33:04 +00001189 */
Trond Myklebustabf4e132016-05-16 17:42:44 -04001190bool nfs4_copy_delegation_stateid(struct inode *inode, fmode_t flags,
NeilBrowna52458b2018-12-03 11:30:31 +11001191 nfs4_stateid *dst, const struct cred **cred)
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001192{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001193 struct nfs_inode *nfsi = NFS_I(inode);
1194 struct nfs_delegation *delegation;
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001195 bool ret;
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001196
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001197 flags &= FMODE_READ|FMODE_WRITE;
Trond Myklebust8383e462007-07-06 15:12:04 -04001198 rcu_read_lock();
1199 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebustaa05c872016-09-22 13:38:54 -04001200 ret = nfs4_is_valid_delegation(delegation, flags);
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001201 if (ret) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001202 nfs4_stateid_copy(dst, &delegation->stateid);
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001203 nfs_mark_delegation_referenced(delegation);
Trond Myklebustabf4e132016-05-16 17:42:44 -04001204 if (cred)
NeilBrowna52458b2018-12-03 11:30:31 +11001205 *cred = get_cred(delegation->cred);
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001206 }
Trond Myklebust8383e462007-07-06 15:12:04 -04001207 rcu_read_unlock();
1208 return ret;
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001209}
Trond Myklebust5445b1f2015-09-05 19:06:58 -04001210
1211/**
1212 * nfs4_delegation_flush_on_close - Check if we must flush file on close
1213 * @inode: inode to check
1214 *
1215 * This function checks the number of outstanding writes to the file
1216 * against the delegation 'space_limit' field to see if
1217 * the spec requires us to flush the file on close.
1218 */
1219bool nfs4_delegation_flush_on_close(const struct inode *inode)
1220{
1221 struct nfs_inode *nfsi = NFS_I(inode);
1222 struct nfs_delegation *delegation;
1223 bool ret = true;
1224
1225 rcu_read_lock();
1226 delegation = rcu_dereference(nfsi->delegation);
1227 if (delegation == NULL || !(delegation->type & FMODE_WRITE))
1228 goto out;
Trond Myklebusta6b6d5b2017-08-01 15:39:46 -04001229 if (atomic_long_read(&nfsi->nrequests) < delegation->pagemod_limit)
Trond Myklebust5445b1f2015-09-05 19:06:58 -04001230 ret = false;
1231out:
1232 rcu_read_unlock();
1233 return ret;
1234}