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