Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Copyright (c) 2001 The Regents of the University of Michigan. |
| 3 | * All rights reserved. |
| 4 | * |
| 5 | * Kendrick Smith <kmsmith@umich.edu> |
| 6 | * Andy Adamson <kandros@umich.edu> |
| 7 | * |
| 8 | * Redistribution and use in source and binary forms, with or without |
| 9 | * modification, are permitted provided that the following conditions |
| 10 | * are met: |
| 11 | * |
| 12 | * 1. Redistributions of source code must retain the above copyright |
| 13 | * notice, this list of conditions and the following disclaimer. |
| 14 | * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in the |
| 16 | * documentation and/or other materials provided with the distribution. |
| 17 | * 3. Neither the name of the University nor the names of its |
| 18 | * contributors may be used to endorse or promote products derived |
| 19 | * from this software without specific prior written permission. |
| 20 | * |
| 21 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED |
| 22 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 23 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 24 | * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
| 25 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | * |
| 33 | */ |
| 34 | |
Dave Hansen | aceaf78 | 2008-02-15 14:37:31 -0800 | [diff] [blame] | 35 | #include <linux/file.h> |
Arnd Bergmann | b89f432 | 2010-09-18 15:09:31 +0200 | [diff] [blame] | 36 | #include <linux/fs.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 37 | #include <linux/slab.h> |
NeilBrown | 0964a3d | 2005-06-23 22:04:32 -0700 | [diff] [blame] | 38 | #include <linux/namei.h> |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 39 | #include <linux/swap.h> |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 40 | #include <linux/pagemap.h> |
Chuck Lever | 7df302f | 2012-05-29 13:56:37 -0400 | [diff] [blame] | 41 | #include <linux/ratelimit.h> |
Olga Kornievskaia | 68e76ad | 2008-12-23 16:17:15 -0500 | [diff] [blame] | 42 | #include <linux/sunrpc/svcauth_gss.h> |
Jeff Layton | 5976687 | 2013-02-04 12:50:00 -0500 | [diff] [blame] | 43 | #include <linux/sunrpc/addr.h> |
Daniel Borkmann | 8754589 | 2014-12-10 16:33:11 +0100 | [diff] [blame] | 44 | #include <linux/jhash.h> |
J. Bruce Fields | 169319f | 2019-06-19 12:39:46 -0400 | [diff] [blame] | 45 | #include <linux/string_helpers.h> |
NeilBrown | 472d155 | 2021-03-20 09:38:04 +1100 | [diff] [blame] | 46 | #include <linux/fsnotify.h> |
Dai Ngo | f4e44b3 | 2021-05-21 15:09:37 -0400 | [diff] [blame] | 47 | #include <linux/nfs_ssc.h> |
Boaz Harrosh | 9a74af2 | 2009-12-03 20:30:56 +0200 | [diff] [blame] | 48 | #include "xdr4.h" |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 49 | #include "xdr4cb.h" |
J. Bruce Fields | 0a3adad | 2009-11-04 18:12:35 -0500 | [diff] [blame] | 50 | #include "vfs.h" |
J. Bruce Fields | bfa4b36 | 2012-04-25 16:49:18 -0400 | [diff] [blame] | 51 | #include "current_stateid.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 53 | #include "netns.h" |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 54 | #include "pnfs.h" |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 55 | #include "filecache.h" |
Chuck Lever | dd5e3fb | 2020-04-05 11:42:29 -0400 | [diff] [blame] | 56 | #include "trace.h" |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 57 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | #define NFSDDBG_FACILITY NFSDDBG_PROC |
| 59 | |
J. Bruce Fields | f32f3c2 | 2011-12-12 15:00:35 -0500 | [diff] [blame] | 60 | #define all_ones {{~0,~0},~0} |
| 61 | static const stateid_t one_stateid = { |
| 62 | .si_generation = ~0, |
| 63 | .si_opaque = all_ones, |
| 64 | }; |
| 65 | static const stateid_t zero_stateid = { |
| 66 | /* all fields zero */ |
| 67 | }; |
Tigran Mkrtchyan | 19ff0f2 | 2012-02-13 22:55:23 +0100 | [diff] [blame] | 68 | static const stateid_t currentstateid = { |
| 69 | .si_generation = 1, |
| 70 | }; |
Trond Myklebust | fb500a7 | 2017-11-03 08:00:12 -0400 | [diff] [blame] | 71 | static const stateid_t close_stateid = { |
| 72 | .si_generation = 0xffffffffU, |
| 73 | }; |
J. Bruce Fields | f32f3c2 | 2011-12-12 15:00:35 -0500 | [diff] [blame] | 74 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 75 | static u64 current_sessionid = 1; |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 76 | |
J. Bruce Fields | f32f3c2 | 2011-12-12 15:00:35 -0500 | [diff] [blame] | 77 | #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t))) |
| 78 | #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t))) |
Tigran Mkrtchyan | 19ff0f2 | 2012-02-13 22:55:23 +0100 | [diff] [blame] | 79 | #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t))) |
Andrew Elble | ae254dac | 2017-11-09 13:41:10 -0500 | [diff] [blame] | 80 | #define CLOSE_STATEID(stateid) (!memcmp((stateid), &close_stateid, sizeof(stateid_t))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | /* forward declarations */ |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 83 | static bool check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 84 | static void nfs4_free_ol_stateid(struct nfs4_stid *stid); |
Scott Mayhew | 362063a | 2019-03-26 18:06:28 -0400 | [diff] [blame] | 85 | void nfsd4_end_grace(struct nfsd_net *nn); |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 86 | static void _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 88 | /* Locking: */ |
| 89 | |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 90 | /* |
| 91 | * Currently used for the del_recall_lru and file hash table. In an |
| 92 | * effort to decrease the scope of the client_mutex, this spinlock may |
| 93 | * eventually cover more: |
| 94 | */ |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 95 | static DEFINE_SPINLOCK(state_lock); |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 96 | |
Andrew Elble | 4f34bd0 | 2017-11-08 17:29:51 -0500 | [diff] [blame] | 97 | enum nfsd4_st_mutex_lock_subclass { |
| 98 | OPEN_STATEID_MUTEX = 0, |
| 99 | LOCK_STATEID_MUTEX = 1, |
| 100 | }; |
| 101 | |
Jeff Layton | b401be22 | 2014-07-29 21:34:33 -0400 | [diff] [blame] | 102 | /* |
| 103 | * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for |
| 104 | * the refcount on the open stateid to drop. |
| 105 | */ |
| 106 | static DECLARE_WAIT_QUEUE_HEAD(close_wq); |
| 107 | |
J. Bruce Fields | 89c905b | 2019-06-19 12:43:11 -0400 | [diff] [blame] | 108 | /* |
| 109 | * A waitqueue where a writer to clients/#/ctl destroying a client can |
| 110 | * wait for cl_rpc_users to drop to 0 and then for the client to be |
| 111 | * unhashed. |
| 112 | */ |
| 113 | static DECLARE_WAIT_QUEUE_HEAD(expiry_wq); |
| 114 | |
Jeff Layton | 9258a2d | 2018-03-16 09:47:22 -0400 | [diff] [blame] | 115 | static struct kmem_cache *client_slab; |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 116 | static struct kmem_cache *openowner_slab; |
| 117 | static struct kmem_cache *lockowner_slab; |
| 118 | static struct kmem_cache *file_slab; |
| 119 | static struct kmem_cache *stateid_slab; |
| 120 | static struct kmem_cache *deleg_slab; |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 121 | static struct kmem_cache *odstate_slab; |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 122 | |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 123 | static void free_session(struct nfsd4_session *); |
Benny Halevy | 508dc6e | 2012-02-23 17:40:52 -0800 | [diff] [blame] | 124 | |
Julia Lawall | c4cb897 | 2015-11-21 22:57:39 +0100 | [diff] [blame] | 125 | static const struct nfsd4_callback_ops nfsd4_cb_recall_ops; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 126 | static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops; |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 127 | |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 128 | static bool is_session_dead(struct nfsd4_session *ses) |
Benny Halevy | 508dc6e | 2012-02-23 17:40:52 -0800 | [diff] [blame] | 129 | { |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 130 | return ses->se_flags & NFS4_SESSION_DEAD; |
| 131 | } |
| 132 | |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 133 | static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me) |
| 134 | { |
| 135 | if (atomic_read(&ses->se_ref) > ref_held_by_me) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 136 | return nfserr_jukebox; |
| 137 | ses->se_flags |= NFS4_SESSION_DEAD; |
| 138 | return nfs_ok; |
| 139 | } |
| 140 | |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 141 | static bool is_client_expired(struct nfs4_client *clp) |
| 142 | { |
| 143 | return clp->cl_time == 0; |
| 144 | } |
| 145 | |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 146 | static __be32 get_client_locked(struct nfs4_client *clp) |
| 147 | { |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 148 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 149 | |
| 150 | lockdep_assert_held(&nn->client_lock); |
| 151 | |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 152 | if (is_client_expired(clp)) |
| 153 | return nfserr_expired; |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 154 | atomic_inc(&clp->cl_rpc_users); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 155 | return nfs_ok; |
| 156 | } |
| 157 | |
| 158 | /* must be called under the client_lock */ |
| 159 | static inline void |
| 160 | renew_client_locked(struct nfs4_client *clp) |
| 161 | { |
| 162 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 163 | |
| 164 | if (is_client_expired(clp)) { |
| 165 | WARN_ON(1); |
| 166 | printk("%s: client (clientid %08x/%08x) already expired\n", |
| 167 | __func__, |
| 168 | clp->cl_clientid.cl_boot, |
| 169 | clp->cl_clientid.cl_id); |
| 170 | return; |
| 171 | } |
| 172 | |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 173 | list_move_tail(&clp->cl_lru, &nn->client_lru); |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 174 | clp->cl_time = ktime_get_boottime_seconds(); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 175 | } |
| 176 | |
Fengguang Wu | ba13843 | 2013-04-16 22:14:15 -0400 | [diff] [blame] | 177 | static void put_client_renew_locked(struct nfs4_client *clp) |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 178 | { |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 179 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 180 | |
| 181 | lockdep_assert_held(&nn->client_lock); |
| 182 | |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 183 | if (!atomic_dec_and_test(&clp->cl_rpc_users)) |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 184 | return; |
| 185 | if (!is_client_expired(clp)) |
| 186 | renew_client_locked(clp); |
J. Bruce Fields | 89c905b | 2019-06-19 12:43:11 -0400 | [diff] [blame] | 187 | else |
| 188 | wake_up_all(&expiry_wq); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 189 | } |
| 190 | |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 191 | static void put_client_renew(struct nfs4_client *clp) |
| 192 | { |
| 193 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 194 | |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 195 | if (!atomic_dec_and_lock(&clp->cl_rpc_users, &nn->client_lock)) |
Jeff Layton | d6c249b | 2014-07-08 14:02:50 -0400 | [diff] [blame] | 196 | return; |
| 197 | if (!is_client_expired(clp)) |
| 198 | renew_client_locked(clp); |
J. Bruce Fields | 89c905b | 2019-06-19 12:43:11 -0400 | [diff] [blame] | 199 | else |
| 200 | wake_up_all(&expiry_wq); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 201 | spin_unlock(&nn->client_lock); |
| 202 | } |
| 203 | |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 204 | static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses) |
| 205 | { |
| 206 | __be32 status; |
| 207 | |
| 208 | if (is_session_dead(ses)) |
| 209 | return nfserr_badsession; |
| 210 | status = get_client_locked(ses->se_client); |
| 211 | if (status) |
| 212 | return status; |
| 213 | atomic_inc(&ses->se_ref); |
| 214 | return nfs_ok; |
| 215 | } |
| 216 | |
| 217 | static void nfsd4_put_session_locked(struct nfsd4_session *ses) |
| 218 | { |
| 219 | struct nfs4_client *clp = ses->se_client; |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 220 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 221 | |
| 222 | lockdep_assert_held(&nn->client_lock); |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 223 | |
| 224 | if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses)) |
| 225 | free_session(ses); |
| 226 | put_client_renew_locked(clp); |
| 227 | } |
| 228 | |
| 229 | static void nfsd4_put_session(struct nfsd4_session *ses) |
| 230 | { |
| 231 | struct nfs4_client *clp = ses->se_client; |
| 232 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 233 | |
| 234 | spin_lock(&nn->client_lock); |
| 235 | nfsd4_put_session_locked(ses); |
| 236 | spin_unlock(&nn->client_lock); |
| 237 | } |
| 238 | |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 239 | static struct nfsd4_blocked_lock * |
| 240 | find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh, |
| 241 | struct nfsd_net *nn) |
| 242 | { |
| 243 | struct nfsd4_blocked_lock *cur, *found = NULL; |
| 244 | |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 245 | spin_lock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 246 | list_for_each_entry(cur, &lo->lo_blocked, nbl_list) { |
| 247 | if (fh_match(fh, &cur->nbl_fh)) { |
| 248 | list_del_init(&cur->nbl_list); |
Vasily Averin | 47446d7 | 2021-12-17 09:49:39 +0300 | [diff] [blame] | 249 | WARN_ON(list_empty(&cur->nbl_lru)); |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 250 | list_del_init(&cur->nbl_lru); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 251 | found = cur; |
| 252 | break; |
| 253 | } |
| 254 | } |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 255 | spin_unlock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 256 | if (found) |
NeilBrown | cb03f94 | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 257 | locks_delete_block(&found->nbl_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 258 | return found; |
| 259 | } |
| 260 | |
| 261 | static struct nfsd4_blocked_lock * |
| 262 | find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh, |
| 263 | struct nfsd_net *nn) |
| 264 | { |
| 265 | struct nfsd4_blocked_lock *nbl; |
| 266 | |
| 267 | nbl = find_blocked_lock(lo, fh, nn); |
| 268 | if (!nbl) { |
| 269 | nbl= kmalloc(sizeof(*nbl), GFP_KERNEL); |
| 270 | if (nbl) { |
Vasily Averin | e1e8399 | 2020-03-27 07:50:40 +0300 | [diff] [blame] | 271 | INIT_LIST_HEAD(&nbl->nbl_list); |
| 272 | INIT_LIST_HEAD(&nbl->nbl_lru); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 273 | fh_copy_shallow(&nbl->nbl_fh, fh); |
| 274 | locks_init_lock(&nbl->nbl_lock); |
Vasily Averin | 47446d7 | 2021-12-17 09:49:39 +0300 | [diff] [blame] | 275 | kref_init(&nbl->nbl_kref); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 276 | nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client, |
| 277 | &nfsd4_cb_notify_lock_ops, |
| 278 | NFSPROC4_CLNT_CB_NOTIFY_LOCK); |
| 279 | } |
| 280 | } |
| 281 | return nbl; |
| 282 | } |
| 283 | |
| 284 | static void |
Vasily Averin | 47446d7 | 2021-12-17 09:49:39 +0300 | [diff] [blame] | 285 | free_nbl(struct kref *kref) |
| 286 | { |
| 287 | struct nfsd4_blocked_lock *nbl; |
| 288 | |
| 289 | nbl = container_of(kref, struct nfsd4_blocked_lock, nbl_kref); |
| 290 | kfree(nbl); |
| 291 | } |
| 292 | |
| 293 | static void |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 294 | free_blocked_lock(struct nfsd4_blocked_lock *nbl) |
| 295 | { |
Jeff Layton | 6aaafc4 | 2019-04-22 12:34:23 -0400 | [diff] [blame] | 296 | locks_delete_block(&nbl->nbl_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 297 | locks_release_private(&nbl->nbl_lock); |
Vasily Averin | 47446d7 | 2021-12-17 09:49:39 +0300 | [diff] [blame] | 298 | kref_put(&nbl->nbl_kref, free_nbl); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 299 | } |
| 300 | |
Jeff Layton | 68ef3bc | 2018-03-16 11:32:02 -0400 | [diff] [blame] | 301 | static void |
| 302 | remove_blocked_locks(struct nfs4_lockowner *lo) |
| 303 | { |
| 304 | struct nfs4_client *clp = lo->lo_owner.so_client; |
| 305 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 306 | struct nfsd4_blocked_lock *nbl; |
| 307 | LIST_HEAD(reaplist); |
| 308 | |
| 309 | /* Dequeue all blocked locks */ |
| 310 | spin_lock(&nn->blocked_locks_lock); |
| 311 | while (!list_empty(&lo->lo_blocked)) { |
| 312 | nbl = list_first_entry(&lo->lo_blocked, |
| 313 | struct nfsd4_blocked_lock, |
| 314 | nbl_list); |
| 315 | list_del_init(&nbl->nbl_list); |
Vasily Averin | 47446d7 | 2021-12-17 09:49:39 +0300 | [diff] [blame] | 316 | WARN_ON(list_empty(&nbl->nbl_lru)); |
Jeff Layton | 68ef3bc | 2018-03-16 11:32:02 -0400 | [diff] [blame] | 317 | list_move(&nbl->nbl_lru, &reaplist); |
| 318 | } |
| 319 | spin_unlock(&nn->blocked_locks_lock); |
| 320 | |
| 321 | /* Now free them */ |
| 322 | while (!list_empty(&reaplist)) { |
| 323 | nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock, |
| 324 | nbl_lru); |
| 325 | list_del_init(&nbl->nbl_lru); |
Jeff Layton | 68ef3bc | 2018-03-16 11:32:02 -0400 | [diff] [blame] | 326 | free_blocked_lock(nbl); |
| 327 | } |
| 328 | } |
| 329 | |
Jeff Layton | f456458 | 2019-04-22 12:34:24 -0400 | [diff] [blame] | 330 | static void |
| 331 | nfsd4_cb_notify_lock_prepare(struct nfsd4_callback *cb) |
| 332 | { |
| 333 | struct nfsd4_blocked_lock *nbl = container_of(cb, |
| 334 | struct nfsd4_blocked_lock, nbl_cb); |
| 335 | locks_delete_block(&nbl->nbl_lock); |
| 336 | } |
| 337 | |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 338 | static int |
| 339 | nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task) |
| 340 | { |
| 341 | /* |
| 342 | * Since this is just an optimization, we don't try very hard if it |
| 343 | * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and |
| 344 | * just quit trying on anything else. |
| 345 | */ |
| 346 | switch (task->tk_status) { |
| 347 | case -NFS4ERR_DELAY: |
| 348 | rpc_delay(task, 1 * HZ); |
| 349 | return 0; |
| 350 | default: |
| 351 | return 1; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | static void |
| 356 | nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb) |
| 357 | { |
| 358 | struct nfsd4_blocked_lock *nbl = container_of(cb, |
| 359 | struct nfsd4_blocked_lock, nbl_cb); |
| 360 | |
| 361 | free_blocked_lock(nbl); |
| 362 | } |
| 363 | |
| 364 | static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = { |
Jeff Layton | f456458 | 2019-04-22 12:34:24 -0400 | [diff] [blame] | 365 | .prepare = nfsd4_cb_notify_lock_prepare, |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 366 | .done = nfsd4_cb_notify_lock_done, |
| 367 | .release = nfsd4_cb_notify_lock_release, |
| 368 | }; |
| 369 | |
J. Bruce Fields | ebd9d2c | 2021-04-16 14:00:17 -0400 | [diff] [blame] | 370 | /* |
| 371 | * We store the NONE, READ, WRITE, and BOTH bits separately in the |
| 372 | * st_{access,deny}_bmap field of the stateid, in order to track not |
| 373 | * only what share bits are currently in force, but also what |
| 374 | * combinations of share bits previous opens have used. This allows us |
J. Bruce Fields | 3dcd1d8 | 2021-12-07 17:32:21 -0500 | [diff] [blame] | 375 | * to enforce the recommendation in |
| 376 | * https://datatracker.ietf.org/doc/html/rfc7530#section-16.19.4 that |
| 377 | * the server return an error if the client attempt to downgrade to a |
| 378 | * combination of share bits not explicable by closing some of its |
| 379 | * previous opens. |
J. Bruce Fields | ebd9d2c | 2021-04-16 14:00:17 -0400 | [diff] [blame] | 380 | * |
J. Bruce Fields | 3dcd1d8 | 2021-12-07 17:32:21 -0500 | [diff] [blame] | 381 | * This enforcement is arguably incomplete, since we don't keep |
J. Bruce Fields | ebd9d2c | 2021-04-16 14:00:17 -0400 | [diff] [blame] | 382 | * track of access/deny bit combinations; so, e.g., we allow: |
| 383 | * |
| 384 | * OPEN allow read, deny write |
| 385 | * OPEN allow both, deny none |
| 386 | * DOWNGRADE allow read, deny none |
| 387 | * |
| 388 | * which we should reject. |
J. Bruce Fields | 3dcd1d8 | 2021-12-07 17:32:21 -0500 | [diff] [blame] | 389 | * |
| 390 | * But you could also argue that our current code is already overkill, |
| 391 | * since it only exists to return NFS4ERR_INVAL on incorrect client |
| 392 | * behavior. |
J. Bruce Fields | ebd9d2c | 2021-04-16 14:00:17 -0400 | [diff] [blame] | 393 | */ |
| 394 | static unsigned int |
| 395 | bmap_to_share_mode(unsigned long bmap) |
| 396 | { |
| 397 | int i; |
| 398 | unsigned int access = 0; |
| 399 | |
| 400 | for (i = 1; i < 4; i++) { |
| 401 | if (test_bit(i, &bmap)) |
| 402 | access |= i; |
| 403 | } |
| 404 | return access; |
| 405 | } |
| 406 | |
| 407 | /* set share access for a given stateid */ |
| 408 | static inline void |
| 409 | set_access(u32 access, struct nfs4_ol_stateid *stp) |
| 410 | { |
| 411 | unsigned char mask = 1 << access; |
| 412 | |
| 413 | WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH); |
| 414 | stp->st_access_bmap |= mask; |
| 415 | } |
| 416 | |
| 417 | /* clear share access for a given stateid */ |
| 418 | static inline void |
| 419 | clear_access(u32 access, struct nfs4_ol_stateid *stp) |
| 420 | { |
| 421 | unsigned char mask = 1 << access; |
| 422 | |
| 423 | WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH); |
| 424 | stp->st_access_bmap &= ~mask; |
| 425 | } |
| 426 | |
| 427 | /* test whether a given stateid has access */ |
| 428 | static inline bool |
| 429 | test_access(u32 access, struct nfs4_ol_stateid *stp) |
| 430 | { |
| 431 | unsigned char mask = 1 << access; |
| 432 | |
| 433 | return (bool)(stp->st_access_bmap & mask); |
| 434 | } |
| 435 | |
| 436 | /* set share deny for a given stateid */ |
| 437 | static inline void |
| 438 | set_deny(u32 deny, struct nfs4_ol_stateid *stp) |
| 439 | { |
| 440 | unsigned char mask = 1 << deny; |
| 441 | |
| 442 | WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH); |
| 443 | stp->st_deny_bmap |= mask; |
| 444 | } |
| 445 | |
| 446 | /* clear share deny for a given stateid */ |
| 447 | static inline void |
| 448 | clear_deny(u32 deny, struct nfs4_ol_stateid *stp) |
| 449 | { |
| 450 | unsigned char mask = 1 << deny; |
| 451 | |
| 452 | WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH); |
| 453 | stp->st_deny_bmap &= ~mask; |
| 454 | } |
| 455 | |
| 456 | /* test whether a given stateid is denying specific access */ |
| 457 | static inline bool |
| 458 | test_deny(u32 deny, struct nfs4_ol_stateid *stp) |
| 459 | { |
| 460 | unsigned char mask = 1 << deny; |
| 461 | |
| 462 | return (bool)(stp->st_deny_bmap & mask); |
| 463 | } |
| 464 | |
| 465 | static int nfs4_access_to_omode(u32 access) |
| 466 | { |
| 467 | switch (access & NFS4_SHARE_ACCESS_BOTH) { |
| 468 | case NFS4_SHARE_ACCESS_READ: |
| 469 | return O_RDONLY; |
| 470 | case NFS4_SHARE_ACCESS_WRITE: |
| 471 | return O_WRONLY; |
| 472 | case NFS4_SHARE_ACCESS_BOTH: |
| 473 | return O_RDWR; |
| 474 | } |
| 475 | WARN_ON_ONCE(1); |
| 476 | return O_RDONLY; |
| 477 | } |
| 478 | |
| 479 | static inline int |
| 480 | access_permit_read(struct nfs4_ol_stateid *stp) |
| 481 | { |
| 482 | return test_access(NFS4_SHARE_ACCESS_READ, stp) || |
| 483 | test_access(NFS4_SHARE_ACCESS_BOTH, stp) || |
| 484 | test_access(NFS4_SHARE_ACCESS_WRITE, stp); |
| 485 | } |
| 486 | |
| 487 | static inline int |
| 488 | access_permit_write(struct nfs4_ol_stateid *stp) |
| 489 | { |
| 490 | return test_access(NFS4_SHARE_ACCESS_WRITE, stp) || |
| 491 | test_access(NFS4_SHARE_ACCESS_BOTH, stp); |
| 492 | } |
| 493 | |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 494 | static inline struct nfs4_stateowner * |
| 495 | nfs4_get_stateowner(struct nfs4_stateowner *sop) |
| 496 | { |
| 497 | atomic_inc(&sop->so_count); |
| 498 | return sop; |
| 499 | } |
| 500 | |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 501 | static int |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 502 | same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner) |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 503 | { |
| 504 | return (sop->so_owner.len == owner->len) && |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 505 | 0 == memcmp(sop->so_owner.data, owner->data, owner->len); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | static struct nfs4_openowner * |
| 509 | find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open, |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 510 | struct nfs4_client *clp) |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 511 | { |
| 512 | struct nfs4_stateowner *so; |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 513 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 514 | lockdep_assert_held(&clp->cl_lock); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 515 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 516 | list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval], |
| 517 | so_strhash) { |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 518 | if (!so->so_is_open_owner) |
| 519 | continue; |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 520 | if (same_owner_str(so, &open->op_owner)) |
| 521 | return openowner(nfs4_get_stateowner(so)); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 522 | } |
| 523 | return NULL; |
| 524 | } |
| 525 | |
| 526 | static struct nfs4_openowner * |
| 527 | find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open, |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 528 | struct nfs4_client *clp) |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 529 | { |
| 530 | struct nfs4_openowner *oo; |
| 531 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 532 | spin_lock(&clp->cl_lock); |
| 533 | oo = find_openstateowner_str_locked(hashval, open, clp); |
| 534 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 535 | return oo; |
| 536 | } |
| 537 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 538 | static inline u32 |
| 539 | opaque_hashval(const void *ptr, int nbytes) |
| 540 | { |
| 541 | unsigned char *cptr = (unsigned char *) ptr; |
| 542 | |
| 543 | u32 x = 0; |
| 544 | while (nbytes--) { |
| 545 | x *= 37; |
| 546 | x += *cptr++; |
| 547 | } |
| 548 | return x; |
| 549 | } |
| 550 | |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 551 | static void nfsd4_free_file_rcu(struct rcu_head *rcu) |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 552 | { |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 553 | struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu); |
| 554 | |
| 555 | kmem_cache_free(file_slab, fp); |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 556 | } |
| 557 | |
Christoph Hellwig | e6ba76e | 2014-08-14 08:50:16 +0200 | [diff] [blame] | 558 | void |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 559 | put_nfs4_file(struct nfs4_file *fi) |
| 560 | { |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 561 | might_lock(&state_lock); |
| 562 | |
Elena Reshetova | 818a34e | 2017-10-20 12:53:30 +0300 | [diff] [blame] | 563 | if (refcount_dec_and_lock(&fi->fi_ref, &state_lock)) { |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 564 | hlist_del_rcu(&fi->fi_hash); |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 565 | spin_unlock(&state_lock); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 566 | WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate)); |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 567 | WARN_ON_ONCE(!list_empty(&fi->fi_delegations)); |
| 568 | call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu); |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 569 | } |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 570 | } |
| 571 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 572 | static struct nfsd_file * |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 573 | __nfs4_get_fd(struct nfs4_file *f, int oflag) |
| 574 | { |
| 575 | if (f->fi_fds[oflag]) |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 576 | return nfsd_file_get(f->fi_fds[oflag]); |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 577 | return NULL; |
| 578 | } |
| 579 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 580 | static struct nfsd_file * |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 581 | find_writeable_file_locked(struct nfs4_file *f) |
| 582 | { |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 583 | struct nfsd_file *ret; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 584 | |
| 585 | lockdep_assert_held(&f->fi_lock); |
| 586 | |
| 587 | ret = __nfs4_get_fd(f, O_WRONLY); |
| 588 | if (!ret) |
| 589 | ret = __nfs4_get_fd(f, O_RDWR); |
| 590 | return ret; |
| 591 | } |
| 592 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 593 | static struct nfsd_file * |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 594 | find_writeable_file(struct nfs4_file *f) |
| 595 | { |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 596 | struct nfsd_file *ret; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 597 | |
| 598 | spin_lock(&f->fi_lock); |
| 599 | ret = find_writeable_file_locked(f); |
| 600 | spin_unlock(&f->fi_lock); |
| 601 | |
| 602 | return ret; |
| 603 | } |
| 604 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 605 | static struct nfsd_file * |
| 606 | find_readable_file_locked(struct nfs4_file *f) |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 607 | { |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 608 | struct nfsd_file *ret; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 609 | |
| 610 | lockdep_assert_held(&f->fi_lock); |
| 611 | |
| 612 | ret = __nfs4_get_fd(f, O_RDONLY); |
| 613 | if (!ret) |
| 614 | ret = __nfs4_get_fd(f, O_RDWR); |
| 615 | return ret; |
| 616 | } |
| 617 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 618 | static struct nfsd_file * |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 619 | find_readable_file(struct nfs4_file *f) |
| 620 | { |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 621 | struct nfsd_file *ret; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 622 | |
| 623 | spin_lock(&f->fi_lock); |
| 624 | ret = find_readable_file_locked(f); |
| 625 | spin_unlock(&f->fi_lock); |
| 626 | |
| 627 | return ret; |
| 628 | } |
| 629 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 630 | struct nfsd_file * |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 631 | find_any_file(struct nfs4_file *f) |
| 632 | { |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 633 | struct nfsd_file *ret; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 634 | |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 635 | if (!f) |
| 636 | return NULL; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 637 | spin_lock(&f->fi_lock); |
| 638 | ret = __nfs4_get_fd(f, O_RDWR); |
| 639 | if (!ret) { |
| 640 | ret = __nfs4_get_fd(f, O_WRONLY); |
| 641 | if (!ret) |
| 642 | ret = __nfs4_get_fd(f, O_RDONLY); |
| 643 | } |
| 644 | spin_unlock(&f->fi_lock); |
| 645 | return ret; |
| 646 | } |
| 647 | |
J. Bruce Fields | 9affa43 | 2020-07-15 13:31:36 -0400 | [diff] [blame] | 648 | static struct nfsd_file *find_deleg_file(struct nfs4_file *f) |
| 649 | { |
| 650 | struct nfsd_file *ret = NULL; |
| 651 | |
| 652 | spin_lock(&f->fi_lock); |
| 653 | if (f->fi_deleg_file) |
| 654 | ret = nfsd_file_get(f->fi_deleg_file); |
| 655 | spin_unlock(&f->fi_lock); |
| 656 | return ret; |
| 657 | } |
| 658 | |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 659 | static atomic_long_t num_delegations; |
Zhang Yanfei | 697ce9b | 2013-02-22 16:35:47 -0800 | [diff] [blame] | 660 | unsigned long max_delegations; |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 661 | |
| 662 | /* |
| 663 | * Open owner state (share locks) |
| 664 | */ |
| 665 | |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 666 | /* hash tables for lock and open owners */ |
| 667 | #define OWNER_HASH_BITS 8 |
| 668 | #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS) |
| 669 | #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1) |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 670 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 671 | static unsigned int ownerstr_hashval(struct xdr_netobj *ownername) |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 672 | { |
| 673 | unsigned int ret; |
| 674 | |
| 675 | ret = opaque_hashval(ownername->data, ownername->len); |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 676 | return ret & OWNER_HASH_MASK; |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 677 | } |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 678 | |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 679 | /* hash table for nfs4_file */ |
| 680 | #define FILE_HASH_BITS 8 |
| 681 | #define FILE_HASH_SIZE (1 << FILE_HASH_BITS) |
Shan Wei | 3507958 | 2011-01-14 17:35:59 +0800 | [diff] [blame] | 682 | |
J. Bruce Fields | f9b60e2 | 2021-04-16 14:00:15 -0400 | [diff] [blame] | 683 | static unsigned int file_hashval(struct svc_fh *fh) |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 684 | { |
J. Bruce Fields | f9b60e2 | 2021-04-16 14:00:15 -0400 | [diff] [blame] | 685 | struct inode *inode = d_inode(fh->fh_dentry); |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 686 | |
J. Bruce Fields | f9b60e2 | 2021-04-16 14:00:15 -0400 | [diff] [blame] | 687 | /* XXX: why not (here & in file cache) use inode? */ |
| 688 | return (unsigned int)hash_long(inode->i_ino, FILE_HASH_BITS); |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 689 | } |
| 690 | |
Jeff Layton | 89876f8 | 2013-04-02 09:01:59 -0400 | [diff] [blame] | 691 | static struct hlist_head file_hashtbl[FILE_HASH_SIZE]; |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 692 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 693 | static void |
| 694 | __nfs4_file_get_access(struct nfs4_file *fp, u32 access) |
J. Bruce Fields | 3477565 | 2013-08-23 17:55:18 -0400 | [diff] [blame] | 695 | { |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 696 | lockdep_assert_held(&fp->fi_lock); |
| 697 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 698 | if (access & NFS4_SHARE_ACCESS_WRITE) |
| 699 | atomic_inc(&fp->fi_access[O_WRONLY]); |
| 700 | if (access & NFS4_SHARE_ACCESS_READ) |
| 701 | atomic_inc(&fp->fi_access[O_RDONLY]); |
J. Bruce Fields | 3477565 | 2013-08-23 17:55:18 -0400 | [diff] [blame] | 702 | } |
| 703 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 704 | static __be32 |
| 705 | nfs4_file_get_access(struct nfs4_file *fp, u32 access) |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 706 | { |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 707 | lockdep_assert_held(&fp->fi_lock); |
| 708 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 709 | /* Does this access mode make sense? */ |
| 710 | if (access & ~NFS4_SHARE_ACCESS_BOTH) |
| 711 | return nfserr_inval; |
| 712 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 713 | /* Does it conflict with a deny mode already set? */ |
| 714 | if ((access & fp->fi_share_deny) != 0) |
| 715 | return nfserr_share_denied; |
| 716 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 717 | __nfs4_file_get_access(fp, access); |
| 718 | return nfs_ok; |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 719 | } |
| 720 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 721 | static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny) |
| 722 | { |
| 723 | /* Common case is that there is no deny mode. */ |
| 724 | if (deny) { |
| 725 | /* Does this deny mode make sense? */ |
| 726 | if (deny & ~NFS4_SHARE_DENY_BOTH) |
| 727 | return nfserr_inval; |
| 728 | |
| 729 | if ((deny & NFS4_SHARE_DENY_READ) && |
| 730 | atomic_read(&fp->fi_access[O_RDONLY])) |
| 731 | return nfserr_share_denied; |
| 732 | |
| 733 | if ((deny & NFS4_SHARE_DENY_WRITE) && |
| 734 | atomic_read(&fp->fi_access[O_WRONLY])) |
| 735 | return nfserr_share_denied; |
| 736 | } |
| 737 | return nfs_ok; |
| 738 | } |
| 739 | |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 740 | static void __nfs4_file_put_access(struct nfs4_file *fp, int oflag) |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 741 | { |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 742 | might_lock(&fp->fi_lock); |
| 743 | |
| 744 | if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) { |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 745 | struct nfsd_file *f1 = NULL; |
| 746 | struct nfsd_file *f2 = NULL; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 747 | |
Jeff Layton | 6d338b5 | 2014-07-10 14:07:29 -0400 | [diff] [blame] | 748 | swap(f1, fp->fi_fds[oflag]); |
J. Bruce Fields | 0c7c3e6 | 2013-03-28 20:37:14 -0400 | [diff] [blame] | 749 | if (atomic_read(&fp->fi_access[1 - oflag]) == 0) |
Jeff Layton | 6d338b5 | 2014-07-10 14:07:29 -0400 | [diff] [blame] | 750 | swap(f2, fp->fi_fds[O_RDWR]); |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 751 | spin_unlock(&fp->fi_lock); |
| 752 | if (f1) |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 753 | nfsd_file_put(f1); |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 754 | if (f2) |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 755 | nfsd_file_put(f2); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 756 | } |
| 757 | } |
| 758 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 759 | static void nfs4_file_put_access(struct nfs4_file *fp, u32 access) |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 760 | { |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 761 | WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH); |
| 762 | |
| 763 | if (access & NFS4_SHARE_ACCESS_WRITE) |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 764 | __nfs4_file_put_access(fp, O_WRONLY); |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 765 | if (access & NFS4_SHARE_ACCESS_READ) |
| 766 | __nfs4_file_put_access(fp, O_RDONLY); |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 767 | } |
| 768 | |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 769 | /* |
| 770 | * Allocate a new open/delegation state counter. This is needed for |
| 771 | * pNFS for proper return on close semantics. |
| 772 | * |
| 773 | * Note that we only allocate it for pNFS-enabled exports, otherwise |
| 774 | * all pointers to struct nfs4_clnt_odstate are always NULL. |
| 775 | */ |
| 776 | static struct nfs4_clnt_odstate * |
| 777 | alloc_clnt_odstate(struct nfs4_client *clp) |
| 778 | { |
| 779 | struct nfs4_clnt_odstate *co; |
| 780 | |
| 781 | co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL); |
| 782 | if (co) { |
| 783 | co->co_client = clp; |
Elena Reshetova | cff7cb2 | 2017-10-20 12:53:29 +0300 | [diff] [blame] | 784 | refcount_set(&co->co_odcount, 1); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 785 | } |
| 786 | return co; |
| 787 | } |
| 788 | |
| 789 | static void |
| 790 | hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co) |
| 791 | { |
| 792 | struct nfs4_file *fp = co->co_file; |
| 793 | |
| 794 | lockdep_assert_held(&fp->fi_lock); |
| 795 | list_add(&co->co_perfile, &fp->fi_clnt_odstate); |
| 796 | } |
| 797 | |
| 798 | static inline void |
| 799 | get_clnt_odstate(struct nfs4_clnt_odstate *co) |
| 800 | { |
| 801 | if (co) |
Elena Reshetova | cff7cb2 | 2017-10-20 12:53:29 +0300 | [diff] [blame] | 802 | refcount_inc(&co->co_odcount); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 803 | } |
| 804 | |
| 805 | static void |
| 806 | put_clnt_odstate(struct nfs4_clnt_odstate *co) |
| 807 | { |
| 808 | struct nfs4_file *fp; |
| 809 | |
| 810 | if (!co) |
| 811 | return; |
| 812 | |
| 813 | fp = co->co_file; |
Elena Reshetova | cff7cb2 | 2017-10-20 12:53:29 +0300 | [diff] [blame] | 814 | if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) { |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 815 | list_del(&co->co_perfile); |
| 816 | spin_unlock(&fp->fi_lock); |
| 817 | |
| 818 | nfsd4_return_all_file_layouts(co->co_client, fp); |
| 819 | kmem_cache_free(odstate_slab, co); |
| 820 | } |
| 821 | } |
| 822 | |
| 823 | static struct nfs4_clnt_odstate * |
| 824 | find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new) |
| 825 | { |
| 826 | struct nfs4_clnt_odstate *co; |
| 827 | struct nfs4_client *cl; |
| 828 | |
| 829 | if (!new) |
| 830 | return NULL; |
| 831 | |
| 832 | cl = new->co_client; |
| 833 | |
| 834 | spin_lock(&fp->fi_lock); |
| 835 | list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) { |
| 836 | if (co->co_client == cl) { |
| 837 | get_clnt_odstate(co); |
| 838 | goto out; |
| 839 | } |
| 840 | } |
| 841 | co = new; |
| 842 | co->co_file = fp; |
| 843 | hash_clnt_odstate_locked(new); |
| 844 | out: |
| 845 | spin_unlock(&fp->fi_lock); |
| 846 | return co; |
| 847 | } |
| 848 | |
Kinglong Mee | d19fb70 | 2017-01-18 19:04:42 +0800 | [diff] [blame] | 849 | struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab, |
| 850 | void (*sc_free)(struct nfs4_stid *)) |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 851 | { |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 852 | struct nfs4_stid *stid; |
| 853 | int new_id; |
| 854 | |
Trond Myklebust | f833883 | 2014-07-25 07:34:19 -0400 | [diff] [blame] | 855 | stid = kmem_cache_zalloc(slab, GFP_KERNEL); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 856 | if (!stid) |
| 857 | return NULL; |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 858 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 859 | idr_preload(GFP_KERNEL); |
| 860 | spin_lock(&cl->cl_lock); |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 861 | /* Reserving 0 for start of file in nfsdfs "states" file: */ |
| 862 | new_id = idr_alloc_cyclic(&cl->cl_stateids, stid, 1, 0, GFP_NOWAIT); |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 863 | spin_unlock(&cl->cl_lock); |
| 864 | idr_preload_end(); |
Tejun Heo | ebd6c70 | 2013-03-13 14:59:37 -0700 | [diff] [blame] | 865 | if (new_id < 0) |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 866 | goto out_free; |
Kinglong Mee | d19fb70 | 2017-01-18 19:04:42 +0800 | [diff] [blame] | 867 | |
| 868 | stid->sc_free = sc_free; |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 869 | stid->sc_client = cl; |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 870 | stid->sc_stateid.si_opaque.so_id = new_id; |
| 871 | stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid; |
| 872 | /* Will be incremented before return to client: */ |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 873 | refcount_set(&stid->sc_count, 1); |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 874 | spin_lock_init(&stid->sc_lock); |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 875 | INIT_LIST_HEAD(&stid->sc_cp_list); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 876 | |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 877 | /* |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 878 | * It shouldn't be a problem to reuse an opaque stateid value. |
| 879 | * I don't think it is for 4.1. But with 4.0 I worry that, for |
| 880 | * example, a stray write retransmission could be accepted by |
| 881 | * the server when it should have been rejected. Therefore, |
| 882 | * adopt a trick from the sctp code to attempt to maximize the |
| 883 | * amount of time until an id is reused, by ensuring they always |
| 884 | * "increase" (mod INT_MAX): |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 885 | */ |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 886 | return stid; |
| 887 | out_free: |
Wei Yongjun | 2c44a23 | 2013-04-09 14:15:31 +0800 | [diff] [blame] | 888 | kmem_cache_free(slab, stid); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 889 | return NULL; |
J. Bruce Fields | 2a74aba | 2011-09-23 17:20:02 -0400 | [diff] [blame] | 890 | } |
| 891 | |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 892 | /* |
| 893 | * Create a unique stateid_t to represent each COPY. |
| 894 | */ |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 895 | static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid, |
| 896 | unsigned char sc_type) |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 897 | { |
| 898 | int new_id; |
| 899 | |
Arnd Bergmann | 9cc7680 | 2019-10-31 15:53:13 +0100 | [diff] [blame] | 900 | stid->stid.si_opaque.so_clid.cl_boot = (u32)nn->boot_time; |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 901 | stid->stid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id; |
| 902 | stid->sc_type = sc_type; |
| 903 | |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 904 | idr_preload(GFP_KERNEL); |
| 905 | spin_lock(&nn->s2s_cp_lock); |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 906 | new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, stid, 0, 0, GFP_NOWAIT); |
| 907 | stid->stid.si_opaque.so_id = new_id; |
Dai Ngo | ca9364d | 2020-11-30 16:24:49 -0500 | [diff] [blame] | 908 | stid->stid.si_generation = 1; |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 909 | spin_unlock(&nn->s2s_cp_lock); |
| 910 | idr_preload_end(); |
| 911 | if (new_id < 0) |
| 912 | return 0; |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 913 | return 1; |
| 914 | } |
| 915 | |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 916 | int nfs4_init_copy_state(struct nfsd_net *nn, struct nfsd4_copy *copy) |
| 917 | { |
| 918 | return nfs4_init_cp_state(nn, ©->cp_stateid, NFS4_COPY_STID); |
| 919 | } |
| 920 | |
| 921 | struct nfs4_cpntf_state *nfs4_alloc_init_cpntf_state(struct nfsd_net *nn, |
| 922 | struct nfs4_stid *p_stid) |
| 923 | { |
| 924 | struct nfs4_cpntf_state *cps; |
| 925 | |
| 926 | cps = kzalloc(sizeof(struct nfs4_cpntf_state), GFP_KERNEL); |
| 927 | if (!cps) |
| 928 | return NULL; |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 929 | cps->cpntf_time = ktime_get_boottime_seconds(); |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 930 | refcount_set(&cps->cp_stateid.sc_count, 1); |
| 931 | if (!nfs4_init_cp_state(nn, &cps->cp_stateid, NFS4_COPYNOTIFY_STID)) |
| 932 | goto out_free; |
| 933 | spin_lock(&nn->s2s_cp_lock); |
| 934 | list_add(&cps->cp_list, &p_stid->sc_cp_list); |
| 935 | spin_unlock(&nn->s2s_cp_lock); |
| 936 | return cps; |
| 937 | out_free: |
| 938 | kfree(cps); |
| 939 | return NULL; |
| 940 | } |
| 941 | |
| 942 | void nfs4_free_copy_state(struct nfsd4_copy *copy) |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 943 | { |
| 944 | struct nfsd_net *nn; |
| 945 | |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 946 | WARN_ON_ONCE(copy->cp_stateid.sc_type != NFS4_COPY_STID); |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 947 | nn = net_generic(copy->cp_clp->net, nfsd_net_id); |
| 948 | spin_lock(&nn->s2s_cp_lock); |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 949 | idr_remove(&nn->s2s_cp_stateids, |
| 950 | copy->cp_stateid.stid.si_opaque.so_id); |
| 951 | spin_unlock(&nn->s2s_cp_lock); |
| 952 | } |
| 953 | |
| 954 | static void nfs4_free_cpntf_statelist(struct net *net, struct nfs4_stid *stid) |
| 955 | { |
| 956 | struct nfs4_cpntf_state *cps; |
| 957 | struct nfsd_net *nn; |
| 958 | |
| 959 | nn = net_generic(net, nfsd_net_id); |
| 960 | spin_lock(&nn->s2s_cp_lock); |
| 961 | while (!list_empty(&stid->sc_cp_list)) { |
| 962 | cps = list_first_entry(&stid->sc_cp_list, |
| 963 | struct nfs4_cpntf_state, cp_list); |
| 964 | _free_cpntf_state_locked(nn, cps); |
| 965 | } |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 966 | spin_unlock(&nn->s2s_cp_lock); |
| 967 | } |
| 968 | |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 969 | static struct nfs4_ol_stateid * nfs4_alloc_open_stateid(struct nfs4_client *clp) |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 970 | { |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 971 | struct nfs4_stid *stid; |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 972 | |
Kinglong Mee | d19fb70 | 2017-01-18 19:04:42 +0800 | [diff] [blame] | 973 | stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 974 | if (!stid) |
| 975 | return NULL; |
| 976 | |
Kinglong Mee | d19fb70 | 2017-01-18 19:04:42 +0800 | [diff] [blame] | 977 | return openlockstateid(stid); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 978 | } |
| 979 | |
| 980 | static void nfs4_free_deleg(struct nfs4_stid *stid) |
| 981 | { |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 982 | kmem_cache_free(deleg_slab, stid); |
| 983 | atomic_long_dec(&num_delegations); |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 984 | } |
| 985 | |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 986 | /* |
| 987 | * When we recall a delegation, we should be careful not to hand it |
| 988 | * out again straight away. |
| 989 | * To ensure this we keep a pair of bloom filters ('new' and 'old') |
| 990 | * in which the filehandles of recalled delegations are "stored". |
| 991 | * If a filehandle appear in either filter, a delegation is blocked. |
| 992 | * When a delegation is recalled, the filehandle is stored in the "new" |
| 993 | * filter. |
| 994 | * Every 30 seconds we swap the filters and clear the "new" one, |
| 995 | * unless both are empty of course. |
| 996 | * |
| 997 | * Each filter is 256 bits. We hash the filehandle to 32bit and use the |
| 998 | * low 3 bytes as hash-table indices. |
| 999 | * |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 1000 | * 'blocked_delegations_lock', which is always taken in block_delegations(), |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 1001 | * is used to manage concurrent access. Testing does not need the lock |
| 1002 | * except when swapping the two filters. |
| 1003 | */ |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 1004 | static DEFINE_SPINLOCK(blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 1005 | static struct bloom_pair { |
| 1006 | int entries, old_entries; |
Arnd Bergmann | b3f255e | 2017-10-20 11:25:34 +0200 | [diff] [blame] | 1007 | time64_t swap_time; |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 1008 | int new; /* index into 'set' */ |
| 1009 | DECLARE_BITMAP(set[2], 256); |
| 1010 | } blocked_delegations; |
| 1011 | |
| 1012 | static int delegation_blocked(struct knfsd_fh *fh) |
| 1013 | { |
| 1014 | u32 hash; |
| 1015 | struct bloom_pair *bd = &blocked_delegations; |
| 1016 | |
| 1017 | if (bd->entries == 0) |
| 1018 | return 0; |
Arnd Bergmann | b3f255e | 2017-10-20 11:25:34 +0200 | [diff] [blame] | 1019 | if (ktime_get_seconds() - bd->swap_time > 30) { |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 1020 | spin_lock(&blocked_delegations_lock); |
Arnd Bergmann | b3f255e | 2017-10-20 11:25:34 +0200 | [diff] [blame] | 1021 | if (ktime_get_seconds() - bd->swap_time > 30) { |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 1022 | bd->entries -= bd->old_entries; |
| 1023 | bd->old_entries = bd->entries; |
| 1024 | memset(bd->set[bd->new], 0, |
| 1025 | sizeof(bd->set[0])); |
| 1026 | bd->new = 1-bd->new; |
Arnd Bergmann | b3f255e | 2017-10-20 11:25:34 +0200 | [diff] [blame] | 1027 | bd->swap_time = ktime_get_seconds(); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 1028 | } |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 1029 | spin_unlock(&blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 1030 | } |
NeilBrown | d8b2607 | 2021-09-02 11:16:32 +1000 | [diff] [blame] | 1031 | hash = jhash(&fh->fh_raw, fh->fh_size, 0); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 1032 | if (test_bit(hash&255, bd->set[0]) && |
| 1033 | test_bit((hash>>8)&255, bd->set[0]) && |
| 1034 | test_bit((hash>>16)&255, bd->set[0])) |
| 1035 | return 1; |
| 1036 | |
| 1037 | if (test_bit(hash&255, bd->set[1]) && |
| 1038 | test_bit((hash>>8)&255, bd->set[1]) && |
| 1039 | test_bit((hash>>16)&255, bd->set[1])) |
| 1040 | return 1; |
| 1041 | |
| 1042 | return 0; |
| 1043 | } |
| 1044 | |
| 1045 | static void block_delegations(struct knfsd_fh *fh) |
| 1046 | { |
| 1047 | u32 hash; |
| 1048 | struct bloom_pair *bd = &blocked_delegations; |
| 1049 | |
NeilBrown | d8b2607 | 2021-09-02 11:16:32 +1000 | [diff] [blame] | 1050 | hash = jhash(&fh->fh_raw, fh->fh_size, 0); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 1051 | |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 1052 | spin_lock(&blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 1053 | __set_bit(hash&255, bd->set[bd->new]); |
| 1054 | __set_bit((hash>>8)&255, bd->set[bd->new]); |
| 1055 | __set_bit((hash>>16)&255, bd->set[bd->new]); |
| 1056 | if (bd->entries == 0) |
Arnd Bergmann | b3f255e | 2017-10-20 11:25:34 +0200 | [diff] [blame] | 1057 | bd->swap_time = ktime_get_seconds(); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 1058 | bd->entries += 1; |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 1059 | spin_unlock(&blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 1060 | } |
| 1061 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | static struct nfs4_delegation * |
J. Bruce Fields | 86d29b1 | 2018-02-21 15:27:28 -0500 | [diff] [blame] | 1063 | alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp, |
| 1064 | struct svc_fh *current_fh, |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 1065 | struct nfs4_clnt_odstate *odstate) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1066 | { |
| 1067 | struct nfs4_delegation *dp; |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 1068 | long n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1069 | |
| 1070 | dprintk("NFSD alloc_init_deleg\n"); |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 1071 | n = atomic_long_inc_return(&num_delegations); |
| 1072 | if (n < 0 || n > max_delegations) |
| 1073 | goto out_dec; |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 1074 | if (delegation_blocked(¤t_fh->fh_handle)) |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 1075 | goto out_dec; |
Kinglong Mee | d19fb70 | 2017-01-18 19:04:42 +0800 | [diff] [blame] | 1076 | dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg)); |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 1077 | if (dp == NULL) |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 1078 | goto out_dec; |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1079 | |
J. Bruce Fields | 2a74aba | 2011-09-23 17:20:02 -0400 | [diff] [blame] | 1080 | /* |
| 1081 | * delegation seqid's are never incremented. The 4.1 special |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 1082 | * meaning of seqid 0 isn't meaningful, really, but let's avoid |
| 1083 | * 0 anyway just for consistency and use 1: |
J. Bruce Fields | 2a74aba | 2011-09-23 17:20:02 -0400 | [diff] [blame] | 1084 | */ |
| 1085 | dp->dl_stid.sc_stateid.si_generation = 1; |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 1086 | INIT_LIST_HEAD(&dp->dl_perfile); |
| 1087 | INIT_LIST_HEAD(&dp->dl_perclnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | INIT_LIST_HEAD(&dp->dl_recall_lru); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 1089 | dp->dl_clnt_odstate = odstate; |
| 1090 | get_clnt_odstate(odstate); |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 1091 | dp->dl_type = NFS4_OPEN_DELEGATE_READ; |
Christoph Hellwig | f0b5de1 | 2014-09-24 12:19:18 +0200 | [diff] [blame] | 1092 | dp->dl_retries = 1; |
| 1093 | nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client, |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 1094 | &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL); |
J. Bruce Fields | 86d29b1 | 2018-02-21 15:27:28 -0500 | [diff] [blame] | 1095 | get_nfs4_file(fp); |
| 1096 | dp->dl_stid.sc_file = fp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | return dp; |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 1098 | out_dec: |
| 1099 | atomic_long_dec(&num_delegations); |
| 1100 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1101 | } |
| 1102 | |
| 1103 | void |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1104 | nfs4_put_stid(struct nfs4_stid *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 1106 | struct nfs4_file *fp = s->sc_file; |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1107 | struct nfs4_client *clp = s->sc_client; |
| 1108 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 1109 | might_lock(&clp->cl_lock); |
| 1110 | |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 1111 | if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) { |
Jeff Layton | b401be22 | 2014-07-29 21:34:33 -0400 | [diff] [blame] | 1112 | wake_up_all(&close_wq); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1113 | return; |
Jeff Layton | b401be22 | 2014-07-29 21:34:33 -0400 | [diff] [blame] | 1114 | } |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1115 | idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id); |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 1116 | nfs4_free_cpntf_statelist(clp->net, s); |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 1117 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1118 | s->sc_free(s); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 1119 | if (fp) |
| 1120 | put_nfs4_file(fp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1121 | } |
| 1122 | |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 1123 | void |
| 1124 | nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid) |
| 1125 | { |
| 1126 | stateid_t *src = &stid->sc_stateid; |
| 1127 | |
| 1128 | spin_lock(&stid->sc_lock); |
| 1129 | if (unlikely(++src->si_generation == 0)) |
| 1130 | src->si_generation = 1; |
| 1131 | memcpy(dst, src, sizeof(*dst)); |
| 1132 | spin_unlock(&stid->sc_lock); |
| 1133 | } |
| 1134 | |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 1135 | static void put_deleg_file(struct nfs4_file *fp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | { |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 1137 | struct nfsd_file *nf = NULL; |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 1138 | |
| 1139 | spin_lock(&fp->fi_lock); |
| 1140 | if (--fp->fi_delegees == 0) |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 1141 | swap(nf, fp->fi_deleg_file); |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 1142 | spin_unlock(&fp->fi_lock); |
| 1143 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 1144 | if (nf) |
| 1145 | nfsd_file_put(nf); |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 1146 | } |
| 1147 | |
| 1148 | static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp) |
| 1149 | { |
J. Bruce Fields | cba7b3d | 2018-02-15 15:50:49 -0500 | [diff] [blame] | 1150 | struct nfs4_file *fp = dp->dl_stid.sc_file; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 1151 | struct nfsd_file *nf = fp->fi_deleg_file; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 1152 | |
J. Bruce Fields | b8232d3 | 2018-02-15 15:29:15 -0500 | [diff] [blame] | 1153 | WARN_ON_ONCE(!fp->fi_delegees); |
| 1154 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 1155 | vfs_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp); |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 1156 | put_deleg_file(fp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1157 | } |
| 1158 | |
J. Bruce Fields | 0af6e69 | 2018-02-21 15:11:03 -0500 | [diff] [blame] | 1159 | static void destroy_unhashed_deleg(struct nfs4_delegation *dp) |
| 1160 | { |
| 1161 | put_clnt_odstate(dp->dl_clnt_odstate); |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 1162 | nfs4_unlock_deleg_lease(dp); |
J. Bruce Fields | 0af6e69 | 2018-02-21 15:11:03 -0500 | [diff] [blame] | 1163 | nfs4_put_stid(&dp->dl_stid); |
| 1164 | } |
| 1165 | |
Christoph Hellwig | cd61c52 | 2014-08-14 08:44:57 +0200 | [diff] [blame] | 1166 | void nfs4_unhash_stid(struct nfs4_stid *s) |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 1167 | { |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 1168 | s->sc_type = 0; |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 1169 | } |
| 1170 | |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1171 | /** |
J. Bruce Fields | 68b18f5 | 2018-02-19 11:38:33 -0500 | [diff] [blame] | 1172 | * nfs4_delegation_exists - Discover if this delegation already exists |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1173 | * @clp: a pointer to the nfs4_client we're granting a delegation to |
| 1174 | * @fp: a pointer to the nfs4_file we're granting a delegation on |
| 1175 | * |
| 1176 | * Return: |
J. Bruce Fields | 68b18f5 | 2018-02-19 11:38:33 -0500 | [diff] [blame] | 1177 | * On success: true iff an existing delegation is found |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1178 | */ |
| 1179 | |
J. Bruce Fields | 68b18f5 | 2018-02-19 11:38:33 -0500 | [diff] [blame] | 1180 | static bool |
| 1181 | nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp) |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 1182 | { |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1183 | struct nfs4_delegation *searchdp = NULL; |
| 1184 | struct nfs4_client *searchclp = NULL; |
| 1185 | |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 1186 | lockdep_assert_held(&state_lock); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 1187 | lockdep_assert_held(&fp->fi_lock); |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 1188 | |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1189 | list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) { |
| 1190 | searchclp = searchdp->dl_stid.sc_client; |
| 1191 | if (clp == searchclp) { |
Fengguang Wu | 51d87bc | 2018-03-22 13:37:20 +0800 | [diff] [blame] | 1192 | return true; |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1193 | } |
| 1194 | } |
Fengguang Wu | 51d87bc | 2018-03-22 13:37:20 +0800 | [diff] [blame] | 1195 | return false; |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | /** |
| 1199 | * hash_delegation_locked - Add a delegation to the appropriate lists |
| 1200 | * @dp: a pointer to the nfs4_delegation we are adding. |
| 1201 | * @fp: a pointer to the nfs4_file we're granting a delegation on |
| 1202 | * |
| 1203 | * Return: |
| 1204 | * On success: NULL if the delegation was successfully hashed. |
| 1205 | * |
| 1206 | * On error: -EAGAIN if one was previously granted to this |
| 1207 | * nfs4_client for this nfs4_file. Delegation is not hashed. |
| 1208 | * |
| 1209 | */ |
| 1210 | |
| 1211 | static int |
| 1212 | hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp) |
| 1213 | { |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1214 | struct nfs4_client *clp = dp->dl_stid.sc_client; |
| 1215 | |
| 1216 | lockdep_assert_held(&state_lock); |
| 1217 | lockdep_assert_held(&fp->fi_lock); |
| 1218 | |
J. Bruce Fields | 68b18f5 | 2018-02-19 11:38:33 -0500 | [diff] [blame] | 1219 | if (nfs4_delegation_exists(clp, fp)) |
| 1220 | return -EAGAIN; |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 1221 | refcount_inc(&dp->dl_stid.sc_count); |
Benny Halevy | 3fb87d1 | 2014-05-30 09:09:31 -0400 | [diff] [blame] | 1222 | dp->dl_stid.sc_type = NFS4_DELEG_STID; |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 1223 | list_add(&dp->dl_perfile, &fp->fi_delegations); |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1224 | list_add(&dp->dl_perclnt, &clp->cl_delegations); |
| 1225 | return 0; |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 1226 | } |
| 1227 | |
J. Bruce Fields | 548ec08 | 2021-11-29 15:08:00 -0500 | [diff] [blame] | 1228 | static bool delegation_hashed(struct nfs4_delegation *dp) |
| 1229 | { |
| 1230 | return !(list_empty(&dp->dl_perfile)); |
| 1231 | } |
| 1232 | |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 1233 | static bool |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 1234 | unhash_delegation_locked(struct nfs4_delegation *dp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1235 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 1236 | struct nfs4_file *fp = dp->dl_stid.sc_file; |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 1237 | |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 1238 | lockdep_assert_held(&state_lock); |
| 1239 | |
J. Bruce Fields | 548ec08 | 2021-11-29 15:08:00 -0500 | [diff] [blame] | 1240 | if (!delegation_hashed(dp)) |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 1241 | return false; |
| 1242 | |
Trond Myklebust | b0fc29d | 2014-07-16 10:31:59 -0400 | [diff] [blame] | 1243 | dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID; |
Jeff Layton | d55a166 | 2014-07-22 13:52:06 -0400 | [diff] [blame] | 1244 | /* Ensure that deleg break won't try to requeue it */ |
| 1245 | ++dp->dl_time; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 1246 | spin_lock(&fp->fi_lock); |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 1247 | list_del_init(&dp->dl_perclnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1248 | list_del_init(&dp->dl_recall_lru); |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 1249 | list_del_init(&dp->dl_perfile); |
| 1250 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 1251 | return true; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 1252 | } |
| 1253 | |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 1254 | static void destroy_delegation(struct nfs4_delegation *dp) |
| 1255 | { |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 1256 | bool unhashed; |
| 1257 | |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 1258 | spin_lock(&state_lock); |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 1259 | unhashed = unhash_delegation_locked(dp); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 1260 | spin_unlock(&state_lock); |
J. Bruce Fields | 0af6e69 | 2018-02-21 15:11:03 -0500 | [diff] [blame] | 1261 | if (unhashed) |
| 1262 | destroy_unhashed_deleg(dp); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 1263 | } |
| 1264 | |
| 1265 | static void revoke_delegation(struct nfs4_delegation *dp) |
| 1266 | { |
| 1267 | struct nfs4_client *clp = dp->dl_stid.sc_client; |
| 1268 | |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 1269 | WARN_ON(!list_empty(&dp->dl_recall_lru)); |
| 1270 | |
J. Bruce Fields | 0af6e69 | 2018-02-21 15:11:03 -0500 | [diff] [blame] | 1271 | if (clp->cl_minorversion) { |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 1272 | dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID; |
J. Bruce Fields | 0af6e69 | 2018-02-21 15:11:03 -0500 | [diff] [blame] | 1273 | refcount_inc(&dp->dl_stid.sc_count); |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 1274 | spin_lock(&clp->cl_lock); |
| 1275 | list_add(&dp->dl_recall_lru, &clp->cl_revoked); |
| 1276 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 1277 | } |
J. Bruce Fields | 0af6e69 | 2018-02-21 15:11:03 -0500 | [diff] [blame] | 1278 | destroy_unhashed_deleg(dp); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 1279 | } |
| 1280 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1281 | /* |
| 1282 | * SETCLIENTID state |
| 1283 | */ |
| 1284 | |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 1285 | static unsigned int clientid_hashval(u32 id) |
| 1286 | { |
| 1287 | return id & CLIENT_HASH_MASK; |
| 1288 | } |
| 1289 | |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 1290 | static unsigned int clientstr_hashval(struct xdr_netobj name) |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 1291 | { |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 1292 | return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK; |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 1293 | } |
| 1294 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | /* |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 1296 | * A stateid that had a deny mode associated with it is being released |
| 1297 | * or downgraded. Recalculate the deny mode on the file. |
| 1298 | */ |
| 1299 | static void |
| 1300 | recalculate_deny_mode(struct nfs4_file *fp) |
| 1301 | { |
| 1302 | struct nfs4_ol_stateid *stp; |
| 1303 | |
| 1304 | spin_lock(&fp->fi_lock); |
| 1305 | fp->fi_share_deny = 0; |
| 1306 | list_for_each_entry(stp, &fp->fi_stateids, st_perfile) |
| 1307 | fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap); |
| 1308 | spin_unlock(&fp->fi_lock); |
| 1309 | } |
| 1310 | |
| 1311 | static void |
| 1312 | reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp) |
| 1313 | { |
| 1314 | int i; |
| 1315 | bool change = false; |
| 1316 | |
| 1317 | for (i = 1; i < 4; i++) { |
| 1318 | if ((i & deny) != i) { |
| 1319 | change = true; |
| 1320 | clear_deny(i, stp); |
| 1321 | } |
| 1322 | } |
| 1323 | |
| 1324 | /* Recalculate per-file deny mode if there was a change */ |
| 1325 | if (change) |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 1326 | recalculate_deny_mode(stp->st_stid.sc_file); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 1327 | } |
| 1328 | |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 1329 | /* release all access and file references for a given stateid */ |
| 1330 | static void |
| 1331 | release_all_access(struct nfs4_ol_stateid *stp) |
| 1332 | { |
| 1333 | int i; |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 1334 | struct nfs4_file *fp = stp->st_stid.sc_file; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 1335 | |
| 1336 | if (fp && stp->st_deny_bmap != 0) |
| 1337 | recalculate_deny_mode(fp); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 1338 | |
| 1339 | for (i = 1; i < 4; i++) { |
| 1340 | if (test_access(i, stp)) |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 1341 | nfs4_file_put_access(stp->st_stid.sc_file, i); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 1342 | clear_access(i, stp); |
| 1343 | } |
| 1344 | } |
| 1345 | |
Kinglong Mee | d50ffde | 2015-07-16 12:05:07 +0800 | [diff] [blame] | 1346 | static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop) |
| 1347 | { |
| 1348 | kfree(sop->so_owner.data); |
| 1349 | sop->so_ops->so_free(sop); |
| 1350 | } |
| 1351 | |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 1352 | static void nfs4_put_stateowner(struct nfs4_stateowner *sop) |
| 1353 | { |
Jeff Layton | a819ecc | 2014-07-29 21:34:38 -0400 | [diff] [blame] | 1354 | struct nfs4_client *clp = sop->so_client; |
| 1355 | |
| 1356 | might_lock(&clp->cl_lock); |
| 1357 | |
| 1358 | if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock)) |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 1359 | return; |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 1360 | sop->so_ops->so_unhash(sop); |
Jeff Layton | a819ecc | 2014-07-29 21:34:38 -0400 | [diff] [blame] | 1361 | spin_unlock(&clp->cl_lock); |
Kinglong Mee | d50ffde | 2015-07-16 12:05:07 +0800 | [diff] [blame] | 1362 | nfs4_free_stateowner(sop); |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 1363 | } |
| 1364 | |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 1365 | static bool |
| 1366 | nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid *stp) |
| 1367 | { |
| 1368 | return list_empty(&stp->st_perfile); |
| 1369 | } |
| 1370 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1371 | static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1372 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 1373 | struct nfs4_file *fp = stp->st_stid.sc_file; |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 1374 | |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 1375 | lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock); |
| 1376 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1377 | if (list_empty(&stp->st_perfile)) |
| 1378 | return false; |
| 1379 | |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 1380 | spin_lock(&fp->fi_lock); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1381 | list_del_init(&stp->st_perfile); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 1382 | spin_unlock(&fp->fi_lock); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1383 | list_del(&stp->st_perstateowner); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1384 | return true; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1385 | } |
| 1386 | |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1387 | static void nfs4_free_ol_stateid(struct nfs4_stid *stid) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1388 | { |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1389 | struct nfs4_ol_stateid *stp = openlockstateid(stid); |
J. Bruce Fields | 4665e2b | 2011-09-06 14:50:49 -0400 | [diff] [blame] | 1390 | |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 1391 | put_clnt_odstate(stp->st_clnt_odstate); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1392 | release_all_access(stp); |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 1393 | if (stp->st_stateowner) |
| 1394 | nfs4_put_stateowner(stp->st_stateowner); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1395 | kmem_cache_free(stateid_slab, stid); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1396 | } |
| 1397 | |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 1398 | static void nfs4_free_lock_stateid(struct nfs4_stid *stid) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1399 | { |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 1400 | struct nfs4_ol_stateid *stp = openlockstateid(stid); |
| 1401 | struct nfs4_lockowner *lo = lockowner(stp->st_stateowner); |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 1402 | struct nfsd_file *nf; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1403 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 1404 | nf = find_any_file(stp->st_stid.sc_file); |
| 1405 | if (nf) { |
| 1406 | get_file(nf->nf_file); |
| 1407 | filp_close(nf->nf_file, (fl_owner_t)lo); |
| 1408 | nfsd_file_put(nf); |
| 1409 | } |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 1410 | nfs4_free_ol_stateid(stid); |
| 1411 | } |
| 1412 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1413 | /* |
| 1414 | * Put the persistent reference to an already unhashed generic stateid, while |
| 1415 | * holding the cl_lock. If it's the last reference, then put it onto the |
| 1416 | * reaplist for later destruction. |
| 1417 | */ |
| 1418 | static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp, |
| 1419 | struct list_head *reaplist) |
| 1420 | { |
| 1421 | struct nfs4_stid *s = &stp->st_stid; |
| 1422 | struct nfs4_client *clp = s->sc_client; |
| 1423 | |
| 1424 | lockdep_assert_held(&clp->cl_lock); |
| 1425 | |
| 1426 | WARN_ON_ONCE(!list_empty(&stp->st_locks)); |
| 1427 | |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 1428 | if (!refcount_dec_and_test(&s->sc_count)) { |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1429 | wake_up_all(&close_wq); |
| 1430 | return; |
| 1431 | } |
| 1432 | |
| 1433 | idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id); |
| 1434 | list_add(&stp->st_locks, reaplist); |
| 1435 | } |
| 1436 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1437 | static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp) |
Jeff Layton | 3c1c995 | 2014-07-29 21:34:39 -0400 | [diff] [blame] | 1438 | { |
Chuck Lever | f46c445 | 2016-10-29 18:19:03 -0400 | [diff] [blame] | 1439 | lockdep_assert_held(&stp->st_stid.sc_client->cl_lock); |
Jeff Layton | 3c1c995 | 2014-07-29 21:34:39 -0400 | [diff] [blame] | 1440 | |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 1441 | if (!unhash_ol_stateid(stp)) |
| 1442 | return false; |
Jeff Layton | 3c1c995 | 2014-07-29 21:34:39 -0400 | [diff] [blame] | 1443 | list_del_init(&stp->st_locks); |
Christoph Hellwig | cd61c52 | 2014-08-14 08:44:57 +0200 | [diff] [blame] | 1444 | nfs4_unhash_stid(&stp->st_stid); |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 1445 | return true; |
Jeff Layton | 3c1c995 | 2014-07-29 21:34:39 -0400 | [diff] [blame] | 1446 | } |
| 1447 | |
Jeff Layton | 5adfd88 | 2014-07-29 21:34:31 -0400 | [diff] [blame] | 1448 | static void release_lock_stateid(struct nfs4_ol_stateid *stp) |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 1449 | { |
Chuck Lever | f46c445 | 2016-10-29 18:19:03 -0400 | [diff] [blame] | 1450 | struct nfs4_client *clp = stp->st_stid.sc_client; |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1451 | bool unhashed; |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 1452 | |
Chuck Lever | f46c445 | 2016-10-29 18:19:03 -0400 | [diff] [blame] | 1453 | spin_lock(&clp->cl_lock); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1454 | unhashed = unhash_lock_stateid(stp); |
Chuck Lever | f46c445 | 2016-10-29 18:19:03 -0400 | [diff] [blame] | 1455 | spin_unlock(&clp->cl_lock); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1456 | if (unhashed) |
| 1457 | nfs4_put_stid(&stp->st_stid); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1458 | } |
| 1459 | |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 1460 | static void unhash_lockowner_locked(struct nfs4_lockowner *lo) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1461 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1462 | struct nfs4_client *clp = lo->lo_owner.so_client; |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 1463 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1464 | lockdep_assert_held(&clp->cl_lock); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 1465 | |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 1466 | list_del_init(&lo->lo_owner.so_strhash); |
| 1467 | } |
| 1468 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1469 | /* |
| 1470 | * Free a list of generic stateids that were collected earlier after being |
| 1471 | * fully unhashed. |
| 1472 | */ |
| 1473 | static void |
| 1474 | free_ol_stateid_reaplist(struct list_head *reaplist) |
| 1475 | { |
| 1476 | struct nfs4_ol_stateid *stp; |
Kinglong Mee | fb94d76 | 2014-08-05 21:20:27 +0800 | [diff] [blame] | 1477 | struct nfs4_file *fp; |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1478 | |
| 1479 | might_sleep(); |
| 1480 | |
| 1481 | while (!list_empty(reaplist)) { |
| 1482 | stp = list_first_entry(reaplist, struct nfs4_ol_stateid, |
| 1483 | st_locks); |
| 1484 | list_del(&stp->st_locks); |
Kinglong Mee | fb94d76 | 2014-08-05 21:20:27 +0800 | [diff] [blame] | 1485 | fp = stp->st_stid.sc_file; |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1486 | stp->st_stid.sc_free(&stp->st_stid); |
Kinglong Mee | fb94d76 | 2014-08-05 21:20:27 +0800 | [diff] [blame] | 1487 | if (fp) |
| 1488 | put_nfs4_file(fp); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1489 | } |
| 1490 | } |
| 1491 | |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 1492 | static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp, |
| 1493 | struct list_head *reaplist) |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 1494 | { |
| 1495 | struct nfs4_ol_stateid *stp; |
| 1496 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1497 | lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock); |
| 1498 | |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 1499 | while (!list_empty(&open_stp->st_locks)) { |
| 1500 | stp = list_entry(open_stp->st_locks.next, |
| 1501 | struct nfs4_ol_stateid, st_locks); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1502 | WARN_ON(!unhash_lock_stateid(stp)); |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 1503 | put_ol_stateid_locked(stp, reaplist); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1504 | } |
| 1505 | } |
| 1506 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1507 | static bool unhash_open_stateid(struct nfs4_ol_stateid *stp, |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 1508 | struct list_head *reaplist) |
J. Bruce Fields | 2283963 | 2009-01-11 14:27:17 -0500 | [diff] [blame] | 1509 | { |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1510 | lockdep_assert_held(&stp->st_stid.sc_client->cl_lock); |
| 1511 | |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 1512 | if (!unhash_ol_stateid(stp)) |
| 1513 | return false; |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 1514 | release_open_stateid_locks(stp, reaplist); |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 1515 | return true; |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 1516 | } |
| 1517 | |
| 1518 | static void release_open_stateid(struct nfs4_ol_stateid *stp) |
| 1519 | { |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1520 | LIST_HEAD(reaplist); |
| 1521 | |
| 1522 | spin_lock(&stp->st_stid.sc_client->cl_lock); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1523 | if (unhash_open_stateid(stp, &reaplist)) |
| 1524 | put_ol_stateid_locked(stp, &reaplist); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1525 | spin_unlock(&stp->st_stid.sc_client->cl_lock); |
| 1526 | free_ol_stateid_reaplist(&reaplist); |
J. Bruce Fields | 2283963 | 2009-01-11 14:27:17 -0500 | [diff] [blame] | 1527 | } |
| 1528 | |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1529 | static void unhash_openowner_locked(struct nfs4_openowner *oo) |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 1530 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1531 | struct nfs4_client *clp = oo->oo_owner.so_client; |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1532 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1533 | lockdep_assert_held(&clp->cl_lock); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1534 | |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 1535 | list_del_init(&oo->oo_owner.so_strhash); |
| 1536 | list_del_init(&oo->oo_perclient); |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 1537 | } |
| 1538 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1539 | static void release_last_closed_stateid(struct nfs4_openowner *oo) |
| 1540 | { |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 1541 | struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net, |
| 1542 | nfsd_net_id); |
| 1543 | struct nfs4_ol_stateid *s; |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1544 | |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 1545 | spin_lock(&nn->client_lock); |
| 1546 | s = oo->oo_last_closed_stid; |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1547 | if (s) { |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 1548 | list_del_init(&oo->oo_close_lru); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1549 | oo->oo_last_closed_stid = NULL; |
| 1550 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 1551 | spin_unlock(&nn->client_lock); |
| 1552 | if (s) |
| 1553 | nfs4_put_stid(&s->st_stid); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1554 | } |
| 1555 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1556 | static void release_openowner(struct nfs4_openowner *oo) |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 1557 | { |
| 1558 | struct nfs4_ol_stateid *stp; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1559 | struct nfs4_client *clp = oo->oo_owner.so_client; |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1560 | struct list_head reaplist; |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1561 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1562 | INIT_LIST_HEAD(&reaplist); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1563 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1564 | spin_lock(&clp->cl_lock); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1565 | unhash_openowner_locked(oo); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1566 | while (!list_empty(&oo->oo_owner.so_stateids)) { |
| 1567 | stp = list_first_entry(&oo->oo_owner.so_stateids, |
| 1568 | struct nfs4_ol_stateid, st_perstateowner); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1569 | if (unhash_open_stateid(stp, &reaplist)) |
| 1570 | put_ol_stateid_locked(stp, &reaplist); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1571 | } |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1572 | spin_unlock(&clp->cl_lock); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1573 | free_ol_stateid_reaplist(&reaplist); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1574 | release_last_closed_stateid(oo); |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 1575 | nfs4_put_stateowner(&oo->oo_owner); |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 1576 | } |
| 1577 | |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1578 | static inline int |
| 1579 | hash_sessionid(struct nfs4_sessionid *sessionid) |
| 1580 | { |
| 1581 | struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid; |
| 1582 | |
| 1583 | return sid->sequence % SESSION_HASH_SIZE; |
| 1584 | } |
| 1585 | |
Mark Salter | 135dd00 | 2015-04-06 09:46:00 -0400 | [diff] [blame] | 1586 | #ifdef CONFIG_SUNRPC_DEBUG |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1587 | static inline void |
| 1588 | dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) |
| 1589 | { |
| 1590 | u32 *ptr = (u32 *)(&sessionid->data[0]); |
| 1591 | dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]); |
| 1592 | } |
Trond Myklebust | 8f199b8 | 2012-03-20 15:11:17 -0400 | [diff] [blame] | 1593 | #else |
| 1594 | static inline void |
| 1595 | dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) |
| 1596 | { |
| 1597 | } |
| 1598 | #endif |
| 1599 | |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 1600 | /* |
| 1601 | * Bump the seqid on cstate->replay_owner, and clear replay_owner if it |
| 1602 | * won't be used for replay. |
| 1603 | */ |
| 1604 | void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr) |
| 1605 | { |
| 1606 | struct nfs4_stateowner *so = cstate->replay_owner; |
| 1607 | |
| 1608 | if (nfserr == nfserr_replay_me) |
| 1609 | return; |
| 1610 | |
| 1611 | if (!seqid_mutating_err(ntohl(nfserr))) { |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 1612 | nfsd4_cstate_clear_replay(cstate); |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 1613 | return; |
| 1614 | } |
| 1615 | if (!so) |
| 1616 | return; |
| 1617 | if (so->so_is_open_owner) |
| 1618 | release_last_closed_stateid(openowner(so)); |
| 1619 | so->so_seqid++; |
| 1620 | return; |
| 1621 | } |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1622 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1623 | static void |
| 1624 | gen_sessionid(struct nfsd4_session *ses) |
| 1625 | { |
| 1626 | struct nfs4_client *clp = ses->se_client; |
| 1627 | struct nfsd4_sessionid *sid; |
| 1628 | |
| 1629 | sid = (struct nfsd4_sessionid *)ses->se_sessionid.data; |
| 1630 | sid->clientid = clp->cl_clientid; |
| 1631 | sid->sequence = current_sessionid++; |
| 1632 | sid->reserved = 0; |
| 1633 | } |
| 1634 | |
| 1635 | /* |
Andy Adamson | a649637c7 | 2009-08-28 08:45:01 -0400 | [diff] [blame] | 1636 | * The protocol defines ca_maxresponssize_cached to include the size of |
| 1637 | * the rpc header, but all we need to cache is the data starting after |
| 1638 | * the end of the initial SEQUENCE operation--the rest we regenerate |
| 1639 | * each time. Therefore we can advertise a ca_maxresponssize_cached |
| 1640 | * value that is the number of bytes in our cache plus a few additional |
| 1641 | * bytes. In order to stay on the safe side, and not promise more than |
| 1642 | * we can cache, those additional bytes must be the minimum possible: 24 |
| 1643 | * bytes of rpc header (xid through accept state, with AUTH_NULL |
| 1644 | * verifier), 12 for the compound header (with zero-length tag), and 44 |
| 1645 | * for the SEQUENCE op response: |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1646 | */ |
Andy Adamson | a649637c7 | 2009-08-28 08:45:01 -0400 | [diff] [blame] | 1647 | #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44) |
| 1648 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1649 | static void |
| 1650 | free_session_slots(struct nfsd4_session *ses) |
| 1651 | { |
| 1652 | int i; |
| 1653 | |
J. Bruce Fields | 53da6a5 | 2017-10-17 20:38:49 -0400 | [diff] [blame] | 1654 | for (i = 0; i < ses->se_fchannel.maxreqs; i++) { |
| 1655 | free_svc_cred(&ses->se_slots[i]->sl_cred); |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1656 | kfree(ses->se_slots[i]); |
J. Bruce Fields | 53da6a5 | 2017-10-17 20:38:49 -0400 | [diff] [blame] | 1657 | } |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1658 | } |
| 1659 | |
J. Bruce Fields | efe0cb6 | 2009-10-24 20:52:16 -0400 | [diff] [blame] | 1660 | /* |
| 1661 | * We don't actually need to cache the rpc and session headers, so we |
| 1662 | * can allocate a little less for each slot: |
| 1663 | */ |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1664 | static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca) |
J. Bruce Fields | efe0cb6 | 2009-10-24 20:52:16 -0400 | [diff] [blame] | 1665 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1666 | u32 size; |
J. Bruce Fields | efe0cb6 | 2009-10-24 20:52:16 -0400 | [diff] [blame] | 1667 | |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1668 | if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ) |
| 1669 | size = 0; |
| 1670 | else |
| 1671 | size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ; |
| 1672 | return size + sizeof(struct nfsd4_slot); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1673 | } |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1674 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1675 | /* |
| 1676 | * XXX: If we run out of reserved DRC memory we could (up to a point) |
| 1677 | * re-negotiate active sessions and reduce their slot usage to make |
Justin P. Mattock | 42b2aa8 | 2011-11-28 20:31:00 -0800 | [diff] [blame] | 1678 | * room for new connections. For now we just fail the create session. |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1679 | */ |
NeilBrown | 2030ca5 | 2019-09-20 16:36:45 +1000 | [diff] [blame] | 1680 | static u32 nfsd4_get_drc_mem(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1681 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1682 | u32 slotsize = slot_bytes(ca); |
| 1683 | u32 num = ca->maxreqs; |
J. Bruce Fields | c54f24e | 2019-02-21 10:47:00 -0500 | [diff] [blame] | 1684 | unsigned long avail, total_avail; |
NeilBrown | 2030ca5 | 2019-09-20 16:36:45 +1000 | [diff] [blame] | 1685 | unsigned int scale_factor; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1686 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1687 | spin_lock(&nfsd_drc_lock); |
NeilBrown | 7f49fd5d | 2019-09-20 16:33:16 +1000 | [diff] [blame] | 1688 | if (nfsd_drc_max_mem > nfsd_drc_mem_used) |
| 1689 | total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used; |
| 1690 | else |
| 1691 | /* We have handed out more space than we chose in |
| 1692 | * set_max_drc() to allow. That isn't really a |
| 1693 | * problem as long as that doesn't make us think we |
| 1694 | * have lots more due to integer overflow. |
| 1695 | */ |
| 1696 | total_avail = 0; |
J. Bruce Fields | c54f24e | 2019-02-21 10:47:00 -0500 | [diff] [blame] | 1697 | avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail); |
J. Bruce Fields | de766e5 | 2017-09-19 19:25:41 -0400 | [diff] [blame] | 1698 | /* |
NeilBrown | 2030ca5 | 2019-09-20 16:36:45 +1000 | [diff] [blame] | 1699 | * Never use more than a fraction of the remaining memory, |
NeilBrown | 7f49fd5d | 2019-09-20 16:33:16 +1000 | [diff] [blame] | 1700 | * unless it's the only way to give this client a slot. |
NeilBrown | 2030ca5 | 2019-09-20 16:36:45 +1000 | [diff] [blame] | 1701 | * The chosen fraction is either 1/8 or 1/number of threads, |
| 1702 | * whichever is smaller. This ensures there are adequate |
| 1703 | * slots to support multiple clients per thread. |
NeilBrown | 7f49fd5d | 2019-09-20 16:33:16 +1000 | [diff] [blame] | 1704 | * Give the client one slot even if that would require |
| 1705 | * over-allocation--it is better than failure. |
J. Bruce Fields | de766e5 | 2017-09-19 19:25:41 -0400 | [diff] [blame] | 1706 | */ |
NeilBrown | 2030ca5 | 2019-09-20 16:36:45 +1000 | [diff] [blame] | 1707 | scale_factor = max_t(unsigned int, 8, nn->nfsd_serv->sv_nrthreads); |
| 1708 | |
| 1709 | avail = clamp_t(unsigned long, avail, slotsize, |
| 1710 | total_avail/scale_factor); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1711 | num = min_t(int, num, avail / slotsize); |
NeilBrown | 7f49fd5d | 2019-09-20 16:33:16 +1000 | [diff] [blame] | 1712 | num = max_t(int, num, 1); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1713 | nfsd_drc_mem_used += num * slotsize; |
| 1714 | spin_unlock(&nfsd_drc_lock); |
| 1715 | |
| 1716 | return num; |
| 1717 | } |
| 1718 | |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1719 | static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1720 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1721 | int slotsize = slot_bytes(ca); |
| 1722 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1723 | spin_lock(&nfsd_drc_lock); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1724 | nfsd_drc_mem_used -= slotsize * ca->maxreqs; |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1725 | spin_unlock(&nfsd_drc_lock); |
| 1726 | } |
| 1727 | |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1728 | static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs, |
| 1729 | struct nfsd4_channel_attrs *battrs) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1730 | { |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1731 | int numslots = fattrs->maxreqs; |
| 1732 | int slotsize = slot_bytes(fattrs); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1733 | struct nfsd4_session *new; |
| 1734 | int mem, i; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1735 | |
J. Bruce Fields | c23753d | 2010-09-27 16:22:30 -0400 | [diff] [blame] | 1736 | BUILD_BUG_ON(NFSD_MAX_SLOTS_PER_SESSION * sizeof(struct nfsd4_slot *) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1737 | + sizeof(struct nfsd4_session) > PAGE_SIZE); |
| 1738 | mem = numslots * sizeof(struct nfsd4_slot *); |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1739 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1740 | new = kzalloc(sizeof(*new) + mem, GFP_KERNEL); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1741 | if (!new) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1742 | return NULL; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1743 | /* allocate each struct nfsd4_slot and data cache in one piece */ |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1744 | for (i = 0; i < numslots; i++) { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1745 | new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1746 | if (!new->se_slots[i]) |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1747 | goto out_free; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1748 | } |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1749 | |
| 1750 | memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs)); |
| 1751 | memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs)); |
| 1752 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1753 | return new; |
| 1754 | out_free: |
| 1755 | while (i--) |
| 1756 | kfree(new->se_slots[i]); |
| 1757 | kfree(new); |
| 1758 | return NULL; |
| 1759 | } |
| 1760 | |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1761 | static void free_conn(struct nfsd4_conn *c) |
| 1762 | { |
| 1763 | svc_xprt_put(c->cn_xprt); |
| 1764 | kfree(c); |
| 1765 | } |
| 1766 | |
| 1767 | static void nfsd4_conn_lost(struct svc_xpt_user *u) |
| 1768 | { |
| 1769 | struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user); |
| 1770 | struct nfs4_client *clp = c->cn_session->se_client; |
| 1771 | |
Chuck Lever | 806d65b | 2021-05-14 15:56:43 -0400 | [diff] [blame] | 1772 | trace_nfsd_cb_lost(clp); |
| 1773 | |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1774 | spin_lock(&clp->cl_lock); |
| 1775 | if (!list_empty(&c->cn_persession)) { |
| 1776 | list_del(&c->cn_persession); |
| 1777 | free_conn(c); |
| 1778 | } |
J. Bruce Fields | eea4980 | 2010-11-18 08:34:12 -0500 | [diff] [blame] | 1779 | nfsd4_probe_callback(clp); |
J. Bruce Fields | 2e4b723 | 2013-03-08 09:30:43 -0500 | [diff] [blame] | 1780 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1781 | } |
| 1782 | |
J. Bruce Fields | d29c374 | 2010-06-15 17:34:11 -0400 | [diff] [blame] | 1783 | static struct nfsd4_conn *alloc_conn(struct svc_rqst *rqstp, u32 flags) |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1784 | { |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1785 | struct nfsd4_conn *conn; |
| 1786 | |
| 1787 | conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL); |
| 1788 | if (!conn) |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1789 | return NULL; |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1790 | svc_xprt_get(rqstp->rq_xprt); |
| 1791 | conn->cn_xprt = rqstp->rq_xprt; |
J. Bruce Fields | d29c374 | 2010-06-15 17:34:11 -0400 | [diff] [blame] | 1792 | conn->cn_flags = flags; |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1793 | INIT_LIST_HEAD(&conn->cn_xpt_user.list); |
| 1794 | return conn; |
| 1795 | } |
| 1796 | |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 1797 | static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) |
| 1798 | { |
| 1799 | conn->cn_session = ses; |
| 1800 | list_add(&conn->cn_persession, &ses->se_conns); |
| 1801 | } |
| 1802 | |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1803 | static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) |
| 1804 | { |
| 1805 | struct nfs4_client *clp = ses->se_client; |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1806 | |
| 1807 | spin_lock(&clp->cl_lock); |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 1808 | __nfsd4_hash_conn(conn, ses); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1809 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1810 | } |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1811 | |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1812 | static int nfsd4_register_conn(struct nfsd4_conn *conn) |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1813 | { |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1814 | conn->cn_xpt_user.callback = nfsd4_conn_lost; |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1815 | return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user); |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1816 | } |
| 1817 | |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1818 | static void nfsd4_init_conn(struct svc_rqst *rqstp, struct nfsd4_conn *conn, struct nfsd4_session *ses) |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1819 | { |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1820 | int ret; |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1821 | |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1822 | nfsd4_hash_conn(conn, ses); |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1823 | ret = nfsd4_register_conn(conn); |
| 1824 | if (ret) |
| 1825 | /* oops; xprt is already down: */ |
| 1826 | nfsd4_conn_lost(&conn->cn_xpt_user); |
J. Bruce Fields | 57a3714 | 2014-07-18 15:06:47 -0400 | [diff] [blame] | 1827 | /* We may have gained or lost a callback channel: */ |
| 1828 | nfsd4_probe_callback_sync(ses->se_client); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1829 | } |
| 1830 | |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1831 | static struct nfsd4_conn *alloc_conn_from_crses(struct svc_rqst *rqstp, struct nfsd4_create_session *cses) |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 1832 | { |
| 1833 | u32 dir = NFS4_CDFC4_FORE; |
| 1834 | |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1835 | if (cses->flags & SESSION4_BACK_CHAN) |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 1836 | dir |= NFS4_CDFC4_BACK; |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1837 | return alloc_conn(rqstp, dir); |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 1838 | } |
| 1839 | |
| 1840 | /* must be called under client_lock */ |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1841 | static void nfsd4_del_conns(struct nfsd4_session *s) |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1842 | { |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1843 | struct nfs4_client *clp = s->se_client; |
| 1844 | struct nfsd4_conn *c; |
| 1845 | |
| 1846 | spin_lock(&clp->cl_lock); |
| 1847 | while (!list_empty(&s->se_conns)) { |
| 1848 | c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession); |
| 1849 | list_del_init(&c->cn_persession); |
| 1850 | spin_unlock(&clp->cl_lock); |
| 1851 | |
| 1852 | unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user); |
| 1853 | free_conn(c); |
| 1854 | |
| 1855 | spin_lock(&clp->cl_lock); |
| 1856 | } |
| 1857 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1858 | } |
| 1859 | |
J. Bruce Fields | 1377b69 | 2012-09-11 21:42:40 -0400 | [diff] [blame] | 1860 | static void __free_session(struct nfsd4_session *ses) |
| 1861 | { |
J. Bruce Fields | 1377b69 | 2012-09-11 21:42:40 -0400 | [diff] [blame] | 1862 | free_session_slots(ses); |
| 1863 | kfree(ses); |
| 1864 | } |
| 1865 | |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 1866 | static void free_session(struct nfsd4_session *ses) |
Benny Halevy | 508dc6e | 2012-02-23 17:40:52 -0800 | [diff] [blame] | 1867 | { |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1868 | nfsd4_del_conns(ses); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1869 | nfsd4_put_drc_mem(&ses->se_fchannel); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1870 | __free_session(ses); |
J. Bruce Fields | a827bcb | 2012-09-12 09:51:34 -0400 | [diff] [blame] | 1871 | } |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1872 | |
Fengguang Wu | 135ae82 | 2012-11-10 07:20:25 -0500 | [diff] [blame] | 1873 | static void init_session(struct svc_rqst *rqstp, struct nfsd4_session *new, struct nfs4_client *clp, struct nfsd4_create_session *cses) |
J. Bruce Fields | a827bcb | 2012-09-12 09:51:34 -0400 | [diff] [blame] | 1874 | { |
J. Bruce Fields | a827bcb | 2012-09-12 09:51:34 -0400 | [diff] [blame] | 1875 | int idx; |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1876 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
J. Bruce Fields | a827bcb | 2012-09-12 09:51:34 -0400 | [diff] [blame] | 1877 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1878 | new->se_client = clp; |
| 1879 | gen_sessionid(new); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1880 | |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1881 | INIT_LIST_HEAD(&new->se_conns); |
| 1882 | |
J. Bruce Fields | ac7c46f2 | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 1883 | new->se_cb_seq_nr = 1; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1884 | new->se_flags = cses->flags; |
J. Bruce Fields | 8b5ce5c | 2010-10-19 17:31:50 -0400 | [diff] [blame] | 1885 | new->se_cb_prog = cses->callback_prog; |
J. Bruce Fields | c6bb3ca | 2012-11-01 16:31:02 -0400 | [diff] [blame] | 1886 | new->se_cb_sec = cses->cb_sec; |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 1887 | atomic_set(&new->se_ref, 0); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1888 | idx = hash_sessionid(&new->se_sessionid); |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1889 | list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1890 | spin_lock(&clp->cl_lock); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1891 | list_add(&new->se_perclnt, &clp->cl_sessions); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1892 | spin_unlock(&clp->cl_lock); |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1893 | |
Chuck Lever | b0d2e42 | 2014-08-22 15:10:59 -0400 | [diff] [blame] | 1894 | { |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1895 | struct sockaddr *sa = svc_addr(rqstp); |
J. Bruce Fields | dcbeaa6 | 2010-06-15 17:25:45 -0400 | [diff] [blame] | 1896 | /* |
| 1897 | * This is a little silly; with sessions there's no real |
| 1898 | * use for the callback address. Use the peer address |
| 1899 | * as a reasonable default for now, but consider fixing |
| 1900 | * the rpc client not to require an address in the |
| 1901 | * future: |
| 1902 | */ |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1903 | rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa); |
| 1904 | clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa); |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1905 | } |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1906 | } |
| 1907 | |
Benny Halevy | 9089f1b | 2010-05-12 00:12:26 +0300 | [diff] [blame] | 1908 | /* caller must hold client_lock */ |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1909 | static struct nfsd4_session * |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 1910 | __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net) |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1911 | { |
| 1912 | struct nfsd4_session *elem; |
| 1913 | int idx; |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1914 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1915 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 1916 | lockdep_assert_held(&nn->client_lock); |
| 1917 | |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1918 | dump_sessionid(__func__, sessionid); |
| 1919 | idx = hash_sessionid(sessionid); |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1920 | /* Search in the appropriate list */ |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1921 | list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) { |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1922 | if (!memcmp(elem->se_sessionid.data, sessionid->data, |
| 1923 | NFS4_MAX_SESSIONID_LEN)) { |
| 1924 | return elem; |
| 1925 | } |
| 1926 | } |
| 1927 | |
| 1928 | dprintk("%s: session not found\n", __func__); |
| 1929 | return NULL; |
| 1930 | } |
| 1931 | |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 1932 | static struct nfsd4_session * |
| 1933 | find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net, |
| 1934 | __be32 *ret) |
| 1935 | { |
| 1936 | struct nfsd4_session *session; |
| 1937 | __be32 status = nfserr_badsession; |
| 1938 | |
| 1939 | session = __find_in_sessionid_hashtbl(sessionid, net); |
| 1940 | if (!session) |
| 1941 | goto out; |
| 1942 | status = nfsd4_get_session_locked(session); |
| 1943 | if (status) |
| 1944 | session = NULL; |
| 1945 | out: |
| 1946 | *ret = status; |
| 1947 | return session; |
| 1948 | } |
| 1949 | |
Benny Halevy | 9089f1b | 2010-05-12 00:12:26 +0300 | [diff] [blame] | 1950 | /* caller must hold client_lock */ |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1951 | static void |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1952 | unhash_session(struct nfsd4_session *ses) |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1953 | { |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 1954 | struct nfs4_client *clp = ses->se_client; |
| 1955 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 1956 | |
| 1957 | lockdep_assert_held(&nn->client_lock); |
| 1958 | |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1959 | list_del(&ses->se_hash); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1960 | spin_lock(&ses->se_client->cl_lock); |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1961 | list_del(&ses->se_perclnt); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1962 | spin_unlock(&ses->se_client->cl_lock); |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1963 | } |
| 1964 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1965 | /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */ |
| 1966 | static int |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 1967 | STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1968 | { |
J. Bruce Fields | bbc7f33 | 2015-01-20 11:51:26 -0500 | [diff] [blame] | 1969 | /* |
| 1970 | * We're assuming the clid was not given out from a boot |
| 1971 | * precisely 2^32 (about 136 years) before this one. That seems |
| 1972 | * a safe assumption: |
| 1973 | */ |
| 1974 | if (clid->cl_boot == (u32)nn->boot_time) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1975 | return 0; |
Chuck Lever | dd5e3fb | 2020-04-05 11:42:29 -0400 | [diff] [blame] | 1976 | trace_nfsd_clid_stale(clid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1977 | return 1; |
| 1978 | } |
| 1979 | |
| 1980 | /* |
| 1981 | * XXX Should we use a slab cache ? |
| 1982 | * This type of memory management is somewhat inefficient, but we use it |
| 1983 | * anyway since SETCLIENTID is not a common operation. |
| 1984 | */ |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1985 | static struct nfs4_client *alloc_client(struct xdr_netobj name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1986 | { |
| 1987 | struct nfs4_client *clp; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1988 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | |
Jeff Layton | 9258a2d | 2018-03-16 09:47:22 -0400 | [diff] [blame] | 1990 | clp = kmem_cache_zalloc(client_slab, GFP_KERNEL); |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1991 | if (clp == NULL) |
| 1992 | return NULL; |
J. Bruce Fields | 6f4859b | 2019-06-19 14:30:33 -0400 | [diff] [blame] | 1993 | xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1994 | if (clp->cl_name.data == NULL) |
| 1995 | goto err_no_name; |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 1996 | clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE, |
| 1997 | sizeof(struct list_head), |
| 1998 | GFP_KERNEL); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1999 | if (!clp->cl_ownerstr_hashtbl) |
| 2000 | goto err_no_hashtbl; |
| 2001 | for (i = 0; i < OWNER_HASH_SIZE; i++) |
| 2002 | INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]); |
Trond Myklebust | 5694c93 | 2014-04-18 14:43:56 -0400 | [diff] [blame] | 2003 | INIT_LIST_HEAD(&clp->cl_sessions); |
| 2004 | idr_init(&clp->cl_stateids); |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 2005 | atomic_set(&clp->cl_rpc_users, 0); |
Trond Myklebust | 5694c93 | 2014-04-18 14:43:56 -0400 | [diff] [blame] | 2006 | clp->cl_cb_state = NFSD4_CB_UNKNOWN; |
| 2007 | INIT_LIST_HEAD(&clp->cl_idhash); |
| 2008 | INIT_LIST_HEAD(&clp->cl_openowners); |
| 2009 | INIT_LIST_HEAD(&clp->cl_delegations); |
| 2010 | INIT_LIST_HEAD(&clp->cl_lru); |
Trond Myklebust | 5694c93 | 2014-04-18 14:43:56 -0400 | [diff] [blame] | 2011 | INIT_LIST_HEAD(&clp->cl_revoked); |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 2012 | #ifdef CONFIG_NFSD_PNFS |
| 2013 | INIT_LIST_HEAD(&clp->cl_lo_states); |
| 2014 | #endif |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 2015 | INIT_LIST_HEAD(&clp->async_copies); |
| 2016 | spin_lock_init(&clp->async_lock); |
Trond Myklebust | 5694c93 | 2014-04-18 14:43:56 -0400 | [diff] [blame] | 2017 | spin_lock_init(&clp->cl_lock); |
| 2018 | rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2019 | return clp; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 2020 | err_no_hashtbl: |
| 2021 | kfree(clp->cl_name.data); |
| 2022 | err_no_name: |
Jeff Layton | 9258a2d | 2018-03-16 09:47:22 -0400 | [diff] [blame] | 2023 | kmem_cache_free(client_slab, clp); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 2024 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2025 | } |
| 2026 | |
J. Bruce Fields | 59f8e91 | 2019-03-20 20:03:02 -0400 | [diff] [blame] | 2027 | static void __free_client(struct kref *k) |
| 2028 | { |
J. Bruce Fields | e8a79fb | 2019-03-22 11:11:06 -0400 | [diff] [blame] | 2029 | struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref); |
| 2030 | struct nfs4_client *clp = container_of(c, struct nfs4_client, cl_nfsdfs); |
J. Bruce Fields | 59f8e91 | 2019-03-20 20:03:02 -0400 | [diff] [blame] | 2031 | |
| 2032 | free_svc_cred(&clp->cl_cred); |
| 2033 | kfree(clp->cl_ownerstr_hashtbl); |
| 2034 | kfree(clp->cl_name.data); |
J. Bruce Fields | 7912344 | 2019-06-05 12:42:05 -0400 | [diff] [blame] | 2035 | kfree(clp->cl_nii_domain.data); |
| 2036 | kfree(clp->cl_nii_name.data); |
J. Bruce Fields | 59f8e91 | 2019-03-20 20:03:02 -0400 | [diff] [blame] | 2037 | idr_destroy(&clp->cl_stateids); |
J. Bruce Fields | 59f8e91 | 2019-03-20 20:03:02 -0400 | [diff] [blame] | 2038 | kmem_cache_free(client_slab, clp); |
| 2039 | } |
| 2040 | |
YueHaibing | 297e57a | 2019-07-08 15:29:33 +0800 | [diff] [blame] | 2041 | static void drop_client(struct nfs4_client *clp) |
J. Bruce Fields | 59f8e91 | 2019-03-20 20:03:02 -0400 | [diff] [blame] | 2042 | { |
J. Bruce Fields | e8a79fb | 2019-03-22 11:11:06 -0400 | [diff] [blame] | 2043 | kref_put(&clp->cl_nfsdfs.cl_ref, __free_client); |
J. Bruce Fields | 59f8e91 | 2019-03-20 20:03:02 -0400 | [diff] [blame] | 2044 | } |
| 2045 | |
Trond Myklebust | 4dd86e15 | 2014-04-18 14:43:58 -0400 | [diff] [blame] | 2046 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2047 | free_client(struct nfs4_client *clp) |
| 2048 | { |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 2049 | while (!list_empty(&clp->cl_sessions)) { |
| 2050 | struct nfsd4_session *ses; |
| 2051 | ses = list_entry(clp->cl_sessions.next, struct nfsd4_session, |
| 2052 | se_perclnt); |
| 2053 | list_del(&ses->se_perclnt); |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2054 | WARN_ON_ONCE(atomic_read(&ses->se_ref)); |
| 2055 | free_session(ses); |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 2056 | } |
Trond Myklebust | 4cb57e3 | 2014-04-18 14:43:57 -0400 | [diff] [blame] | 2057 | rpc_destroy_wait_queue(&clp->cl_cb_waitq); |
J. Bruce Fields | 89c905b | 2019-06-19 12:43:11 -0400 | [diff] [blame] | 2058 | if (clp->cl_nfsd_dentry) { |
J. Bruce Fields | e8a79fb | 2019-03-22 11:11:06 -0400 | [diff] [blame] | 2059 | nfsd_client_rmdir(clp->cl_nfsd_dentry); |
J. Bruce Fields | 89c905b | 2019-06-19 12:43:11 -0400 | [diff] [blame] | 2060 | clp->cl_nfsd_dentry = NULL; |
| 2061 | wake_up_all(&expiry_wq); |
| 2062 | } |
J. Bruce Fields | 59f8e91 | 2019-03-20 20:03:02 -0400 | [diff] [blame] | 2063 | drop_client(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2064 | } |
| 2065 | |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 2066 | /* must be called under the client_lock */ |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 2067 | static void |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 2068 | unhash_client_locked(struct nfs4_client *clp) |
| 2069 | { |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 2070 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 2071 | struct nfsd4_session *ses; |
| 2072 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2073 | lockdep_assert_held(&nn->client_lock); |
| 2074 | |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 2075 | /* Mark the client as expired! */ |
| 2076 | clp->cl_time = 0; |
| 2077 | /* Make it invisible */ |
| 2078 | if (!list_empty(&clp->cl_idhash)) { |
| 2079 | list_del_init(&clp->cl_idhash); |
| 2080 | if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags)) |
| 2081 | rb_erase(&clp->cl_namenode, &nn->conf_name_tree); |
| 2082 | else |
| 2083 | rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); |
| 2084 | } |
| 2085 | list_del_init(&clp->cl_lru); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 2086 | spin_lock(&clp->cl_lock); |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 2087 | list_for_each_entry(ses, &clp->cl_sessions, se_perclnt) |
| 2088 | list_del_init(&ses->se_hash); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 2089 | spin_unlock(&clp->cl_lock); |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 2090 | } |
| 2091 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2092 | static void |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 2093 | unhash_client(struct nfs4_client *clp) |
| 2094 | { |
| 2095 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 2096 | |
| 2097 | spin_lock(&nn->client_lock); |
| 2098 | unhash_client_locked(clp); |
| 2099 | spin_unlock(&nn->client_lock); |
| 2100 | } |
| 2101 | |
Jeff Layton | 97403d9 | 2014-07-30 08:27:12 -0400 | [diff] [blame] | 2102 | static __be32 mark_client_expired_locked(struct nfs4_client *clp) |
| 2103 | { |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 2104 | if (atomic_read(&clp->cl_rpc_users)) |
Jeff Layton | 97403d9 | 2014-07-30 08:27:12 -0400 | [diff] [blame] | 2105 | return nfserr_jukebox; |
| 2106 | unhash_client_locked(clp); |
| 2107 | return nfs_ok; |
| 2108 | } |
| 2109 | |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 2110 | static void |
| 2111 | __destroy_client(struct nfs4_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2112 | { |
Jeff Layton | 68ef3bc | 2018-03-16 11:32:02 -0400 | [diff] [blame] | 2113 | int i; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2114 | struct nfs4_openowner *oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2115 | struct nfs4_delegation *dp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2116 | struct list_head reaplist; |
| 2117 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2118 | INIT_LIST_HEAD(&reaplist); |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 2119 | spin_lock(&state_lock); |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 2120 | while (!list_empty(&clp->cl_delegations)) { |
| 2121 | dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt); |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 2122 | WARN_ON(!unhash_delegation_locked(dp)); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 2123 | list_add(&dp->dl_recall_lru, &reaplist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2124 | } |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 2125 | spin_unlock(&state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2126 | while (!list_empty(&reaplist)) { |
| 2127 | dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 2128 | list_del_init(&dp->dl_recall_lru); |
J. Bruce Fields | 0af6e69 | 2018-02-21 15:11:03 -0500 | [diff] [blame] | 2129 | destroy_unhashed_deleg(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2130 | } |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 2131 | while (!list_empty(&clp->cl_revoked)) { |
Andrew Elble | c876486 | 2015-02-25 17:46:27 -0500 | [diff] [blame] | 2132 | dp = list_entry(clp->cl_revoked.next, struct nfs4_delegation, dl_recall_lru); |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 2133 | list_del_init(&dp->dl_recall_lru); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 2134 | nfs4_put_stid(&dp->dl_stid); |
Benny Halevy | 956c4fe | 2013-10-29 11:39:12 +0200 | [diff] [blame] | 2135 | } |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 2136 | while (!list_empty(&clp->cl_openowners)) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2137 | oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient); |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 2138 | nfs4_get_stateowner(&oo->oo_owner); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2139 | release_openowner(oo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2140 | } |
Jeff Layton | 68ef3bc | 2018-03-16 11:32:02 -0400 | [diff] [blame] | 2141 | for (i = 0; i < OWNER_HASH_SIZE; i++) { |
| 2142 | struct nfs4_stateowner *so, *tmp; |
| 2143 | |
| 2144 | list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i], |
| 2145 | so_strhash) { |
| 2146 | /* Should be no openowners at this point */ |
| 2147 | WARN_ON_ONCE(so->so_is_open_owner); |
| 2148 | remove_blocked_locks(lockowner(so)); |
| 2149 | } |
| 2150 | } |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 2151 | nfsd4_return_all_client_layouts(clp); |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 2152 | nfsd4_shutdown_copy(clp); |
J. Bruce Fields | 6ff8da0 | 2010-06-04 20:04:45 -0400 | [diff] [blame] | 2153 | nfsd4_shutdown_callback(clp); |
J. Bruce Fields | 2bf2387 | 2010-03-08 12:37:27 -0500 | [diff] [blame] | 2154 | if (clp->cl_cb_conn.cb_xprt) |
| 2155 | svc_xprt_put(clp->cl_cb_conn.cb_xprt); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2156 | free_client(clp); |
J. Bruce Fields | 89c905b | 2019-06-19 12:43:11 -0400 | [diff] [blame] | 2157 | wake_up_all(&expiry_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2158 | } |
| 2159 | |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 2160 | static void |
| 2161 | destroy_client(struct nfs4_client *clp) |
| 2162 | { |
| 2163 | unhash_client(clp); |
| 2164 | __destroy_client(clp); |
| 2165 | } |
| 2166 | |
Scott Mayhew | 362063a | 2019-03-26 18:06:28 -0400 | [diff] [blame] | 2167 | static void inc_reclaim_complete(struct nfs4_client *clp) |
| 2168 | { |
| 2169 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 2170 | |
| 2171 | if (!nn->track_reclaim_completes) |
| 2172 | return; |
| 2173 | if (!nfsd4_find_reclaim_client(clp->cl_name, nn)) |
| 2174 | return; |
| 2175 | if (atomic_inc_return(&nn->nr_reclaim_complete) == |
| 2176 | nn->reclaim_str_hashtbl_size) { |
| 2177 | printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n", |
| 2178 | clp->net->ns.inum); |
| 2179 | nfsd4_end_grace(nn); |
| 2180 | } |
| 2181 | } |
| 2182 | |
J. Bruce Fields | 0d22f68 | 2012-09-26 11:36:16 -0400 | [diff] [blame] | 2183 | static void expire_client(struct nfs4_client *clp) |
| 2184 | { |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 2185 | unhash_client(clp); |
J. Bruce Fields | 0d22f68 | 2012-09-26 11:36:16 -0400 | [diff] [blame] | 2186 | nfsd4_client_record_remove(clp); |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 2187 | __destroy_client(clp); |
J. Bruce Fields | 0d22f68 | 2012-09-26 11:36:16 -0400 | [diff] [blame] | 2188 | } |
| 2189 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 2190 | static void copy_verf(struct nfs4_client *target, nfs4_verifier *source) |
| 2191 | { |
| 2192 | memcpy(target->cl_verifier.data, source->data, |
| 2193 | sizeof(target->cl_verifier.data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2194 | } |
| 2195 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 2196 | static void copy_clid(struct nfs4_client *target, struct nfs4_client *source) |
| 2197 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2198 | target->cl_clientid.cl_boot = source->cl_clientid.cl_boot; |
| 2199 | target->cl_clientid.cl_id = source->cl_clientid.cl_id; |
| 2200 | } |
| 2201 | |
J. Bruce Fields | 5004385 | 2015-11-20 15:58:37 -0500 | [diff] [blame] | 2202 | static int copy_cred(struct svc_cred *target, struct svc_cred *source) |
| 2203 | { |
NeilBrown | 2f10fdc | 2017-03-23 16:57:36 +0800 | [diff] [blame] | 2204 | target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL); |
| 2205 | target->cr_raw_principal = kstrdup(source->cr_raw_principal, |
| 2206 | GFP_KERNEL); |
Chuck Lever | 9abdda5 | 2018-08-16 12:05:59 -0400 | [diff] [blame] | 2207 | target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL); |
| 2208 | if ((source->cr_principal && !target->cr_principal) || |
| 2209 | (source->cr_raw_principal && !target->cr_raw_principal) || |
| 2210 | (source->cr_targ_princ && !target->cr_targ_princ)) |
NeilBrown | 2f10fdc | 2017-03-23 16:57:36 +0800 | [diff] [blame] | 2211 | return -ENOMEM; |
J. Bruce Fields | 5004385 | 2015-11-20 15:58:37 -0500 | [diff] [blame] | 2212 | |
J. Bruce Fields | d5497fc | 2012-05-14 22:06:49 -0400 | [diff] [blame] | 2213 | target->cr_flavor = source->cr_flavor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2214 | target->cr_uid = source->cr_uid; |
| 2215 | target->cr_gid = source->cr_gid; |
| 2216 | target->cr_group_info = source->cr_group_info; |
| 2217 | get_group_info(target->cr_group_info); |
J. Bruce Fields | 0dc1531 | 2013-05-14 16:07:13 -0400 | [diff] [blame] | 2218 | target->cr_gss_mech = source->cr_gss_mech; |
| 2219 | if (source->cr_gss_mech) |
| 2220 | gss_mech_get(source->cr_gss_mech); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 2221 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2222 | } |
| 2223 | |
Rasmus Villemoes | ef17af2 | 2014-12-05 16:40:07 +0100 | [diff] [blame] | 2224 | static int |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2225 | compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2) |
| 2226 | { |
Rasmus Villemoes | ef17af2 | 2014-12-05 16:40:07 +0100 | [diff] [blame] | 2227 | if (o1->len < o2->len) |
| 2228 | return -1; |
| 2229 | if (o1->len > o2->len) |
| 2230 | return 1; |
| 2231 | return memcmp(o1->data, o2->data, o1->len); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2232 | } |
| 2233 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2234 | static int |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 2235 | same_verf(nfs4_verifier *v1, nfs4_verifier *v2) |
| 2236 | { |
| 2237 | return 0 == memcmp(v1->data, v2->data, sizeof(v1->data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2238 | } |
| 2239 | |
| 2240 | static int |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 2241 | same_clid(clientid_t *cl1, clientid_t *cl2) |
| 2242 | { |
| 2243 | return (cl1->cl_boot == cl2->cl_boot) && (cl1->cl_id == cl2->cl_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2244 | } |
| 2245 | |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 2246 | static bool groups_equal(struct group_info *g1, struct group_info *g2) |
| 2247 | { |
| 2248 | int i; |
| 2249 | |
| 2250 | if (g1->ngroups != g2->ngroups) |
| 2251 | return false; |
| 2252 | for (i=0; i<g1->ngroups; i++) |
Alexey Dobriyan | 81243ea | 2016-10-07 17:03:12 -0700 | [diff] [blame] | 2253 | if (!gid_eq(g1->gid[i], g2->gid[i])) |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 2254 | return false; |
| 2255 | return true; |
| 2256 | } |
| 2257 | |
J. Bruce Fields | 68eb350 | 2012-08-21 12:48:30 -0400 | [diff] [blame] | 2258 | /* |
| 2259 | * RFC 3530 language requires clid_inuse be returned when the |
| 2260 | * "principal" associated with a requests differs from that previously |
| 2261 | * used. We use uid, gid's, and gss principal string as our best |
| 2262 | * approximation. We also don't want to allow non-gss use of a client |
| 2263 | * established using gss: in theory cr_principal should catch that |
| 2264 | * change, but in practice cr_principal can be null even in the gss case |
| 2265 | * since gssd doesn't always pass down a principal string. |
| 2266 | */ |
| 2267 | static bool is_gss_cred(struct svc_cred *cr) |
| 2268 | { |
| 2269 | /* Is cr_flavor one of the gss "pseudoflavors"?: */ |
| 2270 | return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR); |
| 2271 | } |
| 2272 | |
| 2273 | |
Vivek Trivedi | 5559b50 | 2012-07-24 21:18:20 +0530 | [diff] [blame] | 2274 | static bool |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 2275 | same_creds(struct svc_cred *cr1, struct svc_cred *cr2) |
| 2276 | { |
J. Bruce Fields | 68eb350 | 2012-08-21 12:48:30 -0400 | [diff] [blame] | 2277 | if ((is_gss_cred(cr1) != is_gss_cred(cr2)) |
Eric W. Biederman | 6fab877 | 2013-02-02 06:53:11 -0800 | [diff] [blame] | 2278 | || (!uid_eq(cr1->cr_uid, cr2->cr_uid)) |
| 2279 | || (!gid_eq(cr1->cr_gid, cr2->cr_gid)) |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 2280 | || !groups_equal(cr1->cr_group_info, cr2->cr_group_info)) |
| 2281 | return false; |
Chuck Lever | 9abdda5 | 2018-08-16 12:05:59 -0400 | [diff] [blame] | 2282 | /* XXX: check that cr_targ_princ fields match ? */ |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 2283 | if (cr1->cr_principal == cr2->cr_principal) |
| 2284 | return true; |
| 2285 | if (!cr1->cr_principal || !cr2->cr_principal) |
| 2286 | return false; |
Vivek Trivedi | 5559b50 | 2012-07-24 21:18:20 +0530 | [diff] [blame] | 2287 | return 0 == strcmp(cr1->cr_principal, cr2->cr_principal); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2288 | } |
| 2289 | |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2290 | static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp) |
| 2291 | { |
| 2292 | struct svc_cred *cr = &rqstp->rq_cred; |
| 2293 | u32 service; |
| 2294 | |
J. Bruce Fields | c472059 | 2013-08-07 11:41:49 -0400 | [diff] [blame] | 2295 | if (!cr->cr_gss_mech) |
| 2296 | return false; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2297 | service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor); |
| 2298 | return service == RPC_GSS_SVC_INTEGRITY || |
| 2299 | service == RPC_GSS_SVC_PRIVACY; |
| 2300 | } |
| 2301 | |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 2302 | bool nfsd4_mach_creds_match(struct nfs4_client *cl, struct svc_rqst *rqstp) |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2303 | { |
| 2304 | struct svc_cred *cr = &rqstp->rq_cred; |
| 2305 | |
| 2306 | if (!cl->cl_mach_cred) |
| 2307 | return true; |
| 2308 | if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech) |
| 2309 | return false; |
| 2310 | if (!svc_rqst_integrity_protected(rqstp)) |
| 2311 | return false; |
J. Bruce Fields | 414ca01 | 2015-11-20 10:48:02 -0500 | [diff] [blame] | 2312 | if (cl->cl_cred.cr_raw_principal) |
| 2313 | return 0 == strcmp(cl->cl_cred.cr_raw_principal, |
| 2314 | cr->cr_raw_principal); |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2315 | if (!cr->cr_principal) |
| 2316 | return false; |
| 2317 | return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal); |
| 2318 | } |
| 2319 | |
Jeff Layton | 294ac32 | 2014-07-30 08:27:15 -0400 | [diff] [blame] | 2320 | static void gen_confirm(struct nfs4_client *clp, struct nfsd_net *nn) |
J. Bruce Fields | deda2fa | 2007-11-19 20:31:04 -0500 | [diff] [blame] | 2321 | { |
Chuck Lever | ab4684d | 2012-03-02 17:13:50 -0500 | [diff] [blame] | 2322 | __be32 verf[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2323 | |
Jeff Layton | f419992 | 2014-06-17 07:44:11 -0400 | [diff] [blame] | 2324 | /* |
| 2325 | * This is opaque to client, so no need to byte-swap. Use |
| 2326 | * __force to keep sparse happy |
| 2327 | */ |
Arnd Bergmann | 9104ae4 | 2019-11-04 16:45:30 +0100 | [diff] [blame] | 2328 | verf[0] = (__force __be32)(u32)ktime_get_real_seconds(); |
Kinglong Mee | 19311aa8 | 2015-07-18 07:33:31 +0800 | [diff] [blame] | 2329 | verf[1] = (__force __be32)nn->clverifier_counter++; |
Chuck Lever | ab4684d | 2012-03-02 17:13:50 -0500 | [diff] [blame] | 2330 | memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2331 | } |
| 2332 | |
Jeff Layton | 294ac32 | 2014-07-30 08:27:15 -0400 | [diff] [blame] | 2333 | static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn) |
| 2334 | { |
Arnd Bergmann | 9cc7680 | 2019-10-31 15:53:13 +0100 | [diff] [blame] | 2335 | clp->cl_clientid.cl_boot = (u32)nn->boot_time; |
Jeff Layton | 294ac32 | 2014-07-30 08:27:15 -0400 | [diff] [blame] | 2336 | clp->cl_clientid.cl_id = nn->clientid_counter++; |
| 2337 | gen_confirm(clp, nn); |
| 2338 | } |
| 2339 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 2340 | static struct nfs4_stid * |
| 2341 | find_stateid_locked(struct nfs4_client *cl, stateid_t *t) |
J. Bruce Fields | 4581d14 | 2011-09-06 14:56:09 -0400 | [diff] [blame] | 2342 | { |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 2343 | struct nfs4_stid *ret; |
| 2344 | |
| 2345 | ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id); |
| 2346 | if (!ret || !ret->sc_type) |
| 2347 | return NULL; |
| 2348 | return ret; |
J. Bruce Fields | 4d71ab8 | 2011-09-06 16:48:57 -0400 | [diff] [blame] | 2349 | } |
| 2350 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 2351 | static struct nfs4_stid * |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 2352 | find_stateid_by_type(struct nfs4_client *cl, stateid_t *t, char typemask) |
J. Bruce Fields | f459e45 | 2011-09-09 09:06:12 -0400 | [diff] [blame] | 2353 | { |
| 2354 | struct nfs4_stid *s; |
| 2355 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 2356 | spin_lock(&cl->cl_lock); |
| 2357 | s = find_stateid_locked(cl, t); |
Trond Myklebust | 2d3f966 | 2014-07-29 21:34:25 -0400 | [diff] [blame] | 2358 | if (s != NULL) { |
| 2359 | if (typemask & s->sc_type) |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 2360 | refcount_inc(&s->sc_count); |
Trond Myklebust | 2d3f966 | 2014-07-29 21:34:25 -0400 | [diff] [blame] | 2361 | else |
| 2362 | s = NULL; |
| 2363 | } |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 2364 | spin_unlock(&cl->cl_lock); |
| 2365 | return s; |
J. Bruce Fields | 4581d14 | 2011-09-06 14:56:09 -0400 | [diff] [blame] | 2366 | } |
| 2367 | |
J. Bruce Fields | a204f25 | 2019-06-19 12:54:45 -0400 | [diff] [blame] | 2368 | static struct nfs4_client *get_nfsdfs_clp(struct inode *inode) |
| 2369 | { |
| 2370 | struct nfsdfs_client *nc; |
| 2371 | nc = get_nfsdfs_client(inode); |
| 2372 | if (!nc) |
| 2373 | return NULL; |
| 2374 | return container_of(nc, struct nfs4_client, cl_nfsdfs); |
| 2375 | } |
| 2376 | |
J. Bruce Fields | 169319f | 2019-06-19 12:39:46 -0400 | [diff] [blame] | 2377 | static void seq_quote_mem(struct seq_file *m, char *data, int len) |
| 2378 | { |
| 2379 | seq_printf(m, "\""); |
Andy Shevchenko | c054639 | 2021-06-30 18:55:43 -0700 | [diff] [blame] | 2380 | seq_escape_mem(m, data, len, ESCAPE_HEX | ESCAPE_NAP | ESCAPE_APPEND, "\"\\"); |
J. Bruce Fields | 169319f | 2019-06-19 12:39:46 -0400 | [diff] [blame] | 2381 | seq_printf(m, "\""); |
| 2382 | } |
| 2383 | |
Dave Wysochanski | 3518c86 | 2021-06-02 13:51:39 -0400 | [diff] [blame] | 2384 | static const char *cb_state2str(int state) |
| 2385 | { |
| 2386 | switch (state) { |
| 2387 | case NFSD4_CB_UP: |
| 2388 | return "UP"; |
| 2389 | case NFSD4_CB_UNKNOWN: |
| 2390 | return "UNKNOWN"; |
| 2391 | case NFSD4_CB_DOWN: |
| 2392 | return "DOWN"; |
| 2393 | case NFSD4_CB_FAULT: |
| 2394 | return "FAULT"; |
| 2395 | } |
| 2396 | return "UNDEFINED"; |
| 2397 | } |
| 2398 | |
J. Bruce Fields | 97ad403 | 2019-04-09 15:56:57 -0400 | [diff] [blame] | 2399 | static int client_info_show(struct seq_file *m, void *v) |
| 2400 | { |
| 2401 | struct inode *inode = m->private; |
J. Bruce Fields | 97ad403 | 2019-04-09 15:56:57 -0400 | [diff] [blame] | 2402 | struct nfs4_client *clp; |
| 2403 | u64 clid; |
| 2404 | |
J. Bruce Fields | a204f25 | 2019-06-19 12:54:45 -0400 | [diff] [blame] | 2405 | clp = get_nfsdfs_clp(inode); |
| 2406 | if (!clp) |
J. Bruce Fields | 97ad403 | 2019-04-09 15:56:57 -0400 | [diff] [blame] | 2407 | return -ENXIO; |
J. Bruce Fields | 97ad403 | 2019-04-09 15:56:57 -0400 | [diff] [blame] | 2408 | memcpy(&clid, &clp->cl_clientid, sizeof(clid)); |
| 2409 | seq_printf(m, "clientid: 0x%llx\n", clid); |
J. Bruce Fields | 169319f | 2019-06-19 12:39:46 -0400 | [diff] [blame] | 2410 | seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr); |
NeilBrown | 472d155 | 2021-03-20 09:38:04 +1100 | [diff] [blame] | 2411 | if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags)) |
| 2412 | seq_puts(m, "status: confirmed\n"); |
| 2413 | else |
| 2414 | seq_puts(m, "status: unconfirmed\n"); |
J. Bruce Fields | 169319f | 2019-06-19 12:39:46 -0400 | [diff] [blame] | 2415 | seq_printf(m, "name: "); |
| 2416 | seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len); |
| 2417 | seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion); |
J. Bruce Fields | 7912344 | 2019-06-05 12:42:05 -0400 | [diff] [blame] | 2418 | if (clp->cl_nii_domain.data) { |
| 2419 | seq_printf(m, "Implementation domain: "); |
| 2420 | seq_quote_mem(m, clp->cl_nii_domain.data, |
| 2421 | clp->cl_nii_domain.len); |
| 2422 | seq_printf(m, "\nImplementation name: "); |
| 2423 | seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len); |
Arnd Bergmann | e29f470 | 2019-10-31 14:52:43 +0100 | [diff] [blame] | 2424 | seq_printf(m, "\nImplementation time: [%lld, %ld]\n", |
J. Bruce Fields | 7912344 | 2019-06-05 12:42:05 -0400 | [diff] [blame] | 2425 | clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec); |
| 2426 | } |
Dave Wysochanski | 3518c86 | 2021-06-02 13:51:39 -0400 | [diff] [blame] | 2427 | seq_printf(m, "callback state: %s\n", cb_state2str(clp->cl_cb_state)); |
| 2428 | seq_printf(m, "callback address: %pISpc\n", &clp->cl_cb_conn.cb_addr); |
J. Bruce Fields | 97ad403 | 2019-04-09 15:56:57 -0400 | [diff] [blame] | 2429 | drop_client(clp); |
| 2430 | |
| 2431 | return 0; |
| 2432 | } |
| 2433 | |
| 2434 | static int client_info_open(struct inode *inode, struct file *file) |
| 2435 | { |
| 2436 | return single_open(file, client_info_show, inode); |
| 2437 | } |
| 2438 | |
| 2439 | static const struct file_operations client_info_fops = { |
| 2440 | .open = client_info_open, |
| 2441 | .read = seq_read, |
| 2442 | .llseek = seq_lseek, |
| 2443 | .release = single_release, |
| 2444 | }; |
| 2445 | |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2446 | static void *states_start(struct seq_file *s, loff_t *pos) |
| 2447 | __acquires(&clp->cl_lock) |
| 2448 | { |
| 2449 | struct nfs4_client *clp = s->private; |
| 2450 | unsigned long id = *pos; |
| 2451 | void *ret; |
| 2452 | |
| 2453 | spin_lock(&clp->cl_lock); |
| 2454 | ret = idr_get_next_ul(&clp->cl_stateids, &id); |
| 2455 | *pos = id; |
| 2456 | return ret; |
| 2457 | } |
| 2458 | |
| 2459 | static void *states_next(struct seq_file *s, void *v, loff_t *pos) |
| 2460 | { |
| 2461 | struct nfs4_client *clp = s->private; |
| 2462 | unsigned long id = *pos; |
| 2463 | void *ret; |
| 2464 | |
| 2465 | id = *pos; |
| 2466 | id++; |
| 2467 | ret = idr_get_next_ul(&clp->cl_stateids, &id); |
| 2468 | *pos = id; |
| 2469 | return ret; |
| 2470 | } |
| 2471 | |
| 2472 | static void states_stop(struct seq_file *s, void *v) |
| 2473 | __releases(&clp->cl_lock) |
| 2474 | { |
| 2475 | struct nfs4_client *clp = s->private; |
| 2476 | |
| 2477 | spin_unlock(&clp->cl_lock); |
| 2478 | } |
| 2479 | |
Achilles Gaikwad | 580da46 | 2020-04-20 18:20:31 +0530 | [diff] [blame] | 2480 | static void nfs4_show_fname(struct seq_file *s, struct nfsd_file *f) |
| 2481 | { |
| 2482 | seq_printf(s, "filename: \"%pD2\"", f->nf_file); |
| 2483 | } |
| 2484 | |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 2485 | static void nfs4_show_superblock(struct seq_file *s, struct nfsd_file *f) |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2486 | { |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 2487 | struct inode *inode = f->nf_inode; |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2488 | |
| 2489 | seq_printf(s, "superblock: \"%02x:%02x:%ld\"", |
| 2490 | MAJOR(inode->i_sb->s_dev), |
| 2491 | MINOR(inode->i_sb->s_dev), |
| 2492 | inode->i_ino); |
| 2493 | } |
| 2494 | |
| 2495 | static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo) |
| 2496 | { |
| 2497 | seq_printf(s, "owner: "); |
| 2498 | seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len); |
| 2499 | } |
| 2500 | |
J. Bruce Fields | ace7ade | 2020-04-13 21:37:19 -0400 | [diff] [blame] | 2501 | static void nfs4_show_stateid(struct seq_file *s, stateid_t *stid) |
| 2502 | { |
J. Bruce Fields | ee590d2 | 2020-04-13 22:03:06 -0400 | [diff] [blame] | 2503 | seq_printf(s, "0x%.8x", stid->si_generation); |
| 2504 | seq_printf(s, "%12phN", &stid->si_opaque); |
J. Bruce Fields | ace7ade | 2020-04-13 21:37:19 -0400 | [diff] [blame] | 2505 | } |
| 2506 | |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2507 | static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st) |
| 2508 | { |
| 2509 | struct nfs4_ol_stateid *ols; |
| 2510 | struct nfs4_file *nf; |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 2511 | struct nfsd_file *file; |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2512 | struct nfs4_stateowner *oo; |
| 2513 | unsigned int access, deny; |
| 2514 | |
| 2515 | if (st->sc_type != NFS4_OPEN_STID && st->sc_type != NFS4_LOCK_STID) |
| 2516 | return 0; /* XXX: or SEQ_SKIP? */ |
| 2517 | ols = openlockstateid(st); |
| 2518 | oo = ols->st_stateowner; |
| 2519 | nf = st->sc_file; |
| 2520 | file = find_any_file(nf); |
J. Bruce Fields | 9affa43 | 2020-07-15 13:31:36 -0400 | [diff] [blame] | 2521 | if (!file) |
| 2522 | return 0; |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2523 | |
J. Bruce Fields | ace7ade | 2020-04-13 21:37:19 -0400 | [diff] [blame] | 2524 | seq_printf(s, "- "); |
| 2525 | nfs4_show_stateid(s, &st->sc_stateid); |
| 2526 | seq_printf(s, ": { type: open, "); |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2527 | |
| 2528 | access = bmap_to_share_mode(ols->st_access_bmap); |
| 2529 | deny = bmap_to_share_mode(ols->st_deny_bmap); |
| 2530 | |
J. Bruce Fields | c4b77ed | 2019-10-03 12:33:08 -0400 | [diff] [blame] | 2531 | seq_printf(s, "access: %s%s, ", |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2532 | access & NFS4_SHARE_ACCESS_READ ? "r" : "-", |
| 2533 | access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-"); |
J. Bruce Fields | c4b77ed | 2019-10-03 12:33:08 -0400 | [diff] [blame] | 2534 | seq_printf(s, "deny: %s%s, ", |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2535 | deny & NFS4_SHARE_ACCESS_READ ? "r" : "-", |
| 2536 | deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-"); |
| 2537 | |
| 2538 | nfs4_show_superblock(s, file); |
| 2539 | seq_printf(s, ", "); |
Achilles Gaikwad | 580da46 | 2020-04-20 18:20:31 +0530 | [diff] [blame] | 2540 | nfs4_show_fname(s, file); |
| 2541 | seq_printf(s, ", "); |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2542 | nfs4_show_owner(s, oo); |
| 2543 | seq_printf(s, " }\n"); |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 2544 | nfsd_file_put(file); |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2545 | |
| 2546 | return 0; |
| 2547 | } |
| 2548 | |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2549 | static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st) |
| 2550 | { |
| 2551 | struct nfs4_ol_stateid *ols; |
| 2552 | struct nfs4_file *nf; |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 2553 | struct nfsd_file *file; |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2554 | struct nfs4_stateowner *oo; |
| 2555 | |
| 2556 | ols = openlockstateid(st); |
| 2557 | oo = ols->st_stateowner; |
| 2558 | nf = st->sc_file; |
| 2559 | file = find_any_file(nf); |
J. Bruce Fields | 9affa43 | 2020-07-15 13:31:36 -0400 | [diff] [blame] | 2560 | if (!file) |
| 2561 | return 0; |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2562 | |
J. Bruce Fields | ace7ade | 2020-04-13 21:37:19 -0400 | [diff] [blame] | 2563 | seq_printf(s, "- "); |
| 2564 | nfs4_show_stateid(s, &st->sc_stateid); |
| 2565 | seq_printf(s, ": { type: lock, "); |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2566 | |
| 2567 | /* |
| 2568 | * Note: a lock stateid isn't really the same thing as a lock, |
| 2569 | * it's the locking state held by one owner on a file, and there |
| 2570 | * may be multiple (or no) lock ranges associated with it. |
| 2571 | * (Same for the matter is true of open stateids.) |
| 2572 | */ |
| 2573 | |
| 2574 | nfs4_show_superblock(s, file); |
| 2575 | /* XXX: open stateid? */ |
| 2576 | seq_printf(s, ", "); |
Achilles Gaikwad | 580da46 | 2020-04-20 18:20:31 +0530 | [diff] [blame] | 2577 | nfs4_show_fname(s, file); |
| 2578 | seq_printf(s, ", "); |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2579 | nfs4_show_owner(s, oo); |
| 2580 | seq_printf(s, " }\n"); |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 2581 | nfsd_file_put(file); |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2582 | |
| 2583 | return 0; |
| 2584 | } |
| 2585 | |
| 2586 | static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st) |
| 2587 | { |
| 2588 | struct nfs4_delegation *ds; |
| 2589 | struct nfs4_file *nf; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 2590 | struct nfsd_file *file; |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2591 | |
| 2592 | ds = delegstateid(st); |
| 2593 | nf = st->sc_file; |
J. Bruce Fields | 9affa43 | 2020-07-15 13:31:36 -0400 | [diff] [blame] | 2594 | file = find_deleg_file(nf); |
| 2595 | if (!file) |
| 2596 | return 0; |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2597 | |
J. Bruce Fields | ace7ade | 2020-04-13 21:37:19 -0400 | [diff] [blame] | 2598 | seq_printf(s, "- "); |
| 2599 | nfs4_show_stateid(s, &st->sc_stateid); |
| 2600 | seq_printf(s, ": { type: deleg, "); |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2601 | |
| 2602 | /* Kinda dead code as long as we only support read delegs: */ |
| 2603 | seq_printf(s, "access: %s, ", |
| 2604 | ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w"); |
| 2605 | |
| 2606 | /* XXX: lease time, whether it's being recalled. */ |
| 2607 | |
| 2608 | nfs4_show_superblock(s, file); |
Achilles Gaikwad | 580da46 | 2020-04-20 18:20:31 +0530 | [diff] [blame] | 2609 | seq_printf(s, ", "); |
| 2610 | nfs4_show_fname(s, file); |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2611 | seq_printf(s, " }\n"); |
J. Bruce Fields | 9affa43 | 2020-07-15 13:31:36 -0400 | [diff] [blame] | 2612 | nfsd_file_put(file); |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2613 | |
| 2614 | return 0; |
| 2615 | } |
| 2616 | |
J. Bruce Fields | 0c4b62b | 2019-05-14 15:52:57 -0400 | [diff] [blame] | 2617 | static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st) |
| 2618 | { |
| 2619 | struct nfs4_layout_stateid *ls; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 2620 | struct nfsd_file *file; |
J. Bruce Fields | 0c4b62b | 2019-05-14 15:52:57 -0400 | [diff] [blame] | 2621 | |
| 2622 | ls = container_of(st, struct nfs4_layout_stateid, ls_stid); |
| 2623 | file = ls->ls_file; |
| 2624 | |
J. Bruce Fields | ace7ade | 2020-04-13 21:37:19 -0400 | [diff] [blame] | 2625 | seq_printf(s, "- "); |
| 2626 | nfs4_show_stateid(s, &st->sc_stateid); |
| 2627 | seq_printf(s, ": { type: layout, "); |
J. Bruce Fields | 0c4b62b | 2019-05-14 15:52:57 -0400 | [diff] [blame] | 2628 | |
| 2629 | /* XXX: What else would be useful? */ |
| 2630 | |
| 2631 | nfs4_show_superblock(s, file); |
Achilles Gaikwad | 580da46 | 2020-04-20 18:20:31 +0530 | [diff] [blame] | 2632 | seq_printf(s, ", "); |
| 2633 | nfs4_show_fname(s, file); |
J. Bruce Fields | 0c4b62b | 2019-05-14 15:52:57 -0400 | [diff] [blame] | 2634 | seq_printf(s, " }\n"); |
| 2635 | |
| 2636 | return 0; |
| 2637 | } |
| 2638 | |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2639 | static int states_show(struct seq_file *s, void *v) |
| 2640 | { |
| 2641 | struct nfs4_stid *st = v; |
| 2642 | |
| 2643 | switch (st->sc_type) { |
| 2644 | case NFS4_OPEN_STID: |
| 2645 | return nfs4_show_open(s, st); |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2646 | case NFS4_LOCK_STID: |
| 2647 | return nfs4_show_lock(s, st); |
| 2648 | case NFS4_DELEG_STID: |
| 2649 | return nfs4_show_deleg(s, st); |
J. Bruce Fields | 0c4b62b | 2019-05-14 15:52:57 -0400 | [diff] [blame] | 2650 | case NFS4_LAYOUT_STID: |
| 2651 | return nfs4_show_layout(s, st); |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2652 | default: |
| 2653 | return 0; /* XXX: or SEQ_SKIP? */ |
| 2654 | } |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2655 | /* XXX: copy stateids? */ |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2656 | } |
| 2657 | |
| 2658 | static struct seq_operations states_seq_ops = { |
| 2659 | .start = states_start, |
| 2660 | .next = states_next, |
| 2661 | .stop = states_stop, |
| 2662 | .show = states_show |
| 2663 | }; |
| 2664 | |
| 2665 | static int client_states_open(struct inode *inode, struct file *file) |
| 2666 | { |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2667 | struct seq_file *s; |
| 2668 | struct nfs4_client *clp; |
| 2669 | int ret; |
| 2670 | |
J. Bruce Fields | a204f25 | 2019-06-19 12:54:45 -0400 | [diff] [blame] | 2671 | clp = get_nfsdfs_clp(inode); |
| 2672 | if (!clp) |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2673 | return -ENXIO; |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2674 | |
| 2675 | ret = seq_open(file, &states_seq_ops); |
| 2676 | if (ret) |
| 2677 | return ret; |
| 2678 | s = file->private_data; |
| 2679 | s->private = clp; |
| 2680 | return 0; |
| 2681 | } |
| 2682 | |
| 2683 | static int client_opens_release(struct inode *inode, struct file *file) |
| 2684 | { |
| 2685 | struct seq_file *m = file->private_data; |
| 2686 | struct nfs4_client *clp = m->private; |
| 2687 | |
| 2688 | /* XXX: alternatively, we could get/drop in seq start/stop */ |
| 2689 | drop_client(clp); |
| 2690 | return 0; |
| 2691 | } |
| 2692 | |
| 2693 | static const struct file_operations client_states_fops = { |
| 2694 | .open = client_states_open, |
| 2695 | .read = seq_read, |
| 2696 | .llseek = seq_lseek, |
| 2697 | .release = client_opens_release, |
| 2698 | }; |
| 2699 | |
J. Bruce Fields | 89c905b | 2019-06-19 12:43:11 -0400 | [diff] [blame] | 2700 | /* |
| 2701 | * Normally we refuse to destroy clients that are in use, but here the |
| 2702 | * administrator is telling us to just do it. We also want to wait |
| 2703 | * so the caller has a guarantee that the client's locks are gone by |
| 2704 | * the time the write returns: |
| 2705 | */ |
YueHaibing | 297e57a | 2019-07-08 15:29:33 +0800 | [diff] [blame] | 2706 | static void force_expire_client(struct nfs4_client *clp) |
J. Bruce Fields | 89c905b | 2019-06-19 12:43:11 -0400 | [diff] [blame] | 2707 | { |
| 2708 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 2709 | bool already_expired; |
| 2710 | |
Chuck Lever | 2958d2e | 2021-05-14 15:56:06 -0400 | [diff] [blame] | 2711 | trace_nfsd_clid_admin_expired(&clp->cl_clientid); |
| 2712 | |
J. Bruce Fields | f7104cc | 2021-08-12 16:41:43 -0400 | [diff] [blame] | 2713 | spin_lock(&nn->client_lock); |
J. Bruce Fields | 89c905b | 2019-06-19 12:43:11 -0400 | [diff] [blame] | 2714 | clp->cl_time = 0; |
J. Bruce Fields | f7104cc | 2021-08-12 16:41:43 -0400 | [diff] [blame] | 2715 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | 89c905b | 2019-06-19 12:43:11 -0400 | [diff] [blame] | 2716 | |
| 2717 | wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0); |
| 2718 | spin_lock(&nn->client_lock); |
| 2719 | already_expired = list_empty(&clp->cl_lru); |
| 2720 | if (!already_expired) |
| 2721 | unhash_client_locked(clp); |
| 2722 | spin_unlock(&nn->client_lock); |
| 2723 | |
| 2724 | if (!already_expired) |
| 2725 | expire_client(clp); |
| 2726 | else |
| 2727 | wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL); |
| 2728 | } |
| 2729 | |
| 2730 | static ssize_t client_ctl_write(struct file *file, const char __user *buf, |
| 2731 | size_t size, loff_t *pos) |
| 2732 | { |
| 2733 | char *data; |
| 2734 | struct nfs4_client *clp; |
| 2735 | |
| 2736 | data = simple_transaction_get(file, buf, size); |
| 2737 | if (IS_ERR(data)) |
| 2738 | return PTR_ERR(data); |
| 2739 | if (size != 7 || 0 != memcmp(data, "expire\n", 7)) |
| 2740 | return -EINVAL; |
| 2741 | clp = get_nfsdfs_clp(file_inode(file)); |
| 2742 | if (!clp) |
| 2743 | return -ENXIO; |
| 2744 | force_expire_client(clp); |
| 2745 | drop_client(clp); |
| 2746 | return 7; |
| 2747 | } |
| 2748 | |
| 2749 | static const struct file_operations client_ctl_fops = { |
| 2750 | .write = client_ctl_write, |
| 2751 | .release = simple_transaction_release, |
| 2752 | }; |
| 2753 | |
J. Bruce Fields | 97ad403 | 2019-04-09 15:56:57 -0400 | [diff] [blame] | 2754 | static const struct tree_descr client_files[] = { |
| 2755 | [0] = {"info", &client_info_fops, S_IRUSR}, |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2756 | [1] = {"states", &client_states_fops, S_IRUSR}, |
Petr Vorel | 6cbfad5 | 2020-03-13 13:39:57 +0100 | [diff] [blame] | 2757 | [2] = {"ctl", &client_ctl_fops, S_IWUSR}, |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2758 | [3] = {""}, |
J. Bruce Fields | 97ad403 | 2019-04-09 15:56:57 -0400 | [diff] [blame] | 2759 | }; |
| 2760 | |
Jeff Layton | 2216d44 | 2012-11-12 15:00:57 -0500 | [diff] [blame] | 2761 | static struct nfs4_client *create_client(struct xdr_netobj name, |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2762 | struct svc_rqst *rqstp, nfs4_verifier *verf) |
| 2763 | { |
| 2764 | struct nfs4_client *clp; |
| 2765 | struct sockaddr *sa = svc_addr(rqstp); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 2766 | int ret; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2767 | struct net *net = SVC_NET(rqstp); |
J. Bruce Fields | e8a79fb | 2019-03-22 11:11:06 -0400 | [diff] [blame] | 2768 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
NeilBrown | 472d155 | 2021-03-20 09:38:04 +1100 | [diff] [blame] | 2769 | struct dentry *dentries[ARRAY_SIZE(client_files)]; |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2770 | |
| 2771 | clp = alloc_client(name); |
| 2772 | if (clp == NULL) |
| 2773 | return NULL; |
| 2774 | |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 2775 | ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred); |
| 2776 | if (ret) { |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 2777 | free_client(clp); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 2778 | return NULL; |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2779 | } |
J. Bruce Fields | e8a79fb | 2019-03-22 11:11:06 -0400 | [diff] [blame] | 2780 | gen_clid(clp, nn); |
| 2781 | kref_init(&clp->cl_nfsdfs.cl_ref); |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 2782 | nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL); |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 2783 | clp->cl_time = ktime_get_boottime_seconds(); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2784 | clear_bit(0, &clp->cl_cb_slot_busy); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2785 | copy_verf(clp, verf); |
J. Bruce Fields | 3bade24 | 2019-05-14 21:38:11 -0400 | [diff] [blame] | 2786 | memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage)); |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 2787 | clp->cl_cb_session = NULL; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2788 | clp->net = net; |
NeilBrown | 472d155 | 2021-03-20 09:38:04 +1100 | [diff] [blame] | 2789 | clp->cl_nfsd_dentry = nfsd_client_mkdir( |
| 2790 | nn, &clp->cl_nfsdfs, |
| 2791 | clp->cl_clientid.cl_id - nn->clientid_base, |
| 2792 | client_files, dentries); |
| 2793 | clp->cl_nfsd_info_dentry = dentries[0]; |
J. Bruce Fields | e8a79fb | 2019-03-22 11:11:06 -0400 | [diff] [blame] | 2794 | if (!clp->cl_nfsd_dentry) { |
| 2795 | free_client(clp); |
| 2796 | return NULL; |
| 2797 | } |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2798 | return clp; |
| 2799 | } |
| 2800 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 2801 | static void |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2802 | add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root) |
| 2803 | { |
| 2804 | struct rb_node **new = &(root->rb_node), *parent = NULL; |
| 2805 | struct nfs4_client *clp; |
| 2806 | |
| 2807 | while (*new) { |
| 2808 | clp = rb_entry(*new, struct nfs4_client, cl_namenode); |
| 2809 | parent = *new; |
| 2810 | |
| 2811 | if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0) |
| 2812 | new = &((*new)->rb_left); |
| 2813 | else |
| 2814 | new = &((*new)->rb_right); |
| 2815 | } |
| 2816 | |
| 2817 | rb_link_node(&new_clp->cl_namenode, parent, new); |
| 2818 | rb_insert_color(&new_clp->cl_namenode, root); |
| 2819 | } |
| 2820 | |
| 2821 | static struct nfs4_client * |
| 2822 | find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root) |
| 2823 | { |
Rasmus Villemoes | ef17af2 | 2014-12-05 16:40:07 +0100 | [diff] [blame] | 2824 | int cmp; |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2825 | struct rb_node *node = root->rb_node; |
| 2826 | struct nfs4_client *clp; |
| 2827 | |
| 2828 | while (node) { |
| 2829 | clp = rb_entry(node, struct nfs4_client, cl_namenode); |
| 2830 | cmp = compare_blob(&clp->cl_name, name); |
| 2831 | if (cmp > 0) |
| 2832 | node = node->rb_left; |
| 2833 | else if (cmp < 0) |
| 2834 | node = node->rb_right; |
| 2835 | else |
| 2836 | return clp; |
| 2837 | } |
| 2838 | return NULL; |
| 2839 | } |
| 2840 | |
| 2841 | static void |
| 2842 | add_to_unconfirmed(struct nfs4_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2843 | { |
| 2844 | unsigned int idhashval; |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 2845 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2846 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2847 | lockdep_assert_held(&nn->client_lock); |
| 2848 | |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2849 | clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2850 | add_clp_to_name_tree(clp, &nn->unconf_name_tree); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2851 | idhashval = clientid_hashval(clp->cl_clientid.cl_id); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 2852 | list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]); |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2853 | renew_client_locked(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2854 | } |
| 2855 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 2856 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2857 | move_to_confirmed(struct nfs4_client *clp) |
| 2858 | { |
| 2859 | unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2860 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2861 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2862 | lockdep_assert_held(&nn->client_lock); |
| 2863 | |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2864 | list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]); |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2865 | rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2866 | add_clp_to_name_tree(clp, &nn->conf_name_tree); |
J. Bruce Fields | 934bd07 | 2021-05-25 14:53:44 -0400 | [diff] [blame] | 2867 | set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); |
| 2868 | trace_nfsd_clid_confirmed(&clp->cl_clientid); |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2869 | renew_client_locked(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2870 | } |
| 2871 | |
| 2872 | static struct nfs4_client * |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2873 | find_client_in_id_table(struct list_head *tbl, clientid_t *clid, bool sessions) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2874 | { |
| 2875 | struct nfs4_client *clp; |
| 2876 | unsigned int idhashval = clientid_hashval(clid->cl_id); |
| 2877 | |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2878 | list_for_each_entry(clp, &tbl[idhashval], cl_idhash) { |
J. Bruce Fields | a50d2ad | 2011-10-12 16:24:27 -0400 | [diff] [blame] | 2879 | if (same_clid(&clp->cl_clientid, clid)) { |
J. Bruce Fields | d15c077 | 2012-09-13 16:19:31 -0400 | [diff] [blame] | 2880 | if ((bool)clp->cl_minorversion != sessions) |
| 2881 | return NULL; |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2882 | renew_client_locked(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2883 | return clp; |
J. Bruce Fields | a50d2ad | 2011-10-12 16:24:27 -0400 | [diff] [blame] | 2884 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2885 | } |
| 2886 | return NULL; |
| 2887 | } |
| 2888 | |
| 2889 | static struct nfs4_client * |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2890 | find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) |
| 2891 | { |
| 2892 | struct list_head *tbl = nn->conf_id_hashtbl; |
| 2893 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2894 | lockdep_assert_held(&nn->client_lock); |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2895 | return find_client_in_id_table(tbl, clid, sessions); |
| 2896 | } |
| 2897 | |
| 2898 | static struct nfs4_client * |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 2899 | find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2900 | { |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2901 | struct list_head *tbl = nn->unconf_id_hashtbl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2902 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2903 | lockdep_assert_held(&nn->client_lock); |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2904 | return find_client_in_id_table(tbl, clid, sessions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2905 | } |
| 2906 | |
J. Bruce Fields | 6e5f15c | 2010-11-24 17:17:34 -0500 | [diff] [blame] | 2907 | static bool clp_used_exchangeid(struct nfs4_client *clp) |
Andy Adamson | a1bcecd | 2009-04-03 08:28:05 +0300 | [diff] [blame] | 2908 | { |
J. Bruce Fields | 6e5f15c | 2010-11-24 17:17:34 -0500 | [diff] [blame] | 2909 | return clp->cl_exchange_flags != 0; |
J. Bruce Fields | e203d50 | 2010-11-24 17:30:54 -0500 | [diff] [blame] | 2910 | } |
Andy Adamson | a1bcecd | 2009-04-03 08:28:05 +0300 | [diff] [blame] | 2911 | |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2912 | static struct nfs4_client * |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2913 | find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2914 | { |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2915 | lockdep_assert_held(&nn->client_lock); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2916 | return find_clp_in_name_tree(name, &nn->conf_name_tree); |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2917 | } |
| 2918 | |
| 2919 | static struct nfs4_client * |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2920 | find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2921 | { |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2922 | lockdep_assert_held(&nn->client_lock); |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2923 | return find_clp_in_name_tree(name, &nn->unconf_name_tree); |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2924 | } |
| 2925 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 2926 | static void |
Takuma Umeya | 6f3d772 | 2010-12-15 14:09:01 +0900 | [diff] [blame] | 2927 | gen_callback(struct nfs4_client *clp, struct nfsd4_setclientid *se, struct svc_rqst *rqstp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2928 | { |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2929 | struct nfs4_cb_conn *conn = &clp->cl_cb_conn; |
Takuma Umeya | 6f3d772 | 2010-12-15 14:09:01 +0900 | [diff] [blame] | 2930 | struct sockaddr *sa = svc_addr(rqstp); |
| 2931 | u32 scopeid = rpc_get_scope_id(sa); |
Jeff Layton | 7077ecb | 2009-08-14 12:57:58 -0400 | [diff] [blame] | 2932 | unsigned short expected_family; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2933 | |
Jeff Layton | 7077ecb | 2009-08-14 12:57:58 -0400 | [diff] [blame] | 2934 | /* Currently, we only support tcp and tcp6 for the callback channel */ |
| 2935 | if (se->se_callback_netid_len == 3 && |
| 2936 | !memcmp(se->se_callback_netid_val, "tcp", 3)) |
| 2937 | expected_family = AF_INET; |
| 2938 | else if (se->se_callback_netid_len == 4 && |
| 2939 | !memcmp(se->se_callback_netid_val, "tcp6", 4)) |
| 2940 | expected_family = AF_INET6; |
| 2941 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2942 | goto out_err; |
| 2943 | |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2944 | conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val, |
Jeff Layton | aa9a4ec | 2009-08-14 12:57:57 -0400 | [diff] [blame] | 2945 | se->se_callback_addr_len, |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2946 | (struct sockaddr *)&conn->cb_addr, |
| 2947 | sizeof(conn->cb_addr)); |
Jeff Layton | aa9a4ec | 2009-08-14 12:57:57 -0400 | [diff] [blame] | 2948 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2949 | if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2950 | goto out_err; |
Jeff Layton | aa9a4ec | 2009-08-14 12:57:57 -0400 | [diff] [blame] | 2951 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2952 | if (conn->cb_addr.ss_family == AF_INET6) |
| 2953 | ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid; |
Jeff Layton | fbf4665 | 2009-08-14 12:57:59 -0400 | [diff] [blame] | 2954 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2955 | conn->cb_prog = se->se_callback_prog; |
| 2956 | conn->cb_ident = se->se_callback_ident; |
Mi Jinlong | 849a1cf | 2011-08-30 17:18:41 +0800 | [diff] [blame] | 2957 | memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen); |
Chuck Lever | 1eace0d | 2020-04-05 14:15:29 -0400 | [diff] [blame] | 2958 | trace_nfsd_cb_args(clp, conn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2959 | return; |
| 2960 | out_err: |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2961 | conn->cb_addr.ss_family = AF_UNSPEC; |
| 2962 | conn->cb_addrlen = 0; |
Chuck Lever | 1eace0d | 2020-04-05 14:15:29 -0400 | [diff] [blame] | 2963 | trace_nfsd_cb_nodelegs(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2964 | return; |
| 2965 | } |
| 2966 | |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2967 | /* |
J. Bruce Fields | 067e1ac | 2014-03-21 17:26:44 -0400 | [diff] [blame] | 2968 | * Cache a reply. nfsd4_check_resp_size() has bounded the cache size. |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2969 | */ |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 2970 | static void |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2971 | nfsd4_store_cache_entry(struct nfsd4_compoundres *resp) |
| 2972 | { |
Chuck Lever | bddfdbc | 2020-10-27 15:53:42 -0400 | [diff] [blame] | 2973 | struct xdr_buf *buf = resp->xdr->buf; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2974 | struct nfsd4_slot *slot = resp->cstate.slot; |
| 2975 | unsigned int base; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2976 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2977 | dprintk("--> %s slot %p\n", __func__, slot); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2978 | |
J. Bruce Fields | 085def3 | 2017-10-18 16:17:18 -0400 | [diff] [blame] | 2979 | slot->sl_flags |= NFSD4_SLOT_INITIALIZED; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2980 | slot->sl_opcnt = resp->opcnt; |
| 2981 | slot->sl_status = resp->cstate.status; |
J. Bruce Fields | 53da6a5 | 2017-10-17 20:38:49 -0400 | [diff] [blame] | 2982 | free_svc_cred(&slot->sl_cred); |
| 2983 | copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred); |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 2984 | |
J. Bruce Fields | 085def3 | 2017-10-18 16:17:18 -0400 | [diff] [blame] | 2985 | if (!nfsd4_cache_this(resp)) { |
| 2986 | slot->sl_flags &= ~NFSD4_SLOT_CACHED; |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 2987 | return; |
| 2988 | } |
J. Bruce Fields | 085def3 | 2017-10-18 16:17:18 -0400 | [diff] [blame] | 2989 | slot->sl_flags |= NFSD4_SLOT_CACHED; |
| 2990 | |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 2991 | base = resp->cstate.data_offset; |
| 2992 | slot->sl_datalen = buf->len - base; |
| 2993 | if (read_bytes_from_xdr_buf(buf, base, slot->sl_data, slot->sl_datalen)) |
Dan Carpenter | d3f0340 | 2015-11-21 13:28:50 +0300 | [diff] [blame] | 2994 | WARN(1, "%s: sessions DRC could not cache compound\n", |
| 2995 | __func__); |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2996 | return; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2997 | } |
| 2998 | |
| 2999 | /* |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 3000 | * Encode the replay sequence operation from the slot values. |
| 3001 | * If cachethis is FALSE encode the uncached rep error on the next |
| 3002 | * operation which sets resp->p and increments resp->opcnt for |
| 3003 | * nfs4svc_encode_compoundres. |
| 3004 | * |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 3005 | */ |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 3006 | static __be32 |
| 3007 | nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args, |
| 3008 | struct nfsd4_compoundres *resp) |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 3009 | { |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 3010 | struct nfsd4_op *op; |
| 3011 | struct nfsd4_slot *slot = resp->cstate.slot; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 3012 | |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 3013 | /* Encode the replayed sequence operation */ |
| 3014 | op = &args->ops[resp->opcnt - 1]; |
| 3015 | nfsd4_encode_operation(resp, op); |
| 3016 | |
J. Bruce Fields | 085def3 | 2017-10-18 16:17:18 -0400 | [diff] [blame] | 3017 | if (slot->sl_flags & NFSD4_SLOT_CACHED) |
| 3018 | return op->status; |
| 3019 | if (args->opcnt == 1) { |
| 3020 | /* |
| 3021 | * The original operation wasn't a solo sequence--we |
| 3022 | * always cache those--so this retry must not match the |
| 3023 | * original: |
| 3024 | */ |
| 3025 | op->status = nfserr_seq_false_retry; |
| 3026 | } else { |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 3027 | op = &args->ops[resp->opcnt++]; |
| 3028 | op->status = nfserr_retry_uncached_rep; |
| 3029 | nfsd4_encode_operation(resp, op); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 3030 | } |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 3031 | return op->status; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 3032 | } |
| 3033 | |
| 3034 | /* |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 3035 | * The sequence operation is not cached because we can use the slot and |
| 3036 | * session values. |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 3037 | */ |
J. Bruce Fields | 3ca2eb9 | 2014-03-21 12:04:21 -0400 | [diff] [blame] | 3038 | static __be32 |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 3039 | nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp, |
| 3040 | struct nfsd4_sequence *seq) |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 3041 | { |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 3042 | struct nfsd4_slot *slot = resp->cstate.slot; |
Chuck Lever | bddfdbc | 2020-10-27 15:53:42 -0400 | [diff] [blame] | 3043 | struct xdr_stream *xdr = resp->xdr; |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 3044 | __be32 *p; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 3045 | __be32 status; |
| 3046 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 3047 | dprintk("--> %s slot %p\n", __func__, slot); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 3048 | |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 3049 | status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp); |
J. Bruce Fields | 0da7b19 | 2014-03-21 11:43:34 -0400 | [diff] [blame] | 3050 | if (status) |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 3051 | return status; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 3052 | |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 3053 | p = xdr_reserve_space(xdr, slot->sl_datalen); |
| 3054 | if (!p) { |
| 3055 | WARN_ON_ONCE(1); |
| 3056 | return nfserr_serverfault; |
| 3057 | } |
| 3058 | xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen); |
| 3059 | xdr_commit_encode(xdr); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 3060 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 3061 | resp->opcnt = slot->sl_opcnt; |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 3062 | return slot->sl_status; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 3063 | } |
| 3064 | |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3065 | /* |
| 3066 | * Set the exchange_id flags returned by the server. |
| 3067 | */ |
| 3068 | static void |
| 3069 | nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid) |
| 3070 | { |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 3071 | #ifdef CONFIG_NFSD_PNFS |
| 3072 | new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS; |
| 3073 | #else |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3074 | new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS; |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 3075 | #endif |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3076 | |
| 3077 | /* Referrals are supported, Migration is not. */ |
| 3078 | new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER; |
| 3079 | |
| 3080 | /* set the wire flags to return to client. */ |
| 3081 | clid->flags = new->cl_exchange_flags; |
| 3082 | } |
| 3083 | |
J. Bruce Fields | 4eaea13 | 2015-10-15 16:11:01 -0400 | [diff] [blame] | 3084 | static bool client_has_openowners(struct nfs4_client *clp) |
| 3085 | { |
| 3086 | struct nfs4_openowner *oo; |
| 3087 | |
| 3088 | list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) { |
| 3089 | if (!list_empty(&oo->oo_owner.so_stateids)) |
| 3090 | return true; |
| 3091 | } |
| 3092 | return false; |
| 3093 | } |
| 3094 | |
J. Bruce Fields | 631fc9e | 2012-05-14 15:57:23 -0400 | [diff] [blame] | 3095 | static bool client_has_state(struct nfs4_client *clp) |
| 3096 | { |
J. Bruce Fields | 4eaea13 | 2015-10-15 16:11:01 -0400 | [diff] [blame] | 3097 | return client_has_openowners(clp) |
Kinglong Mee | 47e970b | 2015-07-21 13:09:17 +0800 | [diff] [blame] | 3098 | #ifdef CONFIG_NFSD_PNFS |
| 3099 | || !list_empty(&clp->cl_lo_states) |
| 3100 | #endif |
J. Bruce Fields | 6eccece | 2012-05-29 16:37:44 -0400 | [diff] [blame] | 3101 | || !list_empty(&clp->cl_delegations) |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 3102 | || !list_empty(&clp->cl_sessions) |
| 3103 | || !list_empty(&clp->async_copies); |
J. Bruce Fields | 631fc9e | 2012-05-14 15:57:23 -0400 | [diff] [blame] | 3104 | } |
| 3105 | |
J. Bruce Fields | 7912344 | 2019-06-05 12:42:05 -0400 | [diff] [blame] | 3106 | static __be32 copy_impl_id(struct nfs4_client *clp, |
| 3107 | struct nfsd4_exchange_id *exid) |
| 3108 | { |
| 3109 | if (!exid->nii_domain.data) |
| 3110 | return 0; |
| 3111 | xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL); |
| 3112 | if (!clp->cl_nii_domain.data) |
| 3113 | return nfserr_jukebox; |
| 3114 | xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL); |
| 3115 | if (!clp->cl_nii_name.data) |
| 3116 | return nfserr_jukebox; |
Arnd Bergmann | e29f470 | 2019-10-31 14:52:43 +0100 | [diff] [blame] | 3117 | clp->cl_nii_time = exid->nii_time; |
J. Bruce Fields | 7912344 | 2019-06-05 12:42:05 -0400 | [diff] [blame] | 3118 | return 0; |
| 3119 | } |
| 3120 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3121 | __be32 |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3122 | nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 3123 | union nfsd4_op_u *u) |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3124 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3125 | struct nfsd4_exchange_id *exid = &u->exchange_id; |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3126 | struct nfs4_client *conf, *new; |
| 3127 | struct nfs4_client *unconf = NULL; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3128 | __be32 status; |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 3129 | char addr_str[INET6_ADDRSTRLEN]; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3130 | nfs4_verifier verf = exid->verifier; |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 3131 | struct sockaddr *sa = svc_addr(rqstp); |
J. Bruce Fields | 83e08fd | 2012-05-14 09:08:10 -0400 | [diff] [blame] | 3132 | bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 3133 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3134 | |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 3135 | rpc_ntop(sa, addr_str, sizeof(addr_str)); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3136 | dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p " |
Chuck Lever | 523ec6e | 2020-11-02 15:19:12 -0500 | [diff] [blame] | 3137 | "ip_addr=%s flags %x, spa_how %u\n", |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3138 | __func__, rqstp, exid, exid->clname.len, exid->clname.data, |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 3139 | addr_str, exid->flags, exid->spa_how); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3140 | |
J. Bruce Fields | a084daf | 2011-10-10 15:07:40 -0400 | [diff] [blame] | 3141 | if (exid->flags & ~EXCHGID4_FLAG_MASK_A) |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3142 | return nfserr_inval; |
| 3143 | |
J. Bruce Fields | 50c7b94 | 2015-11-23 15:39:12 -0700 | [diff] [blame] | 3144 | new = create_client(exid->clname, rqstp, &verf); |
| 3145 | if (new == NULL) |
| 3146 | return nfserr_jukebox; |
J. Bruce Fields | 7912344 | 2019-06-05 12:42:05 -0400 | [diff] [blame] | 3147 | status = copy_impl_id(new, exid); |
| 3148 | if (status) |
| 3149 | goto out_nolock; |
J. Bruce Fields | 50c7b94 | 2015-11-23 15:39:12 -0700 | [diff] [blame] | 3150 | |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3151 | switch (exid->spa_how) { |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3152 | case SP4_MACH_CRED: |
Andrew Elble | ed94164 | 2016-06-15 12:52:09 -0400 | [diff] [blame] | 3153 | exid->spo_must_enforce[0] = 0; |
| 3154 | exid->spo_must_enforce[1] = ( |
| 3155 | 1 << (OP_BIND_CONN_TO_SESSION - 32) | |
| 3156 | 1 << (OP_EXCHANGE_ID - 32) | |
| 3157 | 1 << (OP_CREATE_SESSION - 32) | |
| 3158 | 1 << (OP_DESTROY_SESSION - 32) | |
| 3159 | 1 << (OP_DESTROY_CLIENTID - 32)); |
| 3160 | |
| 3161 | exid->spo_must_allow[0] &= (1 << (OP_CLOSE) | |
| 3162 | 1 << (OP_OPEN_DOWNGRADE) | |
| 3163 | 1 << (OP_LOCKU) | |
| 3164 | 1 << (OP_DELEGRETURN)); |
| 3165 | |
| 3166 | exid->spo_must_allow[1] &= ( |
| 3167 | 1 << (OP_TEST_STATEID - 32) | |
| 3168 | 1 << (OP_FREE_STATEID - 32)); |
J. Bruce Fields | 50c7b94 | 2015-11-23 15:39:12 -0700 | [diff] [blame] | 3169 | if (!svc_rqst_integrity_protected(rqstp)) { |
| 3170 | status = nfserr_inval; |
| 3171 | goto out_nolock; |
| 3172 | } |
J. Bruce Fields | 920dd9b | 2015-11-20 16:42:40 -0500 | [diff] [blame] | 3173 | /* |
| 3174 | * Sometimes userspace doesn't give us a principal. |
| 3175 | * Which is a bug, really. Anyway, we can't enforce |
| 3176 | * MACH_CRED in that case, better to give up now: |
| 3177 | */ |
J. Bruce Fields | 414ca01 | 2015-11-20 10:48:02 -0500 | [diff] [blame] | 3178 | if (!new->cl_cred.cr_principal && |
| 3179 | !new->cl_cred.cr_raw_principal) { |
J. Bruce Fields | 920dd9b | 2015-11-20 16:42:40 -0500 | [diff] [blame] | 3180 | status = nfserr_serverfault; |
| 3181 | goto out_nolock; |
| 3182 | } |
J. Bruce Fields | 50c7b94 | 2015-11-23 15:39:12 -0700 | [diff] [blame] | 3183 | new->cl_mach_cred = true; |
Gustavo A. R. Silva | 76c50eb | 2020-11-20 12:26:40 -0600 | [diff] [blame] | 3184 | break; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3185 | case SP4_NONE: |
| 3186 | break; |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 3187 | default: /* checked by xdr code */ |
| 3188 | WARN_ON_ONCE(1); |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 3189 | fallthrough; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3190 | case SP4_SSV: |
Kinglong Mee | 8edf4b0 | 2016-02-26 22:36:42 +0800 | [diff] [blame] | 3191 | status = nfserr_encr_alg_unsupp; |
| 3192 | goto out_nolock; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3193 | } |
| 3194 | |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 3195 | /* Cases below refer to rfc 5661 section 18.35.4: */ |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3196 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 3197 | conf = find_confirmed_client_by_name(&exid->clname, nn); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3198 | if (conf) { |
J. Bruce Fields | 83e08fd | 2012-05-14 09:08:10 -0400 | [diff] [blame] | 3199 | bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred); |
| 3200 | bool verfs_match = same_verf(&verf, &conf->cl_verifier); |
| 3201 | |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3202 | if (update) { |
| 3203 | if (!clp_used_exchangeid(conf)) { /* buggy client */ |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 3204 | status = nfserr_inval; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3205 | goto out; |
| 3206 | } |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 3207 | if (!nfsd4_mach_creds_match(conf, rqstp)) { |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3208 | status = nfserr_wrong_cred; |
| 3209 | goto out; |
| 3210 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3211 | if (!creds_match) { /* case 9 */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3212 | status = nfserr_perm; |
| 3213 | goto out; |
| 3214 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3215 | if (!verfs_match) { /* case 8 */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3216 | status = nfserr_not_same; |
| 3217 | goto out; |
| 3218 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3219 | /* case 6 */ |
| 3220 | exid->flags |= EXCHGID4_FLAG_CONFIRMED_R; |
Chuck Lever | e8f80c5 | 2021-05-14 15:56:19 -0400 | [diff] [blame] | 3221 | trace_nfsd_clid_confirmed_r(conf); |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3222 | goto out_copy; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3223 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3224 | if (!creds_match) { /* case 3 */ |
J. Bruce Fields | 631fc9e | 2012-05-14 15:57:23 -0400 | [diff] [blame] | 3225 | if (client_has_state(conf)) { |
| 3226 | status = nfserr_clid_inuse; |
Chuck Lever | 2778773 | 2021-05-14 15:55:29 -0400 | [diff] [blame] | 3227 | trace_nfsd_clid_cred_mismatch(conf, rqstp); |
J. Bruce Fields | 631fc9e | 2012-05-14 15:57:23 -0400 | [diff] [blame] | 3228 | goto out; |
| 3229 | } |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3230 | goto out_new; |
| 3231 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3232 | if (verfs_match) { /* case 2 */ |
J. Bruce Fields | 0f1ba0e | 2012-05-25 21:24:40 -0400 | [diff] [blame] | 3233 | conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R; |
Chuck Lever | e8f80c5 | 2021-05-14 15:56:19 -0400 | [diff] [blame] | 3234 | trace_nfsd_clid_confirmed_r(conf); |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3235 | goto out_copy; |
| 3236 | } |
| 3237 | /* case 5, client reboot */ |
Chuck Lever | 744ea54 | 2021-05-14 15:55:36 -0400 | [diff] [blame] | 3238 | trace_nfsd_clid_verf_mismatch(conf, rqstp, &verf); |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3239 | conf = NULL; |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3240 | goto out_new; |
Mike Sager | 6ddbbbf | 2009-06-16 04:20:47 +0300 | [diff] [blame] | 3241 | } |
| 3242 | |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 3243 | if (update) { /* case 7 */ |
Mike Sager | 6ddbbbf | 2009-06-16 04:20:47 +0300 | [diff] [blame] | 3244 | status = nfserr_noent; |
| 3245 | goto out; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3246 | } |
| 3247 | |
Chuck Lever | e8f80c5 | 2021-05-14 15:56:19 -0400 | [diff] [blame] | 3248 | unconf = find_unconfirmed_client_by_name(&exid->clname, nn); |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 3249 | if (unconf) /* case 4, possible retry or client restart */ |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3250 | unhash_client_locked(unconf); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3251 | |
Chuck Lever | e8f80c5 | 2021-05-14 15:56:19 -0400 | [diff] [blame] | 3252 | /* case 1, new owner ID */ |
| 3253 | trace_nfsd_clid_fresh(new); |
| 3254 | |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3255 | out_new: |
Jeff Layton | fd699b8 | 2014-07-30 08:27:14 -0400 | [diff] [blame] | 3256 | if (conf) { |
| 3257 | status = mark_client_expired_locked(conf); |
| 3258 | if (status) |
| 3259 | goto out; |
Chuck Lever | 2958d2e | 2021-05-14 15:56:06 -0400 | [diff] [blame] | 3260 | trace_nfsd_clid_replaced(&conf->cl_clientid); |
Jeff Layton | fd699b8 | 2014-07-30 08:27:14 -0400 | [diff] [blame] | 3261 | } |
J. Bruce Fields | 4f540e2 | 2013-05-07 11:57:52 -0400 | [diff] [blame] | 3262 | new->cl_minorversion = cstate->minorversion; |
Andrew Elble | ed94164 | 2016-06-15 12:52:09 -0400 | [diff] [blame] | 3263 | new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0]; |
| 3264 | new->cl_spo_must_allow.u.words[1] = exid->spo_must_allow[1]; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3265 | |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 3266 | add_to_unconfirmed(new); |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3267 | swap(new, conf); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3268 | out_copy: |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 3269 | exid->clientid.cl_boot = conf->cl_clientid.cl_boot; |
| 3270 | exid->clientid.cl_id = conf->cl_clientid.cl_id; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3271 | |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 3272 | exid->seqid = conf->cl_cs_slot.sl_seqid + 1; |
| 3273 | nfsd4_set_ex_flags(conf, exid); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3274 | |
| 3275 | dprintk("nfsd4_exchange_id seqid %d flags %x\n", |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 3276 | conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3277 | status = nfs_ok; |
| 3278 | |
| 3279 | out: |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3280 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | 50c7b94 | 2015-11-23 15:39:12 -0700 | [diff] [blame] | 3281 | out_nolock: |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 3282 | if (new) |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3283 | expire_client(new); |
Chuck Lever | e8f80c5 | 2021-05-14 15:56:19 -0400 | [diff] [blame] | 3284 | if (unconf) { |
| 3285 | trace_nfsd_clid_expire_unconf(&unconf->cl_clientid); |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3286 | expire_client(unconf); |
Chuck Lever | e8f80c5 | 2021-05-14 15:56:19 -0400 | [diff] [blame] | 3287 | } |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3288 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3289 | } |
| 3290 | |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3291 | static __be32 |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 3292 | check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3293 | { |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 3294 | dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid, |
| 3295 | slot_seqid); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3296 | |
| 3297 | /* The slot is in use, and no response has been sent. */ |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 3298 | if (slot_inuse) { |
| 3299 | if (seqid == slot_seqid) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3300 | return nfserr_jukebox; |
| 3301 | else |
| 3302 | return nfserr_seq_misordered; |
| 3303 | } |
J. Bruce Fields | f6d8248 | 2012-02-13 16:13:41 -0500 | [diff] [blame] | 3304 | /* Note unsigned 32-bit arithmetic handles wraparound: */ |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 3305 | if (likely(seqid == slot_seqid + 1)) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3306 | return nfs_ok; |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 3307 | if (seqid == slot_seqid) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3308 | return nfserr_replay_cache; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3309 | return nfserr_seq_misordered; |
| 3310 | } |
| 3311 | |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 3312 | /* |
| 3313 | * Cache the create session result into the create session single DRC |
| 3314 | * slot cache by saving the xdr structure. sl_seqid has been set. |
| 3315 | * Do this for solo or embedded create session operations. |
| 3316 | */ |
| 3317 | static void |
| 3318 | nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses, |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3319 | struct nfsd4_clid_slot *slot, __be32 nfserr) |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 3320 | { |
| 3321 | slot->sl_status = nfserr; |
| 3322 | memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses)); |
| 3323 | } |
| 3324 | |
| 3325 | static __be32 |
| 3326 | nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses, |
| 3327 | struct nfsd4_clid_slot *slot) |
| 3328 | { |
| 3329 | memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses)); |
| 3330 | return slot->sl_status; |
| 3331 | } |
| 3332 | |
Mi Jinlong | 1b74c25 | 2011-07-14 14:50:17 +0800 | [diff] [blame] | 3333 | #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\ |
| 3334 | 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \ |
| 3335 | 1 + /* MIN tag is length with zero, only length */ \ |
| 3336 | 3 + /* version, opcount, opcode */ \ |
| 3337 | XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ |
| 3338 | /* seqid, slotID, slotID, cache */ \ |
| 3339 | 4 ) * sizeof(__be32)) |
| 3340 | |
| 3341 | #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\ |
| 3342 | 2 + /* verifier: AUTH_NULL, length 0 */\ |
| 3343 | 1 + /* status */ \ |
| 3344 | 1 + /* MIN tag is length with zero, only length */ \ |
| 3345 | 3 + /* opcount, opcode, opstatus*/ \ |
| 3346 | XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ |
| 3347 | /* seqid, slotID, slotID, slotID, status */ \ |
| 3348 | 5 ) * sizeof(__be32)) |
| 3349 | |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 3350 | static __be32 check_forechannel_attrs(struct nfsd4_channel_attrs *ca, struct nfsd_net *nn) |
Mi Jinlong | 1b74c25 | 2011-07-14 14:50:17 +0800 | [diff] [blame] | 3351 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 3352 | u32 maxrpc = nn->nfsd_serv->sv_max_mesg; |
| 3353 | |
J. Bruce Fields | 373cd40 | 2013-04-08 15:42:12 -0400 | [diff] [blame] | 3354 | if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ) |
| 3355 | return nfserr_toosmall; |
| 3356 | if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ) |
| 3357 | return nfserr_toosmall; |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 3358 | ca->headerpadsz = 0; |
| 3359 | ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc); |
| 3360 | ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc); |
| 3361 | ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND); |
| 3362 | ca->maxresp_cached = min_t(u32, ca->maxresp_cached, |
| 3363 | NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ); |
| 3364 | ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION); |
| 3365 | /* |
| 3366 | * Note decreasing slot size below client's request may make it |
| 3367 | * difficult for client to function correctly, whereas |
| 3368 | * decreasing the number of slots will (just?) affect |
| 3369 | * performance. When short on memory we therefore prefer to |
| 3370 | * decrease number of slots instead of their size. Clients that |
| 3371 | * request larger slots than they need will get poor results: |
NeilBrown | 7f49fd5d | 2019-09-20 16:33:16 +1000 | [diff] [blame] | 3372 | * Note that we always allow at least one slot, because our |
| 3373 | * accounting is soft and provides no guarantees either way. |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 3374 | */ |
NeilBrown | 2030ca5 | 2019-09-20 16:36:45 +1000 | [diff] [blame] | 3375 | ca->maxreqs = nfsd4_get_drc_mem(ca, nn); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 3376 | |
J. Bruce Fields | 373cd40 | 2013-04-08 15:42:12 -0400 | [diff] [blame] | 3377 | return nfs_ok; |
Mi Jinlong | 1b74c25 | 2011-07-14 14:50:17 +0800 | [diff] [blame] | 3378 | } |
| 3379 | |
Chuck Lever | 4500632 | 2016-03-01 13:06:02 -0500 | [diff] [blame] | 3380 | /* |
| 3381 | * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now. |
| 3382 | * These are based on similar macros in linux/sunrpc/msg_prot.h . |
| 3383 | */ |
| 3384 | #define RPC_MAX_HEADER_WITH_AUTH_SYS \ |
| 3385 | (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK)) |
| 3386 | |
| 3387 | #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \ |
| 3388 | (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK)) |
| 3389 | |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 3390 | #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \ |
Chuck Lever | 4500632 | 2016-03-01 13:06:02 -0500 | [diff] [blame] | 3391 | RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32)) |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 3392 | #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \ |
Chuck Lever | 4500632 | 2016-03-01 13:06:02 -0500 | [diff] [blame] | 3393 | RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \ |
| 3394 | sizeof(__be32)) |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 3395 | |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 3396 | static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca) |
| 3397 | { |
| 3398 | ca->headerpadsz = 0; |
| 3399 | |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 3400 | if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ) |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 3401 | return nfserr_toosmall; |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 3402 | if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ) |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 3403 | return nfserr_toosmall; |
| 3404 | ca->maxresp_cached = 0; |
| 3405 | if (ca->maxops < 2) |
| 3406 | return nfserr_toosmall; |
| 3407 | |
| 3408 | return nfs_ok; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3409 | } |
| 3410 | |
J. Bruce Fields | b78724b | 2013-05-15 17:34:39 -0400 | [diff] [blame] | 3411 | static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs) |
| 3412 | { |
| 3413 | switch (cbs->flavor) { |
| 3414 | case RPC_AUTH_NULL: |
| 3415 | case RPC_AUTH_UNIX: |
| 3416 | return nfs_ok; |
| 3417 | default: |
| 3418 | /* |
| 3419 | * GSS case: the spec doesn't allow us to return this |
| 3420 | * error. But it also doesn't allow us not to support |
| 3421 | * GSS. |
| 3422 | * I'd rather this fail hard than return some error the |
| 3423 | * client might think it can already handle: |
| 3424 | */ |
| 3425 | return nfserr_encr_alg_unsupp; |
| 3426 | } |
| 3427 | } |
| 3428 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3429 | __be32 |
| 3430 | nfsd4_create_session(struct svc_rqst *rqstp, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3431 | struct nfsd4_compound_state *cstate, union nfsd4_op_u *u) |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3432 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3433 | struct nfsd4_create_session *cr_ses = &u->create_session; |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 3434 | struct sockaddr *sa = svc_addr(rqstp); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3435 | struct nfs4_client *conf, *unconf; |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3436 | struct nfs4_client *old = NULL; |
J. Bruce Fields | ac7c46f2 | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 3437 | struct nfsd4_session *new; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3438 | struct nfsd4_conn *conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 3439 | struct nfsd4_clid_slot *cs_slot = NULL; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3440 | __be32 status = 0; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 3441 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3442 | |
Mi Jinlong | a62573d | 2011-03-23 17:57:07 +0800 | [diff] [blame] | 3443 | if (cr_ses->flags & ~SESSION4_FLAG_MASK_A) |
| 3444 | return nfserr_inval; |
J. Bruce Fields | b78724b | 2013-05-15 17:34:39 -0400 | [diff] [blame] | 3445 | status = nfsd4_check_cb_sec(&cr_ses->cb_sec); |
| 3446 | if (status) |
| 3447 | return status; |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 3448 | status = check_forechannel_attrs(&cr_ses->fore_channel, nn); |
J. Bruce Fields | 373cd40 | 2013-04-08 15:42:12 -0400 | [diff] [blame] | 3449 | if (status) |
| 3450 | return status; |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 3451 | status = check_backchannel_attrs(&cr_ses->back_channel); |
| 3452 | if (status) |
Kinglong Mee | f403e45 | 2013-12-23 17:31:21 +0800 | [diff] [blame] | 3453 | goto out_release_drc_mem; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3454 | status = nfserr_jukebox; |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 3455 | new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 3456 | if (!new) |
| 3457 | goto out_release_drc_mem; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3458 | conn = alloc_conn_from_crses(rqstp, cr_ses); |
| 3459 | if (!conn) |
| 3460 | goto out_free_session; |
Mi Jinlong | a62573d | 2011-03-23 17:57:07 +0800 | [diff] [blame] | 3461 | |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3462 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 3463 | unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 3464 | conf = find_confirmed_client(&cr_ses->clientid, true, nn); |
J. Bruce Fields | 7838904 | 2013-03-12 10:12:37 -0400 | [diff] [blame] | 3465 | WARN_ON_ONCE(conf && unconf); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3466 | |
| 3467 | if (conf) { |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3468 | status = nfserr_wrong_cred; |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 3469 | if (!nfsd4_mach_creds_match(conf, rqstp)) |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3470 | goto out_free_conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 3471 | cs_slot = &conf->cl_cs_slot; |
| 3472 | status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); |
Kinglong Mee | f5e22bb | 2015-07-13 17:32:34 +0800 | [diff] [blame] | 3473 | if (status) { |
| 3474 | if (status == nfserr_replay_cache) |
| 3475 | status = nfsd4_replay_create_session(cr_ses, cs_slot); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3476 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3477 | } |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3478 | } else if (unconf) { |
Chuck Lever | 2778773 | 2021-05-14 15:55:29 -0400 | [diff] [blame] | 3479 | status = nfserr_clid_inuse; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3480 | if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) || |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 3481 | !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) { |
Chuck Lever | 2778773 | 2021-05-14 15:55:29 -0400 | [diff] [blame] | 3482 | trace_nfsd_clid_cred_mismatch(unconf, rqstp); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3483 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3484 | } |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3485 | status = nfserr_wrong_cred; |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 3486 | if (!nfsd4_mach_creds_match(unconf, rqstp)) |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3487 | goto out_free_conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 3488 | cs_slot = &unconf->cl_cs_slot; |
| 3489 | status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); |
Andy Adamson | 38eb76a | 2009-04-03 08:28:32 +0300 | [diff] [blame] | 3490 | if (status) { |
| 3491 | /* an unconfirmed replay returns misordered */ |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3492 | status = nfserr_seq_misordered; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3493 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3494 | } |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 3495 | old = find_confirmed_client_by_name(&unconf->cl_name, nn); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3496 | if (old) { |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3497 | status = mark_client_expired_locked(old); |
Jeff Layton | 7abea1e | 2014-07-30 08:27:13 -0400 | [diff] [blame] | 3498 | if (status) { |
| 3499 | old = NULL; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3500 | goto out_free_conn; |
Jeff Layton | 7abea1e | 2014-07-30 08:27:13 -0400 | [diff] [blame] | 3501 | } |
Chuck Lever | 2958d2e | 2021-05-14 15:56:06 -0400 | [diff] [blame] | 3502 | trace_nfsd_clid_replaced(&old->cl_clientid); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3503 | } |
J. Bruce Fields | 8f9d3d3 | 2012-09-12 14:41:31 -0400 | [diff] [blame] | 3504 | move_to_confirmed(unconf); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3505 | conf = unconf; |
| 3506 | } else { |
| 3507 | status = nfserr_stale_clientid; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3508 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3509 | } |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3510 | status = nfs_ok; |
Chuck Lever | 4ce85c8 | 2016-03-01 13:05:27 -0500 | [diff] [blame] | 3511 | /* Persistent sessions are not supported */ |
J. Bruce Fields | 408b79b | 2010-04-15 15:11:09 -0400 | [diff] [blame] | 3512 | cr_ses->flags &= ~SESSION4_PERSIST; |
Chuck Lever | 4ce85c8 | 2016-03-01 13:05:27 -0500 | [diff] [blame] | 3513 | /* Upshifting from TCP to RDMA is not supported */ |
J. Bruce Fields | 408b79b | 2010-04-15 15:11:09 -0400 | [diff] [blame] | 3514 | cr_ses->flags &= ~SESSION4_RDMA; |
| 3515 | |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3516 | init_session(rqstp, new, conf, cr_ses); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3517 | nfsd4_get_session_locked(new); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3518 | |
J. Bruce Fields | ac7c46f2 | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 3519 | memcpy(cr_ses->sessionid.data, new->se_sessionid.data, |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3520 | NFS4_MAX_SESSIONID_LEN); |
J. Bruce Fields | 86c3e16 | 2010-10-02 17:04:00 -0400 | [diff] [blame] | 3521 | cs_slot->sl_seqid++; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 3522 | cr_ses->seqid = cs_slot->sl_seqid; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3523 | |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3524 | /* cache solo and embedded create sessions under the client_lock */ |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 3525 | nfsd4_cache_create_session(cr_ses, cs_slot, status); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3526 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | 934bd07 | 2021-05-25 14:53:44 -0400 | [diff] [blame] | 3527 | if (conf == unconf) |
| 3528 | fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3529 | /* init connection and backchannel */ |
| 3530 | nfsd4_init_conn(rqstp, conn, new); |
| 3531 | nfsd4_put_session(new); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3532 | if (old) |
| 3533 | expire_client(old); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3534 | return status; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3535 | out_free_conn: |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3536 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3537 | free_conn(conn); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3538 | if (old) |
| 3539 | expire_client(old); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3540 | out_free_session: |
| 3541 | __free_session(new); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 3542 | out_release_drc_mem: |
| 3543 | nfsd4_put_drc_mem(&cr_ses->fore_channel); |
J. Bruce Fields | 1ca5079 | 2013-03-14 18:12:03 -0400 | [diff] [blame] | 3544 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3545 | } |
| 3546 | |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 3547 | static __be32 nfsd4_map_bcts_dir(u32 *dir) |
| 3548 | { |
| 3549 | switch (*dir) { |
| 3550 | case NFS4_CDFC4_FORE: |
| 3551 | case NFS4_CDFC4_BACK: |
| 3552 | return nfs_ok; |
| 3553 | case NFS4_CDFC4_FORE_OR_BOTH: |
| 3554 | case NFS4_CDFC4_BACK_OR_BOTH: |
| 3555 | *dir = NFS4_CDFC4_BOTH; |
| 3556 | return nfs_ok; |
zhengbin | fc5fc5d | 2019-12-19 17:29:20 +0800 | [diff] [blame] | 3557 | } |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 3558 | return nfserr_inval; |
| 3559 | } |
| 3560 | |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3561 | __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, |
| 3562 | struct nfsd4_compound_state *cstate, |
| 3563 | union nfsd4_op_u *u) |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 3564 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3565 | struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl; |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 3566 | struct nfsd4_session *session = cstate->session; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3567 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
J. Bruce Fields | b78724b | 2013-05-15 17:34:39 -0400 | [diff] [blame] | 3568 | __be32 status; |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 3569 | |
J. Bruce Fields | b78724b | 2013-05-15 17:34:39 -0400 | [diff] [blame] | 3570 | status = nfsd4_check_cb_sec(&bc->bc_cb_sec); |
| 3571 | if (status) |
| 3572 | return status; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3573 | spin_lock(&nn->client_lock); |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 3574 | session->se_cb_prog = bc->bc_cb_program; |
| 3575 | session->se_cb_sec = bc->bc_cb_sec; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3576 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 3577 | |
| 3578 | nfsd4_probe_callback(session->se_client); |
| 3579 | |
| 3580 | return nfs_ok; |
| 3581 | } |
| 3582 | |
J. Bruce Fields | c2d715a | 2020-04-27 17:59:01 -0400 | [diff] [blame] | 3583 | static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s) |
| 3584 | { |
| 3585 | struct nfsd4_conn *c; |
| 3586 | |
| 3587 | list_for_each_entry(c, &s->se_conns, cn_persession) { |
| 3588 | if (c->cn_xprt == xpt) { |
| 3589 | return c; |
| 3590 | } |
| 3591 | } |
| 3592 | return NULL; |
| 3593 | } |
| 3594 | |
| 3595 | static __be32 nfsd4_match_existing_connection(struct svc_rqst *rqst, |
Dai Ngo | 02579b2 | 2021-09-16 14:22:12 -0400 | [diff] [blame] | 3596 | struct nfsd4_session *session, u32 req, struct nfsd4_conn **conn) |
J. Bruce Fields | c2d715a | 2020-04-27 17:59:01 -0400 | [diff] [blame] | 3597 | { |
| 3598 | struct nfs4_client *clp = session->se_client; |
| 3599 | struct svc_xprt *xpt = rqst->rq_xprt; |
| 3600 | struct nfsd4_conn *c; |
| 3601 | __be32 status; |
| 3602 | |
| 3603 | /* Following the last paragraph of RFC 5661 Section 18.34.3: */ |
| 3604 | spin_lock(&clp->cl_lock); |
| 3605 | c = __nfsd4_find_conn(xpt, session); |
| 3606 | if (!c) |
| 3607 | status = nfserr_noent; |
| 3608 | else if (req == c->cn_flags) |
| 3609 | status = nfs_ok; |
| 3610 | else if (req == NFS4_CDFC4_FORE_OR_BOTH && |
| 3611 | c->cn_flags != NFS4_CDFC4_BACK) |
| 3612 | status = nfs_ok; |
| 3613 | else if (req == NFS4_CDFC4_BACK_OR_BOTH && |
| 3614 | c->cn_flags != NFS4_CDFC4_FORE) |
| 3615 | status = nfs_ok; |
| 3616 | else |
| 3617 | status = nfserr_inval; |
| 3618 | spin_unlock(&clp->cl_lock); |
Dai Ngo | 02579b2 | 2021-09-16 14:22:12 -0400 | [diff] [blame] | 3619 | if (status == nfs_ok && conn) |
| 3620 | *conn = c; |
J. Bruce Fields | c2d715a | 2020-04-27 17:59:01 -0400 | [diff] [blame] | 3621 | return status; |
| 3622 | } |
| 3623 | |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 3624 | __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp, |
| 3625 | struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3626 | union nfsd4_op_u *u) |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 3627 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3628 | struct nfsd4_bind_conn_to_session *bcts = &u->bind_conn_to_session; |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 3629 | __be32 status; |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 3630 | struct nfsd4_conn *conn; |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 3631 | struct nfsd4_session *session; |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3632 | struct net *net = SVC_NET(rqstp); |
| 3633 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 3634 | |
| 3635 | if (!nfsd4_last_compound_op(rqstp)) |
| 3636 | return nfserr_not_only_op; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3637 | spin_lock(&nn->client_lock); |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3638 | session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3639 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 3640 | if (!session) |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3641 | goto out_no_session; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3642 | status = nfserr_wrong_cred; |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 3643 | if (!nfsd4_mach_creds_match(session->se_client, rqstp)) |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3644 | goto out; |
Dai Ngo | 02579b2 | 2021-09-16 14:22:12 -0400 | [diff] [blame] | 3645 | status = nfsd4_match_existing_connection(rqstp, session, |
| 3646 | bcts->dir, &conn); |
| 3647 | if (status == nfs_ok) { |
| 3648 | if (bcts->dir == NFS4_CDFC4_FORE_OR_BOTH || |
| 3649 | bcts->dir == NFS4_CDFC4_BACK) |
| 3650 | conn->cn_flags |= NFS4_CDFC4_BACK; |
| 3651 | nfsd4_probe_callback(session->se_client); |
| 3652 | goto out; |
| 3653 | } |
| 3654 | if (status == nfserr_inval) |
J. Bruce Fields | c2d715a | 2020-04-27 17:59:01 -0400 | [diff] [blame] | 3655 | goto out; |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 3656 | status = nfsd4_map_bcts_dir(&bcts->dir); |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 3657 | if (status) |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 3658 | goto out; |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 3659 | conn = alloc_conn(rqstp, bcts->dir); |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 3660 | status = nfserr_jukebox; |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 3661 | if (!conn) |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 3662 | goto out; |
| 3663 | nfsd4_init_conn(rqstp, conn, session); |
| 3664 | status = nfs_ok; |
| 3665 | out: |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3666 | nfsd4_put_session(session); |
| 3667 | out_no_session: |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 3668 | return status; |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 3669 | } |
| 3670 | |
J. Bruce Fields | 665d507 | 2018-04-11 16:53:36 -0400 | [diff] [blame] | 3671 | static bool nfsd4_compound_in_session(struct nfsd4_compound_state *cstate, struct nfs4_sessionid *sid) |
J. Bruce Fields | 5d4cec2 | 2010-05-01 12:56:06 -0400 | [diff] [blame] | 3672 | { |
J. Bruce Fields | 665d507 | 2018-04-11 16:53:36 -0400 | [diff] [blame] | 3673 | if (!cstate->session) |
Fengguang Wu | 51d87bc | 2018-03-22 13:37:20 +0800 | [diff] [blame] | 3674 | return false; |
J. Bruce Fields | 665d507 | 2018-04-11 16:53:36 -0400 | [diff] [blame] | 3675 | return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid)); |
J. Bruce Fields | 5d4cec2 | 2010-05-01 12:56:06 -0400 | [diff] [blame] | 3676 | } |
| 3677 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3678 | __be32 |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3679 | nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate, |
| 3680 | union nfsd4_op_u *u) |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3681 | { |
J. Bruce Fields | ca0552f | 2018-04-11 17:01:53 -0400 | [diff] [blame] | 3682 | struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid; |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 3683 | struct nfsd4_session *ses; |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 3684 | __be32 status; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 3685 | int ref_held_by_me = 0; |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3686 | struct net *net = SVC_NET(r); |
| 3687 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 3688 | |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 3689 | status = nfserr_not_only_op; |
J. Bruce Fields | ca0552f | 2018-04-11 17:01:53 -0400 | [diff] [blame] | 3690 | if (nfsd4_compound_in_session(cstate, sessionid)) { |
J. Bruce Fields | 5771635 | 2010-04-21 12:27:19 -0400 | [diff] [blame] | 3691 | if (!nfsd4_last_compound_op(r)) |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 3692 | goto out; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 3693 | ref_held_by_me++; |
J. Bruce Fields | 5771635 | 2010-04-21 12:27:19 -0400 | [diff] [blame] | 3694 | } |
J. Bruce Fields | ca0552f | 2018-04-11 17:01:53 -0400 | [diff] [blame] | 3695 | dump_sessionid(__func__, sessionid); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3696 | spin_lock(&nn->client_lock); |
J. Bruce Fields | ca0552f | 2018-04-11 17:01:53 -0400 | [diff] [blame] | 3697 | ses = find_in_sessionid_hashtbl(sessionid, net, &status); |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 3698 | if (!ses) |
| 3699 | goto out_client_lock; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3700 | status = nfserr_wrong_cred; |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 3701 | if (!nfsd4_mach_creds_match(ses->se_client, r)) |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3702 | goto out_put_session; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 3703 | status = mark_session_dead_locked(ses, 1 + ref_held_by_me); |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 3704 | if (status) |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 3705 | goto out_put_session; |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 3706 | unhash_session(ses); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3707 | spin_unlock(&nn->client_lock); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 3708 | |
J. Bruce Fields | 84f5f7c | 2010-12-09 15:52:19 -0500 | [diff] [blame] | 3709 | nfsd4_probe_callback_sync(ses->se_client); |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 3710 | |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3711 | spin_lock(&nn->client_lock); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 3712 | status = nfs_ok; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 3713 | out_put_session: |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3714 | nfsd4_put_session_locked(ses); |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 3715 | out_client_lock: |
| 3716 | spin_unlock(&nn->client_lock); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 3717 | out: |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 3718 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3719 | } |
| 3720 | |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3721 | static __be32 nfsd4_sequence_check_conn(struct nfsd4_conn *new, struct nfsd4_session *ses) |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 3722 | { |
| 3723 | struct nfs4_client *clp = ses->se_client; |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 3724 | struct nfsd4_conn *c; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3725 | __be32 status = nfs_ok; |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 3726 | int ret; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 3727 | |
| 3728 | spin_lock(&clp->cl_lock); |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 3729 | c = __nfsd4_find_conn(new->cn_xprt, ses); |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3730 | if (c) |
| 3731 | goto out_free; |
| 3732 | status = nfserr_conn_not_bound_to_session; |
| 3733 | if (clp->cl_mach_cred) |
| 3734 | goto out_free; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 3735 | __nfsd4_hash_conn(new, ses); |
| 3736 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 3737 | ret = nfsd4_register_conn(new); |
| 3738 | if (ret) |
| 3739 | /* oops; xprt is already down: */ |
| 3740 | nfsd4_conn_lost(&new->cn_xpt_user); |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3741 | return nfs_ok; |
| 3742 | out_free: |
| 3743 | spin_unlock(&clp->cl_lock); |
| 3744 | free_conn(new); |
| 3745 | return status; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 3746 | } |
| 3747 | |
Mi Jinlong | 868b89c | 2011-04-27 09:09:58 +0800 | [diff] [blame] | 3748 | static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session) |
| 3749 | { |
| 3750 | struct nfsd4_compoundargs *args = rqstp->rq_argp; |
| 3751 | |
| 3752 | return args->opcnt > session->se_fchannel.maxops; |
| 3753 | } |
| 3754 | |
Mi Jinlong | ae82a8d | 2011-07-14 14:56:02 +0800 | [diff] [blame] | 3755 | static bool nfsd4_request_too_big(struct svc_rqst *rqstp, |
| 3756 | struct nfsd4_session *session) |
| 3757 | { |
| 3758 | struct xdr_buf *xb = &rqstp->rq_arg; |
| 3759 | |
| 3760 | return xb->len > session->se_fchannel.maxreq_sz; |
| 3761 | } |
| 3762 | |
J. Bruce Fields | 53da6a5 | 2017-10-17 20:38:49 -0400 | [diff] [blame] | 3763 | static bool replay_matches_cache(struct svc_rqst *rqstp, |
| 3764 | struct nfsd4_sequence *seq, struct nfsd4_slot *slot) |
| 3765 | { |
| 3766 | struct nfsd4_compoundargs *argp = rqstp->rq_argp; |
| 3767 | |
| 3768 | if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) != |
| 3769 | (bool)seq->cachethis) |
| 3770 | return false; |
| 3771 | /* |
Scott Mayhew | 6e73e92 | 2019-10-09 15:11:37 -0400 | [diff] [blame] | 3772 | * If there's an error then the reply can have fewer ops than |
| 3773 | * the call. |
J. Bruce Fields | 53da6a5 | 2017-10-17 20:38:49 -0400 | [diff] [blame] | 3774 | */ |
Scott Mayhew | 6e73e92 | 2019-10-09 15:11:37 -0400 | [diff] [blame] | 3775 | if (slot->sl_opcnt < argp->opcnt && !slot->sl_status) |
| 3776 | return false; |
| 3777 | /* |
| 3778 | * But if we cached a reply with *more* ops than the call you're |
| 3779 | * sending us now, then this new call is clearly not really a |
| 3780 | * replay of the old one: |
| 3781 | */ |
| 3782 | if (slot->sl_opcnt > argp->opcnt) |
J. Bruce Fields | 53da6a5 | 2017-10-17 20:38:49 -0400 | [diff] [blame] | 3783 | return false; |
| 3784 | /* This is the only check explicitly called by spec: */ |
| 3785 | if (!same_creds(&rqstp->rq_cred, &slot->sl_cred)) |
| 3786 | return false; |
| 3787 | /* |
| 3788 | * There may be more comparisons we could actually do, but the |
| 3789 | * spec doesn't require us to catch every case where the calls |
| 3790 | * don't match (that would require caching the call as well as |
| 3791 | * the reply), so we don't bother. |
| 3792 | */ |
| 3793 | return true; |
| 3794 | } |
| 3795 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3796 | __be32 |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3797 | nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 3798 | union nfsd4_op_u *u) |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3799 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3800 | struct nfsd4_sequence *seq = &u->sequence; |
Andy Adamson | f9bb94c4 | 2009-04-03 08:28:12 +0300 | [diff] [blame] | 3801 | struct nfsd4_compoundres *resp = rqstp->rq_resp; |
Chuck Lever | bddfdbc | 2020-10-27 15:53:42 -0400 | [diff] [blame] | 3802 | struct xdr_stream *xdr = resp->xdr; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3803 | struct nfsd4_session *session; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3804 | struct nfs4_client *clp; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3805 | struct nfsd4_slot *slot; |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 3806 | struct nfsd4_conn *conn; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3807 | __be32 status; |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 3808 | int buflen; |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3809 | struct net *net = SVC_NET(rqstp); |
| 3810 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3811 | |
Andy Adamson | f9bb94c4 | 2009-04-03 08:28:12 +0300 | [diff] [blame] | 3812 | if (resp->opcnt != 1) |
| 3813 | return nfserr_sequence_pos; |
| 3814 | |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 3815 | /* |
| 3816 | * Will be either used or freed by nfsd4_sequence_check_conn |
| 3817 | * below. |
| 3818 | */ |
| 3819 | conn = alloc_conn(rqstp, NFS4_CDFC4_FORE); |
| 3820 | if (!conn) |
| 3821 | return nfserr_jukebox; |
| 3822 | |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3823 | spin_lock(&nn->client_lock); |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3824 | session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3825 | if (!session) |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3826 | goto out_no_session; |
| 3827 | clp = session->se_client; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3828 | |
Mi Jinlong | 868b89c | 2011-04-27 09:09:58 +0800 | [diff] [blame] | 3829 | status = nfserr_too_many_ops; |
| 3830 | if (nfsd4_session_too_many_ops(rqstp, session)) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 3831 | goto out_put_session; |
Mi Jinlong | 868b89c | 2011-04-27 09:09:58 +0800 | [diff] [blame] | 3832 | |
Mi Jinlong | ae82a8d | 2011-07-14 14:56:02 +0800 | [diff] [blame] | 3833 | status = nfserr_req_too_big; |
| 3834 | if (nfsd4_request_too_big(rqstp, session)) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 3835 | goto out_put_session; |
Mi Jinlong | ae82a8d | 2011-07-14 14:56:02 +0800 | [diff] [blame] | 3836 | |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3837 | status = nfserr_badslot; |
Alexandros Batsakis | 6c18ba9 | 2009-06-16 04:19:13 +0300 | [diff] [blame] | 3838 | if (seq->slotid >= session->se_fchannel.maxreqs) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 3839 | goto out_put_session; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3840 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 3841 | slot = session->se_slots[seq->slotid]; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3842 | dprintk("%s: slotid %d\n", __func__, seq->slotid); |
| 3843 | |
Andy Adamson | a8dfdae | 2009-08-28 08:45:02 -0400 | [diff] [blame] | 3844 | /* We do not negotiate the number of slots yet, so set the |
| 3845 | * maxslots to the session maxreqs which is used to encode |
| 3846 | * sr_highest_slotid and the sr_target_slot id to maxslots */ |
| 3847 | seq->maxslots = session->se_fchannel.maxreqs; |
| 3848 | |
J. Bruce Fields | 73e7948 | 2012-02-13 16:39:00 -0500 | [diff] [blame] | 3849 | status = check_slot_seqid(seq->seqid, slot->sl_seqid, |
| 3850 | slot->sl_flags & NFSD4_SLOT_INUSE); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3851 | if (status == nfserr_replay_cache) { |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 3852 | status = nfserr_seq_misordered; |
| 3853 | if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED)) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 3854 | goto out_put_session; |
J. Bruce Fields | 53da6a5 | 2017-10-17 20:38:49 -0400 | [diff] [blame] | 3855 | status = nfserr_seq_false_retry; |
| 3856 | if (!replay_matches_cache(rqstp, seq, slot)) |
| 3857 | goto out_put_session; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3858 | cstate->slot = slot; |
| 3859 | cstate->session = session; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3860 | cstate->clp = clp; |
Andy Adamson | da3846a | 2009-04-03 08:28:22 +0300 | [diff] [blame] | 3861 | /* Return the cached reply status and set cstate->status |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 3862 | * for nfsd4_proc_compound processing */ |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 3863 | status = nfsd4_replay_cache_entry(resp, seq); |
Andy Adamson | da3846a | 2009-04-03 08:28:22 +0300 | [diff] [blame] | 3864 | cstate->status = nfserr_replay_cache; |
Benny Halevy | aaf84eb | 2009-08-20 03:21:56 +0300 | [diff] [blame] | 3865 | goto out; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3866 | } |
| 3867 | if (status) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 3868 | goto out_put_session; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3869 | |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3870 | status = nfsd4_sequence_check_conn(conn, session); |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 3871 | conn = NULL; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3872 | if (status) |
| 3873 | goto out_put_session; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 3874 | |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 3875 | buflen = (seq->cachethis) ? |
| 3876 | session->se_fchannel.maxresp_cached : |
| 3877 | session->se_fchannel.maxresp_sz; |
| 3878 | status = (seq->cachethis) ? nfserr_rep_too_big_to_cache : |
| 3879 | nfserr_rep_too_big; |
J. Bruce Fields | a5cddc8 | 2014-05-12 18:10:58 -0400 | [diff] [blame] | 3880 | if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack)) |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 3881 | goto out_put_session; |
J. Bruce Fields | 32aaa62 | 2014-03-20 20:47:41 -0400 | [diff] [blame] | 3882 | svc_reserve(rqstp, buflen); |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 3883 | |
| 3884 | status = nfs_ok; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3885 | /* Success! bump slot seqid */ |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3886 | slot->sl_seqid = seq->seqid; |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 3887 | slot->sl_flags |= NFSD4_SLOT_INUSE; |
J. Bruce Fields | 73e7948 | 2012-02-13 16:39:00 -0500 | [diff] [blame] | 3888 | if (seq->cachethis) |
| 3889 | slot->sl_flags |= NFSD4_SLOT_CACHETHIS; |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 3890 | else |
| 3891 | slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3892 | |
| 3893 | cstate->slot = slot; |
| 3894 | cstate->session = session; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3895 | cstate->clp = clp; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3896 | |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3897 | out: |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3898 | switch (clp->cl_cb_state) { |
| 3899 | case NFSD4_CB_DOWN: |
| 3900 | seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN; |
| 3901 | break; |
| 3902 | case NFSD4_CB_FAULT: |
| 3903 | seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT; |
| 3904 | break; |
| 3905 | default: |
| 3906 | seq->status_flags = 0; |
Benny Halevy | aaf84eb | 2009-08-20 03:21:56 +0300 | [diff] [blame] | 3907 | } |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 3908 | if (!list_empty(&clp->cl_revoked)) |
| 3909 | seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3910 | out_no_session: |
Kinglong Mee | 3f42d2c | 2014-03-24 11:56:59 +0800 | [diff] [blame] | 3911 | if (conn) |
| 3912 | free_conn(conn); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3913 | spin_unlock(&nn->client_lock); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3914 | return status; |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 3915 | out_put_session: |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3916 | nfsd4_put_session_locked(session); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3917 | goto out_no_session; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3918 | } |
| 3919 | |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 3920 | void |
| 3921 | nfsd4_sequence_done(struct nfsd4_compoundres *resp) |
| 3922 | { |
| 3923 | struct nfsd4_compound_state *cs = &resp->cstate; |
| 3924 | |
| 3925 | if (nfsd4_has_session(cs)) { |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 3926 | if (cs->status != nfserr_replay_cache) { |
| 3927 | nfsd4_store_cache_entry(resp); |
| 3928 | cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE; |
| 3929 | } |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3930 | /* Drop session reference that was taken in nfsd4_sequence() */ |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 3931 | nfsd4_put_session(cs->session); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3932 | } else if (cs->clp) |
| 3933 | put_client_renew(cs->clp); |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 3934 | } |
| 3935 | |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3936 | __be32 |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3937 | nfsd4_destroy_clientid(struct svc_rqst *rqstp, |
| 3938 | struct nfsd4_compound_state *cstate, |
| 3939 | union nfsd4_op_u *u) |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3940 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3941 | struct nfsd4_destroy_clientid *dc = &u->destroy_clientid; |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3942 | struct nfs4_client *conf, *unconf; |
| 3943 | struct nfs4_client *clp = NULL; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3944 | __be32 status = 0; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 3945 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3946 | |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3947 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 3948 | unconf = find_unconfirmed_client(&dc->clientid, true, nn); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 3949 | conf = find_confirmed_client(&dc->clientid, true, nn); |
J. Bruce Fields | 7838904 | 2013-03-12 10:12:37 -0400 | [diff] [blame] | 3950 | WARN_ON_ONCE(conf && unconf); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3951 | |
| 3952 | if (conf) { |
J. Bruce Fields | c0293b0 | 2013-03-14 18:20:01 -0400 | [diff] [blame] | 3953 | if (client_has_state(conf)) { |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3954 | status = nfserr_clientid_busy; |
| 3955 | goto out; |
| 3956 | } |
Jeff Layton | fd699b8 | 2014-07-30 08:27:14 -0400 | [diff] [blame] | 3957 | status = mark_client_expired_locked(conf); |
| 3958 | if (status) |
| 3959 | goto out; |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3960 | clp = conf; |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3961 | } else if (unconf) |
| 3962 | clp = unconf; |
| 3963 | else { |
| 3964 | status = nfserr_stale_clientid; |
| 3965 | goto out; |
| 3966 | } |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 3967 | if (!nfsd4_mach_creds_match(clp, rqstp)) { |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3968 | clp = NULL; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3969 | status = nfserr_wrong_cred; |
| 3970 | goto out; |
| 3971 | } |
Chuck Lever | c41a9b7 | 2021-05-14 15:56:00 -0400 | [diff] [blame] | 3972 | trace_nfsd_clid_destroyed(&clp->cl_clientid); |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3973 | unhash_client_locked(clp); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3974 | out: |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3975 | spin_unlock(&nn->client_lock); |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3976 | if (clp) |
| 3977 | expire_client(clp); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3978 | return status; |
| 3979 | } |
| 3980 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3981 | __be32 |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3982 | nfsd4_reclaim_complete(struct svc_rqst *rqstp, |
| 3983 | struct nfsd4_compound_state *cstate, union nfsd4_op_u *u) |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 3984 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3985 | struct nfsd4_reclaim_complete *rc = &u->reclaim_complete; |
J. Bruce Fields | ec59659 | 2021-01-21 17:57:45 -0500 | [diff] [blame] | 3986 | struct nfs4_client *clp = cstate->clp; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3987 | __be32 status = 0; |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3988 | |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 3989 | if (rc->rca_one_fs) { |
| 3990 | if (!cstate->current_fh.fh_dentry) |
| 3991 | return nfserr_nofilehandle; |
| 3992 | /* |
| 3993 | * We don't take advantage of the rca_one_fs case. |
| 3994 | * That's OK, it's optional, we can safely ignore it. |
| 3995 | */ |
Christophe JAILLET | d28c442 | 2016-07-02 08:24:32 +0200 | [diff] [blame] | 3996 | return nfs_ok; |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 3997 | } |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3998 | |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3999 | status = nfserr_complete_already; |
J. Bruce Fields | ec59659 | 2021-01-21 17:57:45 -0500 | [diff] [blame] | 4000 | if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags)) |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 4001 | goto out; |
| 4002 | |
| 4003 | status = nfserr_stale_clientid; |
J. Bruce Fields | ec59659 | 2021-01-21 17:57:45 -0500 | [diff] [blame] | 4004 | if (is_client_expired(clp)) |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 4005 | /* |
| 4006 | * The following error isn't really legal. |
| 4007 | * But we only get here if the client just explicitly |
| 4008 | * destroyed the client. Surely it no longer cares what |
| 4009 | * error it gets back on an operation for the dead |
| 4010 | * client. |
| 4011 | */ |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 4012 | goto out; |
| 4013 | |
| 4014 | status = nfs_ok; |
Chuck Lever | cee8aa0 | 2021-05-14 15:55:54 -0400 | [diff] [blame] | 4015 | trace_nfsd_clid_reclaim_complete(&clp->cl_clientid); |
J. Bruce Fields | ec59659 | 2021-01-21 17:57:45 -0500 | [diff] [blame] | 4016 | nfsd4_client_record_create(clp); |
| 4017 | inc_reclaim_complete(clp); |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 4018 | out: |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 4019 | return status; |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 4020 | } |
| 4021 | |
| 4022 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 4023 | nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 4024 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4025 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 4026 | struct nfsd4_setclientid *setclid = &u->setclientid; |
J. Bruce Fields | a084daf | 2011-10-10 15:07:40 -0400 | [diff] [blame] | 4027 | struct xdr_netobj clname = setclid->se_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4028 | nfs4_verifier clverifier = setclid->se_verf; |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 4029 | struct nfs4_client *conf, *new; |
| 4030 | struct nfs4_client *unconf = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4031 | __be32 status; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 4032 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
| 4033 | |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 4034 | new = create_client(clname, rqstp, &clverifier); |
| 4035 | if (new == NULL) |
| 4036 | return nfserr_jukebox; |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 4037 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 4038 | conf = find_confirmed_client_by_name(&clname, nn); |
J. Bruce Fields | 2b63482 | 2015-10-15 15:33:23 -0400 | [diff] [blame] | 4039 | if (conf && client_has_state(conf)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4040 | status = nfserr_clid_inuse; |
J. Bruce Fields | e203d50 | 2010-11-24 17:30:54 -0500 | [diff] [blame] | 4041 | if (clp_used_exchangeid(conf)) |
| 4042 | goto out; |
J. Bruce Fields | 026722c | 2009-03-18 15:06:26 -0400 | [diff] [blame] | 4043 | if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) { |
Chuck Lever | 2778773 | 2021-05-14 15:55:29 -0400 | [diff] [blame] | 4044 | trace_nfsd_clid_cred_mismatch(conf, rqstp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4045 | goto out; |
| 4046 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4047 | } |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 4048 | unconf = find_unconfirmed_client_by_name(&clname, nn); |
J. Bruce Fields | 8f93071 | 2012-05-18 22:06:41 -0400 | [diff] [blame] | 4049 | if (unconf) |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 4050 | unhash_client_locked(unconf); |
Chuck Lever | 744ea54 | 2021-05-14 15:55:36 -0400 | [diff] [blame] | 4051 | if (conf) { |
| 4052 | if (same_verf(&conf->cl_verifier, &clverifier)) { |
| 4053 | copy_clid(new, conf); |
| 4054 | gen_confirm(new, nn); |
| 4055 | } else |
| 4056 | trace_nfsd_clid_verf_mismatch(conf, rqstp, |
| 4057 | &clverifier); |
Chuck Lever | 237f91c | 2021-05-14 15:56:13 -0400 | [diff] [blame] | 4058 | } else |
| 4059 | trace_nfsd_clid_fresh(new); |
J. Bruce Fields | 8323c3b | 2010-10-19 19:36:51 -0400 | [diff] [blame] | 4060 | new->cl_minorversion = 0; |
Takuma Umeya | 6f3d772 | 2010-12-15 14:09:01 +0900 | [diff] [blame] | 4061 | gen_callback(new, setclid, rqstp); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 4062 | add_to_unconfirmed(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4063 | setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot; |
| 4064 | setclid->se_clientid.cl_id = new->cl_clientid.cl_id; |
| 4065 | memcpy(setclid->se_confirm.data, new->cl_confirm.data, sizeof(setclid->se_confirm.data)); |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 4066 | new = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4067 | status = nfs_ok; |
| 4068 | out: |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 4069 | spin_unlock(&nn->client_lock); |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 4070 | if (new) |
| 4071 | free_client(new); |
Chuck Lever | 237f91c | 2021-05-14 15:56:13 -0400 | [diff] [blame] | 4072 | if (unconf) { |
| 4073 | trace_nfsd_clid_expire_unconf(&unconf->cl_clientid); |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 4074 | expire_client(unconf); |
Chuck Lever | 237f91c | 2021-05-14 15:56:13 -0400 | [diff] [blame] | 4075 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4076 | return status; |
| 4077 | } |
| 4078 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4079 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 4080 | nfsd4_setclientid_confirm(struct svc_rqst *rqstp, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 4081 | struct nfsd4_compound_state *cstate, |
| 4082 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4083 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 4084 | struct nfsd4_setclientid_confirm *setclientid_confirm = |
| 4085 | &u->setclientid_confirm; |
NeilBrown | 21ab45a | 2005-06-23 22:04:14 -0700 | [diff] [blame] | 4086 | struct nfs4_client *conf, *unconf; |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 4087 | struct nfs4_client *old = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4088 | nfs4_verifier confirm = setclientid_confirm->sc_confirm; |
| 4089 | clientid_t * clid = &setclientid_confirm->sc_clientid; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4090 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 4091 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4092 | |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 4093 | if (STALE_CLIENTID(clid, nn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4094 | return nfserr_stale_clientid; |
NeilBrown | 21ab45a | 2005-06-23 22:04:14 -0700 | [diff] [blame] | 4095 | |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 4096 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 4097 | conf = find_confirmed_client(clid, false, nn); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 4098 | unconf = find_unconfirmed_client(clid, false, nn); |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 4099 | /* |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 4100 | * We try hard to give out unique clientid's, so if we get an |
| 4101 | * attempt to confirm the same clientid with a different cred, |
J. Bruce Fields | f984a7c | 2015-09-01 13:40:53 -0400 | [diff] [blame] | 4102 | * the client may be buggy; this should never happen. |
| 4103 | * |
| 4104 | * Nevertheless, RFC 7530 recommends INUSE for this case: |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 4105 | */ |
J. Bruce Fields | f984a7c | 2015-09-01 13:40:53 -0400 | [diff] [blame] | 4106 | status = nfserr_clid_inuse; |
Chuck Lever | 2778773 | 2021-05-14 15:55:29 -0400 | [diff] [blame] | 4107 | if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred)) { |
| 4108 | trace_nfsd_clid_cred_mismatch(unconf, rqstp); |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 4109 | goto out; |
Chuck Lever | 2778773 | 2021-05-14 15:55:29 -0400 | [diff] [blame] | 4110 | } |
| 4111 | if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred)) { |
| 4112 | trace_nfsd_clid_cred_mismatch(conf, rqstp); |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 4113 | goto out; |
Chuck Lever | 2778773 | 2021-05-14 15:55:29 -0400 | [diff] [blame] | 4114 | } |
J. Bruce Fields | 90d700b | 2012-05-19 13:55:22 -0400 | [diff] [blame] | 4115 | if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) { |
J. Bruce Fields | 7d22fc1 | 2016-09-20 20:55:36 -0400 | [diff] [blame] | 4116 | if (conf && same_verf(&confirm, &conf->cl_confirm)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4117 | status = nfs_ok; |
Chuck Lever | 237f91c | 2021-05-14 15:56:13 -0400 | [diff] [blame] | 4118 | } else |
J. Bruce Fields | 90d700b | 2012-05-19 13:55:22 -0400 | [diff] [blame] | 4119 | status = nfserr_stale_clientid; |
| 4120 | goto out; |
| 4121 | } |
| 4122 | status = nfs_ok; |
Chuck Lever | 237f91c | 2021-05-14 15:56:13 -0400 | [diff] [blame] | 4123 | if (conf) { |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 4124 | old = unconf; |
| 4125 | unhash_client_locked(old); |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 4126 | nfsd4_change_callback(conf, &unconf->cl_cb_conn); |
Chuck Lever | 237f91c | 2021-05-14 15:56:13 -0400 | [diff] [blame] | 4127 | } else { |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 4128 | old = find_confirmed_client_by_name(&unconf->cl_name, nn); |
| 4129 | if (old) { |
J. Bruce Fields | 2b63482 | 2015-10-15 15:33:23 -0400 | [diff] [blame] | 4130 | status = nfserr_clid_inuse; |
| 4131 | if (client_has_state(old) |
| 4132 | && !same_creds(&unconf->cl_cred, |
| 4133 | &old->cl_cred)) |
| 4134 | goto out; |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 4135 | status = mark_client_expired_locked(old); |
Jeff Layton | 7abea1e | 2014-07-30 08:27:13 -0400 | [diff] [blame] | 4136 | if (status) { |
| 4137 | old = NULL; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 4138 | goto out; |
Jeff Layton | 7abea1e | 2014-07-30 08:27:13 -0400 | [diff] [blame] | 4139 | } |
Chuck Lever | 2958d2e | 2021-05-14 15:56:06 -0400 | [diff] [blame] | 4140 | trace_nfsd_clid_replaced(&old->cl_clientid); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 4141 | } |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 4142 | move_to_confirmed(unconf); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 4143 | conf = unconf; |
NeilBrown | 08e8987 | 2005-06-23 22:04:11 -0700 | [diff] [blame] | 4144 | } |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 4145 | get_client_locked(conf); |
| 4146 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | 934bd07 | 2021-05-25 14:53:44 -0400 | [diff] [blame] | 4147 | if (conf == unconf) |
| 4148 | fsnotify_dentry(conf->cl_nfsd_info_dentry, FS_MODIFY); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 4149 | nfsd4_probe_callback(conf); |
| 4150 | spin_lock(&nn->client_lock); |
| 4151 | put_client_renew_locked(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4152 | out: |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 4153 | spin_unlock(&nn->client_lock); |
| 4154 | if (old) |
| 4155 | expire_client(old); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4156 | return status; |
| 4157 | } |
| 4158 | |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 4159 | static struct nfs4_file *nfsd4_alloc_file(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4160 | { |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 4161 | return kmem_cache_alloc(file_slab, GFP_KERNEL); |
| 4162 | } |
| 4163 | |
| 4164 | /* OPEN Share state helper functions */ |
J. Bruce Fields | f9b60e2 | 2021-04-16 14:00:15 -0400 | [diff] [blame] | 4165 | static void nfsd4_init_file(struct svc_fh *fh, unsigned int hashval, |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 4166 | struct nfs4_file *fp) |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 4167 | { |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 4168 | lockdep_assert_held(&state_lock); |
| 4169 | |
Elena Reshetova | 818a34e | 2017-10-20 12:53:30 +0300 | [diff] [blame] | 4170 | refcount_set(&fp->fi_ref, 1); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 4171 | spin_lock_init(&fp->fi_lock); |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 4172 | INIT_LIST_HEAD(&fp->fi_stateids); |
| 4173 | INIT_LIST_HEAD(&fp->fi_delegations); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 4174 | INIT_LIST_HEAD(&fp->fi_clnt_odstate); |
J. Bruce Fields | f9b60e2 | 2021-04-16 14:00:15 -0400 | [diff] [blame] | 4175 | fh_copy_shallow(&fp->fi_fhandle, &fh->fh_handle); |
Jeff Layton | 0c637be | 2014-08-22 12:05:43 -0400 | [diff] [blame] | 4176 | fp->fi_deleg_file = NULL; |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 4177 | fp->fi_had_conflict = false; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4178 | fp->fi_share_deny = 0; |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 4179 | memset(fp->fi_fds, 0, sizeof(fp->fi_fds)); |
| 4180 | memset(fp->fi_access, 0, sizeof(fp->fi_access)); |
J. Bruce Fields | a0ce483 | 2021-04-16 14:00:16 -0400 | [diff] [blame] | 4181 | fp->fi_aliased = false; |
| 4182 | fp->fi_inode = d_inode(fh->fh_dentry); |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 4183 | #ifdef CONFIG_NFSD_PNFS |
| 4184 | INIT_LIST_HEAD(&fp->fi_lo_states); |
Christoph Hellwig | c5c707f | 2014-09-23 12:38:48 +0200 | [diff] [blame] | 4185 | atomic_set(&fp->fi_lo_recalls, 0); |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 4186 | #endif |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 4187 | hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4188 | } |
| 4189 | |
J. Bruce Fields | e8ff2a8 | 2007-08-01 15:30:59 -0400 | [diff] [blame] | 4190 | void |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 4191 | nfsd4_free_slabs(void) |
| 4192 | { |
Jeff Layton | 9258a2d | 2018-03-16 09:47:22 -0400 | [diff] [blame] | 4193 | kmem_cache_destroy(client_slab); |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 4194 | kmem_cache_destroy(openowner_slab); |
| 4195 | kmem_cache_destroy(lockowner_slab); |
| 4196 | kmem_cache_destroy(file_slab); |
| 4197 | kmem_cache_destroy(stateid_slab); |
| 4198 | kmem_cache_destroy(deleg_slab); |
Jeff Layton | 9258a2d | 2018-03-16 09:47:22 -0400 | [diff] [blame] | 4199 | kmem_cache_destroy(odstate_slab); |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 4200 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4201 | |
Bryan Schumaker | 7208339 | 2011-11-01 15:24:59 -0400 | [diff] [blame] | 4202 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4203 | nfsd4_init_slabs(void) |
| 4204 | { |
Jeff Layton | 9258a2d | 2018-03-16 09:47:22 -0400 | [diff] [blame] | 4205 | client_slab = kmem_cache_create("nfsd4_clients", |
| 4206 | sizeof(struct nfs4_client), 0, 0, NULL); |
| 4207 | if (client_slab == NULL) |
| 4208 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4209 | openowner_slab = kmem_cache_create("nfsd4_openowners", |
| 4210 | sizeof(struct nfs4_openowner), 0, 0, NULL); |
| 4211 | if (openowner_slab == NULL) |
Jeff Layton | 9258a2d | 2018-03-16 09:47:22 -0400 | [diff] [blame] | 4212 | goto out_free_client_slab; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4213 | lockowner_slab = kmem_cache_create("nfsd4_lockowners", |
Yanchuan Nian | 3c40794 | 2012-10-24 14:44:19 +0800 | [diff] [blame] | 4214 | sizeof(struct nfs4_lockowner), 0, 0, NULL); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4215 | if (lockowner_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 4216 | goto out_free_openowner_slab; |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 4217 | file_slab = kmem_cache_create("nfsd4_files", |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 4218 | sizeof(struct nfs4_file), 0, 0, NULL); |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 4219 | if (file_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 4220 | goto out_free_lockowner_slab; |
NeilBrown | 5ac049a | 2005-06-23 22:03:03 -0700 | [diff] [blame] | 4221 | stateid_slab = kmem_cache_create("nfsd4_stateids", |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4222 | sizeof(struct nfs4_ol_stateid), 0, 0, NULL); |
NeilBrown | 5ac049a | 2005-06-23 22:03:03 -0700 | [diff] [blame] | 4223 | if (stateid_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 4224 | goto out_free_file_slab; |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 4225 | deleg_slab = kmem_cache_create("nfsd4_delegations", |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 4226 | sizeof(struct nfs4_delegation), 0, 0, NULL); |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 4227 | if (deleg_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 4228 | goto out_free_stateid_slab; |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 4229 | odstate_slab = kmem_cache_create("nfsd4_odstate", |
| 4230 | sizeof(struct nfs4_clnt_odstate), 0, 0, NULL); |
| 4231 | if (odstate_slab == NULL) |
| 4232 | goto out_free_deleg_slab; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4233 | return 0; |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 4234 | |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 4235 | out_free_deleg_slab: |
| 4236 | kmem_cache_destroy(deleg_slab); |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 4237 | out_free_stateid_slab: |
| 4238 | kmem_cache_destroy(stateid_slab); |
| 4239 | out_free_file_slab: |
| 4240 | kmem_cache_destroy(file_slab); |
| 4241 | out_free_lockowner_slab: |
| 4242 | kmem_cache_destroy(lockowner_slab); |
| 4243 | out_free_openowner_slab: |
| 4244 | kmem_cache_destroy(openowner_slab); |
Jeff Layton | 9258a2d | 2018-03-16 09:47:22 -0400 | [diff] [blame] | 4245 | out_free_client_slab: |
| 4246 | kmem_cache_destroy(client_slab); |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 4247 | out: |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 4248 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4249 | } |
| 4250 | |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4251 | static void init_nfs4_replay(struct nfs4_replay *rp) |
| 4252 | { |
| 4253 | rp->rp_status = nfserr_serverfault; |
| 4254 | rp->rp_buflen = 0; |
| 4255 | rp->rp_buf = rp->rp_ibuf; |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 4256 | mutex_init(&rp->rp_mutex); |
| 4257 | } |
| 4258 | |
| 4259 | static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate, |
| 4260 | struct nfs4_stateowner *so) |
| 4261 | { |
| 4262 | if (!nfsd4_has_session(cstate)) { |
| 4263 | mutex_lock(&so->so_replay.rp_mutex); |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 4264 | cstate->replay_owner = nfs4_get_stateowner(so); |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 4265 | } |
| 4266 | } |
| 4267 | |
| 4268 | void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate) |
| 4269 | { |
| 4270 | struct nfs4_stateowner *so = cstate->replay_owner; |
| 4271 | |
| 4272 | if (so != NULL) { |
| 4273 | cstate->replay_owner = NULL; |
| 4274 | mutex_unlock(&so->so_replay.rp_mutex); |
| 4275 | nfs4_put_stateowner(so); |
| 4276 | } |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4277 | } |
| 4278 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4279 | static inline void *alloc_stateowner(struct kmem_cache *slab, struct xdr_netobj *owner, struct nfs4_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4280 | { |
| 4281 | struct nfs4_stateowner *sop; |
| 4282 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4283 | sop = kmem_cache_alloc(slab, GFP_KERNEL); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4284 | if (!sop) |
| 4285 | return NULL; |
| 4286 | |
J. Bruce Fields | 6f4859b | 2019-06-19 14:30:33 -0400 | [diff] [blame] | 4287 | xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4288 | if (!sop->so_owner.data) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4289 | kmem_cache_free(slab, sop); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4290 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4291 | } |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4292 | |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4293 | INIT_LIST_HEAD(&sop->so_stateids); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4294 | sop->so_client = clp; |
| 4295 | init_nfs4_replay(&sop->so_replay); |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 4296 | atomic_set(&sop->so_count, 1); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4297 | return sop; |
| 4298 | } |
| 4299 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4300 | static void hash_openowner(struct nfs4_openowner *oo, struct nfs4_client *clp, unsigned int strhashval) |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4301 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 4302 | lockdep_assert_held(&clp->cl_lock); |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 4303 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 4304 | list_add(&oo->oo_owner.so_strhash, |
| 4305 | &clp->cl_ownerstr_hashtbl[strhashval]); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4306 | list_add(&oo->oo_perclient, &clp->cl_openowners); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4307 | } |
| 4308 | |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 4309 | static void nfs4_unhash_openowner(struct nfs4_stateowner *so) |
| 4310 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 4311 | unhash_openowner_locked(openowner(so)); |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 4312 | } |
| 4313 | |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 4314 | static void nfs4_free_openowner(struct nfs4_stateowner *so) |
| 4315 | { |
| 4316 | struct nfs4_openowner *oo = openowner(so); |
| 4317 | |
| 4318 | kmem_cache_free(openowner_slab, oo); |
| 4319 | } |
| 4320 | |
| 4321 | static const struct nfs4_stateowner_operations openowner_ops = { |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 4322 | .so_unhash = nfs4_unhash_openowner, |
| 4323 | .so_free = nfs4_free_openowner, |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 4324 | }; |
| 4325 | |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4326 | static struct nfs4_ol_stateid * |
| 4327 | nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open) |
| 4328 | { |
| 4329 | struct nfs4_ol_stateid *local, *ret = NULL; |
| 4330 | struct nfs4_openowner *oo = open->op_openowner; |
| 4331 | |
| 4332 | lockdep_assert_held(&fp->fi_lock); |
| 4333 | |
| 4334 | list_for_each_entry(local, &fp->fi_stateids, st_perfile) { |
| 4335 | /* ignore lock owners */ |
| 4336 | if (local->st_stateowner->so_is_open_owner == 0) |
| 4337 | continue; |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 4338 | if (local->st_stateowner != &oo->oo_owner) |
| 4339 | continue; |
| 4340 | if (local->st_stid.sc_type == NFS4_OPEN_STID) { |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4341 | ret = local; |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 4342 | refcount_inc(&ret->st_stid.sc_count); |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4343 | break; |
| 4344 | } |
| 4345 | } |
| 4346 | return ret; |
| 4347 | } |
| 4348 | |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 4349 | static __be32 |
| 4350 | nfsd4_verify_open_stid(struct nfs4_stid *s) |
| 4351 | { |
| 4352 | __be32 ret = nfs_ok; |
| 4353 | |
| 4354 | switch (s->sc_type) { |
| 4355 | default: |
| 4356 | break; |
Trond Myklebust | 4f17641 | 2018-01-12 17:42:30 -0500 | [diff] [blame] | 4357 | case 0: |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 4358 | case NFS4_CLOSED_STID: |
| 4359 | case NFS4_CLOSED_DELEG_STID: |
| 4360 | ret = nfserr_bad_stateid; |
| 4361 | break; |
| 4362 | case NFS4_REVOKED_DELEG_STID: |
| 4363 | ret = nfserr_deleg_revoked; |
| 4364 | } |
| 4365 | return ret; |
| 4366 | } |
| 4367 | |
| 4368 | /* Lock the stateid st_mutex, and deal with races with CLOSE */ |
| 4369 | static __be32 |
| 4370 | nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp) |
| 4371 | { |
| 4372 | __be32 ret; |
| 4373 | |
Andrew Elble | 4f34bd0 | 2017-11-08 17:29:51 -0500 | [diff] [blame] | 4374 | mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX); |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 4375 | ret = nfsd4_verify_open_stid(&stp->st_stid); |
| 4376 | if (ret != nfs_ok) |
| 4377 | mutex_unlock(&stp->st_mutex); |
| 4378 | return ret; |
| 4379 | } |
| 4380 | |
| 4381 | static struct nfs4_ol_stateid * |
| 4382 | nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open) |
| 4383 | { |
| 4384 | struct nfs4_ol_stateid *stp; |
| 4385 | for (;;) { |
| 4386 | spin_lock(&fp->fi_lock); |
| 4387 | stp = nfsd4_find_existing_open(fp, open); |
| 4388 | spin_unlock(&fp->fi_lock); |
| 4389 | if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok) |
| 4390 | break; |
| 4391 | nfs4_put_stid(&stp->st_stid); |
| 4392 | } |
| 4393 | return stp; |
| 4394 | } |
| 4395 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4396 | static struct nfs4_openowner * |
Trond Myklebust | 13d6f66 | 2014-06-30 11:48:45 -0400 | [diff] [blame] | 4397 | alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open, |
Jeff Layton | db24b3b | 2014-06-30 11:48:36 -0400 | [diff] [blame] | 4398 | struct nfsd4_compound_state *cstate) |
| 4399 | { |
Trond Myklebust | 13d6f66 | 2014-06-30 11:48:45 -0400 | [diff] [blame] | 4400 | struct nfs4_client *clp = cstate->clp; |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 4401 | struct nfs4_openowner *oo, *ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4402 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4403 | oo = alloc_stateowner(openowner_slab, &open->op_owner, clp); |
| 4404 | if (!oo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4405 | return NULL; |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 4406 | oo->oo_owner.so_ops = &openowner_ops; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4407 | oo->oo_owner.so_is_open_owner = 1; |
| 4408 | oo->oo_owner.so_seqid = open->op_seqid; |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 4409 | oo->oo_flags = 0; |
Jeff Layton | db24b3b | 2014-06-30 11:48:36 -0400 | [diff] [blame] | 4410 | if (nfsd4_has_session(cstate)) |
| 4411 | oo->oo_flags |= NFS4_OO_CONFIRMED; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4412 | oo->oo_time = 0; |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 4413 | oo->oo_last_closed_stid = NULL; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4414 | INIT_LIST_HEAD(&oo->oo_close_lru); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 4415 | spin_lock(&clp->cl_lock); |
| 4416 | ret = find_openstateowner_str_locked(strhashval, open, clp); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 4417 | if (ret == NULL) { |
| 4418 | hash_openowner(oo, clp, strhashval); |
| 4419 | ret = oo; |
| 4420 | } else |
Kinglong Mee | d50ffde | 2015-07-16 12:05:07 +0800 | [diff] [blame] | 4421 | nfs4_free_stateowner(&oo->oo_owner); |
| 4422 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 4423 | spin_unlock(&clp->cl_lock); |
Jeff Layton | c595233 | 2015-03-23 10:53:42 -0400 | [diff] [blame] | 4424 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4425 | } |
| 4426 | |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4427 | static struct nfs4_ol_stateid * |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 4428 | init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open) |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4429 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4430 | |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4431 | struct nfs4_openowner *oo = open->op_openowner; |
| 4432 | struct nfs4_ol_stateid *retstp = NULL; |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 4433 | struct nfs4_ol_stateid *stp; |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4434 | |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 4435 | stp = open->op_stp; |
Oleg Drokin | 5cc1fb2 | 2016-06-14 23:28:05 -0400 | [diff] [blame] | 4436 | /* We are moving these outside of the spinlocks to avoid the warnings */ |
| 4437 | mutex_init(&stp->st_mutex); |
Andrew Elble | 4f34bd0 | 2017-11-08 17:29:51 -0500 | [diff] [blame] | 4438 | mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX); |
Oleg Drokin | 5cc1fb2 | 2016-06-14 23:28:05 -0400 | [diff] [blame] | 4439 | |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 4440 | retry: |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4441 | spin_lock(&oo->oo_owner.so_client->cl_lock); |
| 4442 | spin_lock(&fp->fi_lock); |
| 4443 | |
| 4444 | retstp = nfsd4_find_existing_open(fp, open); |
| 4445 | if (retstp) |
| 4446 | goto out_unlock; |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 4447 | |
| 4448 | open->op_stp = NULL; |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 4449 | refcount_inc(&stp->st_stid.sc_count); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 4450 | stp->st_stid.sc_type = NFS4_OPEN_STID; |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 4451 | INIT_LIST_HEAD(&stp->st_locks); |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 4452 | stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 4453 | get_nfs4_file(fp); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 4454 | stp->st_stid.sc_file = fp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4455 | stp->st_access_bmap = 0; |
| 4456 | stp->st_deny_bmap = 0; |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 4457 | stp->st_openstp = NULL; |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 4458 | list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 4459 | list_add(&stp->st_perfile, &fp->fi_stateids); |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4460 | |
| 4461 | out_unlock: |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 4462 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 4463 | spin_unlock(&oo->oo_owner.so_client->cl_lock); |
Oleg Drokin | 5cc1fb2 | 2016-06-14 23:28:05 -0400 | [diff] [blame] | 4464 | if (retstp) { |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 4465 | /* Handle races with CLOSE */ |
| 4466 | if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) { |
| 4467 | nfs4_put_stid(&retstp->st_stid); |
| 4468 | goto retry; |
| 4469 | } |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 4470 | /* To keep mutex tracking happy */ |
Oleg Drokin | 5cc1fb2 | 2016-06-14 23:28:05 -0400 | [diff] [blame] | 4471 | mutex_unlock(&stp->st_mutex); |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 4472 | stp = retstp; |
Oleg Drokin | 5cc1fb2 | 2016-06-14 23:28:05 -0400 | [diff] [blame] | 4473 | } |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 4474 | return stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4475 | } |
| 4476 | |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 4477 | /* |
| 4478 | * In the 4.0 case we need to keep the owners around a little while to handle |
| 4479 | * CLOSE replay. We still do need to release any file access that is held by |
| 4480 | * them before returning however. |
| 4481 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4482 | static void |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 4483 | move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4484 | { |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 4485 | struct nfs4_ol_stateid *last; |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 4486 | struct nfs4_openowner *oo = openowner(s->st_stateowner); |
| 4487 | struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net, |
| 4488 | nfsd_net_id); |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 4489 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4490 | dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4491 | |
Jeff Layton | b401be22 | 2014-07-29 21:34:33 -0400 | [diff] [blame] | 4492 | /* |
| 4493 | * We know that we hold one reference via nfsd4_close, and another |
| 4494 | * "persistent" reference for the client. If the refcount is higher |
| 4495 | * than 2, then there are still calls in progress that are using this |
| 4496 | * stateid. We can't put the sc_file reference until they are finished. |
| 4497 | * Wait for the refcount to drop to 2. Since it has been unhashed, |
| 4498 | * there should be no danger of the refcount going back up again at |
| 4499 | * this point. |
| 4500 | */ |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 4501 | wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2); |
Jeff Layton | b401be22 | 2014-07-29 21:34:33 -0400 | [diff] [blame] | 4502 | |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 4503 | release_all_access(s); |
| 4504 | if (s->st_stid.sc_file) { |
| 4505 | put_nfs4_file(s->st_stid.sc_file); |
| 4506 | s->st_stid.sc_file = NULL; |
| 4507 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 4508 | |
| 4509 | spin_lock(&nn->client_lock); |
| 4510 | last = oo->oo_last_closed_stid; |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 4511 | oo->oo_last_closed_stid = s; |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 4512 | list_move_tail(&oo->oo_close_lru, &nn->close_lru); |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 4513 | oo->oo_time = ktime_get_boottime_seconds(); |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 4514 | spin_unlock(&nn->client_lock); |
| 4515 | if (last) |
| 4516 | nfs4_put_stid(&last->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4517 | } |
| 4518 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4519 | /* search file_hashtbl[] for file */ |
| 4520 | static struct nfs4_file * |
J. Bruce Fields | f9b60e2 | 2021-04-16 14:00:15 -0400 | [diff] [blame] | 4521 | find_file_locked(struct svc_fh *fh, unsigned int hashval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4522 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4523 | struct nfs4_file *fp; |
| 4524 | |
Madhuparna Bhowmik | 36a8049 | 2020-02-13 20:33:31 +0530 | [diff] [blame] | 4525 | hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash, |
| 4526 | lockdep_is_held(&state_lock)) { |
J. Bruce Fields | f9b60e2 | 2021-04-16 14:00:15 -0400 | [diff] [blame] | 4527 | if (fh_match(&fp->fi_fhandle, &fh->fh_handle)) { |
Elena Reshetova | 818a34e | 2017-10-20 12:53:30 +0300 | [diff] [blame] | 4528 | if (refcount_inc_not_zero(&fp->fi_ref)) |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 4529 | return fp; |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 4530 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4531 | } |
| 4532 | return NULL; |
| 4533 | } |
| 4534 | |
J. Bruce Fields | a0ce483 | 2021-04-16 14:00:16 -0400 | [diff] [blame] | 4535 | static struct nfs4_file *insert_file(struct nfs4_file *new, struct svc_fh *fh, |
| 4536 | unsigned int hashval) |
| 4537 | { |
| 4538 | struct nfs4_file *fp; |
| 4539 | struct nfs4_file *ret = NULL; |
| 4540 | bool alias_found = false; |
| 4541 | |
| 4542 | spin_lock(&state_lock); |
| 4543 | hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash, |
| 4544 | lockdep_is_held(&state_lock)) { |
| 4545 | if (fh_match(&fp->fi_fhandle, &fh->fh_handle)) { |
| 4546 | if (refcount_inc_not_zero(&fp->fi_ref)) |
| 4547 | ret = fp; |
| 4548 | } else if (d_inode(fh->fh_dentry) == fp->fi_inode) |
| 4549 | fp->fi_aliased = alias_found = true; |
| 4550 | } |
| 4551 | if (likely(ret == NULL)) { |
| 4552 | nfsd4_init_file(fh, hashval, new); |
| 4553 | new->fi_aliased = alias_found; |
| 4554 | ret = new; |
| 4555 | } |
| 4556 | spin_unlock(&state_lock); |
| 4557 | return ret; |
| 4558 | } |
| 4559 | |
J. Bruce Fields | f9b60e2 | 2021-04-16 14:00:15 -0400 | [diff] [blame] | 4560 | static struct nfs4_file * find_file(struct svc_fh *fh) |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 4561 | { |
| 4562 | struct nfs4_file *fp; |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 4563 | unsigned int hashval = file_hashval(fh); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 4564 | |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 4565 | rcu_read_lock(); |
| 4566 | fp = find_file_locked(fh, hashval); |
| 4567 | rcu_read_unlock(); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 4568 | return fp; |
| 4569 | } |
| 4570 | |
| 4571 | static struct nfs4_file * |
J. Bruce Fields | f9b60e2 | 2021-04-16 14:00:15 -0400 | [diff] [blame] | 4572 | find_or_add_file(struct nfs4_file *new, struct svc_fh *fh) |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 4573 | { |
| 4574 | struct nfs4_file *fp; |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 4575 | unsigned int hashval = file_hashval(fh); |
| 4576 | |
| 4577 | rcu_read_lock(); |
| 4578 | fp = find_file_locked(fh, hashval); |
| 4579 | rcu_read_unlock(); |
| 4580 | if (fp) |
| 4581 | return fp; |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 4582 | |
J. Bruce Fields | a0ce483 | 2021-04-16 14:00:16 -0400 | [diff] [blame] | 4583 | return insert_file(new, fh, hashval); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 4584 | } |
| 4585 | |
J. Bruce Fields | 4f83aa3 | 2008-07-07 15:02:02 -0400 | [diff] [blame] | 4586 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4587 | * Called to check deny when READ with all zero stateid or |
| 4588 | * WRITE with all zero or all one stateid |
| 4589 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4590 | static __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4591 | nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type) |
| 4592 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4593 | struct nfs4_file *fp; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4594 | __be32 ret = nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4595 | |
J. Bruce Fields | f9b60e2 | 2021-04-16 14:00:15 -0400 | [diff] [blame] | 4596 | fp = find_file(current_fh); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 4597 | if (!fp) |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4598 | return ret; |
| 4599 | /* Check for conflicting share reservations */ |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 4600 | spin_lock(&fp->fi_lock); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4601 | if (fp->fi_share_deny & deny_type) |
| 4602 | ret = nfserr_locked; |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 4603 | spin_unlock(&fp->fi_lock); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 4604 | put_nfs4_file(fp); |
| 4605 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4606 | } |
| 4607 | |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 4608 | static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4609 | { |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 4610 | struct nfs4_delegation *dp = cb_to_delegation(cb); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 4611 | struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net, |
| 4612 | nfsd_net_id); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 4613 | |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 4614 | block_delegations(&dp->dl_stid.sc_file->fi_fhandle); |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 4615 | |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 4616 | /* |
| 4617 | * We can't do this in nfsd_break_deleg_cb because it is |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 4618 | * already holding inode->i_lock. |
| 4619 | * |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 4620 | * If the dl_time != 0, then we know that it has already been |
| 4621 | * queued for a lease break. Don't queue it again. |
| 4622 | */ |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 4623 | spin_lock(&state_lock); |
J. Bruce Fields | 548ec08 | 2021-11-29 15:08:00 -0500 | [diff] [blame] | 4624 | if (delegation_hashed(dp) && dp->dl_time == 0) { |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 4625 | dp->dl_time = ktime_get_boottime_seconds(); |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 4626 | list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru); |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 4627 | } |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 4628 | spin_unlock(&state_lock); |
| 4629 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4630 | |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 4631 | static int nfsd4_cb_recall_done(struct nfsd4_callback *cb, |
| 4632 | struct rpc_task *task) |
| 4633 | { |
| 4634 | struct nfs4_delegation *dp = cb_to_delegation(cb); |
| 4635 | |
J. Bruce Fields | 12ed22f | 2020-07-31 16:20:05 -0400 | [diff] [blame] | 4636 | if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID || |
| 4637 | dp->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) |
Andrew Elble | a457974 | 2015-08-31 12:06:41 -0400 | [diff] [blame] | 4638 | return 1; |
| 4639 | |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 4640 | switch (task->tk_status) { |
| 4641 | case 0: |
| 4642 | return 1; |
Scott Mayhew | 1c73b9d | 2019-05-02 13:32:12 -0400 | [diff] [blame] | 4643 | case -NFS4ERR_DELAY: |
| 4644 | rpc_delay(task, 2 * HZ); |
| 4645 | return 0; |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 4646 | case -EBADHANDLE: |
| 4647 | case -NFS4ERR_BAD_STATEID: |
| 4648 | /* |
| 4649 | * Race: client probably got cb_recall before open reply |
| 4650 | * granting delegation. |
| 4651 | */ |
| 4652 | if (dp->dl_retries--) { |
| 4653 | rpc_delay(task, 2 * HZ); |
| 4654 | return 0; |
| 4655 | } |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 4656 | fallthrough; |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 4657 | default: |
Scott Mayhew | 1c73b9d | 2019-05-02 13:32:12 -0400 | [diff] [blame] | 4658 | return 1; |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 4659 | } |
| 4660 | } |
| 4661 | |
| 4662 | static void nfsd4_cb_recall_release(struct nfsd4_callback *cb) |
| 4663 | { |
| 4664 | struct nfs4_delegation *dp = cb_to_delegation(cb); |
| 4665 | |
| 4666 | nfs4_put_stid(&dp->dl_stid); |
| 4667 | } |
| 4668 | |
Julia Lawall | c4cb897 | 2015-11-21 22:57:39 +0100 | [diff] [blame] | 4669 | static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = { |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 4670 | .prepare = nfsd4_cb_recall_prepare, |
| 4671 | .done = nfsd4_cb_recall_done, |
| 4672 | .release = nfsd4_cb_recall_release, |
| 4673 | }; |
| 4674 | |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 4675 | static void nfsd_break_one_deleg(struct nfs4_delegation *dp) |
| 4676 | { |
| 4677 | /* |
| 4678 | * We're assuming the state code never drops its reference |
| 4679 | * without first removing the lease. Since we're in this lease |
J. Bruce Fields | 4a269efb | 2018-04-13 17:34:35 -0400 | [diff] [blame] | 4680 | * callback (and since the lease code is serialized by the |
| 4681 | * i_lock) we know the server hasn't removed the lease yet, and |
| 4682 | * we know it's safe to take a reference. |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 4683 | */ |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 4684 | refcount_inc(&dp->dl_stid.sc_count); |
Christoph Hellwig | f0b5de1 | 2014-09-24 12:19:18 +0200 | [diff] [blame] | 4685 | nfsd4_run_cb(&dp->dl_recall); |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 4686 | } |
| 4687 | |
Jeff Layton | 1c8c601 | 2013-06-21 08:58:15 -0400 | [diff] [blame] | 4688 | /* Called from break_lease() with i_lock held. */ |
Jeff Layton | 4d01b7f | 2014-09-01 15:06:54 -0400 | [diff] [blame] | 4689 | static bool |
| 4690 | nfsd_break_deleg_cb(struct file_lock *fl) |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 4691 | { |
Jeff Layton | 4d01b7f | 2014-09-01 15:06:54 -0400 | [diff] [blame] | 4692 | bool ret = false; |
J. Bruce Fields | 653e514 | 2018-02-15 22:08:45 -0500 | [diff] [blame] | 4693 | struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner; |
| 4694 | struct nfs4_file *fp = dp->dl_stid.sc_file; |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 4695 | |
Chuck Lever | 17d76dd | 2021-05-14 15:57:20 -0400 | [diff] [blame] | 4696 | trace_nfsd_cb_recall(&dp->dl_stid); |
Chuck Lever | dd5e3fb | 2020-04-05 11:42:29 -0400 | [diff] [blame] | 4697 | |
J. Bruce Fields | 0272e1f | 2007-09-12 18:56:12 -0400 | [diff] [blame] | 4698 | /* |
| 4699 | * We don't want the locks code to timeout the lease for us; |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 4700 | * we'll remove it ourself if a delegation isn't returned |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 4701 | * in time: |
J. Bruce Fields | 0272e1f | 2007-09-12 18:56:12 -0400 | [diff] [blame] | 4702 | */ |
| 4703 | fl->fl_break_time = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4704 | |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 4705 | spin_lock(&fp->fi_lock); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4706 | fp->fi_had_conflict = true; |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 4707 | nfsd_break_one_deleg(dp); |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 4708 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 4d01b7f | 2014-09-01 15:06:54 -0400 | [diff] [blame] | 4709 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4710 | } |
| 4711 | |
J. Bruce Fields | 28df3d1 | 2017-07-28 16:35:15 -0400 | [diff] [blame] | 4712 | static bool nfsd_breaker_owns_lease(struct file_lock *fl) |
| 4713 | { |
| 4714 | struct nfs4_delegation *dl = fl->fl_owner; |
| 4715 | struct svc_rqst *rqst; |
| 4716 | struct nfs4_client *clp; |
| 4717 | |
| 4718 | if (!i_am_nfsd()) |
| 4719 | return NULL; |
| 4720 | rqst = kthread_data(current); |
J. Bruce Fields | 1395616 | 2020-09-25 10:12:39 -0400 | [diff] [blame] | 4721 | /* Note rq_prog == NFS_ACL_PROGRAM is also possible: */ |
| 4722 | if (rqst->rq_prog != NFS_PROGRAM || rqst->rq_vers < 4) |
J. Bruce Fields | 34b09af | 2020-08-05 15:10:11 -0400 | [diff] [blame] | 4723 | return NULL; |
J. Bruce Fields | 28df3d1 | 2017-07-28 16:35:15 -0400 | [diff] [blame] | 4724 | clp = *(rqst->rq_lease_breaker); |
| 4725 | return dl->dl_stid.sc_client == clp; |
| 4726 | } |
| 4727 | |
Jeff Layton | c45198e | 2014-09-01 07:12:07 -0400 | [diff] [blame] | 4728 | static int |
Jeff Layton | 7448cc3 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 4729 | nfsd_change_deleg_cb(struct file_lock *onlist, int arg, |
| 4730 | struct list_head *dispose) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4731 | { |
| 4732 | if (arg & F_UNLCK) |
Jeff Layton | c45198e | 2014-09-01 07:12:07 -0400 | [diff] [blame] | 4733 | return lease_modify(onlist, arg, dispose); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4734 | else |
| 4735 | return -EAGAIN; |
| 4736 | } |
| 4737 | |
Alexey Dobriyan | 7b02196 | 2009-09-21 17:01:12 -0700 | [diff] [blame] | 4738 | static const struct lock_manager_operations nfsd_lease_mng_ops = { |
J. Bruce Fields | 28df3d1 | 2017-07-28 16:35:15 -0400 | [diff] [blame] | 4739 | .lm_breaker_owns_lease = nfsd_breaker_owns_lease, |
J. Bruce Fields | 8fb47a4 | 2011-07-20 20:21:59 -0400 | [diff] [blame] | 4740 | .lm_break = nfsd_break_deleg_cb, |
| 4741 | .lm_change = nfsd_change_deleg_cb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4742 | }; |
| 4743 | |
J. Bruce Fields | 7a8711c | 2011-09-02 09:03:37 -0400 | [diff] [blame] | 4744 | static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid) |
| 4745 | { |
| 4746 | if (nfsd4_has_session(cstate)) |
| 4747 | return nfs_ok; |
| 4748 | if (seqid == so->so_seqid - 1) |
| 4749 | return nfserr_replay_me; |
| 4750 | if (seqid == so->so_seqid) |
| 4751 | return nfs_ok; |
| 4752 | return nfserr_bad_seqid; |
| 4753 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4754 | |
J. Bruce Fields | 7950b53 | 2021-01-21 17:57:41 -0500 | [diff] [blame] | 4755 | static struct nfs4_client *lookup_clientid(clientid_t *clid, bool sessions, |
| 4756 | struct nfsd_net *nn) |
| 4757 | { |
| 4758 | struct nfs4_client *found; |
| 4759 | |
| 4760 | spin_lock(&nn->client_lock); |
| 4761 | found = find_confirmed_client(clid, sessions, nn); |
| 4762 | if (found) |
| 4763 | atomic_inc(&found->cl_rpc_users); |
| 4764 | spin_unlock(&nn->client_lock); |
| 4765 | return found; |
| 4766 | } |
| 4767 | |
J. Bruce Fields | 460d270 | 2021-01-21 17:57:40 -0500 | [diff] [blame] | 4768 | static __be32 set_client(clientid_t *clid, |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4769 | struct nfsd4_compound_state *cstate, |
J. Bruce Fields | f71475b | 2021-01-21 17:57:43 -0500 | [diff] [blame] | 4770 | struct nfsd_net *nn) |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4771 | { |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4772 | if (cstate->clp) { |
J. Bruce Fields | 7950b53 | 2021-01-21 17:57:41 -0500 | [diff] [blame] | 4773 | if (!same_clid(&cstate->clp->cl_clientid, clid)) |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4774 | return nfserr_stale_clientid; |
| 4775 | return nfs_ok; |
| 4776 | } |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4777 | if (STALE_CLIENTID(clid, nn)) |
| 4778 | return nfserr_stale_clientid; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4779 | /* |
J. Bruce Fields | f71475b | 2021-01-21 17:57:43 -0500 | [diff] [blame] | 4780 | * We're in the 4.0 case (otherwise the SEQUENCE op would have |
| 4781 | * set cstate->clp), so session = false: |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4782 | */ |
J. Bruce Fields | f71475b | 2021-01-21 17:57:43 -0500 | [diff] [blame] | 4783 | cstate->clp = lookup_clientid(clid, false, nn); |
J. Bruce Fields | 7950b53 | 2021-01-21 17:57:41 -0500 | [diff] [blame] | 4784 | if (!cstate->clp) |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4785 | return nfserr_expired; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4786 | return nfs_ok; |
| 4787 | } |
| 4788 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4789 | __be32 |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 4790 | nfsd4_process_open1(struct nfsd4_compound_state *cstate, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4791 | struct nfsd4_open *open, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4792 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4793 | clientid_t *clientid = &open->op_clientid; |
| 4794 | struct nfs4_client *clp = NULL; |
| 4795 | unsigned int strhashval; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4796 | struct nfs4_openowner *oo = NULL; |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 4797 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4798 | |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 4799 | /* |
| 4800 | * In case we need it later, after we've already created the |
| 4801 | * file and don't want to risk a further failure: |
| 4802 | */ |
| 4803 | open->op_file = nfsd4_alloc_file(); |
| 4804 | if (open->op_file == NULL) |
| 4805 | return nfserr_jukebox; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4806 | |
J. Bruce Fields | f71475b | 2021-01-21 17:57:43 -0500 | [diff] [blame] | 4807 | status = set_client(clientid, cstate, nn); |
Trond Myklebust | 2d91e89 | 2014-06-30 11:48:46 -0400 | [diff] [blame] | 4808 | if (status) |
| 4809 | return status; |
| 4810 | clp = cstate->clp; |
| 4811 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 4812 | strhashval = ownerstr_hashval(&open->op_owner); |
| 4813 | oo = find_openstateowner_str(strhashval, open, clp); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4814 | open->op_openowner = oo; |
| 4815 | if (!oo) { |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 4816 | goto new_owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4817 | } |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 4818 | if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) { |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 4819 | /* Replace unconfirmed owners without checking for replay. */ |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4820 | release_openowner(oo); |
| 4821 | open->op_openowner = NULL; |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 4822 | goto new_owner; |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 4823 | } |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 4824 | status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid); |
| 4825 | if (status) |
| 4826 | return status; |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 4827 | goto alloc_stateid; |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 4828 | new_owner: |
Trond Myklebust | 13d6f66 | 2014-06-30 11:48:45 -0400 | [diff] [blame] | 4829 | oo = alloc_init_open_stateowner(strhashval, open, cstate); |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 4830 | if (oo == NULL) |
| 4831 | return nfserr_jukebox; |
| 4832 | open->op_openowner = oo; |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 4833 | alloc_stateid: |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 4834 | open->op_stp = nfs4_alloc_open_stateid(clp); |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 4835 | if (!open->op_stp) |
| 4836 | return nfserr_jukebox; |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 4837 | |
| 4838 | if (nfsd4_has_session(cstate) && |
| 4839 | (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) { |
| 4840 | open->op_odstate = alloc_clnt_odstate(clp); |
| 4841 | if (!open->op_odstate) |
| 4842 | return nfserr_jukebox; |
| 4843 | } |
| 4844 | |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 4845 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4846 | } |
| 4847 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4848 | static inline __be32 |
NeilBrown | 4a6e43e | 2005-06-23 22:02:50 -0700 | [diff] [blame] | 4849 | nfs4_check_delegmode(struct nfs4_delegation *dp, int flags) |
| 4850 | { |
| 4851 | if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ)) |
| 4852 | return nfserr_openmode; |
| 4853 | else |
| 4854 | return nfs_ok; |
| 4855 | } |
| 4856 | |
Daniel Mack | c47d832 | 2011-05-16 16:38:14 +0200 | [diff] [blame] | 4857 | static int share_access_to_flags(u32 share_access) |
J. Bruce Fields | 24a0111 | 2010-05-18 20:01:35 -0400 | [diff] [blame] | 4858 | { |
J. Bruce Fields | 24a0111 | 2010-05-18 20:01:35 -0400 | [diff] [blame] | 4859 | return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE; |
| 4860 | } |
| 4861 | |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4862 | static struct nfs4_delegation *find_deleg_stateid(struct nfs4_client *cl, stateid_t *s) |
J. Bruce Fields | f459e45 | 2011-09-09 09:06:12 -0400 | [diff] [blame] | 4863 | { |
| 4864 | struct nfs4_stid *ret; |
| 4865 | |
Andrew Elble | 95da1b3a | 2017-11-03 14:06:31 -0400 | [diff] [blame] | 4866 | ret = find_stateid_by_type(cl, s, |
| 4867 | NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID); |
J. Bruce Fields | f459e45 | 2011-09-09 09:06:12 -0400 | [diff] [blame] | 4868 | if (!ret) |
| 4869 | return NULL; |
| 4870 | return delegstateid(ret); |
| 4871 | } |
| 4872 | |
J. Bruce Fields | 8b289b2 | 2011-10-19 11:52:12 -0400 | [diff] [blame] | 4873 | static bool nfsd4_is_deleg_cur(struct nfsd4_open *open) |
| 4874 | { |
| 4875 | return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR || |
| 4876 | open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH; |
| 4877 | } |
| 4878 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4879 | static __be32 |
J. Bruce Fields | 41d2266 | 2013-03-21 15:49:47 -0400 | [diff] [blame] | 4880 | nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open, |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 4881 | struct nfs4_delegation **dp) |
| 4882 | { |
| 4883 | int flags; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4884 | __be32 status = nfserr_bad_stateid; |
Trond Myklebust | dcd94cc | 2014-07-29 21:34:18 -0400 | [diff] [blame] | 4885 | struct nfs4_delegation *deleg; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 4886 | |
Trond Myklebust | dcd94cc | 2014-07-29 21:34:18 -0400 | [diff] [blame] | 4887 | deleg = find_deleg_stateid(cl, &open->op_delegate_stateid); |
| 4888 | if (deleg == NULL) |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 4889 | goto out; |
Andrew Elble | 95da1b3a | 2017-11-03 14:06:31 -0400 | [diff] [blame] | 4890 | if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) { |
| 4891 | nfs4_put_stid(&deleg->dl_stid); |
| 4892 | if (cl->cl_minorversion) |
| 4893 | status = nfserr_deleg_revoked; |
| 4894 | goto out; |
| 4895 | } |
J. Bruce Fields | 24a0111 | 2010-05-18 20:01:35 -0400 | [diff] [blame] | 4896 | flags = share_access_to_flags(open->op_share_access); |
Trond Myklebust | dcd94cc | 2014-07-29 21:34:18 -0400 | [diff] [blame] | 4897 | status = nfs4_check_delegmode(deleg, flags); |
| 4898 | if (status) { |
| 4899 | nfs4_put_stid(&deleg->dl_stid); |
| 4900 | goto out; |
| 4901 | } |
| 4902 | *dp = deleg; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 4903 | out: |
J. Bruce Fields | 8b289b2 | 2011-10-19 11:52:12 -0400 | [diff] [blame] | 4904 | if (!nfsd4_is_deleg_cur(open)) |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 4905 | return nfs_ok; |
| 4906 | if (status) |
| 4907 | return status; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 4908 | open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 4909 | return nfs_ok; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 4910 | } |
| 4911 | |
J. Bruce Fields | 21fb401 | 2010-07-28 12:21:23 -0400 | [diff] [blame] | 4912 | static inline int nfs4_access_to_access(u32 nfs4_access) |
| 4913 | { |
| 4914 | int flags = 0; |
| 4915 | |
| 4916 | if (nfs4_access & NFS4_SHARE_ACCESS_READ) |
| 4917 | flags |= NFSD_MAY_READ; |
| 4918 | if (nfs4_access & NFS4_SHARE_ACCESS_WRITE) |
| 4919 | flags |= NFSD_MAY_WRITE; |
| 4920 | return flags; |
| 4921 | } |
| 4922 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4923 | static inline __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4924 | nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh, |
| 4925 | struct nfsd4_open *open) |
| 4926 | { |
| 4927 | struct iattr iattr = { |
| 4928 | .ia_valid = ATTR_SIZE, |
| 4929 | .ia_size = 0, |
| 4930 | }; |
| 4931 | if (!open->op_truncate) |
| 4932 | return 0; |
| 4933 | if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE)) |
Al Viro | 9246585 | 2006-01-18 17:43:46 -0800 | [diff] [blame] | 4934 | return nfserr_inval; |
Arnd Bergmann | 2a1aa48 | 2019-11-03 17:50:18 +0100 | [diff] [blame] | 4935 | return nfsd_setattr(rqstp, fh, &iattr, 0, (time64_t)0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4936 | } |
| 4937 | |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4938 | static __be32 nfs4_get_vfs_file(struct svc_rqst *rqstp, struct nfs4_file *fp, |
Jeff Layton | 6eb3a1d | 2014-07-10 14:07:31 -0400 | [diff] [blame] | 4939 | struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, |
| 4940 | struct nfsd4_open *open) |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4941 | { |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 4942 | struct nfsd_file *nf = NULL; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4943 | __be32 status; |
| 4944 | int oflag = nfs4_access_to_omode(open->op_share_access); |
| 4945 | int access = nfs4_access_to_access(open->op_share_access); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4946 | unsigned char old_access_bmap, old_deny_bmap; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4947 | |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4948 | spin_lock(&fp->fi_lock); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4949 | |
| 4950 | /* |
| 4951 | * Are we trying to set a deny mode that would conflict with |
| 4952 | * current access? |
| 4953 | */ |
| 4954 | status = nfs4_file_check_deny(fp, open->op_share_deny); |
| 4955 | if (status != nfs_ok) { |
| 4956 | spin_unlock(&fp->fi_lock); |
| 4957 | goto out; |
| 4958 | } |
| 4959 | |
| 4960 | /* set access to the file */ |
| 4961 | status = nfs4_file_get_access(fp, open->op_share_access); |
| 4962 | if (status != nfs_ok) { |
| 4963 | spin_unlock(&fp->fi_lock); |
| 4964 | goto out; |
| 4965 | } |
| 4966 | |
| 4967 | /* Set access bits in stateid */ |
| 4968 | old_access_bmap = stp->st_access_bmap; |
| 4969 | set_access(open->op_share_access, stp); |
| 4970 | |
| 4971 | /* Set new deny mask */ |
| 4972 | old_deny_bmap = stp->st_deny_bmap; |
| 4973 | set_deny(open->op_share_deny, stp); |
| 4974 | fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH); |
| 4975 | |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4976 | if (!fp->fi_fds[oflag]) { |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4977 | spin_unlock(&fp->fi_lock); |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 4978 | status = nfsd_file_acquire(rqstp, cur_fh, access, &nf); |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4979 | if (status) |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4980 | goto out_put_access; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4981 | spin_lock(&fp->fi_lock); |
| 4982 | if (!fp->fi_fds[oflag]) { |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 4983 | fp->fi_fds[oflag] = nf; |
| 4984 | nf = NULL; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4985 | } |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4986 | } |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4987 | spin_unlock(&fp->fi_lock); |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 4988 | if (nf) |
| 4989 | nfsd_file_put(nf); |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4990 | |
J. Bruce Fields | 217fd6f | 2021-04-16 14:00:14 -0400 | [diff] [blame] | 4991 | status = nfserrno(nfsd_open_break_lease(cur_fh->fh_dentry->d_inode, |
| 4992 | access)); |
| 4993 | if (status) |
| 4994 | goto out_put_access; |
| 4995 | |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4996 | status = nfsd4_truncate(rqstp, cur_fh, open); |
| 4997 | if (status) |
| 4998 | goto out_put_access; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4999 | out: |
| 5000 | return status; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 5001 | out_put_access: |
| 5002 | stp->st_access_bmap = old_access_bmap; |
| 5003 | nfs4_file_put_access(fp, open->op_share_access); |
| 5004 | reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp); |
| 5005 | goto out; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 5006 | } |
| 5007 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5008 | static __be32 |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5009 | nfs4_upgrade_open(struct svc_rqst *rqstp, struct nfs4_file *fp, struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, struct nfsd4_open *open) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5010 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5011 | __be32 status; |
Arnd Bergmann | 6ac7536 | 2015-05-12 23:31:29 +0200 | [diff] [blame] | 5012 | unsigned char old_deny_bmap = stp->st_deny_bmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5013 | |
Jeff Layton | 6eb3a1d | 2014-07-10 14:07:31 -0400 | [diff] [blame] | 5014 | if (!test_access(open->op_share_access, stp)) |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 5015 | return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open); |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 5016 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 5017 | /* test and set deny mode */ |
| 5018 | spin_lock(&fp->fi_lock); |
| 5019 | status = nfs4_file_check_deny(fp, open->op_share_deny); |
| 5020 | if (status == nfs_ok) { |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 5021 | set_deny(open->op_share_deny, stp); |
| 5022 | fp->fi_share_deny |= |
| 5023 | (open->op_share_deny & NFS4_SHARE_DENY_BOTH); |
| 5024 | } |
| 5025 | spin_unlock(&fp->fi_lock); |
| 5026 | |
| 5027 | if (status != nfs_ok) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5028 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5029 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 5030 | status = nfsd4_truncate(rqstp, cur_fh, open); |
| 5031 | if (status != nfs_ok) |
| 5032 | reset_union_bmap_deny(old_deny_bmap, stp); |
| 5033 | return status; |
| 5034 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5035 | |
J. Bruce Fields | 14a24e9 | 2010-12-10 19:02:49 -0500 | [diff] [blame] | 5036 | /* Should we give out recallable state?: */ |
| 5037 | static bool nfsd4_cb_channel_good(struct nfs4_client *clp) |
| 5038 | { |
| 5039 | if (clp->cl_cb_state == NFSD4_CB_UP) |
| 5040 | return true; |
| 5041 | /* |
| 5042 | * In the sessions case, since we don't have to establish a |
| 5043 | * separate connection for callbacks, we assume it's OK |
| 5044 | * until we hear otherwise: |
| 5045 | */ |
| 5046 | return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN; |
| 5047 | } |
| 5048 | |
J. Bruce Fields | 653e514 | 2018-02-15 22:08:45 -0500 | [diff] [blame] | 5049 | static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, |
| 5050 | int flag) |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 5051 | { |
| 5052 | struct file_lock *fl; |
| 5053 | |
| 5054 | fl = locks_alloc_lock(); |
| 5055 | if (!fl) |
| 5056 | return NULL; |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 5057 | fl->fl_lmops = &nfsd_lease_mng_ops; |
J. Bruce Fields | 617588d | 2011-07-01 15:18:34 -0400 | [diff] [blame] | 5058 | fl->fl_flags = FL_DELEG; |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 5059 | fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK; |
| 5060 | fl->fl_end = OFFSET_MAX; |
J. Bruce Fields | 653e514 | 2018-02-15 22:08:45 -0500 | [diff] [blame] | 5061 | fl->fl_owner = (fl_owner_t)dp; |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 5062 | fl->fl_pid = current->tgid; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5063 | fl->fl_file = dp->dl_stid.sc_file->fi_deleg_file->nf_file; |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 5064 | return fl; |
| 5065 | } |
| 5066 | |
J. Bruce Fields | aba2072 | 2021-04-16 14:00:18 -0400 | [diff] [blame] | 5067 | static int nfsd4_check_conflicting_opens(struct nfs4_client *clp, |
| 5068 | struct nfs4_file *fp) |
| 5069 | { |
| 5070 | struct nfs4_ol_stateid *st; |
| 5071 | struct file *f = fp->fi_deleg_file->nf_file; |
| 5072 | struct inode *ino = locks_inode(f); |
| 5073 | int writes; |
| 5074 | |
| 5075 | writes = atomic_read(&ino->i_writecount); |
| 5076 | if (!writes) |
| 5077 | return 0; |
| 5078 | /* |
| 5079 | * There could be multiple filehandles (hence multiple |
| 5080 | * nfs4_files) referencing this file, but that's not too |
| 5081 | * common; let's just give up in that case rather than |
| 5082 | * trying to go look up all the clients using that other |
| 5083 | * nfs4_file as well: |
| 5084 | */ |
| 5085 | if (fp->fi_aliased) |
| 5086 | return -EAGAIN; |
| 5087 | /* |
| 5088 | * If there's a close in progress, make sure that we see it |
| 5089 | * clear any fi_fds[] entries before we see it decrement |
| 5090 | * i_writecount: |
| 5091 | */ |
| 5092 | smp_mb__after_atomic(); |
| 5093 | |
| 5094 | if (fp->fi_fds[O_WRONLY]) |
| 5095 | writes--; |
| 5096 | if (fp->fi_fds[O_RDWR]) |
| 5097 | writes--; |
| 5098 | if (writes > 0) |
| 5099 | return -EAGAIN; /* There may be non-NFSv4 writers */ |
| 5100 | /* |
| 5101 | * It's possible there are non-NFSv4 write opens in progress, |
| 5102 | * but if they haven't incremented i_writecount yet then they |
| 5103 | * also haven't called break lease yet; so, they'll break this |
| 5104 | * lease soon enough. So, all that's left to check for is NFSv4 |
| 5105 | * opens: |
| 5106 | */ |
| 5107 | spin_lock(&fp->fi_lock); |
| 5108 | list_for_each_entry(st, &fp->fi_stateids, st_perfile) { |
| 5109 | if (st->st_openstp == NULL /* it's an open */ && |
| 5110 | access_permit_write(st) && |
| 5111 | st->st_stid.sc_client != clp) { |
| 5112 | spin_unlock(&fp->fi_lock); |
| 5113 | return -EAGAIN; |
| 5114 | } |
| 5115 | } |
| 5116 | spin_unlock(&fp->fi_lock); |
| 5117 | /* |
| 5118 | * There's a small chance that we could be racing with another |
| 5119 | * NFSv4 open. However, any open that hasn't added itself to |
| 5120 | * the fi_stateids list also hasn't called break_lease yet; so, |
| 5121 | * they'll break this lease soon enough. |
| 5122 | */ |
| 5123 | return 0; |
| 5124 | } |
| 5125 | |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 5126 | static struct nfs4_delegation * |
| 5127 | nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh, |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 5128 | struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate) |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 5129 | { |
J. Bruce Fields | 68b18f5 | 2018-02-19 11:38:33 -0500 | [diff] [blame] | 5130 | int status = 0; |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 5131 | struct nfs4_delegation *dp; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5132 | struct nfsd_file *nf; |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 5133 | struct file_lock *fl; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 5134 | |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 5135 | /* |
| 5136 | * The fi_had_conflict and nfs_get_existing_delegation checks |
| 5137 | * here are just optimizations; we'll need to recheck them at |
| 5138 | * the end: |
| 5139 | */ |
J. Bruce Fields | bf7bd3e | 2013-08-15 16:55:26 -0400 | [diff] [blame] | 5140 | if (fp->fi_had_conflict) |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 5141 | return ERR_PTR(-EAGAIN); |
| 5142 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5143 | nf = find_readable_file(fp); |
| 5144 | if (!nf) { |
J. Bruce Fields | aba2072 | 2021-04-16 14:00:18 -0400 | [diff] [blame] | 5145 | /* |
| 5146 | * We probably could attempt another open and get a read |
| 5147 | * delegation, but for now, don't bother until the |
| 5148 | * client actually sends us one. |
| 5149 | */ |
| 5150 | return ERR_PTR(-EAGAIN); |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 5151 | } |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 5152 | spin_lock(&state_lock); |
| 5153 | spin_lock(&fp->fi_lock); |
J. Bruce Fields | 68b18f5 | 2018-02-19 11:38:33 -0500 | [diff] [blame] | 5154 | if (nfs4_delegation_exists(clp, fp)) |
| 5155 | status = -EAGAIN; |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 5156 | else if (!fp->fi_deleg_file) { |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5157 | fp->fi_deleg_file = nf; |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 5158 | /* increment early to prevent fi_deleg_file from being |
| 5159 | * cleared */ |
| 5160 | fp->fi_delegees = 1; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5161 | nf = NULL; |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 5162 | } else |
| 5163 | fp->fi_delegees++; |
| 5164 | spin_unlock(&fp->fi_lock); |
| 5165 | spin_unlock(&state_lock); |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5166 | if (nf) |
| 5167 | nfsd_file_put(nf); |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 5168 | if (status) |
| 5169 | return ERR_PTR(status); |
| 5170 | |
| 5171 | status = -ENOMEM; |
| 5172 | dp = alloc_init_deleg(clp, fp, fh, odstate); |
| 5173 | if (!dp) |
| 5174 | goto out_delegees; |
| 5175 | |
| 5176 | fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ); |
| 5177 | if (!fl) |
Andrew Elble | bd8d725 | 2018-10-05 09:32:08 -0400 | [diff] [blame] | 5178 | goto out_clnt_odstate; |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 5179 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5180 | status = vfs_setlease(fp->fi_deleg_file->nf_file, fl->fl_type, &fl, NULL); |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 5181 | if (fl) |
| 5182 | locks_free_lock(fl); |
| 5183 | if (status) |
| 5184 | goto out_clnt_odstate; |
J. Bruce Fields | aba2072 | 2021-04-16 14:00:18 -0400 | [diff] [blame] | 5185 | status = nfsd4_check_conflicting_opens(clp, fp); |
| 5186 | if (status) |
| 5187 | goto out_unlock; |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 5188 | |
| 5189 | spin_lock(&state_lock); |
| 5190 | spin_lock(&fp->fi_lock); |
| 5191 | if (fp->fi_had_conflict) |
| 5192 | status = -EAGAIN; |
| 5193 | else |
| 5194 | status = hash_delegation_locked(dp, fp); |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 5195 | spin_unlock(&fp->fi_lock); |
| 5196 | spin_unlock(&state_lock); |
| 5197 | |
| 5198 | if (status) |
Andrew Elble | 692ad28 | 2018-04-18 17:04:37 -0400 | [diff] [blame] | 5199 | goto out_unlock; |
| 5200 | |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 5201 | return dp; |
Andrew Elble | 692ad28 | 2018-04-18 17:04:37 -0400 | [diff] [blame] | 5202 | out_unlock: |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5203 | vfs_setlease(fp->fi_deleg_file->nf_file, F_UNLCK, NULL, (void **)&dp); |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 5204 | out_clnt_odstate: |
| 5205 | put_clnt_odstate(dp->dl_clnt_odstate); |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 5206 | nfs4_put_stid(&dp->dl_stid); |
| 5207 | out_delegees: |
| 5208 | put_deleg_file(fp); |
| 5209 | return ERR_PTR(status); |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 5210 | } |
| 5211 | |
Benny Halevy | 4aa8913 | 2012-02-21 14:16:44 -0800 | [diff] [blame] | 5212 | static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status) |
| 5213 | { |
| 5214 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 5215 | if (status == -EAGAIN) |
| 5216 | open->op_why_no_deleg = WND4_CONTENTION; |
| 5217 | else { |
| 5218 | open->op_why_no_deleg = WND4_RESOURCE; |
| 5219 | switch (open->op_deleg_want) { |
| 5220 | case NFS4_SHARE_WANT_READ_DELEG: |
| 5221 | case NFS4_SHARE_WANT_WRITE_DELEG: |
| 5222 | case NFS4_SHARE_WANT_ANY_DELEG: |
| 5223 | break; |
| 5224 | case NFS4_SHARE_WANT_CANCEL: |
| 5225 | open->op_why_no_deleg = WND4_CANCELLED; |
| 5226 | break; |
| 5227 | case NFS4_SHARE_WANT_NO_DELEG: |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 5228 | WARN_ON_ONCE(1); |
Benny Halevy | 4aa8913 | 2012-02-21 14:16:44 -0800 | [diff] [blame] | 5229 | } |
| 5230 | } |
| 5231 | } |
| 5232 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5233 | /* |
| 5234 | * Attempt to hand out a delegation. |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 5235 | * |
| 5236 | * Note we don't support write delegations, and won't until the vfs has |
| 5237 | * proper support for them. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5238 | */ |
| 5239 | static void |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame] | 5240 | nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, |
| 5241 | struct nfs4_ol_stateid *stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5242 | { |
| 5243 | struct nfs4_delegation *dp; |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame] | 5244 | struct nfs4_openowner *oo = openowner(stp->st_stateowner); |
| 5245 | struct nfs4_client *clp = stp->st_stid.sc_client; |
J. Bruce Fields | 14a24e9 | 2010-12-10 19:02:49 -0500 | [diff] [blame] | 5246 | int cb_up; |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 5247 | int status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5248 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5249 | cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client); |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 5250 | open->op_recall = 0; |
| 5251 | switch (open->op_claim_type) { |
| 5252 | case NFS4_OPEN_CLAIM_PREVIOUS: |
J. Bruce Fields | 2bf2387 | 2010-03-08 12:37:27 -0500 | [diff] [blame] | 5253 | if (!cb_up) |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 5254 | open->op_recall = 1; |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 5255 | if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ) |
| 5256 | goto out_no_deleg; |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 5257 | break; |
| 5258 | case NFS4_OPEN_CLAIM_NULL: |
Ming Chen | ed47b06 | 2014-01-09 21:26:10 +0000 | [diff] [blame] | 5259 | case NFS4_OPEN_CLAIM_FH: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 5260 | /* |
| 5261 | * Let's not give out any delegations till everyone's |
J. Bruce Fields | c87fb4a | 2015-08-06 12:47:02 -0400 | [diff] [blame] | 5262 | * had the chance to reclaim theirs, *and* until |
| 5263 | * NLM locks have all been reclaimed: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 5264 | */ |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame] | 5265 | if (locks_in_grace(clp->net)) |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 5266 | goto out_no_deleg; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 5267 | if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED)) |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 5268 | goto out_no_deleg; |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 5269 | break; |
| 5270 | default: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 5271 | goto out_no_deleg; |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 5272 | } |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 5273 | dp = nfs4_set_delegation(clp, fh, stp->st_stid.sc_file, stp->st_clnt_odstate); |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 5274 | if (IS_ERR(dp)) |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 5275 | goto out_no_deleg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5276 | |
J. Bruce Fields | d5477a8 | 2011-09-08 12:07:44 -0400 | [diff] [blame] | 5277 | memcpy(&open->op_delegate_stateid, &dp->dl_stid.sc_stateid, sizeof(dp->dl_stid.sc_stateid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5278 | |
Hou Tao | 3caf917 | 2020-08-28 15:02:55 +0800 | [diff] [blame] | 5279 | trace_nfsd_deleg_read(&dp->dl_stid.sc_stateid); |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 5280 | open->op_delegate_type = NFS4_OPEN_DELEGATE_READ; |
Trond Myklebust | 67cb127 | 2014-07-29 21:34:17 -0400 | [diff] [blame] | 5281 | nfs4_put_stid(&dp->dl_stid); |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 5282 | return; |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 5283 | out_no_deleg: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 5284 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE; |
| 5285 | if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS && |
J. Bruce Fields | d08d32e | 2013-06-21 11:05:32 -0400 | [diff] [blame] | 5286 | open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) { |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 5287 | dprintk("NFSD: WARNING: refusing delegation reclaim\n"); |
J. Bruce Fields | d08d32e | 2013-06-21 11:05:32 -0400 | [diff] [blame] | 5288 | open->op_recall = 1; |
| 5289 | } |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 5290 | |
| 5291 | /* 4.1 client asking for a delegation? */ |
| 5292 | if (open->op_deleg_want) |
| 5293 | nfsd4_open_deleg_none_ext(open, status); |
| 5294 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5295 | } |
| 5296 | |
Benny Halevy | e27f49c | 2012-02-21 14:16:54 -0800 | [diff] [blame] | 5297 | static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open, |
| 5298 | struct nfs4_delegation *dp) |
| 5299 | { |
| 5300 | if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG && |
| 5301 | dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) { |
| 5302 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 5303 | open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE; |
| 5304 | } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG && |
| 5305 | dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) { |
| 5306 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 5307 | open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE; |
| 5308 | } |
| 5309 | /* Otherwise the client must be confused wanting a delegation |
| 5310 | * it already has, therefore we don't return |
| 5311 | * NFS4_OPEN_DELEGATE_NONE_EXT and reason. |
| 5312 | */ |
| 5313 | } |
| 5314 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5315 | __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5316 | nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open) |
| 5317 | { |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 5318 | struct nfsd4_compoundres *resp = rqstp->rq_resp; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5319 | struct nfs4_client *cl = open->op_openowner->oo_owner.so_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5320 | struct nfs4_file *fp = NULL; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5321 | struct nfs4_ol_stateid *stp = NULL; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 5322 | struct nfs4_delegation *dp = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5323 | __be32 status; |
Trond Myklebust | d8a1a00 | 2017-11-03 08:00:11 -0400 | [diff] [blame] | 5324 | bool new_stp = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5325 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5326 | /* |
| 5327 | * Lookup file; if found, lookup stateid and check open request, |
| 5328 | * and check for delegations in the process of being recalled. |
| 5329 | * If not found, create the nfs4_file struct |
| 5330 | */ |
J. Bruce Fields | f9b60e2 | 2021-04-16 14:00:15 -0400 | [diff] [blame] | 5331 | fp = find_or_add_file(open->op_file, current_fh); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 5332 | if (fp != open->op_file) { |
J. Bruce Fields | 41d2266 | 2013-03-21 15:49:47 -0400 | [diff] [blame] | 5333 | status = nfs4_check_deleg(cl, open, &dp); |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 5334 | if (status) |
| 5335 | goto out; |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 5336 | stp = nfsd4_find_and_lock_existing_open(fp, open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5337 | } else { |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 5338 | open->op_file = NULL; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 5339 | status = nfserr_bad_stateid; |
J. Bruce Fields | 8b289b2 | 2011-10-19 11:52:12 -0400 | [diff] [blame] | 5340 | if (nfsd4_is_deleg_cur(open)) |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 5341 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5342 | } |
| 5343 | |
Trond Myklebust | d8a1a00 | 2017-11-03 08:00:11 -0400 | [diff] [blame] | 5344 | if (!stp) { |
| 5345 | stp = init_open_stateid(fp, open); |
| 5346 | if (!open->op_stp) |
| 5347 | new_stp = true; |
| 5348 | } |
| 5349 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5350 | /* |
| 5351 | * OPEN the file, or upgrade an existing OPEN. |
| 5352 | * If truncate fails, the OPEN fails. |
Trond Myklebust | d8a1a00 | 2017-11-03 08:00:11 -0400 | [diff] [blame] | 5353 | * |
| 5354 | * stp is already locked. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5355 | */ |
Trond Myklebust | d8a1a00 | 2017-11-03 08:00:11 -0400 | [diff] [blame] | 5356 | if (!new_stp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5357 | /* Stateid was found, this is an OPEN upgrade */ |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 5358 | status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open); |
Jeff Layton | 35a92fe | 2015-09-17 07:47:08 -0400 | [diff] [blame] | 5359 | if (status) { |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 5360 | mutex_unlock(&stp->st_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5361 | goto out; |
Jeff Layton | 35a92fe | 2015-09-17 07:47:08 -0400 | [diff] [blame] | 5362 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5363 | } else { |
Jeff Layton | 6eb3a1d | 2014-07-10 14:07:31 -0400 | [diff] [blame] | 5364 | status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open); |
| 5365 | if (status) { |
Trond Myklebust | d8a1a00 | 2017-11-03 08:00:11 -0400 | [diff] [blame] | 5366 | stp->st_stid.sc_type = NFS4_CLOSED_STID; |
Jeff Layton | 6eb3a1d | 2014-07-10 14:07:31 -0400 | [diff] [blame] | 5367 | release_open_stateid(stp); |
Trond Myklebust | d8a1a00 | 2017-11-03 08:00:11 -0400 | [diff] [blame] | 5368 | mutex_unlock(&stp->st_mutex); |
Jeff Layton | 6eb3a1d | 2014-07-10 14:07:31 -0400 | [diff] [blame] | 5369 | goto out; |
| 5370 | } |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 5371 | |
| 5372 | stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp, |
| 5373 | open->op_odstate); |
| 5374 | if (stp->st_clnt_odstate == open->op_odstate) |
| 5375 | open->op_odstate = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5376 | } |
Trond Myklebust | d8a1a00 | 2017-11-03 08:00:11 -0400 | [diff] [blame] | 5377 | |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 5378 | nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid); |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 5379 | mutex_unlock(&stp->st_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5380 | |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 5381 | if (nfsd4_has_session(&resp->cstate)) { |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 5382 | if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) { |
| 5383 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 5384 | open->op_why_no_deleg = WND4_NOT_WANTED; |
| 5385 | goto nodeleg; |
| 5386 | } |
| 5387 | } |
| 5388 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5389 | /* |
| 5390 | * Attempt to hand out a delegation. No error return, because the |
| 5391 | * OPEN succeeds even if we fail. |
| 5392 | */ |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame] | 5393 | nfs4_open_delegation(current_fh, open, stp); |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 5394 | nodeleg: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5395 | status = nfs_ok; |
Hou Tao | 3caf917 | 2020-08-28 15:02:55 +0800 | [diff] [blame] | 5396 | trace_nfsd_open(&stp->st_stid.sc_stateid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5397 | out: |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 5398 | /* 4.1 client trying to upgrade/downgrade delegation? */ |
| 5399 | if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp && |
Benny Halevy | e27f49c | 2012-02-21 14:16:54 -0800 | [diff] [blame] | 5400 | open->op_deleg_want) |
| 5401 | nfsd4_deleg_xgrade_none_ext(open, dp); |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 5402 | |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 5403 | if (fp) |
| 5404 | put_nfs4_file(fp); |
NeilBrown | 3751517 | 2005-07-07 17:59:16 -0700 | [diff] [blame] | 5405 | if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS) |
Kinglong Mee | 8718602 | 2015-07-13 17:32:59 +0800 | [diff] [blame] | 5406 | open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5407 | /* |
| 5408 | * To finish the open response, we just need to set the rflags. |
| 5409 | */ |
| 5410 | open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX; |
Jeff Layton | 19e4c34 | 2016-09-16 16:28:27 -0400 | [diff] [blame] | 5411 | if (nfsd4_has_session(&resp->cstate)) |
| 5412 | open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK; |
| 5413 | else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5414 | open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM; |
Jeff Layton | 19e4c34 | 2016-09-16 16:28:27 -0400 | [diff] [blame] | 5415 | |
Trond Myklebust | dcd94cc | 2014-07-29 21:34:18 -0400 | [diff] [blame] | 5416 | if (dp) |
| 5417 | nfs4_put_stid(&dp->dl_stid); |
Trond Myklebust | d6f2bc5 | 2014-07-29 21:34:19 -0400 | [diff] [blame] | 5418 | if (stp) |
| 5419 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5420 | |
| 5421 | return status; |
| 5422 | } |
| 5423 | |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 5424 | void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate, |
Jeff Layton | 4229789 | 2015-03-23 10:53:44 -0400 | [diff] [blame] | 5425 | struct nfsd4_open *open) |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 5426 | { |
| 5427 | if (open->op_openowner) { |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 5428 | struct nfs4_stateowner *so = &open->op_openowner->oo_owner; |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 5429 | |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 5430 | nfsd4_cstate_assign_replay(cstate, so); |
| 5431 | nfs4_put_stateowner(so); |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 5432 | } |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 5433 | if (open->op_file) |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 5434 | kmem_cache_free(file_slab, open->op_file); |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 5435 | if (open->op_stp) |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 5436 | nfs4_put_stid(&open->op_stp->st_stid); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 5437 | if (open->op_odstate) |
| 5438 | kmem_cache_free(odstate_slab, open->op_odstate); |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 5439 | } |
| 5440 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5441 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 5442 | nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5443 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5444 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5445 | clientid_t *clid = &u->renew; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5446 | struct nfs4_client *clp; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5447 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 5448 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5449 | |
Chuck Lever | dd5e3fb | 2020-04-05 11:42:29 -0400 | [diff] [blame] | 5450 | trace_nfsd_clid_renew(clid); |
J. Bruce Fields | f71475b | 2021-01-21 17:57:43 -0500 | [diff] [blame] | 5451 | status = set_client(clid, cstate, nn); |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 5452 | if (status) |
J. Bruce Fields | b4587eb | 2021-01-21 17:57:39 -0500 | [diff] [blame] | 5453 | return status; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 5454 | clp = cstate->clp; |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 5455 | if (!list_empty(&clp->cl_delegations) |
J. Bruce Fields | 77a3569 | 2010-04-30 18:51:44 -0400 | [diff] [blame] | 5456 | && clp->cl_cb_state != NFSD4_CB_UP) |
J. Bruce Fields | b4587eb | 2021-01-21 17:57:39 -0500 | [diff] [blame] | 5457 | return nfserr_cb_path_down; |
| 5458 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5459 | } |
| 5460 | |
Jeff Layton | 7f5ef2e | 2014-09-12 16:40:21 -0400 | [diff] [blame] | 5461 | void |
Stanislav Kinsbursky | 12760c6 | 2012-11-14 18:22:12 +0300 | [diff] [blame] | 5462 | nfsd4_end_grace(struct nfsd_net *nn) |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 5463 | { |
Jeff Layton | 33dcc48 | 2012-04-10 11:08:48 -0400 | [diff] [blame] | 5464 | /* do nothing if grace period already ended */ |
Stanislav Kinsbursky | a51c84e | 2012-07-25 16:57:37 +0400 | [diff] [blame] | 5465 | if (nn->grace_ended) |
Jeff Layton | 33dcc48 | 2012-04-10 11:08:48 -0400 | [diff] [blame] | 5466 | return; |
| 5467 | |
Chuck Lever | dd5e3fb | 2020-04-05 11:42:29 -0400 | [diff] [blame] | 5468 | trace_nfsd_grace_complete(nn); |
Stanislav Kinsbursky | a51c84e | 2012-07-25 16:57:37 +0400 | [diff] [blame] | 5469 | nn->grace_ended = true; |
J. Bruce Fields | 70b2823 | 2014-09-16 17:37:32 -0400 | [diff] [blame] | 5470 | /* |
| 5471 | * If the server goes down again right now, an NFSv4 |
| 5472 | * client will still be allowed to reclaim after it comes back up, |
| 5473 | * even if it hasn't yet had a chance to reclaim state this time. |
| 5474 | * |
| 5475 | */ |
Jeff Layton | 919b804 | 2014-09-12 16:40:20 -0400 | [diff] [blame] | 5476 | nfsd4_record_grace_done(nn); |
J. Bruce Fields | 70b2823 | 2014-09-16 17:37:32 -0400 | [diff] [blame] | 5477 | /* |
| 5478 | * At this point, NFSv4 clients can still reclaim. But if the |
| 5479 | * server crashes, any that have not yet reclaimed will be out |
| 5480 | * of luck on the next boot. |
| 5481 | * |
| 5482 | * (NFSv4.1+ clients are considered to have reclaimed once they |
| 5483 | * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to |
| 5484 | * have reclaimed after their first OPEN.) |
| 5485 | */ |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 5486 | locks_end_grace(&nn->nfsd4_manager); |
J. Bruce Fields | 70b2823 | 2014-09-16 17:37:32 -0400 | [diff] [blame] | 5487 | /* |
| 5488 | * At this point, and once lockd and/or any other containers |
| 5489 | * exit their grace period, further reclaims will fail and |
| 5490 | * regular locking can resume. |
| 5491 | */ |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 5492 | } |
| 5493 | |
J. Bruce Fields | 03f318c | 2018-06-08 12:28:47 -0400 | [diff] [blame] | 5494 | /* |
| 5495 | * If we've waited a lease period but there are still clients trying to |
| 5496 | * reclaim, wait a little longer to give them a chance to finish. |
| 5497 | */ |
| 5498 | static bool clients_still_reclaiming(struct nfsd_net *nn) |
| 5499 | { |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 5500 | time64_t double_grace_period_end = nn->boot_time + |
| 5501 | 2 * nn->nfsd4_lease; |
J. Bruce Fields | 03f318c | 2018-06-08 12:28:47 -0400 | [diff] [blame] | 5502 | |
Scott Mayhew | 362063a | 2019-03-26 18:06:28 -0400 | [diff] [blame] | 5503 | if (nn->track_reclaim_completes && |
| 5504 | atomic_read(&nn->nr_reclaim_complete) == |
| 5505 | nn->reclaim_str_hashtbl_size) |
| 5506 | return false; |
J. Bruce Fields | 03f318c | 2018-06-08 12:28:47 -0400 | [diff] [blame] | 5507 | if (!nn->somebody_reclaimed) |
| 5508 | return false; |
| 5509 | nn->somebody_reclaimed = false; |
| 5510 | /* |
| 5511 | * If we've given them *two* lease times to reclaim, and they're |
| 5512 | * still not done, give up: |
| 5513 | */ |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 5514 | if (ktime_get_boottime_seconds() > double_grace_period_end) |
J. Bruce Fields | 03f318c | 2018-06-08 12:28:47 -0400 | [diff] [blame] | 5515 | return false; |
| 5516 | return true; |
| 5517 | } |
| 5518 | |
J. Bruce Fields | 7f7e7a4 | 2021-03-02 10:46:23 -0500 | [diff] [blame] | 5519 | struct laundry_time { |
| 5520 | time64_t cutoff; |
| 5521 | time64_t new_timeo; |
| 5522 | }; |
| 5523 | |
| 5524 | static bool state_expired(struct laundry_time *lt, time64_t last_refresh) |
| 5525 | { |
| 5526 | time64_t time_remaining; |
| 5527 | |
| 5528 | if (last_refresh < lt->cutoff) |
| 5529 | return true; |
| 5530 | time_remaining = last_refresh - lt->cutoff; |
| 5531 | lt->new_timeo = min(lt->new_timeo, time_remaining); |
| 5532 | return false; |
| 5533 | } |
| 5534 | |
Dai Ngo | f4e44b3 | 2021-05-21 15:09:37 -0400 | [diff] [blame] | 5535 | #ifdef CONFIG_NFSD_V4_2_INTER_SSC |
| 5536 | void nfsd4_ssc_init_umount_work(struct nfsd_net *nn) |
| 5537 | { |
| 5538 | spin_lock_init(&nn->nfsd_ssc_lock); |
| 5539 | INIT_LIST_HEAD(&nn->nfsd_ssc_mount_list); |
| 5540 | init_waitqueue_head(&nn->nfsd_ssc_waitq); |
| 5541 | } |
| 5542 | EXPORT_SYMBOL_GPL(nfsd4_ssc_init_umount_work); |
| 5543 | |
| 5544 | /* |
| 5545 | * This is called when nfsd is being shutdown, after all inter_ssc |
| 5546 | * cleanup were done, to destroy the ssc delayed unmount list. |
| 5547 | */ |
| 5548 | static void nfsd4_ssc_shutdown_umount(struct nfsd_net *nn) |
| 5549 | { |
Dai Ngo | f47dc2d | 2021-06-03 20:02:26 -0400 | [diff] [blame] | 5550 | struct nfsd4_ssc_umount_item *ni = NULL; |
Dai Ngo | f4e44b3 | 2021-05-21 15:09:37 -0400 | [diff] [blame] | 5551 | struct nfsd4_ssc_umount_item *tmp; |
| 5552 | |
| 5553 | spin_lock(&nn->nfsd_ssc_lock); |
| 5554 | list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) { |
| 5555 | list_del(&ni->nsui_list); |
| 5556 | spin_unlock(&nn->nfsd_ssc_lock); |
| 5557 | mntput(ni->nsui_vfsmount); |
| 5558 | kfree(ni); |
| 5559 | spin_lock(&nn->nfsd_ssc_lock); |
| 5560 | } |
| 5561 | spin_unlock(&nn->nfsd_ssc_lock); |
| 5562 | } |
| 5563 | |
| 5564 | static void nfsd4_ssc_expire_umount(struct nfsd_net *nn) |
| 5565 | { |
| 5566 | bool do_wakeup = false; |
Colin Ian King | 8e70bf2 | 2021-09-25 23:58:41 +0100 | [diff] [blame] | 5567 | struct nfsd4_ssc_umount_item *ni = NULL; |
Dai Ngo | f4e44b3 | 2021-05-21 15:09:37 -0400 | [diff] [blame] | 5568 | struct nfsd4_ssc_umount_item *tmp; |
| 5569 | |
| 5570 | spin_lock(&nn->nfsd_ssc_lock); |
| 5571 | list_for_each_entry_safe(ni, tmp, &nn->nfsd_ssc_mount_list, nsui_list) { |
| 5572 | if (time_after(jiffies, ni->nsui_expire)) { |
| 5573 | if (refcount_read(&ni->nsui_refcnt) > 1) |
| 5574 | continue; |
| 5575 | |
| 5576 | /* mark being unmount */ |
| 5577 | ni->nsui_busy = true; |
| 5578 | spin_unlock(&nn->nfsd_ssc_lock); |
| 5579 | mntput(ni->nsui_vfsmount); |
| 5580 | spin_lock(&nn->nfsd_ssc_lock); |
| 5581 | |
| 5582 | /* waiters need to start from begin of list */ |
| 5583 | list_del(&ni->nsui_list); |
| 5584 | kfree(ni); |
| 5585 | |
| 5586 | /* wakeup ssc_connect waiters */ |
| 5587 | do_wakeup = true; |
| 5588 | continue; |
| 5589 | } |
| 5590 | break; |
| 5591 | } |
| 5592 | if (do_wakeup) |
| 5593 | wake_up_all(&nn->nfsd_ssc_waitq); |
| 5594 | spin_unlock(&nn->nfsd_ssc_lock); |
| 5595 | } |
| 5596 | #endif |
| 5597 | |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 5598 | static time64_t |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 5599 | nfs4_laundromat(struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5600 | { |
| 5601 | struct nfs4_client *clp; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5602 | struct nfs4_openowner *oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5603 | struct nfs4_delegation *dp; |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 5604 | struct nfs4_ol_stateid *stp; |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 5605 | struct nfsd4_blocked_lock *nbl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5606 | struct list_head *pos, *next, reaplist; |
J. Bruce Fields | 7f7e7a4 | 2021-03-02 10:46:23 -0500 | [diff] [blame] | 5607 | struct laundry_time lt = { |
| 5608 | .cutoff = ktime_get_boottime_seconds() - nn->nfsd4_lease, |
| 5609 | .new_timeo = nn->nfsd4_lease |
| 5610 | }; |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 5611 | struct nfs4_cpntf_state *cps; |
| 5612 | copy_stateid_t *cps_t; |
| 5613 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5614 | |
J. Bruce Fields | 03f318c | 2018-06-08 12:28:47 -0400 | [diff] [blame] | 5615 | if (clients_still_reclaiming(nn)) { |
J. Bruce Fields | 7f7e7a4 | 2021-03-02 10:46:23 -0500 | [diff] [blame] | 5616 | lt.new_timeo = 0; |
J. Bruce Fields | 03f318c | 2018-06-08 12:28:47 -0400 | [diff] [blame] | 5617 | goto out; |
| 5618 | } |
Stanislav Kinsbursky | 12760c6 | 2012-11-14 18:22:12 +0300 | [diff] [blame] | 5619 | nfsd4_end_grace(nn); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 5620 | INIT_LIST_HEAD(&reaplist); |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 5621 | |
| 5622 | spin_lock(&nn->s2s_cp_lock); |
| 5623 | idr_for_each_entry(&nn->s2s_cp_stateids, cps_t, i) { |
| 5624 | cps = container_of(cps_t, struct nfs4_cpntf_state, cp_stateid); |
| 5625 | if (cps->cp_stateid.sc_type == NFS4_COPYNOTIFY_STID && |
J. Bruce Fields | 7f7e7a4 | 2021-03-02 10:46:23 -0500 | [diff] [blame] | 5626 | state_expired(<, cps->cpntf_time)) |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 5627 | _free_cpntf_state_locked(nn, cps); |
| 5628 | } |
| 5629 | spin_unlock(&nn->s2s_cp_lock); |
| 5630 | |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 5631 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 5ed58bb | 2012-11-14 18:21:56 +0300 | [diff] [blame] | 5632 | list_for_each_safe(pos, next, &nn->client_lru) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5633 | clp = list_entry(pos, struct nfs4_client, cl_lru); |
J. Bruce Fields | 7f7e7a4 | 2021-03-02 10:46:23 -0500 | [diff] [blame] | 5634 | if (!state_expired(<, clp->cl_time)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5635 | break; |
Chuck Lever | 2958d2e | 2021-05-14 15:56:06 -0400 | [diff] [blame] | 5636 | if (mark_client_expired_locked(clp)) |
Benny Halevy | d768298 | 2010-05-12 00:13:54 +0300 | [diff] [blame] | 5637 | continue; |
Trond Myklebust | 4864af9 | 2014-07-30 08:27:03 -0400 | [diff] [blame] | 5638 | list_add(&clp->cl_lru, &reaplist); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 5639 | } |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 5640 | spin_unlock(&nn->client_lock); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 5641 | list_for_each_safe(pos, next, &reaplist) { |
| 5642 | clp = list_entry(pos, struct nfs4_client, cl_lru); |
Chuck Lever | dd5e3fb | 2020-04-05 11:42:29 -0400 | [diff] [blame] | 5643 | trace_nfsd_clid_purged(&clp->cl_clientid); |
Trond Myklebust | 4864af9 | 2014-07-30 08:27:03 -0400 | [diff] [blame] | 5644 | list_del_init(&clp->cl_lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5645 | expire_client(clp); |
| 5646 | } |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5647 | spin_lock(&state_lock); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 5648 | list_for_each_safe(pos, next, &nn->del_recall_lru) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5649 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
J. Bruce Fields | 7f7e7a4 | 2021-03-02 10:46:23 -0500 | [diff] [blame] | 5650 | if (!state_expired(<, dp->dl_time)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5651 | break; |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 5652 | WARN_ON(!unhash_delegation_locked(dp)); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 5653 | list_add(&dp->dl_recall_lru, &reaplist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5654 | } |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5655 | spin_unlock(&state_lock); |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 5656 | while (!list_empty(&reaplist)) { |
| 5657 | dp = list_first_entry(&reaplist, struct nfs4_delegation, |
| 5658 | dl_recall_lru); |
| 5659 | list_del_init(&dp->dl_recall_lru); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 5660 | revoke_delegation(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5661 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 5662 | |
| 5663 | spin_lock(&nn->client_lock); |
| 5664 | while (!list_empty(&nn->close_lru)) { |
| 5665 | oo = list_first_entry(&nn->close_lru, struct nfs4_openowner, |
| 5666 | oo_close_lru); |
J. Bruce Fields | 7f7e7a4 | 2021-03-02 10:46:23 -0500 | [diff] [blame] | 5667 | if (!state_expired(<, oo->oo_time)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5668 | break; |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 5669 | list_del_init(&oo->oo_close_lru); |
| 5670 | stp = oo->oo_last_closed_stid; |
| 5671 | oo->oo_last_closed_stid = NULL; |
| 5672 | spin_unlock(&nn->client_lock); |
| 5673 | nfs4_put_stid(&stp->st_stid); |
| 5674 | spin_lock(&nn->client_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5675 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 5676 | spin_unlock(&nn->client_lock); |
| 5677 | |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 5678 | /* |
| 5679 | * It's possible for a client to try and acquire an already held lock |
| 5680 | * that is being held for a long time, and then lose interest in it. |
| 5681 | * So, we clean out any un-revisited request after a lease period |
| 5682 | * under the assumption that the client is no longer interested. |
| 5683 | * |
| 5684 | * RFC5661, sec. 9.6 states that the client must not rely on getting |
| 5685 | * notifications and must continue to poll for locks, even when the |
| 5686 | * server supports them. Thus this shouldn't lead to clients blocking |
| 5687 | * indefinitely once the lock does become free. |
| 5688 | */ |
| 5689 | BUG_ON(!list_empty(&reaplist)); |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 5690 | spin_lock(&nn->blocked_locks_lock); |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 5691 | while (!list_empty(&nn->blocked_locks_lru)) { |
| 5692 | nbl = list_first_entry(&nn->blocked_locks_lru, |
| 5693 | struct nfsd4_blocked_lock, nbl_lru); |
J. Bruce Fields | 7f7e7a4 | 2021-03-02 10:46:23 -0500 | [diff] [blame] | 5694 | if (!state_expired(<, nbl->nbl_time)) |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 5695 | break; |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 5696 | list_move(&nbl->nbl_lru, &reaplist); |
| 5697 | list_del_init(&nbl->nbl_list); |
| 5698 | } |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 5699 | spin_unlock(&nn->blocked_locks_lock); |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 5700 | |
| 5701 | while (!list_empty(&reaplist)) { |
Naofumi Honda | 64ebe124 | 2017-11-09 10:57:16 -0500 | [diff] [blame] | 5702 | nbl = list_first_entry(&reaplist, |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 5703 | struct nfsd4_blocked_lock, nbl_lru); |
| 5704 | list_del_init(&nbl->nbl_lru); |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 5705 | free_blocked_lock(nbl); |
| 5706 | } |
Dai Ngo | f4e44b3 | 2021-05-21 15:09:37 -0400 | [diff] [blame] | 5707 | #ifdef CONFIG_NFSD_V4_2_INTER_SSC |
| 5708 | /* service the server-to-server copy delayed unmount list */ |
| 5709 | nfsd4_ssc_expire_umount(nn); |
| 5710 | #endif |
J. Bruce Fields | 03f318c | 2018-06-08 12:28:47 -0400 | [diff] [blame] | 5711 | out: |
J. Bruce Fields | 7f7e7a4 | 2021-03-02 10:46:23 -0500 | [diff] [blame] | 5712 | return max_t(time64_t, lt.new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5713 | } |
| 5714 | |
Harvey Harrison | a254b24 | 2008-02-20 12:49:00 -0800 | [diff] [blame] | 5715 | static struct workqueue_struct *laundry_wq; |
| 5716 | static void laundromat_main(struct work_struct *); |
Harvey Harrison | a254b24 | 2008-02-20 12:49:00 -0800 | [diff] [blame] | 5717 | |
| 5718 | static void |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 5719 | laundromat_main(struct work_struct *laundry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5720 | { |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 5721 | time64_t t; |
Geliang Tang | 2e55f3a | 2016-01-01 22:06:28 +0800 | [diff] [blame] | 5722 | struct delayed_work *dwork = to_delayed_work(laundry); |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 5723 | struct nfsd_net *nn = container_of(dwork, struct nfsd_net, |
| 5724 | laundromat_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5725 | |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 5726 | t = nfs4_laundromat(nn); |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 5727 | queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5728 | } |
| 5729 | |
Jeff Layton | 8fcd461 | 2015-07-30 06:57:46 -0400 | [diff] [blame] | 5730 | static inline __be32 nfs4_check_fh(struct svc_fh *fhp, struct nfs4_stid *stp) |
NeilBrown | f881651 | 2005-07-07 17:59:25 -0700 | [diff] [blame] | 5731 | { |
Jeff Layton | 8fcd461 | 2015-07-30 06:57:46 -0400 | [diff] [blame] | 5732 | if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle)) |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 5733 | return nfserr_bad_stateid; |
| 5734 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5735 | } |
| 5736 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5737 | static |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5738 | __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5739 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5740 | __be32 status = nfserr_openmode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5741 | |
J. Bruce Fields | 0292191 | 2010-07-29 15:16:59 -0400 | [diff] [blame] | 5742 | /* For lock stateid's, we test the parent open, not the lock: */ |
| 5743 | if (stp->st_openstp) |
| 5744 | stp = stp->st_openstp; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 5745 | if ((flags & WR_STATE) && !access_permit_write(stp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5746 | goto out; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 5747 | if ((flags & RD_STATE) && !access_permit_read(stp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5748 | goto out; |
| 5749 | status = nfs_ok; |
| 5750 | out: |
| 5751 | return status; |
| 5752 | } |
| 5753 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5754 | static inline __be32 |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 5755 | check_special_stateids(struct net *net, svc_fh *current_fh, stateid_t *stateid, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5756 | { |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 5757 | if (ONE_STATEID(stateid) && (flags & RD_STATE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5758 | return nfs_ok; |
J. Bruce Fields | c87fb4a | 2015-08-06 12:47:02 -0400 | [diff] [blame] | 5759 | else if (opens_in_grace(net)) { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 5760 | /* Answer in remaining cases depends on existence of |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5761 | * conflicting state; so we must wait out the grace period. */ |
| 5762 | return nfserr_grace; |
| 5763 | } else if (flags & WR_STATE) |
| 5764 | return nfs4_share_conflict(current_fh, |
| 5765 | NFS4_SHARE_DENY_WRITE); |
| 5766 | else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */ |
| 5767 | return nfs4_share_conflict(current_fh, |
| 5768 | NFS4_SHARE_DENY_READ); |
| 5769 | } |
| 5770 | |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 5771 | static __be32 check_stateid_generation(stateid_t *in, stateid_t *ref, bool has_session) |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 5772 | { |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 5773 | /* |
| 5774 | * When sessions are used the stateid generation number is ignored |
| 5775 | * when it is zero. |
| 5776 | */ |
J. Bruce Fields | 28dde24 | 2011-08-22 10:07:12 -0400 | [diff] [blame] | 5777 | if (has_session && in->si_generation == 0) |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 5778 | return nfs_ok; |
| 5779 | |
| 5780 | if (in->si_generation == ref->si_generation) |
| 5781 | return nfs_ok; |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 5782 | |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 5783 | /* If the client sends us a stateid from the future, it's buggy: */ |
Jeff Layton | 14b7f4a | 2016-05-05 06:53:47 -0400 | [diff] [blame] | 5784 | if (nfsd4_stateid_generation_after(in, ref)) |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 5785 | return nfserr_bad_stateid; |
| 5786 | /* |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 5787 | * However, we could see a stateid from the past, even from a |
| 5788 | * non-buggy client. For example, if the client sends a lock |
| 5789 | * while some IO is outstanding, the lock may bump si_generation |
| 5790 | * while the IO is still in flight. The client could avoid that |
| 5791 | * situation by waiting for responses on all the IO requests, |
| 5792 | * but better performance may result in retrying IO that |
| 5793 | * receives an old_stateid error if requests are rarely |
| 5794 | * reordered in flight: |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 5795 | */ |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 5796 | return nfserr_old_stateid; |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 5797 | } |
| 5798 | |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 5799 | static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session) |
| 5800 | { |
| 5801 | __be32 ret; |
| 5802 | |
| 5803 | spin_lock(&s->sc_lock); |
| 5804 | ret = nfsd4_verify_open_stid(s); |
| 5805 | if (ret == nfs_ok) |
| 5806 | ret = check_stateid_generation(in, &s->sc_stateid, has_session); |
| 5807 | spin_unlock(&s->sc_lock); |
| 5808 | return ret; |
| 5809 | } |
| 5810 | |
Christoph Hellwig | ebe9cb3 | 2015-04-28 15:41:15 +0200 | [diff] [blame] | 5811 | static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols) |
| 5812 | { |
| 5813 | if (ols->st_stateowner->so_is_open_owner && |
| 5814 | !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED)) |
| 5815 | return nfserr_bad_stateid; |
| 5816 | return nfs_ok; |
| 5817 | } |
| 5818 | |
Chuck Lever | 7df302f | 2012-05-29 13:56:37 -0400 | [diff] [blame] | 5819 | static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid) |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 5820 | { |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 5821 | struct nfs4_stid *s; |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5822 | __be32 status = nfserr_bad_stateid; |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 5823 | |
Andrew Elble | ae254dac | 2017-11-09 13:41:10 -0500 | [diff] [blame] | 5824 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) || |
| 5825 | CLOSE_STATEID(stateid)) |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5826 | return status; |
J. Bruce Fields | 663e36f | 2020-03-19 10:18:49 -0400 | [diff] [blame] | 5827 | if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5828 | return status; |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5829 | spin_lock(&cl->cl_lock); |
| 5830 | s = find_stateid_locked(cl, stateid); |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 5831 | if (!s) |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5832 | goto out_unlock; |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 5833 | status = nfsd4_stid_check_stateid_generation(stateid, s, 1); |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 5834 | if (status) |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5835 | goto out_unlock; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 5836 | switch (s->sc_type) { |
| 5837 | case NFS4_DELEG_STID: |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5838 | status = nfs_ok; |
| 5839 | break; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 5840 | case NFS4_REVOKED_DELEG_STID: |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5841 | status = nfserr_deleg_revoked; |
| 5842 | break; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 5843 | case NFS4_OPEN_STID: |
| 5844 | case NFS4_LOCK_STID: |
Christoph Hellwig | ebe9cb3 | 2015-04-28 15:41:15 +0200 | [diff] [blame] | 5845 | status = nfsd4_check_openowner_confirmed(openlockstateid(s)); |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5846 | break; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 5847 | default: |
| 5848 | printk("unknown stateid type %x\n", s->sc_type); |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 5849 | fallthrough; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 5850 | case NFS4_CLOSED_STID: |
Trond Myklebust | b0fc29d | 2014-07-16 10:31:59 -0400 | [diff] [blame] | 5851 | case NFS4_CLOSED_DELEG_STID: |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5852 | status = nfserr_bad_stateid; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 5853 | } |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5854 | out_unlock: |
| 5855 | spin_unlock(&cl->cl_lock); |
| 5856 | return status; |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 5857 | } |
| 5858 | |
Christoph Hellwig | cd61c52 | 2014-08-14 08:44:57 +0200 | [diff] [blame] | 5859 | __be32 |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 5860 | nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate, |
| 5861 | stateid_t *stateid, unsigned char typemask, |
| 5862 | struct nfs4_stid **s, struct nfsd_net *nn) |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5863 | { |
J. Bruce Fields | 0eb6f20 | 2013-03-12 17:36:17 -0400 | [diff] [blame] | 5864 | __be32 status; |
Andrew Elble | 95da1b3a | 2017-11-03 14:06:31 -0400 | [diff] [blame] | 5865 | bool return_revoked = false; |
| 5866 | |
| 5867 | /* |
| 5868 | * only return revoked delegations if explicitly asked. |
| 5869 | * otherwise we report revoked or bad_stateid status. |
| 5870 | */ |
| 5871 | if (typemask & NFS4_REVOKED_DELEG_STID) |
| 5872 | return_revoked = true; |
| 5873 | else if (typemask & NFS4_DELEG_STID) |
| 5874 | typemask |= NFS4_REVOKED_DELEG_STID; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5875 | |
Andrew Elble | ae254dac | 2017-11-09 13:41:10 -0500 | [diff] [blame] | 5876 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) || |
| 5877 | CLOSE_STATEID(stateid)) |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5878 | return nfserr_bad_stateid; |
J. Bruce Fields | f71475b | 2021-01-21 17:57:43 -0500 | [diff] [blame] | 5879 | status = set_client(&stateid->si_opaque.so_clid, cstate, nn); |
Trond Myklebust | a8a7c67 | 2014-03-29 14:43:38 -0400 | [diff] [blame] | 5880 | if (status == nfserr_stale_clientid) { |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 5881 | if (cstate->session) |
Trond Myklebust | a8a7c67 | 2014-03-29 14:43:38 -0400 | [diff] [blame] | 5882 | return nfserr_bad_stateid; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5883 | return nfserr_stale_stateid; |
Trond Myklebust | a8a7c67 | 2014-03-29 14:43:38 -0400 | [diff] [blame] | 5884 | } |
J. Bruce Fields | 0eb6f20 | 2013-03-12 17:36:17 -0400 | [diff] [blame] | 5885 | if (status) |
| 5886 | return status; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 5887 | *s = find_stateid_by_type(cstate->clp, stateid, typemask); |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5888 | if (!*s) |
| 5889 | return nfserr_bad_stateid; |
Andrew Elble | 95da1b3a | 2017-11-03 14:06:31 -0400 | [diff] [blame] | 5890 | if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) { |
| 5891 | nfs4_put_stid(*s); |
| 5892 | if (cstate->minorversion) |
| 5893 | return nfserr_deleg_revoked; |
| 5894 | return nfserr_bad_stateid; |
| 5895 | } |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5896 | return nfs_ok; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5897 | } |
| 5898 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5899 | static struct nfsd_file * |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5900 | nfs4_find_file(struct nfs4_stid *s, int flags) |
| 5901 | { |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5902 | if (!s) |
| 5903 | return NULL; |
| 5904 | |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5905 | switch (s->sc_type) { |
| 5906 | case NFS4_DELEG_STID: |
| 5907 | if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file)) |
| 5908 | return NULL; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5909 | return nfsd_file_get(s->sc_file->fi_deleg_file); |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5910 | case NFS4_OPEN_STID: |
| 5911 | case NFS4_LOCK_STID: |
| 5912 | if (flags & RD_STATE) |
| 5913 | return find_readable_file(s->sc_file); |
| 5914 | else |
| 5915 | return find_writeable_file(s->sc_file); |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5916 | } |
| 5917 | |
| 5918 | return NULL; |
| 5919 | } |
| 5920 | |
| 5921 | static __be32 |
J. Bruce Fields | d8836f7 | 2018-11-05 20:04:06 -0500 | [diff] [blame] | 5922 | nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags) |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5923 | { |
| 5924 | __be32 status; |
| 5925 | |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5926 | status = nfsd4_check_openowner_confirmed(ols); |
| 5927 | if (status) |
| 5928 | return status; |
| 5929 | return nfs4_check_openmode(ols, flags); |
| 5930 | } |
| 5931 | |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5932 | static __be32 |
| 5933 | nfs4_check_file(struct svc_rqst *rqstp, struct svc_fh *fhp, struct nfs4_stid *s, |
Jeff Layton | 5c4583b | 2019-08-18 14:18:54 -0400 | [diff] [blame] | 5934 | struct nfsd_file **nfp, int flags) |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5935 | { |
| 5936 | int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5937 | struct nfsd_file *nf; |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5938 | __be32 status; |
| 5939 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5940 | nf = nfs4_find_file(s, flags); |
| 5941 | if (nf) { |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5942 | status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry, |
| 5943 | acc | NFSD_MAY_OWNER_OVERRIDE); |
Jeff Layton | 5c4583b | 2019-08-18 14:18:54 -0400 | [diff] [blame] | 5944 | if (status) { |
| 5945 | nfsd_file_put(nf); |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5946 | goto out; |
Jeff Layton | 5c4583b | 2019-08-18 14:18:54 -0400 | [diff] [blame] | 5947 | } |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5948 | } else { |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5949 | status = nfsd_file_acquire(rqstp, fhp, acc, &nf); |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5950 | if (status) |
| 5951 | return status; |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5952 | } |
Jeff Layton | 5c4583b | 2019-08-18 14:18:54 -0400 | [diff] [blame] | 5953 | *nfp = nf; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5954 | out: |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5955 | return status; |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5956 | } |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 5957 | static void |
| 5958 | _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps) |
| 5959 | { |
| 5960 | WARN_ON_ONCE(cps->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID); |
| 5961 | if (!refcount_dec_and_test(&cps->cp_stateid.sc_count)) |
| 5962 | return; |
| 5963 | list_del(&cps->cp_list); |
| 5964 | idr_remove(&nn->s2s_cp_stateids, |
| 5965 | cps->cp_stateid.stid.si_opaque.so_id); |
| 5966 | kfree(cps); |
| 5967 | } |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 5968 | /* |
| 5969 | * A READ from an inter server to server COPY will have a |
| 5970 | * copy stateid. Look up the copy notify stateid from the |
| 5971 | * idr structure and take a reference on it. |
| 5972 | */ |
Olga Kornievskaia | ce0887a | 2019-10-09 11:50:48 -0400 | [diff] [blame] | 5973 | __be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st, |
| 5974 | struct nfs4_client *clp, |
| 5975 | struct nfs4_cpntf_state **cps) |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 5976 | { |
| 5977 | copy_stateid_t *cps_t; |
| 5978 | struct nfs4_cpntf_state *state = NULL; |
| 5979 | |
| 5980 | if (st->si_opaque.so_clid.cl_id != nn->s2s_cp_cl_id) |
| 5981 | return nfserr_bad_stateid; |
| 5982 | spin_lock(&nn->s2s_cp_lock); |
| 5983 | cps_t = idr_find(&nn->s2s_cp_stateids, st->si_opaque.so_id); |
| 5984 | if (cps_t) { |
| 5985 | state = container_of(cps_t, struct nfs4_cpntf_state, |
| 5986 | cp_stateid); |
Dan Carpenter | 5277a79 | 2019-12-04 10:59:36 +0300 | [diff] [blame] | 5987 | if (state->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID) { |
| 5988 | state = NULL; |
| 5989 | goto unlock; |
| 5990 | } |
Olga Kornievskaia | ce0887a | 2019-10-09 11:50:48 -0400 | [diff] [blame] | 5991 | if (!clp) |
| 5992 | refcount_inc(&state->cp_stateid.sc_count); |
| 5993 | else |
| 5994 | _free_cpntf_state_locked(nn, state); |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 5995 | } |
Dan Carpenter | 5277a79 | 2019-12-04 10:59:36 +0300 | [diff] [blame] | 5996 | unlock: |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 5997 | spin_unlock(&nn->s2s_cp_lock); |
| 5998 | if (!state) |
| 5999 | return nfserr_bad_stateid; |
Olga Kornievskaia | ce0887a | 2019-10-09 11:50:48 -0400 | [diff] [blame] | 6000 | if (!clp && state) |
| 6001 | *cps = state; |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 6002 | return 0; |
| 6003 | } |
| 6004 | |
| 6005 | static __be32 find_cpntf_state(struct nfsd_net *nn, stateid_t *st, |
| 6006 | struct nfs4_stid **stid) |
| 6007 | { |
| 6008 | __be32 status; |
| 6009 | struct nfs4_cpntf_state *cps = NULL; |
J. Bruce Fields | 47fdb22 | 2021-01-21 17:57:42 -0500 | [diff] [blame] | 6010 | struct nfs4_client *found; |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 6011 | |
Olga Kornievskaia | ce0887a | 2019-10-09 11:50:48 -0400 | [diff] [blame] | 6012 | status = manage_cpntf_state(nn, st, NULL, &cps); |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 6013 | if (status) |
| 6014 | return status; |
| 6015 | |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 6016 | cps->cpntf_time = ktime_get_boottime_seconds(); |
J. Bruce Fields | 47fdb22 | 2021-01-21 17:57:42 -0500 | [diff] [blame] | 6017 | |
| 6018 | status = nfserr_expired; |
| 6019 | found = lookup_clientid(&cps->cp_p_clid, true, nn); |
| 6020 | if (!found) |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 6021 | goto out; |
J. Bruce Fields | 47fdb22 | 2021-01-21 17:57:42 -0500 | [diff] [blame] | 6022 | |
| 6023 | *stid = find_stateid_by_type(found, &cps->cp_p_stateid, |
| 6024 | NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID); |
| 6025 | if (*stid) |
| 6026 | status = nfs_ok; |
| 6027 | else |
| 6028 | status = nfserr_bad_stateid; |
| 6029 | |
| 6030 | put_client_renew(found); |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 6031 | out: |
| 6032 | nfs4_put_cpntf_state(nn, cps); |
| 6033 | return status; |
| 6034 | } |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 6035 | |
| 6036 | void nfs4_put_cpntf_state(struct nfsd_net *nn, struct nfs4_cpntf_state *cps) |
| 6037 | { |
| 6038 | spin_lock(&nn->s2s_cp_lock); |
| 6039 | _free_cpntf_state_locked(nn, cps); |
| 6040 | spin_unlock(&nn->s2s_cp_lock); |
| 6041 | } |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 6042 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6043 | /* |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 6044 | * Checks for stateid operations |
| 6045 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6046 | __be32 |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 6047 | nfs4_preprocess_stateid_op(struct svc_rqst *rqstp, |
Anna Schumaker | aa0d6ae | 2015-12-03 12:59:51 +0100 | [diff] [blame] | 6048 | struct nfsd4_compound_state *cstate, struct svc_fh *fhp, |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 6049 | stateid_t *stateid, int flags, struct nfsd_file **nfp, |
| 6050 | struct nfs4_stid **cstid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6051 | { |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 6052 | struct net *net = SVC_NET(rqstp); |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6053 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 6054 | struct nfs4_stid *s = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6055 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6056 | |
Jeff Layton | 5c4583b | 2019-08-18 14:18:54 -0400 | [diff] [blame] | 6057 | if (nfp) |
| 6058 | *nfp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6059 | |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 6060 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) { |
J. Bruce Fields | 074b07d | 2022-01-05 14:15:03 -0500 | [diff] [blame] | 6061 | if (cstid) |
| 6062 | status = nfserr_bad_stateid; |
| 6063 | else |
| 6064 | status = check_special_stateids(net, fhp, stateid, |
| 6065 | flags); |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 6066 | goto done; |
| 6067 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6068 | |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 6069 | status = nfsd4_lookup_stateid(cstate, stateid, |
Jeff Layton | db24b3b | 2014-06-30 11:48:36 -0400 | [diff] [blame] | 6070 | NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID, |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 6071 | &s, nn); |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 6072 | if (status == nfserr_bad_stateid) |
| 6073 | status = find_cpntf_state(nn, stateid, &s); |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 6074 | if (status) |
Trond Myklebust | c2d1d6a | 2014-07-30 08:27:25 -0400 | [diff] [blame] | 6075 | return status; |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 6076 | status = nfsd4_stid_check_stateid_generation(stateid, s, |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 6077 | nfsd4_has_session(cstate)); |
J. Bruce Fields | 69064a2 | 2011-09-09 11:54:57 -0400 | [diff] [blame] | 6078 | if (status) |
| 6079 | goto out; |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 6080 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 6081 | switch (s->sc_type) { |
| 6082 | case NFS4_DELEG_STID: |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 6083 | status = nfs4_check_delegmode(delegstateid(s), flags); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 6084 | break; |
| 6085 | case NFS4_OPEN_STID: |
| 6086 | case NFS4_LOCK_STID: |
J. Bruce Fields | d8836f7 | 2018-11-05 20:04:06 -0500 | [diff] [blame] | 6087 | status = nfs4_check_olstateid(openlockstateid(s), flags); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 6088 | break; |
| 6089 | default: |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 6090 | status = nfserr_bad_stateid; |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 6091 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6092 | } |
Jeff Layton | 8fcd461 | 2015-07-30 06:57:46 -0400 | [diff] [blame] | 6093 | if (status) |
| 6094 | goto out; |
| 6095 | status = nfs4_check_fh(fhp, s); |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 6096 | |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 6097 | done: |
Jeff Layton | 5c4583b | 2019-08-18 14:18:54 -0400 | [diff] [blame] | 6098 | if (status == nfs_ok && nfp) |
| 6099 | status = nfs4_check_file(rqstp, fhp, s, nfp, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6100 | out: |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 6101 | if (s) { |
| 6102 | if (!status && cstid) |
| 6103 | *cstid = s; |
| 6104 | else |
| 6105 | nfs4_put_stid(s); |
| 6106 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6107 | return status; |
| 6108 | } |
| 6109 | |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 6110 | /* |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 6111 | * Test if the stateid is valid |
| 6112 | */ |
| 6113 | __be32 |
| 6114 | nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6115 | union nfsd4_op_u *u) |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 6116 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6117 | struct nfsd4_test_stateid *test_stateid = &u->test_stateid; |
Bryan Schumaker | 03cfb42 | 2012-01-27 10:22:49 -0500 | [diff] [blame] | 6118 | struct nfsd4_test_stateid_id *stateid; |
J. Bruce Fields | ec59659 | 2021-01-21 17:57:45 -0500 | [diff] [blame] | 6119 | struct nfs4_client *cl = cstate->clp; |
Bryan Schumaker | 03cfb42 | 2012-01-27 10:22:49 -0500 | [diff] [blame] | 6120 | |
Bryan Schumaker | 03cfb42 | 2012-01-27 10:22:49 -0500 | [diff] [blame] | 6121 | list_for_each_entry(stateid, &test_stateid->ts_stateid_list, ts_id_list) |
Chuck Lever | 7df302f | 2012-05-29 13:56:37 -0400 | [diff] [blame] | 6122 | stateid->ts_id_status = |
| 6123 | nfsd4_validate_stateid(cl, &stateid->ts_id_stateid); |
Bryan Schumaker | 03cfb42 | 2012-01-27 10:22:49 -0500 | [diff] [blame] | 6124 | |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 6125 | return nfs_ok; |
| 6126 | } |
| 6127 | |
Chuck Lever | 4269139 | 2016-08-11 10:37:30 -0400 | [diff] [blame] | 6128 | static __be32 |
| 6129 | nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s) |
| 6130 | { |
| 6131 | struct nfs4_ol_stateid *stp = openlockstateid(s); |
| 6132 | __be32 ret; |
| 6133 | |
Trond Myklebust | 659aefb | 2017-11-03 08:00:13 -0400 | [diff] [blame] | 6134 | ret = nfsd4_lock_ol_stateid(stp); |
| 6135 | if (ret) |
| 6136 | goto out_put_stid; |
Chuck Lever | 4269139 | 2016-08-11 10:37:30 -0400 | [diff] [blame] | 6137 | |
| 6138 | ret = check_stateid_generation(stateid, &s->sc_stateid, 1); |
| 6139 | if (ret) |
| 6140 | goto out; |
| 6141 | |
| 6142 | ret = nfserr_locks_held; |
| 6143 | if (check_for_locks(stp->st_stid.sc_file, |
| 6144 | lockowner(stp->st_stateowner))) |
| 6145 | goto out; |
| 6146 | |
| 6147 | release_lock_stateid(stp); |
| 6148 | ret = nfs_ok; |
| 6149 | |
| 6150 | out: |
| 6151 | mutex_unlock(&stp->st_mutex); |
Trond Myklebust | 659aefb | 2017-11-03 08:00:13 -0400 | [diff] [blame] | 6152 | out_put_stid: |
Chuck Lever | 4269139 | 2016-08-11 10:37:30 -0400 | [diff] [blame] | 6153 | nfs4_put_stid(s); |
| 6154 | return ret; |
| 6155 | } |
| 6156 | |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 6157 | __be32 |
| 6158 | nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6159 | union nfsd4_op_u *u) |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 6160 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6161 | struct nfsd4_free_stateid *free_stateid = &u->free_stateid; |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 6162 | stateid_t *stateid = &free_stateid->fr_stateid; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 6163 | struct nfs4_stid *s; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 6164 | struct nfs4_delegation *dp; |
J. Bruce Fields | ec59659 | 2021-01-21 17:57:45 -0500 | [diff] [blame] | 6165 | struct nfs4_client *cl = cstate->clp; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 6166 | __be32 ret = nfserr_bad_stateid; |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 6167 | |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 6168 | spin_lock(&cl->cl_lock); |
| 6169 | s = find_stateid_locked(cl, stateid); |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 6170 | if (!s) |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 6171 | goto out_unlock; |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 6172 | spin_lock(&s->sc_lock); |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 6173 | switch (s->sc_type) { |
| 6174 | case NFS4_DELEG_STID: |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 6175 | ret = nfserr_locks_held; |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 6176 | break; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 6177 | case NFS4_OPEN_STID: |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 6178 | ret = check_stateid_generation(stateid, &s->sc_stateid, 1); |
| 6179 | if (ret) |
| 6180 | break; |
| 6181 | ret = nfserr_locks_held; |
| 6182 | break; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 6183 | case NFS4_LOCK_STID: |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 6184 | spin_unlock(&s->sc_lock); |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 6185 | refcount_inc(&s->sc_count); |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 6186 | spin_unlock(&cl->cl_lock); |
Chuck Lever | 4269139 | 2016-08-11 10:37:30 -0400 | [diff] [blame] | 6187 | ret = nfsd4_free_lock_stateid(stateid, s); |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 6188 | goto out; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 6189 | case NFS4_REVOKED_DELEG_STID: |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 6190 | spin_unlock(&s->sc_lock); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 6191 | dp = delegstateid(s); |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 6192 | list_del_init(&dp->dl_recall_lru); |
| 6193 | spin_unlock(&cl->cl_lock); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 6194 | nfs4_put_stid(s); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 6195 | ret = nfs_ok; |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 6196 | goto out; |
| 6197 | /* Default falls through and returns nfserr_bad_stateid */ |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 6198 | } |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 6199 | spin_unlock(&s->sc_lock); |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 6200 | out_unlock: |
| 6201 | spin_unlock(&cl->cl_lock); |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 6202 | out: |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 6203 | return ret; |
| 6204 | } |
| 6205 | |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 6206 | static inline int |
| 6207 | setlkflg (int type) |
| 6208 | { |
| 6209 | return (type == NFS4_READW_LT || type == NFS4_READ_LT) ? |
| 6210 | RD_STATE : WR_STATE; |
| 6211 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6212 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 6213 | static __be32 nfs4_seqid_op_checks(struct nfsd4_compound_state *cstate, stateid_t *stateid, u32 seqid, struct nfs4_ol_stateid *stp) |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 6214 | { |
| 6215 | struct svc_fh *current_fh = &cstate->current_fh; |
| 6216 | struct nfs4_stateowner *sop = stp->st_stateowner; |
| 6217 | __be32 status; |
| 6218 | |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 6219 | status = nfsd4_check_seqid(cstate, sop, seqid); |
| 6220 | if (status) |
| 6221 | return status; |
Trond Myklebust | 9271d7e | 2017-11-03 08:00:15 -0400 | [diff] [blame] | 6222 | status = nfsd4_lock_ol_stateid(stp); |
| 6223 | if (status != nfs_ok) |
| 6224 | return status; |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 6225 | status = check_stateid_generation(stateid, &stp->st_stid.sc_stateid, nfsd4_has_session(cstate)); |
Jeff Layton | 35a92fe | 2015-09-17 07:47:08 -0400 | [diff] [blame] | 6226 | if (status == nfs_ok) |
| 6227 | status = nfs4_check_fh(current_fh, &stp->st_stid); |
| 6228 | if (status != nfs_ok) |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 6229 | mutex_unlock(&stp->st_mutex); |
Jeff Layton | 35a92fe | 2015-09-17 07:47:08 -0400 | [diff] [blame] | 6230 | return status; |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 6231 | } |
| 6232 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6233 | /* |
| 6234 | * Checks for sequence id mutating operations. |
| 6235 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6236 | static __be32 |
Benny Halevy | dd453df | 2009-04-03 08:28:41 +0300 | [diff] [blame] | 6237 | nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, |
J. Bruce Fields | 2288d0e | 2011-09-06 15:50:21 -0400 | [diff] [blame] | 6238 | stateid_t *stateid, char typemask, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6239 | struct nfs4_ol_stateid **stpp, |
| 6240 | struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6241 | { |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 6242 | __be32 status; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 6243 | struct nfs4_stid *s; |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 6244 | struct nfs4_ol_stateid *stp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6245 | |
Chuck Lever | dd5e3fb | 2020-04-05 11:42:29 -0400 | [diff] [blame] | 6246 | trace_nfsd_preprocess(seqid, stateid); |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 6247 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6248 | *stpp = NULL; |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 6249 | status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn); |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 6250 | if (status) |
| 6251 | return status; |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 6252 | stp = openlockstateid(s); |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 6253 | nfsd4_cstate_assign_replay(cstate, stp->st_stateowner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6254 | |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 6255 | status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp); |
Trond Myklebust | fd91101 | 2014-07-29 21:34:24 -0400 | [diff] [blame] | 6256 | if (!status) |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 6257 | *stpp = stp; |
Trond Myklebust | fd91101 | 2014-07-29 21:34:24 -0400 | [diff] [blame] | 6258 | else |
| 6259 | nfs4_put_stid(&stp->st_stid); |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 6260 | return status; |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 6261 | } |
J. Bruce Fields | 39325bd | 2007-11-26 17:06:39 -0500 | [diff] [blame] | 6262 | |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6263 | static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, |
| 6264 | stateid_t *stateid, struct nfs4_ol_stateid **stpp, struct nfsd_net *nn) |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 6265 | { |
| 6266 | __be32 status; |
| 6267 | struct nfs4_openowner *oo; |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 6268 | struct nfs4_ol_stateid *stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6269 | |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 6270 | status = nfs4_preprocess_seqid_op(cstate, seqid, stateid, |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 6271 | NFS4_OPEN_STID, &stp, nn); |
J. Bruce Fields | 7a8711c | 2011-09-02 09:03:37 -0400 | [diff] [blame] | 6272 | if (status) |
| 6273 | return status; |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 6274 | oo = openowner(stp->st_stateowner); |
| 6275 | if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) { |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 6276 | mutex_unlock(&stp->st_mutex); |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 6277 | nfs4_put_stid(&stp->st_stid); |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 6278 | return nfserr_bad_stateid; |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 6279 | } |
| 6280 | *stpp = stp; |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 6281 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6282 | } |
| 6283 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6284 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6285 | nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6286 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6287 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6288 | struct nfsd4_open_confirm *oc = &u->open_confirm; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6289 | __be32 status; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6290 | struct nfs4_openowner *oo; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 6291 | struct nfs4_ol_stateid *stp; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6292 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6293 | |
Al Viro | a6a9f18 | 2013-09-16 10:57:01 -0400 | [diff] [blame] | 6294 | dprintk("NFSD: nfsd4_open_confirm on file %pd\n", |
| 6295 | cstate->current_fh.fh_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6296 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6297 | status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0); |
J. Bruce Fields | a8cddc5 | 2006-06-30 01:56:13 -0700 | [diff] [blame] | 6298 | if (status) |
| 6299 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6300 | |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 6301 | status = nfs4_preprocess_seqid_op(cstate, |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6302 | oc->oc_seqid, &oc->oc_req_stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6303 | NFS4_OPEN_STID, &stp, nn); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 6304 | if (status) |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 6305 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6306 | oo = openowner(stp->st_stateowner); |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 6307 | status = nfserr_bad_stateid; |
Jeff Layton | 35a92fe | 2015-09-17 07:47:08 -0400 | [diff] [blame] | 6308 | if (oo->oo_flags & NFS4_OO_CONFIRMED) { |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 6309 | mutex_unlock(&stp->st_mutex); |
Trond Myklebust | 2585fc7 | 2014-07-29 21:34:21 -0400 | [diff] [blame] | 6310 | goto put_stateid; |
Jeff Layton | 35a92fe | 2015-09-17 07:47:08 -0400 | [diff] [blame] | 6311 | } |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 6312 | oo->oo_flags |= NFS4_OO_CONFIRMED; |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 6313 | nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid); |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 6314 | mutex_unlock(&stp->st_mutex); |
Chuck Lever | dd5e3fb | 2020-04-05 11:42:29 -0400 | [diff] [blame] | 6315 | trace_nfsd_open_confirm(oc->oc_seqid, &stp->st_stid.sc_stateid); |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 6316 | nfsd4_client_record_create(oo->oo_owner.so_client); |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 6317 | status = nfs_ok; |
Trond Myklebust | 2585fc7 | 2014-07-29 21:34:21 -0400 | [diff] [blame] | 6318 | put_stateid: |
| 6319 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6320 | out: |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 6321 | nfsd4_bump_seqid(cstate, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6322 | return status; |
| 6323 | } |
| 6324 | |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 6325 | static inline void nfs4_stateid_downgrade_bit(struct nfs4_ol_stateid *stp, u32 access) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6326 | { |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 6327 | if (!test_access(access, stp)) |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 6328 | return; |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 6329 | nfs4_file_put_access(stp->st_stid.sc_file, access); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 6330 | clear_access(access, stp); |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 6331 | } |
J. Bruce Fields | f197c27 | 2011-06-29 08:23:50 -0400 | [diff] [blame] | 6332 | |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 6333 | static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access) |
| 6334 | { |
| 6335 | switch (to_access) { |
| 6336 | case NFS4_SHARE_ACCESS_READ: |
| 6337 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE); |
| 6338 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); |
| 6339 | break; |
| 6340 | case NFS4_SHARE_ACCESS_WRITE: |
| 6341 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ); |
| 6342 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); |
| 6343 | break; |
| 6344 | case NFS4_SHARE_ACCESS_BOTH: |
| 6345 | break; |
| 6346 | default: |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 6347 | WARN_ON_ONCE(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6348 | } |
| 6349 | } |
| 6350 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6351 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6352 | nfsd4_open_downgrade(struct svc_rqst *rqstp, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6353 | struct nfsd4_compound_state *cstate, union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6354 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6355 | struct nfsd4_open_downgrade *od = &u->open_downgrade; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6356 | __be32 status; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 6357 | struct nfs4_ol_stateid *stp; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6358 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6359 | |
Al Viro | a6a9f18 | 2013-09-16 10:57:01 -0400 | [diff] [blame] | 6360 | dprintk("NFSD: nfsd4_open_downgrade on file %pd\n", |
| 6361 | cstate->current_fh.fh_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6362 | |
J. Bruce Fields | c30e92d | 2011-10-10 17:34:31 -0400 | [diff] [blame] | 6363 | /* We don't yet support WANT bits: */ |
Benny Halevy | 2c8bd7e | 2012-02-16 20:57:09 +0200 | [diff] [blame] | 6364 | if (od->od_deleg_want) |
| 6365 | dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__, |
| 6366 | od->od_deleg_want); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6367 | |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 6368 | status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6369 | &od->od_stateid, &stp, nn); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 6370 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6371 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6372 | status = nfserr_inval; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 6373 | if (!test_access(od->od_share_access, stp)) { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 6374 | dprintk("NFSD: access not a subset of current bitmap: 0x%hhx, input access=%08x\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6375 | stp->st_access_bmap, od->od_share_access); |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 6376 | goto put_stateid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6377 | } |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 6378 | if (!test_deny(od->od_share_deny, stp)) { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 6379 | dprintk("NFSD: deny not a subset of current bitmap: 0x%hhx, input deny=%08x\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6380 | stp->st_deny_bmap, od->od_share_deny); |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 6381 | goto put_stateid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6382 | } |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 6383 | nfs4_stateid_downgrade(stp, od->od_share_access); |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 6384 | reset_union_bmap_deny(od->od_share_deny, stp); |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 6385 | nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6386 | status = nfs_ok; |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 6387 | put_stateid: |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 6388 | mutex_unlock(&stp->st_mutex); |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 6389 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6390 | out: |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 6391 | nfsd4_bump_seqid(cstate, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6392 | return status; |
| 6393 | } |
| 6394 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 6395 | static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s) |
| 6396 | { |
Trond Myklebust | acf9295 | 2014-06-30 11:48:37 -0400 | [diff] [blame] | 6397 | struct nfs4_client *clp = s->st_stid.sc_client; |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 6398 | bool unhashed; |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 6399 | LIST_HEAD(reaplist); |
Trond Myklebust | acf9295 | 2014-06-30 11:48:37 -0400 | [diff] [blame] | 6400 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 6401 | spin_lock(&clp->cl_lock); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 6402 | unhashed = unhash_open_stateid(s, &reaplist); |
Trond Myklebust | acf9295 | 2014-06-30 11:48:37 -0400 | [diff] [blame] | 6403 | |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 6404 | if (clp->cl_minorversion) { |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 6405 | if (unhashed) |
| 6406 | put_ol_stateid_locked(s, &reaplist); |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 6407 | spin_unlock(&clp->cl_lock); |
| 6408 | free_ol_stateid_reaplist(&reaplist); |
| 6409 | } else { |
| 6410 | spin_unlock(&clp->cl_lock); |
| 6411 | free_ol_stateid_reaplist(&reaplist); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 6412 | if (unhashed) |
| 6413 | move_to_close_lru(s, clp->net); |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 6414 | } |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 6415 | } |
| 6416 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6417 | /* |
| 6418 | * nfs4_unlock_state() called after encode |
| 6419 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6420 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6421 | nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6422 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6423 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6424 | struct nfsd4_close *close = &u->close; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6425 | __be32 status; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 6426 | struct nfs4_ol_stateid *stp; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6427 | struct net *net = SVC_NET(rqstp); |
| 6428 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6429 | |
Al Viro | a6a9f18 | 2013-09-16 10:57:01 -0400 | [diff] [blame] | 6430 | dprintk("NFSD: nfsd4_close on file %pd\n", |
| 6431 | cstate->current_fh.fh_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6432 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 6433 | status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid, |
| 6434 | &close->cl_stateid, |
| 6435 | NFS4_OPEN_STID|NFS4_CLOSED_STID, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6436 | &stp, nn); |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 6437 | nfsd4_bump_seqid(cstate, status); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 6438 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6439 | goto out; |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 6440 | |
| 6441 | stp->st_stid.sc_type = NFS4_CLOSED_STID; |
Jeff Layton | bd2deca | 2018-02-15 15:16:18 -0500 | [diff] [blame] | 6442 | |
| 6443 | /* |
| 6444 | * Technically we don't _really_ have to increment or copy it, since |
| 6445 | * it should just be gone after this operation and we clobber the |
| 6446 | * copied value below, but we continue to do so here just to ensure |
| 6447 | * that racing ops see that there was a state change. |
| 6448 | */ |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 6449 | nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6450 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 6451 | nfsd4_close_open_stateid(stp); |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 6452 | mutex_unlock(&stp->st_mutex); |
Trond Myklebust | 8a0b589 | 2014-07-29 21:34:20 -0400 | [diff] [blame] | 6453 | |
Jeff Layton | bd2deca | 2018-02-15 15:16:18 -0500 | [diff] [blame] | 6454 | /* v4.1+ suggests that we send a special stateid in here, since the |
| 6455 | * clients should just ignore this anyway. Since this is not useful |
| 6456 | * for v4.0 clients either, we set it to the special close_stateid |
| 6457 | * universally. |
| 6458 | * |
| 6459 | * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5 |
| 6460 | */ |
| 6461 | memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid)); |
Trond Myklebust | fb500a7 | 2017-11-03 08:00:12 -0400 | [diff] [blame] | 6462 | |
Trond Myklebust | 8a0b589 | 2014-07-29 21:34:20 -0400 | [diff] [blame] | 6463 | /* put reference from nfs4_preprocess_seqid_op */ |
| 6464 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6465 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6466 | return status; |
| 6467 | } |
| 6468 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6469 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6470 | nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6471 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6472 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6473 | struct nfsd4_delegreturn *dr = &u->delegreturn; |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 6474 | struct nfs4_delegation *dp; |
| 6475 | stateid_t *stateid = &dr->dr_stateid; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 6476 | struct nfs4_stid *s; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6477 | __be32 status; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6478 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6479 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6480 | if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 6481 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6482 | |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 6483 | status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn); |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 6484 | if (status) |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 6485 | goto out; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 6486 | dp = delegstateid(s); |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 6487 | status = nfsd4_stid_check_stateid_generation(stateid, &dp->dl_stid, nfsd4_has_session(cstate)); |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 6488 | if (status) |
Trond Myklebust | fd91101 | 2014-07-29 21:34:24 -0400 | [diff] [blame] | 6489 | goto put_stateid; |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 6490 | |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 6491 | destroy_delegation(dp); |
Trond Myklebust | fd91101 | 2014-07-29 21:34:24 -0400 | [diff] [blame] | 6492 | put_stateid: |
| 6493 | nfs4_put_stid(&dp->dl_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6494 | out: |
| 6495 | return status; |
| 6496 | } |
| 6497 | |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 6498 | /* last octet in a range */ |
| 6499 | static inline u64 |
| 6500 | last_byte_offset(u64 start, u64 len) |
| 6501 | { |
| 6502 | u64 end; |
| 6503 | |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 6504 | WARN_ON_ONCE(!len); |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 6505 | end = start + len; |
| 6506 | return end > start ? end - 1: NFS4_MAX_UINT64; |
| 6507 | } |
| 6508 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6509 | /* |
| 6510 | * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that |
| 6511 | * we can't properly handle lock requests that go beyond the (2^63 - 1)-th |
| 6512 | * byte, because of sign extension problems. Since NFSv4 calls for 64-bit |
| 6513 | * locking, this prevents us from being completely protocol-compliant. The |
| 6514 | * real solution to this problem is to start using unsigned file offsets in |
| 6515 | * the VFS, but this is a very deep change! |
| 6516 | */ |
| 6517 | static inline void |
| 6518 | nfs4_transform_lock_offset(struct file_lock *lock) |
| 6519 | { |
| 6520 | if (lock->fl_start < 0) |
| 6521 | lock->fl_start = OFFSET_MAX; |
| 6522 | if (lock->fl_end < 0) |
| 6523 | lock->fl_end = OFFSET_MAX; |
| 6524 | } |
| 6525 | |
Jeff Layton | cae80b3 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 6526 | static fl_owner_t |
| 6527 | nfsd4_fl_get_owner(fl_owner_t owner) |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6528 | { |
Jeff Layton | cae80b3 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 6529 | struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner; |
| 6530 | |
| 6531 | nfs4_get_stateowner(&lo->lo_owner); |
| 6532 | return owner; |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6533 | } |
| 6534 | |
Jeff Layton | cae80b3 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 6535 | static void |
| 6536 | nfsd4_fl_put_owner(fl_owner_t owner) |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6537 | { |
Jeff Layton | cae80b3 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 6538 | struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner; |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6539 | |
Jeff Layton | cae80b3 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 6540 | if (lo) |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6541 | nfs4_put_stateowner(&lo->lo_owner); |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6542 | } |
| 6543 | |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6544 | static void |
| 6545 | nfsd4_lm_notify(struct file_lock *fl) |
| 6546 | { |
| 6547 | struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner; |
| 6548 | struct net *net = lo->lo_owner.so_client->net; |
| 6549 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 6550 | struct nfsd4_blocked_lock *nbl = container_of(fl, |
| 6551 | struct nfsd4_blocked_lock, nbl_lock); |
| 6552 | bool queue = false; |
| 6553 | |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 6554 | /* An empty list means that something else is going to be using it */ |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 6555 | spin_lock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6556 | if (!list_empty(&nbl->nbl_list)) { |
| 6557 | list_del_init(&nbl->nbl_list); |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 6558 | list_del_init(&nbl->nbl_lru); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6559 | queue = true; |
| 6560 | } |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 6561 | spin_unlock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6562 | |
Chuck Lever | 2cde7f8 | 2021-05-14 15:57:08 -0400 | [diff] [blame] | 6563 | if (queue) { |
| 6564 | trace_nfsd_cb_notify_lock(lo, nbl); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6565 | nfsd4_run_cb(&nbl->nbl_cb); |
Chuck Lever | 2cde7f8 | 2021-05-14 15:57:08 -0400 | [diff] [blame] | 6566 | } |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6567 | } |
| 6568 | |
Alexey Dobriyan | 7b02196 | 2009-09-21 17:01:12 -0700 | [diff] [blame] | 6569 | static const struct lock_manager_operations nfsd_posix_mng_ops = { |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6570 | .lm_notify = nfsd4_lm_notify, |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6571 | .lm_get_owner = nfsd4_fl_get_owner, |
| 6572 | .lm_put_owner = nfsd4_fl_put_owner, |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 6573 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6574 | |
| 6575 | static inline void |
| 6576 | nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny) |
| 6577 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6578 | struct nfs4_lockowner *lo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6579 | |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 6580 | if (fl->fl_lmops == &nfsd_posix_mng_ops) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6581 | lo = (struct nfs4_lockowner *) fl->fl_owner; |
J. Bruce Fields | 6f4859b | 2019-06-19 14:30:33 -0400 | [diff] [blame] | 6582 | xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner, |
| 6583 | GFP_KERNEL); |
J. Bruce Fields | 7c13f34 | 2011-08-30 22:15:47 -0400 | [diff] [blame] | 6584 | if (!deny->ld_owner.data) |
| 6585 | /* We just don't care that much */ |
| 6586 | goto nevermind; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6587 | deny->ld_clientid = lo->lo_owner.so_client->cl_clientid; |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 6588 | } else { |
J. Bruce Fields | 7c13f34 | 2011-08-30 22:15:47 -0400 | [diff] [blame] | 6589 | nevermind: |
| 6590 | deny->ld_owner.len = 0; |
| 6591 | deny->ld_owner.data = NULL; |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 6592 | deny->ld_clientid.cl_boot = 0; |
| 6593 | deny->ld_clientid.cl_id = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6594 | } |
| 6595 | deny->ld_start = fl->fl_start; |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 6596 | deny->ld_length = NFS4_MAX_UINT64; |
| 6597 | if (fl->fl_end != NFS4_MAX_UINT64) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6598 | deny->ld_length = fl->fl_end - fl->fl_start + 1; |
| 6599 | deny->ld_type = NFS4_READ_LT; |
| 6600 | if (fl->fl_type != F_RDLCK) |
| 6601 | deny->ld_type = NFS4_WRITE_LT; |
| 6602 | } |
| 6603 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6604 | static struct nfs4_lockowner * |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 6605 | find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6606 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6607 | unsigned int strhashval = ownerstr_hashval(owner); |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame] | 6608 | struct nfs4_stateowner *so; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6609 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 6610 | lockdep_assert_held(&clp->cl_lock); |
| 6611 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6612 | list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval], |
| 6613 | so_strhash) { |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame] | 6614 | if (so->so_is_open_owner) |
| 6615 | continue; |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 6616 | if (same_owner_str(so, owner)) |
| 6617 | return lockowner(nfs4_get_stateowner(so)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6618 | } |
| 6619 | return NULL; |
| 6620 | } |
| 6621 | |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6622 | static struct nfs4_lockowner * |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 6623 | find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner) |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6624 | { |
| 6625 | struct nfs4_lockowner *lo; |
| 6626 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6627 | spin_lock(&clp->cl_lock); |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 6628 | lo = find_lockowner_str_locked(clp, owner); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6629 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6630 | return lo; |
| 6631 | } |
| 6632 | |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 6633 | static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop) |
| 6634 | { |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6635 | unhash_lockowner_locked(lockowner(sop)); |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 6636 | } |
| 6637 | |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 6638 | static void nfs4_free_lockowner(struct nfs4_stateowner *sop) |
| 6639 | { |
| 6640 | struct nfs4_lockowner *lo = lockowner(sop); |
| 6641 | |
| 6642 | kmem_cache_free(lockowner_slab, lo); |
| 6643 | } |
| 6644 | |
| 6645 | static const struct nfs4_stateowner_operations lockowner_ops = { |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 6646 | .so_unhash = nfs4_unhash_lockowner, |
| 6647 | .so_free = nfs4_free_lockowner, |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 6648 | }; |
| 6649 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6650 | /* |
| 6651 | * Alloc a lock owner structure. |
| 6652 | * Called in nfsd4_lock - therefore, OPEN and OPEN_CONFIRM (if needed) has |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 6653 | * occurred. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6654 | * |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 6655 | * strhashval = ownerstr_hashval |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6656 | */ |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6657 | static struct nfs4_lockowner * |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6658 | alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, |
| 6659 | struct nfs4_ol_stateid *open_stp, |
| 6660 | struct nfsd4_lock *lock) |
| 6661 | { |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6662 | struct nfs4_lockowner *lo, *ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6663 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6664 | lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp); |
| 6665 | if (!lo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6666 | return NULL; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6667 | INIT_LIST_HEAD(&lo->lo_blocked); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6668 | INIT_LIST_HEAD(&lo->lo_owner.so_stateids); |
| 6669 | lo->lo_owner.so_is_open_owner = 0; |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6670 | lo->lo_owner.so_seqid = lock->lk_new_lock_seqid; |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 6671 | lo->lo_owner.so_ops = &lockowner_ops; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6672 | spin_lock(&clp->cl_lock); |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 6673 | ret = find_lockowner_str_locked(clp, &lock->lk_new_owner); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6674 | if (ret == NULL) { |
| 6675 | list_add(&lo->lo_owner.so_strhash, |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6676 | &clp->cl_ownerstr_hashtbl[strhashval]); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6677 | ret = lo; |
| 6678 | } else |
Kinglong Mee | d50ffde | 2015-07-16 12:05:07 +0800 | [diff] [blame] | 6679 | nfs4_free_stateowner(&lo->lo_owner); |
| 6680 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6681 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 340f0ba | 2015-03-23 11:02:30 -0400 | [diff] [blame] | 6682 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6683 | } |
| 6684 | |
Trond Myklebust | fd1fd68 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6685 | static struct nfs4_ol_stateid * |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 6686 | find_lock_stateid(const struct nfs4_lockowner *lo, |
| 6687 | const struct nfs4_ol_stateid *ost) |
Trond Myklebust | fd1fd68 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6688 | { |
| 6689 | struct nfs4_ol_stateid *lst; |
Trond Myklebust | fd1fd68 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6690 | |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 6691 | lockdep_assert_held(&ost->st_stid.sc_client->cl_lock); |
Trond Myklebust | fd1fd68 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6692 | |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 6693 | /* If ost is not hashed, ost->st_locks will not be valid */ |
| 6694 | if (!nfs4_ol_stateid_unhashed(ost)) |
| 6695 | list_for_each_entry(lst, &ost->st_locks, st_locks) { |
| 6696 | if (lst->st_stateowner == &lo->lo_owner) { |
| 6697 | refcount_inc(&lst->st_stid.sc_count); |
| 6698 | return lst; |
| 6699 | } |
Trond Myklebust | fd1fd68 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6700 | } |
Trond Myklebust | fd1fd68 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6701 | return NULL; |
| 6702 | } |
| 6703 | |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6704 | static struct nfs4_ol_stateid * |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 6705 | init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo, |
| 6706 | struct nfs4_file *fp, struct inode *inode, |
| 6707 | struct nfs4_ol_stateid *open_stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6708 | { |
J. Bruce Fields | d3b313a | 2011-09-15 15:02:41 -0400 | [diff] [blame] | 6709 | struct nfs4_client *clp = lo->lo_owner.so_client; |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6710 | struct nfs4_ol_stateid *retstp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6711 | |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6712 | mutex_init(&stp->st_mutex); |
Andrew Elble | 4f34bd0 | 2017-11-08 17:29:51 -0500 | [diff] [blame] | 6713 | mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX); |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6714 | retry: |
| 6715 | spin_lock(&clp->cl_lock); |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 6716 | if (nfs4_ol_stateid_unhashed(open_stp)) |
| 6717 | goto out_close; |
| 6718 | retstp = find_lock_stateid(lo, open_stp); |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6719 | if (retstp) |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 6720 | goto out_found; |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 6721 | refcount_inc(&stp->st_stid.sc_count); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 6722 | stp->st_stid.sc_type = NFS4_LOCK_STID; |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 6723 | stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 6724 | get_nfs4_file(fp); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 6725 | stp->st_stid.sc_file = fp; |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 6726 | stp->st_access_bmap = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6727 | stp->st_deny_bmap = open_stp->st_deny_bmap; |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 6728 | stp->st_openstp = open_stp; |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 6729 | spin_lock(&fp->fi_lock); |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 6730 | list_add(&stp->st_locks, &open_stp->st_locks); |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 6731 | list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 6732 | list_add(&stp->st_perfile, &fp->fi_stateids); |
| 6733 | spin_unlock(&fp->fi_lock); |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6734 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6735 | return stp; |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 6736 | out_found: |
| 6737 | spin_unlock(&clp->cl_lock); |
| 6738 | if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) { |
| 6739 | nfs4_put_stid(&retstp->st_stid); |
| 6740 | goto retry; |
| 6741 | } |
| 6742 | /* To keep mutex tracking happy */ |
| 6743 | mutex_unlock(&stp->st_mutex); |
| 6744 | return retstp; |
| 6745 | out_close: |
| 6746 | spin_unlock(&clp->cl_lock); |
| 6747 | mutex_unlock(&stp->st_mutex); |
| 6748 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6749 | } |
| 6750 | |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 6751 | static struct nfs4_ol_stateid * |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 6752 | find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi, |
| 6753 | struct inode *inode, struct nfs4_ol_stateid *ost, |
| 6754 | bool *new) |
| 6755 | { |
| 6756 | struct nfs4_stid *ns = NULL; |
| 6757 | struct nfs4_ol_stateid *lst; |
| 6758 | struct nfs4_openowner *oo = openowner(ost->st_stateowner); |
| 6759 | struct nfs4_client *clp = oo->oo_owner.so_client; |
| 6760 | |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6761 | *new = false; |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 6762 | spin_lock(&clp->cl_lock); |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 6763 | lst = find_lock_stateid(lo, ost); |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 6764 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6765 | if (lst != NULL) { |
| 6766 | if (nfsd4_lock_ol_stateid(lst) == nfs_ok) |
| 6767 | goto out; |
| 6768 | nfs4_put_stid(&lst->st_stid); |
| 6769 | } |
| 6770 | ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid); |
| 6771 | if (ns == NULL) |
| 6772 | return NULL; |
| 6773 | |
| 6774 | lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost); |
| 6775 | if (lst == openlockstateid(ns)) |
| 6776 | *new = true; |
| 6777 | else |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 6778 | nfs4_put_stid(ns); |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6779 | out: |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 6780 | return lst; |
| 6781 | } |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 6782 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 6783 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6784 | check_lock_length(u64 offset, u64 length) |
| 6785 | { |
Kinglong Mee | e796931 | 2015-07-13 17:34:19 +0800 | [diff] [blame] | 6786 | return ((length == 0) || ((length != NFS4_MAX_UINT64) && |
| 6787 | (length > ~offset))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6788 | } |
| 6789 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 6790 | static void get_lock_access(struct nfs4_ol_stateid *lock_stp, u32 access) |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 6791 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 6792 | struct nfs4_file *fp = lock_stp->st_stid.sc_file; |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 6793 | |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 6794 | lockdep_assert_held(&fp->fi_lock); |
| 6795 | |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 6796 | if (test_access(access, lock_stp)) |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 6797 | return; |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 6798 | __nfs4_file_get_access(fp, access); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 6799 | set_access(access, lock_stp); |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 6800 | } |
| 6801 | |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 6802 | static __be32 |
| 6803 | lookup_or_create_lock_state(struct nfsd4_compound_state *cstate, |
| 6804 | struct nfs4_ol_stateid *ost, |
| 6805 | struct nfsd4_lock *lock, |
Jeff Layton | dd25793 | 2016-08-11 10:37:39 -0400 | [diff] [blame] | 6806 | struct nfs4_ol_stateid **plst, bool *new) |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6807 | { |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6808 | __be32 status; |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 6809 | struct nfs4_file *fi = ost->st_stid.sc_file; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6810 | struct nfs4_openowner *oo = openowner(ost->st_stateowner); |
| 6811 | struct nfs4_client *cl = oo->oo_owner.so_client; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 6812 | struct inode *inode = d_inode(cstate->current_fh.fh_dentry); |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6813 | struct nfs4_lockowner *lo; |
Jeff Layton | dd25793 | 2016-08-11 10:37:39 -0400 | [diff] [blame] | 6814 | struct nfs4_ol_stateid *lst; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6815 | unsigned int strhashval; |
| 6816 | |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 6817 | lo = find_lockowner_str(cl, &lock->lk_new_owner); |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 6818 | if (!lo) { |
Kinglong Mee | 76f6c9e | 2015-07-13 17:35:10 +0800 | [diff] [blame] | 6819 | strhashval = ownerstr_hashval(&lock->lk_new_owner); |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 6820 | lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock); |
| 6821 | if (lo == NULL) |
| 6822 | return nfserr_jukebox; |
| 6823 | } else { |
| 6824 | /* with an existing lockowner, seqids must be the same */ |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6825 | status = nfserr_bad_seqid; |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 6826 | if (!cstate->minorversion && |
| 6827 | lock->lk_new_lock_seqid != lo->lo_owner.so_seqid) |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6828 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6829 | } |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 6830 | |
Jeff Layton | dd25793 | 2016-08-11 10:37:39 -0400 | [diff] [blame] | 6831 | lst = find_or_create_lock_stateid(lo, fi, inode, ost, new); |
| 6832 | if (lst == NULL) { |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6833 | status = nfserr_jukebox; |
| 6834 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6835 | } |
Jeff Layton | dd25793 | 2016-08-11 10:37:39 -0400 | [diff] [blame] | 6836 | |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6837 | status = nfs_ok; |
Jeff Layton | dd25793 | 2016-08-11 10:37:39 -0400 | [diff] [blame] | 6838 | *plst = lst; |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6839 | out: |
| 6840 | nfs4_put_stateowner(&lo->lo_owner); |
| 6841 | return status; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6842 | } |
| 6843 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6844 | /* |
| 6845 | * LOCK operation |
| 6846 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6847 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6848 | nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6849 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6850 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6851 | struct nfsd4_lock *lock = &u->lock; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6852 | struct nfs4_openowner *open_sop = NULL; |
| 6853 | struct nfs4_lockowner *lock_sop = NULL; |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 6854 | struct nfs4_ol_stateid *lock_stp = NULL; |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 6855 | struct nfs4_ol_stateid *open_stp = NULL; |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 6856 | struct nfs4_file *fp; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6857 | struct nfsd_file *nf = NULL; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6858 | struct nfsd4_blocked_lock *nbl = NULL; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6859 | struct file_lock *file_lock = NULL; |
| 6860 | struct file_lock *conflock = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6861 | __be32 status = 0; |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 6862 | int lkflg; |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 6863 | int err; |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6864 | bool new = false; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6865 | unsigned char fl_type; |
| 6866 | unsigned int fl_flags = FL_POSIX; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6867 | struct net *net = SVC_NET(rqstp); |
| 6868 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6869 | |
| 6870 | dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n", |
| 6871 | (long long) lock->lk_offset, |
| 6872 | (long long) lock->lk_length); |
| 6873 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6874 | if (check_lock_length(lock->lk_offset, lock->lk_length)) |
| 6875 | return nfserr_inval; |
| 6876 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6877 | if ((status = fh_verify(rqstp, &cstate->current_fh, |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 6878 | S_IFREG, NFSD_MAY_LOCK))) { |
Andy Adamson | a6f6ef2 | 2006-01-18 17:43:17 -0800 | [diff] [blame] | 6879 | dprintk("NFSD: nfsd4_lock: permission denied!\n"); |
| 6880 | return status; |
| 6881 | } |
| 6882 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6883 | if (lock->lk_is_new) { |
J. Bruce Fields | 684e563 | 2011-11-04 17:08:10 -0400 | [diff] [blame] | 6884 | if (nfsd4_has_session(cstate)) |
| 6885 | /* See rfc 5661 18.10.3: given clientid is ignored: */ |
Kinglong Mee | 76f6c9e | 2015-07-13 17:35:10 +0800 | [diff] [blame] | 6886 | memcpy(&lock->lk_new_clientid, |
J. Bruce Fields | ec59659 | 2021-01-21 17:57:45 -0500 | [diff] [blame] | 6887 | &cstate->clp->cl_clientid, |
J. Bruce Fields | 684e563 | 2011-11-04 17:08:10 -0400 | [diff] [blame] | 6888 | sizeof(clientid_t)); |
| 6889 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6890 | /* validate and update open stateid and open seqid */ |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 6891 | status = nfs4_preprocess_confirmed_seqid_op(cstate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6892 | lock->lk_new_open_seqid, |
| 6893 | &lock->lk_new_open_stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6894 | &open_stp, nn); |
NeilBrown | 3751517 | 2005-07-07 17:59:16 -0700 | [diff] [blame] | 6895 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6896 | goto out; |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 6897 | mutex_unlock(&open_stp->st_mutex); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6898 | open_sop = openowner(open_stp->st_stateowner); |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 6899 | status = nfserr_bad_stateid; |
J. Bruce Fields | 684e563 | 2011-11-04 17:08:10 -0400 | [diff] [blame] | 6900 | if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid, |
Kinglong Mee | 76f6c9e | 2015-07-13 17:35:10 +0800 | [diff] [blame] | 6901 | &lock->lk_new_clientid)) |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 6902 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6903 | status = lookup_or_create_lock_state(cstate, open_stp, lock, |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6904 | &lock_stp, &new); |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 6905 | } else { |
Benny Halevy | dd453df | 2009-04-03 08:28:41 +0300 | [diff] [blame] | 6906 | status = nfs4_preprocess_seqid_op(cstate, |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6907 | lock->lk_old_lock_seqid, |
| 6908 | &lock->lk_old_lock_stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6909 | NFS4_LOCK_STID, &lock_stp, nn); |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 6910 | } |
J. Bruce Fields | e1aaa89 | 2012-06-06 16:01:37 -0400 | [diff] [blame] | 6911 | if (status) |
| 6912 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6913 | lock_sop = lockowner(lock_stp->st_stateowner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6914 | |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 6915 | lkflg = setlkflg(lock->lk_type); |
| 6916 | status = nfs4_check_openmode(lock_stp, lkflg); |
| 6917 | if (status) |
| 6918 | goto out; |
| 6919 | |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 6920 | status = nfserr_grace; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6921 | if (locks_in_grace(net) && !lock->lk_reclaim) |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 6922 | goto out; |
| 6923 | status = nfserr_no_grace; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6924 | if (!locks_in_grace(net) && lock->lk_reclaim) |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 6925 | goto out; |
| 6926 | |
J. Bruce Fields | bb0a55b | 2021-08-20 17:02:06 -0400 | [diff] [blame] | 6927 | if (lock->lk_reclaim) |
| 6928 | fl_flags |= FL_RECLAIM; |
| 6929 | |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 6930 | fp = lock_stp->st_stid.sc_file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6931 | switch (lock->lk_type) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6932 | case NFS4_READW_LT: |
J. Bruce Fields | 40595cd | 2021-12-16 12:20:13 -0500 | [diff] [blame] | 6933 | if (nfsd4_has_session(cstate)) |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6934 | fl_flags |= FL_SLEEP; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 6935 | fallthrough; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6936 | case NFS4_READ_LT: |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 6937 | spin_lock(&fp->fi_lock); |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6938 | nf = find_readable_file_locked(fp); |
| 6939 | if (nf) |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 6940 | get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ); |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 6941 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6942 | fl_type = F_RDLCK; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 6943 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6944 | case NFS4_WRITEW_LT: |
J. Bruce Fields | 40595cd | 2021-12-16 12:20:13 -0500 | [diff] [blame] | 6945 | if (nfsd4_has_session(cstate)) |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6946 | fl_flags |= FL_SLEEP; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 6947 | fallthrough; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6948 | case NFS4_WRITE_LT: |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 6949 | spin_lock(&fp->fi_lock); |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6950 | nf = find_writeable_file_locked(fp); |
| 6951 | if (nf) |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 6952 | get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE); |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 6953 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6954 | fl_type = F_WRLCK; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 6955 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6956 | default: |
| 6957 | status = nfserr_inval; |
| 6958 | goto out; |
| 6959 | } |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6960 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6961 | if (!nf) { |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 6962 | status = nfserr_openmode; |
| 6963 | goto out; |
| 6964 | } |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6965 | |
J. Bruce Fields | 40595cd | 2021-12-16 12:20:13 -0500 | [diff] [blame] | 6966 | /* |
| 6967 | * Most filesystems with their own ->lock operations will block |
| 6968 | * the nfsd thread waiting to acquire the lock. That leads to |
| 6969 | * deadlocks (we don't want every nfsd thread tied up waiting |
| 6970 | * for file locks), so don't attempt blocking lock notifications |
| 6971 | * on those filesystems: |
| 6972 | */ |
| 6973 | if (nf->nf_file->f_op->lock) |
| 6974 | fl_flags &= ~FL_SLEEP; |
| 6975 | |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6976 | nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn); |
| 6977 | if (!nbl) { |
| 6978 | dprintk("NFSD: %s: unable to allocate block!\n", __func__); |
| 6979 | status = nfserr_jukebox; |
| 6980 | goto out; |
| 6981 | } |
| 6982 | |
| 6983 | file_lock = &nbl->nbl_lock; |
| 6984 | file_lock->fl_type = fl_type; |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6985 | file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(&lock_sop->lo_owner)); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6986 | file_lock->fl_pid = current->tgid; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6987 | file_lock->fl_file = nf->nf_file; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6988 | file_lock->fl_flags = fl_flags; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6989 | file_lock->fl_lmops = &nfsd_posix_mng_ops; |
| 6990 | file_lock->fl_start = lock->lk_offset; |
| 6991 | file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length); |
| 6992 | nfs4_transform_lock_offset(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6993 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6994 | conflock = locks_alloc_lock(); |
| 6995 | if (!conflock) { |
| 6996 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 6997 | status = nfserr_jukebox; |
| 6998 | goto out; |
| 6999 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7000 | |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 7001 | if (fl_flags & FL_SLEEP) { |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 7002 | nbl->nbl_time = ktime_get_boottime_seconds(); |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 7003 | spin_lock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 7004 | list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked); |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 7005 | list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru); |
Vasily Averin | 47446d7 | 2021-12-17 09:49:39 +0300 | [diff] [blame] | 7006 | kref_get(&nbl->nbl_kref); |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 7007 | spin_unlock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 7008 | } |
| 7009 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 7010 | err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 7011 | switch (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7012 | case 0: /* success! */ |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 7013 | nfs4_inc_and_copy_stateid(&lock->lk_resp_stateid, &lock_stp->st_stid); |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 7014 | status = 0; |
J. Bruce Fields | 03f318c | 2018-06-08 12:28:47 -0400 | [diff] [blame] | 7015 | if (lock->lk_reclaim) |
| 7016 | nn->somebody_reclaimed = true; |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 7017 | break; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 7018 | case FILE_LOCK_DEFERRED: |
Vasily Averin | 47446d7 | 2021-12-17 09:49:39 +0300 | [diff] [blame] | 7019 | kref_put(&nbl->nbl_kref, free_nbl); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 7020 | nbl = NULL; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 7021 | fallthrough; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 7022 | case -EAGAIN: /* conflock holds conflicting lock */ |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 7023 | status = nfserr_denied; |
| 7024 | dprintk("NFSD: nfsd4_lock: conflicting lock found!\n"); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 7025 | nfs4_set_lock_denied(conflock, &lock->lk_denied); |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 7026 | break; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 7027 | case -EDEADLK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7028 | status = nfserr_deadlock; |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 7029 | break; |
J. Bruce Fields | 3e77246 | 2011-08-10 19:07:33 -0400 | [diff] [blame] | 7030 | default: |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 7031 | dprintk("NFSD: nfsd4_lock: vfs_lock_file() failed! status %d\n",err); |
J. Bruce Fields | 3e77246 | 2011-08-10 19:07:33 -0400 | [diff] [blame] | 7032 | status = nfserrno(err); |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 7033 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7034 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7035 | out: |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 7036 | if (nbl) { |
| 7037 | /* dequeue it if we queued it before */ |
| 7038 | if (fl_flags & FL_SLEEP) { |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 7039 | spin_lock(&nn->blocked_locks_lock); |
Vasily Averin | 47446d7 | 2021-12-17 09:49:39 +0300 | [diff] [blame] | 7040 | if (!list_empty(&nbl->nbl_list) && |
| 7041 | !list_empty(&nbl->nbl_lru)) { |
| 7042 | list_del_init(&nbl->nbl_list); |
| 7043 | list_del_init(&nbl->nbl_lru); |
| 7044 | kref_put(&nbl->nbl_kref, free_nbl); |
| 7045 | } |
| 7046 | /* nbl can use one of lists to be linked to reaplist */ |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 7047 | spin_unlock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 7048 | } |
| 7049 | free_blocked_lock(nbl); |
| 7050 | } |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 7051 | if (nf) |
| 7052 | nfsd_file_put(nf); |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 7053 | if (lock_stp) { |
| 7054 | /* Bump seqid manually if the 4.0 replay owner is openowner */ |
| 7055 | if (cstate->replay_owner && |
| 7056 | cstate->replay_owner != &lock_sop->lo_owner && |
| 7057 | seqid_mutating_err(ntohl(status))) |
| 7058 | lock_sop->lo_owner.so_seqid++; |
| 7059 | |
| 7060 | /* |
| 7061 | * If this is a new, never-before-used stateid, and we are |
| 7062 | * returning an error, then just go ahead and release it. |
| 7063 | */ |
J. Bruce Fields | 2502072 | 2018-01-17 16:25:59 -0500 | [diff] [blame] | 7064 | if (status && new) |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 7065 | release_lock_stateid(lock_stp); |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 7066 | |
| 7067 | mutex_unlock(&lock_stp->st_mutex); |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 7068 | |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 7069 | nfs4_put_stid(&lock_stp->st_stid); |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 7070 | } |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 7071 | if (open_stp) |
| 7072 | nfs4_put_stid(&open_stp->st_stid); |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 7073 | nfsd4_bump_seqid(cstate, status); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 7074 | if (conflock) |
| 7075 | locks_free_lock(conflock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7076 | return status; |
| 7077 | } |
| 7078 | |
| 7079 | /* |
J. Bruce Fields | 55ef127 | 2008-12-20 11:58:38 -0800 | [diff] [blame] | 7080 | * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN, |
| 7081 | * so we do a temporary open here just to get an open file to pass to |
J. Bruce Fields | 0bcc7ca | 2021-08-24 22:36:03 -0400 | [diff] [blame] | 7082 | * vfs_test_lock. |
J. Bruce Fields | 55ef127 | 2008-12-20 11:58:38 -0800 | [diff] [blame] | 7083 | */ |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 7084 | static __be32 nfsd_test_lock(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file_lock *lock) |
J. Bruce Fields | 55ef127 | 2008-12-20 11:58:38 -0800 | [diff] [blame] | 7085 | { |
Jeff Layton | 6b556ca | 2019-08-18 14:18:55 -0400 | [diff] [blame] | 7086 | struct nfsd_file *nf; |
J. Bruce Fields | 217fd6f | 2021-04-16 14:00:14 -0400 | [diff] [blame] | 7087 | __be32 err; |
| 7088 | |
| 7089 | err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf); |
| 7090 | if (err) |
| 7091 | return err; |
| 7092 | fh_lock(fhp); /* to block new leases till after test_lock: */ |
| 7093 | err = nfserrno(nfsd_open_break_lease(fhp->fh_dentry->d_inode, |
| 7094 | NFSD_MAY_READ)); |
| 7095 | if (err) |
| 7096 | goto out; |
J. Bruce Fields | 0bcc7ca | 2021-08-24 22:36:03 -0400 | [diff] [blame] | 7097 | lock->fl_file = nf->nf_file; |
J. Bruce Fields | 217fd6f | 2021-04-16 14:00:14 -0400 | [diff] [blame] | 7098 | err = nfserrno(vfs_test_lock(nf->nf_file, lock)); |
J. Bruce Fields | 0bcc7ca | 2021-08-24 22:36:03 -0400 | [diff] [blame] | 7099 | lock->fl_file = NULL; |
J. Bruce Fields | 217fd6f | 2021-04-16 14:00:14 -0400 | [diff] [blame] | 7100 | out: |
| 7101 | fh_unlock(fhp); |
| 7102 | nfsd_file_put(nf); |
J. Bruce Fields | 55ef127 | 2008-12-20 11:58:38 -0800 | [diff] [blame] | 7103 | return err; |
| 7104 | } |
| 7105 | |
| 7106 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7107 | * LOCKT operation |
| 7108 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 7109 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 7110 | nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 7111 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7112 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 7113 | struct nfsd4_lockt *lockt = &u->lockt; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 7114 | struct file_lock *file_lock = NULL; |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 7115 | struct nfs4_lockowner *lo = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 7116 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 7117 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7118 | |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 7119 | if (locks_in_grace(SVC_NET(rqstp))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7120 | return nfserr_grace; |
| 7121 | |
| 7122 | if (check_lock_length(lockt->lt_offset, lockt->lt_length)) |
| 7123 | return nfserr_inval; |
| 7124 | |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 7125 | if (!nfsd4_has_session(cstate)) { |
J. Bruce Fields | f71475b | 2021-01-21 17:57:43 -0500 | [diff] [blame] | 7126 | status = set_client(&lockt->lt_clientid, cstate, nn); |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 7127 | if (status) |
| 7128 | goto out; |
| 7129 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7130 | |
J. Bruce Fields | 75c096f | 2011-08-15 18:39:32 -0400 | [diff] [blame] | 7131 | if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7132 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7133 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 7134 | file_lock = locks_alloc_lock(); |
| 7135 | if (!file_lock) { |
| 7136 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 7137 | status = nfserr_jukebox; |
| 7138 | goto out; |
| 7139 | } |
Kinglong Mee | 6cd9066 | 2014-08-15 08:02:55 +0800 | [diff] [blame] | 7140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7141 | switch (lockt->lt_type) { |
| 7142 | case NFS4_READ_LT: |
| 7143 | case NFS4_READW_LT: |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 7144 | file_lock->fl_type = F_RDLCK; |
Colin Ian King | f50c9d7 | 2018-11-03 16:12:53 +0000 | [diff] [blame] | 7145 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7146 | case NFS4_WRITE_LT: |
| 7147 | case NFS4_WRITEW_LT: |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 7148 | file_lock->fl_type = F_WRLCK; |
Colin Ian King | f50c9d7 | 2018-11-03 16:12:53 +0000 | [diff] [blame] | 7149 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7150 | default: |
J. Bruce Fields | 2fdada0 | 2007-07-27 16:10:37 -0400 | [diff] [blame] | 7151 | dprintk("NFSD: nfs4_lockt: bad lock type!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7152 | status = nfserr_inval; |
Colin Ian King | f50c9d7 | 2018-11-03 16:12:53 +0000 | [diff] [blame] | 7153 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7154 | } |
| 7155 | |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 7156 | lo = find_lockowner_str(cstate->clp, &lockt->lt_owner); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 7157 | if (lo) |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 7158 | file_lock->fl_owner = (fl_owner_t)lo; |
| 7159 | file_lock->fl_pid = current->tgid; |
| 7160 | file_lock->fl_flags = FL_POSIX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7161 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 7162 | file_lock->fl_start = lockt->lt_offset; |
| 7163 | file_lock->fl_end = last_byte_offset(lockt->lt_offset, lockt->lt_length); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7164 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 7165 | nfs4_transform_lock_offset(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7166 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 7167 | status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock); |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 7168 | if (status) |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 7169 | goto out; |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 7170 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 7171 | if (file_lock->fl_type != F_UNLCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7172 | status = nfserr_denied; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 7173 | nfs4_set_lock_denied(file_lock, &lockt->lt_denied); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7174 | } |
| 7175 | out: |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 7176 | if (lo) |
| 7177 | nfs4_put_stateowner(&lo->lo_owner); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 7178 | if (file_lock) |
| 7179 | locks_free_lock(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7180 | return status; |
| 7181 | } |
| 7182 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 7183 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 7184 | nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 7185 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7186 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 7187 | struct nfsd4_locku *locku = &u->locku; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 7188 | struct nfs4_ol_stateid *stp; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 7189 | struct nfsd_file *nf = NULL; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 7190 | struct file_lock *file_lock = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 7191 | __be32 status; |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 7192 | int err; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 7193 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
| 7194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7195 | dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n", |
| 7196 | (long long) locku->lu_offset, |
| 7197 | (long long) locku->lu_length); |
| 7198 | |
| 7199 | if (check_lock_length(locku->lu_offset, locku->lu_length)) |
| 7200 | return nfserr_inval; |
| 7201 | |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 7202 | status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 7203 | &locku->lu_stateid, NFS4_LOCK_STID, |
| 7204 | &stp, nn); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 7205 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7206 | goto out; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 7207 | nf = find_any_file(stp->st_stid.sc_file); |
| 7208 | if (!nf) { |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 7209 | status = nfserr_lock_range; |
Trond Myklebust | 858cc57 | 2014-07-29 21:34:16 -0400 | [diff] [blame] | 7210 | goto put_stateid; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 7211 | } |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 7212 | file_lock = locks_alloc_lock(); |
| 7213 | if (!file_lock) { |
| 7214 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 7215 | status = nfserr_jukebox; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 7216 | goto put_file; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 7217 | } |
Kinglong Mee | 6cd9066 | 2014-08-15 08:02:55 +0800 | [diff] [blame] | 7218 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 7219 | file_lock->fl_type = F_UNLCK; |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 7220 | file_lock->fl_owner = (fl_owner_t)lockowner(nfs4_get_stateowner(stp->st_stateowner)); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 7221 | file_lock->fl_pid = current->tgid; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 7222 | file_lock->fl_file = nf->nf_file; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 7223 | file_lock->fl_flags = FL_POSIX; |
| 7224 | file_lock->fl_lmops = &nfsd_posix_mng_ops; |
| 7225 | file_lock->fl_start = locku->lu_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7226 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 7227 | file_lock->fl_end = last_byte_offset(locku->lu_offset, |
| 7228 | locku->lu_length); |
| 7229 | nfs4_transform_lock_offset(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7230 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 7231 | err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL); |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 7232 | if (err) { |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 7233 | dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7234 | goto out_nfserr; |
| 7235 | } |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 7236 | nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid); |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 7237 | put_file: |
| 7238 | nfsd_file_put(nf); |
Trond Myklebust | 858cc57 | 2014-07-29 21:34:16 -0400 | [diff] [blame] | 7239 | put_stateid: |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 7240 | mutex_unlock(&stp->st_mutex); |
Trond Myklebust | 858cc57 | 2014-07-29 21:34:16 -0400 | [diff] [blame] | 7241 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7242 | out: |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 7243 | nfsd4_bump_seqid(cstate, status); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 7244 | if (file_lock) |
| 7245 | locks_free_lock(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7246 | return status; |
| 7247 | |
| 7248 | out_nfserr: |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 7249 | status = nfserrno(err); |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 7250 | goto put_file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7251 | } |
| 7252 | |
| 7253 | /* |
| 7254 | * returns |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 7255 | * true: locks held by lockowner |
| 7256 | * false: no locks held by lockowner |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7257 | */ |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 7258 | static bool |
| 7259 | check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7260 | { |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 7261 | struct file_lock *fl; |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 7262 | int status = false; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 7263 | struct nfsd_file *nf = find_any_file(fp); |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 7264 | struct inode *inode; |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 7265 | struct file_lock_context *flctx; |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 7266 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 7267 | if (!nf) { |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 7268 | /* Any valid lock stateid should have some sort of access */ |
| 7269 | WARN_ON_ONCE(1); |
| 7270 | return status; |
| 7271 | } |
| 7272 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 7273 | inode = locks_inode(nf->nf_file); |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 7274 | flctx = inode->i_flctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7275 | |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 7276 | if (flctx && !list_empty_careful(&flctx->flc_posix)) { |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 7277 | spin_lock(&flctx->flc_lock); |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 7278 | list_for_each_entry(fl, &flctx->flc_posix, fl_list) { |
| 7279 | if (fl->fl_owner == (fl_owner_t)lowner) { |
| 7280 | status = true; |
| 7281 | break; |
| 7282 | } |
J. Bruce Fields | 796dadf | 2006-01-18 17:43:22 -0800 | [diff] [blame] | 7283 | } |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 7284 | spin_unlock(&flctx->flc_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7285 | } |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 7286 | nfsd_file_put(nf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7287 | return status; |
| 7288 | } |
| 7289 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 7290 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 7291 | nfsd4_release_lockowner(struct svc_rqst *rqstp, |
| 7292 | struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 7293 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7294 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 7295 | struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7296 | clientid_t *clid = &rlockowner->rl_clientid; |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 7297 | struct nfs4_stateowner *sop; |
| 7298 | struct nfs4_lockowner *lo = NULL; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 7299 | struct nfs4_ol_stateid *stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7300 | struct xdr_netobj *owner = &rlockowner->rl_owner; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 7301 | unsigned int hashval = ownerstr_hashval(owner); |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 7302 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 7303 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 7304 | struct nfs4_client *clp; |
Chuck Lever | 8858481 | 2016-07-13 16:40:14 -0400 | [diff] [blame] | 7305 | LIST_HEAD (reaplist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7306 | |
| 7307 | dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n", |
| 7308 | clid->cl_boot, clid->cl_id); |
| 7309 | |
J. Bruce Fields | f71475b | 2021-01-21 17:57:43 -0500 | [diff] [blame] | 7310 | status = set_client(clid, cstate, nn); |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 7311 | if (status) |
Trond Myklebust | 51f5e78 | 2014-07-30 08:27:27 -0400 | [diff] [blame] | 7312 | return status; |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 7313 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 7314 | clp = cstate->clp; |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 7315 | /* Find the matching lock stateowner */ |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 7316 | spin_lock(&clp->cl_lock); |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 7317 | list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval], |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 7318 | so_strhash) { |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 7319 | |
| 7320 | if (sop->so_is_open_owner || !same_owner_str(sop, owner)) |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 7321 | continue; |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 7322 | |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 7323 | /* see if there are still any locks associated with it */ |
| 7324 | lo = lockowner(sop); |
| 7325 | list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) { |
| 7326 | if (check_for_locks(stp->st_stid.sc_file, lo)) { |
| 7327 | status = nfserr_locks_held; |
| 7328 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | 51f5e78 | 2014-07-30 08:27:27 -0400 | [diff] [blame] | 7329 | return status; |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 7330 | } |
Jeff Layton | 5adfd88 | 2014-07-29 21:34:31 -0400 | [diff] [blame] | 7331 | } |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 7332 | |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 7333 | nfs4_get_stateowner(sop); |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 7334 | break; |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 7335 | } |
Chuck Lever | 8858481 | 2016-07-13 16:40:14 -0400 | [diff] [blame] | 7336 | if (!lo) { |
| 7337 | spin_unlock(&clp->cl_lock); |
| 7338 | return status; |
| 7339 | } |
| 7340 | |
| 7341 | unhash_lockowner_locked(lo); |
| 7342 | while (!list_empty(&lo->lo_owner.so_stateids)) { |
| 7343 | stp = list_first_entry(&lo->lo_owner.so_stateids, |
| 7344 | struct nfs4_ol_stateid, |
| 7345 | st_perstateowner); |
| 7346 | WARN_ON(!unhash_lock_stateid(stp)); |
| 7347 | put_ol_stateid_locked(stp, &reaplist); |
| 7348 | } |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 7349 | spin_unlock(&clp->cl_lock); |
Chuck Lever | 8858481 | 2016-07-13 16:40:14 -0400 | [diff] [blame] | 7350 | free_ol_stateid_reaplist(&reaplist); |
Jeff Layton | 68ef3bc | 2018-03-16 11:32:02 -0400 | [diff] [blame] | 7351 | remove_blocked_locks(lo); |
Chuck Lever | 8858481 | 2016-07-13 16:40:14 -0400 | [diff] [blame] | 7352 | nfs4_put_stateowner(&lo->lo_owner); |
| 7353 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7354 | return status; |
| 7355 | } |
| 7356 | |
| 7357 | static inline struct nfs4_client_reclaim * |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 7358 | alloc_reclaim(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7359 | { |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 7360 | return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7361 | } |
| 7362 | |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 7363 | bool |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 7364 | nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn) |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 7365 | { |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 7366 | struct nfs4_client_reclaim *crp; |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 7367 | |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7368 | crp = nfsd4_find_reclaim_client(name, nn); |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 7369 | return (crp && crp->cr_clp); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 7370 | } |
| 7371 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7372 | /* |
| 7373 | * failure => all reset bets are off, nfserr_no_grace... |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 7374 | * |
| 7375 | * The caller is responsible for freeing name.data if NULL is returned (it |
| 7376 | * will be freed in nfs4_remove_reclaim_record in the normal case). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7377 | */ |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 7378 | struct nfs4_client_reclaim * |
Scott Mayhew | 6ee95d1 | 2019-09-09 16:10:31 -0400 | [diff] [blame] | 7379 | nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash, |
| 7380 | struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7381 | { |
| 7382 | unsigned int strhashval; |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 7383 | struct nfs4_client_reclaim *crp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7384 | |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 7385 | crp = alloc_reclaim(); |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 7386 | if (crp) { |
| 7387 | strhashval = clientstr_hashval(name); |
| 7388 | INIT_LIST_HEAD(&crp->cr_strhash); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7389 | list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]); |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 7390 | crp->cr_name.data = name.data; |
| 7391 | crp->cr_name.len = name.len; |
Scott Mayhew | 6ee95d1 | 2019-09-09 16:10:31 -0400 | [diff] [blame] | 7392 | crp->cr_princhash.data = princhash.data; |
| 7393 | crp->cr_princhash.len = princhash.len; |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 7394 | crp->cr_clp = NULL; |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7395 | nn->reclaim_str_hashtbl_size++; |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 7396 | } |
| 7397 | return crp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7398 | } |
| 7399 | |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 7400 | void |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7401 | nfs4_remove_reclaim_record(struct nfs4_client_reclaim *crp, struct nfsd_net *nn) |
Jeff Layton | ce30e53 | 2012-11-12 15:00:53 -0500 | [diff] [blame] | 7402 | { |
| 7403 | list_del(&crp->cr_strhash); |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 7404 | kfree(crp->cr_name.data); |
Scott Mayhew | 6ee95d1 | 2019-09-09 16:10:31 -0400 | [diff] [blame] | 7405 | kfree(crp->cr_princhash.data); |
Jeff Layton | ce30e53 | 2012-11-12 15:00:53 -0500 | [diff] [blame] | 7406 | kfree(crp); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7407 | nn->reclaim_str_hashtbl_size--; |
Jeff Layton | ce30e53 | 2012-11-12 15:00:53 -0500 | [diff] [blame] | 7408 | } |
| 7409 | |
| 7410 | void |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7411 | nfs4_release_reclaim(struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7412 | { |
| 7413 | struct nfs4_client_reclaim *crp = NULL; |
| 7414 | int i; |
| 7415 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7416 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7417 | while (!list_empty(&nn->reclaim_str_hashtbl[i])) { |
| 7418 | crp = list_entry(nn->reclaim_str_hashtbl[i].next, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7419 | struct nfs4_client_reclaim, cr_strhash); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7420 | nfs4_remove_reclaim_record(crp, nn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7421 | } |
| 7422 | } |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 7423 | WARN_ON_ONCE(nn->reclaim_str_hashtbl_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7424 | } |
| 7425 | |
| 7426 | /* |
| 7427 | * called from OPEN, CLAIM_PREVIOUS with a new clientid. */ |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 7428 | struct nfs4_client_reclaim * |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 7429 | nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7430 | { |
| 7431 | unsigned int strhashval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7432 | struct nfs4_client_reclaim *crp = NULL; |
| 7433 | |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 7434 | strhashval = clientstr_hashval(name); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7435 | list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) { |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 7436 | if (compare_blob(&crp->cr_name, &name) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7437 | return crp; |
| 7438 | } |
| 7439 | } |
| 7440 | return NULL; |
| 7441 | } |
| 7442 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 7443 | __be32 |
J. Bruce Fields | 1722b04 | 2021-01-21 17:57:44 -0500 | [diff] [blame] | 7444 | nfs4_check_open_reclaim(struct nfs4_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7445 | { |
J. Bruce Fields | 1722b04 | 2021-01-21 17:57:44 -0500 | [diff] [blame] | 7446 | if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags)) |
Jeff Layton | 3b3e7b7 | 2014-09-12 16:40:22 -0400 | [diff] [blame] | 7447 | return nfserr_no_grace; |
| 7448 | |
J. Bruce Fields | 1722b04 | 2021-01-21 17:57:44 -0500 | [diff] [blame] | 7449 | if (nfsd4_client_record_check(clp)) |
Trond Myklebust | 0fe492d | 2014-06-30 11:48:47 -0400 | [diff] [blame] | 7450 | return nfserr_reclaim_bad; |
| 7451 | |
| 7452 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7453 | } |
| 7454 | |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 7455 | /* |
| 7456 | * Since the lifetime of a delegation isn't limited to that of an open, a |
| 7457 | * client may quite reasonably hang on to a delegation as long as it has |
| 7458 | * the inode cached. This becomes an obvious problem the first time a |
| 7459 | * client's inode cache approaches the size of the server's total memory. |
| 7460 | * |
| 7461 | * For now we avoid this problem by imposing a hard limit on the number |
| 7462 | * of delegations, which varies according to the server's memory size. |
| 7463 | */ |
| 7464 | static void |
| 7465 | set_max_delegations(void) |
| 7466 | { |
| 7467 | /* |
| 7468 | * Allow at most 4 delegations per megabyte of RAM. Quick |
| 7469 | * estimates suggest that in the worst case (where every delegation |
| 7470 | * is for a different inode), a delegation could take about 1.5K, |
| 7471 | * giving a worst case usage of about 6% of memory. |
| 7472 | */ |
| 7473 | max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT); |
| 7474 | } |
| 7475 | |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 7476 | static int nfs4_state_create_net(struct net *net) |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7477 | { |
| 7478 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 7479 | int i; |
| 7480 | |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 7481 | nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE, |
| 7482 | sizeof(struct list_head), |
| 7483 | GFP_KERNEL); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7484 | if (!nn->conf_id_hashtbl) |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 7485 | goto err; |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 7486 | nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE, |
| 7487 | sizeof(struct list_head), |
| 7488 | GFP_KERNEL); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 7489 | if (!nn->unconf_id_hashtbl) |
| 7490 | goto err_unconf_id; |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 7491 | nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE, |
| 7492 | sizeof(struct list_head), |
| 7493 | GFP_KERNEL); |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 7494 | if (!nn->sessionid_hashtbl) |
| 7495 | goto err_sessionid; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7496 | |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 7497 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7498 | INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 7499 | INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 7500 | } |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 7501 | for (i = 0; i < SESSION_HASH_SIZE; i++) |
| 7502 | INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 7503 | nn->conf_name_tree = RB_ROOT; |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 7504 | nn->unconf_name_tree = RB_ROOT; |
Arnd Bergmann | 9cc7680 | 2019-10-31 15:53:13 +0100 | [diff] [blame] | 7505 | nn->boot_time = ktime_get_real_seconds(); |
Vasily Averin | 81833de | 2017-11-13 07:25:40 +0300 | [diff] [blame] | 7506 | nn->grace_ended = false; |
| 7507 | nn->nfsd4_manager.block_opens = true; |
| 7508 | INIT_LIST_HEAD(&nn->nfsd4_manager.list); |
Stanislav Kinsbursky | 5ed58bb | 2012-11-14 18:21:56 +0300 | [diff] [blame] | 7509 | INIT_LIST_HEAD(&nn->client_lru); |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 7510 | INIT_LIST_HEAD(&nn->close_lru); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 7511 | INIT_LIST_HEAD(&nn->del_recall_lru); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 7512 | spin_lock_init(&nn->client_lock); |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 7513 | spin_lock_init(&nn->s2s_cp_lock); |
| 7514 | idr_init(&nn->s2s_cp_stateids); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7515 | |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 7516 | spin_lock_init(&nn->blocked_locks_lock); |
| 7517 | INIT_LIST_HEAD(&nn->blocked_locks_lru); |
| 7518 | |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 7519 | INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 7520 | get_net(net); |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 7521 | |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7522 | return 0; |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 7523 | |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 7524 | err_sessionid: |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 7525 | kfree(nn->unconf_id_hashtbl); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 7526 | err_unconf_id: |
| 7527 | kfree(nn->conf_id_hashtbl); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 7528 | err: |
| 7529 | return -ENOMEM; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7530 | } |
| 7531 | |
| 7532 | static void |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 7533 | nfs4_state_destroy_net(struct net *net) |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7534 | { |
| 7535 | int i; |
| 7536 | struct nfs4_client *clp = NULL; |
| 7537 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 7538 | |
| 7539 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
| 7540 | while (!list_empty(&nn->conf_id_hashtbl[i])) { |
| 7541 | clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); |
| 7542 | destroy_client(clp); |
| 7543 | } |
| 7544 | } |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 7545 | |
Jeff Layton | 68ef3bc | 2018-03-16 11:32:02 -0400 | [diff] [blame] | 7546 | WARN_ON(!list_empty(&nn->blocked_locks_lru)); |
| 7547 | |
Kinglong Mee | 2b90563 | 2014-03-26 22:09:30 +0800 | [diff] [blame] | 7548 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
| 7549 | while (!list_empty(&nn->unconf_id_hashtbl[i])) { |
| 7550 | clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); |
| 7551 | destroy_client(clp); |
| 7552 | } |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 7553 | } |
| 7554 | |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 7555 | kfree(nn->sessionid_hashtbl); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 7556 | kfree(nn->unconf_id_hashtbl); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7557 | kfree(nn->conf_id_hashtbl); |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 7558 | put_net(net); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7559 | } |
| 7560 | |
Stanislav Kinsbursky | f252bc6 | 2012-11-26 15:22:18 +0300 | [diff] [blame] | 7561 | int |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 7562 | nfs4_state_start_net(struct net *net) |
NeilBrown | ac4d8ff2 | 2005-06-23 22:03:30 -0700 | [diff] [blame] | 7563 | { |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 7564 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 7565 | int ret; |
| 7566 | |
Trond Myklebust | c6c7f2a | 2021-03-13 16:08:47 -0500 | [diff] [blame] | 7567 | ret = nfs4_state_create_net(net); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7568 | if (ret) |
| 7569 | return ret; |
Jeff Layton | d4318ac | 2014-09-12 16:40:21 -0400 | [diff] [blame] | 7570 | locks_start_grace(net, &nn->nfsd4_manager); |
| 7571 | nfsd4_client_tracking_init(net); |
Scott Mayhew | 362063a | 2019-03-26 18:06:28 -0400 | [diff] [blame] | 7572 | if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0) |
| 7573 | goto skip_grace; |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 7574 | printk(KERN_INFO "NFSD: starting %lld-second grace period (net %x)\n", |
Vasily Averin | 7e981a8 | 2017-11-06 16:46:04 +0300 | [diff] [blame] | 7575 | nn->nfsd4_grace, net->ns.inum); |
Chuck Lever | dd5e3fb | 2020-04-05 11:42:29 -0400 | [diff] [blame] | 7576 | trace_nfsd_grace_start(nn); |
Stanislav Kinsbursky | 5284b44 | 2012-11-27 14:11:49 +0300 | [diff] [blame] | 7577 | queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 7578 | return 0; |
Scott Mayhew | 362063a | 2019-03-26 18:06:28 -0400 | [diff] [blame] | 7579 | |
| 7580 | skip_grace: |
| 7581 | printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n", |
| 7582 | net->ns.inum); |
| 7583 | queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ); |
| 7584 | nfsd4_end_grace(nn); |
| 7585 | return 0; |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 7586 | } |
| 7587 | |
| 7588 | /* initialization to perform when the nfsd service is started: */ |
| 7589 | |
| 7590 | int |
| 7591 | nfs4_state_start(void) |
| 7592 | { |
| 7593 | int ret; |
| 7594 | |
Jeff Layton | 51a5456 | 2015-08-20 07:17:01 -0400 | [diff] [blame] | 7595 | laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4"); |
Jeff Layton | a6d6b78 | 2012-03-05 11:42:36 -0500 | [diff] [blame] | 7596 | if (laundry_wq == NULL) { |
| 7597 | ret = -ENOMEM; |
Chuck Lever | a26dd64 | 2018-08-16 12:06:09 -0400 | [diff] [blame] | 7598 | goto out; |
Jeff Layton | a6d6b78 | 2012-03-05 11:42:36 -0500 | [diff] [blame] | 7599 | } |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 7600 | ret = nfsd4_create_callback_queue(); |
| 7601 | if (ret) |
| 7602 | goto out_free_laundry; |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 7603 | |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 7604 | set_max_delegations(); |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 7605 | return 0; |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 7606 | |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 7607 | out_free_laundry: |
| 7608 | destroy_workqueue(laundry_wq); |
Chuck Lever | a26dd64 | 2018-08-16 12:06:09 -0400 | [diff] [blame] | 7609 | out: |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 7610 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7611 | } |
| 7612 | |
Stanislav Kinsbursky | f252bc6 | 2012-11-26 15:22:18 +0300 | [diff] [blame] | 7613 | void |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 7614 | nfs4_state_shutdown_net(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7615 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7616 | struct nfs4_delegation *dp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7617 | struct list_head *pos, *next, reaplist; |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 7618 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7619 | |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 7620 | cancel_delayed_work_sync(&nn->laundromat_work); |
| 7621 | locks_end_grace(&nn->nfsd4_manager); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 7622 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7623 | INIT_LIST_HEAD(&reaplist); |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 7624 | spin_lock(&state_lock); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 7625 | list_for_each_safe(pos, next, &nn->del_recall_lru) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7626 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 7627 | WARN_ON(!unhash_delegation_locked(dp)); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 7628 | list_add(&dp->dl_recall_lru, &reaplist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7629 | } |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 7630 | spin_unlock(&state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7631 | list_for_each_safe(pos, next, &reaplist) { |
| 7632 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 7633 | list_del_init(&dp->dl_recall_lru); |
J. Bruce Fields | 0af6e69 | 2018-02-21 15:11:03 -0500 | [diff] [blame] | 7634 | destroy_unhashed_deleg(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7635 | } |
| 7636 | |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 7637 | nfsd4_client_tracking_exit(net); |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 7638 | nfs4_state_destroy_net(net); |
Dai Ngo | f4e44b3 | 2021-05-21 15:09:37 -0400 | [diff] [blame] | 7639 | #ifdef CONFIG_NFSD_V4_2_INTER_SSC |
| 7640 | nfsd4_ssc_shutdown_umount(nn); |
| 7641 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7642 | } |
| 7643 | |
| 7644 | void |
| 7645 | nfs4_state_shutdown(void) |
| 7646 | { |
NeilBrown | 5e8d5c2 | 2006-04-10 22:55:37 -0700 | [diff] [blame] | 7647 | destroy_workqueue(laundry_wq); |
J. Bruce Fields | c3935e3 | 2010-06-04 16:42:08 -0400 | [diff] [blame] | 7648 | nfsd4_destroy_callback_queue(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7649 | } |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7650 | |
| 7651 | static void |
| 7652 | get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) |
| 7653 | { |
Olga Kornievskaia | 51100d2 | 2018-09-13 13:58:24 -0400 | [diff] [blame] | 7654 | if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG) && |
| 7655 | CURRENT_STATEID(stateid)) |
Tigran Mkrtchyan | 37c593c | 2012-02-13 22:55:32 +0100 | [diff] [blame] | 7656 | memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t)); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7657 | } |
| 7658 | |
| 7659 | static void |
| 7660 | put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) |
| 7661 | { |
Tigran Mkrtchyan | 37c593c | 2012-02-13 22:55:32 +0100 | [diff] [blame] | 7662 | if (cstate->minorversion) { |
| 7663 | memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t)); |
Olga Kornievskaia | 51100d2 | 2018-09-13 13:58:24 -0400 | [diff] [blame] | 7664 | SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG); |
Tigran Mkrtchyan | 37c593c | 2012-02-13 22:55:32 +0100 | [diff] [blame] | 7665 | } |
| 7666 | } |
| 7667 | |
| 7668 | void |
| 7669 | clear_current_stateid(struct nfsd4_compound_state *cstate) |
| 7670 | { |
Olga Kornievskaia | 51100d2 | 2018-09-13 13:58:24 -0400 | [diff] [blame] | 7671 | CLEAR_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7672 | } |
| 7673 | |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 7674 | /* |
| 7675 | * functions to set current state id |
| 7676 | */ |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7677 | void |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7678 | nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, |
| 7679 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 7680 | { |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7681 | put_stateid(cstate, &u->open_downgrade.od_stateid); |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 7682 | } |
| 7683 | |
| 7684 | void |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7685 | nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, |
| 7686 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7687 | { |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7688 | put_stateid(cstate, &u->open.op_stateid); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7689 | } |
| 7690 | |
| 7691 | void |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7692 | nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, |
| 7693 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 7694 | { |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7695 | put_stateid(cstate, &u->close.cl_stateid); |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 7696 | } |
| 7697 | |
| 7698 | void |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7699 | nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, |
| 7700 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 7701 | { |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7702 | put_stateid(cstate, &u->lock.lk_resp_stateid); |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 7703 | } |
| 7704 | |
| 7705 | /* |
| 7706 | * functions to consume current state id |
| 7707 | */ |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 7708 | |
| 7709 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7710 | nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, |
| 7711 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 7712 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7713 | get_stateid(cstate, &u->open_downgrade.od_stateid); |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 7714 | } |
| 7715 | |
| 7716 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7717 | nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, |
| 7718 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 7719 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7720 | get_stateid(cstate, &u->delegreturn.dr_stateid); |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 7721 | } |
| 7722 | |
| 7723 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7724 | nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, |
| 7725 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 7726 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7727 | get_stateid(cstate, &u->free_stateid.fr_stateid); |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 7728 | } |
| 7729 | |
| 7730 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7731 | nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, |
| 7732 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 7733 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7734 | get_stateid(cstate, &u->setattr.sa_stateid); |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 7735 | } |
| 7736 | |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 7737 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7738 | nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, |
| 7739 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7740 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7741 | get_stateid(cstate, &u->close.cl_stateid); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7742 | } |
| 7743 | |
| 7744 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7745 | nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, |
| 7746 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7747 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7748 | get_stateid(cstate, &u->locku.lu_stateid); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7749 | } |
Tigran Mkrtchyan | 30813e2 | 2012-02-13 22:55:26 +0100 | [diff] [blame] | 7750 | |
| 7751 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7752 | nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, |
| 7753 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 30813e2 | 2012-02-13 22:55:26 +0100 | [diff] [blame] | 7754 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7755 | get_stateid(cstate, &u->read.rd_stateid); |
Tigran Mkrtchyan | 30813e2 | 2012-02-13 22:55:26 +0100 | [diff] [blame] | 7756 | } |
| 7757 | |
| 7758 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7759 | nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, |
| 7760 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 30813e2 | 2012-02-13 22:55:26 +0100 | [diff] [blame] | 7761 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7762 | get_stateid(cstate, &u->write.wr_stateid); |
Tigran Mkrtchyan | 30813e2 | 2012-02-13 22:55:26 +0100 | [diff] [blame] | 7763 | } |