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> |
Boaz Harrosh | 9a74af2 | 2009-12-03 20:30:56 +0200 | [diff] [blame] | 47 | #include "xdr4.h" |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 48 | #include "xdr4cb.h" |
J. Bruce Fields | 0a3adad | 2009-11-04 18:12:35 -0500 | [diff] [blame] | 49 | #include "vfs.h" |
J. Bruce Fields | bfa4b36 | 2012-04-25 16:49:18 -0400 | [diff] [blame] | 50 | #include "current_stateid.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 52 | #include "netns.h" |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 53 | #include "pnfs.h" |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 54 | #include "filecache.h" |
Chuck Lever | dd5e3fb | 2020-04-05 11:42:29 -0400 | [diff] [blame] | 55 | #include "trace.h" |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 56 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | #define NFSDDBG_FACILITY NFSDDBG_PROC |
| 58 | |
J. Bruce Fields | f32f3c2 | 2011-12-12 15:00:35 -0500 | [diff] [blame] | 59 | #define all_ones {{~0,~0},~0} |
| 60 | static const stateid_t one_stateid = { |
| 61 | .si_generation = ~0, |
| 62 | .si_opaque = all_ones, |
| 63 | }; |
| 64 | static const stateid_t zero_stateid = { |
| 65 | /* all fields zero */ |
| 66 | }; |
Tigran Mkrtchyan | 19ff0f2 | 2012-02-13 22:55:23 +0100 | [diff] [blame] | 67 | static const stateid_t currentstateid = { |
| 68 | .si_generation = 1, |
| 69 | }; |
Trond Myklebust | fb500a7 | 2017-11-03 08:00:12 -0400 | [diff] [blame] | 70 | static const stateid_t close_stateid = { |
| 71 | .si_generation = 0xffffffffU, |
| 72 | }; |
J. Bruce Fields | f32f3c2 | 2011-12-12 15:00:35 -0500 | [diff] [blame] | 73 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 74 | static u64 current_sessionid = 1; |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 75 | |
J. Bruce Fields | f32f3c2 | 2011-12-12 15:00:35 -0500 | [diff] [blame] | 76 | #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t))) |
| 77 | #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t))) |
Tigran Mkrtchyan | 19ff0f2 | 2012-02-13 22:55:23 +0100 | [diff] [blame] | 78 | #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t))) |
Andrew Elble | ae254dac | 2017-11-09 13:41:10 -0500 | [diff] [blame] | 79 | #define CLOSE_STATEID(stateid) (!memcmp((stateid), &close_stateid, sizeof(stateid_t))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | /* forward declarations */ |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 82 | 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] | 83 | static void nfs4_free_ol_stateid(struct nfs4_stid *stid); |
Scott Mayhew | 362063a | 2019-03-26 18:06:28 -0400 | [diff] [blame] | 84 | void nfsd4_end_grace(struct nfsd_net *nn); |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 85 | 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] | 86 | |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 87 | /* Locking: */ |
| 88 | |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 89 | /* |
| 90 | * Currently used for the del_recall_lru and file hash table. In an |
| 91 | * effort to decrease the scope of the client_mutex, this spinlock may |
| 92 | * eventually cover more: |
| 93 | */ |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 94 | static DEFINE_SPINLOCK(state_lock); |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 95 | |
Andrew Elble | 4f34bd0 | 2017-11-08 17:29:51 -0500 | [diff] [blame] | 96 | enum nfsd4_st_mutex_lock_subclass { |
| 97 | OPEN_STATEID_MUTEX = 0, |
| 98 | LOCK_STATEID_MUTEX = 1, |
| 99 | }; |
| 100 | |
Jeff Layton | b401be22 | 2014-07-29 21:34:33 -0400 | [diff] [blame] | 101 | /* |
| 102 | * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for |
| 103 | * the refcount on the open stateid to drop. |
| 104 | */ |
| 105 | static DECLARE_WAIT_QUEUE_HEAD(close_wq); |
| 106 | |
J. Bruce Fields | 89c905b | 2019-06-19 12:43:11 -0400 | [diff] [blame] | 107 | /* |
| 108 | * A waitqueue where a writer to clients/#/ctl destroying a client can |
| 109 | * wait for cl_rpc_users to drop to 0 and then for the client to be |
| 110 | * unhashed. |
| 111 | */ |
| 112 | static DECLARE_WAIT_QUEUE_HEAD(expiry_wq); |
| 113 | |
Jeff Layton | 9258a2d | 2018-03-16 09:47:22 -0400 | [diff] [blame] | 114 | static struct kmem_cache *client_slab; |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 115 | static struct kmem_cache *openowner_slab; |
| 116 | static struct kmem_cache *lockowner_slab; |
| 117 | static struct kmem_cache *file_slab; |
| 118 | static struct kmem_cache *stateid_slab; |
| 119 | static struct kmem_cache *deleg_slab; |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 120 | static struct kmem_cache *odstate_slab; |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 121 | |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 122 | static void free_session(struct nfsd4_session *); |
Benny Halevy | 508dc6e | 2012-02-23 17:40:52 -0800 | [diff] [blame] | 123 | |
Julia Lawall | c4cb897 | 2015-11-21 22:57:39 +0100 | [diff] [blame] | 124 | static const struct nfsd4_callback_ops nfsd4_cb_recall_ops; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 125 | static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops; |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 126 | |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 127 | static bool is_session_dead(struct nfsd4_session *ses) |
Benny Halevy | 508dc6e | 2012-02-23 17:40:52 -0800 | [diff] [blame] | 128 | { |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 129 | return ses->se_flags & NFS4_SESSION_DEAD; |
| 130 | } |
| 131 | |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 132 | static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me) |
| 133 | { |
| 134 | if (atomic_read(&ses->se_ref) > ref_held_by_me) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 135 | return nfserr_jukebox; |
| 136 | ses->se_flags |= NFS4_SESSION_DEAD; |
| 137 | return nfs_ok; |
| 138 | } |
| 139 | |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 140 | static bool is_client_expired(struct nfs4_client *clp) |
| 141 | { |
| 142 | return clp->cl_time == 0; |
| 143 | } |
| 144 | |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 145 | static __be32 get_client_locked(struct nfs4_client *clp) |
| 146 | { |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 147 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 148 | |
| 149 | lockdep_assert_held(&nn->client_lock); |
| 150 | |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 151 | if (is_client_expired(clp)) |
| 152 | return nfserr_expired; |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 153 | atomic_inc(&clp->cl_rpc_users); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 154 | return nfs_ok; |
| 155 | } |
| 156 | |
| 157 | /* must be called under the client_lock */ |
| 158 | static inline void |
| 159 | renew_client_locked(struct nfs4_client *clp) |
| 160 | { |
| 161 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 162 | |
| 163 | if (is_client_expired(clp)) { |
| 164 | WARN_ON(1); |
| 165 | printk("%s: client (clientid %08x/%08x) already expired\n", |
| 166 | __func__, |
| 167 | clp->cl_clientid.cl_boot, |
| 168 | clp->cl_clientid.cl_id); |
| 169 | return; |
| 170 | } |
| 171 | |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 172 | list_move_tail(&clp->cl_lru, &nn->client_lru); |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 173 | clp->cl_time = ktime_get_boottime_seconds(); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 174 | } |
| 175 | |
Fengguang Wu | ba13843 | 2013-04-16 22:14:15 -0400 | [diff] [blame] | 176 | static void put_client_renew_locked(struct nfs4_client *clp) |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 177 | { |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 178 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 179 | |
| 180 | lockdep_assert_held(&nn->client_lock); |
| 181 | |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 182 | if (!atomic_dec_and_test(&clp->cl_rpc_users)) |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 183 | return; |
| 184 | if (!is_client_expired(clp)) |
| 185 | renew_client_locked(clp); |
J. Bruce Fields | 89c905b | 2019-06-19 12:43:11 -0400 | [diff] [blame] | 186 | else |
| 187 | wake_up_all(&expiry_wq); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 188 | } |
| 189 | |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 190 | static void put_client_renew(struct nfs4_client *clp) |
| 191 | { |
| 192 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 193 | |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 194 | if (!atomic_dec_and_lock(&clp->cl_rpc_users, &nn->client_lock)) |
Jeff Layton | d6c249b | 2014-07-08 14:02:50 -0400 | [diff] [blame] | 195 | return; |
| 196 | if (!is_client_expired(clp)) |
| 197 | renew_client_locked(clp); |
J. Bruce Fields | 89c905b | 2019-06-19 12:43:11 -0400 | [diff] [blame] | 198 | else |
| 199 | wake_up_all(&expiry_wq); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 200 | spin_unlock(&nn->client_lock); |
| 201 | } |
| 202 | |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 203 | static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses) |
| 204 | { |
| 205 | __be32 status; |
| 206 | |
| 207 | if (is_session_dead(ses)) |
| 208 | return nfserr_badsession; |
| 209 | status = get_client_locked(ses->se_client); |
| 210 | if (status) |
| 211 | return status; |
| 212 | atomic_inc(&ses->se_ref); |
| 213 | return nfs_ok; |
| 214 | } |
| 215 | |
| 216 | static void nfsd4_put_session_locked(struct nfsd4_session *ses) |
| 217 | { |
| 218 | struct nfs4_client *clp = ses->se_client; |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 219 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 220 | |
| 221 | lockdep_assert_held(&nn->client_lock); |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 222 | |
| 223 | if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses)) |
| 224 | free_session(ses); |
| 225 | put_client_renew_locked(clp); |
| 226 | } |
| 227 | |
| 228 | static void nfsd4_put_session(struct nfsd4_session *ses) |
| 229 | { |
| 230 | struct nfs4_client *clp = ses->se_client; |
| 231 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 232 | |
| 233 | spin_lock(&nn->client_lock); |
| 234 | nfsd4_put_session_locked(ses); |
| 235 | spin_unlock(&nn->client_lock); |
| 236 | } |
| 237 | |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 238 | static struct nfsd4_blocked_lock * |
| 239 | find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh, |
| 240 | struct nfsd_net *nn) |
| 241 | { |
| 242 | struct nfsd4_blocked_lock *cur, *found = NULL; |
| 243 | |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 244 | spin_lock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 245 | list_for_each_entry(cur, &lo->lo_blocked, nbl_list) { |
| 246 | if (fh_match(fh, &cur->nbl_fh)) { |
| 247 | list_del_init(&cur->nbl_list); |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 248 | list_del_init(&cur->nbl_lru); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 249 | found = cur; |
| 250 | break; |
| 251 | } |
| 252 | } |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 253 | spin_unlock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 254 | if (found) |
NeilBrown | cb03f94 | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 255 | locks_delete_block(&found->nbl_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 256 | return found; |
| 257 | } |
| 258 | |
| 259 | static struct nfsd4_blocked_lock * |
| 260 | find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh, |
| 261 | struct nfsd_net *nn) |
| 262 | { |
| 263 | struct nfsd4_blocked_lock *nbl; |
| 264 | |
| 265 | nbl = find_blocked_lock(lo, fh, nn); |
| 266 | if (!nbl) { |
| 267 | nbl= kmalloc(sizeof(*nbl), GFP_KERNEL); |
| 268 | if (nbl) { |
Vasily Averin | e1e8399 | 2020-03-27 07:50:40 +0300 | [diff] [blame] | 269 | INIT_LIST_HEAD(&nbl->nbl_list); |
| 270 | INIT_LIST_HEAD(&nbl->nbl_lru); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 271 | fh_copy_shallow(&nbl->nbl_fh, fh); |
| 272 | locks_init_lock(&nbl->nbl_lock); |
| 273 | nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client, |
| 274 | &nfsd4_cb_notify_lock_ops, |
| 275 | NFSPROC4_CLNT_CB_NOTIFY_LOCK); |
| 276 | } |
| 277 | } |
| 278 | return nbl; |
| 279 | } |
| 280 | |
| 281 | static void |
| 282 | free_blocked_lock(struct nfsd4_blocked_lock *nbl) |
| 283 | { |
Jeff Layton | 6aaafc4 | 2019-04-22 12:34:23 -0400 | [diff] [blame] | 284 | locks_delete_block(&nbl->nbl_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 285 | locks_release_private(&nbl->nbl_lock); |
| 286 | kfree(nbl); |
| 287 | } |
| 288 | |
Jeff Layton | 68ef3bc | 2018-03-16 11:32:02 -0400 | [diff] [blame] | 289 | static void |
| 290 | remove_blocked_locks(struct nfs4_lockowner *lo) |
| 291 | { |
| 292 | struct nfs4_client *clp = lo->lo_owner.so_client; |
| 293 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 294 | struct nfsd4_blocked_lock *nbl; |
| 295 | LIST_HEAD(reaplist); |
| 296 | |
| 297 | /* Dequeue all blocked locks */ |
| 298 | spin_lock(&nn->blocked_locks_lock); |
| 299 | while (!list_empty(&lo->lo_blocked)) { |
| 300 | nbl = list_first_entry(&lo->lo_blocked, |
| 301 | struct nfsd4_blocked_lock, |
| 302 | nbl_list); |
| 303 | list_del_init(&nbl->nbl_list); |
| 304 | list_move(&nbl->nbl_lru, &reaplist); |
| 305 | } |
| 306 | spin_unlock(&nn->blocked_locks_lock); |
| 307 | |
| 308 | /* Now free them */ |
| 309 | while (!list_empty(&reaplist)) { |
| 310 | nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock, |
| 311 | nbl_lru); |
| 312 | list_del_init(&nbl->nbl_lru); |
Jeff Layton | 68ef3bc | 2018-03-16 11:32:02 -0400 | [diff] [blame] | 313 | free_blocked_lock(nbl); |
| 314 | } |
| 315 | } |
| 316 | |
Jeff Layton | f456458 | 2019-04-22 12:34:24 -0400 | [diff] [blame] | 317 | static void |
| 318 | nfsd4_cb_notify_lock_prepare(struct nfsd4_callback *cb) |
| 319 | { |
| 320 | struct nfsd4_blocked_lock *nbl = container_of(cb, |
| 321 | struct nfsd4_blocked_lock, nbl_cb); |
| 322 | locks_delete_block(&nbl->nbl_lock); |
| 323 | } |
| 324 | |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 325 | static int |
| 326 | nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task) |
| 327 | { |
| 328 | /* |
| 329 | * Since this is just an optimization, we don't try very hard if it |
| 330 | * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and |
| 331 | * just quit trying on anything else. |
| 332 | */ |
| 333 | switch (task->tk_status) { |
| 334 | case -NFS4ERR_DELAY: |
| 335 | rpc_delay(task, 1 * HZ); |
| 336 | return 0; |
| 337 | default: |
| 338 | return 1; |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | static void |
| 343 | nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb) |
| 344 | { |
| 345 | struct nfsd4_blocked_lock *nbl = container_of(cb, |
| 346 | struct nfsd4_blocked_lock, nbl_cb); |
| 347 | |
| 348 | free_blocked_lock(nbl); |
| 349 | } |
| 350 | |
| 351 | static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = { |
Jeff Layton | f456458 | 2019-04-22 12:34:24 -0400 | [diff] [blame] | 352 | .prepare = nfsd4_cb_notify_lock_prepare, |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 353 | .done = nfsd4_cb_notify_lock_done, |
| 354 | .release = nfsd4_cb_notify_lock_release, |
| 355 | }; |
| 356 | |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 357 | static inline struct nfs4_stateowner * |
| 358 | nfs4_get_stateowner(struct nfs4_stateowner *sop) |
| 359 | { |
| 360 | atomic_inc(&sop->so_count); |
| 361 | return sop; |
| 362 | } |
| 363 | |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 364 | static int |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 365 | same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner) |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 366 | { |
| 367 | return (sop->so_owner.len == owner->len) && |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 368 | 0 == memcmp(sop->so_owner.data, owner->data, owner->len); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | static struct nfs4_openowner * |
| 372 | find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open, |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 373 | struct nfs4_client *clp) |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 374 | { |
| 375 | struct nfs4_stateowner *so; |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 376 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 377 | lockdep_assert_held(&clp->cl_lock); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 378 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 379 | list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval], |
| 380 | so_strhash) { |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 381 | if (!so->so_is_open_owner) |
| 382 | continue; |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 383 | if (same_owner_str(so, &open->op_owner)) |
| 384 | return openowner(nfs4_get_stateowner(so)); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 385 | } |
| 386 | return NULL; |
| 387 | } |
| 388 | |
| 389 | static struct nfs4_openowner * |
| 390 | find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open, |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 391 | struct nfs4_client *clp) |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 392 | { |
| 393 | struct nfs4_openowner *oo; |
| 394 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 395 | spin_lock(&clp->cl_lock); |
| 396 | oo = find_openstateowner_str_locked(hashval, open, clp); |
| 397 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 398 | return oo; |
| 399 | } |
| 400 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | static inline u32 |
| 402 | opaque_hashval(const void *ptr, int nbytes) |
| 403 | { |
| 404 | unsigned char *cptr = (unsigned char *) ptr; |
| 405 | |
| 406 | u32 x = 0; |
| 407 | while (nbytes--) { |
| 408 | x *= 37; |
| 409 | x += *cptr++; |
| 410 | } |
| 411 | return x; |
| 412 | } |
| 413 | |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 414 | static void nfsd4_free_file_rcu(struct rcu_head *rcu) |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 415 | { |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 416 | struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu); |
| 417 | |
| 418 | kmem_cache_free(file_slab, fp); |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 419 | } |
| 420 | |
Christoph Hellwig | e6ba76e | 2014-08-14 08:50:16 +0200 | [diff] [blame] | 421 | void |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 422 | put_nfs4_file(struct nfs4_file *fi) |
| 423 | { |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 424 | might_lock(&state_lock); |
| 425 | |
Elena Reshetova | 818a34e | 2017-10-20 12:53:30 +0300 | [diff] [blame] | 426 | if (refcount_dec_and_lock(&fi->fi_ref, &state_lock)) { |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 427 | hlist_del_rcu(&fi->fi_hash); |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 428 | spin_unlock(&state_lock); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 429 | WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate)); |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 430 | WARN_ON_ONCE(!list_empty(&fi->fi_delegations)); |
| 431 | call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu); |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 432 | } |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 433 | } |
| 434 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 435 | static struct nfsd_file * |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 436 | __nfs4_get_fd(struct nfs4_file *f, int oflag) |
| 437 | { |
| 438 | if (f->fi_fds[oflag]) |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 439 | return nfsd_file_get(f->fi_fds[oflag]); |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 440 | return NULL; |
| 441 | } |
| 442 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 443 | static struct nfsd_file * |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 444 | find_writeable_file_locked(struct nfs4_file *f) |
| 445 | { |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 446 | struct nfsd_file *ret; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 447 | |
| 448 | lockdep_assert_held(&f->fi_lock); |
| 449 | |
| 450 | ret = __nfs4_get_fd(f, O_WRONLY); |
| 451 | if (!ret) |
| 452 | ret = __nfs4_get_fd(f, O_RDWR); |
| 453 | return ret; |
| 454 | } |
| 455 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 456 | static struct nfsd_file * |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 457 | find_writeable_file(struct nfs4_file *f) |
| 458 | { |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 459 | struct nfsd_file *ret; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 460 | |
| 461 | spin_lock(&f->fi_lock); |
| 462 | ret = find_writeable_file_locked(f); |
| 463 | spin_unlock(&f->fi_lock); |
| 464 | |
| 465 | return ret; |
| 466 | } |
| 467 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 468 | static struct nfsd_file * |
| 469 | find_readable_file_locked(struct nfs4_file *f) |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 470 | { |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 471 | struct nfsd_file *ret; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 472 | |
| 473 | lockdep_assert_held(&f->fi_lock); |
| 474 | |
| 475 | ret = __nfs4_get_fd(f, O_RDONLY); |
| 476 | if (!ret) |
| 477 | ret = __nfs4_get_fd(f, O_RDWR); |
| 478 | return ret; |
| 479 | } |
| 480 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 481 | static struct nfsd_file * |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 482 | find_readable_file(struct nfs4_file *f) |
| 483 | { |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 484 | struct nfsd_file *ret; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 485 | |
| 486 | spin_lock(&f->fi_lock); |
| 487 | ret = find_readable_file_locked(f); |
| 488 | spin_unlock(&f->fi_lock); |
| 489 | |
| 490 | return ret; |
| 491 | } |
| 492 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 493 | struct nfsd_file * |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 494 | find_any_file(struct nfs4_file *f) |
| 495 | { |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 496 | struct nfsd_file *ret; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 497 | |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 498 | if (!f) |
| 499 | return NULL; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 500 | spin_lock(&f->fi_lock); |
| 501 | ret = __nfs4_get_fd(f, O_RDWR); |
| 502 | if (!ret) { |
| 503 | ret = __nfs4_get_fd(f, O_WRONLY); |
| 504 | if (!ret) |
| 505 | ret = __nfs4_get_fd(f, O_RDONLY); |
| 506 | } |
| 507 | spin_unlock(&f->fi_lock); |
| 508 | return ret; |
| 509 | } |
| 510 | |
J. Bruce Fields | 9affa43 | 2020-07-15 13:31:36 -0400 | [diff] [blame] | 511 | static struct nfsd_file *find_deleg_file(struct nfs4_file *f) |
| 512 | { |
| 513 | struct nfsd_file *ret = NULL; |
| 514 | |
| 515 | spin_lock(&f->fi_lock); |
| 516 | if (f->fi_deleg_file) |
| 517 | ret = nfsd_file_get(f->fi_deleg_file); |
| 518 | spin_unlock(&f->fi_lock); |
| 519 | return ret; |
| 520 | } |
| 521 | |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 522 | static atomic_long_t num_delegations; |
Zhang Yanfei | 697ce9b | 2013-02-22 16:35:47 -0800 | [diff] [blame] | 523 | unsigned long max_delegations; |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 524 | |
| 525 | /* |
| 526 | * Open owner state (share locks) |
| 527 | */ |
| 528 | |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 529 | /* hash tables for lock and open owners */ |
| 530 | #define OWNER_HASH_BITS 8 |
| 531 | #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS) |
| 532 | #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1) |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 533 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 534 | static unsigned int ownerstr_hashval(struct xdr_netobj *ownername) |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 535 | { |
| 536 | unsigned int ret; |
| 537 | |
| 538 | ret = opaque_hashval(ownername->data, ownername->len); |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 539 | return ret & OWNER_HASH_MASK; |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 540 | } |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 541 | |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 542 | /* hash table for nfs4_file */ |
| 543 | #define FILE_HASH_BITS 8 |
| 544 | #define FILE_HASH_SIZE (1 << FILE_HASH_BITS) |
Shan Wei | 3507958 | 2011-01-14 17:35:59 +0800 | [diff] [blame] | 545 | |
J. Bruce Fields | f9b60e2 | 2021-04-16 14:00:15 -0400 | [diff] [blame] | 546 | static unsigned int file_hashval(struct svc_fh *fh) |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 547 | { |
J. Bruce Fields | f9b60e2 | 2021-04-16 14:00:15 -0400 | [diff] [blame] | 548 | struct inode *inode = d_inode(fh->fh_dentry); |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 549 | |
J. Bruce Fields | f9b60e2 | 2021-04-16 14:00:15 -0400 | [diff] [blame] | 550 | /* XXX: why not (here & in file cache) use inode? */ |
| 551 | return (unsigned int)hash_long(inode->i_ino, FILE_HASH_BITS); |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 552 | } |
| 553 | |
Jeff Layton | 89876f8 | 2013-04-02 09:01:59 -0400 | [diff] [blame] | 554 | static struct hlist_head file_hashtbl[FILE_HASH_SIZE]; |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 555 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 556 | static void |
| 557 | __nfs4_file_get_access(struct nfs4_file *fp, u32 access) |
J. Bruce Fields | 3477565 | 2013-08-23 17:55:18 -0400 | [diff] [blame] | 558 | { |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 559 | lockdep_assert_held(&fp->fi_lock); |
| 560 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 561 | if (access & NFS4_SHARE_ACCESS_WRITE) |
| 562 | atomic_inc(&fp->fi_access[O_WRONLY]); |
| 563 | if (access & NFS4_SHARE_ACCESS_READ) |
| 564 | atomic_inc(&fp->fi_access[O_RDONLY]); |
J. Bruce Fields | 3477565 | 2013-08-23 17:55:18 -0400 | [diff] [blame] | 565 | } |
| 566 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 567 | static __be32 |
| 568 | nfs4_file_get_access(struct nfs4_file *fp, u32 access) |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 569 | { |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 570 | lockdep_assert_held(&fp->fi_lock); |
| 571 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 572 | /* Does this access mode make sense? */ |
| 573 | if (access & ~NFS4_SHARE_ACCESS_BOTH) |
| 574 | return nfserr_inval; |
| 575 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 576 | /* Does it conflict with a deny mode already set? */ |
| 577 | if ((access & fp->fi_share_deny) != 0) |
| 578 | return nfserr_share_denied; |
| 579 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 580 | __nfs4_file_get_access(fp, access); |
| 581 | return nfs_ok; |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 582 | } |
| 583 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 584 | static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny) |
| 585 | { |
| 586 | /* Common case is that there is no deny mode. */ |
| 587 | if (deny) { |
| 588 | /* Does this deny mode make sense? */ |
| 589 | if (deny & ~NFS4_SHARE_DENY_BOTH) |
| 590 | return nfserr_inval; |
| 591 | |
| 592 | if ((deny & NFS4_SHARE_DENY_READ) && |
| 593 | atomic_read(&fp->fi_access[O_RDONLY])) |
| 594 | return nfserr_share_denied; |
| 595 | |
| 596 | if ((deny & NFS4_SHARE_DENY_WRITE) && |
| 597 | atomic_read(&fp->fi_access[O_WRONLY])) |
| 598 | return nfserr_share_denied; |
| 599 | } |
| 600 | return nfs_ok; |
| 601 | } |
| 602 | |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 603 | 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] | 604 | { |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 605 | might_lock(&fp->fi_lock); |
| 606 | |
| 607 | if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) { |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 608 | struct nfsd_file *f1 = NULL; |
| 609 | struct nfsd_file *f2 = NULL; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 610 | |
Jeff Layton | 6d338b5 | 2014-07-10 14:07:29 -0400 | [diff] [blame] | 611 | swap(f1, fp->fi_fds[oflag]); |
J. Bruce Fields | 0c7c3e6 | 2013-03-28 20:37:14 -0400 | [diff] [blame] | 612 | if (atomic_read(&fp->fi_access[1 - oflag]) == 0) |
Jeff Layton | 6d338b5 | 2014-07-10 14:07:29 -0400 | [diff] [blame] | 613 | swap(f2, fp->fi_fds[O_RDWR]); |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 614 | spin_unlock(&fp->fi_lock); |
| 615 | if (f1) |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 616 | nfsd_file_put(f1); |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 617 | if (f2) |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 618 | nfsd_file_put(f2); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 619 | } |
| 620 | } |
| 621 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 622 | 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] | 623 | { |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 624 | WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH); |
| 625 | |
| 626 | if (access & NFS4_SHARE_ACCESS_WRITE) |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 627 | __nfs4_file_put_access(fp, O_WRONLY); |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 628 | if (access & NFS4_SHARE_ACCESS_READ) |
| 629 | __nfs4_file_put_access(fp, O_RDONLY); |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 630 | } |
| 631 | |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 632 | /* |
| 633 | * Allocate a new open/delegation state counter. This is needed for |
| 634 | * pNFS for proper return on close semantics. |
| 635 | * |
| 636 | * Note that we only allocate it for pNFS-enabled exports, otherwise |
| 637 | * all pointers to struct nfs4_clnt_odstate are always NULL. |
| 638 | */ |
| 639 | static struct nfs4_clnt_odstate * |
| 640 | alloc_clnt_odstate(struct nfs4_client *clp) |
| 641 | { |
| 642 | struct nfs4_clnt_odstate *co; |
| 643 | |
| 644 | co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL); |
| 645 | if (co) { |
| 646 | co->co_client = clp; |
Elena Reshetova | cff7cb2 | 2017-10-20 12:53:29 +0300 | [diff] [blame] | 647 | refcount_set(&co->co_odcount, 1); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 648 | } |
| 649 | return co; |
| 650 | } |
| 651 | |
| 652 | static void |
| 653 | hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co) |
| 654 | { |
| 655 | struct nfs4_file *fp = co->co_file; |
| 656 | |
| 657 | lockdep_assert_held(&fp->fi_lock); |
| 658 | list_add(&co->co_perfile, &fp->fi_clnt_odstate); |
| 659 | } |
| 660 | |
| 661 | static inline void |
| 662 | get_clnt_odstate(struct nfs4_clnt_odstate *co) |
| 663 | { |
| 664 | if (co) |
Elena Reshetova | cff7cb2 | 2017-10-20 12:53:29 +0300 | [diff] [blame] | 665 | refcount_inc(&co->co_odcount); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 666 | } |
| 667 | |
| 668 | static void |
| 669 | put_clnt_odstate(struct nfs4_clnt_odstate *co) |
| 670 | { |
| 671 | struct nfs4_file *fp; |
| 672 | |
| 673 | if (!co) |
| 674 | return; |
| 675 | |
| 676 | fp = co->co_file; |
Elena Reshetova | cff7cb2 | 2017-10-20 12:53:29 +0300 | [diff] [blame] | 677 | if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) { |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 678 | list_del(&co->co_perfile); |
| 679 | spin_unlock(&fp->fi_lock); |
| 680 | |
| 681 | nfsd4_return_all_file_layouts(co->co_client, fp); |
| 682 | kmem_cache_free(odstate_slab, co); |
| 683 | } |
| 684 | } |
| 685 | |
| 686 | static struct nfs4_clnt_odstate * |
| 687 | find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new) |
| 688 | { |
| 689 | struct nfs4_clnt_odstate *co; |
| 690 | struct nfs4_client *cl; |
| 691 | |
| 692 | if (!new) |
| 693 | return NULL; |
| 694 | |
| 695 | cl = new->co_client; |
| 696 | |
| 697 | spin_lock(&fp->fi_lock); |
| 698 | list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) { |
| 699 | if (co->co_client == cl) { |
| 700 | get_clnt_odstate(co); |
| 701 | goto out; |
| 702 | } |
| 703 | } |
| 704 | co = new; |
| 705 | co->co_file = fp; |
| 706 | hash_clnt_odstate_locked(new); |
| 707 | out: |
| 708 | spin_unlock(&fp->fi_lock); |
| 709 | return co; |
| 710 | } |
| 711 | |
Kinglong Mee | d19fb70 | 2017-01-18 19:04:42 +0800 | [diff] [blame] | 712 | struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab, |
| 713 | void (*sc_free)(struct nfs4_stid *)) |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 714 | { |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 715 | struct nfs4_stid *stid; |
| 716 | int new_id; |
| 717 | |
Trond Myklebust | f833883 | 2014-07-25 07:34:19 -0400 | [diff] [blame] | 718 | stid = kmem_cache_zalloc(slab, GFP_KERNEL); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 719 | if (!stid) |
| 720 | return NULL; |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 721 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 722 | idr_preload(GFP_KERNEL); |
| 723 | spin_lock(&cl->cl_lock); |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 724 | /* Reserving 0 for start of file in nfsdfs "states" file: */ |
| 725 | 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] | 726 | spin_unlock(&cl->cl_lock); |
| 727 | idr_preload_end(); |
Tejun Heo | ebd6c70 | 2013-03-13 14:59:37 -0700 | [diff] [blame] | 728 | if (new_id < 0) |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 729 | goto out_free; |
Kinglong Mee | d19fb70 | 2017-01-18 19:04:42 +0800 | [diff] [blame] | 730 | |
| 731 | stid->sc_free = sc_free; |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 732 | stid->sc_client = cl; |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 733 | stid->sc_stateid.si_opaque.so_id = new_id; |
| 734 | stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid; |
| 735 | /* Will be incremented before return to client: */ |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 736 | refcount_set(&stid->sc_count, 1); |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 737 | spin_lock_init(&stid->sc_lock); |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 738 | INIT_LIST_HEAD(&stid->sc_cp_list); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 739 | |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 740 | /* |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 741 | * It shouldn't be a problem to reuse an opaque stateid value. |
| 742 | * I don't think it is for 4.1. But with 4.0 I worry that, for |
| 743 | * example, a stray write retransmission could be accepted by |
| 744 | * the server when it should have been rejected. Therefore, |
| 745 | * adopt a trick from the sctp code to attempt to maximize the |
| 746 | * amount of time until an id is reused, by ensuring they always |
| 747 | * "increase" (mod INT_MAX): |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 748 | */ |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 749 | return stid; |
| 750 | out_free: |
Wei Yongjun | 2c44a23 | 2013-04-09 14:15:31 +0800 | [diff] [blame] | 751 | kmem_cache_free(slab, stid); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 752 | return NULL; |
J. Bruce Fields | 2a74aba | 2011-09-23 17:20:02 -0400 | [diff] [blame] | 753 | } |
| 754 | |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 755 | /* |
| 756 | * Create a unique stateid_t to represent each COPY. |
| 757 | */ |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 758 | static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid, |
| 759 | unsigned char sc_type) |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 760 | { |
| 761 | int new_id; |
| 762 | |
Arnd Bergmann | 9cc7680 | 2019-10-31 15:53:13 +0100 | [diff] [blame] | 763 | stid->stid.si_opaque.so_clid.cl_boot = (u32)nn->boot_time; |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 764 | stid->stid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id; |
| 765 | stid->sc_type = sc_type; |
| 766 | |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 767 | idr_preload(GFP_KERNEL); |
| 768 | spin_lock(&nn->s2s_cp_lock); |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 769 | new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, stid, 0, 0, GFP_NOWAIT); |
| 770 | stid->stid.si_opaque.so_id = new_id; |
Dai Ngo | ca9364d | 2020-11-30 16:24:49 -0500 | [diff] [blame] | 771 | stid->stid.si_generation = 1; |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 772 | spin_unlock(&nn->s2s_cp_lock); |
| 773 | idr_preload_end(); |
| 774 | if (new_id < 0) |
| 775 | return 0; |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 776 | return 1; |
| 777 | } |
| 778 | |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 779 | int nfs4_init_copy_state(struct nfsd_net *nn, struct nfsd4_copy *copy) |
| 780 | { |
| 781 | return nfs4_init_cp_state(nn, ©->cp_stateid, NFS4_COPY_STID); |
| 782 | } |
| 783 | |
| 784 | struct nfs4_cpntf_state *nfs4_alloc_init_cpntf_state(struct nfsd_net *nn, |
| 785 | struct nfs4_stid *p_stid) |
| 786 | { |
| 787 | struct nfs4_cpntf_state *cps; |
| 788 | |
| 789 | cps = kzalloc(sizeof(struct nfs4_cpntf_state), GFP_KERNEL); |
| 790 | if (!cps) |
| 791 | return NULL; |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 792 | cps->cpntf_time = ktime_get_boottime_seconds(); |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 793 | refcount_set(&cps->cp_stateid.sc_count, 1); |
| 794 | if (!nfs4_init_cp_state(nn, &cps->cp_stateid, NFS4_COPYNOTIFY_STID)) |
| 795 | goto out_free; |
| 796 | spin_lock(&nn->s2s_cp_lock); |
| 797 | list_add(&cps->cp_list, &p_stid->sc_cp_list); |
| 798 | spin_unlock(&nn->s2s_cp_lock); |
| 799 | return cps; |
| 800 | out_free: |
| 801 | kfree(cps); |
| 802 | return NULL; |
| 803 | } |
| 804 | |
| 805 | void nfs4_free_copy_state(struct nfsd4_copy *copy) |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 806 | { |
| 807 | struct nfsd_net *nn; |
| 808 | |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 809 | WARN_ON_ONCE(copy->cp_stateid.sc_type != NFS4_COPY_STID); |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 810 | nn = net_generic(copy->cp_clp->net, nfsd_net_id); |
| 811 | spin_lock(&nn->s2s_cp_lock); |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 812 | idr_remove(&nn->s2s_cp_stateids, |
| 813 | copy->cp_stateid.stid.si_opaque.so_id); |
| 814 | spin_unlock(&nn->s2s_cp_lock); |
| 815 | } |
| 816 | |
| 817 | static void nfs4_free_cpntf_statelist(struct net *net, struct nfs4_stid *stid) |
| 818 | { |
| 819 | struct nfs4_cpntf_state *cps; |
| 820 | struct nfsd_net *nn; |
| 821 | |
| 822 | nn = net_generic(net, nfsd_net_id); |
| 823 | spin_lock(&nn->s2s_cp_lock); |
| 824 | while (!list_empty(&stid->sc_cp_list)) { |
| 825 | cps = list_first_entry(&stid->sc_cp_list, |
| 826 | struct nfs4_cpntf_state, cp_list); |
| 827 | _free_cpntf_state_locked(nn, cps); |
| 828 | } |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 829 | spin_unlock(&nn->s2s_cp_lock); |
| 830 | } |
| 831 | |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 832 | 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] | 833 | { |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 834 | struct nfs4_stid *stid; |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 835 | |
Kinglong Mee | d19fb70 | 2017-01-18 19:04:42 +0800 | [diff] [blame] | 836 | stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 837 | if (!stid) |
| 838 | return NULL; |
| 839 | |
Kinglong Mee | d19fb70 | 2017-01-18 19:04:42 +0800 | [diff] [blame] | 840 | return openlockstateid(stid); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 841 | } |
| 842 | |
| 843 | static void nfs4_free_deleg(struct nfs4_stid *stid) |
| 844 | { |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 845 | kmem_cache_free(deleg_slab, stid); |
| 846 | atomic_long_dec(&num_delegations); |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 847 | } |
| 848 | |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 849 | /* |
| 850 | * When we recall a delegation, we should be careful not to hand it |
| 851 | * out again straight away. |
| 852 | * To ensure this we keep a pair of bloom filters ('new' and 'old') |
| 853 | * in which the filehandles of recalled delegations are "stored". |
| 854 | * If a filehandle appear in either filter, a delegation is blocked. |
| 855 | * When a delegation is recalled, the filehandle is stored in the "new" |
| 856 | * filter. |
| 857 | * Every 30 seconds we swap the filters and clear the "new" one, |
| 858 | * unless both are empty of course. |
| 859 | * |
| 860 | * Each filter is 256 bits. We hash the filehandle to 32bit and use the |
| 861 | * low 3 bytes as hash-table indices. |
| 862 | * |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 863 | * 'blocked_delegations_lock', which is always taken in block_delegations(), |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 864 | * is used to manage concurrent access. Testing does not need the lock |
| 865 | * except when swapping the two filters. |
| 866 | */ |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 867 | static DEFINE_SPINLOCK(blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 868 | static struct bloom_pair { |
| 869 | int entries, old_entries; |
Arnd Bergmann | b3f255e | 2017-10-20 11:25:34 +0200 | [diff] [blame] | 870 | time64_t swap_time; |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 871 | int new; /* index into 'set' */ |
| 872 | DECLARE_BITMAP(set[2], 256); |
| 873 | } blocked_delegations; |
| 874 | |
| 875 | static int delegation_blocked(struct knfsd_fh *fh) |
| 876 | { |
| 877 | u32 hash; |
| 878 | struct bloom_pair *bd = &blocked_delegations; |
| 879 | |
| 880 | if (bd->entries == 0) |
| 881 | return 0; |
Arnd Bergmann | b3f255e | 2017-10-20 11:25:34 +0200 | [diff] [blame] | 882 | if (ktime_get_seconds() - bd->swap_time > 30) { |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 883 | spin_lock(&blocked_delegations_lock); |
Arnd Bergmann | b3f255e | 2017-10-20 11:25:34 +0200 | [diff] [blame] | 884 | if (ktime_get_seconds() - bd->swap_time > 30) { |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 885 | bd->entries -= bd->old_entries; |
| 886 | bd->old_entries = bd->entries; |
| 887 | memset(bd->set[bd->new], 0, |
| 888 | sizeof(bd->set[0])); |
| 889 | bd->new = 1-bd->new; |
Arnd Bergmann | b3f255e | 2017-10-20 11:25:34 +0200 | [diff] [blame] | 890 | bd->swap_time = ktime_get_seconds(); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 891 | } |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 892 | spin_unlock(&blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 893 | } |
Daniel Borkmann | 8754589 | 2014-12-10 16:33:11 +0100 | [diff] [blame] | 894 | hash = jhash(&fh->fh_base, fh->fh_size, 0); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 895 | if (test_bit(hash&255, bd->set[0]) && |
| 896 | test_bit((hash>>8)&255, bd->set[0]) && |
| 897 | test_bit((hash>>16)&255, bd->set[0])) |
| 898 | return 1; |
| 899 | |
| 900 | if (test_bit(hash&255, bd->set[1]) && |
| 901 | test_bit((hash>>8)&255, bd->set[1]) && |
| 902 | test_bit((hash>>16)&255, bd->set[1])) |
| 903 | return 1; |
| 904 | |
| 905 | return 0; |
| 906 | } |
| 907 | |
| 908 | static void block_delegations(struct knfsd_fh *fh) |
| 909 | { |
| 910 | u32 hash; |
| 911 | struct bloom_pair *bd = &blocked_delegations; |
| 912 | |
Daniel Borkmann | 8754589 | 2014-12-10 16:33:11 +0100 | [diff] [blame] | 913 | hash = jhash(&fh->fh_base, fh->fh_size, 0); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 914 | |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 915 | spin_lock(&blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 916 | __set_bit(hash&255, bd->set[bd->new]); |
| 917 | __set_bit((hash>>8)&255, bd->set[bd->new]); |
| 918 | __set_bit((hash>>16)&255, bd->set[bd->new]); |
| 919 | if (bd->entries == 0) |
Arnd Bergmann | b3f255e | 2017-10-20 11:25:34 +0200 | [diff] [blame] | 920 | bd->swap_time = ktime_get_seconds(); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 921 | bd->entries += 1; |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 922 | spin_unlock(&blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 923 | } |
| 924 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 925 | static struct nfs4_delegation * |
J. Bruce Fields | 86d29b1 | 2018-02-21 15:27:28 -0500 | [diff] [blame] | 926 | alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp, |
| 927 | struct svc_fh *current_fh, |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 928 | struct nfs4_clnt_odstate *odstate) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | { |
| 930 | struct nfs4_delegation *dp; |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 931 | long n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | |
| 933 | dprintk("NFSD alloc_init_deleg\n"); |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 934 | n = atomic_long_inc_return(&num_delegations); |
| 935 | if (n < 0 || n > max_delegations) |
| 936 | goto out_dec; |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 937 | if (delegation_blocked(¤t_fh->fh_handle)) |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 938 | goto out_dec; |
Kinglong Mee | d19fb70 | 2017-01-18 19:04:42 +0800 | [diff] [blame] | 939 | dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg)); |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 940 | if (dp == NULL) |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 941 | goto out_dec; |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 942 | |
J. Bruce Fields | 2a74aba | 2011-09-23 17:20:02 -0400 | [diff] [blame] | 943 | /* |
| 944 | * delegation seqid's are never incremented. The 4.1 special |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 945 | * meaning of seqid 0 isn't meaningful, really, but let's avoid |
| 946 | * 0 anyway just for consistency and use 1: |
J. Bruce Fields | 2a74aba | 2011-09-23 17:20:02 -0400 | [diff] [blame] | 947 | */ |
| 948 | dp->dl_stid.sc_stateid.si_generation = 1; |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 949 | INIT_LIST_HEAD(&dp->dl_perfile); |
| 950 | INIT_LIST_HEAD(&dp->dl_perclnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | INIT_LIST_HEAD(&dp->dl_recall_lru); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 952 | dp->dl_clnt_odstate = odstate; |
| 953 | get_clnt_odstate(odstate); |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 954 | dp->dl_type = NFS4_OPEN_DELEGATE_READ; |
Christoph Hellwig | f0b5de1 | 2014-09-24 12:19:18 +0200 | [diff] [blame] | 955 | dp->dl_retries = 1; |
| 956 | nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client, |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 957 | &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL); |
J. Bruce Fields | 86d29b1 | 2018-02-21 15:27:28 -0500 | [diff] [blame] | 958 | get_nfs4_file(fp); |
| 959 | dp->dl_stid.sc_file = fp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 960 | return dp; |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 961 | out_dec: |
| 962 | atomic_long_dec(&num_delegations); |
| 963 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | } |
| 965 | |
| 966 | void |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 967 | nfs4_put_stid(struct nfs4_stid *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 969 | struct nfs4_file *fp = s->sc_file; |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 970 | struct nfs4_client *clp = s->sc_client; |
| 971 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 972 | might_lock(&clp->cl_lock); |
| 973 | |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 974 | if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) { |
Jeff Layton | b401be22 | 2014-07-29 21:34:33 -0400 | [diff] [blame] | 975 | wake_up_all(&close_wq); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 976 | return; |
Jeff Layton | b401be22 | 2014-07-29 21:34:33 -0400 | [diff] [blame] | 977 | } |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 978 | idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id); |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 979 | nfs4_free_cpntf_statelist(clp->net, s); |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 980 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 981 | s->sc_free(s); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 982 | if (fp) |
| 983 | put_nfs4_file(fp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | } |
| 985 | |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 986 | void |
| 987 | nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid) |
| 988 | { |
| 989 | stateid_t *src = &stid->sc_stateid; |
| 990 | |
| 991 | spin_lock(&stid->sc_lock); |
| 992 | if (unlikely(++src->si_generation == 0)) |
| 993 | src->si_generation = 1; |
| 994 | memcpy(dst, src, sizeof(*dst)); |
| 995 | spin_unlock(&stid->sc_lock); |
| 996 | } |
| 997 | |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 998 | static void put_deleg_file(struct nfs4_file *fp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 999 | { |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 1000 | struct nfsd_file *nf = NULL; |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 1001 | |
| 1002 | spin_lock(&fp->fi_lock); |
| 1003 | if (--fp->fi_delegees == 0) |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 1004 | swap(nf, fp->fi_deleg_file); |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 1005 | spin_unlock(&fp->fi_lock); |
| 1006 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 1007 | if (nf) |
| 1008 | nfsd_file_put(nf); |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp) |
| 1012 | { |
J. Bruce Fields | cba7b3d | 2018-02-15 15:50:49 -0500 | [diff] [blame] | 1013 | struct nfs4_file *fp = dp->dl_stid.sc_file; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 1014 | struct nfsd_file *nf = fp->fi_deleg_file; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 1015 | |
J. Bruce Fields | b8232d3 | 2018-02-15 15:29:15 -0500 | [diff] [blame] | 1016 | WARN_ON_ONCE(!fp->fi_delegees); |
| 1017 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 1018 | vfs_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp); |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 1019 | put_deleg_file(fp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | } |
| 1021 | |
J. Bruce Fields | 0af6e69 | 2018-02-21 15:11:03 -0500 | [diff] [blame] | 1022 | static void destroy_unhashed_deleg(struct nfs4_delegation *dp) |
| 1023 | { |
| 1024 | put_clnt_odstate(dp->dl_clnt_odstate); |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 1025 | nfs4_unlock_deleg_lease(dp); |
J. Bruce Fields | 0af6e69 | 2018-02-21 15:11:03 -0500 | [diff] [blame] | 1026 | nfs4_put_stid(&dp->dl_stid); |
| 1027 | } |
| 1028 | |
Christoph Hellwig | cd61c52 | 2014-08-14 08:44:57 +0200 | [diff] [blame] | 1029 | void nfs4_unhash_stid(struct nfs4_stid *s) |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 1030 | { |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 1031 | s->sc_type = 0; |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 1032 | } |
| 1033 | |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1034 | /** |
J. Bruce Fields | 68b18f5 | 2018-02-19 11:38:33 -0500 | [diff] [blame] | 1035 | * nfs4_delegation_exists - Discover if this delegation already exists |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1036 | * @clp: a pointer to the nfs4_client we're granting a delegation to |
| 1037 | * @fp: a pointer to the nfs4_file we're granting a delegation on |
| 1038 | * |
| 1039 | * Return: |
J. Bruce Fields | 68b18f5 | 2018-02-19 11:38:33 -0500 | [diff] [blame] | 1040 | * On success: true iff an existing delegation is found |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1041 | */ |
| 1042 | |
J. Bruce Fields | 68b18f5 | 2018-02-19 11:38:33 -0500 | [diff] [blame] | 1043 | static bool |
| 1044 | nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp) |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 1045 | { |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1046 | struct nfs4_delegation *searchdp = NULL; |
| 1047 | struct nfs4_client *searchclp = NULL; |
| 1048 | |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 1049 | lockdep_assert_held(&state_lock); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 1050 | lockdep_assert_held(&fp->fi_lock); |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 1051 | |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1052 | list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) { |
| 1053 | searchclp = searchdp->dl_stid.sc_client; |
| 1054 | if (clp == searchclp) { |
Fengguang Wu | 51d87bc | 2018-03-22 13:37:20 +0800 | [diff] [blame] | 1055 | return true; |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1056 | } |
| 1057 | } |
Fengguang Wu | 51d87bc | 2018-03-22 13:37:20 +0800 | [diff] [blame] | 1058 | return false; |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | /** |
| 1062 | * hash_delegation_locked - Add a delegation to the appropriate lists |
| 1063 | * @dp: a pointer to the nfs4_delegation we are adding. |
| 1064 | * @fp: a pointer to the nfs4_file we're granting a delegation on |
| 1065 | * |
| 1066 | * Return: |
| 1067 | * On success: NULL if the delegation was successfully hashed. |
| 1068 | * |
| 1069 | * On error: -EAGAIN if one was previously granted to this |
| 1070 | * nfs4_client for this nfs4_file. Delegation is not hashed. |
| 1071 | * |
| 1072 | */ |
| 1073 | |
| 1074 | static int |
| 1075 | hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp) |
| 1076 | { |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1077 | struct nfs4_client *clp = dp->dl_stid.sc_client; |
| 1078 | |
| 1079 | lockdep_assert_held(&state_lock); |
| 1080 | lockdep_assert_held(&fp->fi_lock); |
| 1081 | |
J. Bruce Fields | 68b18f5 | 2018-02-19 11:38:33 -0500 | [diff] [blame] | 1082 | if (nfs4_delegation_exists(clp, fp)) |
| 1083 | return -EAGAIN; |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 1084 | refcount_inc(&dp->dl_stid.sc_count); |
Benny Halevy | 3fb87d1 | 2014-05-30 09:09:31 -0400 | [diff] [blame] | 1085 | dp->dl_stid.sc_type = NFS4_DELEG_STID; |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 1086 | list_add(&dp->dl_perfile, &fp->fi_delegations); |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1087 | list_add(&dp->dl_perclnt, &clp->cl_delegations); |
| 1088 | return 0; |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 1089 | } |
| 1090 | |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 1091 | static bool |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 1092 | unhash_delegation_locked(struct nfs4_delegation *dp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 1094 | struct nfs4_file *fp = dp->dl_stid.sc_file; |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 1095 | |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 1096 | lockdep_assert_held(&state_lock); |
| 1097 | |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 1098 | if (list_empty(&dp->dl_perfile)) |
| 1099 | return false; |
| 1100 | |
Trond Myklebust | b0fc29d | 2014-07-16 10:31:59 -0400 | [diff] [blame] | 1101 | dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID; |
Jeff Layton | d55a166 | 2014-07-22 13:52:06 -0400 | [diff] [blame] | 1102 | /* Ensure that deleg break won't try to requeue it */ |
| 1103 | ++dp->dl_time; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 1104 | spin_lock(&fp->fi_lock); |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 1105 | list_del_init(&dp->dl_perclnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | list_del_init(&dp->dl_recall_lru); |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 1107 | list_del_init(&dp->dl_perfile); |
| 1108 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 1109 | return true; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 1110 | } |
| 1111 | |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 1112 | static void destroy_delegation(struct nfs4_delegation *dp) |
| 1113 | { |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 1114 | bool unhashed; |
| 1115 | |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 1116 | spin_lock(&state_lock); |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 1117 | unhashed = unhash_delegation_locked(dp); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 1118 | spin_unlock(&state_lock); |
J. Bruce Fields | 0af6e69 | 2018-02-21 15:11:03 -0500 | [diff] [blame] | 1119 | if (unhashed) |
| 1120 | destroy_unhashed_deleg(dp); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 1121 | } |
| 1122 | |
| 1123 | static void revoke_delegation(struct nfs4_delegation *dp) |
| 1124 | { |
| 1125 | struct nfs4_client *clp = dp->dl_stid.sc_client; |
| 1126 | |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 1127 | WARN_ON(!list_empty(&dp->dl_recall_lru)); |
| 1128 | |
J. Bruce Fields | 0af6e69 | 2018-02-21 15:11:03 -0500 | [diff] [blame] | 1129 | if (clp->cl_minorversion) { |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 1130 | dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID; |
J. Bruce Fields | 0af6e69 | 2018-02-21 15:11:03 -0500 | [diff] [blame] | 1131 | refcount_inc(&dp->dl_stid.sc_count); |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 1132 | spin_lock(&clp->cl_lock); |
| 1133 | list_add(&dp->dl_recall_lru, &clp->cl_revoked); |
| 1134 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 1135 | } |
J. Bruce Fields | 0af6e69 | 2018-02-21 15:11:03 -0500 | [diff] [blame] | 1136 | destroy_unhashed_deleg(dp); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 1137 | } |
| 1138 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1139 | /* |
| 1140 | * SETCLIENTID state |
| 1141 | */ |
| 1142 | |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 1143 | static unsigned int clientid_hashval(u32 id) |
| 1144 | { |
| 1145 | return id & CLIENT_HASH_MASK; |
| 1146 | } |
| 1147 | |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 1148 | static unsigned int clientstr_hashval(struct xdr_netobj name) |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 1149 | { |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 1150 | return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK; |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 1151 | } |
| 1152 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1153 | /* |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1154 | * We store the NONE, READ, WRITE, and BOTH bits separately in the |
| 1155 | * st_{access,deny}_bmap field of the stateid, in order to track not |
| 1156 | * only what share bits are currently in force, but also what |
| 1157 | * combinations of share bits previous opens have used. This allows us |
| 1158 | * to enforce the recommendation of rfc 3530 14.2.19 that the server |
| 1159 | * return an error if the client attempt to downgrade to a combination |
| 1160 | * of share bits not explicable by closing some of its previous opens. |
| 1161 | * |
| 1162 | * XXX: This enforcement is actually incomplete, since we don't keep |
| 1163 | * track of access/deny bit combinations; so, e.g., we allow: |
| 1164 | * |
| 1165 | * OPEN allow read, deny write |
| 1166 | * OPEN allow both, deny none |
| 1167 | * DOWNGRADE allow read, deny none |
| 1168 | * |
| 1169 | * which we should reject. |
| 1170 | */ |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 1171 | static unsigned int |
| 1172 | bmap_to_share_mode(unsigned long bmap) { |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1173 | int i; |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 1174 | unsigned int access = 0; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1175 | |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1176 | for (i = 1; i < 4; i++) { |
| 1177 | if (test_bit(i, &bmap)) |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 1178 | access |= i; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1179 | } |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 1180 | return access; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1181 | } |
| 1182 | |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 1183 | /* set share access for a given stateid */ |
| 1184 | static inline void |
| 1185 | set_access(u32 access, struct nfs4_ol_stateid *stp) |
| 1186 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1187 | unsigned char mask = 1 << access; |
| 1188 | |
| 1189 | WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH); |
| 1190 | stp->st_access_bmap |= mask; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 1191 | } |
| 1192 | |
| 1193 | /* clear share access for a given stateid */ |
| 1194 | static inline void |
| 1195 | clear_access(u32 access, struct nfs4_ol_stateid *stp) |
| 1196 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1197 | unsigned char mask = 1 << access; |
| 1198 | |
| 1199 | WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH); |
| 1200 | stp->st_access_bmap &= ~mask; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 1201 | } |
| 1202 | |
| 1203 | /* test whether a given stateid has access */ |
| 1204 | static inline bool |
| 1205 | test_access(u32 access, struct nfs4_ol_stateid *stp) |
| 1206 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1207 | unsigned char mask = 1 << access; |
| 1208 | |
| 1209 | return (bool)(stp->st_access_bmap & mask); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 1210 | } |
| 1211 | |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 1212 | /* set share deny for a given stateid */ |
| 1213 | static inline void |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1214 | set_deny(u32 deny, struct nfs4_ol_stateid *stp) |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 1215 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1216 | unsigned char mask = 1 << deny; |
| 1217 | |
| 1218 | WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH); |
| 1219 | stp->st_deny_bmap |= mask; |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 1220 | } |
| 1221 | |
| 1222 | /* clear share deny for a given stateid */ |
| 1223 | static inline void |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1224 | clear_deny(u32 deny, struct nfs4_ol_stateid *stp) |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 1225 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1226 | unsigned char mask = 1 << deny; |
| 1227 | |
| 1228 | WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH); |
| 1229 | stp->st_deny_bmap &= ~mask; |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | /* test whether a given stateid is denying specific access */ |
| 1233 | static inline bool |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1234 | test_deny(u32 deny, struct nfs4_ol_stateid *stp) |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 1235 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1236 | unsigned char mask = 1 << deny; |
| 1237 | |
| 1238 | return (bool)(stp->st_deny_bmap & mask); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1239 | } |
| 1240 | |
| 1241 | static int nfs4_access_to_omode(u32 access) |
| 1242 | { |
J. Bruce Fields | 8f34a43 | 2010-09-02 15:23:16 -0400 | [diff] [blame] | 1243 | switch (access & NFS4_SHARE_ACCESS_BOTH) { |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1244 | case NFS4_SHARE_ACCESS_READ: |
| 1245 | return O_RDONLY; |
| 1246 | case NFS4_SHARE_ACCESS_WRITE: |
| 1247 | return O_WRONLY; |
| 1248 | case NFS4_SHARE_ACCESS_BOTH: |
| 1249 | return O_RDWR; |
| 1250 | } |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 1251 | WARN_ON_ONCE(1); |
| 1252 | return O_RDONLY; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1253 | } |
| 1254 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 1255 | /* |
| 1256 | * A stateid that had a deny mode associated with it is being released |
| 1257 | * or downgraded. Recalculate the deny mode on the file. |
| 1258 | */ |
| 1259 | static void |
| 1260 | recalculate_deny_mode(struct nfs4_file *fp) |
| 1261 | { |
| 1262 | struct nfs4_ol_stateid *stp; |
| 1263 | |
| 1264 | spin_lock(&fp->fi_lock); |
| 1265 | fp->fi_share_deny = 0; |
| 1266 | list_for_each_entry(stp, &fp->fi_stateids, st_perfile) |
| 1267 | fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap); |
| 1268 | spin_unlock(&fp->fi_lock); |
| 1269 | } |
| 1270 | |
| 1271 | static void |
| 1272 | reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp) |
| 1273 | { |
| 1274 | int i; |
| 1275 | bool change = false; |
| 1276 | |
| 1277 | for (i = 1; i < 4; i++) { |
| 1278 | if ((i & deny) != i) { |
| 1279 | change = true; |
| 1280 | clear_deny(i, stp); |
| 1281 | } |
| 1282 | } |
| 1283 | |
| 1284 | /* Recalculate per-file deny mode if there was a change */ |
| 1285 | if (change) |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 1286 | recalculate_deny_mode(stp->st_stid.sc_file); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 1287 | } |
| 1288 | |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 1289 | /* release all access and file references for a given stateid */ |
| 1290 | static void |
| 1291 | release_all_access(struct nfs4_ol_stateid *stp) |
| 1292 | { |
| 1293 | int i; |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 1294 | struct nfs4_file *fp = stp->st_stid.sc_file; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 1295 | |
| 1296 | if (fp && stp->st_deny_bmap != 0) |
| 1297 | recalculate_deny_mode(fp); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 1298 | |
| 1299 | for (i = 1; i < 4; i++) { |
| 1300 | if (test_access(i, stp)) |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 1301 | nfs4_file_put_access(stp->st_stid.sc_file, i); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 1302 | clear_access(i, stp); |
| 1303 | } |
| 1304 | } |
| 1305 | |
Kinglong Mee | d50ffde | 2015-07-16 12:05:07 +0800 | [diff] [blame] | 1306 | static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop) |
| 1307 | { |
| 1308 | kfree(sop->so_owner.data); |
| 1309 | sop->so_ops->so_free(sop); |
| 1310 | } |
| 1311 | |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 1312 | static void nfs4_put_stateowner(struct nfs4_stateowner *sop) |
| 1313 | { |
Jeff Layton | a819ecc | 2014-07-29 21:34:38 -0400 | [diff] [blame] | 1314 | struct nfs4_client *clp = sop->so_client; |
| 1315 | |
| 1316 | might_lock(&clp->cl_lock); |
| 1317 | |
| 1318 | if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock)) |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 1319 | return; |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 1320 | sop->so_ops->so_unhash(sop); |
Jeff Layton | a819ecc | 2014-07-29 21:34:38 -0400 | [diff] [blame] | 1321 | spin_unlock(&clp->cl_lock); |
Kinglong Mee | d50ffde | 2015-07-16 12:05:07 +0800 | [diff] [blame] | 1322 | nfs4_free_stateowner(sop); |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 1323 | } |
| 1324 | |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 1325 | static bool |
| 1326 | nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid *stp) |
| 1327 | { |
| 1328 | return list_empty(&stp->st_perfile); |
| 1329 | } |
| 1330 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1331 | static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1332 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 1333 | struct nfs4_file *fp = stp->st_stid.sc_file; |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 1334 | |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 1335 | lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock); |
| 1336 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1337 | if (list_empty(&stp->st_perfile)) |
| 1338 | return false; |
| 1339 | |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 1340 | spin_lock(&fp->fi_lock); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1341 | list_del_init(&stp->st_perfile); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 1342 | spin_unlock(&fp->fi_lock); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1343 | list_del(&stp->st_perstateowner); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1344 | return true; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1345 | } |
| 1346 | |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1347 | static void nfs4_free_ol_stateid(struct nfs4_stid *stid) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1348 | { |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1349 | struct nfs4_ol_stateid *stp = openlockstateid(stid); |
J. Bruce Fields | 4665e2b | 2011-09-06 14:50:49 -0400 | [diff] [blame] | 1350 | |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 1351 | put_clnt_odstate(stp->st_clnt_odstate); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1352 | release_all_access(stp); |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 1353 | if (stp->st_stateowner) |
| 1354 | nfs4_put_stateowner(stp->st_stateowner); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1355 | kmem_cache_free(stateid_slab, stid); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1356 | } |
| 1357 | |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 1358 | static void nfs4_free_lock_stateid(struct nfs4_stid *stid) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1359 | { |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 1360 | struct nfs4_ol_stateid *stp = openlockstateid(stid); |
| 1361 | struct nfs4_lockowner *lo = lockowner(stp->st_stateowner); |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 1362 | struct nfsd_file *nf; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1363 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 1364 | nf = find_any_file(stp->st_stid.sc_file); |
| 1365 | if (nf) { |
| 1366 | get_file(nf->nf_file); |
| 1367 | filp_close(nf->nf_file, (fl_owner_t)lo); |
| 1368 | nfsd_file_put(nf); |
| 1369 | } |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 1370 | nfs4_free_ol_stateid(stid); |
| 1371 | } |
| 1372 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1373 | /* |
| 1374 | * Put the persistent reference to an already unhashed generic stateid, while |
| 1375 | * holding the cl_lock. If it's the last reference, then put it onto the |
| 1376 | * reaplist for later destruction. |
| 1377 | */ |
| 1378 | static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp, |
| 1379 | struct list_head *reaplist) |
| 1380 | { |
| 1381 | struct nfs4_stid *s = &stp->st_stid; |
| 1382 | struct nfs4_client *clp = s->sc_client; |
| 1383 | |
| 1384 | lockdep_assert_held(&clp->cl_lock); |
| 1385 | |
| 1386 | WARN_ON_ONCE(!list_empty(&stp->st_locks)); |
| 1387 | |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 1388 | if (!refcount_dec_and_test(&s->sc_count)) { |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1389 | wake_up_all(&close_wq); |
| 1390 | return; |
| 1391 | } |
| 1392 | |
| 1393 | idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id); |
| 1394 | list_add(&stp->st_locks, reaplist); |
| 1395 | } |
| 1396 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1397 | static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp) |
Jeff Layton | 3c1c995 | 2014-07-29 21:34:39 -0400 | [diff] [blame] | 1398 | { |
Chuck Lever | f46c445 | 2016-10-29 18:19:03 -0400 | [diff] [blame] | 1399 | lockdep_assert_held(&stp->st_stid.sc_client->cl_lock); |
Jeff Layton | 3c1c995 | 2014-07-29 21:34:39 -0400 | [diff] [blame] | 1400 | |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 1401 | if (!unhash_ol_stateid(stp)) |
| 1402 | return false; |
Jeff Layton | 3c1c995 | 2014-07-29 21:34:39 -0400 | [diff] [blame] | 1403 | list_del_init(&stp->st_locks); |
Christoph Hellwig | cd61c52 | 2014-08-14 08:44:57 +0200 | [diff] [blame] | 1404 | nfs4_unhash_stid(&stp->st_stid); |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 1405 | return true; |
Jeff Layton | 3c1c995 | 2014-07-29 21:34:39 -0400 | [diff] [blame] | 1406 | } |
| 1407 | |
Jeff Layton | 5adfd88 | 2014-07-29 21:34:31 -0400 | [diff] [blame] | 1408 | static void release_lock_stateid(struct nfs4_ol_stateid *stp) |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 1409 | { |
Chuck Lever | f46c445 | 2016-10-29 18:19:03 -0400 | [diff] [blame] | 1410 | struct nfs4_client *clp = stp->st_stid.sc_client; |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1411 | bool unhashed; |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 1412 | |
Chuck Lever | f46c445 | 2016-10-29 18:19:03 -0400 | [diff] [blame] | 1413 | spin_lock(&clp->cl_lock); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1414 | unhashed = unhash_lock_stateid(stp); |
Chuck Lever | f46c445 | 2016-10-29 18:19:03 -0400 | [diff] [blame] | 1415 | spin_unlock(&clp->cl_lock); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1416 | if (unhashed) |
| 1417 | nfs4_put_stid(&stp->st_stid); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1418 | } |
| 1419 | |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 1420 | static void unhash_lockowner_locked(struct nfs4_lockowner *lo) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1421 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1422 | struct nfs4_client *clp = lo->lo_owner.so_client; |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 1423 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1424 | lockdep_assert_held(&clp->cl_lock); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 1425 | |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 1426 | list_del_init(&lo->lo_owner.so_strhash); |
| 1427 | } |
| 1428 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1429 | /* |
| 1430 | * Free a list of generic stateids that were collected earlier after being |
| 1431 | * fully unhashed. |
| 1432 | */ |
| 1433 | static void |
| 1434 | free_ol_stateid_reaplist(struct list_head *reaplist) |
| 1435 | { |
| 1436 | struct nfs4_ol_stateid *stp; |
Kinglong Mee | fb94d76 | 2014-08-05 21:20:27 +0800 | [diff] [blame] | 1437 | struct nfs4_file *fp; |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1438 | |
| 1439 | might_sleep(); |
| 1440 | |
| 1441 | while (!list_empty(reaplist)) { |
| 1442 | stp = list_first_entry(reaplist, struct nfs4_ol_stateid, |
| 1443 | st_locks); |
| 1444 | list_del(&stp->st_locks); |
Kinglong Mee | fb94d76 | 2014-08-05 21:20:27 +0800 | [diff] [blame] | 1445 | fp = stp->st_stid.sc_file; |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1446 | stp->st_stid.sc_free(&stp->st_stid); |
Kinglong Mee | fb94d76 | 2014-08-05 21:20:27 +0800 | [diff] [blame] | 1447 | if (fp) |
| 1448 | put_nfs4_file(fp); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1449 | } |
| 1450 | } |
| 1451 | |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 1452 | static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp, |
| 1453 | struct list_head *reaplist) |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 1454 | { |
| 1455 | struct nfs4_ol_stateid *stp; |
| 1456 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1457 | lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock); |
| 1458 | |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 1459 | while (!list_empty(&open_stp->st_locks)) { |
| 1460 | stp = list_entry(open_stp->st_locks.next, |
| 1461 | struct nfs4_ol_stateid, st_locks); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1462 | WARN_ON(!unhash_lock_stateid(stp)); |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 1463 | put_ol_stateid_locked(stp, reaplist); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1464 | } |
| 1465 | } |
| 1466 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1467 | static bool unhash_open_stateid(struct nfs4_ol_stateid *stp, |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 1468 | struct list_head *reaplist) |
J. Bruce Fields | 2283963 | 2009-01-11 14:27:17 -0500 | [diff] [blame] | 1469 | { |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1470 | lockdep_assert_held(&stp->st_stid.sc_client->cl_lock); |
| 1471 | |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 1472 | if (!unhash_ol_stateid(stp)) |
| 1473 | return false; |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 1474 | release_open_stateid_locks(stp, reaplist); |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 1475 | return true; |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | static void release_open_stateid(struct nfs4_ol_stateid *stp) |
| 1479 | { |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1480 | LIST_HEAD(reaplist); |
| 1481 | |
| 1482 | spin_lock(&stp->st_stid.sc_client->cl_lock); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1483 | if (unhash_open_stateid(stp, &reaplist)) |
| 1484 | put_ol_stateid_locked(stp, &reaplist); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1485 | spin_unlock(&stp->st_stid.sc_client->cl_lock); |
| 1486 | free_ol_stateid_reaplist(&reaplist); |
J. Bruce Fields | 2283963 | 2009-01-11 14:27:17 -0500 | [diff] [blame] | 1487 | } |
| 1488 | |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1489 | static void unhash_openowner_locked(struct nfs4_openowner *oo) |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 1490 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1491 | struct nfs4_client *clp = oo->oo_owner.so_client; |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1492 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1493 | lockdep_assert_held(&clp->cl_lock); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1494 | |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 1495 | list_del_init(&oo->oo_owner.so_strhash); |
| 1496 | list_del_init(&oo->oo_perclient); |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 1497 | } |
| 1498 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1499 | static void release_last_closed_stateid(struct nfs4_openowner *oo) |
| 1500 | { |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 1501 | struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net, |
| 1502 | nfsd_net_id); |
| 1503 | struct nfs4_ol_stateid *s; |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1504 | |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 1505 | spin_lock(&nn->client_lock); |
| 1506 | s = oo->oo_last_closed_stid; |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1507 | if (s) { |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 1508 | list_del_init(&oo->oo_close_lru); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1509 | oo->oo_last_closed_stid = NULL; |
| 1510 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 1511 | spin_unlock(&nn->client_lock); |
| 1512 | if (s) |
| 1513 | nfs4_put_stid(&s->st_stid); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1514 | } |
| 1515 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1516 | static void release_openowner(struct nfs4_openowner *oo) |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 1517 | { |
| 1518 | struct nfs4_ol_stateid *stp; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1519 | struct nfs4_client *clp = oo->oo_owner.so_client; |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1520 | struct list_head reaplist; |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1521 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1522 | INIT_LIST_HEAD(&reaplist); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1523 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1524 | spin_lock(&clp->cl_lock); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1525 | unhash_openowner_locked(oo); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1526 | while (!list_empty(&oo->oo_owner.so_stateids)) { |
| 1527 | stp = list_first_entry(&oo->oo_owner.so_stateids, |
| 1528 | struct nfs4_ol_stateid, st_perstateowner); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1529 | if (unhash_open_stateid(stp, &reaplist)) |
| 1530 | put_ol_stateid_locked(stp, &reaplist); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1531 | } |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1532 | spin_unlock(&clp->cl_lock); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1533 | free_ol_stateid_reaplist(&reaplist); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1534 | release_last_closed_stateid(oo); |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 1535 | nfs4_put_stateowner(&oo->oo_owner); |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 1536 | } |
| 1537 | |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1538 | static inline int |
| 1539 | hash_sessionid(struct nfs4_sessionid *sessionid) |
| 1540 | { |
| 1541 | struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid; |
| 1542 | |
| 1543 | return sid->sequence % SESSION_HASH_SIZE; |
| 1544 | } |
| 1545 | |
Mark Salter | 135dd00 | 2015-04-06 09:46:00 -0400 | [diff] [blame] | 1546 | #ifdef CONFIG_SUNRPC_DEBUG |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1547 | static inline void |
| 1548 | dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) |
| 1549 | { |
| 1550 | u32 *ptr = (u32 *)(&sessionid->data[0]); |
| 1551 | dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]); |
| 1552 | } |
Trond Myklebust | 8f199b8 | 2012-03-20 15:11:17 -0400 | [diff] [blame] | 1553 | #else |
| 1554 | static inline void |
| 1555 | dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) |
| 1556 | { |
| 1557 | } |
| 1558 | #endif |
| 1559 | |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 1560 | /* |
| 1561 | * Bump the seqid on cstate->replay_owner, and clear replay_owner if it |
| 1562 | * won't be used for replay. |
| 1563 | */ |
| 1564 | void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr) |
| 1565 | { |
| 1566 | struct nfs4_stateowner *so = cstate->replay_owner; |
| 1567 | |
| 1568 | if (nfserr == nfserr_replay_me) |
| 1569 | return; |
| 1570 | |
| 1571 | if (!seqid_mutating_err(ntohl(nfserr))) { |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 1572 | nfsd4_cstate_clear_replay(cstate); |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 1573 | return; |
| 1574 | } |
| 1575 | if (!so) |
| 1576 | return; |
| 1577 | if (so->so_is_open_owner) |
| 1578 | release_last_closed_stateid(openowner(so)); |
| 1579 | so->so_seqid++; |
| 1580 | return; |
| 1581 | } |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1582 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1583 | static void |
| 1584 | gen_sessionid(struct nfsd4_session *ses) |
| 1585 | { |
| 1586 | struct nfs4_client *clp = ses->se_client; |
| 1587 | struct nfsd4_sessionid *sid; |
| 1588 | |
| 1589 | sid = (struct nfsd4_sessionid *)ses->se_sessionid.data; |
| 1590 | sid->clientid = clp->cl_clientid; |
| 1591 | sid->sequence = current_sessionid++; |
| 1592 | sid->reserved = 0; |
| 1593 | } |
| 1594 | |
| 1595 | /* |
Andy Adamson | a649637c7 | 2009-08-28 08:45:01 -0400 | [diff] [blame] | 1596 | * The protocol defines ca_maxresponssize_cached to include the size of |
| 1597 | * the rpc header, but all we need to cache is the data starting after |
| 1598 | * the end of the initial SEQUENCE operation--the rest we regenerate |
| 1599 | * each time. Therefore we can advertise a ca_maxresponssize_cached |
| 1600 | * value that is the number of bytes in our cache plus a few additional |
| 1601 | * bytes. In order to stay on the safe side, and not promise more than |
| 1602 | * we can cache, those additional bytes must be the minimum possible: 24 |
| 1603 | * bytes of rpc header (xid through accept state, with AUTH_NULL |
| 1604 | * verifier), 12 for the compound header (with zero-length tag), and 44 |
| 1605 | * for the SEQUENCE op response: |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1606 | */ |
Andy Adamson | a649637c7 | 2009-08-28 08:45:01 -0400 | [diff] [blame] | 1607 | #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44) |
| 1608 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1609 | static void |
| 1610 | free_session_slots(struct nfsd4_session *ses) |
| 1611 | { |
| 1612 | int i; |
| 1613 | |
J. Bruce Fields | 53da6a5 | 2017-10-17 20:38:49 -0400 | [diff] [blame] | 1614 | for (i = 0; i < ses->se_fchannel.maxreqs; i++) { |
| 1615 | free_svc_cred(&ses->se_slots[i]->sl_cred); |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1616 | kfree(ses->se_slots[i]); |
J. Bruce Fields | 53da6a5 | 2017-10-17 20:38:49 -0400 | [diff] [blame] | 1617 | } |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1618 | } |
| 1619 | |
J. Bruce Fields | efe0cb6 | 2009-10-24 20:52:16 -0400 | [diff] [blame] | 1620 | /* |
| 1621 | * We don't actually need to cache the rpc and session headers, so we |
| 1622 | * can allocate a little less for each slot: |
| 1623 | */ |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1624 | static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca) |
J. Bruce Fields | efe0cb6 | 2009-10-24 20:52:16 -0400 | [diff] [blame] | 1625 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1626 | u32 size; |
J. Bruce Fields | efe0cb6 | 2009-10-24 20:52:16 -0400 | [diff] [blame] | 1627 | |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1628 | if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ) |
| 1629 | size = 0; |
| 1630 | else |
| 1631 | size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ; |
| 1632 | return size + sizeof(struct nfsd4_slot); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1633 | } |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1634 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1635 | /* |
| 1636 | * XXX: If we run out of reserved DRC memory we could (up to a point) |
| 1637 | * re-negotiate active sessions and reduce their slot usage to make |
Justin P. Mattock | 42b2aa8 | 2011-11-28 20:31:00 -0800 | [diff] [blame] | 1638 | * 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] | 1639 | */ |
NeilBrown | 2030ca5 | 2019-09-20 16:36:45 +1000 | [diff] [blame] | 1640 | 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] | 1641 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1642 | u32 slotsize = slot_bytes(ca); |
| 1643 | u32 num = ca->maxreqs; |
J. Bruce Fields | c54f24e | 2019-02-21 10:47:00 -0500 | [diff] [blame] | 1644 | unsigned long avail, total_avail; |
NeilBrown | 2030ca5 | 2019-09-20 16:36:45 +1000 | [diff] [blame] | 1645 | unsigned int scale_factor; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1646 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1647 | spin_lock(&nfsd_drc_lock); |
NeilBrown | 7f49fd5d | 2019-09-20 16:33:16 +1000 | [diff] [blame] | 1648 | if (nfsd_drc_max_mem > nfsd_drc_mem_used) |
| 1649 | total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used; |
| 1650 | else |
| 1651 | /* We have handed out more space than we chose in |
| 1652 | * set_max_drc() to allow. That isn't really a |
| 1653 | * problem as long as that doesn't make us think we |
| 1654 | * have lots more due to integer overflow. |
| 1655 | */ |
| 1656 | total_avail = 0; |
J. Bruce Fields | c54f24e | 2019-02-21 10:47:00 -0500 | [diff] [blame] | 1657 | avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail); |
J. Bruce Fields | de766e5 | 2017-09-19 19:25:41 -0400 | [diff] [blame] | 1658 | /* |
NeilBrown | 2030ca5 | 2019-09-20 16:36:45 +1000 | [diff] [blame] | 1659 | * Never use more than a fraction of the remaining memory, |
NeilBrown | 7f49fd5d | 2019-09-20 16:33:16 +1000 | [diff] [blame] | 1660 | * unless it's the only way to give this client a slot. |
NeilBrown | 2030ca5 | 2019-09-20 16:36:45 +1000 | [diff] [blame] | 1661 | * The chosen fraction is either 1/8 or 1/number of threads, |
| 1662 | * whichever is smaller. This ensures there are adequate |
| 1663 | * slots to support multiple clients per thread. |
NeilBrown | 7f49fd5d | 2019-09-20 16:33:16 +1000 | [diff] [blame] | 1664 | * Give the client one slot even if that would require |
| 1665 | * over-allocation--it is better than failure. |
J. Bruce Fields | de766e5 | 2017-09-19 19:25:41 -0400 | [diff] [blame] | 1666 | */ |
NeilBrown | 2030ca5 | 2019-09-20 16:36:45 +1000 | [diff] [blame] | 1667 | scale_factor = max_t(unsigned int, 8, nn->nfsd_serv->sv_nrthreads); |
| 1668 | |
| 1669 | avail = clamp_t(unsigned long, avail, slotsize, |
| 1670 | total_avail/scale_factor); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1671 | num = min_t(int, num, avail / slotsize); |
NeilBrown | 7f49fd5d | 2019-09-20 16:33:16 +1000 | [diff] [blame] | 1672 | num = max_t(int, num, 1); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1673 | nfsd_drc_mem_used += num * slotsize; |
| 1674 | spin_unlock(&nfsd_drc_lock); |
| 1675 | |
| 1676 | return num; |
| 1677 | } |
| 1678 | |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1679 | static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1680 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1681 | int slotsize = slot_bytes(ca); |
| 1682 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1683 | spin_lock(&nfsd_drc_lock); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1684 | nfsd_drc_mem_used -= slotsize * ca->maxreqs; |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1685 | spin_unlock(&nfsd_drc_lock); |
| 1686 | } |
| 1687 | |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1688 | static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs, |
| 1689 | struct nfsd4_channel_attrs *battrs) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1690 | { |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1691 | int numslots = fattrs->maxreqs; |
| 1692 | int slotsize = slot_bytes(fattrs); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1693 | struct nfsd4_session *new; |
| 1694 | int mem, i; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1695 | |
J. Bruce Fields | c23753d | 2010-09-27 16:22:30 -0400 | [diff] [blame] | 1696 | 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] | 1697 | + sizeof(struct nfsd4_session) > PAGE_SIZE); |
| 1698 | mem = numslots * sizeof(struct nfsd4_slot *); |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1699 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1700 | new = kzalloc(sizeof(*new) + mem, GFP_KERNEL); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1701 | if (!new) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1702 | return NULL; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1703 | /* allocate each struct nfsd4_slot and data cache in one piece */ |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1704 | for (i = 0; i < numslots; i++) { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1705 | new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1706 | if (!new->se_slots[i]) |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1707 | goto out_free; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1708 | } |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1709 | |
| 1710 | memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs)); |
| 1711 | memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs)); |
| 1712 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1713 | return new; |
| 1714 | out_free: |
| 1715 | while (i--) |
| 1716 | kfree(new->se_slots[i]); |
| 1717 | kfree(new); |
| 1718 | return NULL; |
| 1719 | } |
| 1720 | |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1721 | static void free_conn(struct nfsd4_conn *c) |
| 1722 | { |
| 1723 | svc_xprt_put(c->cn_xprt); |
| 1724 | kfree(c); |
| 1725 | } |
| 1726 | |
| 1727 | static void nfsd4_conn_lost(struct svc_xpt_user *u) |
| 1728 | { |
| 1729 | struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user); |
| 1730 | struct nfs4_client *clp = c->cn_session->se_client; |
| 1731 | |
| 1732 | spin_lock(&clp->cl_lock); |
| 1733 | if (!list_empty(&c->cn_persession)) { |
| 1734 | list_del(&c->cn_persession); |
| 1735 | free_conn(c); |
| 1736 | } |
J. Bruce Fields | eea4980 | 2010-11-18 08:34:12 -0500 | [diff] [blame] | 1737 | nfsd4_probe_callback(clp); |
J. Bruce Fields | 2e4b723 | 2013-03-08 09:30:43 -0500 | [diff] [blame] | 1738 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1739 | } |
| 1740 | |
J. Bruce Fields | d29c374 | 2010-06-15 17:34:11 -0400 | [diff] [blame] | 1741 | 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] | 1742 | { |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1743 | struct nfsd4_conn *conn; |
| 1744 | |
| 1745 | conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL); |
| 1746 | if (!conn) |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1747 | return NULL; |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1748 | svc_xprt_get(rqstp->rq_xprt); |
| 1749 | conn->cn_xprt = rqstp->rq_xprt; |
J. Bruce Fields | d29c374 | 2010-06-15 17:34:11 -0400 | [diff] [blame] | 1750 | conn->cn_flags = flags; |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1751 | INIT_LIST_HEAD(&conn->cn_xpt_user.list); |
| 1752 | return conn; |
| 1753 | } |
| 1754 | |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 1755 | static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) |
| 1756 | { |
| 1757 | conn->cn_session = ses; |
| 1758 | list_add(&conn->cn_persession, &ses->se_conns); |
| 1759 | } |
| 1760 | |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1761 | static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) |
| 1762 | { |
| 1763 | struct nfs4_client *clp = ses->se_client; |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1764 | |
| 1765 | spin_lock(&clp->cl_lock); |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 1766 | __nfsd4_hash_conn(conn, ses); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1767 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1768 | } |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1769 | |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1770 | static int nfsd4_register_conn(struct nfsd4_conn *conn) |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1771 | { |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1772 | conn->cn_xpt_user.callback = nfsd4_conn_lost; |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1773 | return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user); |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1774 | } |
| 1775 | |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1776 | 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] | 1777 | { |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1778 | int ret; |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1779 | |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1780 | nfsd4_hash_conn(conn, ses); |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1781 | ret = nfsd4_register_conn(conn); |
| 1782 | if (ret) |
| 1783 | /* oops; xprt is already down: */ |
| 1784 | nfsd4_conn_lost(&conn->cn_xpt_user); |
J. Bruce Fields | 57a3714 | 2014-07-18 15:06:47 -0400 | [diff] [blame] | 1785 | /* We may have gained or lost a callback channel: */ |
| 1786 | nfsd4_probe_callback_sync(ses->se_client); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1787 | } |
| 1788 | |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1789 | 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] | 1790 | { |
| 1791 | u32 dir = NFS4_CDFC4_FORE; |
| 1792 | |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1793 | if (cses->flags & SESSION4_BACK_CHAN) |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 1794 | dir |= NFS4_CDFC4_BACK; |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1795 | return alloc_conn(rqstp, dir); |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 1796 | } |
| 1797 | |
| 1798 | /* must be called under client_lock */ |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1799 | static void nfsd4_del_conns(struct nfsd4_session *s) |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1800 | { |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1801 | struct nfs4_client *clp = s->se_client; |
| 1802 | struct nfsd4_conn *c; |
| 1803 | |
| 1804 | spin_lock(&clp->cl_lock); |
| 1805 | while (!list_empty(&s->se_conns)) { |
| 1806 | c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession); |
| 1807 | list_del_init(&c->cn_persession); |
| 1808 | spin_unlock(&clp->cl_lock); |
| 1809 | |
| 1810 | unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user); |
| 1811 | free_conn(c); |
| 1812 | |
| 1813 | spin_lock(&clp->cl_lock); |
| 1814 | } |
| 1815 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1816 | } |
| 1817 | |
J. Bruce Fields | 1377b69 | 2012-09-11 21:42:40 -0400 | [diff] [blame] | 1818 | static void __free_session(struct nfsd4_session *ses) |
| 1819 | { |
J. Bruce Fields | 1377b69 | 2012-09-11 21:42:40 -0400 | [diff] [blame] | 1820 | free_session_slots(ses); |
| 1821 | kfree(ses); |
| 1822 | } |
| 1823 | |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 1824 | static void free_session(struct nfsd4_session *ses) |
Benny Halevy | 508dc6e | 2012-02-23 17:40:52 -0800 | [diff] [blame] | 1825 | { |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1826 | nfsd4_del_conns(ses); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1827 | nfsd4_put_drc_mem(&ses->se_fchannel); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1828 | __free_session(ses); |
J. Bruce Fields | a827bcb | 2012-09-12 09:51:34 -0400 | [diff] [blame] | 1829 | } |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1830 | |
Fengguang Wu | 135ae82 | 2012-11-10 07:20:25 -0500 | [diff] [blame] | 1831 | 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] | 1832 | { |
J. Bruce Fields | a827bcb | 2012-09-12 09:51:34 -0400 | [diff] [blame] | 1833 | int idx; |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1834 | 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] | 1835 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1836 | new->se_client = clp; |
| 1837 | gen_sessionid(new); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1838 | |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1839 | INIT_LIST_HEAD(&new->se_conns); |
| 1840 | |
J. Bruce Fields | ac7c46f2 | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 1841 | new->se_cb_seq_nr = 1; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1842 | new->se_flags = cses->flags; |
J. Bruce Fields | 8b5ce5c | 2010-10-19 17:31:50 -0400 | [diff] [blame] | 1843 | new->se_cb_prog = cses->callback_prog; |
J. Bruce Fields | c6bb3ca | 2012-11-01 16:31:02 -0400 | [diff] [blame] | 1844 | new->se_cb_sec = cses->cb_sec; |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 1845 | atomic_set(&new->se_ref, 0); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1846 | idx = hash_sessionid(&new->se_sessionid); |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1847 | list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1848 | spin_lock(&clp->cl_lock); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1849 | list_add(&new->se_perclnt, &clp->cl_sessions); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1850 | spin_unlock(&clp->cl_lock); |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1851 | |
Chuck Lever | b0d2e42 | 2014-08-22 15:10:59 -0400 | [diff] [blame] | 1852 | { |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1853 | struct sockaddr *sa = svc_addr(rqstp); |
J. Bruce Fields | dcbeaa6 | 2010-06-15 17:25:45 -0400 | [diff] [blame] | 1854 | /* |
| 1855 | * This is a little silly; with sessions there's no real |
| 1856 | * use for the callback address. Use the peer address |
| 1857 | * as a reasonable default for now, but consider fixing |
| 1858 | * the rpc client not to require an address in the |
| 1859 | * future: |
| 1860 | */ |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1861 | rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa); |
| 1862 | clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa); |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1863 | } |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1864 | } |
| 1865 | |
Benny Halevy | 9089f1b | 2010-05-12 00:12:26 +0300 | [diff] [blame] | 1866 | /* caller must hold client_lock */ |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1867 | static struct nfsd4_session * |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 1868 | __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net) |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1869 | { |
| 1870 | struct nfsd4_session *elem; |
| 1871 | int idx; |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1872 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1873 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 1874 | lockdep_assert_held(&nn->client_lock); |
| 1875 | |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1876 | dump_sessionid(__func__, sessionid); |
| 1877 | idx = hash_sessionid(sessionid); |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1878 | /* Search in the appropriate list */ |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1879 | list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) { |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1880 | if (!memcmp(elem->se_sessionid.data, sessionid->data, |
| 1881 | NFS4_MAX_SESSIONID_LEN)) { |
| 1882 | return elem; |
| 1883 | } |
| 1884 | } |
| 1885 | |
| 1886 | dprintk("%s: session not found\n", __func__); |
| 1887 | return NULL; |
| 1888 | } |
| 1889 | |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 1890 | static struct nfsd4_session * |
| 1891 | find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net, |
| 1892 | __be32 *ret) |
| 1893 | { |
| 1894 | struct nfsd4_session *session; |
| 1895 | __be32 status = nfserr_badsession; |
| 1896 | |
| 1897 | session = __find_in_sessionid_hashtbl(sessionid, net); |
| 1898 | if (!session) |
| 1899 | goto out; |
| 1900 | status = nfsd4_get_session_locked(session); |
| 1901 | if (status) |
| 1902 | session = NULL; |
| 1903 | out: |
| 1904 | *ret = status; |
| 1905 | return session; |
| 1906 | } |
| 1907 | |
Benny Halevy | 9089f1b | 2010-05-12 00:12:26 +0300 | [diff] [blame] | 1908 | /* caller must hold client_lock */ |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1909 | static void |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1910 | unhash_session(struct nfsd4_session *ses) |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1911 | { |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 1912 | struct nfs4_client *clp = ses->se_client; |
| 1913 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 1914 | |
| 1915 | lockdep_assert_held(&nn->client_lock); |
| 1916 | |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1917 | list_del(&ses->se_hash); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1918 | spin_lock(&ses->se_client->cl_lock); |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1919 | list_del(&ses->se_perclnt); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1920 | spin_unlock(&ses->se_client->cl_lock); |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1921 | } |
| 1922 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1923 | /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */ |
| 1924 | static int |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 1925 | STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1926 | { |
J. Bruce Fields | bbc7f33 | 2015-01-20 11:51:26 -0500 | [diff] [blame] | 1927 | /* |
| 1928 | * We're assuming the clid was not given out from a boot |
| 1929 | * precisely 2^32 (about 136 years) before this one. That seems |
| 1930 | * a safe assumption: |
| 1931 | */ |
| 1932 | if (clid->cl_boot == (u32)nn->boot_time) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1933 | return 0; |
Chuck Lever | dd5e3fb | 2020-04-05 11:42:29 -0400 | [diff] [blame] | 1934 | trace_nfsd_clid_stale(clid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1935 | return 1; |
| 1936 | } |
| 1937 | |
| 1938 | /* |
| 1939 | * XXX Should we use a slab cache ? |
| 1940 | * This type of memory management is somewhat inefficient, but we use it |
| 1941 | * anyway since SETCLIENTID is not a common operation. |
| 1942 | */ |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1943 | static struct nfs4_client *alloc_client(struct xdr_netobj name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1944 | { |
| 1945 | struct nfs4_client *clp; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1946 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1947 | |
Jeff Layton | 9258a2d | 2018-03-16 09:47:22 -0400 | [diff] [blame] | 1948 | clp = kmem_cache_zalloc(client_slab, GFP_KERNEL); |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1949 | if (clp == NULL) |
| 1950 | return NULL; |
J. Bruce Fields | 6f4859b | 2019-06-19 14:30:33 -0400 | [diff] [blame] | 1951 | xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1952 | if (clp->cl_name.data == NULL) |
| 1953 | goto err_no_name; |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 1954 | clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE, |
| 1955 | sizeof(struct list_head), |
| 1956 | GFP_KERNEL); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1957 | if (!clp->cl_ownerstr_hashtbl) |
| 1958 | goto err_no_hashtbl; |
| 1959 | for (i = 0; i < OWNER_HASH_SIZE; i++) |
| 1960 | INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]); |
Trond Myklebust | 5694c93 | 2014-04-18 14:43:56 -0400 | [diff] [blame] | 1961 | INIT_LIST_HEAD(&clp->cl_sessions); |
| 1962 | idr_init(&clp->cl_stateids); |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 1963 | atomic_set(&clp->cl_rpc_users, 0); |
Trond Myklebust | 5694c93 | 2014-04-18 14:43:56 -0400 | [diff] [blame] | 1964 | clp->cl_cb_state = NFSD4_CB_UNKNOWN; |
| 1965 | INIT_LIST_HEAD(&clp->cl_idhash); |
| 1966 | INIT_LIST_HEAD(&clp->cl_openowners); |
| 1967 | INIT_LIST_HEAD(&clp->cl_delegations); |
| 1968 | INIT_LIST_HEAD(&clp->cl_lru); |
Trond Myklebust | 5694c93 | 2014-04-18 14:43:56 -0400 | [diff] [blame] | 1969 | INIT_LIST_HEAD(&clp->cl_revoked); |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 1970 | #ifdef CONFIG_NFSD_PNFS |
| 1971 | INIT_LIST_HEAD(&clp->cl_lo_states); |
| 1972 | #endif |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 1973 | INIT_LIST_HEAD(&clp->async_copies); |
| 1974 | spin_lock_init(&clp->async_lock); |
Trond Myklebust | 5694c93 | 2014-04-18 14:43:56 -0400 | [diff] [blame] | 1975 | spin_lock_init(&clp->cl_lock); |
| 1976 | rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1977 | return clp; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1978 | err_no_hashtbl: |
| 1979 | kfree(clp->cl_name.data); |
| 1980 | err_no_name: |
Jeff Layton | 9258a2d | 2018-03-16 09:47:22 -0400 | [diff] [blame] | 1981 | kmem_cache_free(client_slab, clp); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1982 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 | } |
| 1984 | |
J. Bruce Fields | 59f8e91 | 2019-03-20 20:03:02 -0400 | [diff] [blame] | 1985 | static void __free_client(struct kref *k) |
| 1986 | { |
J. Bruce Fields | e8a79fb | 2019-03-22 11:11:06 -0400 | [diff] [blame] | 1987 | struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref); |
| 1988 | 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] | 1989 | |
| 1990 | free_svc_cred(&clp->cl_cred); |
| 1991 | kfree(clp->cl_ownerstr_hashtbl); |
| 1992 | kfree(clp->cl_name.data); |
J. Bruce Fields | 7912344 | 2019-06-05 12:42:05 -0400 | [diff] [blame] | 1993 | kfree(clp->cl_nii_domain.data); |
| 1994 | kfree(clp->cl_nii_name.data); |
J. Bruce Fields | 59f8e91 | 2019-03-20 20:03:02 -0400 | [diff] [blame] | 1995 | idr_destroy(&clp->cl_stateids); |
J. Bruce Fields | 59f8e91 | 2019-03-20 20:03:02 -0400 | [diff] [blame] | 1996 | kmem_cache_free(client_slab, clp); |
| 1997 | } |
| 1998 | |
YueHaibing | 297e57a | 2019-07-08 15:29:33 +0800 | [diff] [blame] | 1999 | static void drop_client(struct nfs4_client *clp) |
J. Bruce Fields | 59f8e91 | 2019-03-20 20:03:02 -0400 | [diff] [blame] | 2000 | { |
J. Bruce Fields | e8a79fb | 2019-03-22 11:11:06 -0400 | [diff] [blame] | 2001 | kref_put(&clp->cl_nfsdfs.cl_ref, __free_client); |
J. Bruce Fields | 59f8e91 | 2019-03-20 20:03:02 -0400 | [diff] [blame] | 2002 | } |
| 2003 | |
Trond Myklebust | 4dd86e15 | 2014-04-18 14:43:58 -0400 | [diff] [blame] | 2004 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2005 | free_client(struct nfs4_client *clp) |
| 2006 | { |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 2007 | while (!list_empty(&clp->cl_sessions)) { |
| 2008 | struct nfsd4_session *ses; |
| 2009 | ses = list_entry(clp->cl_sessions.next, struct nfsd4_session, |
| 2010 | se_perclnt); |
| 2011 | list_del(&ses->se_perclnt); |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2012 | WARN_ON_ONCE(atomic_read(&ses->se_ref)); |
| 2013 | free_session(ses); |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 2014 | } |
Trond Myklebust | 4cb57e3 | 2014-04-18 14:43:57 -0400 | [diff] [blame] | 2015 | rpc_destroy_wait_queue(&clp->cl_cb_waitq); |
J. Bruce Fields | 89c905b | 2019-06-19 12:43:11 -0400 | [diff] [blame] | 2016 | if (clp->cl_nfsd_dentry) { |
J. Bruce Fields | e8a79fb | 2019-03-22 11:11:06 -0400 | [diff] [blame] | 2017 | nfsd_client_rmdir(clp->cl_nfsd_dentry); |
J. Bruce Fields | 89c905b | 2019-06-19 12:43:11 -0400 | [diff] [blame] | 2018 | clp->cl_nfsd_dentry = NULL; |
| 2019 | wake_up_all(&expiry_wq); |
| 2020 | } |
J. Bruce Fields | 59f8e91 | 2019-03-20 20:03:02 -0400 | [diff] [blame] | 2021 | drop_client(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2022 | } |
| 2023 | |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 2024 | /* must be called under the client_lock */ |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 2025 | static void |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 2026 | unhash_client_locked(struct nfs4_client *clp) |
| 2027 | { |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 2028 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 2029 | struct nfsd4_session *ses; |
| 2030 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2031 | lockdep_assert_held(&nn->client_lock); |
| 2032 | |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 2033 | /* Mark the client as expired! */ |
| 2034 | clp->cl_time = 0; |
| 2035 | /* Make it invisible */ |
| 2036 | if (!list_empty(&clp->cl_idhash)) { |
| 2037 | list_del_init(&clp->cl_idhash); |
| 2038 | if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags)) |
| 2039 | rb_erase(&clp->cl_namenode, &nn->conf_name_tree); |
| 2040 | else |
| 2041 | rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); |
| 2042 | } |
| 2043 | list_del_init(&clp->cl_lru); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 2044 | spin_lock(&clp->cl_lock); |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 2045 | list_for_each_entry(ses, &clp->cl_sessions, se_perclnt) |
| 2046 | list_del_init(&ses->se_hash); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 2047 | spin_unlock(&clp->cl_lock); |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 2048 | } |
| 2049 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2050 | static void |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 2051 | unhash_client(struct nfs4_client *clp) |
| 2052 | { |
| 2053 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 2054 | |
| 2055 | spin_lock(&nn->client_lock); |
| 2056 | unhash_client_locked(clp); |
| 2057 | spin_unlock(&nn->client_lock); |
| 2058 | } |
| 2059 | |
Jeff Layton | 97403d9 | 2014-07-30 08:27:12 -0400 | [diff] [blame] | 2060 | static __be32 mark_client_expired_locked(struct nfs4_client *clp) |
| 2061 | { |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 2062 | if (atomic_read(&clp->cl_rpc_users)) |
Jeff Layton | 97403d9 | 2014-07-30 08:27:12 -0400 | [diff] [blame] | 2063 | return nfserr_jukebox; |
| 2064 | unhash_client_locked(clp); |
| 2065 | return nfs_ok; |
| 2066 | } |
| 2067 | |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 2068 | static void |
| 2069 | __destroy_client(struct nfs4_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2070 | { |
Jeff Layton | 68ef3bc | 2018-03-16 11:32:02 -0400 | [diff] [blame] | 2071 | int i; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2072 | struct nfs4_openowner *oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2073 | struct nfs4_delegation *dp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2074 | struct list_head reaplist; |
| 2075 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2076 | INIT_LIST_HEAD(&reaplist); |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 2077 | spin_lock(&state_lock); |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 2078 | while (!list_empty(&clp->cl_delegations)) { |
| 2079 | dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt); |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 2080 | WARN_ON(!unhash_delegation_locked(dp)); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 2081 | list_add(&dp->dl_recall_lru, &reaplist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2082 | } |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 2083 | spin_unlock(&state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2084 | while (!list_empty(&reaplist)) { |
| 2085 | dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 2086 | list_del_init(&dp->dl_recall_lru); |
J. Bruce Fields | 0af6e69 | 2018-02-21 15:11:03 -0500 | [diff] [blame] | 2087 | destroy_unhashed_deleg(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | } |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 2089 | while (!list_empty(&clp->cl_revoked)) { |
Andrew Elble | c876486 | 2015-02-25 17:46:27 -0500 | [diff] [blame] | 2090 | 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] | 2091 | list_del_init(&dp->dl_recall_lru); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 2092 | nfs4_put_stid(&dp->dl_stid); |
Benny Halevy | 956c4fe | 2013-10-29 11:39:12 +0200 | [diff] [blame] | 2093 | } |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 2094 | while (!list_empty(&clp->cl_openowners)) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2095 | oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient); |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 2096 | nfs4_get_stateowner(&oo->oo_owner); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2097 | release_openowner(oo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2098 | } |
Jeff Layton | 68ef3bc | 2018-03-16 11:32:02 -0400 | [diff] [blame] | 2099 | for (i = 0; i < OWNER_HASH_SIZE; i++) { |
| 2100 | struct nfs4_stateowner *so, *tmp; |
| 2101 | |
| 2102 | list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i], |
| 2103 | so_strhash) { |
| 2104 | /* Should be no openowners at this point */ |
| 2105 | WARN_ON_ONCE(so->so_is_open_owner); |
| 2106 | remove_blocked_locks(lockowner(so)); |
| 2107 | } |
| 2108 | } |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 2109 | nfsd4_return_all_client_layouts(clp); |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 2110 | nfsd4_shutdown_copy(clp); |
J. Bruce Fields | 6ff8da0 | 2010-06-04 20:04:45 -0400 | [diff] [blame] | 2111 | nfsd4_shutdown_callback(clp); |
J. Bruce Fields | 2bf2387 | 2010-03-08 12:37:27 -0500 | [diff] [blame] | 2112 | if (clp->cl_cb_conn.cb_xprt) |
| 2113 | svc_xprt_put(clp->cl_cb_conn.cb_xprt); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2114 | free_client(clp); |
J. Bruce Fields | 89c905b | 2019-06-19 12:43:11 -0400 | [diff] [blame] | 2115 | wake_up_all(&expiry_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2116 | } |
| 2117 | |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 2118 | static void |
| 2119 | destroy_client(struct nfs4_client *clp) |
| 2120 | { |
| 2121 | unhash_client(clp); |
| 2122 | __destroy_client(clp); |
| 2123 | } |
| 2124 | |
Scott Mayhew | 362063a | 2019-03-26 18:06:28 -0400 | [diff] [blame] | 2125 | static void inc_reclaim_complete(struct nfs4_client *clp) |
| 2126 | { |
| 2127 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 2128 | |
| 2129 | if (!nn->track_reclaim_completes) |
| 2130 | return; |
| 2131 | if (!nfsd4_find_reclaim_client(clp->cl_name, nn)) |
| 2132 | return; |
| 2133 | if (atomic_inc_return(&nn->nr_reclaim_complete) == |
| 2134 | nn->reclaim_str_hashtbl_size) { |
| 2135 | printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n", |
| 2136 | clp->net->ns.inum); |
| 2137 | nfsd4_end_grace(nn); |
| 2138 | } |
| 2139 | } |
| 2140 | |
J. Bruce Fields | 0d22f68 | 2012-09-26 11:36:16 -0400 | [diff] [blame] | 2141 | static void expire_client(struct nfs4_client *clp) |
| 2142 | { |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 2143 | unhash_client(clp); |
J. Bruce Fields | 0d22f68 | 2012-09-26 11:36:16 -0400 | [diff] [blame] | 2144 | nfsd4_client_record_remove(clp); |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 2145 | __destroy_client(clp); |
J. Bruce Fields | 0d22f68 | 2012-09-26 11:36:16 -0400 | [diff] [blame] | 2146 | } |
| 2147 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 2148 | static void copy_verf(struct nfs4_client *target, nfs4_verifier *source) |
| 2149 | { |
| 2150 | memcpy(target->cl_verifier.data, source->data, |
| 2151 | sizeof(target->cl_verifier.data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2152 | } |
| 2153 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 2154 | static void copy_clid(struct nfs4_client *target, struct nfs4_client *source) |
| 2155 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2156 | target->cl_clientid.cl_boot = source->cl_clientid.cl_boot; |
| 2157 | target->cl_clientid.cl_id = source->cl_clientid.cl_id; |
| 2158 | } |
| 2159 | |
J. Bruce Fields | 5004385 | 2015-11-20 15:58:37 -0500 | [diff] [blame] | 2160 | static int copy_cred(struct svc_cred *target, struct svc_cred *source) |
| 2161 | { |
NeilBrown | 2f10fdc | 2017-03-23 16:57:36 +0800 | [diff] [blame] | 2162 | target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL); |
| 2163 | target->cr_raw_principal = kstrdup(source->cr_raw_principal, |
| 2164 | GFP_KERNEL); |
Chuck Lever | 9abdda5 | 2018-08-16 12:05:59 -0400 | [diff] [blame] | 2165 | target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL); |
| 2166 | if ((source->cr_principal && !target->cr_principal) || |
| 2167 | (source->cr_raw_principal && !target->cr_raw_principal) || |
| 2168 | (source->cr_targ_princ && !target->cr_targ_princ)) |
NeilBrown | 2f10fdc | 2017-03-23 16:57:36 +0800 | [diff] [blame] | 2169 | return -ENOMEM; |
J. Bruce Fields | 5004385 | 2015-11-20 15:58:37 -0500 | [diff] [blame] | 2170 | |
J. Bruce Fields | d5497fc | 2012-05-14 22:06:49 -0400 | [diff] [blame] | 2171 | target->cr_flavor = source->cr_flavor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2172 | target->cr_uid = source->cr_uid; |
| 2173 | target->cr_gid = source->cr_gid; |
| 2174 | target->cr_group_info = source->cr_group_info; |
| 2175 | get_group_info(target->cr_group_info); |
J. Bruce Fields | 0dc1531 | 2013-05-14 16:07:13 -0400 | [diff] [blame] | 2176 | target->cr_gss_mech = source->cr_gss_mech; |
| 2177 | if (source->cr_gss_mech) |
| 2178 | gss_mech_get(source->cr_gss_mech); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 2179 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2180 | } |
| 2181 | |
Rasmus Villemoes | ef17af2 | 2014-12-05 16:40:07 +0100 | [diff] [blame] | 2182 | static int |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2183 | compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2) |
| 2184 | { |
Rasmus Villemoes | ef17af2 | 2014-12-05 16:40:07 +0100 | [diff] [blame] | 2185 | if (o1->len < o2->len) |
| 2186 | return -1; |
| 2187 | if (o1->len > o2->len) |
| 2188 | return 1; |
| 2189 | return memcmp(o1->data, o2->data, o1->len); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2190 | } |
| 2191 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2192 | static int |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 2193 | same_verf(nfs4_verifier *v1, nfs4_verifier *v2) |
| 2194 | { |
| 2195 | return 0 == memcmp(v1->data, v2->data, sizeof(v1->data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2196 | } |
| 2197 | |
| 2198 | static int |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 2199 | same_clid(clientid_t *cl1, clientid_t *cl2) |
| 2200 | { |
| 2201 | 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] | 2202 | } |
| 2203 | |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 2204 | static bool groups_equal(struct group_info *g1, struct group_info *g2) |
| 2205 | { |
| 2206 | int i; |
| 2207 | |
| 2208 | if (g1->ngroups != g2->ngroups) |
| 2209 | return false; |
| 2210 | for (i=0; i<g1->ngroups; i++) |
Alexey Dobriyan | 81243ea | 2016-10-07 17:03:12 -0700 | [diff] [blame] | 2211 | if (!gid_eq(g1->gid[i], g2->gid[i])) |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 2212 | return false; |
| 2213 | return true; |
| 2214 | } |
| 2215 | |
J. Bruce Fields | 68eb350 | 2012-08-21 12:48:30 -0400 | [diff] [blame] | 2216 | /* |
| 2217 | * RFC 3530 language requires clid_inuse be returned when the |
| 2218 | * "principal" associated with a requests differs from that previously |
| 2219 | * used. We use uid, gid's, and gss principal string as our best |
| 2220 | * approximation. We also don't want to allow non-gss use of a client |
| 2221 | * established using gss: in theory cr_principal should catch that |
| 2222 | * change, but in practice cr_principal can be null even in the gss case |
| 2223 | * since gssd doesn't always pass down a principal string. |
| 2224 | */ |
| 2225 | static bool is_gss_cred(struct svc_cred *cr) |
| 2226 | { |
| 2227 | /* Is cr_flavor one of the gss "pseudoflavors"?: */ |
| 2228 | return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR); |
| 2229 | } |
| 2230 | |
| 2231 | |
Vivek Trivedi | 5559b50 | 2012-07-24 21:18:20 +0530 | [diff] [blame] | 2232 | static bool |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 2233 | same_creds(struct svc_cred *cr1, struct svc_cred *cr2) |
| 2234 | { |
J. Bruce Fields | 68eb350 | 2012-08-21 12:48:30 -0400 | [diff] [blame] | 2235 | if ((is_gss_cred(cr1) != is_gss_cred(cr2)) |
Eric W. Biederman | 6fab877 | 2013-02-02 06:53:11 -0800 | [diff] [blame] | 2236 | || (!uid_eq(cr1->cr_uid, cr2->cr_uid)) |
| 2237 | || (!gid_eq(cr1->cr_gid, cr2->cr_gid)) |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 2238 | || !groups_equal(cr1->cr_group_info, cr2->cr_group_info)) |
| 2239 | return false; |
Chuck Lever | 9abdda5 | 2018-08-16 12:05:59 -0400 | [diff] [blame] | 2240 | /* XXX: check that cr_targ_princ fields match ? */ |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 2241 | if (cr1->cr_principal == cr2->cr_principal) |
| 2242 | return true; |
| 2243 | if (!cr1->cr_principal || !cr2->cr_principal) |
| 2244 | return false; |
Vivek Trivedi | 5559b50 | 2012-07-24 21:18:20 +0530 | [diff] [blame] | 2245 | return 0 == strcmp(cr1->cr_principal, cr2->cr_principal); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2246 | } |
| 2247 | |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2248 | static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp) |
| 2249 | { |
| 2250 | struct svc_cred *cr = &rqstp->rq_cred; |
| 2251 | u32 service; |
| 2252 | |
J. Bruce Fields | c472059 | 2013-08-07 11:41:49 -0400 | [diff] [blame] | 2253 | if (!cr->cr_gss_mech) |
| 2254 | return false; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2255 | service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor); |
| 2256 | return service == RPC_GSS_SVC_INTEGRITY || |
| 2257 | service == RPC_GSS_SVC_PRIVACY; |
| 2258 | } |
| 2259 | |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 2260 | 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] | 2261 | { |
| 2262 | struct svc_cred *cr = &rqstp->rq_cred; |
| 2263 | |
| 2264 | if (!cl->cl_mach_cred) |
| 2265 | return true; |
| 2266 | if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech) |
| 2267 | return false; |
| 2268 | if (!svc_rqst_integrity_protected(rqstp)) |
| 2269 | return false; |
J. Bruce Fields | 414ca01 | 2015-11-20 10:48:02 -0500 | [diff] [blame] | 2270 | if (cl->cl_cred.cr_raw_principal) |
| 2271 | return 0 == strcmp(cl->cl_cred.cr_raw_principal, |
| 2272 | cr->cr_raw_principal); |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2273 | if (!cr->cr_principal) |
| 2274 | return false; |
| 2275 | return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal); |
| 2276 | } |
| 2277 | |
Jeff Layton | 294ac32 | 2014-07-30 08:27:15 -0400 | [diff] [blame] | 2278 | 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] | 2279 | { |
Chuck Lever | ab4684d | 2012-03-02 17:13:50 -0500 | [diff] [blame] | 2280 | __be32 verf[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2281 | |
Jeff Layton | f419992 | 2014-06-17 07:44:11 -0400 | [diff] [blame] | 2282 | /* |
| 2283 | * This is opaque to client, so no need to byte-swap. Use |
| 2284 | * __force to keep sparse happy |
| 2285 | */ |
Arnd Bergmann | 9104ae4 | 2019-11-04 16:45:30 +0100 | [diff] [blame] | 2286 | verf[0] = (__force __be32)(u32)ktime_get_real_seconds(); |
Kinglong Mee | 19311aa8 | 2015-07-18 07:33:31 +0800 | [diff] [blame] | 2287 | verf[1] = (__force __be32)nn->clverifier_counter++; |
Chuck Lever | ab4684d | 2012-03-02 17:13:50 -0500 | [diff] [blame] | 2288 | memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2289 | } |
| 2290 | |
Jeff Layton | 294ac32 | 2014-07-30 08:27:15 -0400 | [diff] [blame] | 2291 | static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn) |
| 2292 | { |
Arnd Bergmann | 9cc7680 | 2019-10-31 15:53:13 +0100 | [diff] [blame] | 2293 | clp->cl_clientid.cl_boot = (u32)nn->boot_time; |
Jeff Layton | 294ac32 | 2014-07-30 08:27:15 -0400 | [diff] [blame] | 2294 | clp->cl_clientid.cl_id = nn->clientid_counter++; |
| 2295 | gen_confirm(clp, nn); |
| 2296 | } |
| 2297 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 2298 | static struct nfs4_stid * |
| 2299 | find_stateid_locked(struct nfs4_client *cl, stateid_t *t) |
J. Bruce Fields | 4581d14 | 2011-09-06 14:56:09 -0400 | [diff] [blame] | 2300 | { |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 2301 | struct nfs4_stid *ret; |
| 2302 | |
| 2303 | ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id); |
| 2304 | if (!ret || !ret->sc_type) |
| 2305 | return NULL; |
| 2306 | return ret; |
J. Bruce Fields | 4d71ab8 | 2011-09-06 16:48:57 -0400 | [diff] [blame] | 2307 | } |
| 2308 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 2309 | static struct nfs4_stid * |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 2310 | 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] | 2311 | { |
| 2312 | struct nfs4_stid *s; |
| 2313 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 2314 | spin_lock(&cl->cl_lock); |
| 2315 | s = find_stateid_locked(cl, t); |
Trond Myklebust | 2d3f966 | 2014-07-29 21:34:25 -0400 | [diff] [blame] | 2316 | if (s != NULL) { |
| 2317 | if (typemask & s->sc_type) |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 2318 | refcount_inc(&s->sc_count); |
Trond Myklebust | 2d3f966 | 2014-07-29 21:34:25 -0400 | [diff] [blame] | 2319 | else |
| 2320 | s = NULL; |
| 2321 | } |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 2322 | spin_unlock(&cl->cl_lock); |
| 2323 | return s; |
J. Bruce Fields | 4581d14 | 2011-09-06 14:56:09 -0400 | [diff] [blame] | 2324 | } |
| 2325 | |
J. Bruce Fields | a204f25 | 2019-06-19 12:54:45 -0400 | [diff] [blame] | 2326 | static struct nfs4_client *get_nfsdfs_clp(struct inode *inode) |
| 2327 | { |
| 2328 | struct nfsdfs_client *nc; |
| 2329 | nc = get_nfsdfs_client(inode); |
| 2330 | if (!nc) |
| 2331 | return NULL; |
| 2332 | return container_of(nc, struct nfs4_client, cl_nfsdfs); |
| 2333 | } |
| 2334 | |
J. Bruce Fields | 169319f | 2019-06-19 12:39:46 -0400 | [diff] [blame] | 2335 | static void seq_quote_mem(struct seq_file *m, char *data, int len) |
| 2336 | { |
| 2337 | seq_printf(m, "\""); |
| 2338 | seq_escape_mem_ascii(m, data, len); |
| 2339 | seq_printf(m, "\""); |
| 2340 | } |
| 2341 | |
J. Bruce Fields | 97ad403 | 2019-04-09 15:56:57 -0400 | [diff] [blame] | 2342 | static int client_info_show(struct seq_file *m, void *v) |
| 2343 | { |
| 2344 | struct inode *inode = m->private; |
J. Bruce Fields | 97ad403 | 2019-04-09 15:56:57 -0400 | [diff] [blame] | 2345 | struct nfs4_client *clp; |
| 2346 | u64 clid; |
| 2347 | |
J. Bruce Fields | a204f25 | 2019-06-19 12:54:45 -0400 | [diff] [blame] | 2348 | clp = get_nfsdfs_clp(inode); |
| 2349 | if (!clp) |
J. Bruce Fields | 97ad403 | 2019-04-09 15:56:57 -0400 | [diff] [blame] | 2350 | return -ENXIO; |
J. Bruce Fields | 97ad403 | 2019-04-09 15:56:57 -0400 | [diff] [blame] | 2351 | memcpy(&clid, &clp->cl_clientid, sizeof(clid)); |
| 2352 | seq_printf(m, "clientid: 0x%llx\n", clid); |
J. Bruce Fields | 169319f | 2019-06-19 12:39:46 -0400 | [diff] [blame] | 2353 | seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr); |
NeilBrown | 472d155 | 2021-03-20 09:38:04 +1100 | [diff] [blame] | 2354 | if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags)) |
| 2355 | seq_puts(m, "status: confirmed\n"); |
| 2356 | else |
| 2357 | seq_puts(m, "status: unconfirmed\n"); |
J. Bruce Fields | 169319f | 2019-06-19 12:39:46 -0400 | [diff] [blame] | 2358 | seq_printf(m, "name: "); |
| 2359 | seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len); |
| 2360 | seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion); |
J. Bruce Fields | 7912344 | 2019-06-05 12:42:05 -0400 | [diff] [blame] | 2361 | if (clp->cl_nii_domain.data) { |
| 2362 | seq_printf(m, "Implementation domain: "); |
| 2363 | seq_quote_mem(m, clp->cl_nii_domain.data, |
| 2364 | clp->cl_nii_domain.len); |
| 2365 | seq_printf(m, "\nImplementation name: "); |
| 2366 | 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] | 2367 | seq_printf(m, "\nImplementation time: [%lld, %ld]\n", |
J. Bruce Fields | 7912344 | 2019-06-05 12:42:05 -0400 | [diff] [blame] | 2368 | clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec); |
| 2369 | } |
J. Bruce Fields | 97ad403 | 2019-04-09 15:56:57 -0400 | [diff] [blame] | 2370 | drop_client(clp); |
| 2371 | |
| 2372 | return 0; |
| 2373 | } |
| 2374 | |
| 2375 | static int client_info_open(struct inode *inode, struct file *file) |
| 2376 | { |
| 2377 | return single_open(file, client_info_show, inode); |
| 2378 | } |
| 2379 | |
| 2380 | static const struct file_operations client_info_fops = { |
| 2381 | .open = client_info_open, |
| 2382 | .read = seq_read, |
| 2383 | .llseek = seq_lseek, |
| 2384 | .release = single_release, |
| 2385 | }; |
| 2386 | |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2387 | static void *states_start(struct seq_file *s, loff_t *pos) |
| 2388 | __acquires(&clp->cl_lock) |
| 2389 | { |
| 2390 | struct nfs4_client *clp = s->private; |
| 2391 | unsigned long id = *pos; |
| 2392 | void *ret; |
| 2393 | |
| 2394 | spin_lock(&clp->cl_lock); |
| 2395 | ret = idr_get_next_ul(&clp->cl_stateids, &id); |
| 2396 | *pos = id; |
| 2397 | return ret; |
| 2398 | } |
| 2399 | |
| 2400 | static void *states_next(struct seq_file *s, void *v, loff_t *pos) |
| 2401 | { |
| 2402 | struct nfs4_client *clp = s->private; |
| 2403 | unsigned long id = *pos; |
| 2404 | void *ret; |
| 2405 | |
| 2406 | id = *pos; |
| 2407 | id++; |
| 2408 | ret = idr_get_next_ul(&clp->cl_stateids, &id); |
| 2409 | *pos = id; |
| 2410 | return ret; |
| 2411 | } |
| 2412 | |
| 2413 | static void states_stop(struct seq_file *s, void *v) |
| 2414 | __releases(&clp->cl_lock) |
| 2415 | { |
| 2416 | struct nfs4_client *clp = s->private; |
| 2417 | |
| 2418 | spin_unlock(&clp->cl_lock); |
| 2419 | } |
| 2420 | |
Achilles Gaikwad | 580da46 | 2020-04-20 18:20:31 +0530 | [diff] [blame] | 2421 | static void nfs4_show_fname(struct seq_file *s, struct nfsd_file *f) |
| 2422 | { |
| 2423 | seq_printf(s, "filename: \"%pD2\"", f->nf_file); |
| 2424 | } |
| 2425 | |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 2426 | 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] | 2427 | { |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 2428 | struct inode *inode = f->nf_inode; |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2429 | |
| 2430 | seq_printf(s, "superblock: \"%02x:%02x:%ld\"", |
| 2431 | MAJOR(inode->i_sb->s_dev), |
| 2432 | MINOR(inode->i_sb->s_dev), |
| 2433 | inode->i_ino); |
| 2434 | } |
| 2435 | |
| 2436 | static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo) |
| 2437 | { |
| 2438 | seq_printf(s, "owner: "); |
| 2439 | seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len); |
| 2440 | } |
| 2441 | |
J. Bruce Fields | ace7ade | 2020-04-13 21:37:19 -0400 | [diff] [blame] | 2442 | static void nfs4_show_stateid(struct seq_file *s, stateid_t *stid) |
| 2443 | { |
J. Bruce Fields | ee590d2 | 2020-04-13 22:03:06 -0400 | [diff] [blame] | 2444 | seq_printf(s, "0x%.8x", stid->si_generation); |
| 2445 | seq_printf(s, "%12phN", &stid->si_opaque); |
J. Bruce Fields | ace7ade | 2020-04-13 21:37:19 -0400 | [diff] [blame] | 2446 | } |
| 2447 | |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2448 | static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st) |
| 2449 | { |
| 2450 | struct nfs4_ol_stateid *ols; |
| 2451 | struct nfs4_file *nf; |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 2452 | struct nfsd_file *file; |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2453 | struct nfs4_stateowner *oo; |
| 2454 | unsigned int access, deny; |
| 2455 | |
| 2456 | if (st->sc_type != NFS4_OPEN_STID && st->sc_type != NFS4_LOCK_STID) |
| 2457 | return 0; /* XXX: or SEQ_SKIP? */ |
| 2458 | ols = openlockstateid(st); |
| 2459 | oo = ols->st_stateowner; |
| 2460 | nf = st->sc_file; |
| 2461 | file = find_any_file(nf); |
J. Bruce Fields | 9affa43 | 2020-07-15 13:31:36 -0400 | [diff] [blame] | 2462 | if (!file) |
| 2463 | return 0; |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2464 | |
J. Bruce Fields | ace7ade | 2020-04-13 21:37:19 -0400 | [diff] [blame] | 2465 | seq_printf(s, "- "); |
| 2466 | nfs4_show_stateid(s, &st->sc_stateid); |
| 2467 | seq_printf(s, ": { type: open, "); |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2468 | |
| 2469 | access = bmap_to_share_mode(ols->st_access_bmap); |
| 2470 | deny = bmap_to_share_mode(ols->st_deny_bmap); |
| 2471 | |
J. Bruce Fields | c4b77ed | 2019-10-03 12:33:08 -0400 | [diff] [blame] | 2472 | seq_printf(s, "access: %s%s, ", |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2473 | access & NFS4_SHARE_ACCESS_READ ? "r" : "-", |
| 2474 | access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-"); |
J. Bruce Fields | c4b77ed | 2019-10-03 12:33:08 -0400 | [diff] [blame] | 2475 | seq_printf(s, "deny: %s%s, ", |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2476 | deny & NFS4_SHARE_ACCESS_READ ? "r" : "-", |
| 2477 | deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-"); |
| 2478 | |
| 2479 | nfs4_show_superblock(s, file); |
| 2480 | seq_printf(s, ", "); |
Achilles Gaikwad | 580da46 | 2020-04-20 18:20:31 +0530 | [diff] [blame] | 2481 | nfs4_show_fname(s, file); |
| 2482 | seq_printf(s, ", "); |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2483 | nfs4_show_owner(s, oo); |
| 2484 | seq_printf(s, " }\n"); |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 2485 | nfsd_file_put(file); |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2486 | |
| 2487 | return 0; |
| 2488 | } |
| 2489 | |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2490 | static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st) |
| 2491 | { |
| 2492 | struct nfs4_ol_stateid *ols; |
| 2493 | struct nfs4_file *nf; |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 2494 | struct nfsd_file *file; |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2495 | struct nfs4_stateowner *oo; |
| 2496 | |
| 2497 | ols = openlockstateid(st); |
| 2498 | oo = ols->st_stateowner; |
| 2499 | nf = st->sc_file; |
| 2500 | file = find_any_file(nf); |
J. Bruce Fields | 9affa43 | 2020-07-15 13:31:36 -0400 | [diff] [blame] | 2501 | if (!file) |
| 2502 | return 0; |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2503 | |
J. Bruce Fields | ace7ade | 2020-04-13 21:37:19 -0400 | [diff] [blame] | 2504 | seq_printf(s, "- "); |
| 2505 | nfs4_show_stateid(s, &st->sc_stateid); |
| 2506 | seq_printf(s, ": { type: lock, "); |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2507 | |
| 2508 | /* |
| 2509 | * Note: a lock stateid isn't really the same thing as a lock, |
| 2510 | * it's the locking state held by one owner on a file, and there |
| 2511 | * may be multiple (or no) lock ranges associated with it. |
| 2512 | * (Same for the matter is true of open stateids.) |
| 2513 | */ |
| 2514 | |
| 2515 | nfs4_show_superblock(s, file); |
| 2516 | /* XXX: open stateid? */ |
| 2517 | seq_printf(s, ", "); |
Achilles Gaikwad | 580da46 | 2020-04-20 18:20:31 +0530 | [diff] [blame] | 2518 | nfs4_show_fname(s, file); |
| 2519 | seq_printf(s, ", "); |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2520 | nfs4_show_owner(s, oo); |
| 2521 | seq_printf(s, " }\n"); |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 2522 | nfsd_file_put(file); |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2523 | |
| 2524 | return 0; |
| 2525 | } |
| 2526 | |
| 2527 | static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st) |
| 2528 | { |
| 2529 | struct nfs4_delegation *ds; |
| 2530 | struct nfs4_file *nf; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 2531 | struct nfsd_file *file; |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2532 | |
| 2533 | ds = delegstateid(st); |
| 2534 | nf = st->sc_file; |
J. Bruce Fields | 9affa43 | 2020-07-15 13:31:36 -0400 | [diff] [blame] | 2535 | file = find_deleg_file(nf); |
| 2536 | if (!file) |
| 2537 | return 0; |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2538 | |
J. Bruce Fields | ace7ade | 2020-04-13 21:37:19 -0400 | [diff] [blame] | 2539 | seq_printf(s, "- "); |
| 2540 | nfs4_show_stateid(s, &st->sc_stateid); |
| 2541 | seq_printf(s, ": { type: deleg, "); |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2542 | |
| 2543 | /* Kinda dead code as long as we only support read delegs: */ |
| 2544 | seq_printf(s, "access: %s, ", |
| 2545 | ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w"); |
| 2546 | |
| 2547 | /* XXX: lease time, whether it's being recalled. */ |
| 2548 | |
| 2549 | nfs4_show_superblock(s, file); |
Achilles Gaikwad | 580da46 | 2020-04-20 18:20:31 +0530 | [diff] [blame] | 2550 | seq_printf(s, ", "); |
| 2551 | nfs4_show_fname(s, file); |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2552 | seq_printf(s, " }\n"); |
J. Bruce Fields | 9affa43 | 2020-07-15 13:31:36 -0400 | [diff] [blame] | 2553 | nfsd_file_put(file); |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2554 | |
| 2555 | return 0; |
| 2556 | } |
| 2557 | |
J. Bruce Fields | 0c4b62b | 2019-05-14 15:52:57 -0400 | [diff] [blame] | 2558 | static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st) |
| 2559 | { |
| 2560 | struct nfs4_layout_stateid *ls; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 2561 | struct nfsd_file *file; |
J. Bruce Fields | 0c4b62b | 2019-05-14 15:52:57 -0400 | [diff] [blame] | 2562 | |
| 2563 | ls = container_of(st, struct nfs4_layout_stateid, ls_stid); |
| 2564 | file = ls->ls_file; |
| 2565 | |
J. Bruce Fields | ace7ade | 2020-04-13 21:37:19 -0400 | [diff] [blame] | 2566 | seq_printf(s, "- "); |
| 2567 | nfs4_show_stateid(s, &st->sc_stateid); |
| 2568 | seq_printf(s, ": { type: layout, "); |
J. Bruce Fields | 0c4b62b | 2019-05-14 15:52:57 -0400 | [diff] [blame] | 2569 | |
| 2570 | /* XXX: What else would be useful? */ |
| 2571 | |
| 2572 | nfs4_show_superblock(s, file); |
Achilles Gaikwad | 580da46 | 2020-04-20 18:20:31 +0530 | [diff] [blame] | 2573 | seq_printf(s, ", "); |
| 2574 | nfs4_show_fname(s, file); |
J. Bruce Fields | 0c4b62b | 2019-05-14 15:52:57 -0400 | [diff] [blame] | 2575 | seq_printf(s, " }\n"); |
| 2576 | |
| 2577 | return 0; |
| 2578 | } |
| 2579 | |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2580 | static int states_show(struct seq_file *s, void *v) |
| 2581 | { |
| 2582 | struct nfs4_stid *st = v; |
| 2583 | |
| 2584 | switch (st->sc_type) { |
| 2585 | case NFS4_OPEN_STID: |
| 2586 | return nfs4_show_open(s, st); |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2587 | case NFS4_LOCK_STID: |
| 2588 | return nfs4_show_lock(s, st); |
| 2589 | case NFS4_DELEG_STID: |
| 2590 | return nfs4_show_deleg(s, st); |
J. Bruce Fields | 0c4b62b | 2019-05-14 15:52:57 -0400 | [diff] [blame] | 2591 | case NFS4_LAYOUT_STID: |
| 2592 | return nfs4_show_layout(s, st); |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2593 | default: |
| 2594 | return 0; /* XXX: or SEQ_SKIP? */ |
| 2595 | } |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2596 | /* XXX: copy stateids? */ |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2597 | } |
| 2598 | |
| 2599 | static struct seq_operations states_seq_ops = { |
| 2600 | .start = states_start, |
| 2601 | .next = states_next, |
| 2602 | .stop = states_stop, |
| 2603 | .show = states_show |
| 2604 | }; |
| 2605 | |
| 2606 | static int client_states_open(struct inode *inode, struct file *file) |
| 2607 | { |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2608 | struct seq_file *s; |
| 2609 | struct nfs4_client *clp; |
| 2610 | int ret; |
| 2611 | |
J. Bruce Fields | a204f25 | 2019-06-19 12:54:45 -0400 | [diff] [blame] | 2612 | clp = get_nfsdfs_clp(inode); |
| 2613 | if (!clp) |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2614 | return -ENXIO; |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2615 | |
| 2616 | ret = seq_open(file, &states_seq_ops); |
| 2617 | if (ret) |
| 2618 | return ret; |
| 2619 | s = file->private_data; |
| 2620 | s->private = clp; |
| 2621 | return 0; |
| 2622 | } |
| 2623 | |
| 2624 | static int client_opens_release(struct inode *inode, struct file *file) |
| 2625 | { |
| 2626 | struct seq_file *m = file->private_data; |
| 2627 | struct nfs4_client *clp = m->private; |
| 2628 | |
| 2629 | /* XXX: alternatively, we could get/drop in seq start/stop */ |
| 2630 | drop_client(clp); |
| 2631 | return 0; |
| 2632 | } |
| 2633 | |
| 2634 | static const struct file_operations client_states_fops = { |
| 2635 | .open = client_states_open, |
| 2636 | .read = seq_read, |
| 2637 | .llseek = seq_lseek, |
| 2638 | .release = client_opens_release, |
| 2639 | }; |
| 2640 | |
J. Bruce Fields | 89c905b | 2019-06-19 12:43:11 -0400 | [diff] [blame] | 2641 | /* |
| 2642 | * Normally we refuse to destroy clients that are in use, but here the |
| 2643 | * administrator is telling us to just do it. We also want to wait |
| 2644 | * so the caller has a guarantee that the client's locks are gone by |
| 2645 | * the time the write returns: |
| 2646 | */ |
YueHaibing | 297e57a | 2019-07-08 15:29:33 +0800 | [diff] [blame] | 2647 | static void force_expire_client(struct nfs4_client *clp) |
J. Bruce Fields | 89c905b | 2019-06-19 12:43:11 -0400 | [diff] [blame] | 2648 | { |
| 2649 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 2650 | bool already_expired; |
| 2651 | |
| 2652 | spin_lock(&clp->cl_lock); |
| 2653 | clp->cl_time = 0; |
| 2654 | spin_unlock(&clp->cl_lock); |
| 2655 | |
| 2656 | wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0); |
| 2657 | spin_lock(&nn->client_lock); |
| 2658 | already_expired = list_empty(&clp->cl_lru); |
| 2659 | if (!already_expired) |
| 2660 | unhash_client_locked(clp); |
| 2661 | spin_unlock(&nn->client_lock); |
| 2662 | |
| 2663 | if (!already_expired) |
| 2664 | expire_client(clp); |
| 2665 | else |
| 2666 | wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL); |
| 2667 | } |
| 2668 | |
| 2669 | static ssize_t client_ctl_write(struct file *file, const char __user *buf, |
| 2670 | size_t size, loff_t *pos) |
| 2671 | { |
| 2672 | char *data; |
| 2673 | struct nfs4_client *clp; |
| 2674 | |
| 2675 | data = simple_transaction_get(file, buf, size); |
| 2676 | if (IS_ERR(data)) |
| 2677 | return PTR_ERR(data); |
| 2678 | if (size != 7 || 0 != memcmp(data, "expire\n", 7)) |
| 2679 | return -EINVAL; |
| 2680 | clp = get_nfsdfs_clp(file_inode(file)); |
| 2681 | if (!clp) |
| 2682 | return -ENXIO; |
| 2683 | force_expire_client(clp); |
| 2684 | drop_client(clp); |
| 2685 | return 7; |
| 2686 | } |
| 2687 | |
| 2688 | static const struct file_operations client_ctl_fops = { |
| 2689 | .write = client_ctl_write, |
| 2690 | .release = simple_transaction_release, |
| 2691 | }; |
| 2692 | |
J. Bruce Fields | 97ad403 | 2019-04-09 15:56:57 -0400 | [diff] [blame] | 2693 | static const struct tree_descr client_files[] = { |
| 2694 | [0] = {"info", &client_info_fops, S_IRUSR}, |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2695 | [1] = {"states", &client_states_fops, S_IRUSR}, |
Petr Vorel | 6cbfad5 | 2020-03-13 13:39:57 +0100 | [diff] [blame] | 2696 | [2] = {"ctl", &client_ctl_fops, S_IWUSR}, |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2697 | [3] = {""}, |
J. Bruce Fields | 97ad403 | 2019-04-09 15:56:57 -0400 | [diff] [blame] | 2698 | }; |
| 2699 | |
Jeff Layton | 2216d44 | 2012-11-12 15:00:57 -0500 | [diff] [blame] | 2700 | static struct nfs4_client *create_client(struct xdr_netobj name, |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2701 | struct svc_rqst *rqstp, nfs4_verifier *verf) |
| 2702 | { |
| 2703 | struct nfs4_client *clp; |
| 2704 | struct sockaddr *sa = svc_addr(rqstp); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 2705 | int ret; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2706 | struct net *net = SVC_NET(rqstp); |
J. Bruce Fields | e8a79fb | 2019-03-22 11:11:06 -0400 | [diff] [blame] | 2707 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
NeilBrown | 472d155 | 2021-03-20 09:38:04 +1100 | [diff] [blame] | 2708 | struct dentry *dentries[ARRAY_SIZE(client_files)]; |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2709 | |
| 2710 | clp = alloc_client(name); |
| 2711 | if (clp == NULL) |
| 2712 | return NULL; |
| 2713 | |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 2714 | ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred); |
| 2715 | if (ret) { |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 2716 | free_client(clp); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 2717 | return NULL; |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2718 | } |
J. Bruce Fields | e8a79fb | 2019-03-22 11:11:06 -0400 | [diff] [blame] | 2719 | gen_clid(clp, nn); |
| 2720 | kref_init(&clp->cl_nfsdfs.cl_ref); |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 2721 | 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] | 2722 | clp->cl_time = ktime_get_boottime_seconds(); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2723 | clear_bit(0, &clp->cl_cb_slot_busy); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2724 | copy_verf(clp, verf); |
J. Bruce Fields | 3bade24 | 2019-05-14 21:38:11 -0400 | [diff] [blame] | 2725 | memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage)); |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 2726 | clp->cl_cb_session = NULL; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2727 | clp->net = net; |
NeilBrown | 472d155 | 2021-03-20 09:38:04 +1100 | [diff] [blame] | 2728 | clp->cl_nfsd_dentry = nfsd_client_mkdir( |
| 2729 | nn, &clp->cl_nfsdfs, |
| 2730 | clp->cl_clientid.cl_id - nn->clientid_base, |
| 2731 | client_files, dentries); |
| 2732 | clp->cl_nfsd_info_dentry = dentries[0]; |
J. Bruce Fields | e8a79fb | 2019-03-22 11:11:06 -0400 | [diff] [blame] | 2733 | if (!clp->cl_nfsd_dentry) { |
| 2734 | free_client(clp); |
| 2735 | return NULL; |
| 2736 | } |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2737 | return clp; |
| 2738 | } |
| 2739 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 2740 | static void |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2741 | add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root) |
| 2742 | { |
| 2743 | struct rb_node **new = &(root->rb_node), *parent = NULL; |
| 2744 | struct nfs4_client *clp; |
| 2745 | |
| 2746 | while (*new) { |
| 2747 | clp = rb_entry(*new, struct nfs4_client, cl_namenode); |
| 2748 | parent = *new; |
| 2749 | |
| 2750 | if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0) |
| 2751 | new = &((*new)->rb_left); |
| 2752 | else |
| 2753 | new = &((*new)->rb_right); |
| 2754 | } |
| 2755 | |
| 2756 | rb_link_node(&new_clp->cl_namenode, parent, new); |
| 2757 | rb_insert_color(&new_clp->cl_namenode, root); |
| 2758 | } |
| 2759 | |
| 2760 | static struct nfs4_client * |
| 2761 | find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root) |
| 2762 | { |
Rasmus Villemoes | ef17af2 | 2014-12-05 16:40:07 +0100 | [diff] [blame] | 2763 | int cmp; |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2764 | struct rb_node *node = root->rb_node; |
| 2765 | struct nfs4_client *clp; |
| 2766 | |
| 2767 | while (node) { |
| 2768 | clp = rb_entry(node, struct nfs4_client, cl_namenode); |
| 2769 | cmp = compare_blob(&clp->cl_name, name); |
| 2770 | if (cmp > 0) |
| 2771 | node = node->rb_left; |
| 2772 | else if (cmp < 0) |
| 2773 | node = node->rb_right; |
| 2774 | else |
| 2775 | return clp; |
| 2776 | } |
| 2777 | return NULL; |
| 2778 | } |
| 2779 | |
| 2780 | static void |
| 2781 | add_to_unconfirmed(struct nfs4_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2782 | { |
| 2783 | unsigned int idhashval; |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 2784 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2785 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2786 | lockdep_assert_held(&nn->client_lock); |
| 2787 | |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2788 | clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2789 | add_clp_to_name_tree(clp, &nn->unconf_name_tree); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2790 | idhashval = clientid_hashval(clp->cl_clientid.cl_id); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 2791 | list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]); |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2792 | renew_client_locked(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2793 | } |
| 2794 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 2795 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2796 | move_to_confirmed(struct nfs4_client *clp) |
| 2797 | { |
| 2798 | unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2799 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2800 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2801 | lockdep_assert_held(&nn->client_lock); |
| 2802 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2803 | dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2804 | list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]); |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2805 | rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2806 | add_clp_to_name_tree(clp, &nn->conf_name_tree); |
NeilBrown | 472d155 | 2021-03-20 09:38:04 +1100 | [diff] [blame] | 2807 | if (!test_and_set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags) && |
| 2808 | clp->cl_nfsd_dentry && |
| 2809 | clp->cl_nfsd_info_dentry) |
| 2810 | fsnotify_dentry(clp->cl_nfsd_info_dentry, FS_MODIFY); |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2811 | renew_client_locked(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2812 | } |
| 2813 | |
| 2814 | static struct nfs4_client * |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2815 | 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] | 2816 | { |
| 2817 | struct nfs4_client *clp; |
| 2818 | unsigned int idhashval = clientid_hashval(clid->cl_id); |
| 2819 | |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2820 | list_for_each_entry(clp, &tbl[idhashval], cl_idhash) { |
J. Bruce Fields | a50d2ad | 2011-10-12 16:24:27 -0400 | [diff] [blame] | 2821 | if (same_clid(&clp->cl_clientid, clid)) { |
J. Bruce Fields | d15c077 | 2012-09-13 16:19:31 -0400 | [diff] [blame] | 2822 | if ((bool)clp->cl_minorversion != sessions) |
| 2823 | return NULL; |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2824 | renew_client_locked(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2825 | return clp; |
J. Bruce Fields | a50d2ad | 2011-10-12 16:24:27 -0400 | [diff] [blame] | 2826 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2827 | } |
| 2828 | return NULL; |
| 2829 | } |
| 2830 | |
| 2831 | static struct nfs4_client * |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2832 | find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) |
| 2833 | { |
| 2834 | struct list_head *tbl = nn->conf_id_hashtbl; |
| 2835 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2836 | lockdep_assert_held(&nn->client_lock); |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2837 | return find_client_in_id_table(tbl, clid, sessions); |
| 2838 | } |
| 2839 | |
| 2840 | static struct nfs4_client * |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 2841 | find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2842 | { |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2843 | struct list_head *tbl = nn->unconf_id_hashtbl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2844 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2845 | lockdep_assert_held(&nn->client_lock); |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2846 | return find_client_in_id_table(tbl, clid, sessions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2847 | } |
| 2848 | |
J. Bruce Fields | 6e5f15c | 2010-11-24 17:17:34 -0500 | [diff] [blame] | 2849 | static bool clp_used_exchangeid(struct nfs4_client *clp) |
Andy Adamson | a1bcecd | 2009-04-03 08:28:05 +0300 | [diff] [blame] | 2850 | { |
J. Bruce Fields | 6e5f15c | 2010-11-24 17:17:34 -0500 | [diff] [blame] | 2851 | return clp->cl_exchange_flags != 0; |
J. Bruce Fields | e203d50 | 2010-11-24 17:30:54 -0500 | [diff] [blame] | 2852 | } |
Andy Adamson | a1bcecd | 2009-04-03 08:28:05 +0300 | [diff] [blame] | 2853 | |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2854 | static struct nfs4_client * |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2855 | find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2856 | { |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2857 | lockdep_assert_held(&nn->client_lock); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2858 | return find_clp_in_name_tree(name, &nn->conf_name_tree); |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2859 | } |
| 2860 | |
| 2861 | static struct nfs4_client * |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2862 | find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2863 | { |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2864 | lockdep_assert_held(&nn->client_lock); |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2865 | return find_clp_in_name_tree(name, &nn->unconf_name_tree); |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2866 | } |
| 2867 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 2868 | static void |
Takuma Umeya | 6f3d772 | 2010-12-15 14:09:01 +0900 | [diff] [blame] | 2869 | 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] | 2870 | { |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2871 | struct nfs4_cb_conn *conn = &clp->cl_cb_conn; |
Takuma Umeya | 6f3d772 | 2010-12-15 14:09:01 +0900 | [diff] [blame] | 2872 | struct sockaddr *sa = svc_addr(rqstp); |
| 2873 | u32 scopeid = rpc_get_scope_id(sa); |
Jeff Layton | 7077ecb | 2009-08-14 12:57:58 -0400 | [diff] [blame] | 2874 | unsigned short expected_family; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2875 | |
Jeff Layton | 7077ecb | 2009-08-14 12:57:58 -0400 | [diff] [blame] | 2876 | /* Currently, we only support tcp and tcp6 for the callback channel */ |
| 2877 | if (se->se_callback_netid_len == 3 && |
| 2878 | !memcmp(se->se_callback_netid_val, "tcp", 3)) |
| 2879 | expected_family = AF_INET; |
| 2880 | else if (se->se_callback_netid_len == 4 && |
| 2881 | !memcmp(se->se_callback_netid_val, "tcp6", 4)) |
| 2882 | expected_family = AF_INET6; |
| 2883 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2884 | goto out_err; |
| 2885 | |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2886 | conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val, |
Jeff Layton | aa9a4ec | 2009-08-14 12:57:57 -0400 | [diff] [blame] | 2887 | se->se_callback_addr_len, |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2888 | (struct sockaddr *)&conn->cb_addr, |
| 2889 | sizeof(conn->cb_addr)); |
Jeff Layton | aa9a4ec | 2009-08-14 12:57:57 -0400 | [diff] [blame] | 2890 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2891 | if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2892 | goto out_err; |
Jeff Layton | aa9a4ec | 2009-08-14 12:57:57 -0400 | [diff] [blame] | 2893 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2894 | if (conn->cb_addr.ss_family == AF_INET6) |
| 2895 | ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid; |
Jeff Layton | fbf4665 | 2009-08-14 12:57:59 -0400 | [diff] [blame] | 2896 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2897 | conn->cb_prog = se->se_callback_prog; |
| 2898 | conn->cb_ident = se->se_callback_ident; |
Mi Jinlong | 849a1cf | 2011-08-30 17:18:41 +0800 | [diff] [blame] | 2899 | memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen); |
Chuck Lever | 1eace0d | 2020-04-05 14:15:29 -0400 | [diff] [blame] | 2900 | trace_nfsd_cb_args(clp, conn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2901 | return; |
| 2902 | out_err: |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2903 | conn->cb_addr.ss_family = AF_UNSPEC; |
| 2904 | conn->cb_addrlen = 0; |
Chuck Lever | 1eace0d | 2020-04-05 14:15:29 -0400 | [diff] [blame] | 2905 | trace_nfsd_cb_nodelegs(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2906 | return; |
| 2907 | } |
| 2908 | |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2909 | /* |
J. Bruce Fields | 067e1ac | 2014-03-21 17:26:44 -0400 | [diff] [blame] | 2910 | * Cache a reply. nfsd4_check_resp_size() has bounded the cache size. |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2911 | */ |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 2912 | static void |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2913 | nfsd4_store_cache_entry(struct nfsd4_compoundres *resp) |
| 2914 | { |
Chuck Lever | bddfdbc | 2020-10-27 15:53:42 -0400 | [diff] [blame] | 2915 | struct xdr_buf *buf = resp->xdr->buf; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2916 | struct nfsd4_slot *slot = resp->cstate.slot; |
| 2917 | unsigned int base; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2918 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2919 | dprintk("--> %s slot %p\n", __func__, slot); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2920 | |
J. Bruce Fields | 085def3 | 2017-10-18 16:17:18 -0400 | [diff] [blame] | 2921 | slot->sl_flags |= NFSD4_SLOT_INITIALIZED; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2922 | slot->sl_opcnt = resp->opcnt; |
| 2923 | slot->sl_status = resp->cstate.status; |
J. Bruce Fields | 53da6a5 | 2017-10-17 20:38:49 -0400 | [diff] [blame] | 2924 | free_svc_cred(&slot->sl_cred); |
| 2925 | copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred); |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 2926 | |
J. Bruce Fields | 085def3 | 2017-10-18 16:17:18 -0400 | [diff] [blame] | 2927 | if (!nfsd4_cache_this(resp)) { |
| 2928 | slot->sl_flags &= ~NFSD4_SLOT_CACHED; |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 2929 | return; |
| 2930 | } |
J. Bruce Fields | 085def3 | 2017-10-18 16:17:18 -0400 | [diff] [blame] | 2931 | slot->sl_flags |= NFSD4_SLOT_CACHED; |
| 2932 | |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 2933 | base = resp->cstate.data_offset; |
| 2934 | slot->sl_datalen = buf->len - base; |
| 2935 | 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] | 2936 | WARN(1, "%s: sessions DRC could not cache compound\n", |
| 2937 | __func__); |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2938 | return; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2939 | } |
| 2940 | |
| 2941 | /* |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2942 | * Encode the replay sequence operation from the slot values. |
| 2943 | * If cachethis is FALSE encode the uncached rep error on the next |
| 2944 | * operation which sets resp->p and increments resp->opcnt for |
| 2945 | * nfs4svc_encode_compoundres. |
| 2946 | * |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2947 | */ |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2948 | static __be32 |
| 2949 | nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args, |
| 2950 | struct nfsd4_compoundres *resp) |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2951 | { |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2952 | struct nfsd4_op *op; |
| 2953 | struct nfsd4_slot *slot = resp->cstate.slot; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2954 | |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2955 | /* Encode the replayed sequence operation */ |
| 2956 | op = &args->ops[resp->opcnt - 1]; |
| 2957 | nfsd4_encode_operation(resp, op); |
| 2958 | |
J. Bruce Fields | 085def3 | 2017-10-18 16:17:18 -0400 | [diff] [blame] | 2959 | if (slot->sl_flags & NFSD4_SLOT_CACHED) |
| 2960 | return op->status; |
| 2961 | if (args->opcnt == 1) { |
| 2962 | /* |
| 2963 | * The original operation wasn't a solo sequence--we |
| 2964 | * always cache those--so this retry must not match the |
| 2965 | * original: |
| 2966 | */ |
| 2967 | op->status = nfserr_seq_false_retry; |
| 2968 | } else { |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2969 | op = &args->ops[resp->opcnt++]; |
| 2970 | op->status = nfserr_retry_uncached_rep; |
| 2971 | nfsd4_encode_operation(resp, op); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2972 | } |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2973 | return op->status; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2974 | } |
| 2975 | |
| 2976 | /* |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2977 | * The sequence operation is not cached because we can use the slot and |
| 2978 | * session values. |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2979 | */ |
J. Bruce Fields | 3ca2eb9 | 2014-03-21 12:04:21 -0400 | [diff] [blame] | 2980 | static __be32 |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 2981 | nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp, |
| 2982 | struct nfsd4_sequence *seq) |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2983 | { |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2984 | struct nfsd4_slot *slot = resp->cstate.slot; |
Chuck Lever | bddfdbc | 2020-10-27 15:53:42 -0400 | [diff] [blame] | 2985 | struct xdr_stream *xdr = resp->xdr; |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 2986 | __be32 *p; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2987 | __be32 status; |
| 2988 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2989 | dprintk("--> %s slot %p\n", __func__, slot); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2990 | |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2991 | status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp); |
J. Bruce Fields | 0da7b19 | 2014-03-21 11:43:34 -0400 | [diff] [blame] | 2992 | if (status) |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2993 | return status; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2994 | |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 2995 | p = xdr_reserve_space(xdr, slot->sl_datalen); |
| 2996 | if (!p) { |
| 2997 | WARN_ON_ONCE(1); |
| 2998 | return nfserr_serverfault; |
| 2999 | } |
| 3000 | xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen); |
| 3001 | xdr_commit_encode(xdr); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 3002 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 3003 | resp->opcnt = slot->sl_opcnt; |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 3004 | return slot->sl_status; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 3005 | } |
| 3006 | |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3007 | /* |
| 3008 | * Set the exchange_id flags returned by the server. |
| 3009 | */ |
| 3010 | static void |
| 3011 | nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid) |
| 3012 | { |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 3013 | #ifdef CONFIG_NFSD_PNFS |
| 3014 | new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS; |
| 3015 | #else |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3016 | new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS; |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 3017 | #endif |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3018 | |
| 3019 | /* Referrals are supported, Migration is not. */ |
| 3020 | new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER; |
| 3021 | |
| 3022 | /* set the wire flags to return to client. */ |
| 3023 | clid->flags = new->cl_exchange_flags; |
| 3024 | } |
| 3025 | |
J. Bruce Fields | 4eaea13 | 2015-10-15 16:11:01 -0400 | [diff] [blame] | 3026 | static bool client_has_openowners(struct nfs4_client *clp) |
| 3027 | { |
| 3028 | struct nfs4_openowner *oo; |
| 3029 | |
| 3030 | list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) { |
| 3031 | if (!list_empty(&oo->oo_owner.so_stateids)) |
| 3032 | return true; |
| 3033 | } |
| 3034 | return false; |
| 3035 | } |
| 3036 | |
J. Bruce Fields | 631fc9e | 2012-05-14 15:57:23 -0400 | [diff] [blame] | 3037 | static bool client_has_state(struct nfs4_client *clp) |
| 3038 | { |
J. Bruce Fields | 4eaea13 | 2015-10-15 16:11:01 -0400 | [diff] [blame] | 3039 | return client_has_openowners(clp) |
Kinglong Mee | 47e970b | 2015-07-21 13:09:17 +0800 | [diff] [blame] | 3040 | #ifdef CONFIG_NFSD_PNFS |
| 3041 | || !list_empty(&clp->cl_lo_states) |
| 3042 | #endif |
J. Bruce Fields | 6eccece | 2012-05-29 16:37:44 -0400 | [diff] [blame] | 3043 | || !list_empty(&clp->cl_delegations) |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 3044 | || !list_empty(&clp->cl_sessions) |
| 3045 | || !list_empty(&clp->async_copies); |
J. Bruce Fields | 631fc9e | 2012-05-14 15:57:23 -0400 | [diff] [blame] | 3046 | } |
| 3047 | |
J. Bruce Fields | 7912344 | 2019-06-05 12:42:05 -0400 | [diff] [blame] | 3048 | static __be32 copy_impl_id(struct nfs4_client *clp, |
| 3049 | struct nfsd4_exchange_id *exid) |
| 3050 | { |
| 3051 | if (!exid->nii_domain.data) |
| 3052 | return 0; |
| 3053 | xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL); |
| 3054 | if (!clp->cl_nii_domain.data) |
| 3055 | return nfserr_jukebox; |
| 3056 | xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL); |
| 3057 | if (!clp->cl_nii_name.data) |
| 3058 | return nfserr_jukebox; |
Arnd Bergmann | e29f470 | 2019-10-31 14:52:43 +0100 | [diff] [blame] | 3059 | clp->cl_nii_time = exid->nii_time; |
J. Bruce Fields | 7912344 | 2019-06-05 12:42:05 -0400 | [diff] [blame] | 3060 | return 0; |
| 3061 | } |
| 3062 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3063 | __be32 |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3064 | nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 3065 | union nfsd4_op_u *u) |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3066 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3067 | struct nfsd4_exchange_id *exid = &u->exchange_id; |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3068 | struct nfs4_client *conf, *new; |
| 3069 | struct nfs4_client *unconf = NULL; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3070 | __be32 status; |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 3071 | char addr_str[INET6_ADDRSTRLEN]; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3072 | nfs4_verifier verf = exid->verifier; |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 3073 | struct sockaddr *sa = svc_addr(rqstp); |
J. Bruce Fields | 83e08fd | 2012-05-14 09:08:10 -0400 | [diff] [blame] | 3074 | bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 3075 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3076 | |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 3077 | rpc_ntop(sa, addr_str, sizeof(addr_str)); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3078 | dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p " |
Chuck Lever | 523ec6e | 2020-11-02 15:19:12 -0500 | [diff] [blame] | 3079 | "ip_addr=%s flags %x, spa_how %u\n", |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3080 | __func__, rqstp, exid, exid->clname.len, exid->clname.data, |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 3081 | addr_str, exid->flags, exid->spa_how); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3082 | |
J. Bruce Fields | a084daf | 2011-10-10 15:07:40 -0400 | [diff] [blame] | 3083 | if (exid->flags & ~EXCHGID4_FLAG_MASK_A) |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3084 | return nfserr_inval; |
| 3085 | |
J. Bruce Fields | 50c7b94 | 2015-11-23 15:39:12 -0700 | [diff] [blame] | 3086 | new = create_client(exid->clname, rqstp, &verf); |
| 3087 | if (new == NULL) |
| 3088 | return nfserr_jukebox; |
J. Bruce Fields | 7912344 | 2019-06-05 12:42:05 -0400 | [diff] [blame] | 3089 | status = copy_impl_id(new, exid); |
| 3090 | if (status) |
| 3091 | goto out_nolock; |
J. Bruce Fields | 50c7b94 | 2015-11-23 15:39:12 -0700 | [diff] [blame] | 3092 | |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3093 | switch (exid->spa_how) { |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3094 | case SP4_MACH_CRED: |
Andrew Elble | ed94164 | 2016-06-15 12:52:09 -0400 | [diff] [blame] | 3095 | exid->spo_must_enforce[0] = 0; |
| 3096 | exid->spo_must_enforce[1] = ( |
| 3097 | 1 << (OP_BIND_CONN_TO_SESSION - 32) | |
| 3098 | 1 << (OP_EXCHANGE_ID - 32) | |
| 3099 | 1 << (OP_CREATE_SESSION - 32) | |
| 3100 | 1 << (OP_DESTROY_SESSION - 32) | |
| 3101 | 1 << (OP_DESTROY_CLIENTID - 32)); |
| 3102 | |
| 3103 | exid->spo_must_allow[0] &= (1 << (OP_CLOSE) | |
| 3104 | 1 << (OP_OPEN_DOWNGRADE) | |
| 3105 | 1 << (OP_LOCKU) | |
| 3106 | 1 << (OP_DELEGRETURN)); |
| 3107 | |
| 3108 | exid->spo_must_allow[1] &= ( |
| 3109 | 1 << (OP_TEST_STATEID - 32) | |
| 3110 | 1 << (OP_FREE_STATEID - 32)); |
J. Bruce Fields | 50c7b94 | 2015-11-23 15:39:12 -0700 | [diff] [blame] | 3111 | if (!svc_rqst_integrity_protected(rqstp)) { |
| 3112 | status = nfserr_inval; |
| 3113 | goto out_nolock; |
| 3114 | } |
J. Bruce Fields | 920dd9b | 2015-11-20 16:42:40 -0500 | [diff] [blame] | 3115 | /* |
| 3116 | * Sometimes userspace doesn't give us a principal. |
| 3117 | * Which is a bug, really. Anyway, we can't enforce |
| 3118 | * MACH_CRED in that case, better to give up now: |
| 3119 | */ |
J. Bruce Fields | 414ca01 | 2015-11-20 10:48:02 -0500 | [diff] [blame] | 3120 | if (!new->cl_cred.cr_principal && |
| 3121 | !new->cl_cred.cr_raw_principal) { |
J. Bruce Fields | 920dd9b | 2015-11-20 16:42:40 -0500 | [diff] [blame] | 3122 | status = nfserr_serverfault; |
| 3123 | goto out_nolock; |
| 3124 | } |
J. Bruce Fields | 50c7b94 | 2015-11-23 15:39:12 -0700 | [diff] [blame] | 3125 | new->cl_mach_cred = true; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3126 | case SP4_NONE: |
| 3127 | break; |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 3128 | default: /* checked by xdr code */ |
| 3129 | WARN_ON_ONCE(1); |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 3130 | fallthrough; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3131 | case SP4_SSV: |
Kinglong Mee | 8edf4b0 | 2016-02-26 22:36:42 +0800 | [diff] [blame] | 3132 | status = nfserr_encr_alg_unsupp; |
| 3133 | goto out_nolock; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3134 | } |
| 3135 | |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 3136 | /* Cases below refer to rfc 5661 section 18.35.4: */ |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3137 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 3138 | conf = find_confirmed_client_by_name(&exid->clname, nn); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3139 | if (conf) { |
J. Bruce Fields | 83e08fd | 2012-05-14 09:08:10 -0400 | [diff] [blame] | 3140 | bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred); |
| 3141 | bool verfs_match = same_verf(&verf, &conf->cl_verifier); |
| 3142 | |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3143 | if (update) { |
| 3144 | if (!clp_used_exchangeid(conf)) { /* buggy client */ |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 3145 | status = nfserr_inval; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3146 | goto out; |
| 3147 | } |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 3148 | if (!nfsd4_mach_creds_match(conf, rqstp)) { |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3149 | status = nfserr_wrong_cred; |
| 3150 | goto out; |
| 3151 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3152 | if (!creds_match) { /* case 9 */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3153 | status = nfserr_perm; |
| 3154 | goto out; |
| 3155 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3156 | if (!verfs_match) { /* case 8 */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3157 | status = nfserr_not_same; |
| 3158 | goto out; |
| 3159 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3160 | /* case 6 */ |
| 3161 | exid->flags |= EXCHGID4_FLAG_CONFIRMED_R; |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3162 | goto out_copy; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3163 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3164 | if (!creds_match) { /* case 3 */ |
J. Bruce Fields | 631fc9e | 2012-05-14 15:57:23 -0400 | [diff] [blame] | 3165 | if (client_has_state(conf)) { |
| 3166 | status = nfserr_clid_inuse; |
| 3167 | goto out; |
| 3168 | } |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3169 | goto out_new; |
| 3170 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3171 | if (verfs_match) { /* case 2 */ |
J. Bruce Fields | 0f1ba0e | 2012-05-25 21:24:40 -0400 | [diff] [blame] | 3172 | conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R; |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3173 | goto out_copy; |
| 3174 | } |
| 3175 | /* case 5, client reboot */ |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3176 | conf = NULL; |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3177 | goto out_new; |
Mike Sager | 6ddbbbf | 2009-06-16 04:20:47 +0300 | [diff] [blame] | 3178 | } |
| 3179 | |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 3180 | if (update) { /* case 7 */ |
Mike Sager | 6ddbbbf | 2009-06-16 04:20:47 +0300 | [diff] [blame] | 3181 | status = nfserr_noent; |
| 3182 | goto out; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3183 | } |
| 3184 | |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 3185 | unconf = find_unconfirmed_client_by_name(&exid->clname, nn); |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 3186 | if (unconf) /* case 4, possible retry or client restart */ |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3187 | unhash_client_locked(unconf); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3188 | |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 3189 | /* case 1 (normal case) */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3190 | out_new: |
Jeff Layton | fd699b8 | 2014-07-30 08:27:14 -0400 | [diff] [blame] | 3191 | if (conf) { |
| 3192 | status = mark_client_expired_locked(conf); |
| 3193 | if (status) |
| 3194 | goto out; |
| 3195 | } |
J. Bruce Fields | 4f540e2 | 2013-05-07 11:57:52 -0400 | [diff] [blame] | 3196 | new->cl_minorversion = cstate->minorversion; |
Andrew Elble | ed94164 | 2016-06-15 12:52:09 -0400 | [diff] [blame] | 3197 | new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0]; |
| 3198 | 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] | 3199 | |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 3200 | add_to_unconfirmed(new); |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3201 | swap(new, conf); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3202 | out_copy: |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 3203 | exid->clientid.cl_boot = conf->cl_clientid.cl_boot; |
| 3204 | exid->clientid.cl_id = conf->cl_clientid.cl_id; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3205 | |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 3206 | exid->seqid = conf->cl_cs_slot.sl_seqid + 1; |
| 3207 | nfsd4_set_ex_flags(conf, exid); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3208 | |
| 3209 | dprintk("nfsd4_exchange_id seqid %d flags %x\n", |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 3210 | conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3211 | status = nfs_ok; |
| 3212 | |
| 3213 | out: |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3214 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | 50c7b94 | 2015-11-23 15:39:12 -0700 | [diff] [blame] | 3215 | out_nolock: |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 3216 | if (new) |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3217 | expire_client(new); |
| 3218 | if (unconf) |
| 3219 | expire_client(unconf); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3220 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3221 | } |
| 3222 | |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3223 | static __be32 |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 3224 | check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3225 | { |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 3226 | dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid, |
| 3227 | slot_seqid); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3228 | |
| 3229 | /* The slot is in use, and no response has been sent. */ |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 3230 | if (slot_inuse) { |
| 3231 | if (seqid == slot_seqid) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3232 | return nfserr_jukebox; |
| 3233 | else |
| 3234 | return nfserr_seq_misordered; |
| 3235 | } |
J. Bruce Fields | f6d8248 | 2012-02-13 16:13:41 -0500 | [diff] [blame] | 3236 | /* Note unsigned 32-bit arithmetic handles wraparound: */ |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 3237 | if (likely(seqid == slot_seqid + 1)) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3238 | return nfs_ok; |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 3239 | if (seqid == slot_seqid) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3240 | return nfserr_replay_cache; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3241 | return nfserr_seq_misordered; |
| 3242 | } |
| 3243 | |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 3244 | /* |
| 3245 | * Cache the create session result into the create session single DRC |
| 3246 | * slot cache by saving the xdr structure. sl_seqid has been set. |
| 3247 | * Do this for solo or embedded create session operations. |
| 3248 | */ |
| 3249 | static void |
| 3250 | nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses, |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3251 | struct nfsd4_clid_slot *slot, __be32 nfserr) |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 3252 | { |
| 3253 | slot->sl_status = nfserr; |
| 3254 | memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses)); |
| 3255 | } |
| 3256 | |
| 3257 | static __be32 |
| 3258 | nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses, |
| 3259 | struct nfsd4_clid_slot *slot) |
| 3260 | { |
| 3261 | memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses)); |
| 3262 | return slot->sl_status; |
| 3263 | } |
| 3264 | |
Mi Jinlong | 1b74c25 | 2011-07-14 14:50:17 +0800 | [diff] [blame] | 3265 | #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\ |
| 3266 | 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \ |
| 3267 | 1 + /* MIN tag is length with zero, only length */ \ |
| 3268 | 3 + /* version, opcount, opcode */ \ |
| 3269 | XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ |
| 3270 | /* seqid, slotID, slotID, cache */ \ |
| 3271 | 4 ) * sizeof(__be32)) |
| 3272 | |
| 3273 | #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\ |
| 3274 | 2 + /* verifier: AUTH_NULL, length 0 */\ |
| 3275 | 1 + /* status */ \ |
| 3276 | 1 + /* MIN tag is length with zero, only length */ \ |
| 3277 | 3 + /* opcount, opcode, opstatus*/ \ |
| 3278 | XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ |
| 3279 | /* seqid, slotID, slotID, slotID, status */ \ |
| 3280 | 5 ) * sizeof(__be32)) |
| 3281 | |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 3282 | 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] | 3283 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 3284 | u32 maxrpc = nn->nfsd_serv->sv_max_mesg; |
| 3285 | |
J. Bruce Fields | 373cd40 | 2013-04-08 15:42:12 -0400 | [diff] [blame] | 3286 | if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ) |
| 3287 | return nfserr_toosmall; |
| 3288 | if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ) |
| 3289 | return nfserr_toosmall; |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 3290 | ca->headerpadsz = 0; |
| 3291 | ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc); |
| 3292 | ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc); |
| 3293 | ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND); |
| 3294 | ca->maxresp_cached = min_t(u32, ca->maxresp_cached, |
| 3295 | NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ); |
| 3296 | ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION); |
| 3297 | /* |
| 3298 | * Note decreasing slot size below client's request may make it |
| 3299 | * difficult for client to function correctly, whereas |
| 3300 | * decreasing the number of slots will (just?) affect |
| 3301 | * performance. When short on memory we therefore prefer to |
| 3302 | * decrease number of slots instead of their size. Clients that |
| 3303 | * request larger slots than they need will get poor results: |
NeilBrown | 7f49fd5d | 2019-09-20 16:33:16 +1000 | [diff] [blame] | 3304 | * Note that we always allow at least one slot, because our |
| 3305 | * accounting is soft and provides no guarantees either way. |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 3306 | */ |
NeilBrown | 2030ca5 | 2019-09-20 16:36:45 +1000 | [diff] [blame] | 3307 | ca->maxreqs = nfsd4_get_drc_mem(ca, nn); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 3308 | |
J. Bruce Fields | 373cd40 | 2013-04-08 15:42:12 -0400 | [diff] [blame] | 3309 | return nfs_ok; |
Mi Jinlong | 1b74c25 | 2011-07-14 14:50:17 +0800 | [diff] [blame] | 3310 | } |
| 3311 | |
Chuck Lever | 4500632 | 2016-03-01 13:06:02 -0500 | [diff] [blame] | 3312 | /* |
| 3313 | * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now. |
| 3314 | * These are based on similar macros in linux/sunrpc/msg_prot.h . |
| 3315 | */ |
| 3316 | #define RPC_MAX_HEADER_WITH_AUTH_SYS \ |
| 3317 | (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK)) |
| 3318 | |
| 3319 | #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \ |
| 3320 | (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK)) |
| 3321 | |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 3322 | #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \ |
Chuck Lever | 4500632 | 2016-03-01 13:06:02 -0500 | [diff] [blame] | 3323 | RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32)) |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 3324 | #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \ |
Chuck Lever | 4500632 | 2016-03-01 13:06:02 -0500 | [diff] [blame] | 3325 | RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \ |
| 3326 | sizeof(__be32)) |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 3327 | |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 3328 | static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca) |
| 3329 | { |
| 3330 | ca->headerpadsz = 0; |
| 3331 | |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 3332 | if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ) |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 3333 | return nfserr_toosmall; |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 3334 | if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ) |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 3335 | return nfserr_toosmall; |
| 3336 | ca->maxresp_cached = 0; |
| 3337 | if (ca->maxops < 2) |
| 3338 | return nfserr_toosmall; |
| 3339 | |
| 3340 | return nfs_ok; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3341 | } |
| 3342 | |
J. Bruce Fields | b78724b | 2013-05-15 17:34:39 -0400 | [diff] [blame] | 3343 | static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs) |
| 3344 | { |
| 3345 | switch (cbs->flavor) { |
| 3346 | case RPC_AUTH_NULL: |
| 3347 | case RPC_AUTH_UNIX: |
| 3348 | return nfs_ok; |
| 3349 | default: |
| 3350 | /* |
| 3351 | * GSS case: the spec doesn't allow us to return this |
| 3352 | * error. But it also doesn't allow us not to support |
| 3353 | * GSS. |
| 3354 | * I'd rather this fail hard than return some error the |
| 3355 | * client might think it can already handle: |
| 3356 | */ |
| 3357 | return nfserr_encr_alg_unsupp; |
| 3358 | } |
| 3359 | } |
| 3360 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3361 | __be32 |
| 3362 | nfsd4_create_session(struct svc_rqst *rqstp, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3363 | struct nfsd4_compound_state *cstate, union nfsd4_op_u *u) |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3364 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3365 | struct nfsd4_create_session *cr_ses = &u->create_session; |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 3366 | struct sockaddr *sa = svc_addr(rqstp); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3367 | struct nfs4_client *conf, *unconf; |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3368 | struct nfs4_client *old = NULL; |
J. Bruce Fields | ac7c46f2 | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 3369 | struct nfsd4_session *new; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3370 | struct nfsd4_conn *conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 3371 | struct nfsd4_clid_slot *cs_slot = NULL; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3372 | __be32 status = 0; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 3373 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3374 | |
Mi Jinlong | a62573d | 2011-03-23 17:57:07 +0800 | [diff] [blame] | 3375 | if (cr_ses->flags & ~SESSION4_FLAG_MASK_A) |
| 3376 | return nfserr_inval; |
J. Bruce Fields | b78724b | 2013-05-15 17:34:39 -0400 | [diff] [blame] | 3377 | status = nfsd4_check_cb_sec(&cr_ses->cb_sec); |
| 3378 | if (status) |
| 3379 | return status; |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 3380 | status = check_forechannel_attrs(&cr_ses->fore_channel, nn); |
J. Bruce Fields | 373cd40 | 2013-04-08 15:42:12 -0400 | [diff] [blame] | 3381 | if (status) |
| 3382 | return status; |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 3383 | status = check_backchannel_attrs(&cr_ses->back_channel); |
| 3384 | if (status) |
Kinglong Mee | f403e45 | 2013-12-23 17:31:21 +0800 | [diff] [blame] | 3385 | goto out_release_drc_mem; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3386 | status = nfserr_jukebox; |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 3387 | new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 3388 | if (!new) |
| 3389 | goto out_release_drc_mem; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3390 | conn = alloc_conn_from_crses(rqstp, cr_ses); |
| 3391 | if (!conn) |
| 3392 | goto out_free_session; |
Mi Jinlong | a62573d | 2011-03-23 17:57:07 +0800 | [diff] [blame] | 3393 | |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3394 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 3395 | unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 3396 | conf = find_confirmed_client(&cr_ses->clientid, true, nn); |
J. Bruce Fields | 7838904 | 2013-03-12 10:12:37 -0400 | [diff] [blame] | 3397 | WARN_ON_ONCE(conf && unconf); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3398 | |
| 3399 | if (conf) { |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3400 | status = nfserr_wrong_cred; |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 3401 | if (!nfsd4_mach_creds_match(conf, rqstp)) |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3402 | goto out_free_conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 3403 | cs_slot = &conf->cl_cs_slot; |
| 3404 | status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); |
Kinglong Mee | f5e22bb | 2015-07-13 17:32:34 +0800 | [diff] [blame] | 3405 | if (status) { |
| 3406 | if (status == nfserr_replay_cache) |
| 3407 | status = nfsd4_replay_create_session(cr_ses, cs_slot); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3408 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3409 | } |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3410 | } else if (unconf) { |
| 3411 | if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) || |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 3412 | !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) { |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3413 | status = nfserr_clid_inuse; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3414 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3415 | } |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3416 | status = nfserr_wrong_cred; |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 3417 | if (!nfsd4_mach_creds_match(unconf, rqstp)) |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3418 | goto out_free_conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 3419 | cs_slot = &unconf->cl_cs_slot; |
| 3420 | status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); |
Andy Adamson | 38eb76a | 2009-04-03 08:28:32 +0300 | [diff] [blame] | 3421 | if (status) { |
| 3422 | /* an unconfirmed replay returns misordered */ |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3423 | status = nfserr_seq_misordered; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3424 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3425 | } |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 3426 | old = find_confirmed_client_by_name(&unconf->cl_name, nn); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3427 | if (old) { |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3428 | status = mark_client_expired_locked(old); |
Jeff Layton | 7abea1e | 2014-07-30 08:27:13 -0400 | [diff] [blame] | 3429 | if (status) { |
| 3430 | old = NULL; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3431 | goto out_free_conn; |
Jeff Layton | 7abea1e | 2014-07-30 08:27:13 -0400 | [diff] [blame] | 3432 | } |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3433 | } |
J. Bruce Fields | 8f9d3d3 | 2012-09-12 14:41:31 -0400 | [diff] [blame] | 3434 | move_to_confirmed(unconf); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3435 | conf = unconf; |
| 3436 | } else { |
| 3437 | status = nfserr_stale_clientid; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3438 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3439 | } |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3440 | status = nfs_ok; |
Chuck Lever | 4ce85c8 | 2016-03-01 13:05:27 -0500 | [diff] [blame] | 3441 | /* Persistent sessions are not supported */ |
J. Bruce Fields | 408b79b | 2010-04-15 15:11:09 -0400 | [diff] [blame] | 3442 | cr_ses->flags &= ~SESSION4_PERSIST; |
Chuck Lever | 4ce85c8 | 2016-03-01 13:05:27 -0500 | [diff] [blame] | 3443 | /* Upshifting from TCP to RDMA is not supported */ |
J. Bruce Fields | 408b79b | 2010-04-15 15:11:09 -0400 | [diff] [blame] | 3444 | cr_ses->flags &= ~SESSION4_RDMA; |
| 3445 | |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3446 | init_session(rqstp, new, conf, cr_ses); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3447 | nfsd4_get_session_locked(new); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3448 | |
J. Bruce Fields | ac7c46f2 | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 3449 | memcpy(cr_ses->sessionid.data, new->se_sessionid.data, |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3450 | NFS4_MAX_SESSIONID_LEN); |
J. Bruce Fields | 86c3e16 | 2010-10-02 17:04:00 -0400 | [diff] [blame] | 3451 | cs_slot->sl_seqid++; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 3452 | cr_ses->seqid = cs_slot->sl_seqid; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3453 | |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3454 | /* cache solo and embedded create sessions under the client_lock */ |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 3455 | nfsd4_cache_create_session(cr_ses, cs_slot, status); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3456 | spin_unlock(&nn->client_lock); |
| 3457 | /* init connection and backchannel */ |
| 3458 | nfsd4_init_conn(rqstp, conn, new); |
| 3459 | nfsd4_put_session(new); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3460 | if (old) |
| 3461 | expire_client(old); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3462 | return status; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3463 | out_free_conn: |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3464 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3465 | free_conn(conn); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3466 | if (old) |
| 3467 | expire_client(old); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3468 | out_free_session: |
| 3469 | __free_session(new); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 3470 | out_release_drc_mem: |
| 3471 | nfsd4_put_drc_mem(&cr_ses->fore_channel); |
J. Bruce Fields | 1ca5079 | 2013-03-14 18:12:03 -0400 | [diff] [blame] | 3472 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3473 | } |
| 3474 | |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 3475 | static __be32 nfsd4_map_bcts_dir(u32 *dir) |
| 3476 | { |
| 3477 | switch (*dir) { |
| 3478 | case NFS4_CDFC4_FORE: |
| 3479 | case NFS4_CDFC4_BACK: |
| 3480 | return nfs_ok; |
| 3481 | case NFS4_CDFC4_FORE_OR_BOTH: |
| 3482 | case NFS4_CDFC4_BACK_OR_BOTH: |
| 3483 | *dir = NFS4_CDFC4_BOTH; |
| 3484 | return nfs_ok; |
zhengbin | fc5fc5d | 2019-12-19 17:29:20 +0800 | [diff] [blame] | 3485 | } |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 3486 | return nfserr_inval; |
| 3487 | } |
| 3488 | |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3489 | __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, |
| 3490 | struct nfsd4_compound_state *cstate, |
| 3491 | union nfsd4_op_u *u) |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 3492 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3493 | struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl; |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 3494 | struct nfsd4_session *session = cstate->session; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3495 | 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] | 3496 | __be32 status; |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 3497 | |
J. Bruce Fields | b78724b | 2013-05-15 17:34:39 -0400 | [diff] [blame] | 3498 | status = nfsd4_check_cb_sec(&bc->bc_cb_sec); |
| 3499 | if (status) |
| 3500 | return status; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3501 | spin_lock(&nn->client_lock); |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 3502 | session->se_cb_prog = bc->bc_cb_program; |
| 3503 | session->se_cb_sec = bc->bc_cb_sec; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3504 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 3505 | |
| 3506 | nfsd4_probe_callback(session->se_client); |
| 3507 | |
| 3508 | return nfs_ok; |
| 3509 | } |
| 3510 | |
J. Bruce Fields | c2d715a | 2020-04-27 17:59:01 -0400 | [diff] [blame] | 3511 | static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s) |
| 3512 | { |
| 3513 | struct nfsd4_conn *c; |
| 3514 | |
| 3515 | list_for_each_entry(c, &s->se_conns, cn_persession) { |
| 3516 | if (c->cn_xprt == xpt) { |
| 3517 | return c; |
| 3518 | } |
| 3519 | } |
| 3520 | return NULL; |
| 3521 | } |
| 3522 | |
| 3523 | static __be32 nfsd4_match_existing_connection(struct svc_rqst *rqst, |
| 3524 | struct nfsd4_session *session, u32 req) |
| 3525 | { |
| 3526 | struct nfs4_client *clp = session->se_client; |
| 3527 | struct svc_xprt *xpt = rqst->rq_xprt; |
| 3528 | struct nfsd4_conn *c; |
| 3529 | __be32 status; |
| 3530 | |
| 3531 | /* Following the last paragraph of RFC 5661 Section 18.34.3: */ |
| 3532 | spin_lock(&clp->cl_lock); |
| 3533 | c = __nfsd4_find_conn(xpt, session); |
| 3534 | if (!c) |
| 3535 | status = nfserr_noent; |
| 3536 | else if (req == c->cn_flags) |
| 3537 | status = nfs_ok; |
| 3538 | else if (req == NFS4_CDFC4_FORE_OR_BOTH && |
| 3539 | c->cn_flags != NFS4_CDFC4_BACK) |
| 3540 | status = nfs_ok; |
| 3541 | else if (req == NFS4_CDFC4_BACK_OR_BOTH && |
| 3542 | c->cn_flags != NFS4_CDFC4_FORE) |
| 3543 | status = nfs_ok; |
| 3544 | else |
| 3545 | status = nfserr_inval; |
| 3546 | spin_unlock(&clp->cl_lock); |
| 3547 | return status; |
| 3548 | } |
| 3549 | |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 3550 | __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp, |
| 3551 | struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3552 | union nfsd4_op_u *u) |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 3553 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3554 | 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] | 3555 | __be32 status; |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 3556 | struct nfsd4_conn *conn; |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 3557 | struct nfsd4_session *session; |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3558 | struct net *net = SVC_NET(rqstp); |
| 3559 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 3560 | |
| 3561 | if (!nfsd4_last_compound_op(rqstp)) |
| 3562 | return nfserr_not_only_op; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3563 | spin_lock(&nn->client_lock); |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3564 | session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3565 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 3566 | if (!session) |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3567 | goto out_no_session; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3568 | status = nfserr_wrong_cred; |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 3569 | if (!nfsd4_mach_creds_match(session->se_client, rqstp)) |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3570 | goto out; |
J. Bruce Fields | c2d715a | 2020-04-27 17:59:01 -0400 | [diff] [blame] | 3571 | status = nfsd4_match_existing_connection(rqstp, session, bcts->dir); |
| 3572 | if (status == nfs_ok || status == nfserr_inval) |
| 3573 | goto out; |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 3574 | status = nfsd4_map_bcts_dir(&bcts->dir); |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 3575 | if (status) |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 3576 | goto out; |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 3577 | conn = alloc_conn(rqstp, bcts->dir); |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 3578 | status = nfserr_jukebox; |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 3579 | if (!conn) |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 3580 | goto out; |
| 3581 | nfsd4_init_conn(rqstp, conn, session); |
| 3582 | status = nfs_ok; |
| 3583 | out: |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3584 | nfsd4_put_session(session); |
| 3585 | out_no_session: |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 3586 | return status; |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 3587 | } |
| 3588 | |
J. Bruce Fields | 665d507 | 2018-04-11 16:53:36 -0400 | [diff] [blame] | 3589 | 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] | 3590 | { |
J. Bruce Fields | 665d507 | 2018-04-11 16:53:36 -0400 | [diff] [blame] | 3591 | if (!cstate->session) |
Fengguang Wu | 51d87bc | 2018-03-22 13:37:20 +0800 | [diff] [blame] | 3592 | return false; |
J. Bruce Fields | 665d507 | 2018-04-11 16:53:36 -0400 | [diff] [blame] | 3593 | return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid)); |
J. Bruce Fields | 5d4cec2 | 2010-05-01 12:56:06 -0400 | [diff] [blame] | 3594 | } |
| 3595 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3596 | __be32 |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3597 | nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate, |
| 3598 | union nfsd4_op_u *u) |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3599 | { |
J. Bruce Fields | ca0552f | 2018-04-11 17:01:53 -0400 | [diff] [blame] | 3600 | struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid; |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 3601 | struct nfsd4_session *ses; |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 3602 | __be32 status; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 3603 | int ref_held_by_me = 0; |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3604 | struct net *net = SVC_NET(r); |
| 3605 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 3606 | |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 3607 | status = nfserr_not_only_op; |
J. Bruce Fields | ca0552f | 2018-04-11 17:01:53 -0400 | [diff] [blame] | 3608 | if (nfsd4_compound_in_session(cstate, sessionid)) { |
J. Bruce Fields | 5771635 | 2010-04-21 12:27:19 -0400 | [diff] [blame] | 3609 | if (!nfsd4_last_compound_op(r)) |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 3610 | goto out; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 3611 | ref_held_by_me++; |
J. Bruce Fields | 5771635 | 2010-04-21 12:27:19 -0400 | [diff] [blame] | 3612 | } |
J. Bruce Fields | ca0552f | 2018-04-11 17:01:53 -0400 | [diff] [blame] | 3613 | dump_sessionid(__func__, sessionid); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3614 | spin_lock(&nn->client_lock); |
J. Bruce Fields | ca0552f | 2018-04-11 17:01:53 -0400 | [diff] [blame] | 3615 | ses = find_in_sessionid_hashtbl(sessionid, net, &status); |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 3616 | if (!ses) |
| 3617 | goto out_client_lock; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3618 | status = nfserr_wrong_cred; |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 3619 | if (!nfsd4_mach_creds_match(ses->se_client, r)) |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3620 | goto out_put_session; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 3621 | status = mark_session_dead_locked(ses, 1 + ref_held_by_me); |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 3622 | if (status) |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 3623 | goto out_put_session; |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 3624 | unhash_session(ses); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3625 | spin_unlock(&nn->client_lock); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 3626 | |
J. Bruce Fields | 84f5f7c | 2010-12-09 15:52:19 -0500 | [diff] [blame] | 3627 | nfsd4_probe_callback_sync(ses->se_client); |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 3628 | |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3629 | spin_lock(&nn->client_lock); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 3630 | status = nfs_ok; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 3631 | out_put_session: |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3632 | nfsd4_put_session_locked(ses); |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 3633 | out_client_lock: |
| 3634 | spin_unlock(&nn->client_lock); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 3635 | out: |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 3636 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3637 | } |
| 3638 | |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3639 | 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] | 3640 | { |
| 3641 | struct nfs4_client *clp = ses->se_client; |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 3642 | struct nfsd4_conn *c; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3643 | __be32 status = nfs_ok; |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 3644 | int ret; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 3645 | |
| 3646 | spin_lock(&clp->cl_lock); |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 3647 | c = __nfsd4_find_conn(new->cn_xprt, ses); |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3648 | if (c) |
| 3649 | goto out_free; |
| 3650 | status = nfserr_conn_not_bound_to_session; |
| 3651 | if (clp->cl_mach_cred) |
| 3652 | goto out_free; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 3653 | __nfsd4_hash_conn(new, ses); |
| 3654 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 3655 | ret = nfsd4_register_conn(new); |
| 3656 | if (ret) |
| 3657 | /* oops; xprt is already down: */ |
| 3658 | nfsd4_conn_lost(&new->cn_xpt_user); |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3659 | return nfs_ok; |
| 3660 | out_free: |
| 3661 | spin_unlock(&clp->cl_lock); |
| 3662 | free_conn(new); |
| 3663 | return status; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 3664 | } |
| 3665 | |
Mi Jinlong | 868b89c | 2011-04-27 09:09:58 +0800 | [diff] [blame] | 3666 | static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session) |
| 3667 | { |
| 3668 | struct nfsd4_compoundargs *args = rqstp->rq_argp; |
| 3669 | |
| 3670 | return args->opcnt > session->se_fchannel.maxops; |
| 3671 | } |
| 3672 | |
Mi Jinlong | ae82a8d | 2011-07-14 14:56:02 +0800 | [diff] [blame] | 3673 | static bool nfsd4_request_too_big(struct svc_rqst *rqstp, |
| 3674 | struct nfsd4_session *session) |
| 3675 | { |
| 3676 | struct xdr_buf *xb = &rqstp->rq_arg; |
| 3677 | |
| 3678 | return xb->len > session->se_fchannel.maxreq_sz; |
| 3679 | } |
| 3680 | |
J. Bruce Fields | 53da6a5 | 2017-10-17 20:38:49 -0400 | [diff] [blame] | 3681 | static bool replay_matches_cache(struct svc_rqst *rqstp, |
| 3682 | struct nfsd4_sequence *seq, struct nfsd4_slot *slot) |
| 3683 | { |
| 3684 | struct nfsd4_compoundargs *argp = rqstp->rq_argp; |
| 3685 | |
| 3686 | if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) != |
| 3687 | (bool)seq->cachethis) |
| 3688 | return false; |
| 3689 | /* |
Scott Mayhew | 6e73e92 | 2019-10-09 15:11:37 -0400 | [diff] [blame] | 3690 | * If there's an error then the reply can have fewer ops than |
| 3691 | * the call. |
J. Bruce Fields | 53da6a5 | 2017-10-17 20:38:49 -0400 | [diff] [blame] | 3692 | */ |
Scott Mayhew | 6e73e92 | 2019-10-09 15:11:37 -0400 | [diff] [blame] | 3693 | if (slot->sl_opcnt < argp->opcnt && !slot->sl_status) |
| 3694 | return false; |
| 3695 | /* |
| 3696 | * But if we cached a reply with *more* ops than the call you're |
| 3697 | * sending us now, then this new call is clearly not really a |
| 3698 | * replay of the old one: |
| 3699 | */ |
| 3700 | if (slot->sl_opcnt > argp->opcnt) |
J. Bruce Fields | 53da6a5 | 2017-10-17 20:38:49 -0400 | [diff] [blame] | 3701 | return false; |
| 3702 | /* This is the only check explicitly called by spec: */ |
| 3703 | if (!same_creds(&rqstp->rq_cred, &slot->sl_cred)) |
| 3704 | return false; |
| 3705 | /* |
| 3706 | * There may be more comparisons we could actually do, but the |
| 3707 | * spec doesn't require us to catch every case where the calls |
| 3708 | * don't match (that would require caching the call as well as |
| 3709 | * the reply), so we don't bother. |
| 3710 | */ |
| 3711 | return true; |
| 3712 | } |
| 3713 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3714 | __be32 |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3715 | nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 3716 | union nfsd4_op_u *u) |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3717 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3718 | struct nfsd4_sequence *seq = &u->sequence; |
Andy Adamson | f9bb94c4 | 2009-04-03 08:28:12 +0300 | [diff] [blame] | 3719 | struct nfsd4_compoundres *resp = rqstp->rq_resp; |
Chuck Lever | bddfdbc | 2020-10-27 15:53:42 -0400 | [diff] [blame] | 3720 | struct xdr_stream *xdr = resp->xdr; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3721 | struct nfsd4_session *session; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3722 | struct nfs4_client *clp; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3723 | struct nfsd4_slot *slot; |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 3724 | struct nfsd4_conn *conn; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3725 | __be32 status; |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 3726 | int buflen; |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3727 | struct net *net = SVC_NET(rqstp); |
| 3728 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3729 | |
Andy Adamson | f9bb94c4 | 2009-04-03 08:28:12 +0300 | [diff] [blame] | 3730 | if (resp->opcnt != 1) |
| 3731 | return nfserr_sequence_pos; |
| 3732 | |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 3733 | /* |
| 3734 | * Will be either used or freed by nfsd4_sequence_check_conn |
| 3735 | * below. |
| 3736 | */ |
| 3737 | conn = alloc_conn(rqstp, NFS4_CDFC4_FORE); |
| 3738 | if (!conn) |
| 3739 | return nfserr_jukebox; |
| 3740 | |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3741 | spin_lock(&nn->client_lock); |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3742 | session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3743 | if (!session) |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3744 | goto out_no_session; |
| 3745 | clp = session->se_client; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3746 | |
Mi Jinlong | 868b89c | 2011-04-27 09:09:58 +0800 | [diff] [blame] | 3747 | status = nfserr_too_many_ops; |
| 3748 | if (nfsd4_session_too_many_ops(rqstp, session)) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 3749 | goto out_put_session; |
Mi Jinlong | 868b89c | 2011-04-27 09:09:58 +0800 | [diff] [blame] | 3750 | |
Mi Jinlong | ae82a8d | 2011-07-14 14:56:02 +0800 | [diff] [blame] | 3751 | status = nfserr_req_too_big; |
| 3752 | if (nfsd4_request_too_big(rqstp, session)) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 3753 | goto out_put_session; |
Mi Jinlong | ae82a8d | 2011-07-14 14:56:02 +0800 | [diff] [blame] | 3754 | |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3755 | status = nfserr_badslot; |
Alexandros Batsakis | 6c18ba9 | 2009-06-16 04:19:13 +0300 | [diff] [blame] | 3756 | if (seq->slotid >= session->se_fchannel.maxreqs) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 3757 | goto out_put_session; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3758 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 3759 | slot = session->se_slots[seq->slotid]; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3760 | dprintk("%s: slotid %d\n", __func__, seq->slotid); |
| 3761 | |
Andy Adamson | a8dfdae | 2009-08-28 08:45:02 -0400 | [diff] [blame] | 3762 | /* We do not negotiate the number of slots yet, so set the |
| 3763 | * maxslots to the session maxreqs which is used to encode |
| 3764 | * sr_highest_slotid and the sr_target_slot id to maxslots */ |
| 3765 | seq->maxslots = session->se_fchannel.maxreqs; |
| 3766 | |
J. Bruce Fields | 73e7948 | 2012-02-13 16:39:00 -0500 | [diff] [blame] | 3767 | status = check_slot_seqid(seq->seqid, slot->sl_seqid, |
| 3768 | slot->sl_flags & NFSD4_SLOT_INUSE); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3769 | if (status == nfserr_replay_cache) { |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 3770 | status = nfserr_seq_misordered; |
| 3771 | if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED)) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 3772 | goto out_put_session; |
J. Bruce Fields | 53da6a5 | 2017-10-17 20:38:49 -0400 | [diff] [blame] | 3773 | status = nfserr_seq_false_retry; |
| 3774 | if (!replay_matches_cache(rqstp, seq, slot)) |
| 3775 | goto out_put_session; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3776 | cstate->slot = slot; |
| 3777 | cstate->session = session; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3778 | cstate->clp = clp; |
Andy Adamson | da3846a | 2009-04-03 08:28:22 +0300 | [diff] [blame] | 3779 | /* Return the cached reply status and set cstate->status |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 3780 | * for nfsd4_proc_compound processing */ |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 3781 | status = nfsd4_replay_cache_entry(resp, seq); |
Andy Adamson | da3846a | 2009-04-03 08:28:22 +0300 | [diff] [blame] | 3782 | cstate->status = nfserr_replay_cache; |
Benny Halevy | aaf84eb | 2009-08-20 03:21:56 +0300 | [diff] [blame] | 3783 | goto out; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3784 | } |
| 3785 | if (status) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 3786 | goto out_put_session; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3787 | |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3788 | status = nfsd4_sequence_check_conn(conn, session); |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 3789 | conn = NULL; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3790 | if (status) |
| 3791 | goto out_put_session; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 3792 | |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 3793 | buflen = (seq->cachethis) ? |
| 3794 | session->se_fchannel.maxresp_cached : |
| 3795 | session->se_fchannel.maxresp_sz; |
| 3796 | status = (seq->cachethis) ? nfserr_rep_too_big_to_cache : |
| 3797 | nfserr_rep_too_big; |
J. Bruce Fields | a5cddc8 | 2014-05-12 18:10:58 -0400 | [diff] [blame] | 3798 | if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack)) |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 3799 | goto out_put_session; |
J. Bruce Fields | 32aaa62 | 2014-03-20 20:47:41 -0400 | [diff] [blame] | 3800 | svc_reserve(rqstp, buflen); |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 3801 | |
| 3802 | status = nfs_ok; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3803 | /* Success! bump slot seqid */ |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3804 | slot->sl_seqid = seq->seqid; |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 3805 | slot->sl_flags |= NFSD4_SLOT_INUSE; |
J. Bruce Fields | 73e7948 | 2012-02-13 16:39:00 -0500 | [diff] [blame] | 3806 | if (seq->cachethis) |
| 3807 | slot->sl_flags |= NFSD4_SLOT_CACHETHIS; |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 3808 | else |
| 3809 | slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3810 | |
| 3811 | cstate->slot = slot; |
| 3812 | cstate->session = session; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3813 | cstate->clp = clp; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3814 | |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3815 | out: |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3816 | switch (clp->cl_cb_state) { |
| 3817 | case NFSD4_CB_DOWN: |
| 3818 | seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN; |
| 3819 | break; |
| 3820 | case NFSD4_CB_FAULT: |
| 3821 | seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT; |
| 3822 | break; |
| 3823 | default: |
| 3824 | seq->status_flags = 0; |
Benny Halevy | aaf84eb | 2009-08-20 03:21:56 +0300 | [diff] [blame] | 3825 | } |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 3826 | if (!list_empty(&clp->cl_revoked)) |
| 3827 | seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3828 | out_no_session: |
Kinglong Mee | 3f42d2c | 2014-03-24 11:56:59 +0800 | [diff] [blame] | 3829 | if (conn) |
| 3830 | free_conn(conn); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3831 | spin_unlock(&nn->client_lock); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3832 | return status; |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 3833 | out_put_session: |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3834 | nfsd4_put_session_locked(session); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3835 | goto out_no_session; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3836 | } |
| 3837 | |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 3838 | void |
| 3839 | nfsd4_sequence_done(struct nfsd4_compoundres *resp) |
| 3840 | { |
| 3841 | struct nfsd4_compound_state *cs = &resp->cstate; |
| 3842 | |
| 3843 | if (nfsd4_has_session(cs)) { |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 3844 | if (cs->status != nfserr_replay_cache) { |
| 3845 | nfsd4_store_cache_entry(resp); |
| 3846 | cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE; |
| 3847 | } |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3848 | /* Drop session reference that was taken in nfsd4_sequence() */ |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 3849 | nfsd4_put_session(cs->session); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3850 | } else if (cs->clp) |
| 3851 | put_client_renew(cs->clp); |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 3852 | } |
| 3853 | |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3854 | __be32 |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3855 | nfsd4_destroy_clientid(struct svc_rqst *rqstp, |
| 3856 | struct nfsd4_compound_state *cstate, |
| 3857 | union nfsd4_op_u *u) |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3858 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3859 | struct nfsd4_destroy_clientid *dc = &u->destroy_clientid; |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3860 | struct nfs4_client *conf, *unconf; |
| 3861 | struct nfs4_client *clp = NULL; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3862 | __be32 status = 0; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 3863 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3864 | |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3865 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 3866 | unconf = find_unconfirmed_client(&dc->clientid, true, nn); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 3867 | conf = find_confirmed_client(&dc->clientid, true, nn); |
J. Bruce Fields | 7838904 | 2013-03-12 10:12:37 -0400 | [diff] [blame] | 3868 | WARN_ON_ONCE(conf && unconf); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3869 | |
| 3870 | if (conf) { |
J. Bruce Fields | c0293b0 | 2013-03-14 18:20:01 -0400 | [diff] [blame] | 3871 | if (client_has_state(conf)) { |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3872 | status = nfserr_clientid_busy; |
| 3873 | goto out; |
| 3874 | } |
Jeff Layton | fd699b8 | 2014-07-30 08:27:14 -0400 | [diff] [blame] | 3875 | status = mark_client_expired_locked(conf); |
| 3876 | if (status) |
| 3877 | goto out; |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3878 | clp = conf; |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3879 | } else if (unconf) |
| 3880 | clp = unconf; |
| 3881 | else { |
| 3882 | status = nfserr_stale_clientid; |
| 3883 | goto out; |
| 3884 | } |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 3885 | if (!nfsd4_mach_creds_match(clp, rqstp)) { |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3886 | clp = NULL; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3887 | status = nfserr_wrong_cred; |
| 3888 | goto out; |
| 3889 | } |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3890 | unhash_client_locked(clp); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3891 | out: |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3892 | spin_unlock(&nn->client_lock); |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3893 | if (clp) |
| 3894 | expire_client(clp); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3895 | return status; |
| 3896 | } |
| 3897 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3898 | __be32 |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3899 | nfsd4_reclaim_complete(struct svc_rqst *rqstp, |
| 3900 | struct nfsd4_compound_state *cstate, union nfsd4_op_u *u) |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 3901 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3902 | struct nfsd4_reclaim_complete *rc = &u->reclaim_complete; |
J. Bruce Fields | ec59659 | 2021-01-21 17:57:45 -0500 | [diff] [blame] | 3903 | struct nfs4_client *clp = cstate->clp; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3904 | __be32 status = 0; |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3905 | |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 3906 | if (rc->rca_one_fs) { |
| 3907 | if (!cstate->current_fh.fh_dentry) |
| 3908 | return nfserr_nofilehandle; |
| 3909 | /* |
| 3910 | * We don't take advantage of the rca_one_fs case. |
| 3911 | * That's OK, it's optional, we can safely ignore it. |
| 3912 | */ |
Christophe JAILLET | d28c442 | 2016-07-02 08:24:32 +0200 | [diff] [blame] | 3913 | return nfs_ok; |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 3914 | } |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3915 | |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3916 | status = nfserr_complete_already; |
J. Bruce Fields | ec59659 | 2021-01-21 17:57:45 -0500 | [diff] [blame] | 3917 | if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags)) |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3918 | goto out; |
| 3919 | |
| 3920 | status = nfserr_stale_clientid; |
J. Bruce Fields | ec59659 | 2021-01-21 17:57:45 -0500 | [diff] [blame] | 3921 | if (is_client_expired(clp)) |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 3922 | /* |
| 3923 | * The following error isn't really legal. |
| 3924 | * But we only get here if the client just explicitly |
| 3925 | * destroyed the client. Surely it no longer cares what |
| 3926 | * error it gets back on an operation for the dead |
| 3927 | * client. |
| 3928 | */ |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3929 | goto out; |
| 3930 | |
| 3931 | status = nfs_ok; |
J. Bruce Fields | ec59659 | 2021-01-21 17:57:45 -0500 | [diff] [blame] | 3932 | nfsd4_client_record_create(clp); |
| 3933 | inc_reclaim_complete(clp); |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3934 | out: |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3935 | return status; |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 3936 | } |
| 3937 | |
| 3938 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 3939 | nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3940 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3941 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3942 | struct nfsd4_setclientid *setclid = &u->setclientid; |
J. Bruce Fields | a084daf | 2011-10-10 15:07:40 -0400 | [diff] [blame] | 3943 | struct xdr_netobj clname = setclid->se_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3944 | nfs4_verifier clverifier = setclid->se_verf; |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3945 | struct nfs4_client *conf, *new; |
| 3946 | struct nfs4_client *unconf = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3947 | __be32 status; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 3948 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
| 3949 | |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 3950 | new = create_client(clname, rqstp, &clverifier); |
| 3951 | if (new == NULL) |
| 3952 | return nfserr_jukebox; |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 3953 | /* Cases below refer to rfc 3530 section 14.2.33: */ |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3954 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 3955 | conf = find_confirmed_client_by_name(&clname, nn); |
J. Bruce Fields | 2b63482 | 2015-10-15 15:33:23 -0400 | [diff] [blame] | 3956 | if (conf && client_has_state(conf)) { |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 3957 | /* case 0: */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3958 | status = nfserr_clid_inuse; |
J. Bruce Fields | e203d50 | 2010-11-24 17:30:54 -0500 | [diff] [blame] | 3959 | if (clp_used_exchangeid(conf)) |
| 3960 | goto out; |
J. Bruce Fields | 026722c | 2009-03-18 15:06:26 -0400 | [diff] [blame] | 3961 | if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) { |
Chuck Lever | dd5e3fb | 2020-04-05 11:42:29 -0400 | [diff] [blame] | 3962 | trace_nfsd_clid_inuse_err(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3963 | goto out; |
| 3964 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3965 | } |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 3966 | unconf = find_unconfirmed_client_by_name(&clname, nn); |
J. Bruce Fields | 8f93071 | 2012-05-18 22:06:41 -0400 | [diff] [blame] | 3967 | if (unconf) |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3968 | unhash_client_locked(unconf); |
Chuck Lever | 45f56da | 2020-04-05 14:40:25 -0400 | [diff] [blame] | 3969 | /* We need to handle only case 1: probable callback update */ |
Kinglong Mee | 41eb167 | 2015-07-13 17:29:41 +0800 | [diff] [blame] | 3970 | if (conf && same_verf(&conf->cl_verifier, &clverifier)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3971 | copy_clid(new, conf); |
Kinglong Mee | 41eb167 | 2015-07-13 17:29:41 +0800 | [diff] [blame] | 3972 | gen_confirm(new, nn); |
Chuck Lever | 45f56da | 2020-04-05 14:40:25 -0400 | [diff] [blame] | 3973 | } |
J. Bruce Fields | 8323c3b | 2010-10-19 19:36:51 -0400 | [diff] [blame] | 3974 | new->cl_minorversion = 0; |
Takuma Umeya | 6f3d772 | 2010-12-15 14:09:01 +0900 | [diff] [blame] | 3975 | gen_callback(new, setclid, rqstp); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 3976 | add_to_unconfirmed(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3977 | setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot; |
| 3978 | setclid->se_clientid.cl_id = new->cl_clientid.cl_id; |
| 3979 | 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] | 3980 | new = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3981 | status = nfs_ok; |
| 3982 | out: |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3983 | spin_unlock(&nn->client_lock); |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 3984 | if (new) |
| 3985 | free_client(new); |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3986 | if (unconf) |
| 3987 | expire_client(unconf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3988 | return status; |
| 3989 | } |
| 3990 | |
| 3991 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3992 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 3993 | nfsd4_setclientid_confirm(struct svc_rqst *rqstp, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3994 | struct nfsd4_compound_state *cstate, |
| 3995 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3996 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3997 | struct nfsd4_setclientid_confirm *setclientid_confirm = |
| 3998 | &u->setclientid_confirm; |
NeilBrown | 21ab45a | 2005-06-23 22:04:14 -0700 | [diff] [blame] | 3999 | struct nfs4_client *conf, *unconf; |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 4000 | struct nfs4_client *old = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4001 | nfs4_verifier confirm = setclientid_confirm->sc_confirm; |
| 4002 | clientid_t * clid = &setclientid_confirm->sc_clientid; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4003 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 4004 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4005 | |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 4006 | if (STALE_CLIENTID(clid, nn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4007 | return nfserr_stale_clientid; |
NeilBrown | 21ab45a | 2005-06-23 22:04:14 -0700 | [diff] [blame] | 4008 | |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 4009 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 4010 | conf = find_confirmed_client(clid, false, nn); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 4011 | unconf = find_unconfirmed_client(clid, false, nn); |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 4012 | /* |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 4013 | * We try hard to give out unique clientid's, so if we get an |
| 4014 | * attempt to confirm the same clientid with a different cred, |
J. Bruce Fields | f984a7c | 2015-09-01 13:40:53 -0400 | [diff] [blame] | 4015 | * the client may be buggy; this should never happen. |
| 4016 | * |
| 4017 | * Nevertheless, RFC 7530 recommends INUSE for this case: |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 4018 | */ |
J. Bruce Fields | f984a7c | 2015-09-01 13:40:53 -0400 | [diff] [blame] | 4019 | status = nfserr_clid_inuse; |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 4020 | if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred)) |
| 4021 | goto out; |
| 4022 | if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred)) |
| 4023 | goto out; |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 4024 | /* cases below refer to rfc 3530 section 14.2.34: */ |
J. Bruce Fields | 90d700b | 2012-05-19 13:55:22 -0400 | [diff] [blame] | 4025 | if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) { |
J. Bruce Fields | 7d22fc1 | 2016-09-20 20:55:36 -0400 | [diff] [blame] | 4026 | if (conf && same_verf(&confirm, &conf->cl_confirm)) { |
| 4027 | /* case 2: probable retransmit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4028 | status = nfs_ok; |
J. Bruce Fields | 7d22fc1 | 2016-09-20 20:55:36 -0400 | [diff] [blame] | 4029 | } else /* case 4: client hasn't noticed we rebooted yet? */ |
J. Bruce Fields | 90d700b | 2012-05-19 13:55:22 -0400 | [diff] [blame] | 4030 | status = nfserr_stale_clientid; |
| 4031 | goto out; |
| 4032 | } |
| 4033 | status = nfs_ok; |
| 4034 | if (conf) { /* case 1: callback update */ |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 4035 | old = unconf; |
| 4036 | unhash_client_locked(old); |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 4037 | nfsd4_change_callback(conf, &unconf->cl_cb_conn); |
J. Bruce Fields | 90d700b | 2012-05-19 13:55:22 -0400 | [diff] [blame] | 4038 | } else { /* case 3: normal case; new or rebooted client */ |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 4039 | old = find_confirmed_client_by_name(&unconf->cl_name, nn); |
| 4040 | if (old) { |
J. Bruce Fields | 2b63482 | 2015-10-15 15:33:23 -0400 | [diff] [blame] | 4041 | status = nfserr_clid_inuse; |
| 4042 | if (client_has_state(old) |
| 4043 | && !same_creds(&unconf->cl_cred, |
| 4044 | &old->cl_cred)) |
| 4045 | goto out; |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 4046 | status = mark_client_expired_locked(old); |
Jeff Layton | 7abea1e | 2014-07-30 08:27:13 -0400 | [diff] [blame] | 4047 | if (status) { |
| 4048 | old = NULL; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 4049 | goto out; |
Jeff Layton | 7abea1e | 2014-07-30 08:27:13 -0400 | [diff] [blame] | 4050 | } |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 4051 | } |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 4052 | move_to_confirmed(unconf); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 4053 | conf = unconf; |
NeilBrown | 08e8987 | 2005-06-23 22:04:11 -0700 | [diff] [blame] | 4054 | } |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 4055 | get_client_locked(conf); |
| 4056 | spin_unlock(&nn->client_lock); |
| 4057 | nfsd4_probe_callback(conf); |
| 4058 | spin_lock(&nn->client_lock); |
| 4059 | put_client_renew_locked(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4060 | out: |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 4061 | spin_unlock(&nn->client_lock); |
| 4062 | if (old) |
| 4063 | expire_client(old); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4064 | return status; |
| 4065 | } |
| 4066 | |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 4067 | static struct nfs4_file *nfsd4_alloc_file(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4068 | { |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 4069 | return kmem_cache_alloc(file_slab, GFP_KERNEL); |
| 4070 | } |
| 4071 | |
| 4072 | /* OPEN Share state helper functions */ |
J. Bruce Fields | f9b60e2 | 2021-04-16 14:00:15 -0400 | [diff] [blame] | 4073 | static void nfsd4_init_file(struct svc_fh *fh, unsigned int hashval, |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 4074 | struct nfs4_file *fp) |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 4075 | { |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 4076 | lockdep_assert_held(&state_lock); |
| 4077 | |
Elena Reshetova | 818a34e | 2017-10-20 12:53:30 +0300 | [diff] [blame] | 4078 | refcount_set(&fp->fi_ref, 1); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 4079 | spin_lock_init(&fp->fi_lock); |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 4080 | INIT_LIST_HEAD(&fp->fi_stateids); |
| 4081 | INIT_LIST_HEAD(&fp->fi_delegations); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 4082 | INIT_LIST_HEAD(&fp->fi_clnt_odstate); |
J. Bruce Fields | f9b60e2 | 2021-04-16 14:00:15 -0400 | [diff] [blame] | 4083 | fh_copy_shallow(&fp->fi_fhandle, &fh->fh_handle); |
Jeff Layton | 0c637be | 2014-08-22 12:05:43 -0400 | [diff] [blame] | 4084 | fp->fi_deleg_file = NULL; |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 4085 | fp->fi_had_conflict = false; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4086 | fp->fi_share_deny = 0; |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 4087 | memset(fp->fi_fds, 0, sizeof(fp->fi_fds)); |
| 4088 | memset(fp->fi_access, 0, sizeof(fp->fi_access)); |
J. Bruce Fields | a0ce483 | 2021-04-16 14:00:16 -0400 | [diff] [blame^] | 4089 | fp->fi_aliased = false; |
| 4090 | fp->fi_inode = d_inode(fh->fh_dentry); |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 4091 | #ifdef CONFIG_NFSD_PNFS |
| 4092 | INIT_LIST_HEAD(&fp->fi_lo_states); |
Christoph Hellwig | c5c707f | 2014-09-23 12:38:48 +0200 | [diff] [blame] | 4093 | atomic_set(&fp->fi_lo_recalls, 0); |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 4094 | #endif |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 4095 | hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4096 | } |
| 4097 | |
J. Bruce Fields | e8ff2a8 | 2007-08-01 15:30:59 -0400 | [diff] [blame] | 4098 | void |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 4099 | nfsd4_free_slabs(void) |
| 4100 | { |
Jeff Layton | 9258a2d | 2018-03-16 09:47:22 -0400 | [diff] [blame] | 4101 | kmem_cache_destroy(client_slab); |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 4102 | kmem_cache_destroy(openowner_slab); |
| 4103 | kmem_cache_destroy(lockowner_slab); |
| 4104 | kmem_cache_destroy(file_slab); |
| 4105 | kmem_cache_destroy(stateid_slab); |
| 4106 | kmem_cache_destroy(deleg_slab); |
Jeff Layton | 9258a2d | 2018-03-16 09:47:22 -0400 | [diff] [blame] | 4107 | kmem_cache_destroy(odstate_slab); |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 4108 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4109 | |
Bryan Schumaker | 7208339 | 2011-11-01 15:24:59 -0400 | [diff] [blame] | 4110 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4111 | nfsd4_init_slabs(void) |
| 4112 | { |
Jeff Layton | 9258a2d | 2018-03-16 09:47:22 -0400 | [diff] [blame] | 4113 | client_slab = kmem_cache_create("nfsd4_clients", |
| 4114 | sizeof(struct nfs4_client), 0, 0, NULL); |
| 4115 | if (client_slab == NULL) |
| 4116 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4117 | openowner_slab = kmem_cache_create("nfsd4_openowners", |
| 4118 | sizeof(struct nfs4_openowner), 0, 0, NULL); |
| 4119 | if (openowner_slab == NULL) |
Jeff Layton | 9258a2d | 2018-03-16 09:47:22 -0400 | [diff] [blame] | 4120 | goto out_free_client_slab; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4121 | lockowner_slab = kmem_cache_create("nfsd4_lockowners", |
Yanchuan Nian | 3c40794 | 2012-10-24 14:44:19 +0800 | [diff] [blame] | 4122 | sizeof(struct nfs4_lockowner), 0, 0, NULL); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4123 | if (lockowner_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 4124 | goto out_free_openowner_slab; |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 4125 | file_slab = kmem_cache_create("nfsd4_files", |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 4126 | sizeof(struct nfs4_file), 0, 0, NULL); |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 4127 | if (file_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 4128 | goto out_free_lockowner_slab; |
NeilBrown | 5ac049a | 2005-06-23 22:03:03 -0700 | [diff] [blame] | 4129 | stateid_slab = kmem_cache_create("nfsd4_stateids", |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4130 | sizeof(struct nfs4_ol_stateid), 0, 0, NULL); |
NeilBrown | 5ac049a | 2005-06-23 22:03:03 -0700 | [diff] [blame] | 4131 | if (stateid_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 4132 | goto out_free_file_slab; |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 4133 | deleg_slab = kmem_cache_create("nfsd4_delegations", |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 4134 | sizeof(struct nfs4_delegation), 0, 0, NULL); |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 4135 | if (deleg_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 4136 | goto out_free_stateid_slab; |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 4137 | odstate_slab = kmem_cache_create("nfsd4_odstate", |
| 4138 | sizeof(struct nfs4_clnt_odstate), 0, 0, NULL); |
| 4139 | if (odstate_slab == NULL) |
| 4140 | goto out_free_deleg_slab; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4141 | return 0; |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 4142 | |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 4143 | out_free_deleg_slab: |
| 4144 | kmem_cache_destroy(deleg_slab); |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 4145 | out_free_stateid_slab: |
| 4146 | kmem_cache_destroy(stateid_slab); |
| 4147 | out_free_file_slab: |
| 4148 | kmem_cache_destroy(file_slab); |
| 4149 | out_free_lockowner_slab: |
| 4150 | kmem_cache_destroy(lockowner_slab); |
| 4151 | out_free_openowner_slab: |
| 4152 | kmem_cache_destroy(openowner_slab); |
Jeff Layton | 9258a2d | 2018-03-16 09:47:22 -0400 | [diff] [blame] | 4153 | out_free_client_slab: |
| 4154 | kmem_cache_destroy(client_slab); |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 4155 | out: |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 4156 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4157 | } |
| 4158 | |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4159 | static void init_nfs4_replay(struct nfs4_replay *rp) |
| 4160 | { |
| 4161 | rp->rp_status = nfserr_serverfault; |
| 4162 | rp->rp_buflen = 0; |
| 4163 | rp->rp_buf = rp->rp_ibuf; |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 4164 | mutex_init(&rp->rp_mutex); |
| 4165 | } |
| 4166 | |
| 4167 | static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate, |
| 4168 | struct nfs4_stateowner *so) |
| 4169 | { |
| 4170 | if (!nfsd4_has_session(cstate)) { |
| 4171 | mutex_lock(&so->so_replay.rp_mutex); |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 4172 | cstate->replay_owner = nfs4_get_stateowner(so); |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 4173 | } |
| 4174 | } |
| 4175 | |
| 4176 | void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate) |
| 4177 | { |
| 4178 | struct nfs4_stateowner *so = cstate->replay_owner; |
| 4179 | |
| 4180 | if (so != NULL) { |
| 4181 | cstate->replay_owner = NULL; |
| 4182 | mutex_unlock(&so->so_replay.rp_mutex); |
| 4183 | nfs4_put_stateowner(so); |
| 4184 | } |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4185 | } |
| 4186 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4187 | 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] | 4188 | { |
| 4189 | struct nfs4_stateowner *sop; |
| 4190 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4191 | sop = kmem_cache_alloc(slab, GFP_KERNEL); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4192 | if (!sop) |
| 4193 | return NULL; |
| 4194 | |
J. Bruce Fields | 6f4859b | 2019-06-19 14:30:33 -0400 | [diff] [blame] | 4195 | xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4196 | if (!sop->so_owner.data) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4197 | kmem_cache_free(slab, sop); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4198 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4199 | } |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4200 | |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4201 | INIT_LIST_HEAD(&sop->so_stateids); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4202 | sop->so_client = clp; |
| 4203 | init_nfs4_replay(&sop->so_replay); |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 4204 | atomic_set(&sop->so_count, 1); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4205 | return sop; |
| 4206 | } |
| 4207 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4208 | 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] | 4209 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 4210 | lockdep_assert_held(&clp->cl_lock); |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 4211 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 4212 | list_add(&oo->oo_owner.so_strhash, |
| 4213 | &clp->cl_ownerstr_hashtbl[strhashval]); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4214 | list_add(&oo->oo_perclient, &clp->cl_openowners); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4215 | } |
| 4216 | |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 4217 | static void nfs4_unhash_openowner(struct nfs4_stateowner *so) |
| 4218 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 4219 | unhash_openowner_locked(openowner(so)); |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 4220 | } |
| 4221 | |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 4222 | static void nfs4_free_openowner(struct nfs4_stateowner *so) |
| 4223 | { |
| 4224 | struct nfs4_openowner *oo = openowner(so); |
| 4225 | |
| 4226 | kmem_cache_free(openowner_slab, oo); |
| 4227 | } |
| 4228 | |
| 4229 | static const struct nfs4_stateowner_operations openowner_ops = { |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 4230 | .so_unhash = nfs4_unhash_openowner, |
| 4231 | .so_free = nfs4_free_openowner, |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 4232 | }; |
| 4233 | |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4234 | static struct nfs4_ol_stateid * |
| 4235 | nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open) |
| 4236 | { |
| 4237 | struct nfs4_ol_stateid *local, *ret = NULL; |
| 4238 | struct nfs4_openowner *oo = open->op_openowner; |
| 4239 | |
| 4240 | lockdep_assert_held(&fp->fi_lock); |
| 4241 | |
| 4242 | list_for_each_entry(local, &fp->fi_stateids, st_perfile) { |
| 4243 | /* ignore lock owners */ |
| 4244 | if (local->st_stateowner->so_is_open_owner == 0) |
| 4245 | continue; |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 4246 | if (local->st_stateowner != &oo->oo_owner) |
| 4247 | continue; |
| 4248 | if (local->st_stid.sc_type == NFS4_OPEN_STID) { |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4249 | ret = local; |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 4250 | refcount_inc(&ret->st_stid.sc_count); |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4251 | break; |
| 4252 | } |
| 4253 | } |
| 4254 | return ret; |
| 4255 | } |
| 4256 | |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 4257 | static __be32 |
| 4258 | nfsd4_verify_open_stid(struct nfs4_stid *s) |
| 4259 | { |
| 4260 | __be32 ret = nfs_ok; |
| 4261 | |
| 4262 | switch (s->sc_type) { |
| 4263 | default: |
| 4264 | break; |
Trond Myklebust | 4f17641 | 2018-01-12 17:42:30 -0500 | [diff] [blame] | 4265 | case 0: |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 4266 | case NFS4_CLOSED_STID: |
| 4267 | case NFS4_CLOSED_DELEG_STID: |
| 4268 | ret = nfserr_bad_stateid; |
| 4269 | break; |
| 4270 | case NFS4_REVOKED_DELEG_STID: |
| 4271 | ret = nfserr_deleg_revoked; |
| 4272 | } |
| 4273 | return ret; |
| 4274 | } |
| 4275 | |
| 4276 | /* Lock the stateid st_mutex, and deal with races with CLOSE */ |
| 4277 | static __be32 |
| 4278 | nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp) |
| 4279 | { |
| 4280 | __be32 ret; |
| 4281 | |
Andrew Elble | 4f34bd0 | 2017-11-08 17:29:51 -0500 | [diff] [blame] | 4282 | mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX); |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 4283 | ret = nfsd4_verify_open_stid(&stp->st_stid); |
| 4284 | if (ret != nfs_ok) |
| 4285 | mutex_unlock(&stp->st_mutex); |
| 4286 | return ret; |
| 4287 | } |
| 4288 | |
| 4289 | static struct nfs4_ol_stateid * |
| 4290 | nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open) |
| 4291 | { |
| 4292 | struct nfs4_ol_stateid *stp; |
| 4293 | for (;;) { |
| 4294 | spin_lock(&fp->fi_lock); |
| 4295 | stp = nfsd4_find_existing_open(fp, open); |
| 4296 | spin_unlock(&fp->fi_lock); |
| 4297 | if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok) |
| 4298 | break; |
| 4299 | nfs4_put_stid(&stp->st_stid); |
| 4300 | } |
| 4301 | return stp; |
| 4302 | } |
| 4303 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4304 | static struct nfs4_openowner * |
Trond Myklebust | 13d6f66 | 2014-06-30 11:48:45 -0400 | [diff] [blame] | 4305 | alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open, |
Jeff Layton | db24b3b | 2014-06-30 11:48:36 -0400 | [diff] [blame] | 4306 | struct nfsd4_compound_state *cstate) |
| 4307 | { |
Trond Myklebust | 13d6f66 | 2014-06-30 11:48:45 -0400 | [diff] [blame] | 4308 | struct nfs4_client *clp = cstate->clp; |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 4309 | struct nfs4_openowner *oo, *ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4310 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4311 | oo = alloc_stateowner(openowner_slab, &open->op_owner, clp); |
| 4312 | if (!oo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4313 | return NULL; |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 4314 | oo->oo_owner.so_ops = &openowner_ops; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4315 | oo->oo_owner.so_is_open_owner = 1; |
| 4316 | oo->oo_owner.so_seqid = open->op_seqid; |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 4317 | oo->oo_flags = 0; |
Jeff Layton | db24b3b | 2014-06-30 11:48:36 -0400 | [diff] [blame] | 4318 | if (nfsd4_has_session(cstate)) |
| 4319 | oo->oo_flags |= NFS4_OO_CONFIRMED; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4320 | oo->oo_time = 0; |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 4321 | oo->oo_last_closed_stid = NULL; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4322 | INIT_LIST_HEAD(&oo->oo_close_lru); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 4323 | spin_lock(&clp->cl_lock); |
| 4324 | ret = find_openstateowner_str_locked(strhashval, open, clp); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 4325 | if (ret == NULL) { |
| 4326 | hash_openowner(oo, clp, strhashval); |
| 4327 | ret = oo; |
| 4328 | } else |
Kinglong Mee | d50ffde | 2015-07-16 12:05:07 +0800 | [diff] [blame] | 4329 | nfs4_free_stateowner(&oo->oo_owner); |
| 4330 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 4331 | spin_unlock(&clp->cl_lock); |
Jeff Layton | c595233 | 2015-03-23 10:53:42 -0400 | [diff] [blame] | 4332 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4333 | } |
| 4334 | |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4335 | static struct nfs4_ol_stateid * |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 4336 | init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open) |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4337 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4338 | |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4339 | struct nfs4_openowner *oo = open->op_openowner; |
| 4340 | struct nfs4_ol_stateid *retstp = NULL; |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 4341 | struct nfs4_ol_stateid *stp; |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4342 | |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 4343 | stp = open->op_stp; |
Oleg Drokin | 5cc1fb2 | 2016-06-14 23:28:05 -0400 | [diff] [blame] | 4344 | /* We are moving these outside of the spinlocks to avoid the warnings */ |
| 4345 | mutex_init(&stp->st_mutex); |
Andrew Elble | 4f34bd0 | 2017-11-08 17:29:51 -0500 | [diff] [blame] | 4346 | mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX); |
Oleg Drokin | 5cc1fb2 | 2016-06-14 23:28:05 -0400 | [diff] [blame] | 4347 | |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 4348 | retry: |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4349 | spin_lock(&oo->oo_owner.so_client->cl_lock); |
| 4350 | spin_lock(&fp->fi_lock); |
| 4351 | |
| 4352 | retstp = nfsd4_find_existing_open(fp, open); |
| 4353 | if (retstp) |
| 4354 | goto out_unlock; |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 4355 | |
| 4356 | open->op_stp = NULL; |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 4357 | refcount_inc(&stp->st_stid.sc_count); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 4358 | stp->st_stid.sc_type = NFS4_OPEN_STID; |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 4359 | INIT_LIST_HEAD(&stp->st_locks); |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 4360 | stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 4361 | get_nfs4_file(fp); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 4362 | stp->st_stid.sc_file = fp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4363 | stp->st_access_bmap = 0; |
| 4364 | stp->st_deny_bmap = 0; |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 4365 | stp->st_openstp = NULL; |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 4366 | list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 4367 | list_add(&stp->st_perfile, &fp->fi_stateids); |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4368 | |
| 4369 | out_unlock: |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 4370 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 4371 | spin_unlock(&oo->oo_owner.so_client->cl_lock); |
Oleg Drokin | 5cc1fb2 | 2016-06-14 23:28:05 -0400 | [diff] [blame] | 4372 | if (retstp) { |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 4373 | /* Handle races with CLOSE */ |
| 4374 | if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) { |
| 4375 | nfs4_put_stid(&retstp->st_stid); |
| 4376 | goto retry; |
| 4377 | } |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 4378 | /* To keep mutex tracking happy */ |
Oleg Drokin | 5cc1fb2 | 2016-06-14 23:28:05 -0400 | [diff] [blame] | 4379 | mutex_unlock(&stp->st_mutex); |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 4380 | stp = retstp; |
Oleg Drokin | 5cc1fb2 | 2016-06-14 23:28:05 -0400 | [diff] [blame] | 4381 | } |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 4382 | return stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4383 | } |
| 4384 | |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 4385 | /* |
| 4386 | * In the 4.0 case we need to keep the owners around a little while to handle |
| 4387 | * CLOSE replay. We still do need to release any file access that is held by |
| 4388 | * them before returning however. |
| 4389 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4390 | static void |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 4391 | move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4392 | { |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 4393 | struct nfs4_ol_stateid *last; |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 4394 | struct nfs4_openowner *oo = openowner(s->st_stateowner); |
| 4395 | struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net, |
| 4396 | nfsd_net_id); |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 4397 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4398 | dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4399 | |
Jeff Layton | b401be22 | 2014-07-29 21:34:33 -0400 | [diff] [blame] | 4400 | /* |
| 4401 | * We know that we hold one reference via nfsd4_close, and another |
| 4402 | * "persistent" reference for the client. If the refcount is higher |
| 4403 | * than 2, then there are still calls in progress that are using this |
| 4404 | * stateid. We can't put the sc_file reference until they are finished. |
| 4405 | * Wait for the refcount to drop to 2. Since it has been unhashed, |
| 4406 | * there should be no danger of the refcount going back up again at |
| 4407 | * this point. |
| 4408 | */ |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 4409 | wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2); |
Jeff Layton | b401be22 | 2014-07-29 21:34:33 -0400 | [diff] [blame] | 4410 | |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 4411 | release_all_access(s); |
| 4412 | if (s->st_stid.sc_file) { |
| 4413 | put_nfs4_file(s->st_stid.sc_file); |
| 4414 | s->st_stid.sc_file = NULL; |
| 4415 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 4416 | |
| 4417 | spin_lock(&nn->client_lock); |
| 4418 | last = oo->oo_last_closed_stid; |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 4419 | oo->oo_last_closed_stid = s; |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 4420 | list_move_tail(&oo->oo_close_lru, &nn->close_lru); |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 4421 | oo->oo_time = ktime_get_boottime_seconds(); |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 4422 | spin_unlock(&nn->client_lock); |
| 4423 | if (last) |
| 4424 | nfs4_put_stid(&last->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4425 | } |
| 4426 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4427 | /* search file_hashtbl[] for file */ |
| 4428 | static struct nfs4_file * |
J. Bruce Fields | f9b60e2 | 2021-04-16 14:00:15 -0400 | [diff] [blame] | 4429 | find_file_locked(struct svc_fh *fh, unsigned int hashval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4430 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4431 | struct nfs4_file *fp; |
| 4432 | |
Madhuparna Bhowmik | 36a8049 | 2020-02-13 20:33:31 +0530 | [diff] [blame] | 4433 | hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash, |
| 4434 | lockdep_is_held(&state_lock)) { |
J. Bruce Fields | f9b60e2 | 2021-04-16 14:00:15 -0400 | [diff] [blame] | 4435 | if (fh_match(&fp->fi_fhandle, &fh->fh_handle)) { |
Elena Reshetova | 818a34e | 2017-10-20 12:53:30 +0300 | [diff] [blame] | 4436 | if (refcount_inc_not_zero(&fp->fi_ref)) |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 4437 | return fp; |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 4438 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4439 | } |
| 4440 | return NULL; |
| 4441 | } |
| 4442 | |
J. Bruce Fields | a0ce483 | 2021-04-16 14:00:16 -0400 | [diff] [blame^] | 4443 | static struct nfs4_file *insert_file(struct nfs4_file *new, struct svc_fh *fh, |
| 4444 | unsigned int hashval) |
| 4445 | { |
| 4446 | struct nfs4_file *fp; |
| 4447 | struct nfs4_file *ret = NULL; |
| 4448 | bool alias_found = false; |
| 4449 | |
| 4450 | spin_lock(&state_lock); |
| 4451 | hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash, |
| 4452 | lockdep_is_held(&state_lock)) { |
| 4453 | if (fh_match(&fp->fi_fhandle, &fh->fh_handle)) { |
| 4454 | if (refcount_inc_not_zero(&fp->fi_ref)) |
| 4455 | ret = fp; |
| 4456 | } else if (d_inode(fh->fh_dentry) == fp->fi_inode) |
| 4457 | fp->fi_aliased = alias_found = true; |
| 4458 | } |
| 4459 | if (likely(ret == NULL)) { |
| 4460 | nfsd4_init_file(fh, hashval, new); |
| 4461 | new->fi_aliased = alias_found; |
| 4462 | ret = new; |
| 4463 | } |
| 4464 | spin_unlock(&state_lock); |
| 4465 | return ret; |
| 4466 | } |
| 4467 | |
J. Bruce Fields | f9b60e2 | 2021-04-16 14:00:15 -0400 | [diff] [blame] | 4468 | static struct nfs4_file * find_file(struct svc_fh *fh) |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 4469 | { |
| 4470 | struct nfs4_file *fp; |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 4471 | unsigned int hashval = file_hashval(fh); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 4472 | |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 4473 | rcu_read_lock(); |
| 4474 | fp = find_file_locked(fh, hashval); |
| 4475 | rcu_read_unlock(); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 4476 | return fp; |
| 4477 | } |
| 4478 | |
| 4479 | static struct nfs4_file * |
J. Bruce Fields | f9b60e2 | 2021-04-16 14:00:15 -0400 | [diff] [blame] | 4480 | find_or_add_file(struct nfs4_file *new, struct svc_fh *fh) |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 4481 | { |
| 4482 | struct nfs4_file *fp; |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 4483 | unsigned int hashval = file_hashval(fh); |
| 4484 | |
| 4485 | rcu_read_lock(); |
| 4486 | fp = find_file_locked(fh, hashval); |
| 4487 | rcu_read_unlock(); |
| 4488 | if (fp) |
| 4489 | return fp; |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 4490 | |
J. Bruce Fields | a0ce483 | 2021-04-16 14:00:16 -0400 | [diff] [blame^] | 4491 | return insert_file(new, fh, hashval); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 4492 | } |
| 4493 | |
J. Bruce Fields | 4f83aa3 | 2008-07-07 15:02:02 -0400 | [diff] [blame] | 4494 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4495 | * Called to check deny when READ with all zero stateid or |
| 4496 | * WRITE with all zero or all one stateid |
| 4497 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4498 | static __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4499 | nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type) |
| 4500 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4501 | struct nfs4_file *fp; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4502 | __be32 ret = nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4503 | |
J. Bruce Fields | f9b60e2 | 2021-04-16 14:00:15 -0400 | [diff] [blame] | 4504 | fp = find_file(current_fh); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 4505 | if (!fp) |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4506 | return ret; |
| 4507 | /* Check for conflicting share reservations */ |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 4508 | spin_lock(&fp->fi_lock); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4509 | if (fp->fi_share_deny & deny_type) |
| 4510 | ret = nfserr_locked; |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 4511 | spin_unlock(&fp->fi_lock); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 4512 | put_nfs4_file(fp); |
| 4513 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4514 | } |
| 4515 | |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 4516 | static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4517 | { |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 4518 | struct nfs4_delegation *dp = cb_to_delegation(cb); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 4519 | struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net, |
| 4520 | nfsd_net_id); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 4521 | |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 4522 | block_delegations(&dp->dl_stid.sc_file->fi_fhandle); |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 4523 | |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 4524 | /* |
| 4525 | * 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] | 4526 | * already holding inode->i_lock. |
| 4527 | * |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 4528 | * If the dl_time != 0, then we know that it has already been |
| 4529 | * queued for a lease break. Don't queue it again. |
| 4530 | */ |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 4531 | spin_lock(&state_lock); |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 4532 | if (dp->dl_time == 0) { |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 4533 | dp->dl_time = ktime_get_boottime_seconds(); |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 4534 | list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru); |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 4535 | } |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 4536 | spin_unlock(&state_lock); |
| 4537 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4538 | |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 4539 | static int nfsd4_cb_recall_done(struct nfsd4_callback *cb, |
| 4540 | struct rpc_task *task) |
| 4541 | { |
| 4542 | struct nfs4_delegation *dp = cb_to_delegation(cb); |
| 4543 | |
J. Bruce Fields | 12ed22f | 2020-07-31 16:20:05 -0400 | [diff] [blame] | 4544 | if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID || |
| 4545 | dp->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) |
Andrew Elble | a457974 | 2015-08-31 12:06:41 -0400 | [diff] [blame] | 4546 | return 1; |
| 4547 | |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 4548 | switch (task->tk_status) { |
| 4549 | case 0: |
| 4550 | return 1; |
Scott Mayhew | 1c73b9d | 2019-05-02 13:32:12 -0400 | [diff] [blame] | 4551 | case -NFS4ERR_DELAY: |
| 4552 | rpc_delay(task, 2 * HZ); |
| 4553 | return 0; |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 4554 | case -EBADHANDLE: |
| 4555 | case -NFS4ERR_BAD_STATEID: |
| 4556 | /* |
| 4557 | * Race: client probably got cb_recall before open reply |
| 4558 | * granting delegation. |
| 4559 | */ |
| 4560 | if (dp->dl_retries--) { |
| 4561 | rpc_delay(task, 2 * HZ); |
| 4562 | return 0; |
| 4563 | } |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 4564 | fallthrough; |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 4565 | default: |
Scott Mayhew | 1c73b9d | 2019-05-02 13:32:12 -0400 | [diff] [blame] | 4566 | return 1; |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 4567 | } |
| 4568 | } |
| 4569 | |
| 4570 | static void nfsd4_cb_recall_release(struct nfsd4_callback *cb) |
| 4571 | { |
| 4572 | struct nfs4_delegation *dp = cb_to_delegation(cb); |
| 4573 | |
| 4574 | nfs4_put_stid(&dp->dl_stid); |
| 4575 | } |
| 4576 | |
Julia Lawall | c4cb897 | 2015-11-21 22:57:39 +0100 | [diff] [blame] | 4577 | static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = { |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 4578 | .prepare = nfsd4_cb_recall_prepare, |
| 4579 | .done = nfsd4_cb_recall_done, |
| 4580 | .release = nfsd4_cb_recall_release, |
| 4581 | }; |
| 4582 | |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 4583 | static void nfsd_break_one_deleg(struct nfs4_delegation *dp) |
| 4584 | { |
| 4585 | /* |
| 4586 | * We're assuming the state code never drops its reference |
| 4587 | * without first removing the lease. Since we're in this lease |
J. Bruce Fields | 4a269efb | 2018-04-13 17:34:35 -0400 | [diff] [blame] | 4588 | * callback (and since the lease code is serialized by the |
| 4589 | * i_lock) we know the server hasn't removed the lease yet, and |
| 4590 | * we know it's safe to take a reference. |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 4591 | */ |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 4592 | refcount_inc(&dp->dl_stid.sc_count); |
Christoph Hellwig | f0b5de1 | 2014-09-24 12:19:18 +0200 | [diff] [blame] | 4593 | nfsd4_run_cb(&dp->dl_recall); |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 4594 | } |
| 4595 | |
Jeff Layton | 1c8c601 | 2013-06-21 08:58:15 -0400 | [diff] [blame] | 4596 | /* Called from break_lease() with i_lock held. */ |
Jeff Layton | 4d01b7f | 2014-09-01 15:06:54 -0400 | [diff] [blame] | 4597 | static bool |
| 4598 | nfsd_break_deleg_cb(struct file_lock *fl) |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 4599 | { |
Jeff Layton | 4d01b7f | 2014-09-01 15:06:54 -0400 | [diff] [blame] | 4600 | bool ret = false; |
J. Bruce Fields | 653e514 | 2018-02-15 22:08:45 -0500 | [diff] [blame] | 4601 | struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner; |
| 4602 | struct nfs4_file *fp = dp->dl_stid.sc_file; |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 4603 | |
Chuck Lever | dd5e3fb | 2020-04-05 11:42:29 -0400 | [diff] [blame] | 4604 | trace_nfsd_deleg_break(&dp->dl_stid.sc_stateid); |
| 4605 | |
J. Bruce Fields | 0272e1f | 2007-09-12 18:56:12 -0400 | [diff] [blame] | 4606 | /* |
| 4607 | * 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] | 4608 | * we'll remove it ourself if a delegation isn't returned |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 4609 | * in time: |
J. Bruce Fields | 0272e1f | 2007-09-12 18:56:12 -0400 | [diff] [blame] | 4610 | */ |
| 4611 | fl->fl_break_time = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4612 | |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 4613 | spin_lock(&fp->fi_lock); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4614 | fp->fi_had_conflict = true; |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 4615 | nfsd_break_one_deleg(dp); |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 4616 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 4d01b7f | 2014-09-01 15:06:54 -0400 | [diff] [blame] | 4617 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4618 | } |
| 4619 | |
J. Bruce Fields | 28df3d1 | 2017-07-28 16:35:15 -0400 | [diff] [blame] | 4620 | static bool nfsd_breaker_owns_lease(struct file_lock *fl) |
| 4621 | { |
| 4622 | struct nfs4_delegation *dl = fl->fl_owner; |
| 4623 | struct svc_rqst *rqst; |
| 4624 | struct nfs4_client *clp; |
| 4625 | |
| 4626 | if (!i_am_nfsd()) |
| 4627 | return NULL; |
| 4628 | rqst = kthread_data(current); |
J. Bruce Fields | 1395616 | 2020-09-25 10:12:39 -0400 | [diff] [blame] | 4629 | /* Note rq_prog == NFS_ACL_PROGRAM is also possible: */ |
| 4630 | if (rqst->rq_prog != NFS_PROGRAM || rqst->rq_vers < 4) |
J. Bruce Fields | 34b09af | 2020-08-05 15:10:11 -0400 | [diff] [blame] | 4631 | return NULL; |
J. Bruce Fields | 28df3d1 | 2017-07-28 16:35:15 -0400 | [diff] [blame] | 4632 | clp = *(rqst->rq_lease_breaker); |
| 4633 | return dl->dl_stid.sc_client == clp; |
| 4634 | } |
| 4635 | |
Jeff Layton | c45198e | 2014-09-01 07:12:07 -0400 | [diff] [blame] | 4636 | static int |
Jeff Layton | 7448cc3 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 4637 | nfsd_change_deleg_cb(struct file_lock *onlist, int arg, |
| 4638 | struct list_head *dispose) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4639 | { |
| 4640 | if (arg & F_UNLCK) |
Jeff Layton | c45198e | 2014-09-01 07:12:07 -0400 | [diff] [blame] | 4641 | return lease_modify(onlist, arg, dispose); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4642 | else |
| 4643 | return -EAGAIN; |
| 4644 | } |
| 4645 | |
Alexey Dobriyan | 7b02196 | 2009-09-21 17:01:12 -0700 | [diff] [blame] | 4646 | static const struct lock_manager_operations nfsd_lease_mng_ops = { |
J. Bruce Fields | 28df3d1 | 2017-07-28 16:35:15 -0400 | [diff] [blame] | 4647 | .lm_breaker_owns_lease = nfsd_breaker_owns_lease, |
J. Bruce Fields | 8fb47a4 | 2011-07-20 20:21:59 -0400 | [diff] [blame] | 4648 | .lm_break = nfsd_break_deleg_cb, |
| 4649 | .lm_change = nfsd_change_deleg_cb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4650 | }; |
| 4651 | |
J. Bruce Fields | 7a8711c | 2011-09-02 09:03:37 -0400 | [diff] [blame] | 4652 | static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid) |
| 4653 | { |
| 4654 | if (nfsd4_has_session(cstate)) |
| 4655 | return nfs_ok; |
| 4656 | if (seqid == so->so_seqid - 1) |
| 4657 | return nfserr_replay_me; |
| 4658 | if (seqid == so->so_seqid) |
| 4659 | return nfs_ok; |
| 4660 | return nfserr_bad_seqid; |
| 4661 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4662 | |
J. Bruce Fields | 7950b53 | 2021-01-21 17:57:41 -0500 | [diff] [blame] | 4663 | static struct nfs4_client *lookup_clientid(clientid_t *clid, bool sessions, |
| 4664 | struct nfsd_net *nn) |
| 4665 | { |
| 4666 | struct nfs4_client *found; |
| 4667 | |
| 4668 | spin_lock(&nn->client_lock); |
| 4669 | found = find_confirmed_client(clid, sessions, nn); |
| 4670 | if (found) |
| 4671 | atomic_inc(&found->cl_rpc_users); |
| 4672 | spin_unlock(&nn->client_lock); |
| 4673 | return found; |
| 4674 | } |
| 4675 | |
J. Bruce Fields | 460d270 | 2021-01-21 17:57:40 -0500 | [diff] [blame] | 4676 | static __be32 set_client(clientid_t *clid, |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4677 | struct nfsd4_compound_state *cstate, |
J. Bruce Fields | f71475b | 2021-01-21 17:57:43 -0500 | [diff] [blame] | 4678 | struct nfsd_net *nn) |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4679 | { |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4680 | if (cstate->clp) { |
J. Bruce Fields | 7950b53 | 2021-01-21 17:57:41 -0500 | [diff] [blame] | 4681 | if (!same_clid(&cstate->clp->cl_clientid, clid)) |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4682 | return nfserr_stale_clientid; |
| 4683 | return nfs_ok; |
| 4684 | } |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4685 | if (STALE_CLIENTID(clid, nn)) |
| 4686 | return nfserr_stale_clientid; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4687 | /* |
J. Bruce Fields | f71475b | 2021-01-21 17:57:43 -0500 | [diff] [blame] | 4688 | * We're in the 4.0 case (otherwise the SEQUENCE op would have |
| 4689 | * set cstate->clp), so session = false: |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4690 | */ |
J. Bruce Fields | f71475b | 2021-01-21 17:57:43 -0500 | [diff] [blame] | 4691 | cstate->clp = lookup_clientid(clid, false, nn); |
J. Bruce Fields | 7950b53 | 2021-01-21 17:57:41 -0500 | [diff] [blame] | 4692 | if (!cstate->clp) |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4693 | return nfserr_expired; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4694 | return nfs_ok; |
| 4695 | } |
| 4696 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4697 | __be32 |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 4698 | nfsd4_process_open1(struct nfsd4_compound_state *cstate, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4699 | struct nfsd4_open *open, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4700 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4701 | clientid_t *clientid = &open->op_clientid; |
| 4702 | struct nfs4_client *clp = NULL; |
| 4703 | unsigned int strhashval; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4704 | struct nfs4_openowner *oo = NULL; |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 4705 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4706 | |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 4707 | /* |
| 4708 | * In case we need it later, after we've already created the |
| 4709 | * file and don't want to risk a further failure: |
| 4710 | */ |
| 4711 | open->op_file = nfsd4_alloc_file(); |
| 4712 | if (open->op_file == NULL) |
| 4713 | return nfserr_jukebox; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4714 | |
J. Bruce Fields | f71475b | 2021-01-21 17:57:43 -0500 | [diff] [blame] | 4715 | status = set_client(clientid, cstate, nn); |
Trond Myklebust | 2d91e89 | 2014-06-30 11:48:46 -0400 | [diff] [blame] | 4716 | if (status) |
| 4717 | return status; |
| 4718 | clp = cstate->clp; |
| 4719 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 4720 | strhashval = ownerstr_hashval(&open->op_owner); |
| 4721 | oo = find_openstateowner_str(strhashval, open, clp); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4722 | open->op_openowner = oo; |
| 4723 | if (!oo) { |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 4724 | goto new_owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4725 | } |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 4726 | if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) { |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 4727 | /* Replace unconfirmed owners without checking for replay. */ |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4728 | release_openowner(oo); |
| 4729 | open->op_openowner = NULL; |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 4730 | goto new_owner; |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 4731 | } |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 4732 | status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid); |
| 4733 | if (status) |
| 4734 | return status; |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 4735 | goto alloc_stateid; |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 4736 | new_owner: |
Trond Myklebust | 13d6f66 | 2014-06-30 11:48:45 -0400 | [diff] [blame] | 4737 | oo = alloc_init_open_stateowner(strhashval, open, cstate); |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 4738 | if (oo == NULL) |
| 4739 | return nfserr_jukebox; |
| 4740 | open->op_openowner = oo; |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 4741 | alloc_stateid: |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 4742 | open->op_stp = nfs4_alloc_open_stateid(clp); |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 4743 | if (!open->op_stp) |
| 4744 | return nfserr_jukebox; |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 4745 | |
| 4746 | if (nfsd4_has_session(cstate) && |
| 4747 | (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) { |
| 4748 | open->op_odstate = alloc_clnt_odstate(clp); |
| 4749 | if (!open->op_odstate) |
| 4750 | return nfserr_jukebox; |
| 4751 | } |
| 4752 | |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 4753 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4754 | } |
| 4755 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4756 | static inline __be32 |
NeilBrown | 4a6e43e | 2005-06-23 22:02:50 -0700 | [diff] [blame] | 4757 | nfs4_check_delegmode(struct nfs4_delegation *dp, int flags) |
| 4758 | { |
| 4759 | if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ)) |
| 4760 | return nfserr_openmode; |
| 4761 | else |
| 4762 | return nfs_ok; |
| 4763 | } |
| 4764 | |
Daniel Mack | c47d832 | 2011-05-16 16:38:14 +0200 | [diff] [blame] | 4765 | static int share_access_to_flags(u32 share_access) |
J. Bruce Fields | 24a0111 | 2010-05-18 20:01:35 -0400 | [diff] [blame] | 4766 | { |
J. Bruce Fields | 24a0111 | 2010-05-18 20:01:35 -0400 | [diff] [blame] | 4767 | return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE; |
| 4768 | } |
| 4769 | |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4770 | 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] | 4771 | { |
| 4772 | struct nfs4_stid *ret; |
| 4773 | |
Andrew Elble | 95da1b3a | 2017-11-03 14:06:31 -0400 | [diff] [blame] | 4774 | ret = find_stateid_by_type(cl, s, |
| 4775 | NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID); |
J. Bruce Fields | f459e45 | 2011-09-09 09:06:12 -0400 | [diff] [blame] | 4776 | if (!ret) |
| 4777 | return NULL; |
| 4778 | return delegstateid(ret); |
| 4779 | } |
| 4780 | |
J. Bruce Fields | 8b289b2 | 2011-10-19 11:52:12 -0400 | [diff] [blame] | 4781 | static bool nfsd4_is_deleg_cur(struct nfsd4_open *open) |
| 4782 | { |
| 4783 | return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR || |
| 4784 | open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH; |
| 4785 | } |
| 4786 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4787 | static __be32 |
J. Bruce Fields | 41d2266 | 2013-03-21 15:49:47 -0400 | [diff] [blame] | 4788 | nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open, |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 4789 | struct nfs4_delegation **dp) |
| 4790 | { |
| 4791 | int flags; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4792 | __be32 status = nfserr_bad_stateid; |
Trond Myklebust | dcd94cc | 2014-07-29 21:34:18 -0400 | [diff] [blame] | 4793 | struct nfs4_delegation *deleg; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 4794 | |
Trond Myklebust | dcd94cc | 2014-07-29 21:34:18 -0400 | [diff] [blame] | 4795 | deleg = find_deleg_stateid(cl, &open->op_delegate_stateid); |
| 4796 | if (deleg == NULL) |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 4797 | goto out; |
Andrew Elble | 95da1b3a | 2017-11-03 14:06:31 -0400 | [diff] [blame] | 4798 | if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) { |
| 4799 | nfs4_put_stid(&deleg->dl_stid); |
| 4800 | if (cl->cl_minorversion) |
| 4801 | status = nfserr_deleg_revoked; |
| 4802 | goto out; |
| 4803 | } |
J. Bruce Fields | 24a0111 | 2010-05-18 20:01:35 -0400 | [diff] [blame] | 4804 | flags = share_access_to_flags(open->op_share_access); |
Trond Myklebust | dcd94cc | 2014-07-29 21:34:18 -0400 | [diff] [blame] | 4805 | status = nfs4_check_delegmode(deleg, flags); |
| 4806 | if (status) { |
| 4807 | nfs4_put_stid(&deleg->dl_stid); |
| 4808 | goto out; |
| 4809 | } |
| 4810 | *dp = deleg; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 4811 | out: |
J. Bruce Fields | 8b289b2 | 2011-10-19 11:52:12 -0400 | [diff] [blame] | 4812 | if (!nfsd4_is_deleg_cur(open)) |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 4813 | return nfs_ok; |
| 4814 | if (status) |
| 4815 | return status; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 4816 | open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 4817 | return nfs_ok; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 4818 | } |
| 4819 | |
J. Bruce Fields | 21fb401 | 2010-07-28 12:21:23 -0400 | [diff] [blame] | 4820 | static inline int nfs4_access_to_access(u32 nfs4_access) |
| 4821 | { |
| 4822 | int flags = 0; |
| 4823 | |
| 4824 | if (nfs4_access & NFS4_SHARE_ACCESS_READ) |
| 4825 | flags |= NFSD_MAY_READ; |
| 4826 | if (nfs4_access & NFS4_SHARE_ACCESS_WRITE) |
| 4827 | flags |= NFSD_MAY_WRITE; |
| 4828 | return flags; |
| 4829 | } |
| 4830 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4831 | static inline __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4832 | nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh, |
| 4833 | struct nfsd4_open *open) |
| 4834 | { |
| 4835 | struct iattr iattr = { |
| 4836 | .ia_valid = ATTR_SIZE, |
| 4837 | .ia_size = 0, |
| 4838 | }; |
| 4839 | if (!open->op_truncate) |
| 4840 | return 0; |
| 4841 | if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE)) |
Al Viro | 9246585 | 2006-01-18 17:43:46 -0800 | [diff] [blame] | 4842 | return nfserr_inval; |
Arnd Bergmann | 2a1aa48 | 2019-11-03 17:50:18 +0100 | [diff] [blame] | 4843 | return nfsd_setattr(rqstp, fh, &iattr, 0, (time64_t)0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4844 | } |
| 4845 | |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4846 | 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] | 4847 | struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, |
| 4848 | struct nfsd4_open *open) |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4849 | { |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 4850 | struct nfsd_file *nf = NULL; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4851 | __be32 status; |
| 4852 | int oflag = nfs4_access_to_omode(open->op_share_access); |
| 4853 | int access = nfs4_access_to_access(open->op_share_access); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4854 | unsigned char old_access_bmap, old_deny_bmap; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4855 | |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4856 | spin_lock(&fp->fi_lock); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4857 | |
| 4858 | /* |
| 4859 | * Are we trying to set a deny mode that would conflict with |
| 4860 | * current access? |
| 4861 | */ |
| 4862 | status = nfs4_file_check_deny(fp, open->op_share_deny); |
| 4863 | if (status != nfs_ok) { |
| 4864 | spin_unlock(&fp->fi_lock); |
| 4865 | goto out; |
| 4866 | } |
| 4867 | |
| 4868 | /* set access to the file */ |
| 4869 | status = nfs4_file_get_access(fp, open->op_share_access); |
| 4870 | if (status != nfs_ok) { |
| 4871 | spin_unlock(&fp->fi_lock); |
| 4872 | goto out; |
| 4873 | } |
| 4874 | |
| 4875 | /* Set access bits in stateid */ |
| 4876 | old_access_bmap = stp->st_access_bmap; |
| 4877 | set_access(open->op_share_access, stp); |
| 4878 | |
| 4879 | /* Set new deny mask */ |
| 4880 | old_deny_bmap = stp->st_deny_bmap; |
| 4881 | set_deny(open->op_share_deny, stp); |
| 4882 | fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH); |
| 4883 | |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4884 | if (!fp->fi_fds[oflag]) { |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4885 | spin_unlock(&fp->fi_lock); |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 4886 | status = nfsd_file_acquire(rqstp, cur_fh, access, &nf); |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4887 | if (status) |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4888 | goto out_put_access; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4889 | spin_lock(&fp->fi_lock); |
| 4890 | if (!fp->fi_fds[oflag]) { |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 4891 | fp->fi_fds[oflag] = nf; |
| 4892 | nf = NULL; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4893 | } |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4894 | } |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4895 | spin_unlock(&fp->fi_lock); |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 4896 | if (nf) |
| 4897 | nfsd_file_put(nf); |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4898 | |
J. Bruce Fields | 217fd6f | 2021-04-16 14:00:14 -0400 | [diff] [blame] | 4899 | status = nfserrno(nfsd_open_break_lease(cur_fh->fh_dentry->d_inode, |
| 4900 | access)); |
| 4901 | if (status) |
| 4902 | goto out_put_access; |
| 4903 | |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4904 | status = nfsd4_truncate(rqstp, cur_fh, open); |
| 4905 | if (status) |
| 4906 | goto out_put_access; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4907 | out: |
| 4908 | return status; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4909 | out_put_access: |
| 4910 | stp->st_access_bmap = old_access_bmap; |
| 4911 | nfs4_file_put_access(fp, open->op_share_access); |
| 4912 | reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp); |
| 4913 | goto out; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4914 | } |
| 4915 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4916 | static __be32 |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4917 | 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] | 4918 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4919 | __be32 status; |
Arnd Bergmann | 6ac7536 | 2015-05-12 23:31:29 +0200 | [diff] [blame] | 4920 | unsigned char old_deny_bmap = stp->st_deny_bmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4921 | |
Jeff Layton | 6eb3a1d | 2014-07-10 14:07:31 -0400 | [diff] [blame] | 4922 | if (!test_access(open->op_share_access, stp)) |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4923 | return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open); |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4924 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4925 | /* test and set deny mode */ |
| 4926 | spin_lock(&fp->fi_lock); |
| 4927 | status = nfs4_file_check_deny(fp, open->op_share_deny); |
| 4928 | if (status == nfs_ok) { |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4929 | set_deny(open->op_share_deny, stp); |
| 4930 | fp->fi_share_deny |= |
| 4931 | (open->op_share_deny & NFS4_SHARE_DENY_BOTH); |
| 4932 | } |
| 4933 | spin_unlock(&fp->fi_lock); |
| 4934 | |
| 4935 | if (status != nfs_ok) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4936 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4937 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4938 | status = nfsd4_truncate(rqstp, cur_fh, open); |
| 4939 | if (status != nfs_ok) |
| 4940 | reset_union_bmap_deny(old_deny_bmap, stp); |
| 4941 | return status; |
| 4942 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4943 | |
J. Bruce Fields | 14a24e9 | 2010-12-10 19:02:49 -0500 | [diff] [blame] | 4944 | /* Should we give out recallable state?: */ |
| 4945 | static bool nfsd4_cb_channel_good(struct nfs4_client *clp) |
| 4946 | { |
| 4947 | if (clp->cl_cb_state == NFSD4_CB_UP) |
| 4948 | return true; |
| 4949 | /* |
| 4950 | * In the sessions case, since we don't have to establish a |
| 4951 | * separate connection for callbacks, we assume it's OK |
| 4952 | * until we hear otherwise: |
| 4953 | */ |
| 4954 | return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN; |
| 4955 | } |
| 4956 | |
J. Bruce Fields | 653e514 | 2018-02-15 22:08:45 -0500 | [diff] [blame] | 4957 | static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, |
| 4958 | int flag) |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 4959 | { |
| 4960 | struct file_lock *fl; |
| 4961 | |
| 4962 | fl = locks_alloc_lock(); |
| 4963 | if (!fl) |
| 4964 | return NULL; |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 4965 | fl->fl_lmops = &nfsd_lease_mng_ops; |
J. Bruce Fields | 617588d | 2011-07-01 15:18:34 -0400 | [diff] [blame] | 4966 | fl->fl_flags = FL_DELEG; |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 4967 | fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK; |
| 4968 | fl->fl_end = OFFSET_MAX; |
J. Bruce Fields | 653e514 | 2018-02-15 22:08:45 -0500 | [diff] [blame] | 4969 | fl->fl_owner = (fl_owner_t)dp; |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 4970 | fl->fl_pid = current->tgid; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 4971 | 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] | 4972 | return fl; |
| 4973 | } |
| 4974 | |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 4975 | static struct nfs4_delegation * |
| 4976 | nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh, |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 4977 | struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate) |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 4978 | { |
J. Bruce Fields | 68b18f5 | 2018-02-19 11:38:33 -0500 | [diff] [blame] | 4979 | int status = 0; |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 4980 | struct nfs4_delegation *dp; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 4981 | struct nfsd_file *nf; |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 4982 | struct file_lock *fl; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4983 | |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 4984 | /* |
| 4985 | * The fi_had_conflict and nfs_get_existing_delegation checks |
| 4986 | * here are just optimizations; we'll need to recheck them at |
| 4987 | * the end: |
| 4988 | */ |
J. Bruce Fields | bf7bd3e | 2013-08-15 16:55:26 -0400 | [diff] [blame] | 4989 | if (fp->fi_had_conflict) |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 4990 | return ERR_PTR(-EAGAIN); |
| 4991 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 4992 | nf = find_readable_file(fp); |
| 4993 | if (!nf) { |
J. Bruce Fields | 6ee65a77 | 2021-03-08 10:52:29 -0500 | [diff] [blame] | 4994 | /* We should always have a readable file here */ |
| 4995 | WARN_ON_ONCE(1); |
| 4996 | return ERR_PTR(-EBADF); |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 4997 | } |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 4998 | spin_lock(&state_lock); |
| 4999 | spin_lock(&fp->fi_lock); |
J. Bruce Fields | 68b18f5 | 2018-02-19 11:38:33 -0500 | [diff] [blame] | 5000 | if (nfs4_delegation_exists(clp, fp)) |
| 5001 | status = -EAGAIN; |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 5002 | else if (!fp->fi_deleg_file) { |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5003 | fp->fi_deleg_file = nf; |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 5004 | /* increment early to prevent fi_deleg_file from being |
| 5005 | * cleared */ |
| 5006 | fp->fi_delegees = 1; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5007 | nf = NULL; |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 5008 | } else |
| 5009 | fp->fi_delegees++; |
| 5010 | spin_unlock(&fp->fi_lock); |
| 5011 | spin_unlock(&state_lock); |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5012 | if (nf) |
| 5013 | nfsd_file_put(nf); |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 5014 | if (status) |
| 5015 | return ERR_PTR(status); |
| 5016 | |
| 5017 | status = -ENOMEM; |
| 5018 | dp = alloc_init_deleg(clp, fp, fh, odstate); |
| 5019 | if (!dp) |
| 5020 | goto out_delegees; |
| 5021 | |
| 5022 | fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ); |
| 5023 | if (!fl) |
Andrew Elble | bd8d725 | 2018-10-05 09:32:08 -0400 | [diff] [blame] | 5024 | goto out_clnt_odstate; |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 5025 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5026 | 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] | 5027 | if (fl) |
| 5028 | locks_free_lock(fl); |
| 5029 | if (status) |
| 5030 | goto out_clnt_odstate; |
| 5031 | |
| 5032 | spin_lock(&state_lock); |
| 5033 | spin_lock(&fp->fi_lock); |
| 5034 | if (fp->fi_had_conflict) |
| 5035 | status = -EAGAIN; |
| 5036 | else |
| 5037 | status = hash_delegation_locked(dp, fp); |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 5038 | spin_unlock(&fp->fi_lock); |
| 5039 | spin_unlock(&state_lock); |
| 5040 | |
| 5041 | if (status) |
Andrew Elble | 692ad28 | 2018-04-18 17:04:37 -0400 | [diff] [blame] | 5042 | goto out_unlock; |
| 5043 | |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 5044 | return dp; |
Andrew Elble | 692ad28 | 2018-04-18 17:04:37 -0400 | [diff] [blame] | 5045 | out_unlock: |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5046 | 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] | 5047 | out_clnt_odstate: |
| 5048 | put_clnt_odstate(dp->dl_clnt_odstate); |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 5049 | nfs4_put_stid(&dp->dl_stid); |
| 5050 | out_delegees: |
| 5051 | put_deleg_file(fp); |
| 5052 | return ERR_PTR(status); |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 5053 | } |
| 5054 | |
Benny Halevy | 4aa8913 | 2012-02-21 14:16:44 -0800 | [diff] [blame] | 5055 | static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status) |
| 5056 | { |
| 5057 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 5058 | if (status == -EAGAIN) |
| 5059 | open->op_why_no_deleg = WND4_CONTENTION; |
| 5060 | else { |
| 5061 | open->op_why_no_deleg = WND4_RESOURCE; |
| 5062 | switch (open->op_deleg_want) { |
| 5063 | case NFS4_SHARE_WANT_READ_DELEG: |
| 5064 | case NFS4_SHARE_WANT_WRITE_DELEG: |
| 5065 | case NFS4_SHARE_WANT_ANY_DELEG: |
| 5066 | break; |
| 5067 | case NFS4_SHARE_WANT_CANCEL: |
| 5068 | open->op_why_no_deleg = WND4_CANCELLED; |
| 5069 | break; |
| 5070 | case NFS4_SHARE_WANT_NO_DELEG: |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 5071 | WARN_ON_ONCE(1); |
Benny Halevy | 4aa8913 | 2012-02-21 14:16:44 -0800 | [diff] [blame] | 5072 | } |
| 5073 | } |
| 5074 | } |
| 5075 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5076 | /* |
| 5077 | * Attempt to hand out a delegation. |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 5078 | * |
| 5079 | * Note we don't support write delegations, and won't until the vfs has |
| 5080 | * proper support for them. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5081 | */ |
| 5082 | static void |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame] | 5083 | nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, |
| 5084 | struct nfs4_ol_stateid *stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5085 | { |
| 5086 | struct nfs4_delegation *dp; |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame] | 5087 | struct nfs4_openowner *oo = openowner(stp->st_stateowner); |
| 5088 | struct nfs4_client *clp = stp->st_stid.sc_client; |
J. Bruce Fields | 14a24e9 | 2010-12-10 19:02:49 -0500 | [diff] [blame] | 5089 | int cb_up; |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 5090 | int status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5091 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5092 | cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client); |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 5093 | open->op_recall = 0; |
| 5094 | switch (open->op_claim_type) { |
| 5095 | case NFS4_OPEN_CLAIM_PREVIOUS: |
J. Bruce Fields | 2bf2387 | 2010-03-08 12:37:27 -0500 | [diff] [blame] | 5096 | if (!cb_up) |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 5097 | open->op_recall = 1; |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 5098 | if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ) |
| 5099 | goto out_no_deleg; |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 5100 | break; |
| 5101 | case NFS4_OPEN_CLAIM_NULL: |
Ming Chen | ed47b06 | 2014-01-09 21:26:10 +0000 | [diff] [blame] | 5102 | case NFS4_OPEN_CLAIM_FH: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 5103 | /* |
| 5104 | * Let's not give out any delegations till everyone's |
J. Bruce Fields | c87fb4a | 2015-08-06 12:47:02 -0400 | [diff] [blame] | 5105 | * had the chance to reclaim theirs, *and* until |
| 5106 | * NLM locks have all been reclaimed: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 5107 | */ |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame] | 5108 | if (locks_in_grace(clp->net)) |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 5109 | goto out_no_deleg; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 5110 | if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED)) |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 5111 | goto out_no_deleg; |
J. Bruce Fields | 6ee65a77 | 2021-03-08 10:52:29 -0500 | [diff] [blame] | 5112 | /* |
| 5113 | * Also, if the file was opened for write or |
| 5114 | * create, there's a good chance the client's |
| 5115 | * about to write to it, resulting in an |
| 5116 | * immediate recall (since we don't support |
| 5117 | * write delegations): |
| 5118 | */ |
| 5119 | if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) |
| 5120 | goto out_no_deleg; |
| 5121 | if (open->op_create == NFS4_OPEN_CREATE) |
| 5122 | goto out_no_deleg; |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 5123 | break; |
| 5124 | default: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 5125 | goto out_no_deleg; |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 5126 | } |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 5127 | 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] | 5128 | if (IS_ERR(dp)) |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 5129 | goto out_no_deleg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5130 | |
J. Bruce Fields | d5477a8 | 2011-09-08 12:07:44 -0400 | [diff] [blame] | 5131 | 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] | 5132 | |
Hou Tao | 3caf917 | 2020-08-28 15:02:55 +0800 | [diff] [blame] | 5133 | trace_nfsd_deleg_read(&dp->dl_stid.sc_stateid); |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 5134 | open->op_delegate_type = NFS4_OPEN_DELEGATE_READ; |
Trond Myklebust | 67cb127 | 2014-07-29 21:34:17 -0400 | [diff] [blame] | 5135 | nfs4_put_stid(&dp->dl_stid); |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 5136 | return; |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 5137 | out_no_deleg: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 5138 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE; |
| 5139 | if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS && |
J. Bruce Fields | d08d32e | 2013-06-21 11:05:32 -0400 | [diff] [blame] | 5140 | open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) { |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 5141 | dprintk("NFSD: WARNING: refusing delegation reclaim\n"); |
J. Bruce Fields | d08d32e | 2013-06-21 11:05:32 -0400 | [diff] [blame] | 5142 | open->op_recall = 1; |
| 5143 | } |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 5144 | |
| 5145 | /* 4.1 client asking for a delegation? */ |
| 5146 | if (open->op_deleg_want) |
| 5147 | nfsd4_open_deleg_none_ext(open, status); |
| 5148 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5149 | } |
| 5150 | |
Benny Halevy | e27f49c | 2012-02-21 14:16:54 -0800 | [diff] [blame] | 5151 | static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open, |
| 5152 | struct nfs4_delegation *dp) |
| 5153 | { |
| 5154 | if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG && |
| 5155 | dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) { |
| 5156 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 5157 | open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE; |
| 5158 | } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG && |
| 5159 | dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) { |
| 5160 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 5161 | open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE; |
| 5162 | } |
| 5163 | /* Otherwise the client must be confused wanting a delegation |
| 5164 | * it already has, therefore we don't return |
| 5165 | * NFS4_OPEN_DELEGATE_NONE_EXT and reason. |
| 5166 | */ |
| 5167 | } |
| 5168 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5169 | __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5170 | nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open) |
| 5171 | { |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 5172 | struct nfsd4_compoundres *resp = rqstp->rq_resp; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5173 | struct nfs4_client *cl = open->op_openowner->oo_owner.so_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5174 | struct nfs4_file *fp = NULL; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5175 | struct nfs4_ol_stateid *stp = NULL; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 5176 | struct nfs4_delegation *dp = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5177 | __be32 status; |
Trond Myklebust | d8a1a00 | 2017-11-03 08:00:11 -0400 | [diff] [blame] | 5178 | bool new_stp = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5179 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5180 | /* |
| 5181 | * Lookup file; if found, lookup stateid and check open request, |
| 5182 | * and check for delegations in the process of being recalled. |
| 5183 | * If not found, create the nfs4_file struct |
| 5184 | */ |
J. Bruce Fields | f9b60e2 | 2021-04-16 14:00:15 -0400 | [diff] [blame] | 5185 | fp = find_or_add_file(open->op_file, current_fh); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 5186 | if (fp != open->op_file) { |
J. Bruce Fields | 41d2266 | 2013-03-21 15:49:47 -0400 | [diff] [blame] | 5187 | status = nfs4_check_deleg(cl, open, &dp); |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 5188 | if (status) |
| 5189 | goto out; |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 5190 | stp = nfsd4_find_and_lock_existing_open(fp, open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5191 | } else { |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 5192 | open->op_file = NULL; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 5193 | status = nfserr_bad_stateid; |
J. Bruce Fields | 8b289b2 | 2011-10-19 11:52:12 -0400 | [diff] [blame] | 5194 | if (nfsd4_is_deleg_cur(open)) |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 5195 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5196 | } |
| 5197 | |
Trond Myklebust | d8a1a00 | 2017-11-03 08:00:11 -0400 | [diff] [blame] | 5198 | if (!stp) { |
| 5199 | stp = init_open_stateid(fp, open); |
| 5200 | if (!open->op_stp) |
| 5201 | new_stp = true; |
| 5202 | } |
| 5203 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5204 | /* |
| 5205 | * OPEN the file, or upgrade an existing OPEN. |
| 5206 | * If truncate fails, the OPEN fails. |
Trond Myklebust | d8a1a00 | 2017-11-03 08:00:11 -0400 | [diff] [blame] | 5207 | * |
| 5208 | * stp is already locked. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5209 | */ |
Trond Myklebust | d8a1a00 | 2017-11-03 08:00:11 -0400 | [diff] [blame] | 5210 | if (!new_stp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5211 | /* Stateid was found, this is an OPEN upgrade */ |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 5212 | status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open); |
Jeff Layton | 35a92fe | 2015-09-17 07:47:08 -0400 | [diff] [blame] | 5213 | if (status) { |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 5214 | mutex_unlock(&stp->st_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5215 | goto out; |
Jeff Layton | 35a92fe | 2015-09-17 07:47:08 -0400 | [diff] [blame] | 5216 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5217 | } else { |
Jeff Layton | 6eb3a1d | 2014-07-10 14:07:31 -0400 | [diff] [blame] | 5218 | status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open); |
| 5219 | if (status) { |
Trond Myklebust | d8a1a00 | 2017-11-03 08:00:11 -0400 | [diff] [blame] | 5220 | stp->st_stid.sc_type = NFS4_CLOSED_STID; |
Jeff Layton | 6eb3a1d | 2014-07-10 14:07:31 -0400 | [diff] [blame] | 5221 | release_open_stateid(stp); |
Trond Myklebust | d8a1a00 | 2017-11-03 08:00:11 -0400 | [diff] [blame] | 5222 | mutex_unlock(&stp->st_mutex); |
Jeff Layton | 6eb3a1d | 2014-07-10 14:07:31 -0400 | [diff] [blame] | 5223 | goto out; |
| 5224 | } |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 5225 | |
| 5226 | stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp, |
| 5227 | open->op_odstate); |
| 5228 | if (stp->st_clnt_odstate == open->op_odstate) |
| 5229 | open->op_odstate = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5230 | } |
Trond Myklebust | d8a1a00 | 2017-11-03 08:00:11 -0400 | [diff] [blame] | 5231 | |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 5232 | nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid); |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 5233 | mutex_unlock(&stp->st_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5234 | |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 5235 | if (nfsd4_has_session(&resp->cstate)) { |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 5236 | if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) { |
| 5237 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 5238 | open->op_why_no_deleg = WND4_NOT_WANTED; |
| 5239 | goto nodeleg; |
| 5240 | } |
| 5241 | } |
| 5242 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5243 | /* |
| 5244 | * Attempt to hand out a delegation. No error return, because the |
| 5245 | * OPEN succeeds even if we fail. |
| 5246 | */ |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame] | 5247 | nfs4_open_delegation(current_fh, open, stp); |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 5248 | nodeleg: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5249 | status = nfs_ok; |
Hou Tao | 3caf917 | 2020-08-28 15:02:55 +0800 | [diff] [blame] | 5250 | trace_nfsd_open(&stp->st_stid.sc_stateid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5251 | out: |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 5252 | /* 4.1 client trying to upgrade/downgrade delegation? */ |
| 5253 | if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp && |
Benny Halevy | e27f49c | 2012-02-21 14:16:54 -0800 | [diff] [blame] | 5254 | open->op_deleg_want) |
| 5255 | nfsd4_deleg_xgrade_none_ext(open, dp); |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 5256 | |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 5257 | if (fp) |
| 5258 | put_nfs4_file(fp); |
NeilBrown | 3751517 | 2005-07-07 17:59:16 -0700 | [diff] [blame] | 5259 | if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS) |
Kinglong Mee | 8718602 | 2015-07-13 17:32:59 +0800 | [diff] [blame] | 5260 | open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5261 | /* |
| 5262 | * To finish the open response, we just need to set the rflags. |
| 5263 | */ |
| 5264 | open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX; |
Jeff Layton | 19e4c34 | 2016-09-16 16:28:27 -0400 | [diff] [blame] | 5265 | if (nfsd4_has_session(&resp->cstate)) |
| 5266 | open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK; |
| 5267 | else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5268 | open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM; |
Jeff Layton | 19e4c34 | 2016-09-16 16:28:27 -0400 | [diff] [blame] | 5269 | |
Trond Myklebust | dcd94cc | 2014-07-29 21:34:18 -0400 | [diff] [blame] | 5270 | if (dp) |
| 5271 | nfs4_put_stid(&dp->dl_stid); |
Trond Myklebust | d6f2bc5 | 2014-07-29 21:34:19 -0400 | [diff] [blame] | 5272 | if (stp) |
| 5273 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5274 | |
| 5275 | return status; |
| 5276 | } |
| 5277 | |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 5278 | void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate, |
Jeff Layton | 4229789 | 2015-03-23 10:53:44 -0400 | [diff] [blame] | 5279 | struct nfsd4_open *open) |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 5280 | { |
| 5281 | if (open->op_openowner) { |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 5282 | struct nfs4_stateowner *so = &open->op_openowner->oo_owner; |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 5283 | |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 5284 | nfsd4_cstate_assign_replay(cstate, so); |
| 5285 | nfs4_put_stateowner(so); |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 5286 | } |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 5287 | if (open->op_file) |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 5288 | kmem_cache_free(file_slab, open->op_file); |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 5289 | if (open->op_stp) |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 5290 | nfs4_put_stid(&open->op_stp->st_stid); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 5291 | if (open->op_odstate) |
| 5292 | kmem_cache_free(odstate_slab, open->op_odstate); |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 5293 | } |
| 5294 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5295 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 5296 | nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5297 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5298 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5299 | clientid_t *clid = &u->renew; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5300 | struct nfs4_client *clp; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5301 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 5302 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5303 | |
Chuck Lever | dd5e3fb | 2020-04-05 11:42:29 -0400 | [diff] [blame] | 5304 | trace_nfsd_clid_renew(clid); |
J. Bruce Fields | f71475b | 2021-01-21 17:57:43 -0500 | [diff] [blame] | 5305 | status = set_client(clid, cstate, nn); |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 5306 | if (status) |
J. Bruce Fields | b4587eb | 2021-01-21 17:57:39 -0500 | [diff] [blame] | 5307 | return status; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 5308 | clp = cstate->clp; |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 5309 | if (!list_empty(&clp->cl_delegations) |
J. Bruce Fields | 77a3569 | 2010-04-30 18:51:44 -0400 | [diff] [blame] | 5310 | && clp->cl_cb_state != NFSD4_CB_UP) |
J. Bruce Fields | b4587eb | 2021-01-21 17:57:39 -0500 | [diff] [blame] | 5311 | return nfserr_cb_path_down; |
| 5312 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5313 | } |
| 5314 | |
Jeff Layton | 7f5ef2e | 2014-09-12 16:40:21 -0400 | [diff] [blame] | 5315 | void |
Stanislav Kinsbursky | 12760c6 | 2012-11-14 18:22:12 +0300 | [diff] [blame] | 5316 | nfsd4_end_grace(struct nfsd_net *nn) |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 5317 | { |
Jeff Layton | 33dcc48 | 2012-04-10 11:08:48 -0400 | [diff] [blame] | 5318 | /* do nothing if grace period already ended */ |
Stanislav Kinsbursky | a51c84e | 2012-07-25 16:57:37 +0400 | [diff] [blame] | 5319 | if (nn->grace_ended) |
Jeff Layton | 33dcc48 | 2012-04-10 11:08:48 -0400 | [diff] [blame] | 5320 | return; |
| 5321 | |
Chuck Lever | dd5e3fb | 2020-04-05 11:42:29 -0400 | [diff] [blame] | 5322 | trace_nfsd_grace_complete(nn); |
Stanislav Kinsbursky | a51c84e | 2012-07-25 16:57:37 +0400 | [diff] [blame] | 5323 | nn->grace_ended = true; |
J. Bruce Fields | 70b2823 | 2014-09-16 17:37:32 -0400 | [diff] [blame] | 5324 | /* |
| 5325 | * If the server goes down again right now, an NFSv4 |
| 5326 | * client will still be allowed to reclaim after it comes back up, |
| 5327 | * even if it hasn't yet had a chance to reclaim state this time. |
| 5328 | * |
| 5329 | */ |
Jeff Layton | 919b804 | 2014-09-12 16:40:20 -0400 | [diff] [blame] | 5330 | nfsd4_record_grace_done(nn); |
J. Bruce Fields | 70b2823 | 2014-09-16 17:37:32 -0400 | [diff] [blame] | 5331 | /* |
| 5332 | * At this point, NFSv4 clients can still reclaim. But if the |
| 5333 | * server crashes, any that have not yet reclaimed will be out |
| 5334 | * of luck on the next boot. |
| 5335 | * |
| 5336 | * (NFSv4.1+ clients are considered to have reclaimed once they |
| 5337 | * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to |
| 5338 | * have reclaimed after their first OPEN.) |
| 5339 | */ |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 5340 | locks_end_grace(&nn->nfsd4_manager); |
J. Bruce Fields | 70b2823 | 2014-09-16 17:37:32 -0400 | [diff] [blame] | 5341 | /* |
| 5342 | * At this point, and once lockd and/or any other containers |
| 5343 | * exit their grace period, further reclaims will fail and |
| 5344 | * regular locking can resume. |
| 5345 | */ |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 5346 | } |
| 5347 | |
J. Bruce Fields | 03f318c | 2018-06-08 12:28:47 -0400 | [diff] [blame] | 5348 | /* |
| 5349 | * If we've waited a lease period but there are still clients trying to |
| 5350 | * reclaim, wait a little longer to give them a chance to finish. |
| 5351 | */ |
| 5352 | static bool clients_still_reclaiming(struct nfsd_net *nn) |
| 5353 | { |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 5354 | time64_t double_grace_period_end = nn->boot_time + |
| 5355 | 2 * nn->nfsd4_lease; |
J. Bruce Fields | 03f318c | 2018-06-08 12:28:47 -0400 | [diff] [blame] | 5356 | |
Scott Mayhew | 362063a | 2019-03-26 18:06:28 -0400 | [diff] [blame] | 5357 | if (nn->track_reclaim_completes && |
| 5358 | atomic_read(&nn->nr_reclaim_complete) == |
| 5359 | nn->reclaim_str_hashtbl_size) |
| 5360 | return false; |
J. Bruce Fields | 03f318c | 2018-06-08 12:28:47 -0400 | [diff] [blame] | 5361 | if (!nn->somebody_reclaimed) |
| 5362 | return false; |
| 5363 | nn->somebody_reclaimed = false; |
| 5364 | /* |
| 5365 | * If we've given them *two* lease times to reclaim, and they're |
| 5366 | * still not done, give up: |
| 5367 | */ |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 5368 | if (ktime_get_boottime_seconds() > double_grace_period_end) |
J. Bruce Fields | 03f318c | 2018-06-08 12:28:47 -0400 | [diff] [blame] | 5369 | return false; |
| 5370 | return true; |
| 5371 | } |
| 5372 | |
J. Bruce Fields | 7f7e7a4 | 2021-03-02 10:46:23 -0500 | [diff] [blame] | 5373 | struct laundry_time { |
| 5374 | time64_t cutoff; |
| 5375 | time64_t new_timeo; |
| 5376 | }; |
| 5377 | |
| 5378 | static bool state_expired(struct laundry_time *lt, time64_t last_refresh) |
| 5379 | { |
| 5380 | time64_t time_remaining; |
| 5381 | |
| 5382 | if (last_refresh < lt->cutoff) |
| 5383 | return true; |
| 5384 | time_remaining = last_refresh - lt->cutoff; |
| 5385 | lt->new_timeo = min(lt->new_timeo, time_remaining); |
| 5386 | return false; |
| 5387 | } |
| 5388 | |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 5389 | static time64_t |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 5390 | nfs4_laundromat(struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5391 | { |
| 5392 | struct nfs4_client *clp; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5393 | struct nfs4_openowner *oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5394 | struct nfs4_delegation *dp; |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 5395 | struct nfs4_ol_stateid *stp; |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 5396 | struct nfsd4_blocked_lock *nbl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5397 | struct list_head *pos, *next, reaplist; |
J. Bruce Fields | 7f7e7a4 | 2021-03-02 10:46:23 -0500 | [diff] [blame] | 5398 | struct laundry_time lt = { |
| 5399 | .cutoff = ktime_get_boottime_seconds() - nn->nfsd4_lease, |
| 5400 | .new_timeo = nn->nfsd4_lease |
| 5401 | }; |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 5402 | struct nfs4_cpntf_state *cps; |
| 5403 | copy_stateid_t *cps_t; |
| 5404 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5405 | |
J. Bruce Fields | 03f318c | 2018-06-08 12:28:47 -0400 | [diff] [blame] | 5406 | if (clients_still_reclaiming(nn)) { |
J. Bruce Fields | 7f7e7a4 | 2021-03-02 10:46:23 -0500 | [diff] [blame] | 5407 | lt.new_timeo = 0; |
J. Bruce Fields | 03f318c | 2018-06-08 12:28:47 -0400 | [diff] [blame] | 5408 | goto out; |
| 5409 | } |
Stanislav Kinsbursky | 12760c6 | 2012-11-14 18:22:12 +0300 | [diff] [blame] | 5410 | nfsd4_end_grace(nn); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 5411 | INIT_LIST_HEAD(&reaplist); |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 5412 | |
| 5413 | spin_lock(&nn->s2s_cp_lock); |
| 5414 | idr_for_each_entry(&nn->s2s_cp_stateids, cps_t, i) { |
| 5415 | cps = container_of(cps_t, struct nfs4_cpntf_state, cp_stateid); |
| 5416 | if (cps->cp_stateid.sc_type == NFS4_COPYNOTIFY_STID && |
J. Bruce Fields | 7f7e7a4 | 2021-03-02 10:46:23 -0500 | [diff] [blame] | 5417 | state_expired(<, cps->cpntf_time)) |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 5418 | _free_cpntf_state_locked(nn, cps); |
| 5419 | } |
| 5420 | spin_unlock(&nn->s2s_cp_lock); |
| 5421 | |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 5422 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 5ed58bb | 2012-11-14 18:21:56 +0300 | [diff] [blame] | 5423 | list_for_each_safe(pos, next, &nn->client_lru) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5424 | clp = list_entry(pos, struct nfs4_client, cl_lru); |
J. Bruce Fields | 7f7e7a4 | 2021-03-02 10:46:23 -0500 | [diff] [blame] | 5425 | if (!state_expired(<, clp->cl_time)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5426 | break; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 5427 | if (mark_client_expired_locked(clp)) { |
Chuck Lever | dd5e3fb | 2020-04-05 11:42:29 -0400 | [diff] [blame] | 5428 | trace_nfsd_clid_expired(&clp->cl_clientid); |
Benny Halevy | d768298 | 2010-05-12 00:13:54 +0300 | [diff] [blame] | 5429 | continue; |
| 5430 | } |
Trond Myklebust | 4864af9 | 2014-07-30 08:27:03 -0400 | [diff] [blame] | 5431 | list_add(&clp->cl_lru, &reaplist); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 5432 | } |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 5433 | spin_unlock(&nn->client_lock); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 5434 | list_for_each_safe(pos, next, &reaplist) { |
| 5435 | clp = list_entry(pos, struct nfs4_client, cl_lru); |
Chuck Lever | dd5e3fb | 2020-04-05 11:42:29 -0400 | [diff] [blame] | 5436 | trace_nfsd_clid_purged(&clp->cl_clientid); |
Trond Myklebust | 4864af9 | 2014-07-30 08:27:03 -0400 | [diff] [blame] | 5437 | list_del_init(&clp->cl_lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5438 | expire_client(clp); |
| 5439 | } |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5440 | spin_lock(&state_lock); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 5441 | list_for_each_safe(pos, next, &nn->del_recall_lru) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5442 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
J. Bruce Fields | 7f7e7a4 | 2021-03-02 10:46:23 -0500 | [diff] [blame] | 5443 | if (!state_expired(<, dp->dl_time)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5444 | break; |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 5445 | WARN_ON(!unhash_delegation_locked(dp)); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 5446 | list_add(&dp->dl_recall_lru, &reaplist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5447 | } |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5448 | spin_unlock(&state_lock); |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 5449 | while (!list_empty(&reaplist)) { |
| 5450 | dp = list_first_entry(&reaplist, struct nfs4_delegation, |
| 5451 | dl_recall_lru); |
| 5452 | list_del_init(&dp->dl_recall_lru); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 5453 | revoke_delegation(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5454 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 5455 | |
| 5456 | spin_lock(&nn->client_lock); |
| 5457 | while (!list_empty(&nn->close_lru)) { |
| 5458 | oo = list_first_entry(&nn->close_lru, struct nfs4_openowner, |
| 5459 | oo_close_lru); |
J. Bruce Fields | 7f7e7a4 | 2021-03-02 10:46:23 -0500 | [diff] [blame] | 5460 | if (!state_expired(<, oo->oo_time)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5461 | break; |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 5462 | list_del_init(&oo->oo_close_lru); |
| 5463 | stp = oo->oo_last_closed_stid; |
| 5464 | oo->oo_last_closed_stid = NULL; |
| 5465 | spin_unlock(&nn->client_lock); |
| 5466 | nfs4_put_stid(&stp->st_stid); |
| 5467 | spin_lock(&nn->client_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5468 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 5469 | spin_unlock(&nn->client_lock); |
| 5470 | |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 5471 | /* |
| 5472 | * It's possible for a client to try and acquire an already held lock |
| 5473 | * that is being held for a long time, and then lose interest in it. |
| 5474 | * So, we clean out any un-revisited request after a lease period |
| 5475 | * under the assumption that the client is no longer interested. |
| 5476 | * |
| 5477 | * RFC5661, sec. 9.6 states that the client must not rely on getting |
| 5478 | * notifications and must continue to poll for locks, even when the |
| 5479 | * server supports them. Thus this shouldn't lead to clients blocking |
| 5480 | * indefinitely once the lock does become free. |
| 5481 | */ |
| 5482 | BUG_ON(!list_empty(&reaplist)); |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 5483 | spin_lock(&nn->blocked_locks_lock); |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 5484 | while (!list_empty(&nn->blocked_locks_lru)) { |
| 5485 | nbl = list_first_entry(&nn->blocked_locks_lru, |
| 5486 | struct nfsd4_blocked_lock, nbl_lru); |
J. Bruce Fields | 7f7e7a4 | 2021-03-02 10:46:23 -0500 | [diff] [blame] | 5487 | if (!state_expired(<, nbl->nbl_time)) |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 5488 | break; |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 5489 | list_move(&nbl->nbl_lru, &reaplist); |
| 5490 | list_del_init(&nbl->nbl_list); |
| 5491 | } |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 5492 | spin_unlock(&nn->blocked_locks_lock); |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 5493 | |
| 5494 | while (!list_empty(&reaplist)) { |
Naofumi Honda | 64ebe124 | 2017-11-09 10:57:16 -0500 | [diff] [blame] | 5495 | nbl = list_first_entry(&reaplist, |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 5496 | struct nfsd4_blocked_lock, nbl_lru); |
| 5497 | list_del_init(&nbl->nbl_lru); |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 5498 | free_blocked_lock(nbl); |
| 5499 | } |
J. Bruce Fields | 03f318c | 2018-06-08 12:28:47 -0400 | [diff] [blame] | 5500 | out: |
J. Bruce Fields | 7f7e7a4 | 2021-03-02 10:46:23 -0500 | [diff] [blame] | 5501 | return max_t(time64_t, lt.new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5502 | } |
| 5503 | |
Harvey Harrison | a254b24 | 2008-02-20 12:49:00 -0800 | [diff] [blame] | 5504 | static struct workqueue_struct *laundry_wq; |
| 5505 | static void laundromat_main(struct work_struct *); |
Harvey Harrison | a254b24 | 2008-02-20 12:49:00 -0800 | [diff] [blame] | 5506 | |
| 5507 | static void |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 5508 | laundromat_main(struct work_struct *laundry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5509 | { |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 5510 | time64_t t; |
Geliang Tang | 2e55f3a | 2016-01-01 22:06:28 +0800 | [diff] [blame] | 5511 | struct delayed_work *dwork = to_delayed_work(laundry); |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 5512 | struct nfsd_net *nn = container_of(dwork, struct nfsd_net, |
| 5513 | laundromat_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5514 | |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 5515 | t = nfs4_laundromat(nn); |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 5516 | queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5517 | } |
| 5518 | |
Jeff Layton | 8fcd461 | 2015-07-30 06:57:46 -0400 | [diff] [blame] | 5519 | 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] | 5520 | { |
Jeff Layton | 8fcd461 | 2015-07-30 06:57:46 -0400 | [diff] [blame] | 5521 | if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle)) |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 5522 | return nfserr_bad_stateid; |
| 5523 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5524 | } |
| 5525 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5526 | static inline int |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 5527 | access_permit_read(struct nfs4_ol_stateid *stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5528 | { |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 5529 | return test_access(NFS4_SHARE_ACCESS_READ, stp) || |
| 5530 | test_access(NFS4_SHARE_ACCESS_BOTH, stp) || |
| 5531 | test_access(NFS4_SHARE_ACCESS_WRITE, stp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5532 | } |
| 5533 | |
| 5534 | static inline int |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 5535 | access_permit_write(struct nfs4_ol_stateid *stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5536 | { |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 5537 | return test_access(NFS4_SHARE_ACCESS_WRITE, stp) || |
| 5538 | test_access(NFS4_SHARE_ACCESS_BOTH, stp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5539 | } |
| 5540 | |
| 5541 | static |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5542 | __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5543 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5544 | __be32 status = nfserr_openmode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5545 | |
J. Bruce Fields | 0292191 | 2010-07-29 15:16:59 -0400 | [diff] [blame] | 5546 | /* For lock stateid's, we test the parent open, not the lock: */ |
| 5547 | if (stp->st_openstp) |
| 5548 | stp = stp->st_openstp; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 5549 | if ((flags & WR_STATE) && !access_permit_write(stp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5550 | goto out; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 5551 | if ((flags & RD_STATE) && !access_permit_read(stp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5552 | goto out; |
| 5553 | status = nfs_ok; |
| 5554 | out: |
| 5555 | return status; |
| 5556 | } |
| 5557 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5558 | static inline __be32 |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 5559 | 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] | 5560 | { |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 5561 | if (ONE_STATEID(stateid) && (flags & RD_STATE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5562 | return nfs_ok; |
J. Bruce Fields | c87fb4a | 2015-08-06 12:47:02 -0400 | [diff] [blame] | 5563 | else if (opens_in_grace(net)) { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 5564 | /* Answer in remaining cases depends on existence of |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5565 | * conflicting state; so we must wait out the grace period. */ |
| 5566 | return nfserr_grace; |
| 5567 | } else if (flags & WR_STATE) |
| 5568 | return nfs4_share_conflict(current_fh, |
| 5569 | NFS4_SHARE_DENY_WRITE); |
| 5570 | else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */ |
| 5571 | return nfs4_share_conflict(current_fh, |
| 5572 | NFS4_SHARE_DENY_READ); |
| 5573 | } |
| 5574 | |
| 5575 | /* |
| 5576 | * Allow READ/WRITE during grace period on recovered state only for files |
| 5577 | * that are not able to provide mandatory locking. |
| 5578 | */ |
| 5579 | static inline int |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 5580 | grace_disallows_io(struct net *net, struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5581 | { |
J. Bruce Fields | c87fb4a | 2015-08-06 12:47:02 -0400 | [diff] [blame] | 5582 | return opens_in_grace(net) && mandatory_lock(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5583 | } |
| 5584 | |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 5585 | 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] | 5586 | { |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 5587 | /* |
| 5588 | * When sessions are used the stateid generation number is ignored |
| 5589 | * when it is zero. |
| 5590 | */ |
J. Bruce Fields | 28dde24 | 2011-08-22 10:07:12 -0400 | [diff] [blame] | 5591 | if (has_session && in->si_generation == 0) |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 5592 | return nfs_ok; |
| 5593 | |
| 5594 | if (in->si_generation == ref->si_generation) |
| 5595 | return nfs_ok; |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 5596 | |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 5597 | /* 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] | 5598 | if (nfsd4_stateid_generation_after(in, ref)) |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 5599 | return nfserr_bad_stateid; |
| 5600 | /* |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 5601 | * However, we could see a stateid from the past, even from a |
| 5602 | * non-buggy client. For example, if the client sends a lock |
| 5603 | * while some IO is outstanding, the lock may bump si_generation |
| 5604 | * while the IO is still in flight. The client could avoid that |
| 5605 | * situation by waiting for responses on all the IO requests, |
| 5606 | * but better performance may result in retrying IO that |
| 5607 | * receives an old_stateid error if requests are rarely |
| 5608 | * reordered in flight: |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 5609 | */ |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 5610 | return nfserr_old_stateid; |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 5611 | } |
| 5612 | |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 5613 | static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session) |
| 5614 | { |
| 5615 | __be32 ret; |
| 5616 | |
| 5617 | spin_lock(&s->sc_lock); |
| 5618 | ret = nfsd4_verify_open_stid(s); |
| 5619 | if (ret == nfs_ok) |
| 5620 | ret = check_stateid_generation(in, &s->sc_stateid, has_session); |
| 5621 | spin_unlock(&s->sc_lock); |
| 5622 | return ret; |
| 5623 | } |
| 5624 | |
Christoph Hellwig | ebe9cb3 | 2015-04-28 15:41:15 +0200 | [diff] [blame] | 5625 | static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols) |
| 5626 | { |
| 5627 | if (ols->st_stateowner->so_is_open_owner && |
| 5628 | !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED)) |
| 5629 | return nfserr_bad_stateid; |
| 5630 | return nfs_ok; |
| 5631 | } |
| 5632 | |
Chuck Lever | 7df302f | 2012-05-29 13:56:37 -0400 | [diff] [blame] | 5633 | static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid) |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 5634 | { |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 5635 | struct nfs4_stid *s; |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5636 | __be32 status = nfserr_bad_stateid; |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 5637 | |
Andrew Elble | ae254dac | 2017-11-09 13:41:10 -0500 | [diff] [blame] | 5638 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) || |
| 5639 | CLOSE_STATEID(stateid)) |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5640 | return status; |
J. Bruce Fields | 663e36f | 2020-03-19 10:18:49 -0400 | [diff] [blame] | 5641 | if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5642 | return status; |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5643 | spin_lock(&cl->cl_lock); |
| 5644 | s = find_stateid_locked(cl, stateid); |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 5645 | if (!s) |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5646 | goto out_unlock; |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 5647 | status = nfsd4_stid_check_stateid_generation(stateid, s, 1); |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 5648 | if (status) |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5649 | goto out_unlock; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 5650 | switch (s->sc_type) { |
| 5651 | case NFS4_DELEG_STID: |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5652 | status = nfs_ok; |
| 5653 | break; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 5654 | case NFS4_REVOKED_DELEG_STID: |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5655 | status = nfserr_deleg_revoked; |
| 5656 | break; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 5657 | case NFS4_OPEN_STID: |
| 5658 | case NFS4_LOCK_STID: |
Christoph Hellwig | ebe9cb3 | 2015-04-28 15:41:15 +0200 | [diff] [blame] | 5659 | status = nfsd4_check_openowner_confirmed(openlockstateid(s)); |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5660 | break; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 5661 | default: |
| 5662 | printk("unknown stateid type %x\n", s->sc_type); |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 5663 | fallthrough; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 5664 | case NFS4_CLOSED_STID: |
Trond Myklebust | b0fc29d | 2014-07-16 10:31:59 -0400 | [diff] [blame] | 5665 | case NFS4_CLOSED_DELEG_STID: |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5666 | status = nfserr_bad_stateid; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 5667 | } |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5668 | out_unlock: |
| 5669 | spin_unlock(&cl->cl_lock); |
| 5670 | return status; |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 5671 | } |
| 5672 | |
Christoph Hellwig | cd61c52 | 2014-08-14 08:44:57 +0200 | [diff] [blame] | 5673 | __be32 |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 5674 | nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate, |
| 5675 | stateid_t *stateid, unsigned char typemask, |
| 5676 | struct nfs4_stid **s, struct nfsd_net *nn) |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5677 | { |
J. Bruce Fields | 0eb6f20 | 2013-03-12 17:36:17 -0400 | [diff] [blame] | 5678 | __be32 status; |
Andrew Elble | 95da1b3a | 2017-11-03 14:06:31 -0400 | [diff] [blame] | 5679 | bool return_revoked = false; |
| 5680 | |
| 5681 | /* |
| 5682 | * only return revoked delegations if explicitly asked. |
| 5683 | * otherwise we report revoked or bad_stateid status. |
| 5684 | */ |
| 5685 | if (typemask & NFS4_REVOKED_DELEG_STID) |
| 5686 | return_revoked = true; |
| 5687 | else if (typemask & NFS4_DELEG_STID) |
| 5688 | typemask |= NFS4_REVOKED_DELEG_STID; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5689 | |
Andrew Elble | ae254dac | 2017-11-09 13:41:10 -0500 | [diff] [blame] | 5690 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) || |
| 5691 | CLOSE_STATEID(stateid)) |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5692 | return nfserr_bad_stateid; |
J. Bruce Fields | f71475b | 2021-01-21 17:57:43 -0500 | [diff] [blame] | 5693 | status = set_client(&stateid->si_opaque.so_clid, cstate, nn); |
Trond Myklebust | a8a7c67 | 2014-03-29 14:43:38 -0400 | [diff] [blame] | 5694 | if (status == nfserr_stale_clientid) { |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 5695 | if (cstate->session) |
Trond Myklebust | a8a7c67 | 2014-03-29 14:43:38 -0400 | [diff] [blame] | 5696 | return nfserr_bad_stateid; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5697 | return nfserr_stale_stateid; |
Trond Myklebust | a8a7c67 | 2014-03-29 14:43:38 -0400 | [diff] [blame] | 5698 | } |
J. Bruce Fields | 0eb6f20 | 2013-03-12 17:36:17 -0400 | [diff] [blame] | 5699 | if (status) |
| 5700 | return status; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 5701 | *s = find_stateid_by_type(cstate->clp, stateid, typemask); |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5702 | if (!*s) |
| 5703 | return nfserr_bad_stateid; |
Andrew Elble | 95da1b3a | 2017-11-03 14:06:31 -0400 | [diff] [blame] | 5704 | if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) { |
| 5705 | nfs4_put_stid(*s); |
| 5706 | if (cstate->minorversion) |
| 5707 | return nfserr_deleg_revoked; |
| 5708 | return nfserr_bad_stateid; |
| 5709 | } |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5710 | return nfs_ok; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5711 | } |
| 5712 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5713 | static struct nfsd_file * |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5714 | nfs4_find_file(struct nfs4_stid *s, int flags) |
| 5715 | { |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5716 | if (!s) |
| 5717 | return NULL; |
| 5718 | |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5719 | switch (s->sc_type) { |
| 5720 | case NFS4_DELEG_STID: |
| 5721 | if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file)) |
| 5722 | return NULL; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5723 | return nfsd_file_get(s->sc_file->fi_deleg_file); |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5724 | case NFS4_OPEN_STID: |
| 5725 | case NFS4_LOCK_STID: |
| 5726 | if (flags & RD_STATE) |
| 5727 | return find_readable_file(s->sc_file); |
| 5728 | else |
| 5729 | return find_writeable_file(s->sc_file); |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5730 | } |
| 5731 | |
| 5732 | return NULL; |
| 5733 | } |
| 5734 | |
| 5735 | static __be32 |
J. Bruce Fields | d8836f7 | 2018-11-05 20:04:06 -0500 | [diff] [blame] | 5736 | nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags) |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5737 | { |
| 5738 | __be32 status; |
| 5739 | |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5740 | status = nfsd4_check_openowner_confirmed(ols); |
| 5741 | if (status) |
| 5742 | return status; |
| 5743 | return nfs4_check_openmode(ols, flags); |
| 5744 | } |
| 5745 | |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5746 | static __be32 |
| 5747 | 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] | 5748 | struct nfsd_file **nfp, int flags) |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5749 | { |
| 5750 | int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5751 | struct nfsd_file *nf; |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5752 | __be32 status; |
| 5753 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5754 | nf = nfs4_find_file(s, flags); |
| 5755 | if (nf) { |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5756 | status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry, |
| 5757 | acc | NFSD_MAY_OWNER_OVERRIDE); |
Jeff Layton | 5c4583b | 2019-08-18 14:18:54 -0400 | [diff] [blame] | 5758 | if (status) { |
| 5759 | nfsd_file_put(nf); |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5760 | goto out; |
Jeff Layton | 5c4583b | 2019-08-18 14:18:54 -0400 | [diff] [blame] | 5761 | } |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5762 | } else { |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5763 | status = nfsd_file_acquire(rqstp, fhp, acc, &nf); |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5764 | if (status) |
| 5765 | return status; |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5766 | } |
Jeff Layton | 5c4583b | 2019-08-18 14:18:54 -0400 | [diff] [blame] | 5767 | *nfp = nf; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5768 | out: |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5769 | return status; |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5770 | } |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 5771 | static void |
| 5772 | _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps) |
| 5773 | { |
| 5774 | WARN_ON_ONCE(cps->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID); |
| 5775 | if (!refcount_dec_and_test(&cps->cp_stateid.sc_count)) |
| 5776 | return; |
| 5777 | list_del(&cps->cp_list); |
| 5778 | idr_remove(&nn->s2s_cp_stateids, |
| 5779 | cps->cp_stateid.stid.si_opaque.so_id); |
| 5780 | kfree(cps); |
| 5781 | } |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 5782 | /* |
| 5783 | * A READ from an inter server to server COPY will have a |
| 5784 | * copy stateid. Look up the copy notify stateid from the |
| 5785 | * idr structure and take a reference on it. |
| 5786 | */ |
Olga Kornievskaia | ce0887a | 2019-10-09 11:50:48 -0400 | [diff] [blame] | 5787 | __be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st, |
| 5788 | struct nfs4_client *clp, |
| 5789 | struct nfs4_cpntf_state **cps) |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 5790 | { |
| 5791 | copy_stateid_t *cps_t; |
| 5792 | struct nfs4_cpntf_state *state = NULL; |
| 5793 | |
| 5794 | if (st->si_opaque.so_clid.cl_id != nn->s2s_cp_cl_id) |
| 5795 | return nfserr_bad_stateid; |
| 5796 | spin_lock(&nn->s2s_cp_lock); |
| 5797 | cps_t = idr_find(&nn->s2s_cp_stateids, st->si_opaque.so_id); |
| 5798 | if (cps_t) { |
| 5799 | state = container_of(cps_t, struct nfs4_cpntf_state, |
| 5800 | cp_stateid); |
Dan Carpenter | 5277a79 | 2019-12-04 10:59:36 +0300 | [diff] [blame] | 5801 | if (state->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID) { |
| 5802 | state = NULL; |
| 5803 | goto unlock; |
| 5804 | } |
Olga Kornievskaia | ce0887a | 2019-10-09 11:50:48 -0400 | [diff] [blame] | 5805 | if (!clp) |
| 5806 | refcount_inc(&state->cp_stateid.sc_count); |
| 5807 | else |
| 5808 | _free_cpntf_state_locked(nn, state); |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 5809 | } |
Dan Carpenter | 5277a79 | 2019-12-04 10:59:36 +0300 | [diff] [blame] | 5810 | unlock: |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 5811 | spin_unlock(&nn->s2s_cp_lock); |
| 5812 | if (!state) |
| 5813 | return nfserr_bad_stateid; |
Olga Kornievskaia | ce0887a | 2019-10-09 11:50:48 -0400 | [diff] [blame] | 5814 | if (!clp && state) |
| 5815 | *cps = state; |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 5816 | return 0; |
| 5817 | } |
| 5818 | |
| 5819 | static __be32 find_cpntf_state(struct nfsd_net *nn, stateid_t *st, |
| 5820 | struct nfs4_stid **stid) |
| 5821 | { |
| 5822 | __be32 status; |
| 5823 | struct nfs4_cpntf_state *cps = NULL; |
J. Bruce Fields | 47fdb22 | 2021-01-21 17:57:42 -0500 | [diff] [blame] | 5824 | struct nfs4_client *found; |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 5825 | |
Olga Kornievskaia | ce0887a | 2019-10-09 11:50:48 -0400 | [diff] [blame] | 5826 | status = manage_cpntf_state(nn, st, NULL, &cps); |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 5827 | if (status) |
| 5828 | return status; |
| 5829 | |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 5830 | cps->cpntf_time = ktime_get_boottime_seconds(); |
J. Bruce Fields | 47fdb22 | 2021-01-21 17:57:42 -0500 | [diff] [blame] | 5831 | |
| 5832 | status = nfserr_expired; |
| 5833 | found = lookup_clientid(&cps->cp_p_clid, true, nn); |
| 5834 | if (!found) |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 5835 | goto out; |
J. Bruce Fields | 47fdb22 | 2021-01-21 17:57:42 -0500 | [diff] [blame] | 5836 | |
| 5837 | *stid = find_stateid_by_type(found, &cps->cp_p_stateid, |
| 5838 | NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID); |
| 5839 | if (*stid) |
| 5840 | status = nfs_ok; |
| 5841 | else |
| 5842 | status = nfserr_bad_stateid; |
| 5843 | |
| 5844 | put_client_renew(found); |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 5845 | out: |
| 5846 | nfs4_put_cpntf_state(nn, cps); |
| 5847 | return status; |
| 5848 | } |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 5849 | |
| 5850 | void nfs4_put_cpntf_state(struct nfsd_net *nn, struct nfs4_cpntf_state *cps) |
| 5851 | { |
| 5852 | spin_lock(&nn->s2s_cp_lock); |
| 5853 | _free_cpntf_state_locked(nn, cps); |
| 5854 | spin_unlock(&nn->s2s_cp_lock); |
| 5855 | } |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5856 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5857 | /* |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5858 | * Checks for stateid operations |
| 5859 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5860 | __be32 |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5861 | nfs4_preprocess_stateid_op(struct svc_rqst *rqstp, |
Anna Schumaker | aa0d6ae | 2015-12-03 12:59:51 +0100 | [diff] [blame] | 5862 | struct nfsd4_compound_state *cstate, struct svc_fh *fhp, |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 5863 | stateid_t *stateid, int flags, struct nfsd_file **nfp, |
| 5864 | struct nfs4_stid **cstid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5865 | { |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5866 | struct inode *ino = d_inode(fhp->fh_dentry); |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5867 | struct net *net = SVC_NET(rqstp); |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5868 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5869 | struct nfs4_stid *s = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5870 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5871 | |
Jeff Layton | 5c4583b | 2019-08-18 14:18:54 -0400 | [diff] [blame] | 5872 | if (nfp) |
| 5873 | *nfp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5874 | |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 5875 | if (grace_disallows_io(net, ino)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5876 | return nfserr_grace; |
| 5877 | |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5878 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) { |
| 5879 | status = check_special_stateids(net, fhp, stateid, flags); |
| 5880 | goto done; |
| 5881 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5882 | |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 5883 | status = nfsd4_lookup_stateid(cstate, stateid, |
Jeff Layton | db24b3b | 2014-06-30 11:48:36 -0400 | [diff] [blame] | 5884 | NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID, |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 5885 | &s, nn); |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 5886 | if (status == nfserr_bad_stateid) |
| 5887 | status = find_cpntf_state(nn, stateid, &s); |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5888 | if (status) |
Trond Myklebust | c2d1d6a | 2014-07-30 08:27:25 -0400 | [diff] [blame] | 5889 | return status; |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 5890 | status = nfsd4_stid_check_stateid_generation(stateid, s, |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5891 | nfsd4_has_session(cstate)); |
J. Bruce Fields | 69064a2 | 2011-09-09 11:54:57 -0400 | [diff] [blame] | 5892 | if (status) |
| 5893 | goto out; |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5894 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 5895 | switch (s->sc_type) { |
| 5896 | case NFS4_DELEG_STID: |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5897 | status = nfs4_check_delegmode(delegstateid(s), flags); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 5898 | break; |
| 5899 | case NFS4_OPEN_STID: |
| 5900 | case NFS4_LOCK_STID: |
J. Bruce Fields | d8836f7 | 2018-11-05 20:04:06 -0500 | [diff] [blame] | 5901 | status = nfs4_check_olstateid(openlockstateid(s), flags); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 5902 | break; |
| 5903 | default: |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 5904 | status = nfserr_bad_stateid; |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5905 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5906 | } |
Jeff Layton | 8fcd461 | 2015-07-30 06:57:46 -0400 | [diff] [blame] | 5907 | if (status) |
| 5908 | goto out; |
| 5909 | status = nfs4_check_fh(fhp, s); |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5910 | |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5911 | done: |
Jeff Layton | 5c4583b | 2019-08-18 14:18:54 -0400 | [diff] [blame] | 5912 | if (status == nfs_ok && nfp) |
| 5913 | status = nfs4_check_file(rqstp, fhp, s, nfp, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5914 | out: |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 5915 | if (s) { |
| 5916 | if (!status && cstid) |
| 5917 | *cstid = s; |
| 5918 | else |
| 5919 | nfs4_put_stid(s); |
| 5920 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5921 | return status; |
| 5922 | } |
| 5923 | |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 5924 | /* |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 5925 | * Test if the stateid is valid |
| 5926 | */ |
| 5927 | __be32 |
| 5928 | nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5929 | union nfsd4_op_u *u) |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 5930 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5931 | struct nfsd4_test_stateid *test_stateid = &u->test_stateid; |
Bryan Schumaker | 03cfb42 | 2012-01-27 10:22:49 -0500 | [diff] [blame] | 5932 | struct nfsd4_test_stateid_id *stateid; |
J. Bruce Fields | ec59659 | 2021-01-21 17:57:45 -0500 | [diff] [blame] | 5933 | struct nfs4_client *cl = cstate->clp; |
Bryan Schumaker | 03cfb42 | 2012-01-27 10:22:49 -0500 | [diff] [blame] | 5934 | |
Bryan Schumaker | 03cfb42 | 2012-01-27 10:22:49 -0500 | [diff] [blame] | 5935 | 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] | 5936 | stateid->ts_id_status = |
| 5937 | nfsd4_validate_stateid(cl, &stateid->ts_id_stateid); |
Bryan Schumaker | 03cfb42 | 2012-01-27 10:22:49 -0500 | [diff] [blame] | 5938 | |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 5939 | return nfs_ok; |
| 5940 | } |
| 5941 | |
Chuck Lever | 4269139 | 2016-08-11 10:37:30 -0400 | [diff] [blame] | 5942 | static __be32 |
| 5943 | nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s) |
| 5944 | { |
| 5945 | struct nfs4_ol_stateid *stp = openlockstateid(s); |
| 5946 | __be32 ret; |
| 5947 | |
Trond Myklebust | 659aefb | 2017-11-03 08:00:13 -0400 | [diff] [blame] | 5948 | ret = nfsd4_lock_ol_stateid(stp); |
| 5949 | if (ret) |
| 5950 | goto out_put_stid; |
Chuck Lever | 4269139 | 2016-08-11 10:37:30 -0400 | [diff] [blame] | 5951 | |
| 5952 | ret = check_stateid_generation(stateid, &s->sc_stateid, 1); |
| 5953 | if (ret) |
| 5954 | goto out; |
| 5955 | |
| 5956 | ret = nfserr_locks_held; |
| 5957 | if (check_for_locks(stp->st_stid.sc_file, |
| 5958 | lockowner(stp->st_stateowner))) |
| 5959 | goto out; |
| 5960 | |
| 5961 | release_lock_stateid(stp); |
| 5962 | ret = nfs_ok; |
| 5963 | |
| 5964 | out: |
| 5965 | mutex_unlock(&stp->st_mutex); |
Trond Myklebust | 659aefb | 2017-11-03 08:00:13 -0400 | [diff] [blame] | 5966 | out_put_stid: |
Chuck Lever | 4269139 | 2016-08-11 10:37:30 -0400 | [diff] [blame] | 5967 | nfs4_put_stid(s); |
| 5968 | return ret; |
| 5969 | } |
| 5970 | |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 5971 | __be32 |
| 5972 | nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5973 | union nfsd4_op_u *u) |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 5974 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5975 | struct nfsd4_free_stateid *free_stateid = &u->free_stateid; |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 5976 | stateid_t *stateid = &free_stateid->fr_stateid; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 5977 | struct nfs4_stid *s; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 5978 | struct nfs4_delegation *dp; |
J. Bruce Fields | ec59659 | 2021-01-21 17:57:45 -0500 | [diff] [blame] | 5979 | struct nfs4_client *cl = cstate->clp; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 5980 | __be32 ret = nfserr_bad_stateid; |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 5981 | |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5982 | spin_lock(&cl->cl_lock); |
| 5983 | s = find_stateid_locked(cl, stateid); |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 5984 | if (!s) |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5985 | goto out_unlock; |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 5986 | spin_lock(&s->sc_lock); |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 5987 | switch (s->sc_type) { |
| 5988 | case NFS4_DELEG_STID: |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 5989 | ret = nfserr_locks_held; |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5990 | break; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 5991 | case NFS4_OPEN_STID: |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5992 | ret = check_stateid_generation(stateid, &s->sc_stateid, 1); |
| 5993 | if (ret) |
| 5994 | break; |
| 5995 | ret = nfserr_locks_held; |
| 5996 | break; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 5997 | case NFS4_LOCK_STID: |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 5998 | spin_unlock(&s->sc_lock); |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 5999 | refcount_inc(&s->sc_count); |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 6000 | spin_unlock(&cl->cl_lock); |
Chuck Lever | 4269139 | 2016-08-11 10:37:30 -0400 | [diff] [blame] | 6001 | ret = nfsd4_free_lock_stateid(stateid, s); |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 6002 | goto out; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 6003 | case NFS4_REVOKED_DELEG_STID: |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 6004 | spin_unlock(&s->sc_lock); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 6005 | dp = delegstateid(s); |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 6006 | list_del_init(&dp->dl_recall_lru); |
| 6007 | spin_unlock(&cl->cl_lock); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 6008 | nfs4_put_stid(s); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 6009 | ret = nfs_ok; |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 6010 | goto out; |
| 6011 | /* Default falls through and returns nfserr_bad_stateid */ |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 6012 | } |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 6013 | spin_unlock(&s->sc_lock); |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 6014 | out_unlock: |
| 6015 | spin_unlock(&cl->cl_lock); |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 6016 | out: |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 6017 | return ret; |
| 6018 | } |
| 6019 | |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 6020 | static inline int |
| 6021 | setlkflg (int type) |
| 6022 | { |
| 6023 | return (type == NFS4_READW_LT || type == NFS4_READ_LT) ? |
| 6024 | RD_STATE : WR_STATE; |
| 6025 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6026 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 6027 | 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] | 6028 | { |
| 6029 | struct svc_fh *current_fh = &cstate->current_fh; |
| 6030 | struct nfs4_stateowner *sop = stp->st_stateowner; |
| 6031 | __be32 status; |
| 6032 | |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 6033 | status = nfsd4_check_seqid(cstate, sop, seqid); |
| 6034 | if (status) |
| 6035 | return status; |
Trond Myklebust | 9271d7e | 2017-11-03 08:00:15 -0400 | [diff] [blame] | 6036 | status = nfsd4_lock_ol_stateid(stp); |
| 6037 | if (status != nfs_ok) |
| 6038 | return status; |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 6039 | 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] | 6040 | if (status == nfs_ok) |
| 6041 | status = nfs4_check_fh(current_fh, &stp->st_stid); |
| 6042 | if (status != nfs_ok) |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 6043 | mutex_unlock(&stp->st_mutex); |
Jeff Layton | 35a92fe | 2015-09-17 07:47:08 -0400 | [diff] [blame] | 6044 | return status; |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 6045 | } |
| 6046 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6047 | /* |
| 6048 | * Checks for sequence id mutating operations. |
| 6049 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6050 | static __be32 |
Benny Halevy | dd453df | 2009-04-03 08:28:41 +0300 | [diff] [blame] | 6051 | nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, |
J. Bruce Fields | 2288d0e | 2011-09-06 15:50:21 -0400 | [diff] [blame] | 6052 | stateid_t *stateid, char typemask, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6053 | struct nfs4_ol_stateid **stpp, |
| 6054 | struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6055 | { |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 6056 | __be32 status; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 6057 | struct nfs4_stid *s; |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 6058 | struct nfs4_ol_stateid *stp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6059 | |
Chuck Lever | dd5e3fb | 2020-04-05 11:42:29 -0400 | [diff] [blame] | 6060 | trace_nfsd_preprocess(seqid, stateid); |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 6061 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6062 | *stpp = NULL; |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 6063 | status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn); |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 6064 | if (status) |
| 6065 | return status; |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 6066 | stp = openlockstateid(s); |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 6067 | nfsd4_cstate_assign_replay(cstate, stp->st_stateowner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6068 | |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 6069 | status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp); |
Trond Myklebust | fd91101 | 2014-07-29 21:34:24 -0400 | [diff] [blame] | 6070 | if (!status) |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 6071 | *stpp = stp; |
Trond Myklebust | fd91101 | 2014-07-29 21:34:24 -0400 | [diff] [blame] | 6072 | else |
| 6073 | nfs4_put_stid(&stp->st_stid); |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 6074 | return status; |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 6075 | } |
J. Bruce Fields | 39325bd | 2007-11-26 17:06:39 -0500 | [diff] [blame] | 6076 | |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6077 | static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, |
| 6078 | 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] | 6079 | { |
| 6080 | __be32 status; |
| 6081 | struct nfs4_openowner *oo; |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 6082 | struct nfs4_ol_stateid *stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6083 | |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 6084 | status = nfs4_preprocess_seqid_op(cstate, seqid, stateid, |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 6085 | NFS4_OPEN_STID, &stp, nn); |
J. Bruce Fields | 7a8711c | 2011-09-02 09:03:37 -0400 | [diff] [blame] | 6086 | if (status) |
| 6087 | return status; |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 6088 | oo = openowner(stp->st_stateowner); |
| 6089 | if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) { |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 6090 | mutex_unlock(&stp->st_mutex); |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 6091 | nfs4_put_stid(&stp->st_stid); |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 6092 | return nfserr_bad_stateid; |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 6093 | } |
| 6094 | *stpp = stp; |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 6095 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6096 | } |
| 6097 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6098 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6099 | nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6100 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6101 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6102 | struct nfsd4_open_confirm *oc = &u->open_confirm; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6103 | __be32 status; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6104 | struct nfs4_openowner *oo; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 6105 | struct nfs4_ol_stateid *stp; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6106 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6107 | |
Al Viro | a6a9f18 | 2013-09-16 10:57:01 -0400 | [diff] [blame] | 6108 | dprintk("NFSD: nfsd4_open_confirm on file %pd\n", |
| 6109 | cstate->current_fh.fh_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6110 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6111 | status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0); |
J. Bruce Fields | a8cddc5 | 2006-06-30 01:56:13 -0700 | [diff] [blame] | 6112 | if (status) |
| 6113 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6114 | |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 6115 | status = nfs4_preprocess_seqid_op(cstate, |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6116 | oc->oc_seqid, &oc->oc_req_stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6117 | NFS4_OPEN_STID, &stp, nn); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 6118 | if (status) |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 6119 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6120 | oo = openowner(stp->st_stateowner); |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 6121 | status = nfserr_bad_stateid; |
Jeff Layton | 35a92fe | 2015-09-17 07:47:08 -0400 | [diff] [blame] | 6122 | if (oo->oo_flags & NFS4_OO_CONFIRMED) { |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 6123 | mutex_unlock(&stp->st_mutex); |
Trond Myklebust | 2585fc7 | 2014-07-29 21:34:21 -0400 | [diff] [blame] | 6124 | goto put_stateid; |
Jeff Layton | 35a92fe | 2015-09-17 07:47:08 -0400 | [diff] [blame] | 6125 | } |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 6126 | oo->oo_flags |= NFS4_OO_CONFIRMED; |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 6127 | nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid); |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 6128 | mutex_unlock(&stp->st_mutex); |
Chuck Lever | dd5e3fb | 2020-04-05 11:42:29 -0400 | [diff] [blame] | 6129 | trace_nfsd_open_confirm(oc->oc_seqid, &stp->st_stid.sc_stateid); |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 6130 | nfsd4_client_record_create(oo->oo_owner.so_client); |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 6131 | status = nfs_ok; |
Trond Myklebust | 2585fc7 | 2014-07-29 21:34:21 -0400 | [diff] [blame] | 6132 | put_stateid: |
| 6133 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6134 | out: |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 6135 | nfsd4_bump_seqid(cstate, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6136 | return status; |
| 6137 | } |
| 6138 | |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 6139 | 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] | 6140 | { |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 6141 | if (!test_access(access, stp)) |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 6142 | return; |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 6143 | nfs4_file_put_access(stp->st_stid.sc_file, access); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 6144 | clear_access(access, stp); |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 6145 | } |
J. Bruce Fields | f197c27 | 2011-06-29 08:23:50 -0400 | [diff] [blame] | 6146 | |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 6147 | static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access) |
| 6148 | { |
| 6149 | switch (to_access) { |
| 6150 | case NFS4_SHARE_ACCESS_READ: |
| 6151 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE); |
| 6152 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); |
| 6153 | break; |
| 6154 | case NFS4_SHARE_ACCESS_WRITE: |
| 6155 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ); |
| 6156 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); |
| 6157 | break; |
| 6158 | case NFS4_SHARE_ACCESS_BOTH: |
| 6159 | break; |
| 6160 | default: |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 6161 | WARN_ON_ONCE(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6162 | } |
| 6163 | } |
| 6164 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6165 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6166 | nfsd4_open_downgrade(struct svc_rqst *rqstp, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6167 | struct nfsd4_compound_state *cstate, union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6168 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6169 | struct nfsd4_open_downgrade *od = &u->open_downgrade; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6170 | __be32 status; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 6171 | struct nfs4_ol_stateid *stp; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6172 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6173 | |
Al Viro | a6a9f18 | 2013-09-16 10:57:01 -0400 | [diff] [blame] | 6174 | dprintk("NFSD: nfsd4_open_downgrade on file %pd\n", |
| 6175 | cstate->current_fh.fh_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6176 | |
J. Bruce Fields | c30e92d | 2011-10-10 17:34:31 -0400 | [diff] [blame] | 6177 | /* We don't yet support WANT bits: */ |
Benny Halevy | 2c8bd7e | 2012-02-16 20:57:09 +0200 | [diff] [blame] | 6178 | if (od->od_deleg_want) |
| 6179 | dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__, |
| 6180 | od->od_deleg_want); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6181 | |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 6182 | status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6183 | &od->od_stateid, &stp, nn); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 6184 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6185 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6186 | status = nfserr_inval; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 6187 | if (!test_access(od->od_share_access, stp)) { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 6188 | 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] | 6189 | stp->st_access_bmap, od->od_share_access); |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 6190 | goto put_stateid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6191 | } |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 6192 | if (!test_deny(od->od_share_deny, stp)) { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 6193 | 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] | 6194 | stp->st_deny_bmap, od->od_share_deny); |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 6195 | goto put_stateid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6196 | } |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 6197 | nfs4_stateid_downgrade(stp, od->od_share_access); |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 6198 | reset_union_bmap_deny(od->od_share_deny, stp); |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 6199 | nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6200 | status = nfs_ok; |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 6201 | put_stateid: |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 6202 | mutex_unlock(&stp->st_mutex); |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 6203 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6204 | out: |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 6205 | nfsd4_bump_seqid(cstate, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6206 | return status; |
| 6207 | } |
| 6208 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 6209 | static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s) |
| 6210 | { |
Trond Myklebust | acf9295 | 2014-06-30 11:48:37 -0400 | [diff] [blame] | 6211 | struct nfs4_client *clp = s->st_stid.sc_client; |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 6212 | bool unhashed; |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 6213 | LIST_HEAD(reaplist); |
Trond Myklebust | acf9295 | 2014-06-30 11:48:37 -0400 | [diff] [blame] | 6214 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 6215 | spin_lock(&clp->cl_lock); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 6216 | unhashed = unhash_open_stateid(s, &reaplist); |
Trond Myklebust | acf9295 | 2014-06-30 11:48:37 -0400 | [diff] [blame] | 6217 | |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 6218 | if (clp->cl_minorversion) { |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 6219 | if (unhashed) |
| 6220 | put_ol_stateid_locked(s, &reaplist); |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 6221 | spin_unlock(&clp->cl_lock); |
| 6222 | free_ol_stateid_reaplist(&reaplist); |
| 6223 | } else { |
| 6224 | spin_unlock(&clp->cl_lock); |
| 6225 | free_ol_stateid_reaplist(&reaplist); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 6226 | if (unhashed) |
| 6227 | move_to_close_lru(s, clp->net); |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 6228 | } |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 6229 | } |
| 6230 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6231 | /* |
| 6232 | * nfs4_unlock_state() called after encode |
| 6233 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6234 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6235 | nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6236 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6237 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6238 | struct nfsd4_close *close = &u->close; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6239 | __be32 status; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 6240 | struct nfs4_ol_stateid *stp; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6241 | struct net *net = SVC_NET(rqstp); |
| 6242 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6243 | |
Al Viro | a6a9f18 | 2013-09-16 10:57:01 -0400 | [diff] [blame] | 6244 | dprintk("NFSD: nfsd4_close on file %pd\n", |
| 6245 | cstate->current_fh.fh_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6246 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 6247 | status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid, |
| 6248 | &close->cl_stateid, |
| 6249 | NFS4_OPEN_STID|NFS4_CLOSED_STID, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6250 | &stp, nn); |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 6251 | nfsd4_bump_seqid(cstate, status); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 6252 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6253 | goto out; |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 6254 | |
| 6255 | stp->st_stid.sc_type = NFS4_CLOSED_STID; |
Jeff Layton | bd2deca | 2018-02-15 15:16:18 -0500 | [diff] [blame] | 6256 | |
| 6257 | /* |
| 6258 | * Technically we don't _really_ have to increment or copy it, since |
| 6259 | * it should just be gone after this operation and we clobber the |
| 6260 | * copied value below, but we continue to do so here just to ensure |
| 6261 | * that racing ops see that there was a state change. |
| 6262 | */ |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 6263 | nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6264 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 6265 | nfsd4_close_open_stateid(stp); |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 6266 | mutex_unlock(&stp->st_mutex); |
Trond Myklebust | 8a0b589 | 2014-07-29 21:34:20 -0400 | [diff] [blame] | 6267 | |
Jeff Layton | bd2deca | 2018-02-15 15:16:18 -0500 | [diff] [blame] | 6268 | /* v4.1+ suggests that we send a special stateid in here, since the |
| 6269 | * clients should just ignore this anyway. Since this is not useful |
| 6270 | * for v4.0 clients either, we set it to the special close_stateid |
| 6271 | * universally. |
| 6272 | * |
| 6273 | * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5 |
| 6274 | */ |
| 6275 | memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid)); |
Trond Myklebust | fb500a7 | 2017-11-03 08:00:12 -0400 | [diff] [blame] | 6276 | |
Trond Myklebust | 8a0b589 | 2014-07-29 21:34:20 -0400 | [diff] [blame] | 6277 | /* put reference from nfs4_preprocess_seqid_op */ |
| 6278 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6279 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6280 | return status; |
| 6281 | } |
| 6282 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6283 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6284 | nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6285 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6286 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6287 | struct nfsd4_delegreturn *dr = &u->delegreturn; |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 6288 | struct nfs4_delegation *dp; |
| 6289 | stateid_t *stateid = &dr->dr_stateid; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 6290 | struct nfs4_stid *s; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6291 | __be32 status; |
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 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6294 | if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 6295 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6296 | |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 6297 | status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn); |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 6298 | if (status) |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 6299 | goto out; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 6300 | dp = delegstateid(s); |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 6301 | 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] | 6302 | if (status) |
Trond Myklebust | fd91101 | 2014-07-29 21:34:24 -0400 | [diff] [blame] | 6303 | goto put_stateid; |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 6304 | |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 6305 | destroy_delegation(dp); |
Trond Myklebust | fd91101 | 2014-07-29 21:34:24 -0400 | [diff] [blame] | 6306 | put_stateid: |
| 6307 | nfs4_put_stid(&dp->dl_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6308 | out: |
| 6309 | return status; |
| 6310 | } |
| 6311 | |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 6312 | /* last octet in a range */ |
| 6313 | static inline u64 |
| 6314 | last_byte_offset(u64 start, u64 len) |
| 6315 | { |
| 6316 | u64 end; |
| 6317 | |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 6318 | WARN_ON_ONCE(!len); |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 6319 | end = start + len; |
| 6320 | return end > start ? end - 1: NFS4_MAX_UINT64; |
| 6321 | } |
| 6322 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6323 | /* |
| 6324 | * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that |
| 6325 | * we can't properly handle lock requests that go beyond the (2^63 - 1)-th |
| 6326 | * byte, because of sign extension problems. Since NFSv4 calls for 64-bit |
| 6327 | * locking, this prevents us from being completely protocol-compliant. The |
| 6328 | * real solution to this problem is to start using unsigned file offsets in |
| 6329 | * the VFS, but this is a very deep change! |
| 6330 | */ |
| 6331 | static inline void |
| 6332 | nfs4_transform_lock_offset(struct file_lock *lock) |
| 6333 | { |
| 6334 | if (lock->fl_start < 0) |
| 6335 | lock->fl_start = OFFSET_MAX; |
| 6336 | if (lock->fl_end < 0) |
| 6337 | lock->fl_end = OFFSET_MAX; |
| 6338 | } |
| 6339 | |
Jeff Layton | cae80b3 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 6340 | static fl_owner_t |
| 6341 | nfsd4_fl_get_owner(fl_owner_t owner) |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6342 | { |
Jeff Layton | cae80b3 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 6343 | struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner; |
| 6344 | |
| 6345 | nfs4_get_stateowner(&lo->lo_owner); |
| 6346 | return owner; |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6347 | } |
| 6348 | |
Jeff Layton | cae80b3 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 6349 | static void |
| 6350 | nfsd4_fl_put_owner(fl_owner_t owner) |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6351 | { |
Jeff Layton | cae80b3 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 6352 | struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner; |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6353 | |
Jeff Layton | cae80b3 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 6354 | if (lo) |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6355 | nfs4_put_stateowner(&lo->lo_owner); |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6356 | } |
| 6357 | |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6358 | static void |
| 6359 | nfsd4_lm_notify(struct file_lock *fl) |
| 6360 | { |
| 6361 | struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner; |
| 6362 | struct net *net = lo->lo_owner.so_client->net; |
| 6363 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 6364 | struct nfsd4_blocked_lock *nbl = container_of(fl, |
| 6365 | struct nfsd4_blocked_lock, nbl_lock); |
| 6366 | bool queue = false; |
| 6367 | |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 6368 | /* 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] | 6369 | spin_lock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6370 | if (!list_empty(&nbl->nbl_list)) { |
| 6371 | list_del_init(&nbl->nbl_list); |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 6372 | list_del_init(&nbl->nbl_lru); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6373 | queue = true; |
| 6374 | } |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 6375 | spin_unlock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6376 | |
| 6377 | if (queue) |
| 6378 | nfsd4_run_cb(&nbl->nbl_cb); |
| 6379 | } |
| 6380 | |
Alexey Dobriyan | 7b02196 | 2009-09-21 17:01:12 -0700 | [diff] [blame] | 6381 | static const struct lock_manager_operations nfsd_posix_mng_ops = { |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6382 | .lm_notify = nfsd4_lm_notify, |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6383 | .lm_get_owner = nfsd4_fl_get_owner, |
| 6384 | .lm_put_owner = nfsd4_fl_put_owner, |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 6385 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6386 | |
| 6387 | static inline void |
| 6388 | nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny) |
| 6389 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6390 | struct nfs4_lockowner *lo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6391 | |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 6392 | if (fl->fl_lmops == &nfsd_posix_mng_ops) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6393 | lo = (struct nfs4_lockowner *) fl->fl_owner; |
J. Bruce Fields | 6f4859b | 2019-06-19 14:30:33 -0400 | [diff] [blame] | 6394 | xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner, |
| 6395 | GFP_KERNEL); |
J. Bruce Fields | 7c13f34 | 2011-08-30 22:15:47 -0400 | [diff] [blame] | 6396 | if (!deny->ld_owner.data) |
| 6397 | /* We just don't care that much */ |
| 6398 | goto nevermind; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6399 | deny->ld_clientid = lo->lo_owner.so_client->cl_clientid; |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 6400 | } else { |
J. Bruce Fields | 7c13f34 | 2011-08-30 22:15:47 -0400 | [diff] [blame] | 6401 | nevermind: |
| 6402 | deny->ld_owner.len = 0; |
| 6403 | deny->ld_owner.data = NULL; |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 6404 | deny->ld_clientid.cl_boot = 0; |
| 6405 | deny->ld_clientid.cl_id = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6406 | } |
| 6407 | deny->ld_start = fl->fl_start; |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 6408 | deny->ld_length = NFS4_MAX_UINT64; |
| 6409 | if (fl->fl_end != NFS4_MAX_UINT64) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6410 | deny->ld_length = fl->fl_end - fl->fl_start + 1; |
| 6411 | deny->ld_type = NFS4_READ_LT; |
| 6412 | if (fl->fl_type != F_RDLCK) |
| 6413 | deny->ld_type = NFS4_WRITE_LT; |
| 6414 | } |
| 6415 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6416 | static struct nfs4_lockowner * |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 6417 | find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6418 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6419 | unsigned int strhashval = ownerstr_hashval(owner); |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame] | 6420 | struct nfs4_stateowner *so; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6421 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 6422 | lockdep_assert_held(&clp->cl_lock); |
| 6423 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6424 | list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval], |
| 6425 | so_strhash) { |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame] | 6426 | if (so->so_is_open_owner) |
| 6427 | continue; |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 6428 | if (same_owner_str(so, owner)) |
| 6429 | return lockowner(nfs4_get_stateowner(so)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6430 | } |
| 6431 | return NULL; |
| 6432 | } |
| 6433 | |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6434 | static struct nfs4_lockowner * |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 6435 | find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner) |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6436 | { |
| 6437 | struct nfs4_lockowner *lo; |
| 6438 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6439 | spin_lock(&clp->cl_lock); |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 6440 | lo = find_lockowner_str_locked(clp, owner); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6441 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6442 | return lo; |
| 6443 | } |
| 6444 | |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 6445 | static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop) |
| 6446 | { |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6447 | unhash_lockowner_locked(lockowner(sop)); |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 6448 | } |
| 6449 | |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 6450 | static void nfs4_free_lockowner(struct nfs4_stateowner *sop) |
| 6451 | { |
| 6452 | struct nfs4_lockowner *lo = lockowner(sop); |
| 6453 | |
| 6454 | kmem_cache_free(lockowner_slab, lo); |
| 6455 | } |
| 6456 | |
| 6457 | static const struct nfs4_stateowner_operations lockowner_ops = { |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 6458 | .so_unhash = nfs4_unhash_lockowner, |
| 6459 | .so_free = nfs4_free_lockowner, |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 6460 | }; |
| 6461 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6462 | /* |
| 6463 | * Alloc a lock owner structure. |
| 6464 | * 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] | 6465 | * occurred. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6466 | * |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 6467 | * strhashval = ownerstr_hashval |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6468 | */ |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6469 | static struct nfs4_lockowner * |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6470 | alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, |
| 6471 | struct nfs4_ol_stateid *open_stp, |
| 6472 | struct nfsd4_lock *lock) |
| 6473 | { |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6474 | struct nfs4_lockowner *lo, *ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6475 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6476 | lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp); |
| 6477 | if (!lo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6478 | return NULL; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6479 | INIT_LIST_HEAD(&lo->lo_blocked); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6480 | INIT_LIST_HEAD(&lo->lo_owner.so_stateids); |
| 6481 | lo->lo_owner.so_is_open_owner = 0; |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6482 | lo->lo_owner.so_seqid = lock->lk_new_lock_seqid; |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 6483 | lo->lo_owner.so_ops = &lockowner_ops; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6484 | spin_lock(&clp->cl_lock); |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 6485 | ret = find_lockowner_str_locked(clp, &lock->lk_new_owner); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6486 | if (ret == NULL) { |
| 6487 | list_add(&lo->lo_owner.so_strhash, |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6488 | &clp->cl_ownerstr_hashtbl[strhashval]); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6489 | ret = lo; |
| 6490 | } else |
Kinglong Mee | d50ffde | 2015-07-16 12:05:07 +0800 | [diff] [blame] | 6491 | nfs4_free_stateowner(&lo->lo_owner); |
| 6492 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6493 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 340f0ba | 2015-03-23 11:02:30 -0400 | [diff] [blame] | 6494 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6495 | } |
| 6496 | |
Trond Myklebust | fd1fd68 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6497 | static struct nfs4_ol_stateid * |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 6498 | find_lock_stateid(const struct nfs4_lockowner *lo, |
| 6499 | const struct nfs4_ol_stateid *ost) |
Trond Myklebust | fd1fd68 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6500 | { |
| 6501 | struct nfs4_ol_stateid *lst; |
Trond Myklebust | fd1fd68 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6502 | |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 6503 | lockdep_assert_held(&ost->st_stid.sc_client->cl_lock); |
Trond Myklebust | fd1fd68 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6504 | |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 6505 | /* If ost is not hashed, ost->st_locks will not be valid */ |
| 6506 | if (!nfs4_ol_stateid_unhashed(ost)) |
| 6507 | list_for_each_entry(lst, &ost->st_locks, st_locks) { |
| 6508 | if (lst->st_stateowner == &lo->lo_owner) { |
| 6509 | refcount_inc(&lst->st_stid.sc_count); |
| 6510 | return lst; |
| 6511 | } |
Trond Myklebust | fd1fd68 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6512 | } |
Trond Myklebust | fd1fd68 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6513 | return NULL; |
| 6514 | } |
| 6515 | |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6516 | static struct nfs4_ol_stateid * |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 6517 | init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo, |
| 6518 | struct nfs4_file *fp, struct inode *inode, |
| 6519 | struct nfs4_ol_stateid *open_stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6520 | { |
J. Bruce Fields | d3b313a | 2011-09-15 15:02:41 -0400 | [diff] [blame] | 6521 | struct nfs4_client *clp = lo->lo_owner.so_client; |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6522 | struct nfs4_ol_stateid *retstp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6523 | |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6524 | mutex_init(&stp->st_mutex); |
Andrew Elble | 4f34bd0 | 2017-11-08 17:29:51 -0500 | [diff] [blame] | 6525 | mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX); |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6526 | retry: |
| 6527 | spin_lock(&clp->cl_lock); |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 6528 | if (nfs4_ol_stateid_unhashed(open_stp)) |
| 6529 | goto out_close; |
| 6530 | retstp = find_lock_stateid(lo, open_stp); |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6531 | if (retstp) |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 6532 | goto out_found; |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 6533 | refcount_inc(&stp->st_stid.sc_count); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 6534 | stp->st_stid.sc_type = NFS4_LOCK_STID; |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 6535 | stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 6536 | get_nfs4_file(fp); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 6537 | stp->st_stid.sc_file = fp; |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 6538 | stp->st_access_bmap = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6539 | stp->st_deny_bmap = open_stp->st_deny_bmap; |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 6540 | stp->st_openstp = open_stp; |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 6541 | spin_lock(&fp->fi_lock); |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 6542 | list_add(&stp->st_locks, &open_stp->st_locks); |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 6543 | list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 6544 | list_add(&stp->st_perfile, &fp->fi_stateids); |
| 6545 | spin_unlock(&fp->fi_lock); |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6546 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6547 | return stp; |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 6548 | out_found: |
| 6549 | spin_unlock(&clp->cl_lock); |
| 6550 | if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) { |
| 6551 | nfs4_put_stid(&retstp->st_stid); |
| 6552 | goto retry; |
| 6553 | } |
| 6554 | /* To keep mutex tracking happy */ |
| 6555 | mutex_unlock(&stp->st_mutex); |
| 6556 | return retstp; |
| 6557 | out_close: |
| 6558 | spin_unlock(&clp->cl_lock); |
| 6559 | mutex_unlock(&stp->st_mutex); |
| 6560 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6561 | } |
| 6562 | |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 6563 | static struct nfs4_ol_stateid * |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 6564 | find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi, |
| 6565 | struct inode *inode, struct nfs4_ol_stateid *ost, |
| 6566 | bool *new) |
| 6567 | { |
| 6568 | struct nfs4_stid *ns = NULL; |
| 6569 | struct nfs4_ol_stateid *lst; |
| 6570 | struct nfs4_openowner *oo = openowner(ost->st_stateowner); |
| 6571 | struct nfs4_client *clp = oo->oo_owner.so_client; |
| 6572 | |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6573 | *new = false; |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 6574 | spin_lock(&clp->cl_lock); |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 6575 | lst = find_lock_stateid(lo, ost); |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 6576 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6577 | if (lst != NULL) { |
| 6578 | if (nfsd4_lock_ol_stateid(lst) == nfs_ok) |
| 6579 | goto out; |
| 6580 | nfs4_put_stid(&lst->st_stid); |
| 6581 | } |
| 6582 | ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid); |
| 6583 | if (ns == NULL) |
| 6584 | return NULL; |
| 6585 | |
| 6586 | lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost); |
| 6587 | if (lst == openlockstateid(ns)) |
| 6588 | *new = true; |
| 6589 | else |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 6590 | nfs4_put_stid(ns); |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6591 | out: |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 6592 | return lst; |
| 6593 | } |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 6594 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 6595 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6596 | check_lock_length(u64 offset, u64 length) |
| 6597 | { |
Kinglong Mee | e796931 | 2015-07-13 17:34:19 +0800 | [diff] [blame] | 6598 | return ((length == 0) || ((length != NFS4_MAX_UINT64) && |
| 6599 | (length > ~offset))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6600 | } |
| 6601 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 6602 | 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] | 6603 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 6604 | struct nfs4_file *fp = lock_stp->st_stid.sc_file; |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 6605 | |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 6606 | lockdep_assert_held(&fp->fi_lock); |
| 6607 | |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 6608 | if (test_access(access, lock_stp)) |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 6609 | return; |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 6610 | __nfs4_file_get_access(fp, access); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 6611 | set_access(access, lock_stp); |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 6612 | } |
| 6613 | |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 6614 | static __be32 |
| 6615 | lookup_or_create_lock_state(struct nfsd4_compound_state *cstate, |
| 6616 | struct nfs4_ol_stateid *ost, |
| 6617 | struct nfsd4_lock *lock, |
Jeff Layton | dd25793 | 2016-08-11 10:37:39 -0400 | [diff] [blame] | 6618 | struct nfs4_ol_stateid **plst, bool *new) |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6619 | { |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6620 | __be32 status; |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 6621 | struct nfs4_file *fi = ost->st_stid.sc_file; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6622 | struct nfs4_openowner *oo = openowner(ost->st_stateowner); |
| 6623 | struct nfs4_client *cl = oo->oo_owner.so_client; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 6624 | struct inode *inode = d_inode(cstate->current_fh.fh_dentry); |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6625 | struct nfs4_lockowner *lo; |
Jeff Layton | dd25793 | 2016-08-11 10:37:39 -0400 | [diff] [blame] | 6626 | struct nfs4_ol_stateid *lst; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6627 | unsigned int strhashval; |
| 6628 | |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 6629 | lo = find_lockowner_str(cl, &lock->lk_new_owner); |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 6630 | if (!lo) { |
Kinglong Mee | 76f6c9e | 2015-07-13 17:35:10 +0800 | [diff] [blame] | 6631 | strhashval = ownerstr_hashval(&lock->lk_new_owner); |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 6632 | lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock); |
| 6633 | if (lo == NULL) |
| 6634 | return nfserr_jukebox; |
| 6635 | } else { |
| 6636 | /* with an existing lockowner, seqids must be the same */ |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6637 | status = nfserr_bad_seqid; |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 6638 | if (!cstate->minorversion && |
| 6639 | lock->lk_new_lock_seqid != lo->lo_owner.so_seqid) |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6640 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6641 | } |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 6642 | |
Jeff Layton | dd25793 | 2016-08-11 10:37:39 -0400 | [diff] [blame] | 6643 | lst = find_or_create_lock_stateid(lo, fi, inode, ost, new); |
| 6644 | if (lst == NULL) { |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6645 | status = nfserr_jukebox; |
| 6646 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6647 | } |
Jeff Layton | dd25793 | 2016-08-11 10:37:39 -0400 | [diff] [blame] | 6648 | |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6649 | status = nfs_ok; |
Jeff Layton | dd25793 | 2016-08-11 10:37:39 -0400 | [diff] [blame] | 6650 | *plst = lst; |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6651 | out: |
| 6652 | nfs4_put_stateowner(&lo->lo_owner); |
| 6653 | return status; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6654 | } |
| 6655 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6656 | /* |
| 6657 | * LOCK operation |
| 6658 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6659 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6660 | nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6661 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6662 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6663 | struct nfsd4_lock *lock = &u->lock; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6664 | struct nfs4_openowner *open_sop = NULL; |
| 6665 | struct nfs4_lockowner *lock_sop = NULL; |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 6666 | struct nfs4_ol_stateid *lock_stp = NULL; |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 6667 | struct nfs4_ol_stateid *open_stp = NULL; |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 6668 | struct nfs4_file *fp; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6669 | struct nfsd_file *nf = NULL; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6670 | struct nfsd4_blocked_lock *nbl = NULL; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6671 | struct file_lock *file_lock = NULL; |
| 6672 | struct file_lock *conflock = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6673 | __be32 status = 0; |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 6674 | int lkflg; |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 6675 | int err; |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6676 | bool new = false; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6677 | unsigned char fl_type; |
| 6678 | unsigned int fl_flags = FL_POSIX; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6679 | struct net *net = SVC_NET(rqstp); |
| 6680 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6681 | |
| 6682 | dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n", |
| 6683 | (long long) lock->lk_offset, |
| 6684 | (long long) lock->lk_length); |
| 6685 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6686 | if (check_lock_length(lock->lk_offset, lock->lk_length)) |
| 6687 | return nfserr_inval; |
| 6688 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6689 | if ((status = fh_verify(rqstp, &cstate->current_fh, |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 6690 | S_IFREG, NFSD_MAY_LOCK))) { |
Andy Adamson | a6f6ef2 | 2006-01-18 17:43:17 -0800 | [diff] [blame] | 6691 | dprintk("NFSD: nfsd4_lock: permission denied!\n"); |
| 6692 | return status; |
| 6693 | } |
| 6694 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6695 | if (lock->lk_is_new) { |
J. Bruce Fields | 684e563 | 2011-11-04 17:08:10 -0400 | [diff] [blame] | 6696 | if (nfsd4_has_session(cstate)) |
| 6697 | /* See rfc 5661 18.10.3: given clientid is ignored: */ |
Kinglong Mee | 76f6c9e | 2015-07-13 17:35:10 +0800 | [diff] [blame] | 6698 | memcpy(&lock->lk_new_clientid, |
J. Bruce Fields | ec59659 | 2021-01-21 17:57:45 -0500 | [diff] [blame] | 6699 | &cstate->clp->cl_clientid, |
J. Bruce Fields | 684e563 | 2011-11-04 17:08:10 -0400 | [diff] [blame] | 6700 | sizeof(clientid_t)); |
| 6701 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6702 | /* validate and update open stateid and open seqid */ |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 6703 | status = nfs4_preprocess_confirmed_seqid_op(cstate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6704 | lock->lk_new_open_seqid, |
| 6705 | &lock->lk_new_open_stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6706 | &open_stp, nn); |
NeilBrown | 3751517 | 2005-07-07 17:59:16 -0700 | [diff] [blame] | 6707 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6708 | goto out; |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 6709 | mutex_unlock(&open_stp->st_mutex); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6710 | open_sop = openowner(open_stp->st_stateowner); |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 6711 | status = nfserr_bad_stateid; |
J. Bruce Fields | 684e563 | 2011-11-04 17:08:10 -0400 | [diff] [blame] | 6712 | if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid, |
Kinglong Mee | 76f6c9e | 2015-07-13 17:35:10 +0800 | [diff] [blame] | 6713 | &lock->lk_new_clientid)) |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 6714 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6715 | status = lookup_or_create_lock_state(cstate, open_stp, lock, |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6716 | &lock_stp, &new); |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 6717 | } else { |
Benny Halevy | dd453df | 2009-04-03 08:28:41 +0300 | [diff] [blame] | 6718 | status = nfs4_preprocess_seqid_op(cstate, |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6719 | lock->lk_old_lock_seqid, |
| 6720 | &lock->lk_old_lock_stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6721 | NFS4_LOCK_STID, &lock_stp, nn); |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 6722 | } |
J. Bruce Fields | e1aaa89 | 2012-06-06 16:01:37 -0400 | [diff] [blame] | 6723 | if (status) |
| 6724 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6725 | lock_sop = lockowner(lock_stp->st_stateowner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6726 | |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 6727 | lkflg = setlkflg(lock->lk_type); |
| 6728 | status = nfs4_check_openmode(lock_stp, lkflg); |
| 6729 | if (status) |
| 6730 | goto out; |
| 6731 | |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 6732 | status = nfserr_grace; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6733 | if (locks_in_grace(net) && !lock->lk_reclaim) |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 6734 | goto out; |
| 6735 | status = nfserr_no_grace; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6736 | if (!locks_in_grace(net) && lock->lk_reclaim) |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 6737 | goto out; |
| 6738 | |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 6739 | fp = lock_stp->st_stid.sc_file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6740 | switch (lock->lk_type) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6741 | case NFS4_READW_LT: |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6742 | if (nfsd4_has_session(cstate)) |
| 6743 | fl_flags |= FL_SLEEP; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 6744 | fallthrough; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6745 | case NFS4_READ_LT: |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 6746 | spin_lock(&fp->fi_lock); |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6747 | nf = find_readable_file_locked(fp); |
| 6748 | if (nf) |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 6749 | get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ); |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 6750 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6751 | fl_type = F_RDLCK; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 6752 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6753 | case NFS4_WRITEW_LT: |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6754 | if (nfsd4_has_session(cstate)) |
| 6755 | fl_flags |= FL_SLEEP; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 6756 | fallthrough; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6757 | case NFS4_WRITE_LT: |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 6758 | spin_lock(&fp->fi_lock); |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6759 | nf = find_writeable_file_locked(fp); |
| 6760 | if (nf) |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 6761 | get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE); |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 6762 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6763 | fl_type = F_WRLCK; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 6764 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6765 | default: |
| 6766 | status = nfserr_inval; |
| 6767 | goto out; |
| 6768 | } |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6769 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6770 | if (!nf) { |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 6771 | status = nfserr_openmode; |
| 6772 | goto out; |
| 6773 | } |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6774 | |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6775 | nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn); |
| 6776 | if (!nbl) { |
| 6777 | dprintk("NFSD: %s: unable to allocate block!\n", __func__); |
| 6778 | status = nfserr_jukebox; |
| 6779 | goto out; |
| 6780 | } |
| 6781 | |
| 6782 | file_lock = &nbl->nbl_lock; |
| 6783 | file_lock->fl_type = fl_type; |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6784 | 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] | 6785 | file_lock->fl_pid = current->tgid; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6786 | file_lock->fl_file = nf->nf_file; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6787 | file_lock->fl_flags = fl_flags; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6788 | file_lock->fl_lmops = &nfsd_posix_mng_ops; |
| 6789 | file_lock->fl_start = lock->lk_offset; |
| 6790 | file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length); |
| 6791 | nfs4_transform_lock_offset(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6792 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6793 | conflock = locks_alloc_lock(); |
| 6794 | if (!conflock) { |
| 6795 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 6796 | status = nfserr_jukebox; |
| 6797 | goto out; |
| 6798 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6799 | |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6800 | if (fl_flags & FL_SLEEP) { |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 6801 | nbl->nbl_time = ktime_get_boottime_seconds(); |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 6802 | spin_lock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6803 | list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked); |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 6804 | list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru); |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 6805 | spin_unlock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6806 | } |
| 6807 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6808 | err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6809 | switch (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6810 | case 0: /* success! */ |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 6811 | 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] | 6812 | status = 0; |
J. Bruce Fields | 03f318c | 2018-06-08 12:28:47 -0400 | [diff] [blame] | 6813 | if (lock->lk_reclaim) |
| 6814 | nn->somebody_reclaimed = true; |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 6815 | break; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6816 | case FILE_LOCK_DEFERRED: |
| 6817 | nbl = NULL; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 6818 | fallthrough; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6819 | case -EAGAIN: /* conflock holds conflicting lock */ |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 6820 | status = nfserr_denied; |
| 6821 | dprintk("NFSD: nfsd4_lock: conflicting lock found!\n"); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6822 | nfs4_set_lock_denied(conflock, &lock->lk_denied); |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 6823 | break; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6824 | case -EDEADLK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6825 | status = nfserr_deadlock; |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 6826 | break; |
J. Bruce Fields | 3e77246 | 2011-08-10 19:07:33 -0400 | [diff] [blame] | 6827 | default: |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 6828 | 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] | 6829 | status = nfserrno(err); |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 6830 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6831 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6832 | out: |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6833 | if (nbl) { |
| 6834 | /* dequeue it if we queued it before */ |
| 6835 | if (fl_flags & FL_SLEEP) { |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 6836 | spin_lock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6837 | list_del_init(&nbl->nbl_list); |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 6838 | list_del_init(&nbl->nbl_lru); |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 6839 | spin_unlock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6840 | } |
| 6841 | free_blocked_lock(nbl); |
| 6842 | } |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6843 | if (nf) |
| 6844 | nfsd_file_put(nf); |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6845 | if (lock_stp) { |
| 6846 | /* Bump seqid manually if the 4.0 replay owner is openowner */ |
| 6847 | if (cstate->replay_owner && |
| 6848 | cstate->replay_owner != &lock_sop->lo_owner && |
| 6849 | seqid_mutating_err(ntohl(status))) |
| 6850 | lock_sop->lo_owner.so_seqid++; |
| 6851 | |
| 6852 | /* |
| 6853 | * If this is a new, never-before-used stateid, and we are |
| 6854 | * returning an error, then just go ahead and release it. |
| 6855 | */ |
J. Bruce Fields | 2502072 | 2018-01-17 16:25:59 -0500 | [diff] [blame] | 6856 | if (status && new) |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6857 | release_lock_stateid(lock_stp); |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6858 | |
| 6859 | mutex_unlock(&lock_stp->st_mutex); |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6860 | |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 6861 | nfs4_put_stid(&lock_stp->st_stid); |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6862 | } |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 6863 | if (open_stp) |
| 6864 | nfs4_put_stid(&open_stp->st_stid); |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 6865 | nfsd4_bump_seqid(cstate, status); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6866 | if (conflock) |
| 6867 | locks_free_lock(conflock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6868 | return status; |
| 6869 | } |
| 6870 | |
| 6871 | /* |
J. Bruce Fields | 55ef127 | 2008-12-20 11:58:38 -0800 | [diff] [blame] | 6872 | * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN, |
| 6873 | * so we do a temporary open here just to get an open file to pass to |
| 6874 | * vfs_test_lock. (Arguably perhaps test_lock should be done with an |
| 6875 | * inode operation.) |
| 6876 | */ |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 6877 | 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] | 6878 | { |
Jeff Layton | 6b556ca | 2019-08-18 14:18:55 -0400 | [diff] [blame] | 6879 | struct nfsd_file *nf; |
J. Bruce Fields | 217fd6f | 2021-04-16 14:00:14 -0400 | [diff] [blame] | 6880 | __be32 err; |
| 6881 | |
| 6882 | err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf); |
| 6883 | if (err) |
| 6884 | return err; |
| 6885 | fh_lock(fhp); /* to block new leases till after test_lock: */ |
| 6886 | err = nfserrno(nfsd_open_break_lease(fhp->fh_dentry->d_inode, |
| 6887 | NFSD_MAY_READ)); |
| 6888 | if (err) |
| 6889 | goto out; |
| 6890 | err = nfserrno(vfs_test_lock(nf->nf_file, lock)); |
| 6891 | out: |
| 6892 | fh_unlock(fhp); |
| 6893 | nfsd_file_put(nf); |
J. Bruce Fields | 55ef127 | 2008-12-20 11:58:38 -0800 | [diff] [blame] | 6894 | return err; |
| 6895 | } |
| 6896 | |
| 6897 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6898 | * LOCKT operation |
| 6899 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6900 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6901 | nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6902 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6903 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6904 | struct nfsd4_lockt *lockt = &u->lockt; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6905 | struct file_lock *file_lock = NULL; |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6906 | struct nfs4_lockowner *lo = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6907 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 6908 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6909 | |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 6910 | if (locks_in_grace(SVC_NET(rqstp))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6911 | return nfserr_grace; |
| 6912 | |
| 6913 | if (check_lock_length(lockt->lt_offset, lockt->lt_length)) |
| 6914 | return nfserr_inval; |
| 6915 | |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 6916 | if (!nfsd4_has_session(cstate)) { |
J. Bruce Fields | f71475b | 2021-01-21 17:57:43 -0500 | [diff] [blame] | 6917 | status = set_client(&lockt->lt_clientid, cstate, nn); |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 6918 | if (status) |
| 6919 | goto out; |
| 6920 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6921 | |
J. Bruce Fields | 75c096f | 2011-08-15 18:39:32 -0400 | [diff] [blame] | 6922 | if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6923 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6924 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6925 | file_lock = locks_alloc_lock(); |
| 6926 | if (!file_lock) { |
| 6927 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 6928 | status = nfserr_jukebox; |
| 6929 | goto out; |
| 6930 | } |
Kinglong Mee | 6cd9066 | 2014-08-15 08:02:55 +0800 | [diff] [blame] | 6931 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6932 | switch (lockt->lt_type) { |
| 6933 | case NFS4_READ_LT: |
| 6934 | case NFS4_READW_LT: |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6935 | file_lock->fl_type = F_RDLCK; |
Colin Ian King | f50c9d7 | 2018-11-03 16:12:53 +0000 | [diff] [blame] | 6936 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6937 | case NFS4_WRITE_LT: |
| 6938 | case NFS4_WRITEW_LT: |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6939 | file_lock->fl_type = F_WRLCK; |
Colin Ian King | f50c9d7 | 2018-11-03 16:12:53 +0000 | [diff] [blame] | 6940 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6941 | default: |
J. Bruce Fields | 2fdada0 | 2007-07-27 16:10:37 -0400 | [diff] [blame] | 6942 | dprintk("NFSD: nfs4_lockt: bad lock type!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6943 | status = nfserr_inval; |
Colin Ian King | f50c9d7 | 2018-11-03 16:12:53 +0000 | [diff] [blame] | 6944 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6945 | } |
| 6946 | |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 6947 | lo = find_lockowner_str(cstate->clp, &lockt->lt_owner); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6948 | if (lo) |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6949 | file_lock->fl_owner = (fl_owner_t)lo; |
| 6950 | file_lock->fl_pid = current->tgid; |
| 6951 | file_lock->fl_flags = FL_POSIX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6952 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6953 | file_lock->fl_start = lockt->lt_offset; |
| 6954 | 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] | 6955 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6956 | nfs4_transform_lock_offset(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6957 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6958 | status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock); |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 6959 | if (status) |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 6960 | goto out; |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 6961 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6962 | if (file_lock->fl_type != F_UNLCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6963 | status = nfserr_denied; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6964 | nfs4_set_lock_denied(file_lock, &lockt->lt_denied); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6965 | } |
| 6966 | out: |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6967 | if (lo) |
| 6968 | nfs4_put_stateowner(&lo->lo_owner); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6969 | if (file_lock) |
| 6970 | locks_free_lock(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6971 | return status; |
| 6972 | } |
| 6973 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6974 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6975 | nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6976 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6977 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6978 | struct nfsd4_locku *locku = &u->locku; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 6979 | struct nfs4_ol_stateid *stp; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6980 | struct nfsd_file *nf = NULL; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6981 | struct file_lock *file_lock = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6982 | __be32 status; |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 6983 | int err; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6984 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
| 6985 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6986 | dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n", |
| 6987 | (long long) locku->lu_offset, |
| 6988 | (long long) locku->lu_length); |
| 6989 | |
| 6990 | if (check_lock_length(locku->lu_offset, locku->lu_length)) |
| 6991 | return nfserr_inval; |
| 6992 | |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 6993 | status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6994 | &locku->lu_stateid, NFS4_LOCK_STID, |
| 6995 | &stp, nn); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 6996 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6997 | goto out; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6998 | nf = find_any_file(stp->st_stid.sc_file); |
| 6999 | if (!nf) { |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 7000 | status = nfserr_lock_range; |
Trond Myklebust | 858cc57 | 2014-07-29 21:34:16 -0400 | [diff] [blame] | 7001 | goto put_stateid; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 7002 | } |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 7003 | file_lock = locks_alloc_lock(); |
| 7004 | if (!file_lock) { |
| 7005 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 7006 | status = nfserr_jukebox; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 7007 | goto put_file; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 7008 | } |
Kinglong Mee | 6cd9066 | 2014-08-15 08:02:55 +0800 | [diff] [blame] | 7009 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 7010 | file_lock->fl_type = F_UNLCK; |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 7011 | 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] | 7012 | file_lock->fl_pid = current->tgid; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 7013 | file_lock->fl_file = nf->nf_file; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 7014 | file_lock->fl_flags = FL_POSIX; |
| 7015 | file_lock->fl_lmops = &nfsd_posix_mng_ops; |
| 7016 | file_lock->fl_start = locku->lu_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7017 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 7018 | file_lock->fl_end = last_byte_offset(locku->lu_offset, |
| 7019 | locku->lu_length); |
| 7020 | nfs4_transform_lock_offset(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7021 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 7022 | err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL); |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 7023 | if (err) { |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 7024 | dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7025 | goto out_nfserr; |
| 7026 | } |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 7027 | nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid); |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 7028 | put_file: |
| 7029 | nfsd_file_put(nf); |
Trond Myklebust | 858cc57 | 2014-07-29 21:34:16 -0400 | [diff] [blame] | 7030 | put_stateid: |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 7031 | mutex_unlock(&stp->st_mutex); |
Trond Myklebust | 858cc57 | 2014-07-29 21:34:16 -0400 | [diff] [blame] | 7032 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7033 | out: |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 7034 | nfsd4_bump_seqid(cstate, status); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 7035 | if (file_lock) |
| 7036 | locks_free_lock(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7037 | return status; |
| 7038 | |
| 7039 | out_nfserr: |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 7040 | status = nfserrno(err); |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 7041 | goto put_file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7042 | } |
| 7043 | |
| 7044 | /* |
| 7045 | * returns |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 7046 | * true: locks held by lockowner |
| 7047 | * false: no locks held by lockowner |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7048 | */ |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 7049 | static bool |
| 7050 | check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7051 | { |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 7052 | struct file_lock *fl; |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 7053 | int status = false; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 7054 | struct nfsd_file *nf = find_any_file(fp); |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 7055 | struct inode *inode; |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 7056 | struct file_lock_context *flctx; |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 7057 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 7058 | if (!nf) { |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 7059 | /* Any valid lock stateid should have some sort of access */ |
| 7060 | WARN_ON_ONCE(1); |
| 7061 | return status; |
| 7062 | } |
| 7063 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 7064 | inode = locks_inode(nf->nf_file); |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 7065 | flctx = inode->i_flctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7066 | |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 7067 | if (flctx && !list_empty_careful(&flctx->flc_posix)) { |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 7068 | spin_lock(&flctx->flc_lock); |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 7069 | list_for_each_entry(fl, &flctx->flc_posix, fl_list) { |
| 7070 | if (fl->fl_owner == (fl_owner_t)lowner) { |
| 7071 | status = true; |
| 7072 | break; |
| 7073 | } |
J. Bruce Fields | 796dadf | 2006-01-18 17:43:22 -0800 | [diff] [blame] | 7074 | } |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 7075 | spin_unlock(&flctx->flc_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7076 | } |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 7077 | nfsd_file_put(nf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7078 | return status; |
| 7079 | } |
| 7080 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 7081 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 7082 | nfsd4_release_lockowner(struct svc_rqst *rqstp, |
| 7083 | struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 7084 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7085 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 7086 | struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7087 | clientid_t *clid = &rlockowner->rl_clientid; |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 7088 | struct nfs4_stateowner *sop; |
| 7089 | struct nfs4_lockowner *lo = NULL; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 7090 | struct nfs4_ol_stateid *stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7091 | struct xdr_netobj *owner = &rlockowner->rl_owner; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 7092 | unsigned int hashval = ownerstr_hashval(owner); |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 7093 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 7094 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 7095 | struct nfs4_client *clp; |
Chuck Lever | 8858481 | 2016-07-13 16:40:14 -0400 | [diff] [blame] | 7096 | LIST_HEAD (reaplist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7097 | |
| 7098 | dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n", |
| 7099 | clid->cl_boot, clid->cl_id); |
| 7100 | |
J. Bruce Fields | f71475b | 2021-01-21 17:57:43 -0500 | [diff] [blame] | 7101 | status = set_client(clid, cstate, nn); |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 7102 | if (status) |
Trond Myklebust | 51f5e78 | 2014-07-30 08:27:27 -0400 | [diff] [blame] | 7103 | return status; |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 7104 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 7105 | clp = cstate->clp; |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 7106 | /* Find the matching lock stateowner */ |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 7107 | spin_lock(&clp->cl_lock); |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 7108 | list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval], |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 7109 | so_strhash) { |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 7110 | |
| 7111 | if (sop->so_is_open_owner || !same_owner_str(sop, owner)) |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 7112 | continue; |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 7113 | |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 7114 | /* see if there are still any locks associated with it */ |
| 7115 | lo = lockowner(sop); |
| 7116 | list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) { |
| 7117 | if (check_for_locks(stp->st_stid.sc_file, lo)) { |
| 7118 | status = nfserr_locks_held; |
| 7119 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | 51f5e78 | 2014-07-30 08:27:27 -0400 | [diff] [blame] | 7120 | return status; |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 7121 | } |
Jeff Layton | 5adfd88 | 2014-07-29 21:34:31 -0400 | [diff] [blame] | 7122 | } |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 7123 | |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 7124 | nfs4_get_stateowner(sop); |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 7125 | break; |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 7126 | } |
Chuck Lever | 8858481 | 2016-07-13 16:40:14 -0400 | [diff] [blame] | 7127 | if (!lo) { |
| 7128 | spin_unlock(&clp->cl_lock); |
| 7129 | return status; |
| 7130 | } |
| 7131 | |
| 7132 | unhash_lockowner_locked(lo); |
| 7133 | while (!list_empty(&lo->lo_owner.so_stateids)) { |
| 7134 | stp = list_first_entry(&lo->lo_owner.so_stateids, |
| 7135 | struct nfs4_ol_stateid, |
| 7136 | st_perstateowner); |
| 7137 | WARN_ON(!unhash_lock_stateid(stp)); |
| 7138 | put_ol_stateid_locked(stp, &reaplist); |
| 7139 | } |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 7140 | spin_unlock(&clp->cl_lock); |
Chuck Lever | 8858481 | 2016-07-13 16:40:14 -0400 | [diff] [blame] | 7141 | free_ol_stateid_reaplist(&reaplist); |
Jeff Layton | 68ef3bc | 2018-03-16 11:32:02 -0400 | [diff] [blame] | 7142 | remove_blocked_locks(lo); |
Chuck Lever | 8858481 | 2016-07-13 16:40:14 -0400 | [diff] [blame] | 7143 | nfs4_put_stateowner(&lo->lo_owner); |
| 7144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7145 | return status; |
| 7146 | } |
| 7147 | |
| 7148 | static inline struct nfs4_client_reclaim * |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 7149 | alloc_reclaim(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7150 | { |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 7151 | return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7152 | } |
| 7153 | |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 7154 | bool |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 7155 | nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn) |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 7156 | { |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 7157 | struct nfs4_client_reclaim *crp; |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 7158 | |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7159 | crp = nfsd4_find_reclaim_client(name, nn); |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 7160 | return (crp && crp->cr_clp); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 7161 | } |
| 7162 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7163 | /* |
| 7164 | * failure => all reset bets are off, nfserr_no_grace... |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 7165 | * |
| 7166 | * The caller is responsible for freeing name.data if NULL is returned (it |
| 7167 | * will be freed in nfs4_remove_reclaim_record in the normal case). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7168 | */ |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 7169 | struct nfs4_client_reclaim * |
Scott Mayhew | 6ee95d1 | 2019-09-09 16:10:31 -0400 | [diff] [blame] | 7170 | nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash, |
| 7171 | struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7172 | { |
| 7173 | unsigned int strhashval; |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 7174 | struct nfs4_client_reclaim *crp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7175 | |
Chuck Lever | dd5e3fb | 2020-04-05 11:42:29 -0400 | [diff] [blame] | 7176 | trace_nfsd_clid_reclaim(nn, name.len, name.data); |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 7177 | crp = alloc_reclaim(); |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 7178 | if (crp) { |
| 7179 | strhashval = clientstr_hashval(name); |
| 7180 | INIT_LIST_HEAD(&crp->cr_strhash); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7181 | list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]); |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 7182 | crp->cr_name.data = name.data; |
| 7183 | crp->cr_name.len = name.len; |
Scott Mayhew | 6ee95d1 | 2019-09-09 16:10:31 -0400 | [diff] [blame] | 7184 | crp->cr_princhash.data = princhash.data; |
| 7185 | crp->cr_princhash.len = princhash.len; |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 7186 | crp->cr_clp = NULL; |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7187 | nn->reclaim_str_hashtbl_size++; |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 7188 | } |
| 7189 | return crp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7190 | } |
| 7191 | |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 7192 | void |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7193 | 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] | 7194 | { |
| 7195 | list_del(&crp->cr_strhash); |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 7196 | kfree(crp->cr_name.data); |
Scott Mayhew | 6ee95d1 | 2019-09-09 16:10:31 -0400 | [diff] [blame] | 7197 | kfree(crp->cr_princhash.data); |
Jeff Layton | ce30e53 | 2012-11-12 15:00:53 -0500 | [diff] [blame] | 7198 | kfree(crp); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7199 | nn->reclaim_str_hashtbl_size--; |
Jeff Layton | ce30e53 | 2012-11-12 15:00:53 -0500 | [diff] [blame] | 7200 | } |
| 7201 | |
| 7202 | void |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7203 | nfs4_release_reclaim(struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7204 | { |
| 7205 | struct nfs4_client_reclaim *crp = NULL; |
| 7206 | int i; |
| 7207 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7208 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7209 | while (!list_empty(&nn->reclaim_str_hashtbl[i])) { |
| 7210 | crp = list_entry(nn->reclaim_str_hashtbl[i].next, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7211 | struct nfs4_client_reclaim, cr_strhash); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7212 | nfs4_remove_reclaim_record(crp, nn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7213 | } |
| 7214 | } |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 7215 | WARN_ON_ONCE(nn->reclaim_str_hashtbl_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7216 | } |
| 7217 | |
| 7218 | /* |
| 7219 | * called from OPEN, CLAIM_PREVIOUS with a new clientid. */ |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 7220 | struct nfs4_client_reclaim * |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 7221 | nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7222 | { |
| 7223 | unsigned int strhashval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7224 | struct nfs4_client_reclaim *crp = NULL; |
| 7225 | |
Chuck Lever | dd5e3fb | 2020-04-05 11:42:29 -0400 | [diff] [blame] | 7226 | trace_nfsd_clid_find(nn, name.len, name.data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7227 | |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 7228 | strhashval = clientstr_hashval(name); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7229 | list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) { |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 7230 | if (compare_blob(&crp->cr_name, &name) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7231 | return crp; |
| 7232 | } |
| 7233 | } |
| 7234 | return NULL; |
| 7235 | } |
| 7236 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 7237 | __be32 |
J. Bruce Fields | 1722b04 | 2021-01-21 17:57:44 -0500 | [diff] [blame] | 7238 | nfs4_check_open_reclaim(struct nfs4_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7239 | { |
J. Bruce Fields | 1722b04 | 2021-01-21 17:57:44 -0500 | [diff] [blame] | 7240 | if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &clp->cl_flags)) |
Jeff Layton | 3b3e7b7 | 2014-09-12 16:40:22 -0400 | [diff] [blame] | 7241 | return nfserr_no_grace; |
| 7242 | |
J. Bruce Fields | 1722b04 | 2021-01-21 17:57:44 -0500 | [diff] [blame] | 7243 | if (nfsd4_client_record_check(clp)) |
Trond Myklebust | 0fe492d | 2014-06-30 11:48:47 -0400 | [diff] [blame] | 7244 | return nfserr_reclaim_bad; |
| 7245 | |
| 7246 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7247 | } |
| 7248 | |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 7249 | /* |
| 7250 | * Since the lifetime of a delegation isn't limited to that of an open, a |
| 7251 | * client may quite reasonably hang on to a delegation as long as it has |
| 7252 | * the inode cached. This becomes an obvious problem the first time a |
| 7253 | * client's inode cache approaches the size of the server's total memory. |
| 7254 | * |
| 7255 | * For now we avoid this problem by imposing a hard limit on the number |
| 7256 | * of delegations, which varies according to the server's memory size. |
| 7257 | */ |
| 7258 | static void |
| 7259 | set_max_delegations(void) |
| 7260 | { |
| 7261 | /* |
| 7262 | * Allow at most 4 delegations per megabyte of RAM. Quick |
| 7263 | * estimates suggest that in the worst case (where every delegation |
| 7264 | * is for a different inode), a delegation could take about 1.5K, |
| 7265 | * giving a worst case usage of about 6% of memory. |
| 7266 | */ |
| 7267 | max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT); |
| 7268 | } |
| 7269 | |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 7270 | static int nfs4_state_create_net(struct net *net) |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7271 | { |
| 7272 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 7273 | int i; |
| 7274 | |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 7275 | nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE, |
| 7276 | sizeof(struct list_head), |
| 7277 | GFP_KERNEL); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7278 | if (!nn->conf_id_hashtbl) |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 7279 | goto err; |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 7280 | nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE, |
| 7281 | sizeof(struct list_head), |
| 7282 | GFP_KERNEL); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 7283 | if (!nn->unconf_id_hashtbl) |
| 7284 | goto err_unconf_id; |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 7285 | nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE, |
| 7286 | sizeof(struct list_head), |
| 7287 | GFP_KERNEL); |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 7288 | if (!nn->sessionid_hashtbl) |
| 7289 | goto err_sessionid; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7290 | |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 7291 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7292 | INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 7293 | INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 7294 | } |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 7295 | for (i = 0; i < SESSION_HASH_SIZE; i++) |
| 7296 | INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 7297 | nn->conf_name_tree = RB_ROOT; |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 7298 | nn->unconf_name_tree = RB_ROOT; |
Arnd Bergmann | 9cc7680 | 2019-10-31 15:53:13 +0100 | [diff] [blame] | 7299 | nn->boot_time = ktime_get_real_seconds(); |
Vasily Averin | 81833de | 2017-11-13 07:25:40 +0300 | [diff] [blame] | 7300 | nn->grace_ended = false; |
| 7301 | nn->nfsd4_manager.block_opens = true; |
| 7302 | INIT_LIST_HEAD(&nn->nfsd4_manager.list); |
Stanislav Kinsbursky | 5ed58bb | 2012-11-14 18:21:56 +0300 | [diff] [blame] | 7303 | INIT_LIST_HEAD(&nn->client_lru); |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 7304 | INIT_LIST_HEAD(&nn->close_lru); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 7305 | INIT_LIST_HEAD(&nn->del_recall_lru); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 7306 | spin_lock_init(&nn->client_lock); |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 7307 | spin_lock_init(&nn->s2s_cp_lock); |
| 7308 | idr_init(&nn->s2s_cp_stateids); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7309 | |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 7310 | spin_lock_init(&nn->blocked_locks_lock); |
| 7311 | INIT_LIST_HEAD(&nn->blocked_locks_lru); |
| 7312 | |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 7313 | INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 7314 | get_net(net); |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 7315 | |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7316 | return 0; |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 7317 | |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 7318 | err_sessionid: |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 7319 | kfree(nn->unconf_id_hashtbl); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 7320 | err_unconf_id: |
| 7321 | kfree(nn->conf_id_hashtbl); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 7322 | err: |
| 7323 | return -ENOMEM; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7324 | } |
| 7325 | |
| 7326 | static void |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 7327 | nfs4_state_destroy_net(struct net *net) |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7328 | { |
| 7329 | int i; |
| 7330 | struct nfs4_client *clp = NULL; |
| 7331 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 7332 | |
| 7333 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
| 7334 | while (!list_empty(&nn->conf_id_hashtbl[i])) { |
| 7335 | clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); |
| 7336 | destroy_client(clp); |
| 7337 | } |
| 7338 | } |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 7339 | |
Jeff Layton | 68ef3bc | 2018-03-16 11:32:02 -0400 | [diff] [blame] | 7340 | WARN_ON(!list_empty(&nn->blocked_locks_lru)); |
| 7341 | |
Kinglong Mee | 2b90563 | 2014-03-26 22:09:30 +0800 | [diff] [blame] | 7342 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
| 7343 | while (!list_empty(&nn->unconf_id_hashtbl[i])) { |
| 7344 | clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); |
| 7345 | destroy_client(clp); |
| 7346 | } |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 7347 | } |
| 7348 | |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 7349 | kfree(nn->sessionid_hashtbl); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 7350 | kfree(nn->unconf_id_hashtbl); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7351 | kfree(nn->conf_id_hashtbl); |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 7352 | put_net(net); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7353 | } |
| 7354 | |
Stanislav Kinsbursky | f252bc6 | 2012-11-26 15:22:18 +0300 | [diff] [blame] | 7355 | int |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 7356 | nfs4_state_start_net(struct net *net) |
NeilBrown | ac4d8ff2 | 2005-06-23 22:03:30 -0700 | [diff] [blame] | 7357 | { |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 7358 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 7359 | int ret; |
| 7360 | |
Trond Myklebust | c6c7f2a | 2021-03-13 16:08:47 -0500 | [diff] [blame] | 7361 | ret = nfs4_state_create_net(net); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7362 | if (ret) |
| 7363 | return ret; |
Jeff Layton | d4318ac | 2014-09-12 16:40:21 -0400 | [diff] [blame] | 7364 | locks_start_grace(net, &nn->nfsd4_manager); |
| 7365 | nfsd4_client_tracking_init(net); |
Scott Mayhew | 362063a | 2019-03-26 18:06:28 -0400 | [diff] [blame] | 7366 | if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0) |
| 7367 | goto skip_grace; |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 7368 | printk(KERN_INFO "NFSD: starting %lld-second grace period (net %x)\n", |
Vasily Averin | 7e981a8 | 2017-11-06 16:46:04 +0300 | [diff] [blame] | 7369 | nn->nfsd4_grace, net->ns.inum); |
Chuck Lever | dd5e3fb | 2020-04-05 11:42:29 -0400 | [diff] [blame] | 7370 | trace_nfsd_grace_start(nn); |
Stanislav Kinsbursky | 5284b44 | 2012-11-27 14:11:49 +0300 | [diff] [blame] | 7371 | queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 7372 | return 0; |
Scott Mayhew | 362063a | 2019-03-26 18:06:28 -0400 | [diff] [blame] | 7373 | |
| 7374 | skip_grace: |
| 7375 | printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n", |
| 7376 | net->ns.inum); |
| 7377 | queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ); |
| 7378 | nfsd4_end_grace(nn); |
| 7379 | return 0; |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 7380 | } |
| 7381 | |
| 7382 | /* initialization to perform when the nfsd service is started: */ |
| 7383 | |
| 7384 | int |
| 7385 | nfs4_state_start(void) |
| 7386 | { |
| 7387 | int ret; |
| 7388 | |
Jeff Layton | 51a5456 | 2015-08-20 07:17:01 -0400 | [diff] [blame] | 7389 | laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4"); |
Jeff Layton | a6d6b78 | 2012-03-05 11:42:36 -0500 | [diff] [blame] | 7390 | if (laundry_wq == NULL) { |
| 7391 | ret = -ENOMEM; |
Chuck Lever | a26dd64 | 2018-08-16 12:06:09 -0400 | [diff] [blame] | 7392 | goto out; |
Jeff Layton | a6d6b78 | 2012-03-05 11:42:36 -0500 | [diff] [blame] | 7393 | } |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 7394 | ret = nfsd4_create_callback_queue(); |
| 7395 | if (ret) |
| 7396 | goto out_free_laundry; |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 7397 | |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 7398 | set_max_delegations(); |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 7399 | return 0; |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 7400 | |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 7401 | out_free_laundry: |
| 7402 | destroy_workqueue(laundry_wq); |
Chuck Lever | a26dd64 | 2018-08-16 12:06:09 -0400 | [diff] [blame] | 7403 | out: |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 7404 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7405 | } |
| 7406 | |
Stanislav Kinsbursky | f252bc6 | 2012-11-26 15:22:18 +0300 | [diff] [blame] | 7407 | void |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 7408 | nfs4_state_shutdown_net(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7409 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7410 | struct nfs4_delegation *dp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7411 | struct list_head *pos, *next, reaplist; |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 7412 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7413 | |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 7414 | cancel_delayed_work_sync(&nn->laundromat_work); |
| 7415 | locks_end_grace(&nn->nfsd4_manager); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 7416 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7417 | INIT_LIST_HEAD(&reaplist); |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 7418 | spin_lock(&state_lock); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 7419 | list_for_each_safe(pos, next, &nn->del_recall_lru) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7420 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 7421 | WARN_ON(!unhash_delegation_locked(dp)); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 7422 | list_add(&dp->dl_recall_lru, &reaplist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7423 | } |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 7424 | spin_unlock(&state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7425 | list_for_each_safe(pos, next, &reaplist) { |
| 7426 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 7427 | list_del_init(&dp->dl_recall_lru); |
J. Bruce Fields | 0af6e69 | 2018-02-21 15:11:03 -0500 | [diff] [blame] | 7428 | destroy_unhashed_deleg(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7429 | } |
| 7430 | |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 7431 | nfsd4_client_tracking_exit(net); |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 7432 | nfs4_state_destroy_net(net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7433 | } |
| 7434 | |
| 7435 | void |
| 7436 | nfs4_state_shutdown(void) |
| 7437 | { |
NeilBrown | 5e8d5c2 | 2006-04-10 22:55:37 -0700 | [diff] [blame] | 7438 | destroy_workqueue(laundry_wq); |
J. Bruce Fields | c3935e3 | 2010-06-04 16:42:08 -0400 | [diff] [blame] | 7439 | nfsd4_destroy_callback_queue(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7440 | } |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7441 | |
| 7442 | static void |
| 7443 | get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) |
| 7444 | { |
Olga Kornievskaia | 51100d2 | 2018-09-13 13:58:24 -0400 | [diff] [blame] | 7445 | if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG) && |
| 7446 | CURRENT_STATEID(stateid)) |
Tigran Mkrtchyan | 37c593c | 2012-02-13 22:55:32 +0100 | [diff] [blame] | 7447 | memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t)); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7448 | } |
| 7449 | |
| 7450 | static void |
| 7451 | put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) |
| 7452 | { |
Tigran Mkrtchyan | 37c593c | 2012-02-13 22:55:32 +0100 | [diff] [blame] | 7453 | if (cstate->minorversion) { |
| 7454 | memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t)); |
Olga Kornievskaia | 51100d2 | 2018-09-13 13:58:24 -0400 | [diff] [blame] | 7455 | SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG); |
Tigran Mkrtchyan | 37c593c | 2012-02-13 22:55:32 +0100 | [diff] [blame] | 7456 | } |
| 7457 | } |
| 7458 | |
| 7459 | void |
| 7460 | clear_current_stateid(struct nfsd4_compound_state *cstate) |
| 7461 | { |
Olga Kornievskaia | 51100d2 | 2018-09-13 13:58:24 -0400 | [diff] [blame] | 7462 | CLEAR_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7463 | } |
| 7464 | |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 7465 | /* |
| 7466 | * functions to set current state id |
| 7467 | */ |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7468 | void |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7469 | nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, |
| 7470 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 7471 | { |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7472 | put_stateid(cstate, &u->open_downgrade.od_stateid); |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 7473 | } |
| 7474 | |
| 7475 | void |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7476 | nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, |
| 7477 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7478 | { |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7479 | put_stateid(cstate, &u->open.op_stateid); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7480 | } |
| 7481 | |
| 7482 | void |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7483 | nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, |
| 7484 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 7485 | { |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7486 | put_stateid(cstate, &u->close.cl_stateid); |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 7487 | } |
| 7488 | |
| 7489 | void |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7490 | nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, |
| 7491 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 7492 | { |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7493 | put_stateid(cstate, &u->lock.lk_resp_stateid); |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 7494 | } |
| 7495 | |
| 7496 | /* |
| 7497 | * functions to consume current state id |
| 7498 | */ |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 7499 | |
| 7500 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7501 | nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, |
| 7502 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 7503 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7504 | get_stateid(cstate, &u->open_downgrade.od_stateid); |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 7505 | } |
| 7506 | |
| 7507 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7508 | nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, |
| 7509 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 7510 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7511 | get_stateid(cstate, &u->delegreturn.dr_stateid); |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 7512 | } |
| 7513 | |
| 7514 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7515 | nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, |
| 7516 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 7517 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7518 | get_stateid(cstate, &u->free_stateid.fr_stateid); |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 7519 | } |
| 7520 | |
| 7521 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7522 | nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, |
| 7523 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 7524 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7525 | get_stateid(cstate, &u->setattr.sa_stateid); |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 7526 | } |
| 7527 | |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 7528 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7529 | nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, |
| 7530 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7531 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7532 | get_stateid(cstate, &u->close.cl_stateid); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7533 | } |
| 7534 | |
| 7535 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7536 | nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, |
| 7537 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7538 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7539 | get_stateid(cstate, &u->locku.lu_stateid); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7540 | } |
Tigran Mkrtchyan | 30813e2 | 2012-02-13 22:55:26 +0100 | [diff] [blame] | 7541 | |
| 7542 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7543 | nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, |
| 7544 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 30813e2 | 2012-02-13 22:55:26 +0100 | [diff] [blame] | 7545 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7546 | get_stateid(cstate, &u->read.rd_stateid); |
Tigran Mkrtchyan | 30813e2 | 2012-02-13 22:55:26 +0100 | [diff] [blame] | 7547 | } |
| 7548 | |
| 7549 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7550 | nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, |
| 7551 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 30813e2 | 2012-02-13 22:55:26 +0100 | [diff] [blame] | 7552 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7553 | get_stateid(cstate, &u->write.wr_stateid); |
Tigran Mkrtchyan | 30813e2 | 2012-02-13 22:55:26 +0100 | [diff] [blame] | 7554 | } |