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