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