Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * linux/fs/nfs/delegation.c |
| 4 | * |
| 5 | * Copyright (C) 2004 Trond Myklebust |
| 6 | * |
| 7 | * NFS file delegation management |
| 8 | * |
| 9 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/completion.h> |
Trond Myklebust | 58d9714 | 2006-01-03 09:55:24 +0100 | [diff] [blame] | 11 | #include <linux/kthread.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/module.h> |
| 13 | #include <linux/sched.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 14 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/spinlock.h> |
Jeff Layton | 1eb5d98 | 2018-01-09 08:21:17 -0500 | [diff] [blame] | 16 | #include <linux/iversion.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
| 18 | #include <linux/nfs4.h> |
| 19 | #include <linux/nfs_fs.h> |
| 20 | #include <linux/nfs_xdr.h> |
| 21 | |
Trond Myklebust | 4ce7971 | 2005-06-22 17:16:21 +0000 | [diff] [blame] | 22 | #include "nfs4_fs.h" |
Trond Myklebust | c01d364 | 2018-03-20 16:43:20 -0400 | [diff] [blame] | 23 | #include "nfs4session.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "delegation.h" |
David Howells | 24c8dbb | 2006-08-22 20:06:10 -0400 | [diff] [blame] | 25 | #include "internal.h" |
Trond Myklebust | ca8acf8 | 2013-08-13 10:36:56 -0400 | [diff] [blame] | 26 | #include "nfs4trace.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
Trond Myklebust | 10717f4 | 2020-01-27 09:58:19 -0500 | [diff] [blame] | 28 | #define NFS_DEFAULT_DELEGATION_WATERMARK (5000U) |
| 29 | |
Trond Myklebust | d2269ea | 2020-01-27 09:58:18 -0500 | [diff] [blame] | 30 | static atomic_long_t nfs_active_delegations; |
Trond Myklebust | 10717f4 | 2020-01-27 09:58:19 -0500 | [diff] [blame] | 31 | static unsigned nfs_delegation_watermark = NFS_DEFAULT_DELEGATION_WATERMARK; |
Trond Myklebust | d2269ea | 2020-01-27 09:58:18 -0500 | [diff] [blame] | 32 | |
| 33 | static void __nfs_free_delegation(struct nfs_delegation *delegation) |
Trond Myklebust | 905f8d1 | 2007-08-06 12:18:34 -0400 | [diff] [blame] | 34 | { |
NeilBrown | a52458b | 2018-12-03 11:30:31 +1100 | [diff] [blame] | 35 | put_cred(delegation->cred); |
| 36 | delegation->cred = NULL; |
Lai Jiangshan | 26f04dd | 2011-05-01 06:21:54 -0700 | [diff] [blame] | 37 | kfree_rcu(delegation, rcu); |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 38 | } |
| 39 | |
Trond Myklebust | d2269ea | 2020-01-27 09:58:18 -0500 | [diff] [blame] | 40 | static void nfs_mark_delegation_revoked(struct nfs_delegation *delegation) |
| 41 | { |
| 42 | if (!test_and_set_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) { |
| 43 | delegation->stateid.type = NFS4_INVALID_STATEID_TYPE; |
| 44 | atomic_long_dec(&nfs_active_delegations); |
Trond Myklebust | efeda80 | 2020-02-05 09:01:54 -0500 | [diff] [blame] | 45 | if (!test_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) |
| 46 | nfs_clear_verifier_delegated(delegation->inode); |
Trond Myklebust | d2269ea | 2020-01-27 09:58:18 -0500 | [diff] [blame] | 47 | } |
| 48 | } |
| 49 | |
Trond Myklebust | 8c75593 | 2020-02-13 14:51:06 -0500 | [diff] [blame] | 50 | static struct nfs_delegation *nfs_get_delegation(struct nfs_delegation *delegation) |
| 51 | { |
| 52 | refcount_inc(&delegation->refcount); |
| 53 | return delegation; |
| 54 | } |
| 55 | |
| 56 | static void nfs_put_delegation(struct nfs_delegation *delegation) |
| 57 | { |
| 58 | if (refcount_dec_and_test(&delegation->refcount)) |
| 59 | __nfs_free_delegation(delegation); |
| 60 | } |
| 61 | |
Trond Myklebust | d2269ea | 2020-01-27 09:58:18 -0500 | [diff] [blame] | 62 | static void nfs_free_delegation(struct nfs_delegation *delegation) |
| 63 | { |
| 64 | nfs_mark_delegation_revoked(delegation); |
Trond Myklebust | 8c75593 | 2020-02-13 14:51:06 -0500 | [diff] [blame] | 65 | nfs_put_delegation(delegation); |
Trond Myklebust | d2269ea | 2020-01-27 09:58:18 -0500 | [diff] [blame] | 66 | } |
| 67 | |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 68 | /** |
| 69 | * nfs_mark_delegation_referenced - set delegation's REFERENCED flag |
| 70 | * @delegation: delegation to process |
| 71 | * |
| 72 | */ |
Trond Myklebust | b7391f4 | 2008-12-23 15:21:52 -0500 | [diff] [blame] | 73 | void nfs_mark_delegation_referenced(struct nfs_delegation *delegation) |
| 74 | { |
| 75 | set_bit(NFS_DELEGATION_REFERENCED, &delegation->flags); |
| 76 | } |
| 77 | |
Trond Myklebust | be20037 | 2021-05-08 10:01:32 -0400 | [diff] [blame] | 78 | static void nfs_mark_return_delegation(struct nfs_server *server, |
| 79 | struct nfs_delegation *delegation) |
| 80 | { |
| 81 | set_bit(NFS_DELEGATION_RETURN, &delegation->flags); |
| 82 | set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state); |
| 83 | } |
| 84 | |
Trond Myklebust | aa05c87 | 2016-09-22 13:38:54 -0400 | [diff] [blame] | 85 | static bool |
| 86 | nfs4_is_valid_delegation(const struct nfs_delegation *delegation, |
| 87 | fmode_t flags) |
| 88 | { |
| 89 | if (delegation != NULL && (delegation->type & flags) == flags && |
| 90 | !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) && |
| 91 | !test_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) |
| 92 | return true; |
| 93 | return false; |
| 94 | } |
| 95 | |
Trond Myklebust | be3df3d | 2019-10-31 18:40:32 -0400 | [diff] [blame] | 96 | struct nfs_delegation *nfs4_get_valid_delegation(const struct inode *inode) |
| 97 | { |
| 98 | struct nfs_delegation *delegation; |
| 99 | |
| 100 | delegation = rcu_dereference(NFS_I(inode)->delegation); |
| 101 | if (nfs4_is_valid_delegation(delegation, 0)) |
| 102 | return delegation; |
| 103 | return NULL; |
| 104 | } |
| 105 | |
Peng Tao | 15bb3af | 2014-07-03 13:05:00 +0800 | [diff] [blame] | 106 | static int |
| 107 | nfs4_do_check_delegation(struct inode *inode, fmode_t flags, bool mark) |
Trond Myklebust | b7391f4 | 2008-12-23 15:21:52 -0500 | [diff] [blame] | 108 | { |
| 109 | struct nfs_delegation *delegation; |
| 110 | int ret = 0; |
| 111 | |
| 112 | flags &= FMODE_READ|FMODE_WRITE; |
| 113 | rcu_read_lock(); |
| 114 | delegation = rcu_dereference(NFS_I(inode)->delegation); |
Trond Myklebust | aa05c87 | 2016-09-22 13:38:54 -0400 | [diff] [blame] | 115 | if (nfs4_is_valid_delegation(delegation, flags)) { |
Peng Tao | 15bb3af | 2014-07-03 13:05:00 +0800 | [diff] [blame] | 116 | if (mark) |
| 117 | nfs_mark_delegation_referenced(delegation); |
Trond Myklebust | b7391f4 | 2008-12-23 15:21:52 -0500 | [diff] [blame] | 118 | ret = 1; |
| 119 | } |
| 120 | rcu_read_unlock(); |
| 121 | return ret; |
| 122 | } |
Peng Tao | 15bb3af | 2014-07-03 13:05:00 +0800 | [diff] [blame] | 123 | /** |
Trond Myklebust | 6453bcd | 2021-03-04 20:29:50 -0500 | [diff] [blame] | 124 | * nfs4_have_delegation - check if inode has a delegation, mark it |
Peng Tao | 15bb3af | 2014-07-03 13:05:00 +0800 | [diff] [blame] | 125 | * NFS_DELEGATION_REFERENCED if there is one. |
| 126 | * @inode: inode to check |
| 127 | * @flags: delegation types to check for |
| 128 | * |
| 129 | * Returns one if inode has the indicated delegation, otherwise zero. |
| 130 | */ |
| 131 | int nfs4_have_delegation(struct inode *inode, fmode_t flags) |
| 132 | { |
| 133 | return nfs4_do_check_delegation(inode, flags, true); |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * nfs4_check_delegation - check if inode has a delegation, do not mark |
| 138 | * NFS_DELEGATION_REFERENCED if it has one. |
| 139 | */ |
| 140 | int nfs4_check_delegation(struct inode *inode, fmode_t flags) |
| 141 | { |
| 142 | return nfs4_do_check_delegation(inode, flags, false); |
| 143 | } |
Trond Myklebust | b7391f4 | 2008-12-23 15:21:52 -0500 | [diff] [blame] | 144 | |
Olga Kornievskaia | 44f411c | 2018-10-04 14:45:00 -0400 | [diff] [blame] | 145 | static int nfs_delegation_claim_locks(struct nfs4_state *state, const nfs4_stateid *stateid) |
Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 146 | { |
| 147 | struct inode *inode = state->inode; |
| 148 | struct file_lock *fl; |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 149 | struct file_lock_context *flctx = inode->i_flctx; |
| 150 | struct list_head *list; |
Trond Myklebust | d512220 | 2009-06-17 13:22:58 -0700 | [diff] [blame] | 151 | int status = 0; |
Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 152 | |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 153 | if (flctx == NULL) |
Trond Myklebust | 65b62a2 | 2013-02-07 10:54:07 -0500 | [diff] [blame] | 154 | goto out; |
Jeff Layton | 314d7cc | 2013-04-10 15:36:48 -0400 | [diff] [blame] | 155 | |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 156 | list = &flctx->flc_posix; |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 157 | spin_lock(&flctx->flc_lock); |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 158 | restart: |
| 159 | list_for_each_entry(fl, list, fl_list) { |
Olga Kornievskaia | 44f411c | 2018-10-04 14:45:00 -0400 | [diff] [blame] | 160 | if (nfs_file_open_context(fl->fl_file)->state != state) |
Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 161 | continue; |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 162 | spin_unlock(&flctx->flc_lock); |
Trond Myklebust | db4f2e6 | 2013-04-01 15:56:46 -0400 | [diff] [blame] | 163 | status = nfs4_lock_delegation_recall(fl, state, stateid); |
Trond Myklebust | d512220 | 2009-06-17 13:22:58 -0700 | [diff] [blame] | 164 | if (status < 0) |
Trond Myklebust | 3f09df7 | 2009-06-17 13:23:00 -0700 | [diff] [blame] | 165 | goto out; |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 166 | spin_lock(&flctx->flc_lock); |
Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 167 | } |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 168 | if (list == &flctx->flc_posix) { |
| 169 | list = &flctx->flc_flock; |
| 170 | goto restart; |
Jeff Layton | 5263e31 | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 171 | } |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 172 | spin_unlock(&flctx->flc_lock); |
Trond Myklebust | 3f09df7 | 2009-06-17 13:23:00 -0700 | [diff] [blame] | 173 | out: |
Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 174 | return status; |
| 175 | } |
| 176 | |
Trond Myklebust | 24311f8 | 2015-09-20 10:50:17 -0400 | [diff] [blame] | 177 | static int nfs_delegation_claim_opens(struct inode *inode, |
| 178 | const nfs4_stateid *stateid, fmode_t type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | { |
| 180 | struct nfs_inode *nfsi = NFS_I(inode); |
| 181 | struct nfs_open_context *ctx; |
Trond Myklebust | d25be54 | 2013-02-05 11:43:28 -0500 | [diff] [blame] | 182 | struct nfs4_state_owner *sp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | struct nfs4_state *state; |
Trond Myklebust | d25be54 | 2013-02-05 11:43:28 -0500 | [diff] [blame] | 184 | unsigned int seq; |
Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 185 | int err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
| 187 | again: |
Trond Myklebust | 0de4397 | 2018-09-02 15:57:01 -0400 | [diff] [blame] | 188 | rcu_read_lock(); |
| 189 | list_for_each_entry_rcu(ctx, &nfsi->open_files, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | state = ctx->state; |
| 191 | if (state == NULL) |
| 192 | continue; |
| 193 | if (!test_bit(NFS_DELEGATED_STATE, &state->flags)) |
| 194 | continue; |
Trond Myklebust | f8ebf7a | 2014-10-17 23:02:52 +0300 | [diff] [blame] | 195 | if (!nfs4_valid_open_stateid(state)) |
| 196 | continue; |
Trond Myklebust | f597c53 | 2012-03-04 18:13:56 -0500 | [diff] [blame] | 197 | if (!nfs4_stateid_match(&state->stateid, stateid)) |
Trond Myklebust | 9016302 | 2007-07-05 14:55:18 -0400 | [diff] [blame] | 198 | continue; |
Trond Myklebust | 0de4397 | 2018-09-02 15:57:01 -0400 | [diff] [blame] | 199 | if (!get_nfs_open_context(ctx)) |
| 200 | continue; |
| 201 | rcu_read_unlock(); |
Trond Myklebust | d25be54 | 2013-02-05 11:43:28 -0500 | [diff] [blame] | 202 | sp = state->owner; |
Trond Myklebust | 65b62a2 | 2013-02-07 10:54:07 -0500 | [diff] [blame] | 203 | /* Block nfs4_proc_unlck */ |
| 204 | mutex_lock(&sp->so_delegreturn_mutex); |
Trond Myklebust | d25be54 | 2013-02-05 11:43:28 -0500 | [diff] [blame] | 205 | seq = raw_seqcount_begin(&sp->so_reclaim_seqcount); |
Trond Myklebust | 5eb8d18 | 2019-07-19 14:08:37 -0400 | [diff] [blame] | 206 | err = nfs4_open_delegation_recall(ctx, state, stateid); |
Trond Myklebust | d25be54 | 2013-02-05 11:43:28 -0500 | [diff] [blame] | 207 | if (!err) |
Olga Kornievskaia | 44f411c | 2018-10-04 14:45:00 -0400 | [diff] [blame] | 208 | err = nfs_delegation_claim_locks(state, stateid); |
Trond Myklebust | d25be54 | 2013-02-05 11:43:28 -0500 | [diff] [blame] | 209 | if (!err && read_seqcount_retry(&sp->so_reclaim_seqcount, seq)) |
| 210 | err = -EAGAIN; |
Trond Myklebust | 65b62a2 | 2013-02-07 10:54:07 -0500 | [diff] [blame] | 211 | mutex_unlock(&sp->so_delegreturn_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | put_nfs_open_context(ctx); |
Trond Myklebust | 888e694 | 2005-11-04 15:38:11 -0500 | [diff] [blame] | 213 | if (err != 0) |
Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 214 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | goto again; |
| 216 | } |
Trond Myklebust | 0de4397 | 2018-09-02 15:57:01 -0400 | [diff] [blame] | 217 | rcu_read_unlock(); |
Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 218 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 221 | /** |
| 222 | * nfs_inode_reclaim_delegation - process a delegation reclaim request |
| 223 | * @inode: inode to process |
| 224 | * @cred: credential to use for request |
Trond Myklebust | 35156bf | 2018-03-20 17:03:13 -0400 | [diff] [blame] | 225 | * @type: delegation type |
| 226 | * @stateid: delegation stateid |
| 227 | * @pagemod_limit: write delegation "space_limit" |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 228 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | */ |
NeilBrown | a52458b | 2018-12-03 11:30:31 +1100 | [diff] [blame] | 230 | void nfs_inode_reclaim_delegation(struct inode *inode, const struct cred *cred, |
Trond Myklebust | 35156bf | 2018-03-20 17:03:13 -0400 | [diff] [blame] | 231 | fmode_t type, |
| 232 | const nfs4_stateid *stateid, |
| 233 | unsigned long pagemod_limit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | { |
Trond Myklebust | 8f649c3 | 2010-05-01 12:36:18 -0400 | [diff] [blame] | 235 | struct nfs_delegation *delegation; |
NeilBrown | a52458b | 2018-12-03 11:30:31 +1100 | [diff] [blame] | 236 | const struct cred *oldcred = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | |
Trond Myklebust | 8f649c3 | 2010-05-01 12:36:18 -0400 | [diff] [blame] | 238 | rcu_read_lock(); |
| 239 | delegation = rcu_dereference(NFS_I(inode)->delegation); |
| 240 | if (delegation != NULL) { |
| 241 | spin_lock(&delegation->lock); |
Trond Myklebust | 1deed57 | 2019-10-22 08:52:47 -0400 | [diff] [blame] | 242 | if (nfs4_is_valid_delegation(delegation, 0)) { |
Trond Myklebust | 35156bf | 2018-03-20 17:03:13 -0400 | [diff] [blame] | 243 | nfs4_stateid_copy(&delegation->stateid, stateid); |
| 244 | delegation->type = type; |
| 245 | delegation->pagemod_limit = pagemod_limit; |
Trond Myklebust | 8f649c3 | 2010-05-01 12:36:18 -0400 | [diff] [blame] | 246 | oldcred = delegation->cred; |
NeilBrown | a52458b | 2018-12-03 11:30:31 +1100 | [diff] [blame] | 247 | delegation->cred = get_cred(cred); |
Trond Myklebust | 8f649c3 | 2010-05-01 12:36:18 -0400 | [diff] [blame] | 248 | clear_bit(NFS_DELEGATION_NEED_RECLAIM, |
| 249 | &delegation->flags); |
Trond Myklebust | 8f649c3 | 2010-05-01 12:36:18 -0400 | [diff] [blame] | 250 | spin_unlock(&delegation->lock); |
Trond Myklebust | 8f649c3 | 2010-05-01 12:36:18 -0400 | [diff] [blame] | 251 | rcu_read_unlock(); |
NeilBrown | a52458b | 2018-12-03 11:30:31 +1100 | [diff] [blame] | 252 | put_cred(oldcred); |
Trond Myklebust | 35156bf | 2018-03-20 17:03:13 -0400 | [diff] [blame] | 253 | trace_nfs4_reclaim_delegation(inode, type); |
Trond Myklebust | b1a318d | 2016-09-22 13:39:12 -0400 | [diff] [blame] | 254 | return; |
Trond Myklebust | 8f649c3 | 2010-05-01 12:36:18 -0400 | [diff] [blame] | 255 | } |
Trond Myklebust | b1a318d | 2016-09-22 13:39:12 -0400 | [diff] [blame] | 256 | /* We appear to have raced with a delegation return. */ |
| 257 | spin_unlock(&delegation->lock); |
Trond Myklebust | 8f649c3 | 2010-05-01 12:36:18 -0400 | [diff] [blame] | 258 | } |
Trond Myklebust | b1a318d | 2016-09-22 13:39:12 -0400 | [diff] [blame] | 259 | rcu_read_unlock(); |
Trond Myklebust | 35156bf | 2018-03-20 17:03:13 -0400 | [diff] [blame] | 260 | nfs_inode_set_delegation(inode, cred, type, stateid, pagemod_limit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | } |
| 262 | |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 263 | static int nfs_do_return_delegation(struct inode *inode, struct nfs_delegation *delegation, int issync) |
| 264 | { |
Trond Myklebust | 5d63944 | 2020-02-13 14:51:07 -0500 | [diff] [blame] | 265 | const struct cred *cred; |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 266 | int res = 0; |
| 267 | |
Trond Myklebust | 5d63944 | 2020-02-13 14:51:07 -0500 | [diff] [blame] | 268 | if (!test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) { |
| 269 | spin_lock(&delegation->lock); |
| 270 | cred = get_cred(delegation->cred); |
| 271 | spin_unlock(&delegation->lock); |
| 272 | res = nfs4_proc_delegreturn(inode, cred, |
Trond Myklebust | 869f9df | 2014-11-10 18:43:56 -0500 | [diff] [blame] | 273 | &delegation->stateid, |
| 274 | issync); |
Trond Myklebust | 5d63944 | 2020-02-13 14:51:07 -0500 | [diff] [blame] | 275 | put_cred(cred); |
| 276 | } |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 277 | return res; |
| 278 | } |
| 279 | |
Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 280 | static struct inode *nfs_delegation_grab_inode(struct nfs_delegation *delegation) |
| 281 | { |
| 282 | struct inode *inode = NULL; |
| 283 | |
| 284 | spin_lock(&delegation->lock); |
| 285 | if (delegation->inode != NULL) |
| 286 | inode = igrab(delegation->inode); |
Trond Myklebust | 6f9449b | 2019-02-21 14:51:25 -0500 | [diff] [blame] | 287 | if (!inode) |
| 288 | set_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags); |
Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 289 | spin_unlock(&delegation->lock); |
| 290 | return inode; |
| 291 | } |
| 292 | |
Chuck Lever | dda4b22 | 2010-12-24 01:32:54 +0000 | [diff] [blame] | 293 | static struct nfs_delegation * |
Trond Myklebust | d25be54 | 2013-02-05 11:43:28 -0500 | [diff] [blame] | 294 | nfs_start_delegation_return_locked(struct nfs_inode *nfsi) |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 295 | { |
Trond Myklebust | d25be54 | 2013-02-05 11:43:28 -0500 | [diff] [blame] | 296 | struct nfs_delegation *ret = NULL; |
| 297 | struct nfs_delegation *delegation = rcu_dereference(nfsi->delegation); |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 298 | |
| 299 | if (delegation == NULL) |
Trond Myklebust | d25be54 | 2013-02-05 11:43:28 -0500 | [diff] [blame] | 300 | goto out; |
| 301 | spin_lock(&delegation->lock); |
Trond Myklebust | 8c75593 | 2020-02-13 14:51:06 -0500 | [diff] [blame] | 302 | if (!test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) { |
Trond Myklebust | be20037 | 2021-05-08 10:01:32 -0400 | [diff] [blame] | 303 | clear_bit(NFS_DELEGATION_RETURN_DELAYED, &delegation->flags); |
Trond Myklebust | 8c75593 | 2020-02-13 14:51:06 -0500 | [diff] [blame] | 304 | /* Refcount matched in nfs_end_delegation_return() */ |
| 305 | ret = nfs_get_delegation(delegation); |
| 306 | } |
Trond Myklebust | d25be54 | 2013-02-05 11:43:28 -0500 | [diff] [blame] | 307 | spin_unlock(&delegation->lock); |
Trond Myklebust | efeda80 | 2020-02-05 09:01:54 -0500 | [diff] [blame] | 308 | if (ret) |
| 309 | nfs_clear_verifier_delegated(&nfsi->vfs_inode); |
Trond Myklebust | d25be54 | 2013-02-05 11:43:28 -0500 | [diff] [blame] | 310 | out: |
| 311 | return ret; |
| 312 | } |
| 313 | |
| 314 | static struct nfs_delegation * |
| 315 | nfs_start_delegation_return(struct nfs_inode *nfsi) |
| 316 | { |
| 317 | struct nfs_delegation *delegation; |
| 318 | |
| 319 | rcu_read_lock(); |
| 320 | delegation = nfs_start_delegation_return_locked(nfsi); |
| 321 | rcu_read_unlock(); |
| 322 | return delegation; |
| 323 | } |
| 324 | |
Trond Myklebust | be20037 | 2021-05-08 10:01:32 -0400 | [diff] [blame] | 325 | static void nfs_abort_delegation_return(struct nfs_delegation *delegation, |
| 326 | struct nfs_client *clp, int err) |
Trond Myklebust | d25be54 | 2013-02-05 11:43:28 -0500 | [diff] [blame] | 327 | { |
Chuck Lever | dda4b22 | 2010-12-24 01:32:54 +0000 | [diff] [blame] | 328 | |
Trond Myklebust | 3431043 | 2008-12-23 15:21:38 -0500 | [diff] [blame] | 329 | spin_lock(&delegation->lock); |
Trond Myklebust | d25be54 | 2013-02-05 11:43:28 -0500 | [diff] [blame] | 330 | clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags); |
Trond Myklebust | be20037 | 2021-05-08 10:01:32 -0400 | [diff] [blame] | 331 | if (err == -EAGAIN) { |
| 332 | set_bit(NFS_DELEGATION_RETURN_DELAYED, &delegation->flags); |
| 333 | set_bit(NFS4CLNT_DELEGRETURN_DELAYED, &clp->cl_state); |
| 334 | } |
Trond Myklebust | d25be54 | 2013-02-05 11:43:28 -0500 | [diff] [blame] | 335 | spin_unlock(&delegation->lock); |
Trond Myklebust | d25be54 | 2013-02-05 11:43:28 -0500 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | static struct nfs_delegation * |
| 339 | nfs_detach_delegation_locked(struct nfs_inode *nfsi, |
| 340 | struct nfs_delegation *delegation, |
| 341 | struct nfs_client *clp) |
| 342 | { |
| 343 | struct nfs_delegation *deleg_cur = |
| 344 | rcu_dereference_protected(nfsi->delegation, |
| 345 | lockdep_is_held(&clp->cl_lock)); |
| 346 | |
| 347 | if (deleg_cur == NULL || delegation != deleg_cur) |
| 348 | return NULL; |
| 349 | |
| 350 | spin_lock(&delegation->lock); |
Trond Myklebust | f9e0cc9 | 2019-10-21 14:12:13 -0400 | [diff] [blame] | 351 | if (!delegation->inode) { |
| 352 | spin_unlock(&delegation->lock); |
| 353 | return NULL; |
| 354 | } |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 355 | list_del_rcu(&delegation->super_list); |
Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 356 | delegation->inode = NULL; |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 357 | rcu_assign_pointer(nfsi->delegation, NULL); |
Trond Myklebust | 3431043 | 2008-12-23 15:21:38 -0500 | [diff] [blame] | 358 | spin_unlock(&delegation->lock); |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 359 | return delegation; |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 360 | } |
| 361 | |
Chuck Lever | dda4b22 | 2010-12-24 01:32:54 +0000 | [diff] [blame] | 362 | static struct nfs_delegation *nfs_detach_delegation(struct nfs_inode *nfsi, |
Trond Myklebust | d25be54 | 2013-02-05 11:43:28 -0500 | [diff] [blame] | 363 | struct nfs_delegation *delegation, |
| 364 | struct nfs_server *server) |
Chuck Lever | dda4b22 | 2010-12-24 01:32:54 +0000 | [diff] [blame] | 365 | { |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 366 | struct nfs_client *clp = server->nfs_client; |
Chuck Lever | dda4b22 | 2010-12-24 01:32:54 +0000 | [diff] [blame] | 367 | |
| 368 | spin_lock(&clp->cl_lock); |
Trond Myklebust | d25be54 | 2013-02-05 11:43:28 -0500 | [diff] [blame] | 369 | delegation = nfs_detach_delegation_locked(nfsi, delegation, clp); |
Chuck Lever | dda4b22 | 2010-12-24 01:32:54 +0000 | [diff] [blame] | 370 | spin_unlock(&clp->cl_lock); |
| 371 | return delegation; |
| 372 | } |
| 373 | |
Trond Myklebust | d25be54 | 2013-02-05 11:43:28 -0500 | [diff] [blame] | 374 | static struct nfs_delegation * |
| 375 | nfs_inode_detach_delegation(struct inode *inode) |
| 376 | { |
| 377 | struct nfs_inode *nfsi = NFS_I(inode); |
| 378 | struct nfs_server *server = NFS_SERVER(inode); |
| 379 | struct nfs_delegation *delegation; |
| 380 | |
Trond Myklebust | ee05f45 | 2019-10-21 13:56:59 -0400 | [diff] [blame] | 381 | rcu_read_lock(); |
| 382 | delegation = rcu_dereference(nfsi->delegation); |
| 383 | if (delegation != NULL) |
| 384 | delegation = nfs_detach_delegation(nfsi, delegation, server); |
| 385 | rcu_read_unlock(); |
| 386 | return delegation; |
Trond Myklebust | d25be54 | 2013-02-05 11:43:28 -0500 | [diff] [blame] | 387 | } |
| 388 | |
Trond Myklebust | cf6726e | 2014-12-19 11:22:28 -0500 | [diff] [blame] | 389 | static void |
Trond Myklebust | 57f188e | 2020-01-27 12:44:41 -0500 | [diff] [blame] | 390 | nfs_update_delegation_cred(struct nfs_delegation *delegation, |
| 391 | const struct cred *cred) |
| 392 | { |
| 393 | const struct cred *old; |
| 394 | |
| 395 | if (cred_fscmp(delegation->cred, cred) != 0) { |
| 396 | old = xchg(&delegation->cred, get_cred(cred)); |
| 397 | put_cred(old); |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | static void |
Trond Myklebust | cf6726e | 2014-12-19 11:22:28 -0500 | [diff] [blame] | 402 | nfs_update_inplace_delegation(struct nfs_delegation *delegation, |
| 403 | const struct nfs_delegation *update) |
| 404 | { |
| 405 | if (nfs4_stateid_is_newer(&update->stateid, &delegation->stateid)) { |
| 406 | delegation->stateid.seqid = update->stateid.seqid; |
| 407 | smp_wmb(); |
| 408 | delegation->type = update->type; |
Trond Myklebust | 57f188e | 2020-01-27 12:44:41 -0500 | [diff] [blame] | 409 | delegation->pagemod_limit = update->pagemod_limit; |
| 410 | if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) { |
| 411 | delegation->change_attr = update->change_attr; |
| 412 | nfs_update_delegation_cred(delegation, update->cred); |
| 413 | /* smp_mb__before_atomic() is implicit due to xchg() */ |
| 414 | clear_bit(NFS_DELEGATION_REVOKED, &delegation->flags); |
Trond Myklebust | d2269ea | 2020-01-27 09:58:18 -0500 | [diff] [blame] | 415 | atomic_long_inc(&nfs_active_delegations); |
Trond Myklebust | 57f188e | 2020-01-27 12:44:41 -0500 | [diff] [blame] | 416 | } |
Trond Myklebust | cf6726e | 2014-12-19 11:22:28 -0500 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 420 | /** |
| 421 | * nfs_inode_set_delegation - set up a delegation on an inode |
| 422 | * @inode: inode to which delegation applies |
| 423 | * @cred: cred to use for subsequent delegation processing |
Trond Myklebust | 35156bf | 2018-03-20 17:03:13 -0400 | [diff] [blame] | 424 | * @type: delegation type |
| 425 | * @stateid: delegation stateid |
| 426 | * @pagemod_limit: write delegation "space_limit" |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 427 | * |
| 428 | * Returns zero on success, or a negative errno value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | */ |
NeilBrown | a52458b | 2018-12-03 11:30:31 +1100 | [diff] [blame] | 430 | int nfs_inode_set_delegation(struct inode *inode, const struct cred *cred, |
Trond Myklebust | 35156bf | 2018-03-20 17:03:13 -0400 | [diff] [blame] | 431 | fmode_t type, |
| 432 | const nfs4_stateid *stateid, |
| 433 | unsigned long pagemod_limit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | { |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 435 | struct nfs_server *server = NFS_SERVER(inode); |
| 436 | struct nfs_client *clp = server->nfs_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | struct nfs_inode *nfsi = NFS_I(inode); |
David Howells | 17d2c0a | 2010-05-01 12:37:18 -0400 | [diff] [blame] | 438 | struct nfs_delegation *delegation, *old_delegation; |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 439 | struct nfs_delegation *freeme = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | int status = 0; |
| 441 | |
Trond Myklebust | 8535b2b | 2010-05-13 12:51:01 -0400 | [diff] [blame] | 442 | delegation = kmalloc(sizeof(*delegation), GFP_NOFS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | if (delegation == NULL) |
| 444 | return -ENOMEM; |
Trond Myklebust | 35156bf | 2018-03-20 17:03:13 -0400 | [diff] [blame] | 445 | nfs4_stateid_copy(&delegation->stateid, stateid); |
Trond Myklebust | 8c75593 | 2020-02-13 14:51:06 -0500 | [diff] [blame] | 446 | refcount_set(&delegation->refcount, 1); |
Trond Myklebust | 35156bf | 2018-03-20 17:03:13 -0400 | [diff] [blame] | 447 | delegation->type = type; |
| 448 | delegation->pagemod_limit = pagemod_limit; |
Jeff Layton | 1eb5d98 | 2018-01-09 08:21:17 -0500 | [diff] [blame] | 449 | delegation->change_attr = inode_peek_iversion_raw(inode); |
NeilBrown | a52458b | 2018-12-03 11:30:31 +1100 | [diff] [blame] | 450 | delegation->cred = get_cred(cred); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | delegation->inode = inode; |
Trond Myklebust | b7391f4 | 2008-12-23 15:21:52 -0500 | [diff] [blame] | 452 | delegation->flags = 1<<NFS_DELEGATION_REFERENCED; |
Trond Myklebust | 3431043 | 2008-12-23 15:21:38 -0500 | [diff] [blame] | 453 | spin_lock_init(&delegation->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | |
| 455 | spin_lock(&clp->cl_lock); |
David Howells | 17d2c0a | 2010-05-01 12:37:18 -0400 | [diff] [blame] | 456 | old_delegation = rcu_dereference_protected(nfsi->delegation, |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 457 | lockdep_is_held(&clp->cl_lock)); |
Trond Myklebust | ee05f45 | 2019-10-21 13:56:59 -0400 | [diff] [blame] | 458 | if (old_delegation == NULL) |
| 459 | goto add_new; |
| 460 | /* Is this an update of the existing delegation? */ |
| 461 | if (nfs4_stateid_match_other(&old_delegation->stateid, |
| 462 | &delegation->stateid)) { |
| 463 | spin_lock(&old_delegation->lock); |
| 464 | nfs_update_inplace_delegation(old_delegation, |
| 465 | delegation); |
| 466 | spin_unlock(&old_delegation->lock); |
| 467 | goto out; |
| 468 | } |
| 469 | if (!test_bit(NFS_DELEGATION_REVOKED, &old_delegation->flags)) { |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 470 | /* |
| 471 | * Deal with broken servers that hand out two |
| 472 | * delegations for the same file. |
Trond Myklebust | 1728017 | 2012-03-11 13:11:00 -0400 | [diff] [blame] | 473 | * Allow for upgrades to a WRITE delegation, but |
| 474 | * nothing else. |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 475 | */ |
| 476 | dfprintk(FILE, "%s: server %s handed out " |
| 477 | "a duplicate delegation!\n", |
Harvey Harrison | 3110ff8 | 2008-05-02 13:42:44 -0700 | [diff] [blame] | 478 | __func__, clp->cl_hostname); |
Trond Myklebust | 1728017 | 2012-03-11 13:11:00 -0400 | [diff] [blame] | 479 | if (delegation->type == old_delegation->type || |
| 480 | !(delegation->type & FMODE_WRITE)) { |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 481 | freeme = delegation; |
| 482 | delegation = NULL; |
| 483 | goto out; |
| 484 | } |
Trond Myklebust | ade0464 | 2015-02-27 14:25:50 -0500 | [diff] [blame] | 485 | if (test_and_set_bit(NFS_DELEGATION_RETURNING, |
| 486 | &old_delegation->flags)) |
| 487 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | } |
Trond Myklebust | ee05f45 | 2019-10-21 13:56:59 -0400 | [diff] [blame] | 489 | freeme = nfs_detach_delegation_locked(nfsi, old_delegation, clp); |
| 490 | if (freeme == NULL) |
| 491 | goto out; |
| 492 | add_new: |
Trond Myklebust | cc7f2da | 2021-04-11 14:31:24 -0400 | [diff] [blame] | 493 | /* |
| 494 | * If we didn't revalidate the change attribute before setting |
| 495 | * the delegation, then pre-emptively ask for a full attribute |
| 496 | * cache revalidation. |
| 497 | */ |
| 498 | spin_lock(&inode->i_lock); |
| 499 | if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_CHANGE) |
| 500 | nfs_set_cache_invalid(inode, |
| 501 | NFS_INO_INVALID_ATIME | NFS_INO_INVALID_CTIME | |
| 502 | NFS_INO_INVALID_MTIME | NFS_INO_INVALID_SIZE | |
| 503 | NFS_INO_INVALID_BLOCKS | NFS_INO_INVALID_NLINK | |
| 504 | NFS_INO_INVALID_OTHER | NFS_INO_INVALID_DATA | |
| 505 | NFS_INO_INVALID_ACCESS | NFS_INO_INVALID_ACL | |
| 506 | NFS_INO_INVALID_XATTR); |
| 507 | spin_unlock(&inode->i_lock); |
| 508 | |
Trond Myklebust | 38942ba | 2015-03-04 15:59:05 -0500 | [diff] [blame] | 509 | list_add_tail_rcu(&delegation->super_list, &server->delegations); |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 510 | rcu_assign_pointer(nfsi->delegation, delegation); |
| 511 | delegation = NULL; |
Trond Myklebust | 412c77c | 2007-07-03 16:10:55 -0400 | [diff] [blame] | 512 | |
Trond Myklebust | d2269ea | 2020-01-27 09:58:18 -0500 | [diff] [blame] | 513 | atomic_long_inc(&nfs_active_delegations); |
| 514 | |
Trond Myklebust | 35156bf | 2018-03-20 17:03:13 -0400 | [diff] [blame] | 515 | trace_nfs4_set_delegation(inode, type); |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 516 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | 603c83d | 2007-10-18 19:59:20 -0400 | [diff] [blame] | 518 | if (delegation != NULL) |
Trond Myklebust | d2269ea | 2020-01-27 09:58:18 -0500 | [diff] [blame] | 519 | __nfs_free_delegation(delegation); |
Trond Myklebust | ee05f45 | 2019-10-21 13:56:59 -0400 | [diff] [blame] | 520 | if (freeme != NULL) { |
Trond Myklebust | 57bfa89 | 2008-01-25 16:38:18 -0500 | [diff] [blame] | 521 | nfs_do_return_delegation(inode, freeme, 0); |
Trond Myklebust | ee05f45 | 2019-10-21 13:56:59 -0400 | [diff] [blame] | 522 | nfs_free_delegation(freeme); |
| 523 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 524 | return status; |
| 525 | } |
| 526 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | /* |
| 528 | * Basic procedure for returning a delegation to the server |
| 529 | */ |
Trond Myklebust | d25be54 | 2013-02-05 11:43:28 -0500 | [diff] [blame] | 530 | static int nfs_end_delegation_return(struct inode *inode, struct nfs_delegation *delegation, int issync) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | { |
Trond Myklebust | d25be54 | 2013-02-05 11:43:28 -0500 | [diff] [blame] | 532 | struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; |
Trond Myklebust | 6b4befc | 2021-05-07 09:14:37 -0400 | [diff] [blame] | 533 | unsigned int mode = O_WRONLY | O_RDWR; |
Trond Myklebust | 869f9df | 2014-11-10 18:43:56 -0500 | [diff] [blame] | 534 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | |
Trond Myklebust | d25be54 | 2013-02-05 11:43:28 -0500 | [diff] [blame] | 536 | if (delegation == NULL) |
| 537 | return 0; |
Trond Myklebust | 6b4befc | 2021-05-07 09:14:37 -0400 | [diff] [blame] | 538 | |
| 539 | if (!issync) |
| 540 | mode |= O_NONBLOCK; |
| 541 | /* Recall of any remaining application leases */ |
| 542 | err = break_lease(inode, mode); |
| 543 | |
| 544 | while (err == 0) { |
Trond Myklebust | 869f9df | 2014-11-10 18:43:56 -0500 | [diff] [blame] | 545 | if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) |
| 546 | break; |
Trond Myklebust | 24311f8 | 2015-09-20 10:50:17 -0400 | [diff] [blame] | 547 | err = nfs_delegation_claim_opens(inode, &delegation->stateid, |
| 548 | delegation->type); |
Trond Myklebust | d25be54 | 2013-02-05 11:43:28 -0500 | [diff] [blame] | 549 | if (!issync || err != -EAGAIN) |
| 550 | break; |
| 551 | /* |
| 552 | * Guard against state recovery |
| 553 | */ |
| 554 | err = nfs4_wait_clnt_recover(clp); |
Trond Myklebust | 6b4befc | 2021-05-07 09:14:37 -0400 | [diff] [blame] | 555 | } |
Trond Myklebust | d25be54 | 2013-02-05 11:43:28 -0500 | [diff] [blame] | 556 | |
| 557 | if (err) { |
Trond Myklebust | be20037 | 2021-05-08 10:01:32 -0400 | [diff] [blame] | 558 | nfs_abort_delegation_return(delegation, clp, err); |
Trond Myklebust | d25be54 | 2013-02-05 11:43:28 -0500 | [diff] [blame] | 559 | goto out; |
| 560 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | |
Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 562 | err = nfs_do_return_delegation(inode, delegation, issync); |
| 563 | out: |
Trond Myklebust | 8c75593 | 2020-02-13 14:51:06 -0500 | [diff] [blame] | 564 | /* Refcount matched in nfs_start_delegation_return_locked() */ |
| 565 | nfs_put_delegation(delegation); |
Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 566 | return err; |
Trond Myklebust | 9016302 | 2007-07-05 14:55:18 -0400 | [diff] [blame] | 567 | } |
| 568 | |
Trond Myklebust | b757144 | 2013-04-03 14:33:49 -0400 | [diff] [blame] | 569 | static bool nfs_delegation_need_return(struct nfs_delegation *delegation) |
| 570 | { |
| 571 | bool ret = false; |
| 572 | |
| 573 | if (test_and_clear_bit(NFS_DELEGATION_RETURN, &delegation->flags)) |
| 574 | ret = true; |
Trond Myklebust | 0d10416 | 2020-01-27 09:58:16 -0500 | [diff] [blame] | 575 | else if (test_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags)) { |
Trond Myklebust | b757144 | 2013-04-03 14:33:49 -0400 | [diff] [blame] | 576 | struct inode *inode; |
| 577 | |
| 578 | spin_lock(&delegation->lock); |
| 579 | inode = delegation->inode; |
| 580 | if (inode && list_empty(&NFS_I(inode)->open_files)) |
| 581 | ret = true; |
| 582 | spin_unlock(&delegation->lock); |
| 583 | } |
Trond Myklebust | 0d10416 | 2020-01-27 09:58:16 -0500 | [diff] [blame] | 584 | if (ret) |
| 585 | clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags); |
Trond Myklebust | af20b7b | 2019-10-22 13:40:47 -0400 | [diff] [blame] | 586 | if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags) || |
Trond Myklebust | be20037 | 2021-05-08 10:01:32 -0400 | [diff] [blame] | 587 | test_bit(NFS_DELEGATION_RETURN_DELAYED, &delegation->flags) || |
Trond Myklebust | af20b7b | 2019-10-22 13:40:47 -0400 | [diff] [blame] | 588 | test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) |
| 589 | ret = false; |
| 590 | |
Trond Myklebust | b757144 | 2013-04-03 14:33:49 -0400 | [diff] [blame] | 591 | return ret; |
| 592 | } |
| 593 | |
Trond Myklebust | af3b61b | 2020-02-27 08:29:02 -0500 | [diff] [blame] | 594 | static int nfs_server_return_marked_delegations(struct nfs_server *server, |
| 595 | void __always_unused *data) |
| 596 | { |
| 597 | struct nfs_delegation *delegation; |
| 598 | struct nfs_delegation *prev; |
| 599 | struct inode *inode; |
| 600 | struct inode *place_holder = NULL; |
| 601 | struct nfs_delegation *place_holder_deleg = NULL; |
| 602 | int err = 0; |
| 603 | |
| 604 | restart: |
| 605 | /* |
| 606 | * To avoid quadratic looping we hold a reference |
| 607 | * to an inode place_holder. Each time we restart, we |
| 608 | * list delegation in the server from the delegations |
| 609 | * of that inode. |
| 610 | * prev is an RCU-protected pointer to a delegation which |
| 611 | * wasn't marked for return and might be a good choice for |
| 612 | * the next place_holder. |
| 613 | */ |
| 614 | prev = NULL; |
| 615 | delegation = NULL; |
| 616 | rcu_read_lock(); |
| 617 | if (place_holder) |
| 618 | delegation = rcu_dereference(NFS_I(place_holder)->delegation); |
| 619 | if (!delegation || delegation != place_holder_deleg) |
| 620 | delegation = list_entry_rcu(server->delegations.next, |
| 621 | struct nfs_delegation, super_list); |
| 622 | list_for_each_entry_from_rcu(delegation, &server->delegations, super_list) { |
| 623 | struct inode *to_put = NULL; |
| 624 | |
| 625 | if (test_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags)) |
| 626 | continue; |
| 627 | if (!nfs_delegation_need_return(delegation)) { |
| 628 | if (nfs4_is_valid_delegation(delegation, 0)) |
| 629 | prev = delegation; |
| 630 | continue; |
| 631 | } |
| 632 | |
| 633 | if (prev) { |
| 634 | struct inode *tmp = nfs_delegation_grab_inode(prev); |
| 635 | if (tmp) { |
| 636 | to_put = place_holder; |
| 637 | place_holder = tmp; |
| 638 | place_holder_deleg = prev; |
| 639 | } |
| 640 | } |
| 641 | |
| 642 | inode = nfs_delegation_grab_inode(delegation); |
| 643 | if (inode == NULL) { |
| 644 | rcu_read_unlock(); |
| 645 | iput(to_put); |
| 646 | goto restart; |
| 647 | } |
| 648 | delegation = nfs_start_delegation_return_locked(NFS_I(inode)); |
| 649 | rcu_read_unlock(); |
| 650 | |
| 651 | iput(to_put); |
| 652 | |
| 653 | err = nfs_end_delegation_return(inode, delegation, 0); |
| 654 | iput(inode); |
| 655 | cond_resched(); |
| 656 | if (!err) |
| 657 | goto restart; |
| 658 | set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state); |
| 659 | goto out; |
| 660 | } |
| 661 | rcu_read_unlock(); |
| 662 | out: |
| 663 | iput(place_holder); |
| 664 | return err; |
| 665 | } |
| 666 | |
Trond Myklebust | be20037 | 2021-05-08 10:01:32 -0400 | [diff] [blame] | 667 | static bool nfs_server_clear_delayed_delegations(struct nfs_server *server) |
| 668 | { |
| 669 | struct nfs_delegation *d; |
| 670 | bool ret = false; |
| 671 | |
| 672 | list_for_each_entry_rcu (d, &server->delegations, super_list) { |
| 673 | if (!test_bit(NFS_DELEGATION_RETURN_DELAYED, &d->flags)) |
| 674 | continue; |
| 675 | nfs_mark_return_delegation(server, d); |
| 676 | clear_bit(NFS_DELEGATION_RETURN_DELAYED, &d->flags); |
| 677 | ret = true; |
| 678 | } |
| 679 | return ret; |
| 680 | } |
| 681 | |
| 682 | static bool nfs_client_clear_delayed_delegations(struct nfs_client *clp) |
| 683 | { |
| 684 | struct nfs_server *server; |
| 685 | bool ret = false; |
| 686 | |
| 687 | if (!test_and_clear_bit(NFS4CLNT_DELEGRETURN_DELAYED, &clp->cl_state)) |
| 688 | goto out; |
| 689 | rcu_read_lock(); |
| 690 | list_for_each_entry_rcu (server, &clp->cl_superblocks, client_link) { |
| 691 | if (nfs_server_clear_delayed_delegations(server)) |
| 692 | ret = true; |
| 693 | } |
| 694 | rcu_read_unlock(); |
| 695 | out: |
| 696 | return ret; |
| 697 | } |
| 698 | |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 699 | /** |
| 700 | * nfs_client_return_marked_delegations - return previously marked delegations |
| 701 | * @clp: nfs_client to process |
| 702 | * |
Trond Myklebust | dc327ed | 2012-05-06 19:46:30 -0400 | [diff] [blame] | 703 | * Note that this function is designed to be called by the state |
| 704 | * manager thread. For this reason, it cannot flush the dirty data, |
| 705 | * since that could deadlock in case of a state recovery error. |
| 706 | * |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 707 | * Returns zero on success, or a negative errno value. |
Trond Myklebust | 515d861 | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 708 | */ |
Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 709 | int nfs_client_return_marked_delegations(struct nfs_client *clp) |
Trond Myklebust | 515d861 | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 710 | { |
Trond Myklebust | be20037 | 2021-05-08 10:01:32 -0400 | [diff] [blame] | 711 | int err = nfs_client_for_each_server( |
| 712 | clp, nfs_server_return_marked_delegations, NULL); |
| 713 | if (err) |
| 714 | return err; |
| 715 | /* If a return was delayed, sleep to prevent hard looping */ |
| 716 | if (nfs_client_clear_delayed_delegations(clp)) |
| 717 | ssleep(1); |
| 718 | return 0; |
Trond Myklebust | 515d861 | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 719 | } |
| 720 | |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 721 | /** |
Trond Myklebust | b47e0e4 | 2019-10-21 14:04:00 -0400 | [diff] [blame] | 722 | * nfs_inode_evict_delegation - return delegation, don't reclaim opens |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 723 | * @inode: inode to process |
| 724 | * |
| 725 | * Does not protect against delegation reclaims, therefore really only safe |
Trond Myklebust | b47e0e4 | 2019-10-21 14:04:00 -0400 | [diff] [blame] | 726 | * to be called from nfs4_clear_inode(). Guaranteed to always free |
| 727 | * the delegation structure. |
Trond Myklebust | e6f8107 | 2008-01-24 18:14:34 -0500 | [diff] [blame] | 728 | */ |
Trond Myklebust | b47e0e4 | 2019-10-21 14:04:00 -0400 | [diff] [blame] | 729 | void nfs_inode_evict_delegation(struct inode *inode) |
Trond Myklebust | e6f8107 | 2008-01-24 18:14:34 -0500 | [diff] [blame] | 730 | { |
Trond Myklebust | e6f8107 | 2008-01-24 18:14:34 -0500 | [diff] [blame] | 731 | struct nfs_delegation *delegation; |
| 732 | |
Trond Myklebust | d25be54 | 2013-02-05 11:43:28 -0500 | [diff] [blame] | 733 | delegation = nfs_inode_detach_delegation(inode); |
Trond Myklebust | b47e0e4 | 2019-10-21 14:04:00 -0400 | [diff] [blame] | 734 | if (delegation != NULL) { |
Trond Myklebust | f885ea6 | 2020-01-27 09:58:15 -0500 | [diff] [blame] | 735 | set_bit(NFS_DELEGATION_RETURNING, &delegation->flags); |
Trond Myklebust | b47e0e4 | 2019-10-21 14:04:00 -0400 | [diff] [blame] | 736 | set_bit(NFS_DELEGATION_INODE_FREEING, &delegation->flags); |
Trond Myklebust | 5fcdfac | 2015-03-25 13:19:42 -0400 | [diff] [blame] | 737 | nfs_do_return_delegation(inode, delegation, 1); |
Trond Myklebust | ee05f45 | 2019-10-21 13:56:59 -0400 | [diff] [blame] | 738 | nfs_free_delegation(delegation); |
Trond Myklebust | b47e0e4 | 2019-10-21 14:04:00 -0400 | [diff] [blame] | 739 | } |
Trond Myklebust | e6f8107 | 2008-01-24 18:14:34 -0500 | [diff] [blame] | 740 | } |
| 741 | |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 742 | /** |
Trond Myklebust | 6453bcd | 2021-03-04 20:29:50 -0500 | [diff] [blame] | 743 | * nfs4_inode_return_delegation - synchronously return a delegation |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 744 | * @inode: inode to process |
| 745 | * |
Trond Myklebust | c57d1bc | 2012-05-06 19:34:17 -0400 | [diff] [blame] | 746 | * This routine will always flush any dirty data to disk on the |
| 747 | * assumption that if we need to return the delegation, then |
| 748 | * we should stop caching. |
| 749 | * |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 750 | * Returns zero on success, or a negative errno value. |
| 751 | */ |
Bryan Schumaker | 57ec14c | 2012-06-20 15:53:44 -0400 | [diff] [blame] | 752 | int nfs4_inode_return_delegation(struct inode *inode) |
Trond Myklebust | 9016302 | 2007-07-05 14:55:18 -0400 | [diff] [blame] | 753 | { |
Trond Myklebust | 9016302 | 2007-07-05 14:55:18 -0400 | [diff] [blame] | 754 | struct nfs_inode *nfsi = NFS_I(inode); |
| 755 | struct nfs_delegation *delegation; |
Trond Myklebust | 9016302 | 2007-07-05 14:55:18 -0400 | [diff] [blame] | 756 | |
Trond Myklebust | d25be54 | 2013-02-05 11:43:28 -0500 | [diff] [blame] | 757 | delegation = nfs_start_delegation_return(nfsi); |
Trond Myklebust | 6e176d4 | 2021-10-10 10:58:12 +0200 | [diff] [blame] | 758 | if (delegation != NULL) { |
| 759 | /* Synchronous recall of any application leases */ |
| 760 | break_lease(inode, O_WRONLY | O_RDWR); |
| 761 | if (S_ISREG(inode->i_mode)) |
| 762 | nfs_wb_all(inode); |
Trond Myklebust | 6b4befc | 2021-05-07 09:14:37 -0400 | [diff] [blame] | 763 | return nfs_end_delegation_return(inode, delegation, 1); |
Trond Myklebust | 6e176d4 | 2021-10-10 10:58:12 +0200 | [diff] [blame] | 764 | } |
Trond Myklebust | 6b4befc | 2021-05-07 09:14:37 -0400 | [diff] [blame] | 765 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | } |
| 767 | |
Trond Myklebust | c01d364 | 2018-03-20 16:43:20 -0400 | [diff] [blame] | 768 | /** |
Trond Myklebust | 6453bcd | 2021-03-04 20:29:50 -0500 | [diff] [blame] | 769 | * nfs4_inode_return_delegation_on_close - asynchronously return a delegation |
Trond Myklebust | b7b7dac | 2020-01-27 09:58:17 -0500 | [diff] [blame] | 770 | * @inode: inode to process |
| 771 | * |
| 772 | * This routine is called on file close in order to determine if the |
| 773 | * inode delegation needs to be returned immediately. |
| 774 | */ |
| 775 | void nfs4_inode_return_delegation_on_close(struct inode *inode) |
| 776 | { |
| 777 | struct nfs_delegation *delegation; |
| 778 | struct nfs_delegation *ret = NULL; |
| 779 | |
| 780 | if (!inode) |
| 781 | return; |
| 782 | rcu_read_lock(); |
| 783 | delegation = nfs4_get_valid_delegation(inode); |
| 784 | if (!delegation) |
| 785 | goto out; |
Trond Myklebust | 10717f4 | 2020-01-27 09:58:19 -0500 | [diff] [blame] | 786 | if (test_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags) || |
| 787 | atomic_long_read(&nfs_active_delegations) >= nfs_delegation_watermark) { |
Trond Myklebust | b7b7dac | 2020-01-27 09:58:17 -0500 | [diff] [blame] | 788 | spin_lock(&delegation->lock); |
| 789 | if (delegation->inode && |
| 790 | list_empty(&NFS_I(inode)->open_files) && |
| 791 | !test_and_set_bit(NFS_DELEGATION_RETURNING, &delegation->flags)) { |
| 792 | clear_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags); |
Trond Myklebust | 8c75593 | 2020-02-13 14:51:06 -0500 | [diff] [blame] | 793 | /* Refcount matched in nfs_end_delegation_return() */ |
| 794 | ret = nfs_get_delegation(delegation); |
Trond Myklebust | b7b7dac | 2020-01-27 09:58:17 -0500 | [diff] [blame] | 795 | } |
| 796 | spin_unlock(&delegation->lock); |
Trond Myklebust | efeda80 | 2020-02-05 09:01:54 -0500 | [diff] [blame] | 797 | if (ret) |
| 798 | nfs_clear_verifier_delegated(inode); |
Trond Myklebust | b7b7dac | 2020-01-27 09:58:17 -0500 | [diff] [blame] | 799 | } |
| 800 | out: |
| 801 | rcu_read_unlock(); |
| 802 | nfs_end_delegation_return(inode, ret, 0); |
| 803 | } |
| 804 | |
| 805 | /** |
Trond Myklebust | c01d364 | 2018-03-20 16:43:20 -0400 | [diff] [blame] | 806 | * nfs4_inode_make_writeable |
| 807 | * @inode: pointer to inode |
| 808 | * |
| 809 | * Make the inode writeable by returning the delegation if necessary |
| 810 | * |
| 811 | * Returns zero on success, or a negative errno value. |
| 812 | */ |
| 813 | int nfs4_inode_make_writeable(struct inode *inode) |
| 814 | { |
Trond Myklebust | 3887ce1 | 2019-10-27 13:48:18 -0400 | [diff] [blame] | 815 | struct nfs_delegation *delegation; |
| 816 | |
| 817 | rcu_read_lock(); |
| 818 | delegation = nfs4_get_valid_delegation(inode); |
| 819 | if (delegation == NULL || |
| 820 | (nfs4_has_session(NFS_SERVER(inode)->nfs_client) && |
| 821 | (delegation->type & FMODE_WRITE))) { |
| 822 | rcu_read_unlock(); |
| 823 | return 0; |
| 824 | } |
| 825 | rcu_read_unlock(); |
| 826 | return nfs4_inode_return_delegation(inode); |
Trond Myklebust | c01d364 | 2018-03-20 16:43:20 -0400 | [diff] [blame] | 827 | } |
| 828 | |
Trond Myklebust | b757144 | 2013-04-03 14:33:49 -0400 | [diff] [blame] | 829 | static void nfs_mark_return_if_closed_delegation(struct nfs_server *server, |
| 830 | struct nfs_delegation *delegation) |
| 831 | { |
| 832 | set_bit(NFS_DELEGATION_RETURN_IF_CLOSED, &delegation->flags); |
| 833 | set_bit(NFS4CLNT_DELEGRETURN, &server->nfs_client->cl_state); |
| 834 | } |
| 835 | |
Trond Myklebust | 5c31e23 | 2013-04-03 19:04:58 -0400 | [diff] [blame] | 836 | static bool nfs_server_mark_return_all_delegations(struct nfs_server *server) |
| 837 | { |
| 838 | struct nfs_delegation *delegation; |
| 839 | bool ret = false; |
| 840 | |
| 841 | list_for_each_entry_rcu(delegation, &server->delegations, super_list) { |
| 842 | nfs_mark_return_delegation(server, delegation); |
| 843 | ret = true; |
| 844 | } |
| 845 | return ret; |
| 846 | } |
| 847 | |
Trond Myklebust | b02ba0b | 2013-04-03 19:23:58 -0400 | [diff] [blame] | 848 | static void nfs_client_mark_return_all_delegations(struct nfs_client *clp) |
| 849 | { |
| 850 | struct nfs_server *server; |
| 851 | |
| 852 | rcu_read_lock(); |
| 853 | list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) |
| 854 | nfs_server_mark_return_all_delegations(server); |
| 855 | rcu_read_unlock(); |
| 856 | } |
| 857 | |
| 858 | static void nfs_delegation_run_state_manager(struct nfs_client *clp) |
| 859 | { |
| 860 | if (test_bit(NFS4CLNT_DELEGRETURN, &clp->cl_state)) |
| 861 | nfs4_schedule_state_manager(clp); |
| 862 | } |
| 863 | |
| 864 | /** |
| 865 | * nfs_expire_all_delegations |
| 866 | * @clp: client to process |
| 867 | * |
| 868 | */ |
| 869 | void nfs_expire_all_delegations(struct nfs_client *clp) |
| 870 | { |
| 871 | nfs_client_mark_return_all_delegations(clp); |
| 872 | nfs_delegation_run_state_manager(clp); |
| 873 | } |
| 874 | |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 875 | /** |
Trond Myklebust | 6453bcd | 2021-03-04 20:29:50 -0500 | [diff] [blame] | 876 | * nfs_server_return_all_delegations - return delegations for one superblock |
Trond Myklebust | 302fad7 | 2019-02-18 13:32:38 -0500 | [diff] [blame] | 877 | * @server: pointer to nfs_server to process |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 878 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | */ |
Bryan Schumaker | eeebf91 | 2012-06-20 15:53:41 -0400 | [diff] [blame] | 880 | void nfs_server_return_all_delegations(struct nfs_server *server) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 881 | { |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 882 | struct nfs_client *clp = server->nfs_client; |
Trond Myklebust | 5c31e23 | 2013-04-03 19:04:58 -0400 | [diff] [blame] | 883 | bool need_wait; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | |
| 885 | if (clp == NULL) |
| 886 | return; |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 887 | |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 888 | rcu_read_lock(); |
Trond Myklebust | 5c31e23 | 2013-04-03 19:04:58 -0400 | [diff] [blame] | 889 | need_wait = nfs_server_mark_return_all_delegations(server); |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 890 | rcu_read_unlock(); |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 891 | |
Trond Myklebust | 5c31e23 | 2013-04-03 19:04:58 -0400 | [diff] [blame] | 892 | if (need_wait) { |
Trond Myklebust | d18cc1f | 2009-12-03 08:10:17 -0500 | [diff] [blame] | 893 | nfs4_schedule_state_manager(clp); |
Trond Myklebust | 5c31e23 | 2013-04-03 19:04:58 -0400 | [diff] [blame] | 894 | nfs4_wait_clnt_recover(clp); |
| 895 | } |
Trond Myklebust | 515d861 | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 896 | } |
| 897 | |
Trond Myklebust | 826e001 | 2013-04-03 19:27:52 -0400 | [diff] [blame] | 898 | static void nfs_mark_return_unused_delegation_types(struct nfs_server *server, |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 899 | fmode_t flags) |
Trond Myklebust | 515d861 | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 900 | { |
| 901 | struct nfs_delegation *delegation; |
| 902 | |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 903 | list_for_each_entry_rcu(delegation, &server->delegations, super_list) { |
Alexandros Batsakis | c79571a | 2009-12-05 13:20:52 -0500 | [diff] [blame] | 904 | if ((delegation->type == (FMODE_READ|FMODE_WRITE)) && !(flags & FMODE_WRITE)) |
| 905 | continue; |
| 906 | if (delegation->type & flags) |
Trond Myklebust | 826e001 | 2013-04-03 19:27:52 -0400 | [diff] [blame] | 907 | nfs_mark_return_if_closed_delegation(server, delegation); |
Trond Myklebust | 707fb4b | 2008-12-23 15:21:47 -0500 | [diff] [blame] | 908 | } |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 909 | } |
| 910 | |
Trond Myklebust | 826e001 | 2013-04-03 19:27:52 -0400 | [diff] [blame] | 911 | static void nfs_client_mark_return_unused_delegation_types(struct nfs_client *clp, |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 912 | fmode_t flags) |
| 913 | { |
| 914 | struct nfs_server *server; |
| 915 | |
| 916 | rcu_read_lock(); |
| 917 | list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) |
Trond Myklebust | 826e001 | 2013-04-03 19:27:52 -0400 | [diff] [blame] | 918 | nfs_mark_return_unused_delegation_types(server, flags); |
Trond Myklebust | 515d861 | 2008-12-23 15:21:46 -0500 | [diff] [blame] | 919 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | } |
| 921 | |
Trond Myklebust | ee05f45 | 2019-10-21 13:56:59 -0400 | [diff] [blame] | 922 | static void nfs_revoke_delegation(struct inode *inode, |
Trond Myklebust | 41020b6 | 2016-09-22 13:38:58 -0400 | [diff] [blame] | 923 | const nfs4_stateid *stateid) |
| 924 | { |
| 925 | struct nfs_delegation *delegation; |
Trond Myklebust | 7f04883 | 2016-09-22 13:39:14 -0400 | [diff] [blame] | 926 | nfs4_stateid tmp; |
Trond Myklebust | 41020b6 | 2016-09-22 13:38:58 -0400 | [diff] [blame] | 927 | bool ret = false; |
| 928 | |
| 929 | rcu_read_lock(); |
| 930 | delegation = rcu_dereference(NFS_I(inode)->delegation); |
| 931 | if (delegation == NULL) |
| 932 | goto out; |
Trond Myklebust | 7f04883 | 2016-09-22 13:39:14 -0400 | [diff] [blame] | 933 | if (stateid == NULL) { |
| 934 | nfs4_stateid_copy(&tmp, &delegation->stateid); |
| 935 | stateid = &tmp; |
Trond Myklebust | f2d47b5 | 2019-10-21 14:15:32 -0400 | [diff] [blame] | 936 | } else { |
| 937 | if (!nfs4_stateid_match_other(stateid, &delegation->stateid)) |
| 938 | goto out; |
| 939 | spin_lock(&delegation->lock); |
| 940 | if (stateid->seqid) { |
| 941 | if (nfs4_stateid_is_newer(&delegation->stateid, stateid)) { |
| 942 | spin_unlock(&delegation->lock); |
| 943 | goto out; |
| 944 | } |
| 945 | delegation->stateid.seqid = stateid->seqid; |
| 946 | } |
| 947 | spin_unlock(&delegation->lock); |
| 948 | } |
Trond Myklebust | d2269ea | 2020-01-27 09:58:18 -0500 | [diff] [blame] | 949 | nfs_mark_delegation_revoked(delegation); |
Trond Myklebust | 41020b6 | 2016-09-22 13:38:58 -0400 | [diff] [blame] | 950 | ret = true; |
| 951 | out: |
| 952 | rcu_read_unlock(); |
Trond Myklebust | 7f04883 | 2016-09-22 13:39:14 -0400 | [diff] [blame] | 953 | if (ret) |
| 954 | nfs_inode_find_state_and_recover(inode, stateid); |
Trond Myklebust | 41020b6 | 2016-09-22 13:38:58 -0400 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | void nfs_remove_bad_delegation(struct inode *inode, |
| 958 | const nfs4_stateid *stateid) |
Trond Myklebust | a1d0b5e | 2012-03-05 19:56:44 -0500 | [diff] [blame] | 959 | { |
Trond Myklebust | ee05f45 | 2019-10-21 13:56:59 -0400 | [diff] [blame] | 960 | nfs_revoke_delegation(inode, stateid); |
Trond Myklebust | a1d0b5e | 2012-03-05 19:56:44 -0500 | [diff] [blame] | 961 | } |
Andy Adamson | 9cb8196 | 2012-03-07 10:49:41 -0500 | [diff] [blame] | 962 | EXPORT_SYMBOL_GPL(nfs_remove_bad_delegation); |
Trond Myklebust | a1d0b5e | 2012-03-05 19:56:44 -0500 | [diff] [blame] | 963 | |
Trond Myklebust | d51f91d | 2019-10-21 14:22:14 -0400 | [diff] [blame] | 964 | void nfs_delegation_mark_returned(struct inode *inode, |
| 965 | const nfs4_stateid *stateid) |
| 966 | { |
| 967 | struct nfs_delegation *delegation; |
| 968 | |
| 969 | if (!inode) |
| 970 | return; |
| 971 | |
| 972 | rcu_read_lock(); |
| 973 | delegation = rcu_dereference(NFS_I(inode)->delegation); |
| 974 | if (!delegation) |
| 975 | goto out_rcu_unlock; |
| 976 | |
| 977 | spin_lock(&delegation->lock); |
| 978 | if (!nfs4_stateid_match_other(stateid, &delegation->stateid)) |
| 979 | goto out_spin_unlock; |
| 980 | if (stateid->seqid) { |
| 981 | /* If delegation->stateid is newer, dont mark as returned */ |
| 982 | if (nfs4_stateid_is_newer(&delegation->stateid, stateid)) |
| 983 | goto out_clear_returning; |
| 984 | if (delegation->stateid.seqid != stateid->seqid) |
| 985 | delegation->stateid.seqid = stateid->seqid; |
| 986 | } |
| 987 | |
Trond Myklebust | d2269ea | 2020-01-27 09:58:18 -0500 | [diff] [blame] | 988 | nfs_mark_delegation_revoked(delegation); |
Trond Myklebust | d51f91d | 2019-10-21 14:22:14 -0400 | [diff] [blame] | 989 | |
| 990 | out_clear_returning: |
| 991 | clear_bit(NFS_DELEGATION_RETURNING, &delegation->flags); |
| 992 | out_spin_unlock: |
| 993 | spin_unlock(&delegation->lock); |
| 994 | out_rcu_unlock: |
| 995 | rcu_read_unlock(); |
| 996 | |
| 997 | nfs_inode_find_state_and_recover(inode, stateid); |
| 998 | } |
| 999 | |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 1000 | /** |
Trond Myklebust | 826e001 | 2013-04-03 19:27:52 -0400 | [diff] [blame] | 1001 | * nfs_expire_unused_delegation_types |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 1002 | * @clp: client to process |
| 1003 | * @flags: delegation types to expire |
| 1004 | * |
| 1005 | */ |
Trond Myklebust | 826e001 | 2013-04-03 19:27:52 -0400 | [diff] [blame] | 1006 | void nfs_expire_unused_delegation_types(struct nfs_client *clp, fmode_t flags) |
Alexandros Batsakis | c79571a | 2009-12-05 13:20:52 -0500 | [diff] [blame] | 1007 | { |
Trond Myklebust | 826e001 | 2013-04-03 19:27:52 -0400 | [diff] [blame] | 1008 | nfs_client_mark_return_unused_delegation_types(clp, flags); |
Alexandros Batsakis | c79571a | 2009-12-05 13:20:52 -0500 | [diff] [blame] | 1009 | nfs_delegation_run_state_manager(clp); |
| 1010 | } |
| 1011 | |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 1012 | static void nfs_mark_return_unreferenced_delegations(struct nfs_server *server) |
Trond Myklebust | b7391f4 | 2008-12-23 15:21:52 -0500 | [diff] [blame] | 1013 | { |
| 1014 | struct nfs_delegation *delegation; |
| 1015 | |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 1016 | list_for_each_entry_rcu(delegation, &server->delegations, super_list) { |
Trond Myklebust | b7391f4 | 2008-12-23 15:21:52 -0500 | [diff] [blame] | 1017 | if (test_and_clear_bit(NFS_DELEGATION_REFERENCED, &delegation->flags)) |
| 1018 | continue; |
Trond Myklebust | b757144 | 2013-04-03 14:33:49 -0400 | [diff] [blame] | 1019 | nfs_mark_return_if_closed_delegation(server, delegation); |
Trond Myklebust | b7391f4 | 2008-12-23 15:21:52 -0500 | [diff] [blame] | 1020 | } |
Trond Myklebust | b7391f4 | 2008-12-23 15:21:52 -0500 | [diff] [blame] | 1021 | } |
| 1022 | |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 1023 | /** |
| 1024 | * nfs_expire_unreferenced_delegations - Eliminate unused delegations |
| 1025 | * @clp: nfs_client to process |
| 1026 | * |
| 1027 | */ |
Trond Myklebust | b7391f4 | 2008-12-23 15:21:52 -0500 | [diff] [blame] | 1028 | void nfs_expire_unreferenced_delegations(struct nfs_client *clp) |
| 1029 | { |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 1030 | struct nfs_server *server; |
| 1031 | |
| 1032 | rcu_read_lock(); |
| 1033 | list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) |
| 1034 | nfs_mark_return_unreferenced_delegations(server); |
| 1035 | rcu_read_unlock(); |
| 1036 | |
Trond Myklebust | b7391f4 | 2008-12-23 15:21:52 -0500 | [diff] [blame] | 1037 | nfs_delegation_run_state_manager(clp); |
| 1038 | } |
| 1039 | |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 1040 | /** |
| 1041 | * nfs_async_inode_return_delegation - asynchronously return a delegation |
| 1042 | * @inode: inode to process |
Trond Myklebust | 8e663f0 | 2012-03-04 18:13:56 -0500 | [diff] [blame] | 1043 | * @stateid: state ID information |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 1044 | * |
| 1045 | * Returns zero on success, or a negative errno value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1046 | */ |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 1047 | int nfs_async_inode_return_delegation(struct inode *inode, |
| 1048 | const nfs4_stateid *stateid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1049 | { |
Trond Myklebust | ed1e6211 | 2011-07-25 15:37:29 -0400 | [diff] [blame] | 1050 | struct nfs_server *server = NFS_SERVER(inode); |
| 1051 | struct nfs_client *clp = server->nfs_client; |
Trond Myklebust | 6411bd4 | 2008-12-23 15:21:51 -0500 | [diff] [blame] | 1052 | struct nfs_delegation *delegation; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | |
Trond Myklebust | 6411bd4 | 2008-12-23 15:21:51 -0500 | [diff] [blame] | 1054 | rcu_read_lock(); |
Trond Myklebust | 457a504 | 2019-10-22 08:46:06 -0400 | [diff] [blame] | 1055 | delegation = nfs4_get_valid_delegation(inode); |
Trond Myklebust | 755a48a | 2014-03-02 22:03:12 -0500 | [diff] [blame] | 1056 | if (delegation == NULL) |
| 1057 | goto out_enoent; |
Trond Myklebust | 4816fda | 2015-09-20 14:58:42 -0400 | [diff] [blame] | 1058 | if (stateid != NULL && |
| 1059 | !clp->cl_mvops->match_stateid(&delegation->stateid, stateid)) |
Trond Myklebust | 755a48a | 2014-03-02 22:03:12 -0500 | [diff] [blame] | 1060 | goto out_enoent; |
Trond Myklebust | ed1e6211 | 2011-07-25 15:37:29 -0400 | [diff] [blame] | 1061 | nfs_mark_return_delegation(server, delegation); |
Trond Myklebust | 6411bd4 | 2008-12-23 15:21:51 -0500 | [diff] [blame] | 1062 | rcu_read_unlock(); |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 1063 | |
Trond Myklebust | 6b4befc | 2021-05-07 09:14:37 -0400 | [diff] [blame] | 1064 | /* If there are any application leases or delegations, recall them */ |
| 1065 | break_lease(inode, O_WRONLY | O_RDWR | O_NONBLOCK); |
| 1066 | |
Trond Myklebust | 6411bd4 | 2008-12-23 15:21:51 -0500 | [diff] [blame] | 1067 | nfs_delegation_run_state_manager(clp); |
| 1068 | return 0; |
Trond Myklebust | 755a48a | 2014-03-02 22:03:12 -0500 | [diff] [blame] | 1069 | out_enoent: |
| 1070 | rcu_read_unlock(); |
| 1071 | return -ENOENT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1072 | } |
| 1073 | |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 1074 | static struct inode * |
| 1075 | nfs_delegation_find_inode_server(struct nfs_server *server, |
| 1076 | const struct nfs_fh *fhandle) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | { |
| 1078 | struct nfs_delegation *delegation; |
Trond Myklebust | 113aac6 | 2021-01-10 15:46:06 -0500 | [diff] [blame] | 1079 | struct super_block *freeme = NULL; |
| 1080 | struct inode *res = NULL; |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 1081 | |
| 1082 | list_for_each_entry_rcu(delegation, &server->delegations, super_list) { |
Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 1083 | spin_lock(&delegation->lock); |
| 1084 | if (delegation->inode != NULL && |
Trond Myklebust | 457a504 | 2019-10-22 08:46:06 -0400 | [diff] [blame] | 1085 | !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) && |
Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 1086 | nfs_compare_fh(fhandle, &NFS_I(delegation->inode)->fh) == 0) { |
Trond Myklebust | 113aac6 | 2021-01-10 15:46:06 -0500 | [diff] [blame] | 1087 | if (nfs_sb_active(server->super)) { |
| 1088 | freeme = server->super; |
| 1089 | res = igrab(delegation->inode); |
| 1090 | } |
Trond Myklebust | 6c34265 | 2018-06-07 14:22:00 -0400 | [diff] [blame] | 1091 | spin_unlock(&delegation->lock); |
| 1092 | if (res != NULL) |
| 1093 | return res; |
Trond Myklebust | e39d8a1 | 2018-11-13 16:37:54 -0500 | [diff] [blame] | 1094 | if (freeme) { |
| 1095 | rcu_read_unlock(); |
Trond Myklebust | 113aac6 | 2021-01-10 15:46:06 -0500 | [diff] [blame] | 1096 | nfs_sb_deactive(freeme); |
Trond Myklebust | e39d8a1 | 2018-11-13 16:37:54 -0500 | [diff] [blame] | 1097 | rcu_read_lock(); |
| 1098 | } |
Trond Myklebust | 6c34265 | 2018-06-07 14:22:00 -0400 | [diff] [blame] | 1099 | return ERR_PTR(-EAGAIN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | } |
Trond Myklebust | 86e8948 | 2008-12-23 15:21:39 -0500 | [diff] [blame] | 1101 | spin_unlock(&delegation->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | } |
Trond Myklebust | 6c34265 | 2018-06-07 14:22:00 -0400 | [diff] [blame] | 1103 | return ERR_PTR(-ENOENT); |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | /** |
| 1107 | * nfs_delegation_find_inode - retrieve the inode associated with a delegation |
| 1108 | * @clp: client state handle |
| 1109 | * @fhandle: filehandle from a delegation recall |
| 1110 | * |
| 1111 | * Returns pointer to inode matching "fhandle," or NULL if a matching inode |
| 1112 | * cannot be found. |
| 1113 | */ |
| 1114 | struct inode *nfs_delegation_find_inode(struct nfs_client *clp, |
| 1115 | const struct nfs_fh *fhandle) |
| 1116 | { |
| 1117 | struct nfs_server *server; |
Trond Myklebust | 6c34265 | 2018-06-07 14:22:00 -0400 | [diff] [blame] | 1118 | struct inode *res; |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 1119 | |
| 1120 | rcu_read_lock(); |
| 1121 | list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) { |
| 1122 | res = nfs_delegation_find_inode_server(server, fhandle); |
Anna Schumaker | d5681f5 | 2018-06-14 09:39:17 -0400 | [diff] [blame] | 1123 | if (res != ERR_PTR(-ENOENT)) { |
| 1124 | rcu_read_unlock(); |
Trond Myklebust | 6c34265 | 2018-06-07 14:22:00 -0400 | [diff] [blame] | 1125 | return res; |
Anna Schumaker | d5681f5 | 2018-06-14 09:39:17 -0400 | [diff] [blame] | 1126 | } |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 1127 | } |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 1128 | rcu_read_unlock(); |
Trond Myklebust | 6c34265 | 2018-06-07 14:22:00 -0400 | [diff] [blame] | 1129 | return ERR_PTR(-ENOENT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | } |
| 1131 | |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 1132 | static void nfs_delegation_mark_reclaim_server(struct nfs_server *server) |
| 1133 | { |
| 1134 | struct nfs_delegation *delegation; |
| 1135 | |
Trond Myklebust | 45870d6 | 2016-09-22 13:38:59 -0400 | [diff] [blame] | 1136 | list_for_each_entry_rcu(delegation, &server->delegations, super_list) { |
| 1137 | /* |
| 1138 | * If the delegation may have been admin revoked, then we |
| 1139 | * cannot reclaim it. |
| 1140 | */ |
| 1141 | if (test_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags)) |
| 1142 | continue; |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 1143 | set_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags); |
Trond Myklebust | 45870d6 | 2016-09-22 13:38:59 -0400 | [diff] [blame] | 1144 | } |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 1145 | } |
| 1146 | |
| 1147 | /** |
| 1148 | * nfs_delegation_mark_reclaim - mark all delegations as needing to be reclaimed |
| 1149 | * @clp: nfs_client to process |
| 1150 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1151 | */ |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 1152 | void nfs_delegation_mark_reclaim(struct nfs_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | { |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 1154 | struct nfs_server *server; |
| 1155 | |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 1156 | rcu_read_lock(); |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 1157 | list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) |
| 1158 | nfs_delegation_mark_reclaim_server(server); |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 1159 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1160 | } |
| 1161 | |
Trond Myklebust | 1bba38b | 2020-02-27 09:08:25 -0500 | [diff] [blame] | 1162 | static int nfs_server_reap_unclaimed_delegations(struct nfs_server *server, |
| 1163 | void __always_unused *data) |
| 1164 | { |
| 1165 | struct nfs_delegation *delegation; |
| 1166 | struct inode *inode; |
| 1167 | restart: |
| 1168 | rcu_read_lock(); |
| 1169 | restart_locked: |
| 1170 | list_for_each_entry_rcu(delegation, &server->delegations, super_list) { |
| 1171 | if (test_bit(NFS_DELEGATION_INODE_FREEING, |
| 1172 | &delegation->flags) || |
| 1173 | test_bit(NFS_DELEGATION_RETURNING, |
| 1174 | &delegation->flags) || |
| 1175 | test_bit(NFS_DELEGATION_NEED_RECLAIM, |
| 1176 | &delegation->flags) == 0) |
| 1177 | continue; |
| 1178 | inode = nfs_delegation_grab_inode(delegation); |
| 1179 | if (inode == NULL) |
| 1180 | goto restart_locked; |
| 1181 | delegation = nfs_start_delegation_return_locked(NFS_I(inode)); |
| 1182 | rcu_read_unlock(); |
| 1183 | if (delegation != NULL) { |
| 1184 | if (nfs_detach_delegation(NFS_I(inode), delegation, |
| 1185 | server) != NULL) |
| 1186 | nfs_free_delegation(delegation); |
| 1187 | /* Match nfs_start_delegation_return_locked */ |
| 1188 | nfs_put_delegation(delegation); |
| 1189 | } |
| 1190 | iput(inode); |
| 1191 | cond_resched(); |
| 1192 | goto restart; |
| 1193 | } |
| 1194 | rcu_read_unlock(); |
| 1195 | return 0; |
| 1196 | } |
| 1197 | |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 1198 | /** |
| 1199 | * nfs_delegation_reap_unclaimed - reap unclaimed delegations after reboot recovery is done |
| 1200 | * @clp: nfs_client to process |
| 1201 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | */ |
David Howells | adfa6f9 | 2006-08-22 20:06:08 -0400 | [diff] [blame] | 1203 | void nfs_delegation_reap_unclaimed(struct nfs_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1204 | { |
Trond Myklebust | 1bba38b | 2020-02-27 09:08:25 -0500 | [diff] [blame] | 1205 | nfs_client_for_each_server(clp, nfs_server_reap_unclaimed_delegations, |
| 1206 | NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | } |
Trond Myklebust | 3e4f629 | 2006-03-20 13:44:46 -0500 | [diff] [blame] | 1208 | |
Trond Myklebust | bb3d1a3 | 2016-09-22 13:39:00 -0400 | [diff] [blame] | 1209 | static inline bool nfs4_server_rebooted(const struct nfs_client *clp) |
| 1210 | { |
| 1211 | return (clp->cl_state & (BIT(NFS4CLNT_CHECK_LEASE) | |
| 1212 | BIT(NFS4CLNT_LEASE_EXPIRED) | |
| 1213 | BIT(NFS4CLNT_SESSION_RESET))) != 0; |
| 1214 | } |
| 1215 | |
Trond Myklebust | 45870d6 | 2016-09-22 13:38:59 -0400 | [diff] [blame] | 1216 | static void nfs_mark_test_expired_delegation(struct nfs_server *server, |
| 1217 | struct nfs_delegation *delegation) |
| 1218 | { |
Trond Myklebust | 059b43e | 2016-09-22 13:39:06 -0400 | [diff] [blame] | 1219 | if (delegation->stateid.type == NFS4_INVALID_STATEID_TYPE) |
| 1220 | return; |
Trond Myklebust | 45870d6 | 2016-09-22 13:38:59 -0400 | [diff] [blame] | 1221 | clear_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags); |
| 1222 | set_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags); |
| 1223 | set_bit(NFS4CLNT_DELEGATION_EXPIRED, &server->nfs_client->cl_state); |
| 1224 | } |
| 1225 | |
Trond Myklebust | bb3d1a3 | 2016-09-22 13:39:00 -0400 | [diff] [blame] | 1226 | static void nfs_inode_mark_test_expired_delegation(struct nfs_server *server, |
| 1227 | struct inode *inode) |
| 1228 | { |
| 1229 | struct nfs_delegation *delegation; |
| 1230 | |
| 1231 | rcu_read_lock(); |
| 1232 | delegation = rcu_dereference(NFS_I(inode)->delegation); |
| 1233 | if (delegation) |
| 1234 | nfs_mark_test_expired_delegation(server, delegation); |
| 1235 | rcu_read_unlock(); |
| 1236 | |
| 1237 | } |
| 1238 | |
Trond Myklebust | 45870d6 | 2016-09-22 13:38:59 -0400 | [diff] [blame] | 1239 | static void nfs_delegation_mark_test_expired_server(struct nfs_server *server) |
| 1240 | { |
| 1241 | struct nfs_delegation *delegation; |
| 1242 | |
| 1243 | list_for_each_entry_rcu(delegation, &server->delegations, super_list) |
| 1244 | nfs_mark_test_expired_delegation(server, delegation); |
| 1245 | } |
| 1246 | |
| 1247 | /** |
| 1248 | * nfs_mark_test_expired_all_delegations - mark all delegations for testing |
| 1249 | * @clp: nfs_client to process |
| 1250 | * |
| 1251 | * Iterates through all the delegations associated with this server and |
| 1252 | * marks them as needing to be checked for validity. |
| 1253 | */ |
| 1254 | void nfs_mark_test_expired_all_delegations(struct nfs_client *clp) |
| 1255 | { |
| 1256 | struct nfs_server *server; |
| 1257 | |
| 1258 | rcu_read_lock(); |
| 1259 | list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) |
| 1260 | nfs_delegation_mark_test_expired_server(server); |
| 1261 | rcu_read_unlock(); |
| 1262 | } |
| 1263 | |
| 1264 | /** |
Scott Mayhew | 8ca017c | 2019-05-06 11:59:05 -0400 | [diff] [blame] | 1265 | * nfs_test_expired_all_delegations - test all delegations for a client |
| 1266 | * @clp: nfs_client to process |
| 1267 | * |
| 1268 | * Helper for handling "recallable state revoked" status from server. |
| 1269 | */ |
| 1270 | void nfs_test_expired_all_delegations(struct nfs_client *clp) |
| 1271 | { |
| 1272 | nfs_mark_test_expired_all_delegations(clp); |
| 1273 | nfs4_schedule_state_manager(clp); |
| 1274 | } |
| 1275 | |
Trond Myklebust | ad11408 | 2019-07-26 14:40:53 +0100 | [diff] [blame] | 1276 | static void |
| 1277 | nfs_delegation_test_free_expired(struct inode *inode, |
| 1278 | nfs4_stateid *stateid, |
| 1279 | const struct cred *cred) |
| 1280 | { |
| 1281 | struct nfs_server *server = NFS_SERVER(inode); |
| 1282 | const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops; |
| 1283 | int status; |
| 1284 | |
| 1285 | if (!cred) |
| 1286 | return; |
| 1287 | status = ops->test_and_free_expired(server, stateid, cred); |
| 1288 | if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) |
| 1289 | nfs_remove_bad_delegation(inode, stateid); |
| 1290 | } |
| 1291 | |
Trond Myklebust | 7f156ef | 2020-02-27 09:15:19 -0500 | [diff] [blame] | 1292 | static int nfs_server_reap_expired_delegations(struct nfs_server *server, |
| 1293 | void __always_unused *data) |
| 1294 | { |
| 1295 | struct nfs_delegation *delegation; |
| 1296 | struct inode *inode; |
| 1297 | const struct cred *cred; |
| 1298 | nfs4_stateid stateid; |
| 1299 | restart: |
| 1300 | rcu_read_lock(); |
| 1301 | restart_locked: |
| 1302 | list_for_each_entry_rcu(delegation, &server->delegations, super_list) { |
| 1303 | if (test_bit(NFS_DELEGATION_INODE_FREEING, |
| 1304 | &delegation->flags) || |
| 1305 | test_bit(NFS_DELEGATION_RETURNING, |
| 1306 | &delegation->flags) || |
| 1307 | test_bit(NFS_DELEGATION_TEST_EXPIRED, |
| 1308 | &delegation->flags) == 0) |
| 1309 | continue; |
| 1310 | inode = nfs_delegation_grab_inode(delegation); |
| 1311 | if (inode == NULL) |
| 1312 | goto restart_locked; |
Trond Myklebust | fc51b1c | 2020-04-02 15:27:09 -0400 | [diff] [blame] | 1313 | spin_lock(&delegation->lock); |
Trond Myklebust | 7f156ef | 2020-02-27 09:15:19 -0500 | [diff] [blame] | 1314 | cred = get_cred_rcu(delegation->cred); |
| 1315 | nfs4_stateid_copy(&stateid, &delegation->stateid); |
Trond Myklebust | fc51b1c | 2020-04-02 15:27:09 -0400 | [diff] [blame] | 1316 | spin_unlock(&delegation->lock); |
Trond Myklebust | 7f156ef | 2020-02-27 09:15:19 -0500 | [diff] [blame] | 1317 | clear_bit(NFS_DELEGATION_TEST_EXPIRED, &delegation->flags); |
| 1318 | rcu_read_unlock(); |
| 1319 | nfs_delegation_test_free_expired(inode, &stateid, cred); |
| 1320 | put_cred(cred); |
| 1321 | if (!nfs4_server_rebooted(server->nfs_client)) { |
| 1322 | iput(inode); |
| 1323 | cond_resched(); |
| 1324 | goto restart; |
| 1325 | } |
| 1326 | nfs_inode_mark_test_expired_delegation(server,inode); |
| 1327 | iput(inode); |
| 1328 | return -EAGAIN; |
| 1329 | } |
| 1330 | rcu_read_unlock(); |
| 1331 | return 0; |
| 1332 | } |
| 1333 | |
Scott Mayhew | 8ca017c | 2019-05-06 11:59:05 -0400 | [diff] [blame] | 1334 | /** |
Trond Myklebust | 45870d6 | 2016-09-22 13:38:59 -0400 | [diff] [blame] | 1335 | * nfs_reap_expired_delegations - reap expired delegations |
| 1336 | * @clp: nfs_client to process |
| 1337 | * |
| 1338 | * Iterates through all the delegations associated with this server and |
| 1339 | * checks if they have may have been revoked. This function is usually |
| 1340 | * expected to be called in cases where the server may have lost its |
| 1341 | * lease. |
| 1342 | */ |
| 1343 | void nfs_reap_expired_delegations(struct nfs_client *clp) |
| 1344 | { |
Trond Myklebust | 7f156ef | 2020-02-27 09:15:19 -0500 | [diff] [blame] | 1345 | nfs_client_for_each_server(clp, nfs_server_reap_expired_delegations, |
| 1346 | NULL); |
Trond Myklebust | 45870d6 | 2016-09-22 13:38:59 -0400 | [diff] [blame] | 1347 | } |
| 1348 | |
Trond Myklebust | 6c2d8f8 | 2016-09-22 13:39:07 -0400 | [diff] [blame] | 1349 | void nfs_inode_find_delegation_state_and_recover(struct inode *inode, |
| 1350 | const nfs4_stateid *stateid) |
| 1351 | { |
| 1352 | struct nfs_client *clp = NFS_SERVER(inode)->nfs_client; |
| 1353 | struct nfs_delegation *delegation; |
| 1354 | bool found = false; |
| 1355 | |
| 1356 | rcu_read_lock(); |
| 1357 | delegation = rcu_dereference(NFS_I(inode)->delegation); |
| 1358 | if (delegation && |
Trond Myklebust | 42c304c | 2019-10-26 10:16:15 -0400 | [diff] [blame] | 1359 | nfs4_stateid_match_or_older(&delegation->stateid, stateid) && |
| 1360 | !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) { |
Trond Myklebust | 6c2d8f8 | 2016-09-22 13:39:07 -0400 | [diff] [blame] | 1361 | nfs_mark_test_expired_delegation(NFS_SERVER(inode), delegation); |
| 1362 | found = true; |
| 1363 | } |
| 1364 | rcu_read_unlock(); |
| 1365 | if (found) |
| 1366 | nfs4_schedule_state_manager(clp); |
| 1367 | } |
| 1368 | |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 1369 | /** |
| 1370 | * nfs_delegations_present - check for existence of delegations |
| 1371 | * @clp: client state handle |
| 1372 | * |
| 1373 | * Returns one if there are any nfs_delegation structures attached |
| 1374 | * to this nfs_client. |
| 1375 | */ |
| 1376 | int nfs_delegations_present(struct nfs_client *clp) |
| 1377 | { |
| 1378 | struct nfs_server *server; |
| 1379 | int ret = 0; |
| 1380 | |
| 1381 | rcu_read_lock(); |
| 1382 | list_for_each_entry_rcu(server, &clp->cl_superblocks, client_link) |
| 1383 | if (!list_empty(&server->delegations)) { |
| 1384 | ret = 1; |
| 1385 | break; |
| 1386 | } |
| 1387 | rcu_read_unlock(); |
| 1388 | return ret; |
| 1389 | } |
| 1390 | |
| 1391 | /** |
Trond Myklebust | 12f275c | 2017-11-06 15:28:05 -0500 | [diff] [blame] | 1392 | * nfs4_refresh_delegation_stateid - Update delegation stateid seqid |
| 1393 | * @dst: stateid to refresh |
| 1394 | * @inode: inode to check |
| 1395 | * |
| 1396 | * Returns "true" and updates "dst->seqid" * if inode had a delegation |
| 1397 | * that matches our delegation stateid. Otherwise "false" is returned. |
| 1398 | */ |
| 1399 | bool nfs4_refresh_delegation_stateid(nfs4_stateid *dst, struct inode *inode) |
| 1400 | { |
| 1401 | struct nfs_delegation *delegation; |
| 1402 | bool ret = false; |
| 1403 | if (!inode) |
| 1404 | goto out; |
| 1405 | |
| 1406 | rcu_read_lock(); |
| 1407 | delegation = rcu_dereference(NFS_I(inode)->delegation); |
| 1408 | if (delegation != NULL && |
Trond Myklebust | b575620 | 2019-10-22 13:34:06 -0400 | [diff] [blame] | 1409 | nfs4_stateid_match_other(dst, &delegation->stateid) && |
Trond Myklebust | 246afc0a | 2019-10-24 18:00:35 -0400 | [diff] [blame] | 1410 | nfs4_stateid_is_newer(&delegation->stateid, dst) && |
Trond Myklebust | b575620 | 2019-10-22 13:34:06 -0400 | [diff] [blame] | 1411 | !test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) { |
Trond Myklebust | 12f275c | 2017-11-06 15:28:05 -0500 | [diff] [blame] | 1412 | dst->seqid = delegation->stateid.seqid; |
Trond Myklebust | 79cc554 | 2019-10-31 18:40:33 -0400 | [diff] [blame] | 1413 | ret = true; |
Trond Myklebust | 12f275c | 2017-11-06 15:28:05 -0500 | [diff] [blame] | 1414 | } |
| 1415 | rcu_read_unlock(); |
| 1416 | out: |
| 1417 | return ret; |
| 1418 | } |
| 1419 | |
| 1420 | /** |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 1421 | * nfs4_copy_delegation_stateid - Copy inode's state ID information |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 1422 | * @inode: inode to check |
Trond Myklebust | 0032a7a | 2012-03-08 17:16:12 -0500 | [diff] [blame] | 1423 | * @flags: delegation type requirement |
Trond Myklebust | abf4e13 | 2016-05-16 17:42:44 -0400 | [diff] [blame] | 1424 | * @dst: stateid data structure to fill in |
| 1425 | * @cred: optional argument to retrieve credential |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 1426 | * |
Trond Myklebust | 0032a7a | 2012-03-08 17:16:12 -0500 | [diff] [blame] | 1427 | * Returns "true" and fills in "dst->data" * if inode had a delegation, |
| 1428 | * otherwise "false" is returned. |
Chuck Lever | d3978bb | 2010-12-24 01:33:04 +0000 | [diff] [blame] | 1429 | */ |
Trond Myklebust | abf4e13 | 2016-05-16 17:42:44 -0400 | [diff] [blame] | 1430 | bool nfs4_copy_delegation_stateid(struct inode *inode, fmode_t flags, |
NeilBrown | a52458b | 2018-12-03 11:30:31 +1100 | [diff] [blame] | 1431 | nfs4_stateid *dst, const struct cred **cred) |
Trond Myklebust | 3e4f629 | 2006-03-20 13:44:46 -0500 | [diff] [blame] | 1432 | { |
Trond Myklebust | 3e4f629 | 2006-03-20 13:44:46 -0500 | [diff] [blame] | 1433 | struct nfs_inode *nfsi = NFS_I(inode); |
| 1434 | struct nfs_delegation *delegation; |
Trond Myklebust | fc51b1c | 2020-04-02 15:27:09 -0400 | [diff] [blame] | 1435 | bool ret = false; |
Trond Myklebust | 3e4f629 | 2006-03-20 13:44:46 -0500 | [diff] [blame] | 1436 | |
Trond Myklebust | 0032a7a | 2012-03-08 17:16:12 -0500 | [diff] [blame] | 1437 | flags &= FMODE_READ|FMODE_WRITE; |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 1438 | rcu_read_lock(); |
| 1439 | delegation = rcu_dereference(nfsi->delegation); |
Trond Myklebust | fc51b1c | 2020-04-02 15:27:09 -0400 | [diff] [blame] | 1440 | if (!delegation) |
| 1441 | goto out; |
| 1442 | spin_lock(&delegation->lock); |
Trond Myklebust | aa05c87 | 2016-09-22 13:38:54 -0400 | [diff] [blame] | 1443 | ret = nfs4_is_valid_delegation(delegation, flags); |
Trond Myklebust | 0032a7a | 2012-03-08 17:16:12 -0500 | [diff] [blame] | 1444 | if (ret) { |
Trond Myklebust | f597c53 | 2012-03-04 18:13:56 -0500 | [diff] [blame] | 1445 | nfs4_stateid_copy(dst, &delegation->stateid); |
Trond Myklebust | 0032a7a | 2012-03-08 17:16:12 -0500 | [diff] [blame] | 1446 | nfs_mark_delegation_referenced(delegation); |
Trond Myklebust | abf4e13 | 2016-05-16 17:42:44 -0400 | [diff] [blame] | 1447 | if (cred) |
NeilBrown | a52458b | 2018-12-03 11:30:31 +1100 | [diff] [blame] | 1448 | *cred = get_cred(delegation->cred); |
Trond Myklebust | 3e4f629 | 2006-03-20 13:44:46 -0500 | [diff] [blame] | 1449 | } |
Trond Myklebust | fc51b1c | 2020-04-02 15:27:09 -0400 | [diff] [blame] | 1450 | spin_unlock(&delegation->lock); |
| 1451 | out: |
Trond Myklebust | 8383e46 | 2007-07-06 15:12:04 -0400 | [diff] [blame] | 1452 | rcu_read_unlock(); |
| 1453 | return ret; |
Trond Myklebust | 3e4f629 | 2006-03-20 13:44:46 -0500 | [diff] [blame] | 1454 | } |
Trond Myklebust | 5445b1f | 2015-09-05 19:06:58 -0400 | [diff] [blame] | 1455 | |
| 1456 | /** |
| 1457 | * nfs4_delegation_flush_on_close - Check if we must flush file on close |
| 1458 | * @inode: inode to check |
| 1459 | * |
| 1460 | * This function checks the number of outstanding writes to the file |
| 1461 | * against the delegation 'space_limit' field to see if |
| 1462 | * the spec requires us to flush the file on close. |
| 1463 | */ |
| 1464 | bool nfs4_delegation_flush_on_close(const struct inode *inode) |
| 1465 | { |
| 1466 | struct nfs_inode *nfsi = NFS_I(inode); |
| 1467 | struct nfs_delegation *delegation; |
| 1468 | bool ret = true; |
| 1469 | |
| 1470 | rcu_read_lock(); |
| 1471 | delegation = rcu_dereference(nfsi->delegation); |
| 1472 | if (delegation == NULL || !(delegation->type & FMODE_WRITE)) |
| 1473 | goto out; |
Trond Myklebust | a6b6d5b | 2017-08-01 15:39:46 -0400 | [diff] [blame] | 1474 | if (atomic_long_read(&nfsi->nrequests) < delegation->pagemod_limit) |
Trond Myklebust | 5445b1f | 2015-09-05 19:06:58 -0400 | [diff] [blame] | 1475 | ret = false; |
| 1476 | out: |
| 1477 | rcu_read_unlock(); |
| 1478 | return ret; |
| 1479 | } |
Trond Myklebust | 10717f4 | 2020-01-27 09:58:19 -0500 | [diff] [blame] | 1480 | |
| 1481 | module_param_named(delegation_watermark, nfs_delegation_watermark, uint, 0644); |