blob: 70e5931f3c604e92b1d82a0e4ec08229d8af31c3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * linux/fs/nfs/delegation.c
3 *
4 * Copyright (C) 2004 Trond Myklebust
5 *
6 * NFS file delegation management
7 *
8 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/completion.h>
Trond Myklebust58d97142006-01-03 09:55:24 +010010#include <linux/kthread.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/module.h>
12#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/spinlock.h>
Jeff Layton1eb5d982018-01-09 08:21:17 -050015#include <linux/iversion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17#include <linux/nfs4.h>
18#include <linux/nfs_fs.h>
19#include <linux/nfs_xdr.h>
20
Trond Myklebust4ce79712005-06-22 17:16:21 +000021#include "nfs4_fs.h"
Trond Myklebustc01d3642018-03-20 16:43:20 -040022#include "nfs4session.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "delegation.h"
David Howells24c8dbb2006-08-22 20:06:10 -040024#include "internal.h"
Trond Myklebustca8acf82013-08-13 10:36:56 -040025#include "nfs4trace.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Trond Myklebust905f8d12007-08-06 12:18:34 -040027static void nfs_free_delegation(struct nfs_delegation *delegation)
28{
NeilBrowna52458b2018-12-03 11:30:31 +110029 put_cred(delegation->cred);
30 delegation->cred = NULL;
Lai Jiangshan26f04dd2011-05-01 06:21:54 -070031 kfree_rcu(delegation, rcu);
Trond Myklebust8383e462007-07-06 15:12:04 -040032}
33
Chuck Leverd3978bb2010-12-24 01:33:04 +000034/**
35 * nfs_mark_delegation_referenced - set delegation's REFERENCED flag
36 * @delegation: delegation to process
37 *
38 */
Trond Myklebustb7391f42008-12-23 15:21:52 -050039void nfs_mark_delegation_referenced(struct nfs_delegation *delegation)
40{
41 set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags);
42}
43
Trond Myklebustaa05c872016-09-22 13:38:54 -040044static bool
45nfs4_is_valid_delegation(const struct nfs_delegation *delegation,
46 fmode_t flags)
47{
48 if (delegation != NULL && (delegation->type & flags) == flags &&
49 !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) &&
50 !test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
51 return true;
52 return false;
53}
54
Peng Tao15bb3af2014-07-03 13:05:00 +080055static int
56nfs4_do_check_delegation(struct inode *inode, fmode_t flags, bool mark)
Trond Myklebustb7391f42008-12-23 15:21:52 -050057{
58 struct nfs_delegation *delegation;
59 int ret = 0;
60
61 flags &= FMODE_READ|FMODE_WRITE;
62 rcu_read_lock();
63 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustaa05c872016-09-22 13:38:54 -040064 if (nfs4_is_valid_delegation(delegation, flags)) {
Peng Tao15bb3af2014-07-03 13:05:00 +080065 if (mark)
66 nfs_mark_delegation_referenced(delegation);
Trond Myklebustb7391f42008-12-23 15:21:52 -050067 ret = 1;
68 }
69 rcu_read_unlock();
70 return ret;
71}
Peng Tao15bb3af2014-07-03 13:05:00 +080072/**
73 * nfs_have_delegation - check if inode has a delegation, mark it
74 * NFS_DELEGATION_REFERENCED if there is one.
75 * @inode: inode to check
76 * @flags: delegation types to check for
77 *
78 * Returns one if inode has the indicated delegation, otherwise zero.
79 */
80int nfs4_have_delegation(struct inode *inode, fmode_t flags)
81{
82 return nfs4_do_check_delegation(inode, flags, true);
83}
84
85/*
86 * nfs4_check_delegation - check if inode has a delegation, do not mark
87 * NFS_DELEGATION_REFERENCED if it has one.
88 */
89int nfs4_check_delegation(struct inode *inode, fmode_t flags)
90{
91 return nfs4_do_check_delegation(inode, flags, false);
92}
Trond Myklebustb7391f42008-12-23 15:21:52 -050093
Olga Kornievskaia44f411c2018-10-04 14:45:00 -040094static int nfs_delegation_claim_locks(struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -050095{
96 struct inode *inode = state->inode;
97 struct file_lock *fl;
Jeff Laytonbd61e0a2015-01-16 15:05:55 -050098 struct file_lock_context *flctx = inode->i_flctx;
99 struct list_head *list;
Trond Myklebustd5122202009-06-17 13:22:58 -0700100 int status = 0;
Trond Myklebust888e6942005-11-04 15:38:11 -0500101
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500102 if (flctx == NULL)
Trond Myklebust65b62a22013-02-07 10:54:07 -0500103 goto out;
Jeff Layton314d7cc2013-04-10 15:36:48 -0400104
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500105 list = &flctx->flc_posix;
Jeff Layton6109c852015-01-16 15:05:57 -0500106 spin_lock(&flctx->flc_lock);
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500107restart:
108 list_for_each_entry(fl, list, fl_list) {
Olga Kornievskaia44f411c2018-10-04 14:45:00 -0400109 if (nfs_file_open_context(fl->fl_file)->state != state)
Trond Myklebust888e6942005-11-04 15:38:11 -0500110 continue;
Jeff Layton6109c852015-01-16 15:05:57 -0500111 spin_unlock(&flctx->flc_lock);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -0400112 status = nfs4_lock_delegation_recall(fl, state, stateid);
Trond Myklebustd5122202009-06-17 13:22:58 -0700113 if (status < 0)
Trond Myklebust3f09df72009-06-17 13:23:00 -0700114 goto out;
Jeff Layton6109c852015-01-16 15:05:57 -0500115 spin_lock(&flctx->flc_lock);
Trond Myklebust888e6942005-11-04 15:38:11 -0500116 }
Jeff Laytonbd61e0a2015-01-16 15:05:55 -0500117 if (list == &flctx->flc_posix) {
118 list = &flctx->flc_flock;
119 goto restart;
Jeff Layton5263e312015-01-16 15:05:55 -0500120 }
Jeff Layton6109c852015-01-16 15:05:57 -0500121 spin_unlock(&flctx->flc_lock);
Trond Myklebust3f09df72009-06-17 13:23:00 -0700122out:
Trond Myklebust888e6942005-11-04 15:38:11 -0500123 return status;
124}
125
Trond Myklebust24311f82015-09-20 10:50:17 -0400126static int nfs_delegation_claim_opens(struct inode *inode,
127 const nfs4_stateid *stateid, fmode_t type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
129 struct nfs_inode *nfsi = NFS_I(inode);
130 struct nfs_open_context *ctx;
Trond Myklebustd25be542013-02-05 11:43:28 -0500131 struct nfs4_state_owner *sp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 struct nfs4_state *state;
Trond Myklebustd25be542013-02-05 11:43:28 -0500133 unsigned int seq;
Trond Myklebust888e6942005-11-04 15:38:11 -0500134 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135
136again:
Trond Myklebust0de43972018-09-02 15:57:01 -0400137 rcu_read_lock();
138 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 state = ctx->state;
140 if (state == NULL)
141 continue;
142 if (!test_bit(NFS_DELEGATED_STATE, &state->flags))
143 continue;
Trond Myklebustf8ebf7a2014-10-17 23:02:52 +0300144 if (!nfs4_valid_open_stateid(state))
145 continue;
Trond Myklebustf597c532012-03-04 18:13:56 -0500146 if (!nfs4_stateid_match(&state->stateid, stateid))
Trond Myklebust90163022007-07-05 14:55:18 -0400147 continue;
Trond Myklebust0de43972018-09-02 15:57:01 -0400148 if (!get_nfs_open_context(ctx))
149 continue;
150 rcu_read_unlock();
Trond Myklebustd25be542013-02-05 11:43:28 -0500151 sp = state->owner;
Trond Myklebust65b62a22013-02-07 10:54:07 -0500152 /* Block nfs4_proc_unlck */
153 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebustd25be542013-02-05 11:43:28 -0500154 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
Trond Myklebust24311f82015-09-20 10:50:17 -0400155 err = nfs4_open_delegation_recall(ctx, state, stateid, type);
Trond Myklebustd25be542013-02-05 11:43:28 -0500156 if (!err)
Olga Kornievskaia44f411c2018-10-04 14:45:00 -0400157 err = nfs_delegation_claim_locks(state, stateid);
Trond Myklebustd25be542013-02-05 11:43:28 -0500158 if (!err && read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
159 err = -EAGAIN;
Trond Myklebust65b62a22013-02-07 10:54:07 -0500160 mutex_unlock(&sp->so_delegreturn_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 put_nfs_open_context(ctx);
Trond Myklebust888e6942005-11-04 15:38:11 -0500162 if (err != 0)
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500163 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 goto again;
165 }
Trond Myklebust0de43972018-09-02 15:57:01 -0400166 rcu_read_unlock();
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500167 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
Chuck Leverd3978bb2010-12-24 01:33:04 +0000170/**
171 * nfs_inode_reclaim_delegation - process a delegation reclaim request
172 * @inode: inode to process
173 * @cred: credential to use for request
Trond Myklebust35156bf2018-03-20 17:03:13 -0400174 * @type: delegation type
175 * @stateid: delegation stateid
176 * @pagemod_limit: write delegation "space_limit"
Chuck Leverd3978bb2010-12-24 01:33:04 +0000177 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 */
NeilBrowna52458b2018-12-03 11:30:31 +1100179void nfs_inode_reclaim_delegation(struct inode *inode, const struct cred *cred,
Trond Myklebust35156bf2018-03-20 17:03:13 -0400180 fmode_t type,
181 const nfs4_stateid *stateid,
182 unsigned long pagemod_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Trond Myklebust8f649c32010-05-01 12:36:18 -0400184 struct nfs_delegation *delegation;
NeilBrowna52458b2018-12-03 11:30:31 +1100185 const struct cred *oldcred = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Trond Myklebust8f649c32010-05-01 12:36:18 -0400187 rcu_read_lock();
188 delegation = rcu_dereference(NFS_I(inode)->delegation);
189 if (delegation != NULL) {
190 spin_lock(&delegation->lock);
191 if (delegation->inode != NULL) {
Trond Myklebust35156bf2018-03-20 17:03:13 -0400192 nfs4_stateid_copy(&delegation->stateid, stateid);
193 delegation->type = type;
194 delegation->pagemod_limit = pagemod_limit;
Trond Myklebust8f649c32010-05-01 12:36:18 -0400195 oldcred = delegation->cred;
NeilBrowna52458b2018-12-03 11:30:31 +1100196 delegation->cred = get_cred(cred);
Trond Myklebust8f649c32010-05-01 12:36:18 -0400197 clear_bit(NFS_DELEGATION_NEED_RECLAIM,
198 &delegation->flags);
Trond Myklebust8f649c32010-05-01 12:36:18 -0400199 spin_unlock(&delegation->lock);
Trond Myklebust8f649c32010-05-01 12:36:18 -0400200 rcu_read_unlock();
NeilBrowna52458b2018-12-03 11:30:31 +1100201 put_cred(oldcred);
Trond Myklebust35156bf2018-03-20 17:03:13 -0400202 trace_nfs4_reclaim_delegation(inode, type);
Trond Myklebustb1a318d2016-09-22 13:39:12 -0400203 return;
Trond Myklebust8f649c32010-05-01 12:36:18 -0400204 }
Trond Myklebustb1a318d2016-09-22 13:39:12 -0400205 /* We appear to have raced with a delegation return. */
206 spin_unlock(&delegation->lock);
Trond Myklebust8f649c32010-05-01 12:36:18 -0400207 }
Trond Myklebustb1a318d2016-09-22 13:39:12 -0400208 rcu_read_unlock();
Trond Myklebust35156bf2018-03-20 17:03:13 -0400209 nfs_inode_set_delegation(inode, cred, type, stateid, pagemod_limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210}
211
Trond Myklebust57bfa892008-01-25 16:38:18 -0500212static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync)
213{
214 int res = 0;
215
Trond Myklebust869f9df2014-11-10 18:43:56 -0500216 if (!test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
217 res = nfs4_proc_delegreturn(inode,
218 delegation->cred,
219 &delegation->stateid,
220 issync);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500221 nfs_free_delegation(delegation);
222 return res;
223}
224
Trond Myklebust86e89482008-12-23 15:21:39 -0500225static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation)
226{
227 struct inode *inode = NULL;
228
229 spin_lock(&delegation->lock);
230 if (delegation->inode != NULL)
231 inode = igrab(delegation->inode);
232 spin_unlock(&delegation->lock);
233 return inode;
234}
235
Chuck Leverdda4b222010-12-24 01:32:54 +0000236static struct nfs_delegation *
Trond Myklebustd25be542013-02-05 11:43:28 -0500237nfs_start_delegation_return_locked(struct nfs_inode *nfsi)
Trond Myklebust57bfa892008-01-25 16:38:18 -0500238{
Trond Myklebustd25be542013-02-05 11:43:28 -0500239 struct nfs_delegation *ret = NULL;
240 struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500241
242 if (delegation == NULL)
Trond Myklebustd25be542013-02-05 11:43:28 -0500243 goto out;
244 spin_lock(&delegation->lock);
245 if (!test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
246 ret = delegation;
247 spin_unlock(&delegation->lock);
248out:
249 return ret;
250}
251
252static struct nfs_delegation *
253nfs_start_delegation_return(struct nfs_inode *nfsi)
254{
255 struct nfs_delegation *delegation;
256
257 rcu_read_lock();
258 delegation = nfs_start_delegation_return_locked(nfsi);
259 rcu_read_unlock();
260 return delegation;
261}
262
263static void
264nfs_abort_delegation_return(struct nfs_delegation *delegation,
265 struct nfs_client *clp)
266{
Chuck Leverdda4b222010-12-24 01:32:54 +0000267
Trond Myklebust34310432008-12-23 15:21:38 -0500268 spin_lock(&delegation->lock);
Trond Myklebustd25be542013-02-05 11:43:28 -0500269 clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
270 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
271 spin_unlock(&delegation->lock);
272 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
273}
274
275static struct nfs_delegation *
276nfs_detach_delegation_locked(struct nfs_inode *nfsi,
277 struct nfs_delegation *delegation,
278 struct nfs_client *clp)
279{
280 struct nfs_delegation *deleg_cur =
281 rcu_dereference_protected(nfsi->delegation,
282 lockdep_is_held(&clp->cl_lock));
283
284 if (deleg_cur == NULL || delegation != deleg_cur)
285 return NULL;
286
287 spin_lock(&delegation->lock);
288 set_bit(NFS_DELEGATION_RETURNING, &delegation->flags);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500289 list_del_rcu(&delegation->super_list);
Trond Myklebust86e89482008-12-23 15:21:39 -0500290 delegation->inode = NULL;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500291 rcu_assign_pointer(nfsi->delegation, NULL);
Trond Myklebust34310432008-12-23 15:21:38 -0500292 spin_unlock(&delegation->lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500293 return delegation;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500294}
295
Chuck Leverdda4b222010-12-24 01:32:54 +0000296static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi,
Trond Myklebustd25be542013-02-05 11:43:28 -0500297 struct nfs_delegation *delegation,
298 struct nfs_server *server)
Chuck Leverdda4b222010-12-24 01:32:54 +0000299{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000300 struct nfs_client *clp = server->nfs_client;
Chuck Leverdda4b222010-12-24 01:32:54 +0000301
302 spin_lock(&clp->cl_lock);
Trond Myklebustd25be542013-02-05 11:43:28 -0500303 delegation = nfs_detach_delegation_locked(nfsi, delegation, clp);
Chuck Leverdda4b222010-12-24 01:32:54 +0000304 spin_unlock(&clp->cl_lock);
305 return delegation;
306}
307
Trond Myklebustd25be542013-02-05 11:43:28 -0500308static struct nfs_delegation *
309nfs_inode_detach_delegation(struct inode *inode)
310{
311 struct nfs_inode *nfsi = NFS_I(inode);
312 struct nfs_server *server = NFS_SERVER(inode);
313 struct nfs_delegation *delegation;
314
315 delegation = nfs_start_delegation_return(nfsi);
316 if (delegation == NULL)
317 return NULL;
318 return nfs_detach_delegation(nfsi, delegation, server);
319}
320
Trond Myklebustcf6726e2014-12-19 11:22:28 -0500321static void
322nfs_update_inplace_delegation(struct nfs_delegation *delegation,
323 const struct nfs_delegation *update)
324{
325 if (nfs4_stateid_is_newer(&update->stateid, &delegation->stateid)) {
326 delegation->stateid.seqid = update->stateid.seqid;
327 smp_wmb();
328 delegation->type = update->type;
329 }
330}
331
Chuck Leverd3978bb2010-12-24 01:33:04 +0000332/**
333 * nfs_inode_set_delegation - set up a delegation on an inode
334 * @inode: inode to which delegation applies
335 * @cred: cred to use for subsequent delegation processing
Trond Myklebust35156bf2018-03-20 17:03:13 -0400336 * @type: delegation type
337 * @stateid: delegation stateid
338 * @pagemod_limit: write delegation "space_limit"
Chuck Leverd3978bb2010-12-24 01:33:04 +0000339 *
340 * Returns zero on success, or a negative errno value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 */
NeilBrowna52458b2018-12-03 11:30:31 +1100342int nfs_inode_set_delegation(struct inode *inode, const struct cred *cred,
Trond Myklebust35156bf2018-03-20 17:03:13 -0400343 fmode_t type,
344 const nfs4_stateid *stateid,
345 unsigned long pagemod_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000347 struct nfs_server *server = NFS_SERVER(inode);
348 struct nfs_client *clp = server->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 struct nfs_inode *nfsi = NFS_I(inode);
David Howells17d2c0a2010-05-01 12:37:18 -0400350 struct nfs_delegation *delegation, *old_delegation;
Trond Myklebust57bfa892008-01-25 16:38:18 -0500351 struct nfs_delegation *freeme = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 int status = 0;
353
Trond Myklebust8535b2b2010-05-13 12:51:01 -0400354 delegation = kmalloc(sizeof(*delegation), GFP_NOFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 if (delegation == NULL)
356 return -ENOMEM;
Trond Myklebust35156bf2018-03-20 17:03:13 -0400357 nfs4_stateid_copy(&delegation->stateid, stateid);
358 delegation->type = type;
359 delegation->pagemod_limit = pagemod_limit;
Jeff Layton1eb5d982018-01-09 08:21:17 -0500360 delegation->change_attr = inode_peek_iversion_raw(inode);
NeilBrowna52458b2018-12-03 11:30:31 +1100361 delegation->cred = get_cred(cred);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 delegation->inode = inode;
Trond Myklebustb7391f42008-12-23 15:21:52 -0500363 delegation->flags = 1<<NFS_DELEGATION_REFERENCED;
Trond Myklebust34310432008-12-23 15:21:38 -0500364 spin_lock_init(&delegation->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
366 spin_lock(&clp->cl_lock);
David Howells17d2c0a2010-05-01 12:37:18 -0400367 old_delegation = rcu_dereference_protected(nfsi->delegation,
Chuck Leverd3978bb2010-12-24 01:33:04 +0000368 lockdep_is_held(&clp->cl_lock));
David Howells17d2c0a2010-05-01 12:37:18 -0400369 if (old_delegation != NULL) {
Trond Myklebustcf6726e2014-12-19 11:22:28 -0500370 /* Is this an update of the existing delegation? */
371 if (nfs4_stateid_match_other(&old_delegation->stateid,
372 &delegation->stateid)) {
373 nfs_update_inplace_delegation(old_delegation,
374 delegation);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500375 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
Trond Myklebust57bfa892008-01-25 16:38:18 -0500377 /*
378 * Deal with broken servers that hand out two
379 * delegations for the same file.
Trond Myklebust17280172012-03-11 13:11:00 -0400380 * Allow for upgrades to a WRITE delegation, but
381 * nothing else.
Trond Myklebust57bfa892008-01-25 16:38:18 -0500382 */
383 dfprintk(FILE, "%s: server %s handed out "
384 "a duplicate delegation!\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700385 __func__, clp->cl_hostname);
Trond Myklebust17280172012-03-11 13:11:00 -0400386 if (delegation->type == old_delegation->type ||
387 !(delegation->type & FMODE_WRITE)) {
Trond Myklebust57bfa892008-01-25 16:38:18 -0500388 freeme = delegation;
389 delegation = NULL;
390 goto out;
391 }
Trond Myklebustade04642015-02-27 14:25:50 -0500392 if (test_and_set_bit(NFS_DELEGATION_RETURNING,
393 &old_delegation->flags))
394 goto out;
395 freeme = nfs_detach_delegation_locked(nfsi,
Trond Myklebustd25be542013-02-05 11:43:28 -0500396 old_delegation, clp);
397 if (freeme == NULL)
398 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
Trond Myklebust38942ba2015-03-04 15:59:05 -0500400 list_add_tail_rcu(&delegation->super_list, &server->delegations);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500401 rcu_assign_pointer(nfsi->delegation, delegation);
402 delegation = NULL;
Trond Myklebust412c77c2007-07-03 16:10:55 -0400403
Trond Myklebust35156bf2018-03-20 17:03:13 -0400404 trace_nfs4_set_delegation(inode, type);
Trond Myklebust412c77c2007-07-03 16:10:55 -0400405
Trond Myklebust97c2c172018-04-07 18:43:17 -0400406 spin_lock(&inode->i_lock);
407 if (NFS_I(inode)->cache_validity & (NFS_INO_INVALID_ATTR|NFS_INO_INVALID_ATIME))
408 NFS_I(inode)->cache_validity |= NFS_INO_REVAL_FORCED;
409 spin_unlock(&inode->i_lock);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500410out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 spin_unlock(&clp->cl_lock);
Trond Myklebust603c83d2007-10-18 19:59:20 -0400412 if (delegation != NULL)
413 nfs_free_delegation(delegation);
Trond Myklebust57bfa892008-01-25 16:38:18 -0500414 if (freeme != NULL)
415 nfs_do_return_delegation(inode, freeme, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 return status;
417}
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419/*
420 * Basic procedure for returning a delegation to the server
421 */
Trond Myklebustd25be542013-02-05 11:43:28 -0500422static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
Trond Myklebustd25be542013-02-05 11:43:28 -0500424 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust869f9df2014-11-10 18:43:56 -0500426 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Trond Myklebustd25be542013-02-05 11:43:28 -0500428 if (delegation == NULL)
429 return 0;
430 do {
Trond Myklebust869f9df2014-11-10 18:43:56 -0500431 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags))
432 break;
Trond Myklebust24311f82015-09-20 10:50:17 -0400433 err = nfs_delegation_claim_opens(inode, &delegation->stateid,
434 delegation->type);
Trond Myklebustd25be542013-02-05 11:43:28 -0500435 if (!issync || err != -EAGAIN)
436 break;
437 /*
438 * Guard against state recovery
439 */
440 err = nfs4_wait_clnt_recover(clp);
441 } while (err == 0);
442
443 if (err) {
444 nfs_abort_delegation_return(delegation, clp);
445 goto out;
446 }
447 if (!nfs_detach_delegation(nfsi, delegation, NFS_SERVER(inode)))
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500448 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500450 err = nfs_do_return_delegation(inode, delegation, issync);
451out:
452 return err;
Trond Myklebust90163022007-07-05 14:55:18 -0400453}
454
Trond Myklebustb7571442013-04-03 14:33:49 -0400455static bool nfs_delegation_need_return(struct nfs_delegation *delegation)
456{
457 bool ret = false;
458
Trond Myklebustec3ca4e52015-02-26 14:05:05 -0500459 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
460 goto out;
Trond Myklebustb7571442013-04-03 14:33:49 -0400461 if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags))
462 ret = true;
463 if (test_and_clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags) && !ret) {
464 struct inode *inode;
465
466 spin_lock(&delegation->lock);
467 inode = delegation->inode;
468 if (inode && list_empty(&NFS_I(inode)->open_files))
469 ret = true;
470 spin_unlock(&delegation->lock);
471 }
Trond Myklebustec3ca4e52015-02-26 14:05:05 -0500472out:
Trond Myklebustb7571442013-04-03 14:33:49 -0400473 return ret;
474}
475
Chuck Leverd3978bb2010-12-24 01:33:04 +0000476/**
477 * nfs_client_return_marked_delegations - return previously marked delegations
478 * @clp: nfs_client to process
479 *
Trond Myklebustdc327ed2012-05-06 19:46:30 -0400480 * Note that this function is designed to be called by the state
481 * manager thread. For this reason, it cannot flush the dirty data,
482 * since that could deadlock in case of a state recovery error.
483 *
Chuck Leverd3978bb2010-12-24 01:33:04 +0000484 * Returns zero on success, or a negative errno value.
Trond Myklebust515d8612008-12-23 15:21:46 -0500485 */
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500486int nfs_client_return_marked_delegations(struct nfs_client *clp)
Trond Myklebust515d8612008-12-23 15:21:46 -0500487{
488 struct nfs_delegation *delegation;
NeilBrowne04bbf62018-04-30 14:31:30 +1000489 struct nfs_delegation *prev;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000490 struct nfs_server *server;
Trond Myklebust515d8612008-12-23 15:21:46 -0500491 struct inode *inode;
NeilBrowne04bbf62018-04-30 14:31:30 +1000492 struct inode *place_holder = NULL;
493 struct nfs_delegation *place_holder_deleg = NULL;
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500494 int err = 0;
Trond Myklebust515d8612008-12-23 15:21:46 -0500495
496restart:
NeilBrowne04bbf62018-04-30 14:31:30 +1000497 /*
498 * To avoid quadratic looping we hold a reference
499 * to an inode place_holder. Each time we restart, we
500 * list nfs_servers from the server of that inode, and
501 * delegation in the server from the delegations of that
502 * inode.
503 * prev is an RCU-protected pointer to a delegation which
504 * wasn't marked for return and might be a good choice for
505 * the next place_holder.
506 */
Trond Myklebust515d8612008-12-23 15:21:46 -0500507 rcu_read_lock();
NeilBrowne04bbf62018-04-30 14:31:30 +1000508 prev = NULL;
509 if (place_holder)
510 server = NFS_SERVER(place_holder);
511 else
512 server = list_entry_rcu(clp->cl_superblocks.next,
513 struct nfs_server, client_link);
514 list_for_each_entry_from_rcu(server, &clp->cl_superblocks, client_link) {
515 delegation = NULL;
516 if (place_holder && server == NFS_SERVER(place_holder))
517 delegation = rcu_dereference(NFS_I(place_holder)->delegation);
518 if (!delegation || delegation != place_holder_deleg)
519 delegation = list_entry_rcu(server->delegations.next,
520 struct nfs_delegation, super_list);
521 list_for_each_entry_from_rcu(delegation, &server->delegations, super_list) {
522 struct inode *to_put = NULL;
523
524 if (!nfs_delegation_need_return(delegation)) {
525 prev = delegation;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000526 continue;
NeilBrowne04bbf62018-04-30 14:31:30 +1000527 }
Trond Myklebust9f0f8e12015-02-26 09:57:34 -0500528 if (!nfs_sb_active(server->super))
NeilBrownf38934912018-05-31 15:23:22 +1000529 break; /* continue in outer loop */
NeilBrowne04bbf62018-04-30 14:31:30 +1000530
531 if (prev) {
532 struct inode *tmp;
533
534 tmp = nfs_delegation_grab_inode(prev);
535 if (tmp) {
536 to_put = place_holder;
537 place_holder = tmp;
538 place_holder_deleg = prev;
539 }
540 }
541
Trond Myklebust9f0f8e12015-02-26 09:57:34 -0500542 inode = nfs_delegation_grab_inode(delegation);
543 if (inode == NULL) {
544 rcu_read_unlock();
NeilBrowne04bbf62018-04-30 14:31:30 +1000545 if (to_put)
546 iput(to_put);
Trond Myklebust9f0f8e12015-02-26 09:57:34 -0500547 nfs_sb_deactive(server->super);
548 goto restart;
549 }
Trond Myklebustd25be542013-02-05 11:43:28 -0500550 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
Chuck Leverd3978bb2010-12-24 01:33:04 +0000551 rcu_read_unlock();
552
NeilBrowne04bbf62018-04-30 14:31:30 +1000553 if (to_put)
554 iput(to_put);
555
Trond Myklebustd25be542013-02-05 11:43:28 -0500556 err = nfs_end_delegation_return(inode, delegation, 0);
Chuck Leverd3978bb2010-12-24 01:33:04 +0000557 iput(inode);
Trond Myklebust9f0f8e12015-02-26 09:57:34 -0500558 nfs_sb_deactive(server->super);
NeilBrown3ca951b2018-04-30 14:31:30 +1000559 cond_resched();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000560 if (!err)
561 goto restart;
562 set_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state);
NeilBrowne04bbf62018-04-30 14:31:30 +1000563 if (place_holder)
564 iput(place_holder);
Chuck Leverd3978bb2010-12-24 01:33:04 +0000565 return err;
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500566 }
Trond Myklebust515d8612008-12-23 15:21:46 -0500567 }
568 rcu_read_unlock();
NeilBrowne04bbf62018-04-30 14:31:30 +1000569 if (place_holder)
570 iput(place_holder);
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500571 return 0;
Trond Myklebust515d8612008-12-23 15:21:46 -0500572}
573
Chuck Leverd3978bb2010-12-24 01:33:04 +0000574/**
575 * nfs_inode_return_delegation_noreclaim - return delegation, don't reclaim opens
576 * @inode: inode to process
577 *
578 * Does not protect against delegation reclaims, therefore really only safe
579 * to be called from nfs4_clear_inode().
Trond Myklebuste6f81072008-01-24 18:14:34 -0500580 */
581void nfs_inode_return_delegation_noreclaim(struct inode *inode)
582{
Trond Myklebuste6f81072008-01-24 18:14:34 -0500583 struct nfs_delegation *delegation;
584
Trond Myklebustd25be542013-02-05 11:43:28 -0500585 delegation = nfs_inode_detach_delegation(inode);
586 if (delegation != NULL)
Trond Myklebust5fcdfac2015-03-25 13:19:42 -0400587 nfs_do_return_delegation(inode, delegation, 1);
Trond Myklebuste6f81072008-01-24 18:14:34 -0500588}
589
Chuck Leverd3978bb2010-12-24 01:33:04 +0000590/**
591 * nfs_inode_return_delegation - synchronously return a delegation
592 * @inode: inode to process
593 *
Trond Myklebustc57d1bc2012-05-06 19:34:17 -0400594 * This routine will always flush any dirty data to disk on the
595 * assumption that if we need to return the delegation, then
596 * we should stop caching.
597 *
Chuck Leverd3978bb2010-12-24 01:33:04 +0000598 * Returns zero on success, or a negative errno value.
599 */
Bryan Schumaker57ec14c2012-06-20 15:53:44 -0400600int nfs4_inode_return_delegation(struct inode *inode)
Trond Myklebust90163022007-07-05 14:55:18 -0400601{
Trond Myklebust90163022007-07-05 14:55:18 -0400602 struct nfs_inode *nfsi = NFS_I(inode);
603 struct nfs_delegation *delegation;
604 int err = 0;
605
Trond Myklebustc57d1bc2012-05-06 19:34:17 -0400606 nfs_wb_all(inode);
Trond Myklebustd25be542013-02-05 11:43:28 -0500607 delegation = nfs_start_delegation_return(nfsi);
608 if (delegation != NULL)
609 err = nfs_end_delegation_return(inode, delegation, 1);
Trond Myklebust90163022007-07-05 14:55:18 -0400610 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
Trond Myklebustc01d3642018-03-20 16:43:20 -0400613/**
614 * nfs4_inode_make_writeable
615 * @inode: pointer to inode
616 *
617 * Make the inode writeable by returning the delegation if necessary
618 *
619 * Returns zero on success, or a negative errno value.
620 */
621int nfs4_inode_make_writeable(struct inode *inode)
622{
623 if (!nfs4_has_session(NFS_SERVER(inode)->nfs_client) ||
624 !nfs4_check_delegation(inode, FMODE_WRITE))
625 return nfs4_inode_return_delegation(inode);
626 return 0;
627}
628
Trond Myklebustb7571442013-04-03 14:33:49 -0400629static void nfs_mark_return_if_closed_delegation(struct nfs_server *server,
630 struct nfs_delegation *delegation)
631{
632 set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags);
633 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
634}
635
Trond Myklebusted1e62112011-07-25 15:37:29 -0400636static void nfs_mark_return_delegation(struct nfs_server *server,
637 struct nfs_delegation *delegation)
Trond Myklebust6411bd42008-12-23 15:21:51 -0500638{
639 set_bit(NFS_DELEGATION_RETURN, &delegation->flags);
Trond Myklebusted1e62112011-07-25 15:37:29 -0400640 set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state);
Trond Myklebust6411bd42008-12-23 15:21:51 -0500641}
642
Trond Myklebust5c31e232013-04-03 19:04:58 -0400643static bool nfs_server_mark_return_all_delegations(struct nfs_server *server)
644{
645 struct nfs_delegation *delegation;
646 bool ret = false;
647
648 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
649 nfs_mark_return_delegation(server, delegation);
650 ret = true;
651 }
652 return ret;
653}
654
Trond Myklebustb02ba0b2013-04-03 19:23:58 -0400655static void nfs_client_mark_return_all_delegations(struct nfs_client *clp)
656{
657 struct nfs_server *server;
658
659 rcu_read_lock();
660 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
661 nfs_server_mark_return_all_delegations(server);
662 rcu_read_unlock();
663}
664
665static void nfs_delegation_run_state_manager(struct nfs_client *clp)
666{
667 if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state))
668 nfs4_schedule_state_manager(clp);
669}
670
671/**
672 * nfs_expire_all_delegations
673 * @clp: client to process
674 *
675 */
676void nfs_expire_all_delegations(struct nfs_client *clp)
677{
678 nfs_client_mark_return_all_delegations(clp);
679 nfs_delegation_run_state_manager(clp);
680}
681
Chuck Leverd3978bb2010-12-24 01:33:04 +0000682/**
683 * nfs_super_return_all_delegations - return delegations for one superblock
Trond Myklebust302fad72019-02-18 13:32:38 -0500684 * @server: pointer to nfs_server to process
Chuck Leverd3978bb2010-12-24 01:33:04 +0000685 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 */
Bryan Schumakereeebf912012-06-20 15:53:41 -0400687void nfs_server_return_all_delegations(struct nfs_server *server)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000689 struct nfs_client *clp = server->nfs_client;
Trond Myklebust5c31e232013-04-03 19:04:58 -0400690 bool need_wait;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691
692 if (clp == NULL)
693 return;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000694
Trond Myklebust8383e462007-07-06 15:12:04 -0400695 rcu_read_lock();
Trond Myklebust5c31e232013-04-03 19:04:58 -0400696 need_wait = nfs_server_mark_return_all_delegations(server);
Trond Myklebust8383e462007-07-06 15:12:04 -0400697 rcu_read_unlock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000698
Trond Myklebust5c31e232013-04-03 19:04:58 -0400699 if (need_wait) {
Trond Myklebustd18cc1f2009-12-03 08:10:17 -0500700 nfs4_schedule_state_manager(clp);
Trond Myklebust5c31e232013-04-03 19:04:58 -0400701 nfs4_wait_clnt_recover(clp);
702 }
Trond Myklebust515d8612008-12-23 15:21:46 -0500703}
704
Trond Myklebust826e0012013-04-03 19:27:52 -0400705static void nfs_mark_return_unused_delegation_types(struct nfs_server *server,
Chuck Leverd3978bb2010-12-24 01:33:04 +0000706 fmode_t flags)
Trond Myklebust515d8612008-12-23 15:21:46 -0500707{
708 struct nfs_delegation *delegation;
709
Chuck Leverd3978bb2010-12-24 01:33:04 +0000710 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500711 if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE))
712 continue;
713 if (delegation->type & flags)
Trond Myklebust826e0012013-04-03 19:27:52 -0400714 nfs_mark_return_if_closed_delegation(server, delegation);
Trond Myklebust707fb4b2008-12-23 15:21:47 -0500715 }
Chuck Leverd3978bb2010-12-24 01:33:04 +0000716}
717
Trond Myklebust826e0012013-04-03 19:27:52 -0400718static void nfs_client_mark_return_unused_delegation_types(struct nfs_client *clp,
Chuck Leverd3978bb2010-12-24 01:33:04 +0000719 fmode_t flags)
720{
721 struct nfs_server *server;
722
723 rcu_read_lock();
724 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
Trond Myklebust826e0012013-04-03 19:27:52 -0400725 nfs_mark_return_unused_delegation_types(server, flags);
Trond Myklebust515d8612008-12-23 15:21:46 -0500726 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
728
Trond Myklebust41020b62016-09-22 13:38:58 -0400729static void nfs_mark_delegation_revoked(struct nfs_server *server,
730 struct nfs_delegation *delegation)
Trond Myklebust869f9df2014-11-10 18:43:56 -0500731{
Trond Myklebust41020b62016-09-22 13:38:58 -0400732 set_bit(NFS_DELEGATION_REVOKED, &delegation->flags);
Trond Myklebust059b43e2016-09-22 13:39:06 -0400733 delegation->stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust41020b62016-09-22 13:38:58 -0400734 nfs_mark_return_delegation(server, delegation);
Trond Myklebust869f9df2014-11-10 18:43:56 -0500735}
736
Trond Myklebust41020b62016-09-22 13:38:58 -0400737static bool nfs_revoke_delegation(struct inode *inode,
738 const nfs4_stateid *stateid)
739{
740 struct nfs_delegation *delegation;
Trond Myklebust7f048832016-09-22 13:39:14 -0400741 nfs4_stateid tmp;
Trond Myklebust41020b62016-09-22 13:38:58 -0400742 bool ret = false;
743
744 rcu_read_lock();
745 delegation = rcu_dereference(NFS_I(inode)->delegation);
746 if (delegation == NULL)
747 goto out;
Trond Myklebust7f048832016-09-22 13:39:14 -0400748 if (stateid == NULL) {
749 nfs4_stateid_copy(&tmp, &delegation->stateid);
750 stateid = &tmp;
751 } else if (!nfs4_stateid_match(stateid, &delegation->stateid))
Trond Myklebust41020b62016-09-22 13:38:58 -0400752 goto out;
753 nfs_mark_delegation_revoked(NFS_SERVER(inode), delegation);
754 ret = true;
755out:
756 rcu_read_unlock();
Trond Myklebust7f048832016-09-22 13:39:14 -0400757 if (ret)
758 nfs_inode_find_state_and_recover(inode, stateid);
Trond Myklebust41020b62016-09-22 13:38:58 -0400759 return ret;
760}
761
762void nfs_remove_bad_delegation(struct inode *inode,
763 const nfs4_stateid *stateid)
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500764{
765 struct nfs_delegation *delegation;
766
Trond Myklebust41020b62016-09-22 13:38:58 -0400767 if (!nfs_revoke_delegation(inode, stateid))
768 return;
Trond Myklebustd25be542013-02-05 11:43:28 -0500769 delegation = nfs_inode_detach_delegation(inode);
Trond Myklebust7f048832016-09-22 13:39:14 -0400770 if (delegation)
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500771 nfs_free_delegation(delegation);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500772}
Andy Adamson9cb81962012-03-07 10:49:41 -0500773EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation);
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -0500774
Chuck Leverd3978bb2010-12-24 01:33:04 +0000775/**
Trond Myklebust826e0012013-04-03 19:27:52 -0400776 * nfs_expire_unused_delegation_types
Chuck Leverd3978bb2010-12-24 01:33:04 +0000777 * @clp: client to process
778 * @flags: delegation types to expire
779 *
780 */
Trond Myklebust826e0012013-04-03 19:27:52 -0400781void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags)
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500782{
Trond Myklebust826e0012013-04-03 19:27:52 -0400783 nfs_client_mark_return_unused_delegation_types(clp, flags);
Alexandros Batsakisc79571a2009-12-05 13:20:52 -0500784 nfs_delegation_run_state_manager(clp);
785}
786
Chuck Leverd3978bb2010-12-24 01:33:04 +0000787static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server)
Trond Myklebustb7391f42008-12-23 15:21:52 -0500788{
789 struct nfs_delegation *delegation;
790
Chuck Leverd3978bb2010-12-24 01:33:04 +0000791 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
Trond Myklebustb7391f42008-12-23 15:21:52 -0500792 if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags))
793 continue;
Trond Myklebustb7571442013-04-03 14:33:49 -0400794 nfs_mark_return_if_closed_delegation(server, delegation);
Trond Myklebustb7391f42008-12-23 15:21:52 -0500795 }
Trond Myklebustb7391f42008-12-23 15:21:52 -0500796}
797
Chuck Leverd3978bb2010-12-24 01:33:04 +0000798/**
799 * nfs_expire_unreferenced_delegations - Eliminate unused delegations
800 * @clp: nfs_client to process
801 *
802 */
Trond Myklebustb7391f42008-12-23 15:21:52 -0500803void nfs_expire_unreferenced_delegations(struct nfs_client *clp)
804{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000805 struct nfs_server *server;
806
807 rcu_read_lock();
808 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
809 nfs_mark_return_unreferenced_delegations(server);
810 rcu_read_unlock();
811
Trond Myklebustb7391f42008-12-23 15:21:52 -0500812 nfs_delegation_run_state_manager(clp);
813}
814
Chuck Leverd3978bb2010-12-24 01:33:04 +0000815/**
816 * nfs_async_inode_return_delegation - asynchronously return a delegation
817 * @inode: inode to process
Trond Myklebust8e663f02012-03-04 18:13:56 -0500818 * @stateid: state ID information
Chuck Leverd3978bb2010-12-24 01:33:04 +0000819 *
820 * Returns zero on success, or a negative errno value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 */
Chuck Leverd3978bb2010-12-24 01:33:04 +0000822int nfs_async_inode_return_delegation(struct inode *inode,
823 const nfs4_stateid *stateid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
Trond Myklebusted1e62112011-07-25 15:37:29 -0400825 struct nfs_server *server = NFS_SERVER(inode);
826 struct nfs_client *clp = server->nfs_client;
Trond Myklebust6411bd42008-12-23 15:21:51 -0500827 struct nfs_delegation *delegation;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
Trond Myklebust6411bd42008-12-23 15:21:51 -0500829 rcu_read_lock();
830 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebust755a48a2014-03-02 22:03:12 -0500831 if (delegation == NULL)
832 goto out_enoent;
Trond Myklebust4816fda2015-09-20 14:58:42 -0400833 if (stateid != NULL &&
834 !clp->cl_mvops->match_stateid(&delegation->stateid, stateid))
Trond Myklebust755a48a2014-03-02 22:03:12 -0500835 goto out_enoent;
Trond Myklebusted1e62112011-07-25 15:37:29 -0400836 nfs_mark_return_delegation(server, delegation);
Trond Myklebust6411bd42008-12-23 15:21:51 -0500837 rcu_read_unlock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000838
Trond Myklebust6411bd42008-12-23 15:21:51 -0500839 nfs_delegation_run_state_manager(clp);
840 return 0;
Trond Myklebust755a48a2014-03-02 22:03:12 -0500841out_enoent:
842 rcu_read_unlock();
843 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844}
845
Chuck Leverd3978bb2010-12-24 01:33:04 +0000846static struct inode *
847nfs_delegation_find_inode_server(struct nfs_server *server,
848 const struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
850 struct nfs_delegation *delegation;
Trond Myklebuste39d8a12018-11-13 16:37:54 -0500851 struct inode *freeme, *res = NULL;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000852
853 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
Trond Myklebust86e89482008-12-23 15:21:39 -0500854 spin_lock(&delegation->lock);
855 if (delegation->inode != NULL &&
856 nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) {
Trond Myklebuste39d8a12018-11-13 16:37:54 -0500857 freeme = igrab(delegation->inode);
858 if (freeme && nfs_sb_active(freeme->i_sb))
859 res = freeme;
Trond Myklebust6c342652018-06-07 14:22:00 -0400860 spin_unlock(&delegation->lock);
861 if (res != NULL)
862 return res;
Trond Myklebuste39d8a12018-11-13 16:37:54 -0500863 if (freeme) {
864 rcu_read_unlock();
865 iput(freeme);
866 rcu_read_lock();
867 }
Trond Myklebust6c342652018-06-07 14:22:00 -0400868 return ERR_PTR(-EAGAIN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 }
Trond Myklebust86e89482008-12-23 15:21:39 -0500870 spin_unlock(&delegation->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 }
Trond Myklebust6c342652018-06-07 14:22:00 -0400872 return ERR_PTR(-ENOENT);
Chuck Leverd3978bb2010-12-24 01:33:04 +0000873}
874
875/**
876 * nfs_delegation_find_inode - retrieve the inode associated with a delegation
877 * @clp: client state handle
878 * @fhandle: filehandle from a delegation recall
879 *
880 * Returns pointer to inode matching "fhandle," or NULL if a matching inode
881 * cannot be found.
882 */
883struct inode *nfs_delegation_find_inode(struct nfs_client *clp,
884 const struct nfs_fh *fhandle)
885{
886 struct nfs_server *server;
Trond Myklebust6c342652018-06-07 14:22:00 -0400887 struct inode *res;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000888
889 rcu_read_lock();
890 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
891 res = nfs_delegation_find_inode_server(server, fhandle);
Anna Schumakerd5681f52018-06-14 09:39:17 -0400892 if (res != ERR_PTR(-ENOENT)) {
893 rcu_read_unlock();
Trond Myklebust6c342652018-06-07 14:22:00 -0400894 return res;
Anna Schumakerd5681f52018-06-14 09:39:17 -0400895 }
Chuck Leverd3978bb2010-12-24 01:33:04 +0000896 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400897 rcu_read_unlock();
Trond Myklebust6c342652018-06-07 14:22:00 -0400898 return ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899}
900
Chuck Leverd3978bb2010-12-24 01:33:04 +0000901static void nfs_delegation_mark_reclaim_server(struct nfs_server *server)
902{
903 struct nfs_delegation *delegation;
904
Trond Myklebust45870d62016-09-22 13:38:59 -0400905 list_for_each_entry_rcu(delegation, &server->delegations, super_list) {
906 /*
907 * If the delegation may have been admin revoked, then we
908 * cannot reclaim it.
909 */
910 if (test_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags))
911 continue;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000912 set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
Trond Myklebust45870d62016-09-22 13:38:59 -0400913 }
Chuck Leverd3978bb2010-12-24 01:33:04 +0000914}
915
916/**
917 * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed
918 * @clp: nfs_client to process
919 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 */
David Howellsadfa6f92006-08-22 20:06:08 -0400921void nfs_delegation_mark_reclaim(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922{
Chuck Leverd3978bb2010-12-24 01:33:04 +0000923 struct nfs_server *server;
924
Trond Myklebust8383e462007-07-06 15:12:04 -0400925 rcu_read_lock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000926 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
927 nfs_delegation_mark_reclaim_server(server);
Trond Myklebust8383e462007-07-06 15:12:04 -0400928 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929}
930
Chuck Leverd3978bb2010-12-24 01:33:04 +0000931/**
932 * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done
933 * @clp: nfs_client to process
934 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 */
David Howellsadfa6f92006-08-22 20:06:08 -0400936void nfs_delegation_reap_unclaimed(struct nfs_client *clp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
Trond Myklebust8383e462007-07-06 15:12:04 -0400938 struct nfs_delegation *delegation;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000939 struct nfs_server *server;
Trond Myklebust86e89482008-12-23 15:21:39 -0500940 struct inode *inode;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000941
Trond Myklebust8383e462007-07-06 15:12:04 -0400942restart:
943 rcu_read_lock();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000944 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
945 list_for_each_entry_rcu(delegation, &server->delegations,
946 super_list) {
Trond Myklebustec3ca4e52015-02-26 14:05:05 -0500947 if (test_bit(NFS_DELEGATION_RETURNING,
948 &delegation->flags))
949 continue;
Chuck Leverd3978bb2010-12-24 01:33:04 +0000950 if (test_bit(NFS_DELEGATION_NEED_RECLAIM,
951 &delegation->flags) == 0)
952 continue;
Trond Myklebust9f0f8e12015-02-26 09:57:34 -0500953 if (!nfs_sb_active(server->super))
NeilBrownf38934912018-05-31 15:23:22 +1000954 break; /* continue in outer loop */
Trond Myklebust9f0f8e12015-02-26 09:57:34 -0500955 inode = nfs_delegation_grab_inode(delegation);
956 if (inode == NULL) {
957 rcu_read_unlock();
958 nfs_sb_deactive(server->super);
959 goto restart;
960 }
Trond Myklebustb04b22f2015-02-26 13:59:38 -0500961 delegation = nfs_start_delegation_return_locked(NFS_I(inode));
Chuck Leverd3978bb2010-12-24 01:33:04 +0000962 rcu_read_unlock();
Trond Myklebustb04b22f2015-02-26 13:59:38 -0500963 if (delegation != NULL) {
964 delegation = nfs_detach_delegation(NFS_I(inode),
965 delegation, server);
966 if (delegation != NULL)
967 nfs_free_delegation(delegation);
968 }
Chuck Leverd3978bb2010-12-24 01:33:04 +0000969 iput(inode);
Trond Myklebust9f0f8e12015-02-26 09:57:34 -0500970 nfs_sb_deactive(server->super);
NeilBrown3ca951b2018-04-30 14:31:30 +1000971 cond_resched();
Chuck Leverd3978bb2010-12-24 01:33:04 +0000972 goto restart;
973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 }
Trond Myklebust8383e462007-07-06 15:12:04 -0400975 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976}
Trond Myklebust3e4f6292006-03-20 13:44:46 -0500977
Trond Myklebustbb3d1a32016-09-22 13:39:00 -0400978static inline bool nfs4_server_rebooted(const struct nfs_client *clp)
979{
980 return (clp->cl_state & (BIT(NFS4CLNT_CHECK_LEASE) |
981 BIT(NFS4CLNT_LEASE_EXPIRED) |
982 BIT(NFS4CLNT_SESSION_RESET))) != 0;
983}
984
Trond Myklebust45870d62016-09-22 13:38:59 -0400985static void nfs_mark_test_expired_delegation(struct nfs_server *server,
986 struct nfs_delegation *delegation)
987{
Trond Myklebust059b43e2016-09-22 13:39:06 -0400988 if (delegation->stateid.type == NFS4_INVALID_STATEID_TYPE)
989 return;
Trond Myklebust45870d62016-09-22 13:38:59 -0400990 clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags);
991 set_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
992 set_bit(NFS4CLNT_DELEGATION_EXPIRED, &server->nfs_client->cl_state);
993}
994
Trond Myklebustbb3d1a32016-09-22 13:39:00 -0400995static void nfs_inode_mark_test_expired_delegation(struct nfs_server *server,
996 struct inode *inode)
997{
998 struct nfs_delegation *delegation;
999
1000 rcu_read_lock();
1001 delegation = rcu_dereference(NFS_I(inode)->delegation);
1002 if (delegation)
1003 nfs_mark_test_expired_delegation(server, delegation);
1004 rcu_read_unlock();
1005
1006}
1007
Trond Myklebust45870d62016-09-22 13:38:59 -04001008static void nfs_delegation_mark_test_expired_server(struct nfs_server *server)
1009{
1010 struct nfs_delegation *delegation;
1011
1012 list_for_each_entry_rcu(delegation, &server->delegations, super_list)
1013 nfs_mark_test_expired_delegation(server, delegation);
1014}
1015
1016/**
1017 * nfs_mark_test_expired_all_delegations - mark all delegations for testing
1018 * @clp: nfs_client to process
1019 *
1020 * Iterates through all the delegations associated with this server and
1021 * marks them as needing to be checked for validity.
1022 */
1023void nfs_mark_test_expired_all_delegations(struct nfs_client *clp)
1024{
1025 struct nfs_server *server;
1026
1027 rcu_read_lock();
1028 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1029 nfs_delegation_mark_test_expired_server(server);
1030 rcu_read_unlock();
1031}
1032
1033/**
1034 * nfs_reap_expired_delegations - reap expired delegations
1035 * @clp: nfs_client to process
1036 *
1037 * Iterates through all the delegations associated with this server and
1038 * checks if they have may have been revoked. This function is usually
1039 * expected to be called in cases where the server may have lost its
1040 * lease.
1041 */
1042void nfs_reap_expired_delegations(struct nfs_client *clp)
1043{
1044 const struct nfs4_minor_version_ops *ops = clp->cl_mvops;
1045 struct nfs_delegation *delegation;
1046 struct nfs_server *server;
1047 struct inode *inode;
NeilBrowna52458b2018-12-03 11:30:31 +11001048 const struct cred *cred;
Trond Myklebust45870d62016-09-22 13:38:59 -04001049 nfs4_stateid stateid;
1050
1051restart:
1052 rcu_read_lock();
1053 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) {
1054 list_for_each_entry_rcu(delegation, &server->delegations,
1055 super_list) {
1056 if (test_bit(NFS_DELEGATION_RETURNING,
1057 &delegation->flags))
1058 continue;
1059 if (test_bit(NFS_DELEGATION_TEST_EXPIRED,
1060 &delegation->flags) == 0)
1061 continue;
1062 if (!nfs_sb_active(server->super))
NeilBrownf38934912018-05-31 15:23:22 +10001063 break; /* continue in outer loop */
Trond Myklebust45870d62016-09-22 13:38:59 -04001064 inode = nfs_delegation_grab_inode(delegation);
1065 if (inode == NULL) {
1066 rcu_read_unlock();
1067 nfs_sb_deactive(server->super);
1068 goto restart;
1069 }
NeilBrowna52458b2018-12-03 11:30:31 +11001070 cred = get_cred_rcu(delegation->cred);
Trond Myklebust45870d62016-09-22 13:38:59 -04001071 nfs4_stateid_copy(&stateid, &delegation->stateid);
1072 clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags);
1073 rcu_read_unlock();
1074 if (cred != NULL &&
1075 ops->test_and_free_expired(server, &stateid, cred) < 0) {
1076 nfs_revoke_delegation(inode, &stateid);
1077 nfs_inode_find_state_and_recover(inode, &stateid);
1078 }
NeilBrowna52458b2018-12-03 11:30:31 +11001079 put_cred(cred);
Trond Myklebustbb3d1a32016-09-22 13:39:00 -04001080 if (nfs4_server_rebooted(clp)) {
1081 nfs_inode_mark_test_expired_delegation(server,inode);
1082 iput(inode);
1083 nfs_sb_deactive(server->super);
1084 return;
1085 }
Trond Myklebust45870d62016-09-22 13:38:59 -04001086 iput(inode);
1087 nfs_sb_deactive(server->super);
NeilBrown3ca951b2018-04-30 14:31:30 +10001088 cond_resched();
Trond Myklebust45870d62016-09-22 13:38:59 -04001089 goto restart;
1090 }
1091 }
1092 rcu_read_unlock();
1093}
1094
Trond Myklebust6c2d8f82016-09-22 13:39:07 -04001095void nfs_inode_find_delegation_state_and_recover(struct inode *inode,
1096 const nfs4_stateid *stateid)
1097{
1098 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
1099 struct nfs_delegation *delegation;
1100 bool found = false;
1101
1102 rcu_read_lock();
1103 delegation = rcu_dereference(NFS_I(inode)->delegation);
1104 if (delegation &&
1105 nfs4_stateid_match_other(&delegation->stateid, stateid)) {
1106 nfs_mark_test_expired_delegation(NFS_SERVER(inode), delegation);
1107 found = true;
1108 }
1109 rcu_read_unlock();
1110 if (found)
1111 nfs4_schedule_state_manager(clp);
1112}
1113
Chuck Leverd3978bb2010-12-24 01:33:04 +00001114/**
1115 * nfs_delegations_present - check for existence of delegations
1116 * @clp: client state handle
1117 *
1118 * Returns one if there are any nfs_delegation structures attached
1119 * to this nfs_client.
1120 */
1121int nfs_delegations_present(struct nfs_client *clp)
1122{
1123 struct nfs_server *server;
1124 int ret = 0;
1125
1126 rcu_read_lock();
1127 list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link)
1128 if (!list_empty(&server->delegations)) {
1129 ret = 1;
1130 break;
1131 }
1132 rcu_read_unlock();
1133 return ret;
1134}
1135
1136/**
Trond Myklebust12f275c2017-11-06 15:28:05 -05001137 * nfs4_refresh_delegation_stateid - Update delegation stateid seqid
1138 * @dst: stateid to refresh
1139 * @inode: inode to check
1140 *
1141 * Returns "true" and updates "dst->seqid" * if inode had a delegation
1142 * that matches our delegation stateid. Otherwise "false" is returned.
1143 */
1144bool nfs4_refresh_delegation_stateid(nfs4_stateid *dst, struct inode *inode)
1145{
1146 struct nfs_delegation *delegation;
1147 bool ret = false;
1148 if (!inode)
1149 goto out;
1150
1151 rcu_read_lock();
1152 delegation = rcu_dereference(NFS_I(inode)->delegation);
1153 if (delegation != NULL &&
1154 nfs4_stateid_match_other(dst, &delegation->stateid)) {
1155 dst->seqid = delegation->stateid.seqid;
1156 return ret;
1157 }
1158 rcu_read_unlock();
1159out:
1160 return ret;
1161}
1162
1163/**
Chuck Leverd3978bb2010-12-24 01:33:04 +00001164 * nfs4_copy_delegation_stateid - Copy inode's state ID information
Chuck Leverd3978bb2010-12-24 01:33:04 +00001165 * @inode: inode to check
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001166 * @flags: delegation type requirement
Trond Myklebustabf4e132016-05-16 17:42:44 -04001167 * @dst: stateid data structure to fill in
1168 * @cred: optional argument to retrieve credential
Chuck Leverd3978bb2010-12-24 01:33:04 +00001169 *
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001170 * Returns "true" and fills in "dst->data" * if inode had a delegation,
1171 * otherwise "false" is returned.
Chuck Leverd3978bb2010-12-24 01:33:04 +00001172 */
Trond Myklebustabf4e132016-05-16 17:42:44 -04001173bool nfs4_copy_delegation_stateid(struct inode *inode, fmode_t flags,
NeilBrowna52458b2018-12-03 11:30:31 +11001174 nfs4_stateid *dst, const struct cred **cred)
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001175{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001176 struct nfs_inode *nfsi = NFS_I(inode);
1177 struct nfs_delegation *delegation;
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001178 bool ret;
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001179
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001180 flags &= FMODE_READ|FMODE_WRITE;
Trond Myklebust8383e462007-07-06 15:12:04 -04001181 rcu_read_lock();
1182 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebustaa05c872016-09-22 13:38:54 -04001183 ret = nfs4_is_valid_delegation(delegation, flags);
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001184 if (ret) {
Trond Myklebustf597c532012-03-04 18:13:56 -05001185 nfs4_stateid_copy(dst, &delegation->stateid);
Trond Myklebust0032a7a2012-03-08 17:16:12 -05001186 nfs_mark_delegation_referenced(delegation);
Trond Myklebustabf4e132016-05-16 17:42:44 -04001187 if (cred)
NeilBrowna52458b2018-12-03 11:30:31 +11001188 *cred = get_cred(delegation->cred);
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001189 }
Trond Myklebust8383e462007-07-06 15:12:04 -04001190 rcu_read_unlock();
1191 return ret;
Trond Myklebust3e4f6292006-03-20 13:44:46 -05001192}
Trond Myklebust5445b1f2015-09-05 19:06:58 -04001193
1194/**
1195 * nfs4_delegation_flush_on_close - Check if we must flush file on close
1196 * @inode: inode to check
1197 *
1198 * This function checks the number of outstanding writes to the file
1199 * against the delegation 'space_limit' field to see if
1200 * the spec requires us to flush the file on close.
1201 */
1202bool nfs4_delegation_flush_on_close(const struct inode *inode)
1203{
1204 struct nfs_inode *nfsi = NFS_I(inode);
1205 struct nfs_delegation *delegation;
1206 bool ret = true;
1207
1208 rcu_read_lock();
1209 delegation = rcu_dereference(nfsi->delegation);
1210 if (delegation == NULL || !(delegation->type & FMODE_WRITE))
1211 goto out;
Trond Myklebusta6b6d5b2017-08-01 15:39:46 -04001212 if (atomic_long_read(&nfsi->nrequests) < delegation->pagemod_limit)
Trond Myklebust5445b1f2015-09-05 19:06:58 -04001213 ret = false;
1214out:
1215 rcu_read_unlock();
1216 return ret;
1217}