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> |
Boaz Harrosh | 9a74af2 | 2009-12-03 20:30:56 +0200 | [diff] [blame] | 46 | #include "xdr4.h" |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 47 | #include "xdr4cb.h" |
J. Bruce Fields | 0a3adad | 2009-11-04 18:12:35 -0500 | [diff] [blame] | 48 | #include "vfs.h" |
J. Bruce Fields | bfa4b36 | 2012-04-25 16:49:18 -0400 | [diff] [blame] | 49 | #include "current_stateid.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 51 | #include "netns.h" |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 52 | #include "pnfs.h" |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 53 | #include "filecache.h" |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 54 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | #define NFSDDBG_FACILITY NFSDDBG_PROC |
| 56 | |
J. Bruce Fields | f32f3c2 | 2011-12-12 15:00:35 -0500 | [diff] [blame] | 57 | #define all_ones {{~0,~0},~0} |
| 58 | static const stateid_t one_stateid = { |
| 59 | .si_generation = ~0, |
| 60 | .si_opaque = all_ones, |
| 61 | }; |
| 62 | static const stateid_t zero_stateid = { |
| 63 | /* all fields zero */ |
| 64 | }; |
Tigran Mkrtchyan | 19ff0f2 | 2012-02-13 22:55:23 +0100 | [diff] [blame] | 65 | static const stateid_t currentstateid = { |
| 66 | .si_generation = 1, |
| 67 | }; |
Trond Myklebust | fb500a7 | 2017-11-03 08:00:12 -0400 | [diff] [blame] | 68 | static const stateid_t close_stateid = { |
| 69 | .si_generation = 0xffffffffU, |
| 70 | }; |
J. Bruce Fields | f32f3c2 | 2011-12-12 15:00:35 -0500 | [diff] [blame] | 71 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 72 | static u64 current_sessionid = 1; |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 73 | |
J. Bruce Fields | f32f3c2 | 2011-12-12 15:00:35 -0500 | [diff] [blame] | 74 | #define ZERO_STATEID(stateid) (!memcmp((stateid), &zero_stateid, sizeof(stateid_t))) |
| 75 | #define ONE_STATEID(stateid) (!memcmp((stateid), &one_stateid, sizeof(stateid_t))) |
Tigran Mkrtchyan | 19ff0f2 | 2012-02-13 22:55:23 +0100 | [diff] [blame] | 76 | #define CURRENT_STATEID(stateid) (!memcmp((stateid), ¤tstateid, sizeof(stateid_t))) |
Andrew Elble | ae254dac | 2017-11-09 13:41:10 -0500 | [diff] [blame] | 77 | #define CLOSE_STATEID(stateid) (!memcmp((stateid), &close_stateid, sizeof(stateid_t))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | /* forward declarations */ |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 80 | 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] | 81 | static void nfs4_free_ol_stateid(struct nfs4_stid *stid); |
Scott Mayhew | 362063a | 2019-03-26 18:06:28 -0400 | [diff] [blame] | 82 | void nfsd4_end_grace(struct nfsd_net *nn); |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 83 | 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] | 84 | |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 85 | /* Locking: */ |
| 86 | |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 87 | /* |
| 88 | * Currently used for the del_recall_lru and file hash table. In an |
| 89 | * effort to decrease the scope of the client_mutex, this spinlock may |
| 90 | * eventually cover more: |
| 91 | */ |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 92 | static DEFINE_SPINLOCK(state_lock); |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 93 | |
Andrew Elble | 4f34bd0 | 2017-11-08 17:29:51 -0500 | [diff] [blame] | 94 | enum nfsd4_st_mutex_lock_subclass { |
| 95 | OPEN_STATEID_MUTEX = 0, |
| 96 | LOCK_STATEID_MUTEX = 1, |
| 97 | }; |
| 98 | |
Jeff Layton | b401be22 | 2014-07-29 21:34:33 -0400 | [diff] [blame] | 99 | /* |
| 100 | * A waitqueue for all in-progress 4.0 CLOSE operations that are waiting for |
| 101 | * the refcount on the open stateid to drop. |
| 102 | */ |
| 103 | static DECLARE_WAIT_QUEUE_HEAD(close_wq); |
| 104 | |
J. Bruce Fields | 89c905b | 2019-06-19 12:43:11 -0400 | [diff] [blame] | 105 | /* |
| 106 | * A waitqueue where a writer to clients/#/ctl destroying a client can |
| 107 | * wait for cl_rpc_users to drop to 0 and then for the client to be |
| 108 | * unhashed. |
| 109 | */ |
| 110 | static DECLARE_WAIT_QUEUE_HEAD(expiry_wq); |
| 111 | |
Jeff Layton | 9258a2d | 2018-03-16 09:47:22 -0400 | [diff] [blame] | 112 | static struct kmem_cache *client_slab; |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 113 | static struct kmem_cache *openowner_slab; |
| 114 | static struct kmem_cache *lockowner_slab; |
| 115 | static struct kmem_cache *file_slab; |
| 116 | static struct kmem_cache *stateid_slab; |
| 117 | static struct kmem_cache *deleg_slab; |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 118 | static struct kmem_cache *odstate_slab; |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 119 | |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 120 | static void free_session(struct nfsd4_session *); |
Benny Halevy | 508dc6e | 2012-02-23 17:40:52 -0800 | [diff] [blame] | 121 | |
Julia Lawall | c4cb897 | 2015-11-21 22:57:39 +0100 | [diff] [blame] | 122 | static const struct nfsd4_callback_ops nfsd4_cb_recall_ops; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 123 | static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops; |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 124 | |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 125 | static bool is_session_dead(struct nfsd4_session *ses) |
Benny Halevy | 508dc6e | 2012-02-23 17:40:52 -0800 | [diff] [blame] | 126 | { |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 127 | return ses->se_flags & NFS4_SESSION_DEAD; |
| 128 | } |
| 129 | |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 130 | static __be32 mark_session_dead_locked(struct nfsd4_session *ses, int ref_held_by_me) |
| 131 | { |
| 132 | if (atomic_read(&ses->se_ref) > ref_held_by_me) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 133 | return nfserr_jukebox; |
| 134 | ses->se_flags |= NFS4_SESSION_DEAD; |
| 135 | return nfs_ok; |
| 136 | } |
| 137 | |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 138 | static bool is_client_expired(struct nfs4_client *clp) |
| 139 | { |
| 140 | return clp->cl_time == 0; |
| 141 | } |
| 142 | |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 143 | static __be32 get_client_locked(struct nfs4_client *clp) |
| 144 | { |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 145 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 146 | |
| 147 | lockdep_assert_held(&nn->client_lock); |
| 148 | |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 149 | if (is_client_expired(clp)) |
| 150 | return nfserr_expired; |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 151 | atomic_inc(&clp->cl_rpc_users); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 152 | return nfs_ok; |
| 153 | } |
| 154 | |
| 155 | /* must be called under the client_lock */ |
| 156 | static inline void |
| 157 | renew_client_locked(struct nfs4_client *clp) |
| 158 | { |
| 159 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 160 | |
| 161 | if (is_client_expired(clp)) { |
| 162 | WARN_ON(1); |
| 163 | printk("%s: client (clientid %08x/%08x) already expired\n", |
| 164 | __func__, |
| 165 | clp->cl_clientid.cl_boot, |
| 166 | clp->cl_clientid.cl_id); |
| 167 | return; |
| 168 | } |
| 169 | |
| 170 | dprintk("renewing client (clientid %08x/%08x)\n", |
| 171 | clp->cl_clientid.cl_boot, |
| 172 | clp->cl_clientid.cl_id); |
| 173 | list_move_tail(&clp->cl_lru, &nn->client_lru); |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 174 | clp->cl_time = ktime_get_boottime_seconds(); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 175 | } |
| 176 | |
Fengguang Wu | ba13843 | 2013-04-16 22:14:15 -0400 | [diff] [blame] | 177 | static void put_client_renew_locked(struct nfs4_client *clp) |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 178 | { |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 179 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 180 | |
| 181 | lockdep_assert_held(&nn->client_lock); |
| 182 | |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 183 | if (!atomic_dec_and_test(&clp->cl_rpc_users)) |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 184 | return; |
| 185 | if (!is_client_expired(clp)) |
| 186 | renew_client_locked(clp); |
J. Bruce Fields | 89c905b | 2019-06-19 12:43:11 -0400 | [diff] [blame] | 187 | else |
| 188 | wake_up_all(&expiry_wq); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 189 | } |
| 190 | |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 191 | static void put_client_renew(struct nfs4_client *clp) |
| 192 | { |
| 193 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 194 | |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 195 | if (!atomic_dec_and_lock(&clp->cl_rpc_users, &nn->client_lock)) |
Jeff Layton | d6c249b | 2014-07-08 14:02:50 -0400 | [diff] [blame] | 196 | return; |
| 197 | if (!is_client_expired(clp)) |
| 198 | renew_client_locked(clp); |
J. Bruce Fields | 89c905b | 2019-06-19 12:43:11 -0400 | [diff] [blame] | 199 | else |
| 200 | wake_up_all(&expiry_wq); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 201 | spin_unlock(&nn->client_lock); |
| 202 | } |
| 203 | |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 204 | static __be32 nfsd4_get_session_locked(struct nfsd4_session *ses) |
| 205 | { |
| 206 | __be32 status; |
| 207 | |
| 208 | if (is_session_dead(ses)) |
| 209 | return nfserr_badsession; |
| 210 | status = get_client_locked(ses->se_client); |
| 211 | if (status) |
| 212 | return status; |
| 213 | atomic_inc(&ses->se_ref); |
| 214 | return nfs_ok; |
| 215 | } |
| 216 | |
| 217 | static void nfsd4_put_session_locked(struct nfsd4_session *ses) |
| 218 | { |
| 219 | struct nfs4_client *clp = ses->se_client; |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 220 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 221 | |
| 222 | lockdep_assert_held(&nn->client_lock); |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 223 | |
| 224 | if (atomic_dec_and_test(&ses->se_ref) && is_session_dead(ses)) |
| 225 | free_session(ses); |
| 226 | put_client_renew_locked(clp); |
| 227 | } |
| 228 | |
| 229 | static void nfsd4_put_session(struct nfsd4_session *ses) |
| 230 | { |
| 231 | struct nfs4_client *clp = ses->se_client; |
| 232 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 233 | |
| 234 | spin_lock(&nn->client_lock); |
| 235 | nfsd4_put_session_locked(ses); |
| 236 | spin_unlock(&nn->client_lock); |
| 237 | } |
| 238 | |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 239 | static struct nfsd4_blocked_lock * |
| 240 | find_blocked_lock(struct nfs4_lockowner *lo, struct knfsd_fh *fh, |
| 241 | struct nfsd_net *nn) |
| 242 | { |
| 243 | struct nfsd4_blocked_lock *cur, *found = NULL; |
| 244 | |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 245 | spin_lock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 246 | list_for_each_entry(cur, &lo->lo_blocked, nbl_list) { |
| 247 | if (fh_match(fh, &cur->nbl_fh)) { |
| 248 | list_del_init(&cur->nbl_list); |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 249 | list_del_init(&cur->nbl_lru); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 250 | found = cur; |
| 251 | break; |
| 252 | } |
| 253 | } |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 254 | spin_unlock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 255 | if (found) |
NeilBrown | cb03f94 | 2018-11-30 10:04:08 +1100 | [diff] [blame] | 256 | locks_delete_block(&found->nbl_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 257 | return found; |
| 258 | } |
| 259 | |
| 260 | static struct nfsd4_blocked_lock * |
| 261 | find_or_allocate_block(struct nfs4_lockowner *lo, struct knfsd_fh *fh, |
| 262 | struct nfsd_net *nn) |
| 263 | { |
| 264 | struct nfsd4_blocked_lock *nbl; |
| 265 | |
| 266 | nbl = find_blocked_lock(lo, fh, nn); |
| 267 | if (!nbl) { |
| 268 | nbl= kmalloc(sizeof(*nbl), GFP_KERNEL); |
| 269 | if (nbl) { |
Vasily Averin | e1e8399 | 2020-03-27 07:50:40 +0300 | [diff] [blame^] | 270 | INIT_LIST_HEAD(&nbl->nbl_list); |
| 271 | INIT_LIST_HEAD(&nbl->nbl_lru); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 272 | fh_copy_shallow(&nbl->nbl_fh, fh); |
| 273 | locks_init_lock(&nbl->nbl_lock); |
| 274 | nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client, |
| 275 | &nfsd4_cb_notify_lock_ops, |
| 276 | NFSPROC4_CLNT_CB_NOTIFY_LOCK); |
| 277 | } |
| 278 | } |
| 279 | return nbl; |
| 280 | } |
| 281 | |
| 282 | static void |
| 283 | free_blocked_lock(struct nfsd4_blocked_lock *nbl) |
| 284 | { |
Jeff Layton | 6aaafc4 | 2019-04-22 12:34:23 -0400 | [diff] [blame] | 285 | locks_delete_block(&nbl->nbl_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 286 | locks_release_private(&nbl->nbl_lock); |
| 287 | kfree(nbl); |
| 288 | } |
| 289 | |
Jeff Layton | 68ef3bc | 2018-03-16 11:32:02 -0400 | [diff] [blame] | 290 | static void |
| 291 | remove_blocked_locks(struct nfs4_lockowner *lo) |
| 292 | { |
| 293 | struct nfs4_client *clp = lo->lo_owner.so_client; |
| 294 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 295 | struct nfsd4_blocked_lock *nbl; |
| 296 | LIST_HEAD(reaplist); |
| 297 | |
| 298 | /* Dequeue all blocked locks */ |
| 299 | spin_lock(&nn->blocked_locks_lock); |
| 300 | while (!list_empty(&lo->lo_blocked)) { |
| 301 | nbl = list_first_entry(&lo->lo_blocked, |
| 302 | struct nfsd4_blocked_lock, |
| 303 | nbl_list); |
| 304 | list_del_init(&nbl->nbl_list); |
| 305 | list_move(&nbl->nbl_lru, &reaplist); |
| 306 | } |
| 307 | spin_unlock(&nn->blocked_locks_lock); |
| 308 | |
| 309 | /* Now free them */ |
| 310 | while (!list_empty(&reaplist)) { |
| 311 | nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock, |
| 312 | nbl_lru); |
| 313 | list_del_init(&nbl->nbl_lru); |
Jeff Layton | 68ef3bc | 2018-03-16 11:32:02 -0400 | [diff] [blame] | 314 | free_blocked_lock(nbl); |
| 315 | } |
| 316 | } |
| 317 | |
Jeff Layton | f456458 | 2019-04-22 12:34:24 -0400 | [diff] [blame] | 318 | static void |
| 319 | nfsd4_cb_notify_lock_prepare(struct nfsd4_callback *cb) |
| 320 | { |
| 321 | struct nfsd4_blocked_lock *nbl = container_of(cb, |
| 322 | struct nfsd4_blocked_lock, nbl_cb); |
| 323 | locks_delete_block(&nbl->nbl_lock); |
| 324 | } |
| 325 | |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 326 | static int |
| 327 | nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task) |
| 328 | { |
| 329 | /* |
| 330 | * Since this is just an optimization, we don't try very hard if it |
| 331 | * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and |
| 332 | * just quit trying on anything else. |
| 333 | */ |
| 334 | switch (task->tk_status) { |
| 335 | case -NFS4ERR_DELAY: |
| 336 | rpc_delay(task, 1 * HZ); |
| 337 | return 0; |
| 338 | default: |
| 339 | return 1; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | static void |
| 344 | nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb) |
| 345 | { |
| 346 | struct nfsd4_blocked_lock *nbl = container_of(cb, |
| 347 | struct nfsd4_blocked_lock, nbl_cb); |
| 348 | |
| 349 | free_blocked_lock(nbl); |
| 350 | } |
| 351 | |
| 352 | static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = { |
Jeff Layton | f456458 | 2019-04-22 12:34:24 -0400 | [diff] [blame] | 353 | .prepare = nfsd4_cb_notify_lock_prepare, |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 354 | .done = nfsd4_cb_notify_lock_done, |
| 355 | .release = nfsd4_cb_notify_lock_release, |
| 356 | }; |
| 357 | |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 358 | static inline struct nfs4_stateowner * |
| 359 | nfs4_get_stateowner(struct nfs4_stateowner *sop) |
| 360 | { |
| 361 | atomic_inc(&sop->so_count); |
| 362 | return sop; |
| 363 | } |
| 364 | |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 365 | static int |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 366 | same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner) |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 367 | { |
| 368 | return (sop->so_owner.len == owner->len) && |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 369 | 0 == memcmp(sop->so_owner.data, owner->data, owner->len); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | static struct nfs4_openowner * |
| 373 | find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open, |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 374 | struct nfs4_client *clp) |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 375 | { |
| 376 | struct nfs4_stateowner *so; |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 377 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 378 | lockdep_assert_held(&clp->cl_lock); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 379 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 380 | list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval], |
| 381 | so_strhash) { |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 382 | if (!so->so_is_open_owner) |
| 383 | continue; |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 384 | if (same_owner_str(so, &open->op_owner)) |
| 385 | return openowner(nfs4_get_stateowner(so)); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 386 | } |
| 387 | return NULL; |
| 388 | } |
| 389 | |
| 390 | static struct nfs4_openowner * |
| 391 | find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open, |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 392 | struct nfs4_client *clp) |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 393 | { |
| 394 | struct nfs4_openowner *oo; |
| 395 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 396 | spin_lock(&clp->cl_lock); |
| 397 | oo = find_openstateowner_str_locked(hashval, open, clp); |
| 398 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 399 | return oo; |
| 400 | } |
| 401 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 402 | static inline u32 |
| 403 | opaque_hashval(const void *ptr, int nbytes) |
| 404 | { |
| 405 | unsigned char *cptr = (unsigned char *) ptr; |
| 406 | |
| 407 | u32 x = 0; |
| 408 | while (nbytes--) { |
| 409 | x *= 37; |
| 410 | x += *cptr++; |
| 411 | } |
| 412 | return x; |
| 413 | } |
| 414 | |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 415 | static void nfsd4_free_file_rcu(struct rcu_head *rcu) |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 416 | { |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 417 | struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu); |
| 418 | |
| 419 | kmem_cache_free(file_slab, fp); |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 420 | } |
| 421 | |
Christoph Hellwig | e6ba76e | 2014-08-14 08:50:16 +0200 | [diff] [blame] | 422 | void |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 423 | put_nfs4_file(struct nfs4_file *fi) |
| 424 | { |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 425 | might_lock(&state_lock); |
| 426 | |
Elena Reshetova | 818a34e | 2017-10-20 12:53:30 +0300 | [diff] [blame] | 427 | if (refcount_dec_and_lock(&fi->fi_ref, &state_lock)) { |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 428 | hlist_del_rcu(&fi->fi_hash); |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 429 | spin_unlock(&state_lock); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 430 | WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate)); |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 431 | WARN_ON_ONCE(!list_empty(&fi->fi_delegations)); |
| 432 | call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu); |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 433 | } |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 434 | } |
| 435 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 436 | static struct nfsd_file * |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 437 | __nfs4_get_fd(struct nfs4_file *f, int oflag) |
| 438 | { |
| 439 | if (f->fi_fds[oflag]) |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 440 | return nfsd_file_get(f->fi_fds[oflag]); |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 441 | return NULL; |
| 442 | } |
| 443 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 444 | static struct nfsd_file * |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 445 | find_writeable_file_locked(struct nfs4_file *f) |
| 446 | { |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 447 | struct nfsd_file *ret; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 448 | |
| 449 | lockdep_assert_held(&f->fi_lock); |
| 450 | |
| 451 | ret = __nfs4_get_fd(f, O_WRONLY); |
| 452 | if (!ret) |
| 453 | ret = __nfs4_get_fd(f, O_RDWR); |
| 454 | return ret; |
| 455 | } |
| 456 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 457 | static struct nfsd_file * |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 458 | find_writeable_file(struct nfs4_file *f) |
| 459 | { |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 460 | struct nfsd_file *ret; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 461 | |
| 462 | spin_lock(&f->fi_lock); |
| 463 | ret = find_writeable_file_locked(f); |
| 464 | spin_unlock(&f->fi_lock); |
| 465 | |
| 466 | return ret; |
| 467 | } |
| 468 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 469 | static struct nfsd_file * |
| 470 | find_readable_file_locked(struct nfs4_file *f) |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 471 | { |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 472 | struct nfsd_file *ret; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 473 | |
| 474 | lockdep_assert_held(&f->fi_lock); |
| 475 | |
| 476 | ret = __nfs4_get_fd(f, O_RDONLY); |
| 477 | if (!ret) |
| 478 | ret = __nfs4_get_fd(f, O_RDWR); |
| 479 | return ret; |
| 480 | } |
| 481 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 482 | static struct nfsd_file * |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 483 | find_readable_file(struct nfs4_file *f) |
| 484 | { |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 485 | struct nfsd_file *ret; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 486 | |
| 487 | spin_lock(&f->fi_lock); |
| 488 | ret = find_readable_file_locked(f); |
| 489 | spin_unlock(&f->fi_lock); |
| 490 | |
| 491 | return ret; |
| 492 | } |
| 493 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 494 | struct nfsd_file * |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 495 | find_any_file(struct nfs4_file *f) |
| 496 | { |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 497 | struct nfsd_file *ret; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 498 | |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 499 | if (!f) |
| 500 | return NULL; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 501 | spin_lock(&f->fi_lock); |
| 502 | ret = __nfs4_get_fd(f, O_RDWR); |
| 503 | if (!ret) { |
| 504 | ret = __nfs4_get_fd(f, O_WRONLY); |
| 505 | if (!ret) |
| 506 | ret = __nfs4_get_fd(f, O_RDONLY); |
| 507 | } |
| 508 | spin_unlock(&f->fi_lock); |
| 509 | return ret; |
| 510 | } |
| 511 | |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 512 | static atomic_long_t num_delegations; |
Zhang Yanfei | 697ce9b | 2013-02-22 16:35:47 -0800 | [diff] [blame] | 513 | unsigned long max_delegations; |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 514 | |
| 515 | /* |
| 516 | * Open owner state (share locks) |
| 517 | */ |
| 518 | |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 519 | /* hash tables for lock and open owners */ |
| 520 | #define OWNER_HASH_BITS 8 |
| 521 | #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS) |
| 522 | #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1) |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 523 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 524 | static unsigned int ownerstr_hashval(struct xdr_netobj *ownername) |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 525 | { |
| 526 | unsigned int ret; |
| 527 | |
| 528 | ret = opaque_hashval(ownername->data, ownername->len); |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 529 | return ret & OWNER_HASH_MASK; |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 530 | } |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 531 | |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 532 | /* hash table for nfs4_file */ |
| 533 | #define FILE_HASH_BITS 8 |
| 534 | #define FILE_HASH_SIZE (1 << FILE_HASH_BITS) |
Shan Wei | 3507958 | 2011-01-14 17:35:59 +0800 | [diff] [blame] | 535 | |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 536 | static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh) |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 537 | { |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 538 | return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0); |
| 539 | } |
| 540 | |
| 541 | static unsigned int file_hashval(struct knfsd_fh *fh) |
| 542 | { |
| 543 | return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1); |
| 544 | } |
| 545 | |
Jeff Layton | 89876f8 | 2013-04-02 09:01:59 -0400 | [diff] [blame] | 546 | static struct hlist_head file_hashtbl[FILE_HASH_SIZE]; |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 547 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 548 | static void |
| 549 | __nfs4_file_get_access(struct nfs4_file *fp, u32 access) |
J. Bruce Fields | 3477565 | 2013-08-23 17:55:18 -0400 | [diff] [blame] | 550 | { |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 551 | lockdep_assert_held(&fp->fi_lock); |
| 552 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 553 | if (access & NFS4_SHARE_ACCESS_WRITE) |
| 554 | atomic_inc(&fp->fi_access[O_WRONLY]); |
| 555 | if (access & NFS4_SHARE_ACCESS_READ) |
| 556 | atomic_inc(&fp->fi_access[O_RDONLY]); |
J. Bruce Fields | 3477565 | 2013-08-23 17:55:18 -0400 | [diff] [blame] | 557 | } |
| 558 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 559 | static __be32 |
| 560 | nfs4_file_get_access(struct nfs4_file *fp, u32 access) |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 561 | { |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 562 | lockdep_assert_held(&fp->fi_lock); |
| 563 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 564 | /* Does this access mode make sense? */ |
| 565 | if (access & ~NFS4_SHARE_ACCESS_BOTH) |
| 566 | return nfserr_inval; |
| 567 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 568 | /* Does it conflict with a deny mode already set? */ |
| 569 | if ((access & fp->fi_share_deny) != 0) |
| 570 | return nfserr_share_denied; |
| 571 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 572 | __nfs4_file_get_access(fp, access); |
| 573 | return nfs_ok; |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 574 | } |
| 575 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 576 | static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny) |
| 577 | { |
| 578 | /* Common case is that there is no deny mode. */ |
| 579 | if (deny) { |
| 580 | /* Does this deny mode make sense? */ |
| 581 | if (deny & ~NFS4_SHARE_DENY_BOTH) |
| 582 | return nfserr_inval; |
| 583 | |
| 584 | if ((deny & NFS4_SHARE_DENY_READ) && |
| 585 | atomic_read(&fp->fi_access[O_RDONLY])) |
| 586 | return nfserr_share_denied; |
| 587 | |
| 588 | if ((deny & NFS4_SHARE_DENY_WRITE) && |
| 589 | atomic_read(&fp->fi_access[O_WRONLY])) |
| 590 | return nfserr_share_denied; |
| 591 | } |
| 592 | return nfs_ok; |
| 593 | } |
| 594 | |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 595 | 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] | 596 | { |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 597 | might_lock(&fp->fi_lock); |
| 598 | |
| 599 | if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) { |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 600 | struct nfsd_file *f1 = NULL; |
| 601 | struct nfsd_file *f2 = NULL; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 602 | |
Jeff Layton | 6d338b5 | 2014-07-10 14:07:29 -0400 | [diff] [blame] | 603 | swap(f1, fp->fi_fds[oflag]); |
J. Bruce Fields | 0c7c3e6 | 2013-03-28 20:37:14 -0400 | [diff] [blame] | 604 | if (atomic_read(&fp->fi_access[1 - oflag]) == 0) |
Jeff Layton | 6d338b5 | 2014-07-10 14:07:29 -0400 | [diff] [blame] | 605 | swap(f2, fp->fi_fds[O_RDWR]); |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 606 | spin_unlock(&fp->fi_lock); |
| 607 | if (f1) |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 608 | nfsd_file_put(f1); |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 609 | if (f2) |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 610 | nfsd_file_put(f2); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 611 | } |
| 612 | } |
| 613 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 614 | 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] | 615 | { |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 616 | WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH); |
| 617 | |
| 618 | if (access & NFS4_SHARE_ACCESS_WRITE) |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 619 | __nfs4_file_put_access(fp, O_WRONLY); |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 620 | if (access & NFS4_SHARE_ACCESS_READ) |
| 621 | __nfs4_file_put_access(fp, O_RDONLY); |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 622 | } |
| 623 | |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 624 | /* |
| 625 | * Allocate a new open/delegation state counter. This is needed for |
| 626 | * pNFS for proper return on close semantics. |
| 627 | * |
| 628 | * Note that we only allocate it for pNFS-enabled exports, otherwise |
| 629 | * all pointers to struct nfs4_clnt_odstate are always NULL. |
| 630 | */ |
| 631 | static struct nfs4_clnt_odstate * |
| 632 | alloc_clnt_odstate(struct nfs4_client *clp) |
| 633 | { |
| 634 | struct nfs4_clnt_odstate *co; |
| 635 | |
| 636 | co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL); |
| 637 | if (co) { |
| 638 | co->co_client = clp; |
Elena Reshetova | cff7cb2 | 2017-10-20 12:53:29 +0300 | [diff] [blame] | 639 | refcount_set(&co->co_odcount, 1); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 640 | } |
| 641 | return co; |
| 642 | } |
| 643 | |
| 644 | static void |
| 645 | hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co) |
| 646 | { |
| 647 | struct nfs4_file *fp = co->co_file; |
| 648 | |
| 649 | lockdep_assert_held(&fp->fi_lock); |
| 650 | list_add(&co->co_perfile, &fp->fi_clnt_odstate); |
| 651 | } |
| 652 | |
| 653 | static inline void |
| 654 | get_clnt_odstate(struct nfs4_clnt_odstate *co) |
| 655 | { |
| 656 | if (co) |
Elena Reshetova | cff7cb2 | 2017-10-20 12:53:29 +0300 | [diff] [blame] | 657 | refcount_inc(&co->co_odcount); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | static void |
| 661 | put_clnt_odstate(struct nfs4_clnt_odstate *co) |
| 662 | { |
| 663 | struct nfs4_file *fp; |
| 664 | |
| 665 | if (!co) |
| 666 | return; |
| 667 | |
| 668 | fp = co->co_file; |
Elena Reshetova | cff7cb2 | 2017-10-20 12:53:29 +0300 | [diff] [blame] | 669 | if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) { |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 670 | list_del(&co->co_perfile); |
| 671 | spin_unlock(&fp->fi_lock); |
| 672 | |
| 673 | nfsd4_return_all_file_layouts(co->co_client, fp); |
| 674 | kmem_cache_free(odstate_slab, co); |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | static struct nfs4_clnt_odstate * |
| 679 | find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new) |
| 680 | { |
| 681 | struct nfs4_clnt_odstate *co; |
| 682 | struct nfs4_client *cl; |
| 683 | |
| 684 | if (!new) |
| 685 | return NULL; |
| 686 | |
| 687 | cl = new->co_client; |
| 688 | |
| 689 | spin_lock(&fp->fi_lock); |
| 690 | list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) { |
| 691 | if (co->co_client == cl) { |
| 692 | get_clnt_odstate(co); |
| 693 | goto out; |
| 694 | } |
| 695 | } |
| 696 | co = new; |
| 697 | co->co_file = fp; |
| 698 | hash_clnt_odstate_locked(new); |
| 699 | out: |
| 700 | spin_unlock(&fp->fi_lock); |
| 701 | return co; |
| 702 | } |
| 703 | |
Kinglong Mee | d19fb70 | 2017-01-18 19:04:42 +0800 | [diff] [blame] | 704 | struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab, |
| 705 | void (*sc_free)(struct nfs4_stid *)) |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 706 | { |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 707 | struct nfs4_stid *stid; |
| 708 | int new_id; |
| 709 | |
Trond Myklebust | f833883 | 2014-07-25 07:34:19 -0400 | [diff] [blame] | 710 | stid = kmem_cache_zalloc(slab, GFP_KERNEL); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 711 | if (!stid) |
| 712 | return NULL; |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 713 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 714 | idr_preload(GFP_KERNEL); |
| 715 | spin_lock(&cl->cl_lock); |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 716 | /* Reserving 0 for start of file in nfsdfs "states" file: */ |
| 717 | 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] | 718 | spin_unlock(&cl->cl_lock); |
| 719 | idr_preload_end(); |
Tejun Heo | ebd6c70 | 2013-03-13 14:59:37 -0700 | [diff] [blame] | 720 | if (new_id < 0) |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 721 | goto out_free; |
Kinglong Mee | d19fb70 | 2017-01-18 19:04:42 +0800 | [diff] [blame] | 722 | |
| 723 | stid->sc_free = sc_free; |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 724 | stid->sc_client = cl; |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 725 | stid->sc_stateid.si_opaque.so_id = new_id; |
| 726 | stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid; |
| 727 | /* Will be incremented before return to client: */ |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 728 | refcount_set(&stid->sc_count, 1); |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 729 | spin_lock_init(&stid->sc_lock); |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 730 | INIT_LIST_HEAD(&stid->sc_cp_list); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 731 | |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 732 | /* |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 733 | * It shouldn't be a problem to reuse an opaque stateid value. |
| 734 | * I don't think it is for 4.1. But with 4.0 I worry that, for |
| 735 | * example, a stray write retransmission could be accepted by |
| 736 | * the server when it should have been rejected. Therefore, |
| 737 | * adopt a trick from the sctp code to attempt to maximize the |
| 738 | * amount of time until an id is reused, by ensuring they always |
| 739 | * "increase" (mod INT_MAX): |
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 | return stid; |
| 742 | out_free: |
Wei Yongjun | 2c44a23 | 2013-04-09 14:15:31 +0800 | [diff] [blame] | 743 | kmem_cache_free(slab, stid); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 744 | return NULL; |
J. Bruce Fields | 2a74aba | 2011-09-23 17:20:02 -0400 | [diff] [blame] | 745 | } |
| 746 | |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 747 | /* |
| 748 | * Create a unique stateid_t to represent each COPY. |
| 749 | */ |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 750 | static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid, |
| 751 | unsigned char sc_type) |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 752 | { |
| 753 | int new_id; |
| 754 | |
Arnd Bergmann | 9cc7680 | 2019-10-31 15:53:13 +0100 | [diff] [blame] | 755 | stid->stid.si_opaque.so_clid.cl_boot = (u32)nn->boot_time; |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 756 | stid->stid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id; |
| 757 | stid->sc_type = sc_type; |
| 758 | |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 759 | idr_preload(GFP_KERNEL); |
| 760 | spin_lock(&nn->s2s_cp_lock); |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 761 | new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, stid, 0, 0, GFP_NOWAIT); |
| 762 | stid->stid.si_opaque.so_id = new_id; |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 763 | spin_unlock(&nn->s2s_cp_lock); |
| 764 | idr_preload_end(); |
| 765 | if (new_id < 0) |
| 766 | return 0; |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 767 | return 1; |
| 768 | } |
| 769 | |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 770 | int nfs4_init_copy_state(struct nfsd_net *nn, struct nfsd4_copy *copy) |
| 771 | { |
| 772 | return nfs4_init_cp_state(nn, ©->cp_stateid, NFS4_COPY_STID); |
| 773 | } |
| 774 | |
| 775 | struct nfs4_cpntf_state *nfs4_alloc_init_cpntf_state(struct nfsd_net *nn, |
| 776 | struct nfs4_stid *p_stid) |
| 777 | { |
| 778 | struct nfs4_cpntf_state *cps; |
| 779 | |
| 780 | cps = kzalloc(sizeof(struct nfs4_cpntf_state), GFP_KERNEL); |
| 781 | if (!cps) |
| 782 | return NULL; |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 783 | cps->cpntf_time = ktime_get_boottime_seconds(); |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 784 | refcount_set(&cps->cp_stateid.sc_count, 1); |
| 785 | if (!nfs4_init_cp_state(nn, &cps->cp_stateid, NFS4_COPYNOTIFY_STID)) |
| 786 | goto out_free; |
| 787 | spin_lock(&nn->s2s_cp_lock); |
| 788 | list_add(&cps->cp_list, &p_stid->sc_cp_list); |
| 789 | spin_unlock(&nn->s2s_cp_lock); |
| 790 | return cps; |
| 791 | out_free: |
| 792 | kfree(cps); |
| 793 | return NULL; |
| 794 | } |
| 795 | |
| 796 | void nfs4_free_copy_state(struct nfsd4_copy *copy) |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 797 | { |
| 798 | struct nfsd_net *nn; |
| 799 | |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 800 | WARN_ON_ONCE(copy->cp_stateid.sc_type != NFS4_COPY_STID); |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 801 | nn = net_generic(copy->cp_clp->net, nfsd_net_id); |
| 802 | spin_lock(&nn->s2s_cp_lock); |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 803 | idr_remove(&nn->s2s_cp_stateids, |
| 804 | copy->cp_stateid.stid.si_opaque.so_id); |
| 805 | spin_unlock(&nn->s2s_cp_lock); |
| 806 | } |
| 807 | |
| 808 | static void nfs4_free_cpntf_statelist(struct net *net, struct nfs4_stid *stid) |
| 809 | { |
| 810 | struct nfs4_cpntf_state *cps; |
| 811 | struct nfsd_net *nn; |
| 812 | |
| 813 | nn = net_generic(net, nfsd_net_id); |
| 814 | spin_lock(&nn->s2s_cp_lock); |
| 815 | while (!list_empty(&stid->sc_cp_list)) { |
| 816 | cps = list_first_entry(&stid->sc_cp_list, |
| 817 | struct nfs4_cpntf_state, cp_list); |
| 818 | _free_cpntf_state_locked(nn, cps); |
| 819 | } |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 820 | spin_unlock(&nn->s2s_cp_lock); |
| 821 | } |
| 822 | |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 823 | 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] | 824 | { |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 825 | struct nfs4_stid *stid; |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 826 | |
Kinglong Mee | d19fb70 | 2017-01-18 19:04:42 +0800 | [diff] [blame] | 827 | stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 828 | if (!stid) |
| 829 | return NULL; |
| 830 | |
Kinglong Mee | d19fb70 | 2017-01-18 19:04:42 +0800 | [diff] [blame] | 831 | return openlockstateid(stid); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | static void nfs4_free_deleg(struct nfs4_stid *stid) |
| 835 | { |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 836 | kmem_cache_free(deleg_slab, stid); |
| 837 | atomic_long_dec(&num_delegations); |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 838 | } |
| 839 | |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 840 | /* |
| 841 | * When we recall a delegation, we should be careful not to hand it |
| 842 | * out again straight away. |
| 843 | * To ensure this we keep a pair of bloom filters ('new' and 'old') |
| 844 | * in which the filehandles of recalled delegations are "stored". |
| 845 | * If a filehandle appear in either filter, a delegation is blocked. |
| 846 | * When a delegation is recalled, the filehandle is stored in the "new" |
| 847 | * filter. |
| 848 | * Every 30 seconds we swap the filters and clear the "new" one, |
| 849 | * unless both are empty of course. |
| 850 | * |
| 851 | * Each filter is 256 bits. We hash the filehandle to 32bit and use the |
| 852 | * low 3 bytes as hash-table indices. |
| 853 | * |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 854 | * 'blocked_delegations_lock', which is always taken in block_delegations(), |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 855 | * is used to manage concurrent access. Testing does not need the lock |
| 856 | * except when swapping the two filters. |
| 857 | */ |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 858 | static DEFINE_SPINLOCK(blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 859 | static struct bloom_pair { |
| 860 | int entries, old_entries; |
Arnd Bergmann | b3f255e | 2017-10-20 11:25:34 +0200 | [diff] [blame] | 861 | time64_t swap_time; |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 862 | int new; /* index into 'set' */ |
| 863 | DECLARE_BITMAP(set[2], 256); |
| 864 | } blocked_delegations; |
| 865 | |
| 866 | static int delegation_blocked(struct knfsd_fh *fh) |
| 867 | { |
| 868 | u32 hash; |
| 869 | struct bloom_pair *bd = &blocked_delegations; |
| 870 | |
| 871 | if (bd->entries == 0) |
| 872 | return 0; |
Arnd Bergmann | b3f255e | 2017-10-20 11:25:34 +0200 | [diff] [blame] | 873 | if (ktime_get_seconds() - bd->swap_time > 30) { |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 874 | spin_lock(&blocked_delegations_lock); |
Arnd Bergmann | b3f255e | 2017-10-20 11:25:34 +0200 | [diff] [blame] | 875 | if (ktime_get_seconds() - bd->swap_time > 30) { |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 876 | bd->entries -= bd->old_entries; |
| 877 | bd->old_entries = bd->entries; |
| 878 | memset(bd->set[bd->new], 0, |
| 879 | sizeof(bd->set[0])); |
| 880 | bd->new = 1-bd->new; |
Arnd Bergmann | b3f255e | 2017-10-20 11:25:34 +0200 | [diff] [blame] | 881 | bd->swap_time = ktime_get_seconds(); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 882 | } |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 883 | spin_unlock(&blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 884 | } |
Daniel Borkmann | 8754589 | 2014-12-10 16:33:11 +0100 | [diff] [blame] | 885 | hash = jhash(&fh->fh_base, fh->fh_size, 0); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 886 | if (test_bit(hash&255, bd->set[0]) && |
| 887 | test_bit((hash>>8)&255, bd->set[0]) && |
| 888 | test_bit((hash>>16)&255, bd->set[0])) |
| 889 | return 1; |
| 890 | |
| 891 | if (test_bit(hash&255, bd->set[1]) && |
| 892 | test_bit((hash>>8)&255, bd->set[1]) && |
| 893 | test_bit((hash>>16)&255, bd->set[1])) |
| 894 | return 1; |
| 895 | |
| 896 | return 0; |
| 897 | } |
| 898 | |
| 899 | static void block_delegations(struct knfsd_fh *fh) |
| 900 | { |
| 901 | u32 hash; |
| 902 | struct bloom_pair *bd = &blocked_delegations; |
| 903 | |
Daniel Borkmann | 8754589 | 2014-12-10 16:33:11 +0100 | [diff] [blame] | 904 | hash = jhash(&fh->fh_base, fh->fh_size, 0); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 905 | |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 906 | spin_lock(&blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 907 | __set_bit(hash&255, bd->set[bd->new]); |
| 908 | __set_bit((hash>>8)&255, bd->set[bd->new]); |
| 909 | __set_bit((hash>>16)&255, bd->set[bd->new]); |
| 910 | if (bd->entries == 0) |
Arnd Bergmann | b3f255e | 2017-10-20 11:25:34 +0200 | [diff] [blame] | 911 | bd->swap_time = ktime_get_seconds(); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 912 | bd->entries += 1; |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 913 | spin_unlock(&blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 914 | } |
| 915 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | static struct nfs4_delegation * |
J. Bruce Fields | 86d29b1 | 2018-02-21 15:27:28 -0500 | [diff] [blame] | 917 | alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp, |
| 918 | struct svc_fh *current_fh, |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 919 | struct nfs4_clnt_odstate *odstate) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 920 | { |
| 921 | struct nfs4_delegation *dp; |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 922 | long n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | |
| 924 | dprintk("NFSD alloc_init_deleg\n"); |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 925 | n = atomic_long_inc_return(&num_delegations); |
| 926 | if (n < 0 || n > max_delegations) |
| 927 | goto out_dec; |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 928 | if (delegation_blocked(¤t_fh->fh_handle)) |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 929 | goto out_dec; |
Kinglong Mee | d19fb70 | 2017-01-18 19:04:42 +0800 | [diff] [blame] | 930 | dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg)); |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 931 | if (dp == NULL) |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 932 | goto out_dec; |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 933 | |
J. Bruce Fields | 2a74aba | 2011-09-23 17:20:02 -0400 | [diff] [blame] | 934 | /* |
| 935 | * delegation seqid's are never incremented. The 4.1 special |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 936 | * meaning of seqid 0 isn't meaningful, really, but let's avoid |
| 937 | * 0 anyway just for consistency and use 1: |
J. Bruce Fields | 2a74aba | 2011-09-23 17:20:02 -0400 | [diff] [blame] | 938 | */ |
| 939 | dp->dl_stid.sc_stateid.si_generation = 1; |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 940 | INIT_LIST_HEAD(&dp->dl_perfile); |
| 941 | INIT_LIST_HEAD(&dp->dl_perclnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | INIT_LIST_HEAD(&dp->dl_recall_lru); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 943 | dp->dl_clnt_odstate = odstate; |
| 944 | get_clnt_odstate(odstate); |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 945 | dp->dl_type = NFS4_OPEN_DELEGATE_READ; |
Christoph Hellwig | f0b5de1 | 2014-09-24 12:19:18 +0200 | [diff] [blame] | 946 | dp->dl_retries = 1; |
| 947 | nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client, |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 948 | &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL); |
J. Bruce Fields | 86d29b1 | 2018-02-21 15:27:28 -0500 | [diff] [blame] | 949 | get_nfs4_file(fp); |
| 950 | dp->dl_stid.sc_file = fp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | return dp; |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 952 | out_dec: |
| 953 | atomic_long_dec(&num_delegations); |
| 954 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | void |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 958 | nfs4_put_stid(struct nfs4_stid *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 959 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 960 | struct nfs4_file *fp = s->sc_file; |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 961 | struct nfs4_client *clp = s->sc_client; |
| 962 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 963 | might_lock(&clp->cl_lock); |
| 964 | |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 965 | if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) { |
Jeff Layton | b401be22 | 2014-07-29 21:34:33 -0400 | [diff] [blame] | 966 | wake_up_all(&close_wq); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 967 | return; |
Jeff Layton | b401be22 | 2014-07-29 21:34:33 -0400 | [diff] [blame] | 968 | } |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 969 | idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id); |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 970 | nfs4_free_cpntf_statelist(clp->net, s); |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 971 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 972 | s->sc_free(s); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 973 | if (fp) |
| 974 | put_nfs4_file(fp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 975 | } |
| 976 | |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 977 | void |
| 978 | nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid) |
| 979 | { |
| 980 | stateid_t *src = &stid->sc_stateid; |
| 981 | |
| 982 | spin_lock(&stid->sc_lock); |
| 983 | if (unlikely(++src->si_generation == 0)) |
| 984 | src->si_generation = 1; |
| 985 | memcpy(dst, src, sizeof(*dst)); |
| 986 | spin_unlock(&stid->sc_lock); |
| 987 | } |
| 988 | |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 989 | static void put_deleg_file(struct nfs4_file *fp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 990 | { |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 991 | struct nfsd_file *nf = NULL; |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 992 | |
| 993 | spin_lock(&fp->fi_lock); |
| 994 | if (--fp->fi_delegees == 0) |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 995 | swap(nf, fp->fi_deleg_file); |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 996 | spin_unlock(&fp->fi_lock); |
| 997 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 998 | if (nf) |
| 999 | nfsd_file_put(nf); |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 1000 | } |
| 1001 | |
| 1002 | static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp) |
| 1003 | { |
J. Bruce Fields | cba7b3d | 2018-02-15 15:50:49 -0500 | [diff] [blame] | 1004 | struct nfs4_file *fp = dp->dl_stid.sc_file; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 1005 | struct nfsd_file *nf = fp->fi_deleg_file; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 1006 | |
J. Bruce Fields | b8232d3 | 2018-02-15 15:29:15 -0500 | [diff] [blame] | 1007 | WARN_ON_ONCE(!fp->fi_delegees); |
| 1008 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 1009 | vfs_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp); |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 1010 | put_deleg_file(fp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | } |
| 1012 | |
J. Bruce Fields | 0af6e69 | 2018-02-21 15:11:03 -0500 | [diff] [blame] | 1013 | static void destroy_unhashed_deleg(struct nfs4_delegation *dp) |
| 1014 | { |
| 1015 | put_clnt_odstate(dp->dl_clnt_odstate); |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 1016 | nfs4_unlock_deleg_lease(dp); |
J. Bruce Fields | 0af6e69 | 2018-02-21 15:11:03 -0500 | [diff] [blame] | 1017 | nfs4_put_stid(&dp->dl_stid); |
| 1018 | } |
| 1019 | |
Christoph Hellwig | cd61c52 | 2014-08-14 08:44:57 +0200 | [diff] [blame] | 1020 | void nfs4_unhash_stid(struct nfs4_stid *s) |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 1021 | { |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 1022 | s->sc_type = 0; |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 1023 | } |
| 1024 | |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1025 | /** |
J. Bruce Fields | 68b18f5 | 2018-02-19 11:38:33 -0500 | [diff] [blame] | 1026 | * nfs4_delegation_exists - Discover if this delegation already exists |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1027 | * @clp: a pointer to the nfs4_client we're granting a delegation to |
| 1028 | * @fp: a pointer to the nfs4_file we're granting a delegation on |
| 1029 | * |
| 1030 | * Return: |
J. Bruce Fields | 68b18f5 | 2018-02-19 11:38:33 -0500 | [diff] [blame] | 1031 | * On success: true iff an existing delegation is found |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1032 | */ |
| 1033 | |
J. Bruce Fields | 68b18f5 | 2018-02-19 11:38:33 -0500 | [diff] [blame] | 1034 | static bool |
| 1035 | nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp) |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 1036 | { |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1037 | struct nfs4_delegation *searchdp = NULL; |
| 1038 | struct nfs4_client *searchclp = NULL; |
| 1039 | |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 1040 | lockdep_assert_held(&state_lock); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 1041 | lockdep_assert_held(&fp->fi_lock); |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 1042 | |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1043 | list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) { |
| 1044 | searchclp = searchdp->dl_stid.sc_client; |
| 1045 | if (clp == searchclp) { |
Fengguang Wu | 51d87bc | 2018-03-22 13:37:20 +0800 | [diff] [blame] | 1046 | return true; |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1047 | } |
| 1048 | } |
Fengguang Wu | 51d87bc | 2018-03-22 13:37:20 +0800 | [diff] [blame] | 1049 | return false; |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1050 | } |
| 1051 | |
| 1052 | /** |
| 1053 | * hash_delegation_locked - Add a delegation to the appropriate lists |
| 1054 | * @dp: a pointer to the nfs4_delegation we are adding. |
| 1055 | * @fp: a pointer to the nfs4_file we're granting a delegation on |
| 1056 | * |
| 1057 | * Return: |
| 1058 | * On success: NULL if the delegation was successfully hashed. |
| 1059 | * |
| 1060 | * On error: -EAGAIN if one was previously granted to this |
| 1061 | * nfs4_client for this nfs4_file. Delegation is not hashed. |
| 1062 | * |
| 1063 | */ |
| 1064 | |
| 1065 | static int |
| 1066 | hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp) |
| 1067 | { |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1068 | struct nfs4_client *clp = dp->dl_stid.sc_client; |
| 1069 | |
| 1070 | lockdep_assert_held(&state_lock); |
| 1071 | lockdep_assert_held(&fp->fi_lock); |
| 1072 | |
J. Bruce Fields | 68b18f5 | 2018-02-19 11:38:33 -0500 | [diff] [blame] | 1073 | if (nfs4_delegation_exists(clp, fp)) |
| 1074 | return -EAGAIN; |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 1075 | refcount_inc(&dp->dl_stid.sc_count); |
Benny Halevy | 3fb87d1 | 2014-05-30 09:09:31 -0400 | [diff] [blame] | 1076 | dp->dl_stid.sc_type = NFS4_DELEG_STID; |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 1077 | list_add(&dp->dl_perfile, &fp->fi_delegations); |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1078 | list_add(&dp->dl_perclnt, &clp->cl_delegations); |
| 1079 | return 0; |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 1080 | } |
| 1081 | |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 1082 | static bool |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 1083 | unhash_delegation_locked(struct nfs4_delegation *dp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1084 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 1085 | struct nfs4_file *fp = dp->dl_stid.sc_file; |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 1086 | |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 1087 | lockdep_assert_held(&state_lock); |
| 1088 | |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 1089 | if (list_empty(&dp->dl_perfile)) |
| 1090 | return false; |
| 1091 | |
Trond Myklebust | b0fc29d | 2014-07-16 10:31:59 -0400 | [diff] [blame] | 1092 | dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID; |
Jeff Layton | d55a166 | 2014-07-22 13:52:06 -0400 | [diff] [blame] | 1093 | /* Ensure that deleg break won't try to requeue it */ |
| 1094 | ++dp->dl_time; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 1095 | spin_lock(&fp->fi_lock); |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 1096 | list_del_init(&dp->dl_perclnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1097 | list_del_init(&dp->dl_recall_lru); |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 1098 | list_del_init(&dp->dl_perfile); |
| 1099 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 1100 | return true; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 1101 | } |
| 1102 | |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 1103 | static void destroy_delegation(struct nfs4_delegation *dp) |
| 1104 | { |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 1105 | bool unhashed; |
| 1106 | |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 1107 | spin_lock(&state_lock); |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 1108 | unhashed = unhash_delegation_locked(dp); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 1109 | spin_unlock(&state_lock); |
J. Bruce Fields | 0af6e69 | 2018-02-21 15:11:03 -0500 | [diff] [blame] | 1110 | if (unhashed) |
| 1111 | destroy_unhashed_deleg(dp); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 1112 | } |
| 1113 | |
| 1114 | static void revoke_delegation(struct nfs4_delegation *dp) |
| 1115 | { |
| 1116 | struct nfs4_client *clp = dp->dl_stid.sc_client; |
| 1117 | |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 1118 | WARN_ON(!list_empty(&dp->dl_recall_lru)); |
| 1119 | |
J. Bruce Fields | 0af6e69 | 2018-02-21 15:11:03 -0500 | [diff] [blame] | 1120 | if (clp->cl_minorversion) { |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 1121 | dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID; |
J. Bruce Fields | 0af6e69 | 2018-02-21 15:11:03 -0500 | [diff] [blame] | 1122 | refcount_inc(&dp->dl_stid.sc_count); |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 1123 | spin_lock(&clp->cl_lock); |
| 1124 | list_add(&dp->dl_recall_lru, &clp->cl_revoked); |
| 1125 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 1126 | } |
J. Bruce Fields | 0af6e69 | 2018-02-21 15:11:03 -0500 | [diff] [blame] | 1127 | destroy_unhashed_deleg(dp); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 1128 | } |
| 1129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1130 | /* |
| 1131 | * SETCLIENTID state |
| 1132 | */ |
| 1133 | |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 1134 | static unsigned int clientid_hashval(u32 id) |
| 1135 | { |
| 1136 | return id & CLIENT_HASH_MASK; |
| 1137 | } |
| 1138 | |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 1139 | static unsigned int clientstr_hashval(struct xdr_netobj name) |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 1140 | { |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 1141 | return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK; |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 1142 | } |
| 1143 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | /* |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1145 | * We store the NONE, READ, WRITE, and BOTH bits separately in the |
| 1146 | * st_{access,deny}_bmap field of the stateid, in order to track not |
| 1147 | * only what share bits are currently in force, but also what |
| 1148 | * combinations of share bits previous opens have used. This allows us |
| 1149 | * to enforce the recommendation of rfc 3530 14.2.19 that the server |
| 1150 | * return an error if the client attempt to downgrade to a combination |
| 1151 | * of share bits not explicable by closing some of its previous opens. |
| 1152 | * |
| 1153 | * XXX: This enforcement is actually incomplete, since we don't keep |
| 1154 | * track of access/deny bit combinations; so, e.g., we allow: |
| 1155 | * |
| 1156 | * OPEN allow read, deny write |
| 1157 | * OPEN allow both, deny none |
| 1158 | * DOWNGRADE allow read, deny none |
| 1159 | * |
| 1160 | * which we should reject. |
| 1161 | */ |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 1162 | static unsigned int |
| 1163 | bmap_to_share_mode(unsigned long bmap) { |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1164 | int i; |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 1165 | unsigned int access = 0; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1166 | |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1167 | for (i = 1; i < 4; i++) { |
| 1168 | if (test_bit(i, &bmap)) |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 1169 | access |= i; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1170 | } |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 1171 | return access; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1172 | } |
| 1173 | |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 1174 | /* set share access for a given stateid */ |
| 1175 | static inline void |
| 1176 | set_access(u32 access, struct nfs4_ol_stateid *stp) |
| 1177 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1178 | unsigned char mask = 1 << access; |
| 1179 | |
| 1180 | WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH); |
| 1181 | stp->st_access_bmap |= mask; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 1182 | } |
| 1183 | |
| 1184 | /* clear share access for a given stateid */ |
| 1185 | static inline void |
| 1186 | clear_access(u32 access, struct nfs4_ol_stateid *stp) |
| 1187 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1188 | unsigned char mask = 1 << access; |
| 1189 | |
| 1190 | WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH); |
| 1191 | stp->st_access_bmap &= ~mask; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | /* test whether a given stateid has access */ |
| 1195 | static inline bool |
| 1196 | test_access(u32 access, struct nfs4_ol_stateid *stp) |
| 1197 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1198 | unsigned char mask = 1 << access; |
| 1199 | |
| 1200 | return (bool)(stp->st_access_bmap & mask); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 1201 | } |
| 1202 | |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 1203 | /* set share deny for a given stateid */ |
| 1204 | static inline void |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1205 | set_deny(u32 deny, struct nfs4_ol_stateid *stp) |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 1206 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1207 | unsigned char mask = 1 << deny; |
| 1208 | |
| 1209 | WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH); |
| 1210 | stp->st_deny_bmap |= mask; |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 1211 | } |
| 1212 | |
| 1213 | /* clear share deny for a given stateid */ |
| 1214 | static inline void |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1215 | clear_deny(u32 deny, struct nfs4_ol_stateid *stp) |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 1216 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1217 | unsigned char mask = 1 << deny; |
| 1218 | |
| 1219 | WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH); |
| 1220 | stp->st_deny_bmap &= ~mask; |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 1221 | } |
| 1222 | |
| 1223 | /* test whether a given stateid is denying specific access */ |
| 1224 | static inline bool |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1225 | test_deny(u32 deny, struct nfs4_ol_stateid *stp) |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 1226 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1227 | unsigned char mask = 1 << deny; |
| 1228 | |
| 1229 | return (bool)(stp->st_deny_bmap & mask); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1230 | } |
| 1231 | |
| 1232 | static int nfs4_access_to_omode(u32 access) |
| 1233 | { |
J. Bruce Fields | 8f34a43 | 2010-09-02 15:23:16 -0400 | [diff] [blame] | 1234 | switch (access & NFS4_SHARE_ACCESS_BOTH) { |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1235 | case NFS4_SHARE_ACCESS_READ: |
| 1236 | return O_RDONLY; |
| 1237 | case NFS4_SHARE_ACCESS_WRITE: |
| 1238 | return O_WRONLY; |
| 1239 | case NFS4_SHARE_ACCESS_BOTH: |
| 1240 | return O_RDWR; |
| 1241 | } |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 1242 | WARN_ON_ONCE(1); |
| 1243 | return O_RDONLY; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1244 | } |
| 1245 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 1246 | /* |
| 1247 | * A stateid that had a deny mode associated with it is being released |
| 1248 | * or downgraded. Recalculate the deny mode on the file. |
| 1249 | */ |
| 1250 | static void |
| 1251 | recalculate_deny_mode(struct nfs4_file *fp) |
| 1252 | { |
| 1253 | struct nfs4_ol_stateid *stp; |
| 1254 | |
| 1255 | spin_lock(&fp->fi_lock); |
| 1256 | fp->fi_share_deny = 0; |
| 1257 | list_for_each_entry(stp, &fp->fi_stateids, st_perfile) |
| 1258 | fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap); |
| 1259 | spin_unlock(&fp->fi_lock); |
| 1260 | } |
| 1261 | |
| 1262 | static void |
| 1263 | reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp) |
| 1264 | { |
| 1265 | int i; |
| 1266 | bool change = false; |
| 1267 | |
| 1268 | for (i = 1; i < 4; i++) { |
| 1269 | if ((i & deny) != i) { |
| 1270 | change = true; |
| 1271 | clear_deny(i, stp); |
| 1272 | } |
| 1273 | } |
| 1274 | |
| 1275 | /* Recalculate per-file deny mode if there was a change */ |
| 1276 | if (change) |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 1277 | recalculate_deny_mode(stp->st_stid.sc_file); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 1278 | } |
| 1279 | |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 1280 | /* release all access and file references for a given stateid */ |
| 1281 | static void |
| 1282 | release_all_access(struct nfs4_ol_stateid *stp) |
| 1283 | { |
| 1284 | int i; |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 1285 | struct nfs4_file *fp = stp->st_stid.sc_file; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 1286 | |
| 1287 | if (fp && stp->st_deny_bmap != 0) |
| 1288 | recalculate_deny_mode(fp); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 1289 | |
| 1290 | for (i = 1; i < 4; i++) { |
| 1291 | if (test_access(i, stp)) |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 1292 | nfs4_file_put_access(stp->st_stid.sc_file, i); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 1293 | clear_access(i, stp); |
| 1294 | } |
| 1295 | } |
| 1296 | |
Kinglong Mee | d50ffde | 2015-07-16 12:05:07 +0800 | [diff] [blame] | 1297 | static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop) |
| 1298 | { |
| 1299 | kfree(sop->so_owner.data); |
| 1300 | sop->so_ops->so_free(sop); |
| 1301 | } |
| 1302 | |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 1303 | static void nfs4_put_stateowner(struct nfs4_stateowner *sop) |
| 1304 | { |
Jeff Layton | a819ecc | 2014-07-29 21:34:38 -0400 | [diff] [blame] | 1305 | struct nfs4_client *clp = sop->so_client; |
| 1306 | |
| 1307 | might_lock(&clp->cl_lock); |
| 1308 | |
| 1309 | if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock)) |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 1310 | return; |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 1311 | sop->so_ops->so_unhash(sop); |
Jeff Layton | a819ecc | 2014-07-29 21:34:38 -0400 | [diff] [blame] | 1312 | spin_unlock(&clp->cl_lock); |
Kinglong Mee | d50ffde | 2015-07-16 12:05:07 +0800 | [diff] [blame] | 1313 | nfs4_free_stateowner(sop); |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 1314 | } |
| 1315 | |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 1316 | static bool |
| 1317 | nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid *stp) |
| 1318 | { |
| 1319 | return list_empty(&stp->st_perfile); |
| 1320 | } |
| 1321 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1322 | static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1323 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 1324 | struct nfs4_file *fp = stp->st_stid.sc_file; |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 1325 | |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 1326 | lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock); |
| 1327 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1328 | if (list_empty(&stp->st_perfile)) |
| 1329 | return false; |
| 1330 | |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 1331 | spin_lock(&fp->fi_lock); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1332 | list_del_init(&stp->st_perfile); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 1333 | spin_unlock(&fp->fi_lock); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1334 | list_del(&stp->st_perstateowner); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1335 | return true; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1336 | } |
| 1337 | |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1338 | static void nfs4_free_ol_stateid(struct nfs4_stid *stid) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1339 | { |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1340 | struct nfs4_ol_stateid *stp = openlockstateid(stid); |
J. Bruce Fields | 4665e2b | 2011-09-06 14:50:49 -0400 | [diff] [blame] | 1341 | |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 1342 | put_clnt_odstate(stp->st_clnt_odstate); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1343 | release_all_access(stp); |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 1344 | if (stp->st_stateowner) |
| 1345 | nfs4_put_stateowner(stp->st_stateowner); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1346 | kmem_cache_free(stateid_slab, stid); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1347 | } |
| 1348 | |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 1349 | static void nfs4_free_lock_stateid(struct nfs4_stid *stid) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1350 | { |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 1351 | struct nfs4_ol_stateid *stp = openlockstateid(stid); |
| 1352 | struct nfs4_lockowner *lo = lockowner(stp->st_stateowner); |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 1353 | struct nfsd_file *nf; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1354 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 1355 | nf = find_any_file(stp->st_stid.sc_file); |
| 1356 | if (nf) { |
| 1357 | get_file(nf->nf_file); |
| 1358 | filp_close(nf->nf_file, (fl_owner_t)lo); |
| 1359 | nfsd_file_put(nf); |
| 1360 | } |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 1361 | nfs4_free_ol_stateid(stid); |
| 1362 | } |
| 1363 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1364 | /* |
| 1365 | * Put the persistent reference to an already unhashed generic stateid, while |
| 1366 | * holding the cl_lock. If it's the last reference, then put it onto the |
| 1367 | * reaplist for later destruction. |
| 1368 | */ |
| 1369 | static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp, |
| 1370 | struct list_head *reaplist) |
| 1371 | { |
| 1372 | struct nfs4_stid *s = &stp->st_stid; |
| 1373 | struct nfs4_client *clp = s->sc_client; |
| 1374 | |
| 1375 | lockdep_assert_held(&clp->cl_lock); |
| 1376 | |
| 1377 | WARN_ON_ONCE(!list_empty(&stp->st_locks)); |
| 1378 | |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 1379 | if (!refcount_dec_and_test(&s->sc_count)) { |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1380 | wake_up_all(&close_wq); |
| 1381 | return; |
| 1382 | } |
| 1383 | |
| 1384 | idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id); |
| 1385 | list_add(&stp->st_locks, reaplist); |
| 1386 | } |
| 1387 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1388 | static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp) |
Jeff Layton | 3c1c995 | 2014-07-29 21:34:39 -0400 | [diff] [blame] | 1389 | { |
Chuck Lever | f46c445 | 2016-10-29 18:19:03 -0400 | [diff] [blame] | 1390 | lockdep_assert_held(&stp->st_stid.sc_client->cl_lock); |
Jeff Layton | 3c1c995 | 2014-07-29 21:34:39 -0400 | [diff] [blame] | 1391 | |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 1392 | if (!unhash_ol_stateid(stp)) |
| 1393 | return false; |
Jeff Layton | 3c1c995 | 2014-07-29 21:34:39 -0400 | [diff] [blame] | 1394 | list_del_init(&stp->st_locks); |
Christoph Hellwig | cd61c52 | 2014-08-14 08:44:57 +0200 | [diff] [blame] | 1395 | nfs4_unhash_stid(&stp->st_stid); |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 1396 | return true; |
Jeff Layton | 3c1c995 | 2014-07-29 21:34:39 -0400 | [diff] [blame] | 1397 | } |
| 1398 | |
Jeff Layton | 5adfd88 | 2014-07-29 21:34:31 -0400 | [diff] [blame] | 1399 | static void release_lock_stateid(struct nfs4_ol_stateid *stp) |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 1400 | { |
Chuck Lever | f46c445 | 2016-10-29 18:19:03 -0400 | [diff] [blame] | 1401 | struct nfs4_client *clp = stp->st_stid.sc_client; |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1402 | bool unhashed; |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 1403 | |
Chuck Lever | f46c445 | 2016-10-29 18:19:03 -0400 | [diff] [blame] | 1404 | spin_lock(&clp->cl_lock); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1405 | unhashed = unhash_lock_stateid(stp); |
Chuck Lever | f46c445 | 2016-10-29 18:19:03 -0400 | [diff] [blame] | 1406 | spin_unlock(&clp->cl_lock); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1407 | if (unhashed) |
| 1408 | nfs4_put_stid(&stp->st_stid); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1409 | } |
| 1410 | |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 1411 | static void unhash_lockowner_locked(struct nfs4_lockowner *lo) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1412 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1413 | struct nfs4_client *clp = lo->lo_owner.so_client; |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 1414 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1415 | lockdep_assert_held(&clp->cl_lock); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 1416 | |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 1417 | list_del_init(&lo->lo_owner.so_strhash); |
| 1418 | } |
| 1419 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1420 | /* |
| 1421 | * Free a list of generic stateids that were collected earlier after being |
| 1422 | * fully unhashed. |
| 1423 | */ |
| 1424 | static void |
| 1425 | free_ol_stateid_reaplist(struct list_head *reaplist) |
| 1426 | { |
| 1427 | struct nfs4_ol_stateid *stp; |
Kinglong Mee | fb94d76 | 2014-08-05 21:20:27 +0800 | [diff] [blame] | 1428 | struct nfs4_file *fp; |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1429 | |
| 1430 | might_sleep(); |
| 1431 | |
| 1432 | while (!list_empty(reaplist)) { |
| 1433 | stp = list_first_entry(reaplist, struct nfs4_ol_stateid, |
| 1434 | st_locks); |
| 1435 | list_del(&stp->st_locks); |
Kinglong Mee | fb94d76 | 2014-08-05 21:20:27 +0800 | [diff] [blame] | 1436 | fp = stp->st_stid.sc_file; |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1437 | stp->st_stid.sc_free(&stp->st_stid); |
Kinglong Mee | fb94d76 | 2014-08-05 21:20:27 +0800 | [diff] [blame] | 1438 | if (fp) |
| 1439 | put_nfs4_file(fp); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1440 | } |
| 1441 | } |
| 1442 | |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 1443 | static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp, |
| 1444 | struct list_head *reaplist) |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 1445 | { |
| 1446 | struct nfs4_ol_stateid *stp; |
| 1447 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1448 | lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock); |
| 1449 | |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 1450 | while (!list_empty(&open_stp->st_locks)) { |
| 1451 | stp = list_entry(open_stp->st_locks.next, |
| 1452 | struct nfs4_ol_stateid, st_locks); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1453 | WARN_ON(!unhash_lock_stateid(stp)); |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 1454 | put_ol_stateid_locked(stp, reaplist); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1455 | } |
| 1456 | } |
| 1457 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1458 | static bool unhash_open_stateid(struct nfs4_ol_stateid *stp, |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 1459 | struct list_head *reaplist) |
J. Bruce Fields | 2283963 | 2009-01-11 14:27:17 -0500 | [diff] [blame] | 1460 | { |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1461 | lockdep_assert_held(&stp->st_stid.sc_client->cl_lock); |
| 1462 | |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 1463 | if (!unhash_ol_stateid(stp)) |
| 1464 | return false; |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 1465 | release_open_stateid_locks(stp, reaplist); |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 1466 | return true; |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 1467 | } |
| 1468 | |
| 1469 | static void release_open_stateid(struct nfs4_ol_stateid *stp) |
| 1470 | { |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1471 | LIST_HEAD(reaplist); |
| 1472 | |
| 1473 | spin_lock(&stp->st_stid.sc_client->cl_lock); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1474 | if (unhash_open_stateid(stp, &reaplist)) |
| 1475 | put_ol_stateid_locked(stp, &reaplist); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1476 | spin_unlock(&stp->st_stid.sc_client->cl_lock); |
| 1477 | free_ol_stateid_reaplist(&reaplist); |
J. Bruce Fields | 2283963 | 2009-01-11 14:27:17 -0500 | [diff] [blame] | 1478 | } |
| 1479 | |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1480 | static void unhash_openowner_locked(struct nfs4_openowner *oo) |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 1481 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1482 | struct nfs4_client *clp = oo->oo_owner.so_client; |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1483 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1484 | lockdep_assert_held(&clp->cl_lock); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1485 | |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 1486 | list_del_init(&oo->oo_owner.so_strhash); |
| 1487 | list_del_init(&oo->oo_perclient); |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 1488 | } |
| 1489 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1490 | static void release_last_closed_stateid(struct nfs4_openowner *oo) |
| 1491 | { |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 1492 | struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net, |
| 1493 | nfsd_net_id); |
| 1494 | struct nfs4_ol_stateid *s; |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1495 | |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 1496 | spin_lock(&nn->client_lock); |
| 1497 | s = oo->oo_last_closed_stid; |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1498 | if (s) { |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 1499 | list_del_init(&oo->oo_close_lru); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1500 | oo->oo_last_closed_stid = NULL; |
| 1501 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 1502 | spin_unlock(&nn->client_lock); |
| 1503 | if (s) |
| 1504 | nfs4_put_stid(&s->st_stid); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1505 | } |
| 1506 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1507 | static void release_openowner(struct nfs4_openowner *oo) |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 1508 | { |
| 1509 | struct nfs4_ol_stateid *stp; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1510 | struct nfs4_client *clp = oo->oo_owner.so_client; |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1511 | struct list_head reaplist; |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1512 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1513 | INIT_LIST_HEAD(&reaplist); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1514 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1515 | spin_lock(&clp->cl_lock); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1516 | unhash_openowner_locked(oo); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1517 | while (!list_empty(&oo->oo_owner.so_stateids)) { |
| 1518 | stp = list_first_entry(&oo->oo_owner.so_stateids, |
| 1519 | struct nfs4_ol_stateid, st_perstateowner); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1520 | if (unhash_open_stateid(stp, &reaplist)) |
| 1521 | put_ol_stateid_locked(stp, &reaplist); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1522 | } |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1523 | spin_unlock(&clp->cl_lock); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1524 | free_ol_stateid_reaplist(&reaplist); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1525 | release_last_closed_stateid(oo); |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 1526 | nfs4_put_stateowner(&oo->oo_owner); |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 1527 | } |
| 1528 | |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1529 | static inline int |
| 1530 | hash_sessionid(struct nfs4_sessionid *sessionid) |
| 1531 | { |
| 1532 | struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid; |
| 1533 | |
| 1534 | return sid->sequence % SESSION_HASH_SIZE; |
| 1535 | } |
| 1536 | |
Mark Salter | 135dd00 | 2015-04-06 09:46:00 -0400 | [diff] [blame] | 1537 | #ifdef CONFIG_SUNRPC_DEBUG |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1538 | static inline void |
| 1539 | dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) |
| 1540 | { |
| 1541 | u32 *ptr = (u32 *)(&sessionid->data[0]); |
| 1542 | dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]); |
| 1543 | } |
Trond Myklebust | 8f199b8 | 2012-03-20 15:11:17 -0400 | [diff] [blame] | 1544 | #else |
| 1545 | static inline void |
| 1546 | dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) |
| 1547 | { |
| 1548 | } |
| 1549 | #endif |
| 1550 | |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 1551 | /* |
| 1552 | * Bump the seqid on cstate->replay_owner, and clear replay_owner if it |
| 1553 | * won't be used for replay. |
| 1554 | */ |
| 1555 | void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr) |
| 1556 | { |
| 1557 | struct nfs4_stateowner *so = cstate->replay_owner; |
| 1558 | |
| 1559 | if (nfserr == nfserr_replay_me) |
| 1560 | return; |
| 1561 | |
| 1562 | if (!seqid_mutating_err(ntohl(nfserr))) { |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 1563 | nfsd4_cstate_clear_replay(cstate); |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 1564 | return; |
| 1565 | } |
| 1566 | if (!so) |
| 1567 | return; |
| 1568 | if (so->so_is_open_owner) |
| 1569 | release_last_closed_stateid(openowner(so)); |
| 1570 | so->so_seqid++; |
| 1571 | return; |
| 1572 | } |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1573 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1574 | static void |
| 1575 | gen_sessionid(struct nfsd4_session *ses) |
| 1576 | { |
| 1577 | struct nfs4_client *clp = ses->se_client; |
| 1578 | struct nfsd4_sessionid *sid; |
| 1579 | |
| 1580 | sid = (struct nfsd4_sessionid *)ses->se_sessionid.data; |
| 1581 | sid->clientid = clp->cl_clientid; |
| 1582 | sid->sequence = current_sessionid++; |
| 1583 | sid->reserved = 0; |
| 1584 | } |
| 1585 | |
| 1586 | /* |
Andy Adamson | a649637c7 | 2009-08-28 08:45:01 -0400 | [diff] [blame] | 1587 | * The protocol defines ca_maxresponssize_cached to include the size of |
| 1588 | * the rpc header, but all we need to cache is the data starting after |
| 1589 | * the end of the initial SEQUENCE operation--the rest we regenerate |
| 1590 | * each time. Therefore we can advertise a ca_maxresponssize_cached |
| 1591 | * value that is the number of bytes in our cache plus a few additional |
| 1592 | * bytes. In order to stay on the safe side, and not promise more than |
| 1593 | * we can cache, those additional bytes must be the minimum possible: 24 |
| 1594 | * bytes of rpc header (xid through accept state, with AUTH_NULL |
| 1595 | * verifier), 12 for the compound header (with zero-length tag), and 44 |
| 1596 | * for the SEQUENCE op response: |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1597 | */ |
Andy Adamson | a649637c7 | 2009-08-28 08:45:01 -0400 | [diff] [blame] | 1598 | #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44) |
| 1599 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1600 | static void |
| 1601 | free_session_slots(struct nfsd4_session *ses) |
| 1602 | { |
| 1603 | int i; |
| 1604 | |
J. Bruce Fields | 53da6a5 | 2017-10-17 20:38:49 -0400 | [diff] [blame] | 1605 | for (i = 0; i < ses->se_fchannel.maxreqs; i++) { |
| 1606 | free_svc_cred(&ses->se_slots[i]->sl_cred); |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1607 | kfree(ses->se_slots[i]); |
J. Bruce Fields | 53da6a5 | 2017-10-17 20:38:49 -0400 | [diff] [blame] | 1608 | } |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1609 | } |
| 1610 | |
J. Bruce Fields | efe0cb6 | 2009-10-24 20:52:16 -0400 | [diff] [blame] | 1611 | /* |
| 1612 | * We don't actually need to cache the rpc and session headers, so we |
| 1613 | * can allocate a little less for each slot: |
| 1614 | */ |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1615 | static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca) |
J. Bruce Fields | efe0cb6 | 2009-10-24 20:52:16 -0400 | [diff] [blame] | 1616 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1617 | u32 size; |
J. Bruce Fields | efe0cb6 | 2009-10-24 20:52:16 -0400 | [diff] [blame] | 1618 | |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1619 | if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ) |
| 1620 | size = 0; |
| 1621 | else |
| 1622 | size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ; |
| 1623 | return size + sizeof(struct nfsd4_slot); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1624 | } |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1625 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1626 | /* |
| 1627 | * XXX: If we run out of reserved DRC memory we could (up to a point) |
| 1628 | * re-negotiate active sessions and reduce their slot usage to make |
Justin P. Mattock | 42b2aa8 | 2011-11-28 20:31:00 -0800 | [diff] [blame] | 1629 | * 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] | 1630 | */ |
NeilBrown | 2030ca5 | 2019-09-20 16:36:45 +1000 | [diff] [blame] | 1631 | 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] | 1632 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1633 | u32 slotsize = slot_bytes(ca); |
| 1634 | u32 num = ca->maxreqs; |
J. Bruce Fields | c54f24e | 2019-02-21 10:47:00 -0500 | [diff] [blame] | 1635 | unsigned long avail, total_avail; |
NeilBrown | 2030ca5 | 2019-09-20 16:36:45 +1000 | [diff] [blame] | 1636 | unsigned int scale_factor; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1637 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1638 | spin_lock(&nfsd_drc_lock); |
NeilBrown | 7f49fd5d | 2019-09-20 16:33:16 +1000 | [diff] [blame] | 1639 | if (nfsd_drc_max_mem > nfsd_drc_mem_used) |
| 1640 | total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used; |
| 1641 | else |
| 1642 | /* We have handed out more space than we chose in |
| 1643 | * set_max_drc() to allow. That isn't really a |
| 1644 | * problem as long as that doesn't make us think we |
| 1645 | * have lots more due to integer overflow. |
| 1646 | */ |
| 1647 | total_avail = 0; |
J. Bruce Fields | c54f24e | 2019-02-21 10:47:00 -0500 | [diff] [blame] | 1648 | avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail); |
J. Bruce Fields | de766e5 | 2017-09-19 19:25:41 -0400 | [diff] [blame] | 1649 | /* |
NeilBrown | 2030ca5 | 2019-09-20 16:36:45 +1000 | [diff] [blame] | 1650 | * Never use more than a fraction of the remaining memory, |
NeilBrown | 7f49fd5d | 2019-09-20 16:33:16 +1000 | [diff] [blame] | 1651 | * unless it's the only way to give this client a slot. |
NeilBrown | 2030ca5 | 2019-09-20 16:36:45 +1000 | [diff] [blame] | 1652 | * The chosen fraction is either 1/8 or 1/number of threads, |
| 1653 | * whichever is smaller. This ensures there are adequate |
| 1654 | * slots to support multiple clients per thread. |
NeilBrown | 7f49fd5d | 2019-09-20 16:33:16 +1000 | [diff] [blame] | 1655 | * Give the client one slot even if that would require |
| 1656 | * over-allocation--it is better than failure. |
J. Bruce Fields | de766e5 | 2017-09-19 19:25:41 -0400 | [diff] [blame] | 1657 | */ |
NeilBrown | 2030ca5 | 2019-09-20 16:36:45 +1000 | [diff] [blame] | 1658 | scale_factor = max_t(unsigned int, 8, nn->nfsd_serv->sv_nrthreads); |
| 1659 | |
| 1660 | avail = clamp_t(unsigned long, avail, slotsize, |
| 1661 | total_avail/scale_factor); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1662 | num = min_t(int, num, avail / slotsize); |
NeilBrown | 7f49fd5d | 2019-09-20 16:33:16 +1000 | [diff] [blame] | 1663 | num = max_t(int, num, 1); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1664 | nfsd_drc_mem_used += num * slotsize; |
| 1665 | spin_unlock(&nfsd_drc_lock); |
| 1666 | |
| 1667 | return num; |
| 1668 | } |
| 1669 | |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1670 | static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1671 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1672 | int slotsize = slot_bytes(ca); |
| 1673 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1674 | spin_lock(&nfsd_drc_lock); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1675 | nfsd_drc_mem_used -= slotsize * ca->maxreqs; |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1676 | spin_unlock(&nfsd_drc_lock); |
| 1677 | } |
| 1678 | |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1679 | static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs, |
| 1680 | struct nfsd4_channel_attrs *battrs) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1681 | { |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1682 | int numslots = fattrs->maxreqs; |
| 1683 | int slotsize = slot_bytes(fattrs); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1684 | struct nfsd4_session *new; |
| 1685 | int mem, i; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1686 | |
J. Bruce Fields | c23753d | 2010-09-27 16:22:30 -0400 | [diff] [blame] | 1687 | 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] | 1688 | + sizeof(struct nfsd4_session) > PAGE_SIZE); |
| 1689 | mem = numslots * sizeof(struct nfsd4_slot *); |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1690 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1691 | new = kzalloc(sizeof(*new) + mem, GFP_KERNEL); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1692 | if (!new) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1693 | return NULL; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1694 | /* allocate each struct nfsd4_slot and data cache in one piece */ |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1695 | for (i = 0; i < numslots; i++) { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1696 | new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1697 | if (!new->se_slots[i]) |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1698 | goto out_free; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1699 | } |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1700 | |
| 1701 | memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs)); |
| 1702 | memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs)); |
| 1703 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1704 | return new; |
| 1705 | out_free: |
| 1706 | while (i--) |
| 1707 | kfree(new->se_slots[i]); |
| 1708 | kfree(new); |
| 1709 | return NULL; |
| 1710 | } |
| 1711 | |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1712 | static void free_conn(struct nfsd4_conn *c) |
| 1713 | { |
| 1714 | svc_xprt_put(c->cn_xprt); |
| 1715 | kfree(c); |
| 1716 | } |
| 1717 | |
| 1718 | static void nfsd4_conn_lost(struct svc_xpt_user *u) |
| 1719 | { |
| 1720 | struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user); |
| 1721 | struct nfs4_client *clp = c->cn_session->se_client; |
| 1722 | |
| 1723 | spin_lock(&clp->cl_lock); |
| 1724 | if (!list_empty(&c->cn_persession)) { |
| 1725 | list_del(&c->cn_persession); |
| 1726 | free_conn(c); |
| 1727 | } |
J. Bruce Fields | eea4980 | 2010-11-18 08:34:12 -0500 | [diff] [blame] | 1728 | nfsd4_probe_callback(clp); |
J. Bruce Fields | 2e4b723 | 2013-03-08 09:30:43 -0500 | [diff] [blame] | 1729 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1730 | } |
| 1731 | |
J. Bruce Fields | d29c374 | 2010-06-15 17:34:11 -0400 | [diff] [blame] | 1732 | 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] | 1733 | { |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1734 | struct nfsd4_conn *conn; |
| 1735 | |
| 1736 | conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL); |
| 1737 | if (!conn) |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1738 | return NULL; |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1739 | svc_xprt_get(rqstp->rq_xprt); |
| 1740 | conn->cn_xprt = rqstp->rq_xprt; |
J. Bruce Fields | d29c374 | 2010-06-15 17:34:11 -0400 | [diff] [blame] | 1741 | conn->cn_flags = flags; |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1742 | INIT_LIST_HEAD(&conn->cn_xpt_user.list); |
| 1743 | return conn; |
| 1744 | } |
| 1745 | |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 1746 | static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) |
| 1747 | { |
| 1748 | conn->cn_session = ses; |
| 1749 | list_add(&conn->cn_persession, &ses->se_conns); |
| 1750 | } |
| 1751 | |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1752 | static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) |
| 1753 | { |
| 1754 | struct nfs4_client *clp = ses->se_client; |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1755 | |
| 1756 | spin_lock(&clp->cl_lock); |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 1757 | __nfsd4_hash_conn(conn, ses); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1758 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1759 | } |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1760 | |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1761 | static int nfsd4_register_conn(struct nfsd4_conn *conn) |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1762 | { |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1763 | conn->cn_xpt_user.callback = nfsd4_conn_lost; |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1764 | return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user); |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1765 | } |
| 1766 | |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1767 | 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] | 1768 | { |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1769 | int ret; |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1770 | |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1771 | nfsd4_hash_conn(conn, ses); |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1772 | ret = nfsd4_register_conn(conn); |
| 1773 | if (ret) |
| 1774 | /* oops; xprt is already down: */ |
| 1775 | nfsd4_conn_lost(&conn->cn_xpt_user); |
J. Bruce Fields | 57a3714 | 2014-07-18 15:06:47 -0400 | [diff] [blame] | 1776 | /* We may have gained or lost a callback channel: */ |
| 1777 | nfsd4_probe_callback_sync(ses->se_client); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1778 | } |
| 1779 | |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1780 | 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] | 1781 | { |
| 1782 | u32 dir = NFS4_CDFC4_FORE; |
| 1783 | |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1784 | if (cses->flags & SESSION4_BACK_CHAN) |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 1785 | dir |= NFS4_CDFC4_BACK; |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1786 | return alloc_conn(rqstp, dir); |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 1787 | } |
| 1788 | |
| 1789 | /* must be called under client_lock */ |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1790 | static void nfsd4_del_conns(struct nfsd4_session *s) |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1791 | { |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1792 | struct nfs4_client *clp = s->se_client; |
| 1793 | struct nfsd4_conn *c; |
| 1794 | |
| 1795 | spin_lock(&clp->cl_lock); |
| 1796 | while (!list_empty(&s->se_conns)) { |
| 1797 | c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession); |
| 1798 | list_del_init(&c->cn_persession); |
| 1799 | spin_unlock(&clp->cl_lock); |
| 1800 | |
| 1801 | unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user); |
| 1802 | free_conn(c); |
| 1803 | |
| 1804 | spin_lock(&clp->cl_lock); |
| 1805 | } |
| 1806 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1807 | } |
| 1808 | |
J. Bruce Fields | 1377b69 | 2012-09-11 21:42:40 -0400 | [diff] [blame] | 1809 | static void __free_session(struct nfsd4_session *ses) |
| 1810 | { |
J. Bruce Fields | 1377b69 | 2012-09-11 21:42:40 -0400 | [diff] [blame] | 1811 | free_session_slots(ses); |
| 1812 | kfree(ses); |
| 1813 | } |
| 1814 | |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 1815 | static void free_session(struct nfsd4_session *ses) |
Benny Halevy | 508dc6e | 2012-02-23 17:40:52 -0800 | [diff] [blame] | 1816 | { |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1817 | nfsd4_del_conns(ses); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1818 | nfsd4_put_drc_mem(&ses->se_fchannel); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1819 | __free_session(ses); |
J. Bruce Fields | a827bcb | 2012-09-12 09:51:34 -0400 | [diff] [blame] | 1820 | } |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1821 | |
Fengguang Wu | 135ae82 | 2012-11-10 07:20:25 -0500 | [diff] [blame] | 1822 | 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] | 1823 | { |
J. Bruce Fields | a827bcb | 2012-09-12 09:51:34 -0400 | [diff] [blame] | 1824 | int idx; |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1825 | 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] | 1826 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1827 | new->se_client = clp; |
| 1828 | gen_sessionid(new); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1829 | |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1830 | INIT_LIST_HEAD(&new->se_conns); |
| 1831 | |
J. Bruce Fields | ac7c46f2 | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 1832 | new->se_cb_seq_nr = 1; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1833 | new->se_flags = cses->flags; |
J. Bruce Fields | 8b5ce5c | 2010-10-19 17:31:50 -0400 | [diff] [blame] | 1834 | new->se_cb_prog = cses->callback_prog; |
J. Bruce Fields | c6bb3ca | 2012-11-01 16:31:02 -0400 | [diff] [blame] | 1835 | new->se_cb_sec = cses->cb_sec; |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 1836 | atomic_set(&new->se_ref, 0); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1837 | idx = hash_sessionid(&new->se_sessionid); |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1838 | list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1839 | spin_lock(&clp->cl_lock); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1840 | list_add(&new->se_perclnt, &clp->cl_sessions); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1841 | spin_unlock(&clp->cl_lock); |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1842 | |
Chuck Lever | b0d2e42 | 2014-08-22 15:10:59 -0400 | [diff] [blame] | 1843 | { |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1844 | struct sockaddr *sa = svc_addr(rqstp); |
J. Bruce Fields | dcbeaa6 | 2010-06-15 17:25:45 -0400 | [diff] [blame] | 1845 | /* |
| 1846 | * This is a little silly; with sessions there's no real |
| 1847 | * use for the callback address. Use the peer address |
| 1848 | * as a reasonable default for now, but consider fixing |
| 1849 | * the rpc client not to require an address in the |
| 1850 | * future: |
| 1851 | */ |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1852 | rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa); |
| 1853 | clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa); |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1854 | } |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1855 | } |
| 1856 | |
Benny Halevy | 9089f1b | 2010-05-12 00:12:26 +0300 | [diff] [blame] | 1857 | /* caller must hold client_lock */ |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1858 | static struct nfsd4_session * |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 1859 | __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net) |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1860 | { |
| 1861 | struct nfsd4_session *elem; |
| 1862 | int idx; |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1863 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1864 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 1865 | lockdep_assert_held(&nn->client_lock); |
| 1866 | |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1867 | dump_sessionid(__func__, sessionid); |
| 1868 | idx = hash_sessionid(sessionid); |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1869 | /* Search in the appropriate list */ |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1870 | list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) { |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1871 | if (!memcmp(elem->se_sessionid.data, sessionid->data, |
| 1872 | NFS4_MAX_SESSIONID_LEN)) { |
| 1873 | return elem; |
| 1874 | } |
| 1875 | } |
| 1876 | |
| 1877 | dprintk("%s: session not found\n", __func__); |
| 1878 | return NULL; |
| 1879 | } |
| 1880 | |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 1881 | static struct nfsd4_session * |
| 1882 | find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net, |
| 1883 | __be32 *ret) |
| 1884 | { |
| 1885 | struct nfsd4_session *session; |
| 1886 | __be32 status = nfserr_badsession; |
| 1887 | |
| 1888 | session = __find_in_sessionid_hashtbl(sessionid, net); |
| 1889 | if (!session) |
| 1890 | goto out; |
| 1891 | status = nfsd4_get_session_locked(session); |
| 1892 | if (status) |
| 1893 | session = NULL; |
| 1894 | out: |
| 1895 | *ret = status; |
| 1896 | return session; |
| 1897 | } |
| 1898 | |
Benny Halevy | 9089f1b | 2010-05-12 00:12:26 +0300 | [diff] [blame] | 1899 | /* caller must hold client_lock */ |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1900 | static void |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1901 | unhash_session(struct nfsd4_session *ses) |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1902 | { |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 1903 | struct nfs4_client *clp = ses->se_client; |
| 1904 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 1905 | |
| 1906 | lockdep_assert_held(&nn->client_lock); |
| 1907 | |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1908 | list_del(&ses->se_hash); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1909 | spin_lock(&ses->se_client->cl_lock); |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1910 | list_del(&ses->se_perclnt); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1911 | spin_unlock(&ses->se_client->cl_lock); |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1912 | } |
| 1913 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1914 | /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */ |
| 1915 | static int |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 1916 | STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | { |
J. Bruce Fields | bbc7f33 | 2015-01-20 11:51:26 -0500 | [diff] [blame] | 1918 | /* |
| 1919 | * We're assuming the clid was not given out from a boot |
| 1920 | * precisely 2^32 (about 136 years) before this one. That seems |
| 1921 | * a safe assumption: |
| 1922 | */ |
| 1923 | if (clid->cl_boot == (u32)nn->boot_time) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1924 | return 0; |
Arnd Bergmann | 9cc7680 | 2019-10-31 15:53:13 +0100 | [diff] [blame] | 1925 | dprintk("NFSD stale clientid (%08x/%08x) boot_time %08llx\n", |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 1926 | clid->cl_boot, clid->cl_id, nn->boot_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | return 1; |
| 1928 | } |
| 1929 | |
| 1930 | /* |
| 1931 | * XXX Should we use a slab cache ? |
| 1932 | * This type of memory management is somewhat inefficient, but we use it |
| 1933 | * anyway since SETCLIENTID is not a common operation. |
| 1934 | */ |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1935 | static struct nfs4_client *alloc_client(struct xdr_netobj name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1936 | { |
| 1937 | struct nfs4_client *clp; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1938 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | |
Jeff Layton | 9258a2d | 2018-03-16 09:47:22 -0400 | [diff] [blame] | 1940 | clp = kmem_cache_zalloc(client_slab, GFP_KERNEL); |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1941 | if (clp == NULL) |
| 1942 | return NULL; |
J. Bruce Fields | 6f4859b | 2019-06-19 14:30:33 -0400 | [diff] [blame] | 1943 | xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1944 | if (clp->cl_name.data == NULL) |
| 1945 | goto err_no_name; |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 1946 | clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE, |
| 1947 | sizeof(struct list_head), |
| 1948 | GFP_KERNEL); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1949 | if (!clp->cl_ownerstr_hashtbl) |
| 1950 | goto err_no_hashtbl; |
| 1951 | for (i = 0; i < OWNER_HASH_SIZE; i++) |
| 1952 | INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]); |
Trond Myklebust | 5694c93 | 2014-04-18 14:43:56 -0400 | [diff] [blame] | 1953 | INIT_LIST_HEAD(&clp->cl_sessions); |
| 1954 | idr_init(&clp->cl_stateids); |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 1955 | atomic_set(&clp->cl_rpc_users, 0); |
Trond Myklebust | 5694c93 | 2014-04-18 14:43:56 -0400 | [diff] [blame] | 1956 | clp->cl_cb_state = NFSD4_CB_UNKNOWN; |
| 1957 | INIT_LIST_HEAD(&clp->cl_idhash); |
| 1958 | INIT_LIST_HEAD(&clp->cl_openowners); |
| 1959 | INIT_LIST_HEAD(&clp->cl_delegations); |
| 1960 | INIT_LIST_HEAD(&clp->cl_lru); |
Trond Myklebust | 5694c93 | 2014-04-18 14:43:56 -0400 | [diff] [blame] | 1961 | INIT_LIST_HEAD(&clp->cl_revoked); |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 1962 | #ifdef CONFIG_NFSD_PNFS |
| 1963 | INIT_LIST_HEAD(&clp->cl_lo_states); |
| 1964 | #endif |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 1965 | INIT_LIST_HEAD(&clp->async_copies); |
| 1966 | spin_lock_init(&clp->async_lock); |
Trond Myklebust | 5694c93 | 2014-04-18 14:43:56 -0400 | [diff] [blame] | 1967 | spin_lock_init(&clp->cl_lock); |
| 1968 | rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1969 | return clp; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1970 | err_no_hashtbl: |
| 1971 | kfree(clp->cl_name.data); |
| 1972 | err_no_name: |
Jeff Layton | 9258a2d | 2018-03-16 09:47:22 -0400 | [diff] [blame] | 1973 | kmem_cache_free(client_slab, clp); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1974 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1975 | } |
| 1976 | |
J. Bruce Fields | 59f8e91 | 2019-03-20 20:03:02 -0400 | [diff] [blame] | 1977 | static void __free_client(struct kref *k) |
| 1978 | { |
J. Bruce Fields | e8a79fb | 2019-03-22 11:11:06 -0400 | [diff] [blame] | 1979 | struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref); |
| 1980 | 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] | 1981 | |
| 1982 | free_svc_cred(&clp->cl_cred); |
| 1983 | kfree(clp->cl_ownerstr_hashtbl); |
| 1984 | kfree(clp->cl_name.data); |
J. Bruce Fields | 7912344 | 2019-06-05 12:42:05 -0400 | [diff] [blame] | 1985 | kfree(clp->cl_nii_domain.data); |
| 1986 | kfree(clp->cl_nii_name.data); |
J. Bruce Fields | 59f8e91 | 2019-03-20 20:03:02 -0400 | [diff] [blame] | 1987 | idr_destroy(&clp->cl_stateids); |
J. Bruce Fields | 59f8e91 | 2019-03-20 20:03:02 -0400 | [diff] [blame] | 1988 | kmem_cache_free(client_slab, clp); |
| 1989 | } |
| 1990 | |
YueHaibing | 297e57a | 2019-07-08 15:29:33 +0800 | [diff] [blame] | 1991 | static void drop_client(struct nfs4_client *clp) |
J. Bruce Fields | 59f8e91 | 2019-03-20 20:03:02 -0400 | [diff] [blame] | 1992 | { |
J. Bruce Fields | e8a79fb | 2019-03-22 11:11:06 -0400 | [diff] [blame] | 1993 | kref_put(&clp->cl_nfsdfs.cl_ref, __free_client); |
J. Bruce Fields | 59f8e91 | 2019-03-20 20:03:02 -0400 | [diff] [blame] | 1994 | } |
| 1995 | |
Trond Myklebust | 4dd86e15 | 2014-04-18 14:43:58 -0400 | [diff] [blame] | 1996 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1997 | free_client(struct nfs4_client *clp) |
| 1998 | { |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 1999 | while (!list_empty(&clp->cl_sessions)) { |
| 2000 | struct nfsd4_session *ses; |
| 2001 | ses = list_entry(clp->cl_sessions.next, struct nfsd4_session, |
| 2002 | se_perclnt); |
| 2003 | list_del(&ses->se_perclnt); |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2004 | WARN_ON_ONCE(atomic_read(&ses->se_ref)); |
| 2005 | free_session(ses); |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 2006 | } |
Trond Myklebust | 4cb57e3 | 2014-04-18 14:43:57 -0400 | [diff] [blame] | 2007 | rpc_destroy_wait_queue(&clp->cl_cb_waitq); |
J. Bruce Fields | 89c905b | 2019-06-19 12:43:11 -0400 | [diff] [blame] | 2008 | if (clp->cl_nfsd_dentry) { |
J. Bruce Fields | e8a79fb | 2019-03-22 11:11:06 -0400 | [diff] [blame] | 2009 | nfsd_client_rmdir(clp->cl_nfsd_dentry); |
J. Bruce Fields | 89c905b | 2019-06-19 12:43:11 -0400 | [diff] [blame] | 2010 | clp->cl_nfsd_dentry = NULL; |
| 2011 | wake_up_all(&expiry_wq); |
| 2012 | } |
J. Bruce Fields | 59f8e91 | 2019-03-20 20:03:02 -0400 | [diff] [blame] | 2013 | drop_client(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2014 | } |
| 2015 | |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 2016 | /* must be called under the client_lock */ |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 2017 | static void |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 2018 | unhash_client_locked(struct nfs4_client *clp) |
| 2019 | { |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 2020 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 2021 | struct nfsd4_session *ses; |
| 2022 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2023 | lockdep_assert_held(&nn->client_lock); |
| 2024 | |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 2025 | /* Mark the client as expired! */ |
| 2026 | clp->cl_time = 0; |
| 2027 | /* Make it invisible */ |
| 2028 | if (!list_empty(&clp->cl_idhash)) { |
| 2029 | list_del_init(&clp->cl_idhash); |
| 2030 | if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags)) |
| 2031 | rb_erase(&clp->cl_namenode, &nn->conf_name_tree); |
| 2032 | else |
| 2033 | rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); |
| 2034 | } |
| 2035 | list_del_init(&clp->cl_lru); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 2036 | spin_lock(&clp->cl_lock); |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 2037 | list_for_each_entry(ses, &clp->cl_sessions, se_perclnt) |
| 2038 | list_del_init(&ses->se_hash); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 2039 | spin_unlock(&clp->cl_lock); |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 2040 | } |
| 2041 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2042 | static void |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 2043 | unhash_client(struct nfs4_client *clp) |
| 2044 | { |
| 2045 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 2046 | |
| 2047 | spin_lock(&nn->client_lock); |
| 2048 | unhash_client_locked(clp); |
| 2049 | spin_unlock(&nn->client_lock); |
| 2050 | } |
| 2051 | |
Jeff Layton | 97403d9 | 2014-07-30 08:27:12 -0400 | [diff] [blame] | 2052 | static __be32 mark_client_expired_locked(struct nfs4_client *clp) |
| 2053 | { |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 2054 | if (atomic_read(&clp->cl_rpc_users)) |
Jeff Layton | 97403d9 | 2014-07-30 08:27:12 -0400 | [diff] [blame] | 2055 | return nfserr_jukebox; |
| 2056 | unhash_client_locked(clp); |
| 2057 | return nfs_ok; |
| 2058 | } |
| 2059 | |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 2060 | static void |
| 2061 | __destroy_client(struct nfs4_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2062 | { |
Jeff Layton | 68ef3bc | 2018-03-16 11:32:02 -0400 | [diff] [blame] | 2063 | int i; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2064 | struct nfs4_openowner *oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2065 | struct nfs4_delegation *dp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | struct list_head reaplist; |
| 2067 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2068 | INIT_LIST_HEAD(&reaplist); |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 2069 | spin_lock(&state_lock); |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 2070 | while (!list_empty(&clp->cl_delegations)) { |
| 2071 | dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt); |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 2072 | WARN_ON(!unhash_delegation_locked(dp)); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 2073 | list_add(&dp->dl_recall_lru, &reaplist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2074 | } |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 2075 | spin_unlock(&state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2076 | while (!list_empty(&reaplist)) { |
| 2077 | dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 2078 | list_del_init(&dp->dl_recall_lru); |
J. Bruce Fields | 0af6e69 | 2018-02-21 15:11:03 -0500 | [diff] [blame] | 2079 | destroy_unhashed_deleg(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2080 | } |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 2081 | while (!list_empty(&clp->cl_revoked)) { |
Andrew Elble | c876486 | 2015-02-25 17:46:27 -0500 | [diff] [blame] | 2082 | 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] | 2083 | list_del_init(&dp->dl_recall_lru); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 2084 | nfs4_put_stid(&dp->dl_stid); |
Benny Halevy | 956c4fe | 2013-10-29 11:39:12 +0200 | [diff] [blame] | 2085 | } |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 2086 | while (!list_empty(&clp->cl_openowners)) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2087 | oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient); |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 2088 | nfs4_get_stateowner(&oo->oo_owner); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2089 | release_openowner(oo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2090 | } |
Jeff Layton | 68ef3bc | 2018-03-16 11:32:02 -0400 | [diff] [blame] | 2091 | for (i = 0; i < OWNER_HASH_SIZE; i++) { |
| 2092 | struct nfs4_stateowner *so, *tmp; |
| 2093 | |
| 2094 | list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i], |
| 2095 | so_strhash) { |
| 2096 | /* Should be no openowners at this point */ |
| 2097 | WARN_ON_ONCE(so->so_is_open_owner); |
| 2098 | remove_blocked_locks(lockowner(so)); |
| 2099 | } |
| 2100 | } |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 2101 | nfsd4_return_all_client_layouts(clp); |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 2102 | nfsd4_shutdown_copy(clp); |
J. Bruce Fields | 6ff8da0 | 2010-06-04 20:04:45 -0400 | [diff] [blame] | 2103 | nfsd4_shutdown_callback(clp); |
J. Bruce Fields | 2bf2387 | 2010-03-08 12:37:27 -0500 | [diff] [blame] | 2104 | if (clp->cl_cb_conn.cb_xprt) |
| 2105 | svc_xprt_put(clp->cl_cb_conn.cb_xprt); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2106 | free_client(clp); |
J. Bruce Fields | 89c905b | 2019-06-19 12:43:11 -0400 | [diff] [blame] | 2107 | wake_up_all(&expiry_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2108 | } |
| 2109 | |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 2110 | static void |
| 2111 | destroy_client(struct nfs4_client *clp) |
| 2112 | { |
| 2113 | unhash_client(clp); |
| 2114 | __destroy_client(clp); |
| 2115 | } |
| 2116 | |
Scott Mayhew | 362063a | 2019-03-26 18:06:28 -0400 | [diff] [blame] | 2117 | static void inc_reclaim_complete(struct nfs4_client *clp) |
| 2118 | { |
| 2119 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 2120 | |
| 2121 | if (!nn->track_reclaim_completes) |
| 2122 | return; |
| 2123 | if (!nfsd4_find_reclaim_client(clp->cl_name, nn)) |
| 2124 | return; |
| 2125 | if (atomic_inc_return(&nn->nr_reclaim_complete) == |
| 2126 | nn->reclaim_str_hashtbl_size) { |
| 2127 | printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n", |
| 2128 | clp->net->ns.inum); |
| 2129 | nfsd4_end_grace(nn); |
| 2130 | } |
| 2131 | } |
| 2132 | |
J. Bruce Fields | 0d22f68 | 2012-09-26 11:36:16 -0400 | [diff] [blame] | 2133 | static void expire_client(struct nfs4_client *clp) |
| 2134 | { |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 2135 | unhash_client(clp); |
J. Bruce Fields | 0d22f68 | 2012-09-26 11:36:16 -0400 | [diff] [blame] | 2136 | nfsd4_client_record_remove(clp); |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 2137 | __destroy_client(clp); |
J. Bruce Fields | 0d22f68 | 2012-09-26 11:36:16 -0400 | [diff] [blame] | 2138 | } |
| 2139 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 2140 | static void copy_verf(struct nfs4_client *target, nfs4_verifier *source) |
| 2141 | { |
| 2142 | memcpy(target->cl_verifier.data, source->data, |
| 2143 | sizeof(target->cl_verifier.data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2144 | } |
| 2145 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 2146 | static void copy_clid(struct nfs4_client *target, struct nfs4_client *source) |
| 2147 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | target->cl_clientid.cl_boot = source->cl_clientid.cl_boot; |
| 2149 | target->cl_clientid.cl_id = source->cl_clientid.cl_id; |
| 2150 | } |
| 2151 | |
J. Bruce Fields | 5004385 | 2015-11-20 15:58:37 -0500 | [diff] [blame] | 2152 | static int copy_cred(struct svc_cred *target, struct svc_cred *source) |
| 2153 | { |
NeilBrown | 2f10fdc | 2017-03-23 16:57:36 +0800 | [diff] [blame] | 2154 | target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL); |
| 2155 | target->cr_raw_principal = kstrdup(source->cr_raw_principal, |
| 2156 | GFP_KERNEL); |
Chuck Lever | 9abdda5 | 2018-08-16 12:05:59 -0400 | [diff] [blame] | 2157 | target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL); |
| 2158 | if ((source->cr_principal && !target->cr_principal) || |
| 2159 | (source->cr_raw_principal && !target->cr_raw_principal) || |
| 2160 | (source->cr_targ_princ && !target->cr_targ_princ)) |
NeilBrown | 2f10fdc | 2017-03-23 16:57:36 +0800 | [diff] [blame] | 2161 | return -ENOMEM; |
J. Bruce Fields | 5004385 | 2015-11-20 15:58:37 -0500 | [diff] [blame] | 2162 | |
J. Bruce Fields | d5497fc | 2012-05-14 22:06:49 -0400 | [diff] [blame] | 2163 | target->cr_flavor = source->cr_flavor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2164 | target->cr_uid = source->cr_uid; |
| 2165 | target->cr_gid = source->cr_gid; |
| 2166 | target->cr_group_info = source->cr_group_info; |
| 2167 | get_group_info(target->cr_group_info); |
J. Bruce Fields | 0dc1531 | 2013-05-14 16:07:13 -0400 | [diff] [blame] | 2168 | target->cr_gss_mech = source->cr_gss_mech; |
| 2169 | if (source->cr_gss_mech) |
| 2170 | gss_mech_get(source->cr_gss_mech); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 2171 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2172 | } |
| 2173 | |
Rasmus Villemoes | ef17af2 | 2014-12-05 16:40:07 +0100 | [diff] [blame] | 2174 | static int |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2175 | compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2) |
| 2176 | { |
Rasmus Villemoes | ef17af2 | 2014-12-05 16:40:07 +0100 | [diff] [blame] | 2177 | if (o1->len < o2->len) |
| 2178 | return -1; |
| 2179 | if (o1->len > o2->len) |
| 2180 | return 1; |
| 2181 | return memcmp(o1->data, o2->data, o1->len); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2182 | } |
| 2183 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2184 | static int |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 2185 | same_verf(nfs4_verifier *v1, nfs4_verifier *v2) |
| 2186 | { |
| 2187 | return 0 == memcmp(v1->data, v2->data, sizeof(v1->data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2188 | } |
| 2189 | |
| 2190 | static int |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 2191 | same_clid(clientid_t *cl1, clientid_t *cl2) |
| 2192 | { |
| 2193 | 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] | 2194 | } |
| 2195 | |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 2196 | static bool groups_equal(struct group_info *g1, struct group_info *g2) |
| 2197 | { |
| 2198 | int i; |
| 2199 | |
| 2200 | if (g1->ngroups != g2->ngroups) |
| 2201 | return false; |
| 2202 | for (i=0; i<g1->ngroups; i++) |
Alexey Dobriyan | 81243ea | 2016-10-07 17:03:12 -0700 | [diff] [blame] | 2203 | if (!gid_eq(g1->gid[i], g2->gid[i])) |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 2204 | return false; |
| 2205 | return true; |
| 2206 | } |
| 2207 | |
J. Bruce Fields | 68eb350 | 2012-08-21 12:48:30 -0400 | [diff] [blame] | 2208 | /* |
| 2209 | * RFC 3530 language requires clid_inuse be returned when the |
| 2210 | * "principal" associated with a requests differs from that previously |
| 2211 | * used. We use uid, gid's, and gss principal string as our best |
| 2212 | * approximation. We also don't want to allow non-gss use of a client |
| 2213 | * established using gss: in theory cr_principal should catch that |
| 2214 | * change, but in practice cr_principal can be null even in the gss case |
| 2215 | * since gssd doesn't always pass down a principal string. |
| 2216 | */ |
| 2217 | static bool is_gss_cred(struct svc_cred *cr) |
| 2218 | { |
| 2219 | /* Is cr_flavor one of the gss "pseudoflavors"?: */ |
| 2220 | return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR); |
| 2221 | } |
| 2222 | |
| 2223 | |
Vivek Trivedi | 5559b50 | 2012-07-24 21:18:20 +0530 | [diff] [blame] | 2224 | static bool |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 2225 | same_creds(struct svc_cred *cr1, struct svc_cred *cr2) |
| 2226 | { |
J. Bruce Fields | 68eb350 | 2012-08-21 12:48:30 -0400 | [diff] [blame] | 2227 | if ((is_gss_cred(cr1) != is_gss_cred(cr2)) |
Eric W. Biederman | 6fab877 | 2013-02-02 06:53:11 -0800 | [diff] [blame] | 2228 | || (!uid_eq(cr1->cr_uid, cr2->cr_uid)) |
| 2229 | || (!gid_eq(cr1->cr_gid, cr2->cr_gid)) |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 2230 | || !groups_equal(cr1->cr_group_info, cr2->cr_group_info)) |
| 2231 | return false; |
Chuck Lever | 9abdda5 | 2018-08-16 12:05:59 -0400 | [diff] [blame] | 2232 | /* XXX: check that cr_targ_princ fields match ? */ |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 2233 | if (cr1->cr_principal == cr2->cr_principal) |
| 2234 | return true; |
| 2235 | if (!cr1->cr_principal || !cr2->cr_principal) |
| 2236 | return false; |
Vivek Trivedi | 5559b50 | 2012-07-24 21:18:20 +0530 | [diff] [blame] | 2237 | return 0 == strcmp(cr1->cr_principal, cr2->cr_principal); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2238 | } |
| 2239 | |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2240 | static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp) |
| 2241 | { |
| 2242 | struct svc_cred *cr = &rqstp->rq_cred; |
| 2243 | u32 service; |
| 2244 | |
J. Bruce Fields | c472059 | 2013-08-07 11:41:49 -0400 | [diff] [blame] | 2245 | if (!cr->cr_gss_mech) |
| 2246 | return false; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2247 | service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor); |
| 2248 | return service == RPC_GSS_SVC_INTEGRITY || |
| 2249 | service == RPC_GSS_SVC_PRIVACY; |
| 2250 | } |
| 2251 | |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 2252 | 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] | 2253 | { |
| 2254 | struct svc_cred *cr = &rqstp->rq_cred; |
| 2255 | |
| 2256 | if (!cl->cl_mach_cred) |
| 2257 | return true; |
| 2258 | if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech) |
| 2259 | return false; |
| 2260 | if (!svc_rqst_integrity_protected(rqstp)) |
| 2261 | return false; |
J. Bruce Fields | 414ca01 | 2015-11-20 10:48:02 -0500 | [diff] [blame] | 2262 | if (cl->cl_cred.cr_raw_principal) |
| 2263 | return 0 == strcmp(cl->cl_cred.cr_raw_principal, |
| 2264 | cr->cr_raw_principal); |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2265 | if (!cr->cr_principal) |
| 2266 | return false; |
| 2267 | return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal); |
| 2268 | } |
| 2269 | |
Jeff Layton | 294ac32 | 2014-07-30 08:27:15 -0400 | [diff] [blame] | 2270 | 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] | 2271 | { |
Chuck Lever | ab4684d | 2012-03-02 17:13:50 -0500 | [diff] [blame] | 2272 | __be32 verf[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2273 | |
Jeff Layton | f419992 | 2014-06-17 07:44:11 -0400 | [diff] [blame] | 2274 | /* |
| 2275 | * This is opaque to client, so no need to byte-swap. Use |
| 2276 | * __force to keep sparse happy |
| 2277 | */ |
Arnd Bergmann | 9104ae4 | 2019-11-04 16:45:30 +0100 | [diff] [blame] | 2278 | verf[0] = (__force __be32)(u32)ktime_get_real_seconds(); |
Kinglong Mee | 19311aa8 | 2015-07-18 07:33:31 +0800 | [diff] [blame] | 2279 | verf[1] = (__force __be32)nn->clverifier_counter++; |
Chuck Lever | ab4684d | 2012-03-02 17:13:50 -0500 | [diff] [blame] | 2280 | memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2281 | } |
| 2282 | |
Jeff Layton | 294ac32 | 2014-07-30 08:27:15 -0400 | [diff] [blame] | 2283 | static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn) |
| 2284 | { |
Arnd Bergmann | 9cc7680 | 2019-10-31 15:53:13 +0100 | [diff] [blame] | 2285 | clp->cl_clientid.cl_boot = (u32)nn->boot_time; |
Jeff Layton | 294ac32 | 2014-07-30 08:27:15 -0400 | [diff] [blame] | 2286 | clp->cl_clientid.cl_id = nn->clientid_counter++; |
| 2287 | gen_confirm(clp, nn); |
| 2288 | } |
| 2289 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 2290 | static struct nfs4_stid * |
| 2291 | find_stateid_locked(struct nfs4_client *cl, stateid_t *t) |
J. Bruce Fields | 4581d14 | 2011-09-06 14:56:09 -0400 | [diff] [blame] | 2292 | { |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 2293 | struct nfs4_stid *ret; |
| 2294 | |
| 2295 | ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id); |
| 2296 | if (!ret || !ret->sc_type) |
| 2297 | return NULL; |
| 2298 | return ret; |
J. Bruce Fields | 4d71ab8 | 2011-09-06 16:48:57 -0400 | [diff] [blame] | 2299 | } |
| 2300 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 2301 | static struct nfs4_stid * |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 2302 | 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] | 2303 | { |
| 2304 | struct nfs4_stid *s; |
| 2305 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 2306 | spin_lock(&cl->cl_lock); |
| 2307 | s = find_stateid_locked(cl, t); |
Trond Myklebust | 2d3f966 | 2014-07-29 21:34:25 -0400 | [diff] [blame] | 2308 | if (s != NULL) { |
| 2309 | if (typemask & s->sc_type) |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 2310 | refcount_inc(&s->sc_count); |
Trond Myklebust | 2d3f966 | 2014-07-29 21:34:25 -0400 | [diff] [blame] | 2311 | else |
| 2312 | s = NULL; |
| 2313 | } |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 2314 | spin_unlock(&cl->cl_lock); |
| 2315 | return s; |
J. Bruce Fields | 4581d14 | 2011-09-06 14:56:09 -0400 | [diff] [blame] | 2316 | } |
| 2317 | |
J. Bruce Fields | a204f25 | 2019-06-19 12:54:45 -0400 | [diff] [blame] | 2318 | static struct nfs4_client *get_nfsdfs_clp(struct inode *inode) |
| 2319 | { |
| 2320 | struct nfsdfs_client *nc; |
| 2321 | nc = get_nfsdfs_client(inode); |
| 2322 | if (!nc) |
| 2323 | return NULL; |
| 2324 | return container_of(nc, struct nfs4_client, cl_nfsdfs); |
| 2325 | } |
| 2326 | |
J. Bruce Fields | 169319f | 2019-06-19 12:39:46 -0400 | [diff] [blame] | 2327 | static void seq_quote_mem(struct seq_file *m, char *data, int len) |
| 2328 | { |
| 2329 | seq_printf(m, "\""); |
| 2330 | seq_escape_mem_ascii(m, data, len); |
| 2331 | seq_printf(m, "\""); |
| 2332 | } |
| 2333 | |
J. Bruce Fields | 97ad403 | 2019-04-09 15:56:57 -0400 | [diff] [blame] | 2334 | static int client_info_show(struct seq_file *m, void *v) |
| 2335 | { |
| 2336 | struct inode *inode = m->private; |
J. Bruce Fields | 97ad403 | 2019-04-09 15:56:57 -0400 | [diff] [blame] | 2337 | struct nfs4_client *clp; |
| 2338 | u64 clid; |
| 2339 | |
J. Bruce Fields | a204f25 | 2019-06-19 12:54:45 -0400 | [diff] [blame] | 2340 | clp = get_nfsdfs_clp(inode); |
| 2341 | if (!clp) |
J. Bruce Fields | 97ad403 | 2019-04-09 15:56:57 -0400 | [diff] [blame] | 2342 | return -ENXIO; |
J. Bruce Fields | 97ad403 | 2019-04-09 15:56:57 -0400 | [diff] [blame] | 2343 | memcpy(&clid, &clp->cl_clientid, sizeof(clid)); |
| 2344 | seq_printf(m, "clientid: 0x%llx\n", clid); |
J. Bruce Fields | 169319f | 2019-06-19 12:39:46 -0400 | [diff] [blame] | 2345 | seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr); |
| 2346 | seq_printf(m, "name: "); |
| 2347 | seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len); |
| 2348 | seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion); |
J. Bruce Fields | 7912344 | 2019-06-05 12:42:05 -0400 | [diff] [blame] | 2349 | if (clp->cl_nii_domain.data) { |
| 2350 | seq_printf(m, "Implementation domain: "); |
| 2351 | seq_quote_mem(m, clp->cl_nii_domain.data, |
| 2352 | clp->cl_nii_domain.len); |
| 2353 | seq_printf(m, "\nImplementation name: "); |
| 2354 | 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] | 2355 | seq_printf(m, "\nImplementation time: [%lld, %ld]\n", |
J. Bruce Fields | 7912344 | 2019-06-05 12:42:05 -0400 | [diff] [blame] | 2356 | clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec); |
| 2357 | } |
J. Bruce Fields | 97ad403 | 2019-04-09 15:56:57 -0400 | [diff] [blame] | 2358 | drop_client(clp); |
| 2359 | |
| 2360 | return 0; |
| 2361 | } |
| 2362 | |
| 2363 | static int client_info_open(struct inode *inode, struct file *file) |
| 2364 | { |
| 2365 | return single_open(file, client_info_show, inode); |
| 2366 | } |
| 2367 | |
| 2368 | static const struct file_operations client_info_fops = { |
| 2369 | .open = client_info_open, |
| 2370 | .read = seq_read, |
| 2371 | .llseek = seq_lseek, |
| 2372 | .release = single_release, |
| 2373 | }; |
| 2374 | |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2375 | static void *states_start(struct seq_file *s, loff_t *pos) |
| 2376 | __acquires(&clp->cl_lock) |
| 2377 | { |
| 2378 | struct nfs4_client *clp = s->private; |
| 2379 | unsigned long id = *pos; |
| 2380 | void *ret; |
| 2381 | |
| 2382 | spin_lock(&clp->cl_lock); |
| 2383 | ret = idr_get_next_ul(&clp->cl_stateids, &id); |
| 2384 | *pos = id; |
| 2385 | return ret; |
| 2386 | } |
| 2387 | |
| 2388 | static void *states_next(struct seq_file *s, void *v, loff_t *pos) |
| 2389 | { |
| 2390 | struct nfs4_client *clp = s->private; |
| 2391 | unsigned long id = *pos; |
| 2392 | void *ret; |
| 2393 | |
| 2394 | id = *pos; |
| 2395 | id++; |
| 2396 | ret = idr_get_next_ul(&clp->cl_stateids, &id); |
| 2397 | *pos = id; |
| 2398 | return ret; |
| 2399 | } |
| 2400 | |
| 2401 | static void states_stop(struct seq_file *s, void *v) |
| 2402 | __releases(&clp->cl_lock) |
| 2403 | { |
| 2404 | struct nfs4_client *clp = s->private; |
| 2405 | |
| 2406 | spin_unlock(&clp->cl_lock); |
| 2407 | } |
| 2408 | |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 2409 | 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] | 2410 | { |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 2411 | struct inode *inode = f->nf_inode; |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2412 | |
| 2413 | seq_printf(s, "superblock: \"%02x:%02x:%ld\"", |
| 2414 | MAJOR(inode->i_sb->s_dev), |
| 2415 | MINOR(inode->i_sb->s_dev), |
| 2416 | inode->i_ino); |
| 2417 | } |
| 2418 | |
| 2419 | static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo) |
| 2420 | { |
| 2421 | seq_printf(s, "owner: "); |
| 2422 | seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len); |
| 2423 | } |
| 2424 | |
| 2425 | static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st) |
| 2426 | { |
| 2427 | struct nfs4_ol_stateid *ols; |
| 2428 | struct nfs4_file *nf; |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 2429 | struct nfsd_file *file; |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2430 | struct nfs4_stateowner *oo; |
| 2431 | unsigned int access, deny; |
| 2432 | |
| 2433 | if (st->sc_type != NFS4_OPEN_STID && st->sc_type != NFS4_LOCK_STID) |
| 2434 | return 0; /* XXX: or SEQ_SKIP? */ |
| 2435 | ols = openlockstateid(st); |
| 2436 | oo = ols->st_stateowner; |
| 2437 | nf = st->sc_file; |
| 2438 | file = find_any_file(nf); |
| 2439 | |
| 2440 | seq_printf(s, "- 0x%16phN: { type: open, ", &st->sc_stateid); |
| 2441 | |
| 2442 | access = bmap_to_share_mode(ols->st_access_bmap); |
| 2443 | deny = bmap_to_share_mode(ols->st_deny_bmap); |
| 2444 | |
J. Bruce Fields | c4b77ed | 2019-10-03 12:33:08 -0400 | [diff] [blame] | 2445 | seq_printf(s, "access: %s%s, ", |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2446 | access & NFS4_SHARE_ACCESS_READ ? "r" : "-", |
| 2447 | access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-"); |
J. Bruce Fields | c4b77ed | 2019-10-03 12:33:08 -0400 | [diff] [blame] | 2448 | seq_printf(s, "deny: %s%s, ", |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2449 | deny & NFS4_SHARE_ACCESS_READ ? "r" : "-", |
| 2450 | deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-"); |
| 2451 | |
| 2452 | nfs4_show_superblock(s, file); |
| 2453 | seq_printf(s, ", "); |
| 2454 | nfs4_show_owner(s, oo); |
| 2455 | seq_printf(s, " }\n"); |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 2456 | nfsd_file_put(file); |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2457 | |
| 2458 | return 0; |
| 2459 | } |
| 2460 | |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2461 | static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st) |
| 2462 | { |
| 2463 | struct nfs4_ol_stateid *ols; |
| 2464 | struct nfs4_file *nf; |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 2465 | struct nfsd_file *file; |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2466 | struct nfs4_stateowner *oo; |
| 2467 | |
| 2468 | ols = openlockstateid(st); |
| 2469 | oo = ols->st_stateowner; |
| 2470 | nf = st->sc_file; |
| 2471 | file = find_any_file(nf); |
| 2472 | |
| 2473 | seq_printf(s, "- 0x%16phN: { type: lock, ", &st->sc_stateid); |
| 2474 | |
| 2475 | /* |
| 2476 | * Note: a lock stateid isn't really the same thing as a lock, |
| 2477 | * it's the locking state held by one owner on a file, and there |
| 2478 | * may be multiple (or no) lock ranges associated with it. |
| 2479 | * (Same for the matter is true of open stateids.) |
| 2480 | */ |
| 2481 | |
| 2482 | nfs4_show_superblock(s, file); |
| 2483 | /* XXX: open stateid? */ |
| 2484 | seq_printf(s, ", "); |
| 2485 | nfs4_show_owner(s, oo); |
| 2486 | seq_printf(s, " }\n"); |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 2487 | nfsd_file_put(file); |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2488 | |
| 2489 | return 0; |
| 2490 | } |
| 2491 | |
| 2492 | static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st) |
| 2493 | { |
| 2494 | struct nfs4_delegation *ds; |
| 2495 | struct nfs4_file *nf; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 2496 | struct nfsd_file *file; |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2497 | |
| 2498 | ds = delegstateid(st); |
| 2499 | nf = st->sc_file; |
| 2500 | file = nf->fi_deleg_file; |
| 2501 | |
| 2502 | seq_printf(s, "- 0x%16phN: { type: deleg, ", &st->sc_stateid); |
| 2503 | |
| 2504 | /* Kinda dead code as long as we only support read delegs: */ |
| 2505 | seq_printf(s, "access: %s, ", |
| 2506 | ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w"); |
| 2507 | |
| 2508 | /* XXX: lease time, whether it's being recalled. */ |
| 2509 | |
| 2510 | nfs4_show_superblock(s, file); |
| 2511 | seq_printf(s, " }\n"); |
| 2512 | |
| 2513 | return 0; |
| 2514 | } |
| 2515 | |
J. Bruce Fields | 0c4b62b | 2019-05-14 15:52:57 -0400 | [diff] [blame] | 2516 | static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st) |
| 2517 | { |
| 2518 | struct nfs4_layout_stateid *ls; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 2519 | struct nfsd_file *file; |
J. Bruce Fields | 0c4b62b | 2019-05-14 15:52:57 -0400 | [diff] [blame] | 2520 | |
| 2521 | ls = container_of(st, struct nfs4_layout_stateid, ls_stid); |
| 2522 | file = ls->ls_file; |
| 2523 | |
| 2524 | seq_printf(s, "- 0x%16phN: { type: layout, ", &st->sc_stateid); |
| 2525 | |
| 2526 | /* XXX: What else would be useful? */ |
| 2527 | |
| 2528 | nfs4_show_superblock(s, file); |
| 2529 | seq_printf(s, " }\n"); |
| 2530 | |
| 2531 | return 0; |
| 2532 | } |
| 2533 | |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2534 | static int states_show(struct seq_file *s, void *v) |
| 2535 | { |
| 2536 | struct nfs4_stid *st = v; |
| 2537 | |
| 2538 | switch (st->sc_type) { |
| 2539 | case NFS4_OPEN_STID: |
| 2540 | return nfs4_show_open(s, st); |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2541 | case NFS4_LOCK_STID: |
| 2542 | return nfs4_show_lock(s, st); |
| 2543 | case NFS4_DELEG_STID: |
| 2544 | return nfs4_show_deleg(s, st); |
J. Bruce Fields | 0c4b62b | 2019-05-14 15:52:57 -0400 | [diff] [blame] | 2545 | case NFS4_LAYOUT_STID: |
| 2546 | return nfs4_show_layout(s, st); |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2547 | default: |
| 2548 | return 0; /* XXX: or SEQ_SKIP? */ |
| 2549 | } |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2550 | /* XXX: copy stateids? */ |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2551 | } |
| 2552 | |
| 2553 | static struct seq_operations states_seq_ops = { |
| 2554 | .start = states_start, |
| 2555 | .next = states_next, |
| 2556 | .stop = states_stop, |
| 2557 | .show = states_show |
| 2558 | }; |
| 2559 | |
| 2560 | static int client_states_open(struct inode *inode, struct file *file) |
| 2561 | { |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2562 | struct seq_file *s; |
| 2563 | struct nfs4_client *clp; |
| 2564 | int ret; |
| 2565 | |
J. Bruce Fields | a204f25 | 2019-06-19 12:54:45 -0400 | [diff] [blame] | 2566 | clp = get_nfsdfs_clp(inode); |
| 2567 | if (!clp) |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2568 | return -ENXIO; |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2569 | |
| 2570 | ret = seq_open(file, &states_seq_ops); |
| 2571 | if (ret) |
| 2572 | return ret; |
| 2573 | s = file->private_data; |
| 2574 | s->private = clp; |
| 2575 | return 0; |
| 2576 | } |
| 2577 | |
| 2578 | static int client_opens_release(struct inode *inode, struct file *file) |
| 2579 | { |
| 2580 | struct seq_file *m = file->private_data; |
| 2581 | struct nfs4_client *clp = m->private; |
| 2582 | |
| 2583 | /* XXX: alternatively, we could get/drop in seq start/stop */ |
| 2584 | drop_client(clp); |
| 2585 | return 0; |
| 2586 | } |
| 2587 | |
| 2588 | static const struct file_operations client_states_fops = { |
| 2589 | .open = client_states_open, |
| 2590 | .read = seq_read, |
| 2591 | .llseek = seq_lseek, |
| 2592 | .release = client_opens_release, |
| 2593 | }; |
| 2594 | |
J. Bruce Fields | 89c905b | 2019-06-19 12:43:11 -0400 | [diff] [blame] | 2595 | /* |
| 2596 | * Normally we refuse to destroy clients that are in use, but here the |
| 2597 | * administrator is telling us to just do it. We also want to wait |
| 2598 | * so the caller has a guarantee that the client's locks are gone by |
| 2599 | * the time the write returns: |
| 2600 | */ |
YueHaibing | 297e57a | 2019-07-08 15:29:33 +0800 | [diff] [blame] | 2601 | static void force_expire_client(struct nfs4_client *clp) |
J. Bruce Fields | 89c905b | 2019-06-19 12:43:11 -0400 | [diff] [blame] | 2602 | { |
| 2603 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 2604 | bool already_expired; |
| 2605 | |
| 2606 | spin_lock(&clp->cl_lock); |
| 2607 | clp->cl_time = 0; |
| 2608 | spin_unlock(&clp->cl_lock); |
| 2609 | |
| 2610 | wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0); |
| 2611 | spin_lock(&nn->client_lock); |
| 2612 | already_expired = list_empty(&clp->cl_lru); |
| 2613 | if (!already_expired) |
| 2614 | unhash_client_locked(clp); |
| 2615 | spin_unlock(&nn->client_lock); |
| 2616 | |
| 2617 | if (!already_expired) |
| 2618 | expire_client(clp); |
| 2619 | else |
| 2620 | wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL); |
| 2621 | } |
| 2622 | |
| 2623 | static ssize_t client_ctl_write(struct file *file, const char __user *buf, |
| 2624 | size_t size, loff_t *pos) |
| 2625 | { |
| 2626 | char *data; |
| 2627 | struct nfs4_client *clp; |
| 2628 | |
| 2629 | data = simple_transaction_get(file, buf, size); |
| 2630 | if (IS_ERR(data)) |
| 2631 | return PTR_ERR(data); |
| 2632 | if (size != 7 || 0 != memcmp(data, "expire\n", 7)) |
| 2633 | return -EINVAL; |
| 2634 | clp = get_nfsdfs_clp(file_inode(file)); |
| 2635 | if (!clp) |
| 2636 | return -ENXIO; |
| 2637 | force_expire_client(clp); |
| 2638 | drop_client(clp); |
| 2639 | return 7; |
| 2640 | } |
| 2641 | |
| 2642 | static const struct file_operations client_ctl_fops = { |
| 2643 | .write = client_ctl_write, |
| 2644 | .release = simple_transaction_release, |
| 2645 | }; |
| 2646 | |
J. Bruce Fields | 97ad403 | 2019-04-09 15:56:57 -0400 | [diff] [blame] | 2647 | static const struct tree_descr client_files[] = { |
| 2648 | [0] = {"info", &client_info_fops, S_IRUSR}, |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2649 | [1] = {"states", &client_states_fops, S_IRUSR}, |
Petr Vorel | 6cbfad5 | 2020-03-13 13:39:57 +0100 | [diff] [blame] | 2650 | [2] = {"ctl", &client_ctl_fops, S_IWUSR}, |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2651 | [3] = {""}, |
J. Bruce Fields | 97ad403 | 2019-04-09 15:56:57 -0400 | [diff] [blame] | 2652 | }; |
| 2653 | |
Jeff Layton | 2216d44 | 2012-11-12 15:00:57 -0500 | [diff] [blame] | 2654 | static struct nfs4_client *create_client(struct xdr_netobj name, |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2655 | struct svc_rqst *rqstp, nfs4_verifier *verf) |
| 2656 | { |
| 2657 | struct nfs4_client *clp; |
| 2658 | struct sockaddr *sa = svc_addr(rqstp); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 2659 | int ret; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2660 | struct net *net = SVC_NET(rqstp); |
J. Bruce Fields | e8a79fb | 2019-03-22 11:11:06 -0400 | [diff] [blame] | 2661 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2662 | |
| 2663 | clp = alloc_client(name); |
| 2664 | if (clp == NULL) |
| 2665 | return NULL; |
| 2666 | |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 2667 | ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred); |
| 2668 | if (ret) { |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 2669 | free_client(clp); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 2670 | return NULL; |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2671 | } |
J. Bruce Fields | e8a79fb | 2019-03-22 11:11:06 -0400 | [diff] [blame] | 2672 | gen_clid(clp, nn); |
| 2673 | kref_init(&clp->cl_nfsdfs.cl_ref); |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 2674 | 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] | 2675 | clp->cl_time = ktime_get_boottime_seconds(); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2676 | clear_bit(0, &clp->cl_cb_slot_busy); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2677 | copy_verf(clp, verf); |
J. Bruce Fields | 3bade24 | 2019-05-14 21:38:11 -0400 | [diff] [blame] | 2678 | memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage)); |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 2679 | clp->cl_cb_session = NULL; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2680 | clp->net = net; |
J. Bruce Fields | e8a79fb | 2019-03-22 11:11:06 -0400 | [diff] [blame] | 2681 | clp->cl_nfsd_dentry = nfsd_client_mkdir(nn, &clp->cl_nfsdfs, |
J. Bruce Fields | 97ad403 | 2019-04-09 15:56:57 -0400 | [diff] [blame] | 2682 | clp->cl_clientid.cl_id - nn->clientid_base, |
| 2683 | client_files); |
J. Bruce Fields | e8a79fb | 2019-03-22 11:11:06 -0400 | [diff] [blame] | 2684 | if (!clp->cl_nfsd_dentry) { |
| 2685 | free_client(clp); |
| 2686 | return NULL; |
| 2687 | } |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2688 | return clp; |
| 2689 | } |
| 2690 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 2691 | static void |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2692 | add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root) |
| 2693 | { |
| 2694 | struct rb_node **new = &(root->rb_node), *parent = NULL; |
| 2695 | struct nfs4_client *clp; |
| 2696 | |
| 2697 | while (*new) { |
| 2698 | clp = rb_entry(*new, struct nfs4_client, cl_namenode); |
| 2699 | parent = *new; |
| 2700 | |
| 2701 | if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0) |
| 2702 | new = &((*new)->rb_left); |
| 2703 | else |
| 2704 | new = &((*new)->rb_right); |
| 2705 | } |
| 2706 | |
| 2707 | rb_link_node(&new_clp->cl_namenode, parent, new); |
| 2708 | rb_insert_color(&new_clp->cl_namenode, root); |
| 2709 | } |
| 2710 | |
| 2711 | static struct nfs4_client * |
| 2712 | find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root) |
| 2713 | { |
Rasmus Villemoes | ef17af2 | 2014-12-05 16:40:07 +0100 | [diff] [blame] | 2714 | int cmp; |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2715 | struct rb_node *node = root->rb_node; |
| 2716 | struct nfs4_client *clp; |
| 2717 | |
| 2718 | while (node) { |
| 2719 | clp = rb_entry(node, struct nfs4_client, cl_namenode); |
| 2720 | cmp = compare_blob(&clp->cl_name, name); |
| 2721 | if (cmp > 0) |
| 2722 | node = node->rb_left; |
| 2723 | else if (cmp < 0) |
| 2724 | node = node->rb_right; |
| 2725 | else |
| 2726 | return clp; |
| 2727 | } |
| 2728 | return NULL; |
| 2729 | } |
| 2730 | |
| 2731 | static void |
| 2732 | add_to_unconfirmed(struct nfs4_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2733 | { |
| 2734 | unsigned int idhashval; |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 2735 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2736 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2737 | lockdep_assert_held(&nn->client_lock); |
| 2738 | |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2739 | clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2740 | add_clp_to_name_tree(clp, &nn->unconf_name_tree); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2741 | idhashval = clientid_hashval(clp->cl_clientid.cl_id); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 2742 | list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]); |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2743 | renew_client_locked(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2744 | } |
| 2745 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 2746 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2747 | move_to_confirmed(struct nfs4_client *clp) |
| 2748 | { |
| 2749 | unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2750 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2751 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2752 | lockdep_assert_held(&nn->client_lock); |
| 2753 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2754 | dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2755 | list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]); |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2756 | rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2757 | add_clp_to_name_tree(clp, &nn->conf_name_tree); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2758 | set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2759 | renew_client_locked(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2760 | } |
| 2761 | |
| 2762 | static struct nfs4_client * |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2763 | 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] | 2764 | { |
| 2765 | struct nfs4_client *clp; |
| 2766 | unsigned int idhashval = clientid_hashval(clid->cl_id); |
| 2767 | |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2768 | list_for_each_entry(clp, &tbl[idhashval], cl_idhash) { |
J. Bruce Fields | a50d2ad | 2011-10-12 16:24:27 -0400 | [diff] [blame] | 2769 | if (same_clid(&clp->cl_clientid, clid)) { |
J. Bruce Fields | d15c077 | 2012-09-13 16:19:31 -0400 | [diff] [blame] | 2770 | if ((bool)clp->cl_minorversion != sessions) |
| 2771 | return NULL; |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2772 | renew_client_locked(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2773 | return clp; |
J. Bruce Fields | a50d2ad | 2011-10-12 16:24:27 -0400 | [diff] [blame] | 2774 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2775 | } |
| 2776 | return NULL; |
| 2777 | } |
| 2778 | |
| 2779 | static struct nfs4_client * |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2780 | find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) |
| 2781 | { |
| 2782 | struct list_head *tbl = nn->conf_id_hashtbl; |
| 2783 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2784 | lockdep_assert_held(&nn->client_lock); |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2785 | return find_client_in_id_table(tbl, clid, sessions); |
| 2786 | } |
| 2787 | |
| 2788 | static struct nfs4_client * |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 2789 | find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2790 | { |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2791 | struct list_head *tbl = nn->unconf_id_hashtbl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2792 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2793 | lockdep_assert_held(&nn->client_lock); |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2794 | return find_client_in_id_table(tbl, clid, sessions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2795 | } |
| 2796 | |
J. Bruce Fields | 6e5f15c | 2010-11-24 17:17:34 -0500 | [diff] [blame] | 2797 | static bool clp_used_exchangeid(struct nfs4_client *clp) |
Andy Adamson | a1bcecd | 2009-04-03 08:28:05 +0300 | [diff] [blame] | 2798 | { |
J. Bruce Fields | 6e5f15c | 2010-11-24 17:17:34 -0500 | [diff] [blame] | 2799 | return clp->cl_exchange_flags != 0; |
J. Bruce Fields | e203d50 | 2010-11-24 17:30:54 -0500 | [diff] [blame] | 2800 | } |
Andy Adamson | a1bcecd | 2009-04-03 08:28:05 +0300 | [diff] [blame] | 2801 | |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2802 | static struct nfs4_client * |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2803 | find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2804 | { |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2805 | lockdep_assert_held(&nn->client_lock); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2806 | return find_clp_in_name_tree(name, &nn->conf_name_tree); |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2807 | } |
| 2808 | |
| 2809 | static struct nfs4_client * |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2810 | find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2811 | { |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2812 | lockdep_assert_held(&nn->client_lock); |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2813 | return find_clp_in_name_tree(name, &nn->unconf_name_tree); |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2814 | } |
| 2815 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 2816 | static void |
Takuma Umeya | 6f3d772 | 2010-12-15 14:09:01 +0900 | [diff] [blame] | 2817 | 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] | 2818 | { |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2819 | struct nfs4_cb_conn *conn = &clp->cl_cb_conn; |
Takuma Umeya | 6f3d772 | 2010-12-15 14:09:01 +0900 | [diff] [blame] | 2820 | struct sockaddr *sa = svc_addr(rqstp); |
| 2821 | u32 scopeid = rpc_get_scope_id(sa); |
Jeff Layton | 7077ecb | 2009-08-14 12:57:58 -0400 | [diff] [blame] | 2822 | unsigned short expected_family; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2823 | |
Jeff Layton | 7077ecb | 2009-08-14 12:57:58 -0400 | [diff] [blame] | 2824 | /* Currently, we only support tcp and tcp6 for the callback channel */ |
| 2825 | if (se->se_callback_netid_len == 3 && |
| 2826 | !memcmp(se->se_callback_netid_val, "tcp", 3)) |
| 2827 | expected_family = AF_INET; |
| 2828 | else if (se->se_callback_netid_len == 4 && |
| 2829 | !memcmp(se->se_callback_netid_val, "tcp6", 4)) |
| 2830 | expected_family = AF_INET6; |
| 2831 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2832 | goto out_err; |
| 2833 | |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2834 | conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val, |
Jeff Layton | aa9a4ec | 2009-08-14 12:57:57 -0400 | [diff] [blame] | 2835 | se->se_callback_addr_len, |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2836 | (struct sockaddr *)&conn->cb_addr, |
| 2837 | sizeof(conn->cb_addr)); |
Jeff Layton | aa9a4ec | 2009-08-14 12:57:57 -0400 | [diff] [blame] | 2838 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2839 | if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2840 | goto out_err; |
Jeff Layton | aa9a4ec | 2009-08-14 12:57:57 -0400 | [diff] [blame] | 2841 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2842 | if (conn->cb_addr.ss_family == AF_INET6) |
| 2843 | ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid; |
Jeff Layton | fbf4665 | 2009-08-14 12:57:59 -0400 | [diff] [blame] | 2844 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2845 | conn->cb_prog = se->se_callback_prog; |
| 2846 | conn->cb_ident = se->se_callback_ident; |
Mi Jinlong | 849a1cf | 2011-08-30 17:18:41 +0800 | [diff] [blame] | 2847 | memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2848 | return; |
| 2849 | out_err: |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2850 | conn->cb_addr.ss_family = AF_UNSPEC; |
| 2851 | conn->cb_addrlen = 0; |
Rasmus Villemoes | 4ab495b | 2017-02-24 01:15:55 +0100 | [diff] [blame] | 2852 | dprintk("NFSD: this client (clientid %08x/%08x) " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2853 | "will not receive delegations\n", |
| 2854 | clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id); |
| 2855 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2856 | return; |
| 2857 | } |
| 2858 | |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2859 | /* |
J. Bruce Fields | 067e1ac | 2014-03-21 17:26:44 -0400 | [diff] [blame] | 2860 | * Cache a reply. nfsd4_check_resp_size() has bounded the cache size. |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2861 | */ |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 2862 | static void |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2863 | nfsd4_store_cache_entry(struct nfsd4_compoundres *resp) |
| 2864 | { |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 2865 | struct xdr_buf *buf = resp->xdr.buf; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2866 | struct nfsd4_slot *slot = resp->cstate.slot; |
| 2867 | unsigned int base; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2868 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2869 | dprintk("--> %s slot %p\n", __func__, slot); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2870 | |
J. Bruce Fields | 085def3 | 2017-10-18 16:17:18 -0400 | [diff] [blame] | 2871 | slot->sl_flags |= NFSD4_SLOT_INITIALIZED; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2872 | slot->sl_opcnt = resp->opcnt; |
| 2873 | slot->sl_status = resp->cstate.status; |
J. Bruce Fields | 53da6a5 | 2017-10-17 20:38:49 -0400 | [diff] [blame] | 2874 | free_svc_cred(&slot->sl_cred); |
| 2875 | copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred); |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 2876 | |
J. Bruce Fields | 085def3 | 2017-10-18 16:17:18 -0400 | [diff] [blame] | 2877 | if (!nfsd4_cache_this(resp)) { |
| 2878 | slot->sl_flags &= ~NFSD4_SLOT_CACHED; |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 2879 | return; |
| 2880 | } |
J. Bruce Fields | 085def3 | 2017-10-18 16:17:18 -0400 | [diff] [blame] | 2881 | slot->sl_flags |= NFSD4_SLOT_CACHED; |
| 2882 | |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 2883 | base = resp->cstate.data_offset; |
| 2884 | slot->sl_datalen = buf->len - base; |
| 2885 | 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] | 2886 | WARN(1, "%s: sessions DRC could not cache compound\n", |
| 2887 | __func__); |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2888 | return; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2889 | } |
| 2890 | |
| 2891 | /* |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2892 | * Encode the replay sequence operation from the slot values. |
| 2893 | * If cachethis is FALSE encode the uncached rep error on the next |
| 2894 | * operation which sets resp->p and increments resp->opcnt for |
| 2895 | * nfs4svc_encode_compoundres. |
| 2896 | * |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2897 | */ |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2898 | static __be32 |
| 2899 | nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args, |
| 2900 | struct nfsd4_compoundres *resp) |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2901 | { |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2902 | struct nfsd4_op *op; |
| 2903 | struct nfsd4_slot *slot = resp->cstate.slot; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2904 | |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2905 | /* Encode the replayed sequence operation */ |
| 2906 | op = &args->ops[resp->opcnt - 1]; |
| 2907 | nfsd4_encode_operation(resp, op); |
| 2908 | |
J. Bruce Fields | 085def3 | 2017-10-18 16:17:18 -0400 | [diff] [blame] | 2909 | if (slot->sl_flags & NFSD4_SLOT_CACHED) |
| 2910 | return op->status; |
| 2911 | if (args->opcnt == 1) { |
| 2912 | /* |
| 2913 | * The original operation wasn't a solo sequence--we |
| 2914 | * always cache those--so this retry must not match the |
| 2915 | * original: |
| 2916 | */ |
| 2917 | op->status = nfserr_seq_false_retry; |
| 2918 | } else { |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2919 | op = &args->ops[resp->opcnt++]; |
| 2920 | op->status = nfserr_retry_uncached_rep; |
| 2921 | nfsd4_encode_operation(resp, op); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2922 | } |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2923 | return op->status; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2924 | } |
| 2925 | |
| 2926 | /* |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2927 | * The sequence operation is not cached because we can use the slot and |
| 2928 | * session values. |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2929 | */ |
J. Bruce Fields | 3ca2eb9 | 2014-03-21 12:04:21 -0400 | [diff] [blame] | 2930 | static __be32 |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 2931 | nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp, |
| 2932 | struct nfsd4_sequence *seq) |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2933 | { |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2934 | struct nfsd4_slot *slot = resp->cstate.slot; |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 2935 | struct xdr_stream *xdr = &resp->xdr; |
| 2936 | __be32 *p; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2937 | __be32 status; |
| 2938 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2939 | dprintk("--> %s slot %p\n", __func__, slot); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2940 | |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2941 | status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp); |
J. Bruce Fields | 0da7b19 | 2014-03-21 11:43:34 -0400 | [diff] [blame] | 2942 | if (status) |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2943 | return status; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2944 | |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 2945 | p = xdr_reserve_space(xdr, slot->sl_datalen); |
| 2946 | if (!p) { |
| 2947 | WARN_ON_ONCE(1); |
| 2948 | return nfserr_serverfault; |
| 2949 | } |
| 2950 | xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen); |
| 2951 | xdr_commit_encode(xdr); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2952 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2953 | resp->opcnt = slot->sl_opcnt; |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 2954 | return slot->sl_status; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2955 | } |
| 2956 | |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2957 | /* |
| 2958 | * Set the exchange_id flags returned by the server. |
| 2959 | */ |
| 2960 | static void |
| 2961 | nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid) |
| 2962 | { |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 2963 | #ifdef CONFIG_NFSD_PNFS |
| 2964 | new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS; |
| 2965 | #else |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2966 | new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS; |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 2967 | #endif |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2968 | |
| 2969 | /* Referrals are supported, Migration is not. */ |
| 2970 | new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER; |
| 2971 | |
| 2972 | /* set the wire flags to return to client. */ |
| 2973 | clid->flags = new->cl_exchange_flags; |
| 2974 | } |
| 2975 | |
J. Bruce Fields | 4eaea13 | 2015-10-15 16:11:01 -0400 | [diff] [blame] | 2976 | static bool client_has_openowners(struct nfs4_client *clp) |
| 2977 | { |
| 2978 | struct nfs4_openowner *oo; |
| 2979 | |
| 2980 | list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) { |
| 2981 | if (!list_empty(&oo->oo_owner.so_stateids)) |
| 2982 | return true; |
| 2983 | } |
| 2984 | return false; |
| 2985 | } |
| 2986 | |
J. Bruce Fields | 631fc9e | 2012-05-14 15:57:23 -0400 | [diff] [blame] | 2987 | static bool client_has_state(struct nfs4_client *clp) |
| 2988 | { |
J. Bruce Fields | 4eaea13 | 2015-10-15 16:11:01 -0400 | [diff] [blame] | 2989 | return client_has_openowners(clp) |
Kinglong Mee | 47e970b | 2015-07-21 13:09:17 +0800 | [diff] [blame] | 2990 | #ifdef CONFIG_NFSD_PNFS |
| 2991 | || !list_empty(&clp->cl_lo_states) |
| 2992 | #endif |
J. Bruce Fields | 6eccece | 2012-05-29 16:37:44 -0400 | [diff] [blame] | 2993 | || !list_empty(&clp->cl_delegations) |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 2994 | || !list_empty(&clp->cl_sessions) |
| 2995 | || !list_empty(&clp->async_copies); |
J. Bruce Fields | 631fc9e | 2012-05-14 15:57:23 -0400 | [diff] [blame] | 2996 | } |
| 2997 | |
J. Bruce Fields | 7912344 | 2019-06-05 12:42:05 -0400 | [diff] [blame] | 2998 | static __be32 copy_impl_id(struct nfs4_client *clp, |
| 2999 | struct nfsd4_exchange_id *exid) |
| 3000 | { |
| 3001 | if (!exid->nii_domain.data) |
| 3002 | return 0; |
| 3003 | xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL); |
| 3004 | if (!clp->cl_nii_domain.data) |
| 3005 | return nfserr_jukebox; |
| 3006 | xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL); |
| 3007 | if (!clp->cl_nii_name.data) |
| 3008 | return nfserr_jukebox; |
Arnd Bergmann | e29f470 | 2019-10-31 14:52:43 +0100 | [diff] [blame] | 3009 | clp->cl_nii_time = exid->nii_time; |
J. Bruce Fields | 7912344 | 2019-06-05 12:42:05 -0400 | [diff] [blame] | 3010 | return 0; |
| 3011 | } |
| 3012 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3013 | __be32 |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3014 | nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 3015 | union nfsd4_op_u *u) |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3016 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3017 | struct nfsd4_exchange_id *exid = &u->exchange_id; |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3018 | struct nfs4_client *conf, *new; |
| 3019 | struct nfs4_client *unconf = NULL; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3020 | __be32 status; |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 3021 | char addr_str[INET6_ADDRSTRLEN]; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3022 | nfs4_verifier verf = exid->verifier; |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 3023 | struct sockaddr *sa = svc_addr(rqstp); |
J. Bruce Fields | 83e08fd | 2012-05-14 09:08:10 -0400 | [diff] [blame] | 3024 | bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 3025 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3026 | |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 3027 | rpc_ntop(sa, addr_str, sizeof(addr_str)); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3028 | dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p " |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 3029 | "ip_addr=%s flags %x, spa_how %d\n", |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3030 | __func__, rqstp, exid, exid->clname.len, exid->clname.data, |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 3031 | addr_str, exid->flags, exid->spa_how); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3032 | |
J. Bruce Fields | a084daf | 2011-10-10 15:07:40 -0400 | [diff] [blame] | 3033 | if (exid->flags & ~EXCHGID4_FLAG_MASK_A) |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3034 | return nfserr_inval; |
| 3035 | |
J. Bruce Fields | 50c7b94 | 2015-11-23 15:39:12 -0700 | [diff] [blame] | 3036 | new = create_client(exid->clname, rqstp, &verf); |
| 3037 | if (new == NULL) |
| 3038 | return nfserr_jukebox; |
J. Bruce Fields | 7912344 | 2019-06-05 12:42:05 -0400 | [diff] [blame] | 3039 | status = copy_impl_id(new, exid); |
| 3040 | if (status) |
| 3041 | goto out_nolock; |
J. Bruce Fields | 50c7b94 | 2015-11-23 15:39:12 -0700 | [diff] [blame] | 3042 | |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3043 | switch (exid->spa_how) { |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3044 | case SP4_MACH_CRED: |
Andrew Elble | ed94164 | 2016-06-15 12:52:09 -0400 | [diff] [blame] | 3045 | exid->spo_must_enforce[0] = 0; |
| 3046 | exid->spo_must_enforce[1] = ( |
| 3047 | 1 << (OP_BIND_CONN_TO_SESSION - 32) | |
| 3048 | 1 << (OP_EXCHANGE_ID - 32) | |
| 3049 | 1 << (OP_CREATE_SESSION - 32) | |
| 3050 | 1 << (OP_DESTROY_SESSION - 32) | |
| 3051 | 1 << (OP_DESTROY_CLIENTID - 32)); |
| 3052 | |
| 3053 | exid->spo_must_allow[0] &= (1 << (OP_CLOSE) | |
| 3054 | 1 << (OP_OPEN_DOWNGRADE) | |
| 3055 | 1 << (OP_LOCKU) | |
| 3056 | 1 << (OP_DELEGRETURN)); |
| 3057 | |
| 3058 | exid->spo_must_allow[1] &= ( |
| 3059 | 1 << (OP_TEST_STATEID - 32) | |
| 3060 | 1 << (OP_FREE_STATEID - 32)); |
J. Bruce Fields | 50c7b94 | 2015-11-23 15:39:12 -0700 | [diff] [blame] | 3061 | if (!svc_rqst_integrity_protected(rqstp)) { |
| 3062 | status = nfserr_inval; |
| 3063 | goto out_nolock; |
| 3064 | } |
J. Bruce Fields | 920dd9b | 2015-11-20 16:42:40 -0500 | [diff] [blame] | 3065 | /* |
| 3066 | * Sometimes userspace doesn't give us a principal. |
| 3067 | * Which is a bug, really. Anyway, we can't enforce |
| 3068 | * MACH_CRED in that case, better to give up now: |
| 3069 | */ |
J. Bruce Fields | 414ca01 | 2015-11-20 10:48:02 -0500 | [diff] [blame] | 3070 | if (!new->cl_cred.cr_principal && |
| 3071 | !new->cl_cred.cr_raw_principal) { |
J. Bruce Fields | 920dd9b | 2015-11-20 16:42:40 -0500 | [diff] [blame] | 3072 | status = nfserr_serverfault; |
| 3073 | goto out_nolock; |
| 3074 | } |
J. Bruce Fields | 50c7b94 | 2015-11-23 15:39:12 -0700 | [diff] [blame] | 3075 | new->cl_mach_cred = true; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3076 | case SP4_NONE: |
| 3077 | break; |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 3078 | default: /* checked by xdr code */ |
| 3079 | WARN_ON_ONCE(1); |
Gustavo A. R. Silva | 0a4c926 | 2019-01-23 02:48:28 -0600 | [diff] [blame] | 3080 | /* fall through */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3081 | case SP4_SSV: |
Kinglong Mee | 8edf4b0 | 2016-02-26 22:36:42 +0800 | [diff] [blame] | 3082 | status = nfserr_encr_alg_unsupp; |
| 3083 | goto out_nolock; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3084 | } |
| 3085 | |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 3086 | /* Cases below refer to rfc 5661 section 18.35.4: */ |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3087 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 3088 | conf = find_confirmed_client_by_name(&exid->clname, nn); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3089 | if (conf) { |
J. Bruce Fields | 83e08fd | 2012-05-14 09:08:10 -0400 | [diff] [blame] | 3090 | bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred); |
| 3091 | bool verfs_match = same_verf(&verf, &conf->cl_verifier); |
| 3092 | |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3093 | if (update) { |
| 3094 | if (!clp_used_exchangeid(conf)) { /* buggy client */ |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 3095 | status = nfserr_inval; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3096 | goto out; |
| 3097 | } |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 3098 | if (!nfsd4_mach_creds_match(conf, rqstp)) { |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3099 | status = nfserr_wrong_cred; |
| 3100 | goto out; |
| 3101 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3102 | if (!creds_match) { /* case 9 */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3103 | status = nfserr_perm; |
| 3104 | goto out; |
| 3105 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3106 | if (!verfs_match) { /* case 8 */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3107 | status = nfserr_not_same; |
| 3108 | goto out; |
| 3109 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3110 | /* case 6 */ |
| 3111 | exid->flags |= EXCHGID4_FLAG_CONFIRMED_R; |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3112 | goto out_copy; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3113 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3114 | if (!creds_match) { /* case 3 */ |
J. Bruce Fields | 631fc9e | 2012-05-14 15:57:23 -0400 | [diff] [blame] | 3115 | if (client_has_state(conf)) { |
| 3116 | status = nfserr_clid_inuse; |
| 3117 | goto out; |
| 3118 | } |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3119 | goto out_new; |
| 3120 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3121 | if (verfs_match) { /* case 2 */ |
J. Bruce Fields | 0f1ba0e | 2012-05-25 21:24:40 -0400 | [diff] [blame] | 3122 | conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R; |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3123 | goto out_copy; |
| 3124 | } |
| 3125 | /* case 5, client reboot */ |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3126 | conf = NULL; |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3127 | goto out_new; |
Mike Sager | 6ddbbbf | 2009-06-16 04:20:47 +0300 | [diff] [blame] | 3128 | } |
| 3129 | |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 3130 | if (update) { /* case 7 */ |
Mike Sager | 6ddbbbf | 2009-06-16 04:20:47 +0300 | [diff] [blame] | 3131 | status = nfserr_noent; |
| 3132 | goto out; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3133 | } |
| 3134 | |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 3135 | unconf = find_unconfirmed_client_by_name(&exid->clname, nn); |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 3136 | if (unconf) /* case 4, possible retry or client restart */ |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3137 | unhash_client_locked(unconf); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3138 | |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 3139 | /* case 1 (normal case) */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3140 | out_new: |
Jeff Layton | fd699b8 | 2014-07-30 08:27:14 -0400 | [diff] [blame] | 3141 | if (conf) { |
| 3142 | status = mark_client_expired_locked(conf); |
| 3143 | if (status) |
| 3144 | goto out; |
| 3145 | } |
J. Bruce Fields | 4f540e2 | 2013-05-07 11:57:52 -0400 | [diff] [blame] | 3146 | new->cl_minorversion = cstate->minorversion; |
Andrew Elble | ed94164 | 2016-06-15 12:52:09 -0400 | [diff] [blame] | 3147 | new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0]; |
| 3148 | 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] | 3149 | |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 3150 | add_to_unconfirmed(new); |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3151 | swap(new, conf); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3152 | out_copy: |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 3153 | exid->clientid.cl_boot = conf->cl_clientid.cl_boot; |
| 3154 | exid->clientid.cl_id = conf->cl_clientid.cl_id; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3155 | |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 3156 | exid->seqid = conf->cl_cs_slot.sl_seqid + 1; |
| 3157 | nfsd4_set_ex_flags(conf, exid); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3158 | |
| 3159 | dprintk("nfsd4_exchange_id seqid %d flags %x\n", |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 3160 | conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3161 | status = nfs_ok; |
| 3162 | |
| 3163 | out: |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3164 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | 50c7b94 | 2015-11-23 15:39:12 -0700 | [diff] [blame] | 3165 | out_nolock: |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 3166 | if (new) |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3167 | expire_client(new); |
| 3168 | if (unconf) |
| 3169 | expire_client(unconf); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3170 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3171 | } |
| 3172 | |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3173 | static __be32 |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 3174 | check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3175 | { |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 3176 | dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid, |
| 3177 | slot_seqid); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3178 | |
| 3179 | /* The slot is in use, and no response has been sent. */ |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 3180 | if (slot_inuse) { |
| 3181 | if (seqid == slot_seqid) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3182 | return nfserr_jukebox; |
| 3183 | else |
| 3184 | return nfserr_seq_misordered; |
| 3185 | } |
J. Bruce Fields | f6d8248 | 2012-02-13 16:13:41 -0500 | [diff] [blame] | 3186 | /* Note unsigned 32-bit arithmetic handles wraparound: */ |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 3187 | if (likely(seqid == slot_seqid + 1)) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3188 | return nfs_ok; |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 3189 | if (seqid == slot_seqid) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3190 | return nfserr_replay_cache; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3191 | return nfserr_seq_misordered; |
| 3192 | } |
| 3193 | |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 3194 | /* |
| 3195 | * Cache the create session result into the create session single DRC |
| 3196 | * slot cache by saving the xdr structure. sl_seqid has been set. |
| 3197 | * Do this for solo or embedded create session operations. |
| 3198 | */ |
| 3199 | static void |
| 3200 | nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses, |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3201 | struct nfsd4_clid_slot *slot, __be32 nfserr) |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 3202 | { |
| 3203 | slot->sl_status = nfserr; |
| 3204 | memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses)); |
| 3205 | } |
| 3206 | |
| 3207 | static __be32 |
| 3208 | nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses, |
| 3209 | struct nfsd4_clid_slot *slot) |
| 3210 | { |
| 3211 | memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses)); |
| 3212 | return slot->sl_status; |
| 3213 | } |
| 3214 | |
Mi Jinlong | 1b74c25 | 2011-07-14 14:50:17 +0800 | [diff] [blame] | 3215 | #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\ |
| 3216 | 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \ |
| 3217 | 1 + /* MIN tag is length with zero, only length */ \ |
| 3218 | 3 + /* version, opcount, opcode */ \ |
| 3219 | XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ |
| 3220 | /* seqid, slotID, slotID, cache */ \ |
| 3221 | 4 ) * sizeof(__be32)) |
| 3222 | |
| 3223 | #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\ |
| 3224 | 2 + /* verifier: AUTH_NULL, length 0 */\ |
| 3225 | 1 + /* status */ \ |
| 3226 | 1 + /* MIN tag is length with zero, only length */ \ |
| 3227 | 3 + /* opcount, opcode, opstatus*/ \ |
| 3228 | XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ |
| 3229 | /* seqid, slotID, slotID, slotID, status */ \ |
| 3230 | 5 ) * sizeof(__be32)) |
| 3231 | |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 3232 | 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] | 3233 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 3234 | u32 maxrpc = nn->nfsd_serv->sv_max_mesg; |
| 3235 | |
J. Bruce Fields | 373cd40 | 2013-04-08 15:42:12 -0400 | [diff] [blame] | 3236 | if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ) |
| 3237 | return nfserr_toosmall; |
| 3238 | if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ) |
| 3239 | return nfserr_toosmall; |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 3240 | ca->headerpadsz = 0; |
| 3241 | ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc); |
| 3242 | ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc); |
| 3243 | ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND); |
| 3244 | ca->maxresp_cached = min_t(u32, ca->maxresp_cached, |
| 3245 | NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ); |
| 3246 | ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION); |
| 3247 | /* |
| 3248 | * Note decreasing slot size below client's request may make it |
| 3249 | * difficult for client to function correctly, whereas |
| 3250 | * decreasing the number of slots will (just?) affect |
| 3251 | * performance. When short on memory we therefore prefer to |
| 3252 | * decrease number of slots instead of their size. Clients that |
| 3253 | * request larger slots than they need will get poor results: |
NeilBrown | 7f49fd5d | 2019-09-20 16:33:16 +1000 | [diff] [blame] | 3254 | * Note that we always allow at least one slot, because our |
| 3255 | * accounting is soft and provides no guarantees either way. |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 3256 | */ |
NeilBrown | 2030ca5 | 2019-09-20 16:36:45 +1000 | [diff] [blame] | 3257 | ca->maxreqs = nfsd4_get_drc_mem(ca, nn); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 3258 | |
J. Bruce Fields | 373cd40 | 2013-04-08 15:42:12 -0400 | [diff] [blame] | 3259 | return nfs_ok; |
Mi Jinlong | 1b74c25 | 2011-07-14 14:50:17 +0800 | [diff] [blame] | 3260 | } |
| 3261 | |
Chuck Lever | 4500632 | 2016-03-01 13:06:02 -0500 | [diff] [blame] | 3262 | /* |
| 3263 | * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now. |
| 3264 | * These are based on similar macros in linux/sunrpc/msg_prot.h . |
| 3265 | */ |
| 3266 | #define RPC_MAX_HEADER_WITH_AUTH_SYS \ |
| 3267 | (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK)) |
| 3268 | |
| 3269 | #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \ |
| 3270 | (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK)) |
| 3271 | |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 3272 | #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \ |
Chuck Lever | 4500632 | 2016-03-01 13:06:02 -0500 | [diff] [blame] | 3273 | RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32)) |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 3274 | #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \ |
Chuck Lever | 4500632 | 2016-03-01 13:06:02 -0500 | [diff] [blame] | 3275 | RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \ |
| 3276 | sizeof(__be32)) |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 3277 | |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 3278 | static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca) |
| 3279 | { |
| 3280 | ca->headerpadsz = 0; |
| 3281 | |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 3282 | if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ) |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 3283 | return nfserr_toosmall; |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 3284 | if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ) |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 3285 | return nfserr_toosmall; |
| 3286 | ca->maxresp_cached = 0; |
| 3287 | if (ca->maxops < 2) |
| 3288 | return nfserr_toosmall; |
| 3289 | |
| 3290 | return nfs_ok; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3291 | } |
| 3292 | |
J. Bruce Fields | b78724b | 2013-05-15 17:34:39 -0400 | [diff] [blame] | 3293 | static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs) |
| 3294 | { |
| 3295 | switch (cbs->flavor) { |
| 3296 | case RPC_AUTH_NULL: |
| 3297 | case RPC_AUTH_UNIX: |
| 3298 | return nfs_ok; |
| 3299 | default: |
| 3300 | /* |
| 3301 | * GSS case: the spec doesn't allow us to return this |
| 3302 | * error. But it also doesn't allow us not to support |
| 3303 | * GSS. |
| 3304 | * I'd rather this fail hard than return some error the |
| 3305 | * client might think it can already handle: |
| 3306 | */ |
| 3307 | return nfserr_encr_alg_unsupp; |
| 3308 | } |
| 3309 | } |
| 3310 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3311 | __be32 |
| 3312 | nfsd4_create_session(struct svc_rqst *rqstp, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3313 | struct nfsd4_compound_state *cstate, union nfsd4_op_u *u) |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3314 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3315 | struct nfsd4_create_session *cr_ses = &u->create_session; |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 3316 | struct sockaddr *sa = svc_addr(rqstp); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3317 | struct nfs4_client *conf, *unconf; |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3318 | struct nfs4_client *old = NULL; |
J. Bruce Fields | ac7c46f2 | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 3319 | struct nfsd4_session *new; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3320 | struct nfsd4_conn *conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 3321 | struct nfsd4_clid_slot *cs_slot = NULL; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3322 | __be32 status = 0; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 3323 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3324 | |
Mi Jinlong | a62573d | 2011-03-23 17:57:07 +0800 | [diff] [blame] | 3325 | if (cr_ses->flags & ~SESSION4_FLAG_MASK_A) |
| 3326 | return nfserr_inval; |
J. Bruce Fields | b78724b | 2013-05-15 17:34:39 -0400 | [diff] [blame] | 3327 | status = nfsd4_check_cb_sec(&cr_ses->cb_sec); |
| 3328 | if (status) |
| 3329 | return status; |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 3330 | status = check_forechannel_attrs(&cr_ses->fore_channel, nn); |
J. Bruce Fields | 373cd40 | 2013-04-08 15:42:12 -0400 | [diff] [blame] | 3331 | if (status) |
| 3332 | return status; |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 3333 | status = check_backchannel_attrs(&cr_ses->back_channel); |
| 3334 | if (status) |
Kinglong Mee | f403e45 | 2013-12-23 17:31:21 +0800 | [diff] [blame] | 3335 | goto out_release_drc_mem; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3336 | status = nfserr_jukebox; |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 3337 | new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 3338 | if (!new) |
| 3339 | goto out_release_drc_mem; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3340 | conn = alloc_conn_from_crses(rqstp, cr_ses); |
| 3341 | if (!conn) |
| 3342 | goto out_free_session; |
Mi Jinlong | a62573d | 2011-03-23 17:57:07 +0800 | [diff] [blame] | 3343 | |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3344 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 3345 | unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 3346 | conf = find_confirmed_client(&cr_ses->clientid, true, nn); |
J. Bruce Fields | 7838904 | 2013-03-12 10:12:37 -0400 | [diff] [blame] | 3347 | WARN_ON_ONCE(conf && unconf); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3348 | |
| 3349 | if (conf) { |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3350 | status = nfserr_wrong_cred; |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 3351 | if (!nfsd4_mach_creds_match(conf, rqstp)) |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3352 | goto out_free_conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 3353 | cs_slot = &conf->cl_cs_slot; |
| 3354 | status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); |
Kinglong Mee | f5e22bb | 2015-07-13 17:32:34 +0800 | [diff] [blame] | 3355 | if (status) { |
| 3356 | if (status == nfserr_replay_cache) |
| 3357 | status = nfsd4_replay_create_session(cr_ses, cs_slot); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3358 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3359 | } |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3360 | } else if (unconf) { |
| 3361 | if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) || |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 3362 | !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) { |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3363 | status = nfserr_clid_inuse; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3364 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3365 | } |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3366 | status = nfserr_wrong_cred; |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 3367 | if (!nfsd4_mach_creds_match(unconf, rqstp)) |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3368 | goto out_free_conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 3369 | cs_slot = &unconf->cl_cs_slot; |
| 3370 | status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); |
Andy Adamson | 38eb76a | 2009-04-03 08:28:32 +0300 | [diff] [blame] | 3371 | if (status) { |
| 3372 | /* an unconfirmed replay returns misordered */ |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3373 | status = nfserr_seq_misordered; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3374 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3375 | } |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 3376 | old = find_confirmed_client_by_name(&unconf->cl_name, nn); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3377 | if (old) { |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3378 | status = mark_client_expired_locked(old); |
Jeff Layton | 7abea1e | 2014-07-30 08:27:13 -0400 | [diff] [blame] | 3379 | if (status) { |
| 3380 | old = NULL; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3381 | goto out_free_conn; |
Jeff Layton | 7abea1e | 2014-07-30 08:27:13 -0400 | [diff] [blame] | 3382 | } |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3383 | } |
J. Bruce Fields | 8f9d3d3 | 2012-09-12 14:41:31 -0400 | [diff] [blame] | 3384 | move_to_confirmed(unconf); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3385 | conf = unconf; |
| 3386 | } else { |
| 3387 | status = nfserr_stale_clientid; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3388 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3389 | } |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3390 | status = nfs_ok; |
Chuck Lever | 4ce85c8 | 2016-03-01 13:05:27 -0500 | [diff] [blame] | 3391 | /* Persistent sessions are not supported */ |
J. Bruce Fields | 408b79b | 2010-04-15 15:11:09 -0400 | [diff] [blame] | 3392 | cr_ses->flags &= ~SESSION4_PERSIST; |
Chuck Lever | 4ce85c8 | 2016-03-01 13:05:27 -0500 | [diff] [blame] | 3393 | /* Upshifting from TCP to RDMA is not supported */ |
J. Bruce Fields | 408b79b | 2010-04-15 15:11:09 -0400 | [diff] [blame] | 3394 | cr_ses->flags &= ~SESSION4_RDMA; |
| 3395 | |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3396 | init_session(rqstp, new, conf, cr_ses); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3397 | nfsd4_get_session_locked(new); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3398 | |
J. Bruce Fields | ac7c46f2 | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 3399 | memcpy(cr_ses->sessionid.data, new->se_sessionid.data, |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3400 | NFS4_MAX_SESSIONID_LEN); |
J. Bruce Fields | 86c3e16 | 2010-10-02 17:04:00 -0400 | [diff] [blame] | 3401 | cs_slot->sl_seqid++; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 3402 | cr_ses->seqid = cs_slot->sl_seqid; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3403 | |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3404 | /* cache solo and embedded create sessions under the client_lock */ |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 3405 | nfsd4_cache_create_session(cr_ses, cs_slot, status); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3406 | spin_unlock(&nn->client_lock); |
| 3407 | /* init connection and backchannel */ |
| 3408 | nfsd4_init_conn(rqstp, conn, new); |
| 3409 | nfsd4_put_session(new); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3410 | if (old) |
| 3411 | expire_client(old); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3412 | return status; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3413 | out_free_conn: |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3414 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3415 | free_conn(conn); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3416 | if (old) |
| 3417 | expire_client(old); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3418 | out_free_session: |
| 3419 | __free_session(new); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 3420 | out_release_drc_mem: |
| 3421 | nfsd4_put_drc_mem(&cr_ses->fore_channel); |
J. Bruce Fields | 1ca5079 | 2013-03-14 18:12:03 -0400 | [diff] [blame] | 3422 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3423 | } |
| 3424 | |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 3425 | static __be32 nfsd4_map_bcts_dir(u32 *dir) |
| 3426 | { |
| 3427 | switch (*dir) { |
| 3428 | case NFS4_CDFC4_FORE: |
| 3429 | case NFS4_CDFC4_BACK: |
| 3430 | return nfs_ok; |
| 3431 | case NFS4_CDFC4_FORE_OR_BOTH: |
| 3432 | case NFS4_CDFC4_BACK_OR_BOTH: |
| 3433 | *dir = NFS4_CDFC4_BOTH; |
| 3434 | return nfs_ok; |
zhengbin | fc5fc5d | 2019-12-19 17:29:20 +0800 | [diff] [blame] | 3435 | } |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 3436 | return nfserr_inval; |
| 3437 | } |
| 3438 | |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3439 | __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, |
| 3440 | struct nfsd4_compound_state *cstate, |
| 3441 | union nfsd4_op_u *u) |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 3442 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3443 | struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl; |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 3444 | struct nfsd4_session *session = cstate->session; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3445 | 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] | 3446 | __be32 status; |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 3447 | |
J. Bruce Fields | b78724b | 2013-05-15 17:34:39 -0400 | [diff] [blame] | 3448 | status = nfsd4_check_cb_sec(&bc->bc_cb_sec); |
| 3449 | if (status) |
| 3450 | return status; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3451 | spin_lock(&nn->client_lock); |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 3452 | session->se_cb_prog = bc->bc_cb_program; |
| 3453 | session->se_cb_sec = bc->bc_cb_sec; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3454 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 3455 | |
| 3456 | nfsd4_probe_callback(session->se_client); |
| 3457 | |
| 3458 | return nfs_ok; |
| 3459 | } |
| 3460 | |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 3461 | __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp, |
| 3462 | struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3463 | union nfsd4_op_u *u) |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 3464 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3465 | 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] | 3466 | __be32 status; |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 3467 | struct nfsd4_conn *conn; |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 3468 | struct nfsd4_session *session; |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3469 | struct net *net = SVC_NET(rqstp); |
| 3470 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 3471 | |
| 3472 | if (!nfsd4_last_compound_op(rqstp)) |
| 3473 | return nfserr_not_only_op; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3474 | spin_lock(&nn->client_lock); |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3475 | session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3476 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 3477 | if (!session) |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3478 | goto out_no_session; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3479 | status = nfserr_wrong_cred; |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 3480 | if (!nfsd4_mach_creds_match(session->se_client, rqstp)) |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3481 | goto out; |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 3482 | status = nfsd4_map_bcts_dir(&bcts->dir); |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 3483 | if (status) |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 3484 | goto out; |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 3485 | conn = alloc_conn(rqstp, bcts->dir); |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 3486 | status = nfserr_jukebox; |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 3487 | if (!conn) |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 3488 | goto out; |
| 3489 | nfsd4_init_conn(rqstp, conn, session); |
| 3490 | status = nfs_ok; |
| 3491 | out: |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3492 | nfsd4_put_session(session); |
| 3493 | out_no_session: |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 3494 | return status; |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 3495 | } |
| 3496 | |
J. Bruce Fields | 665d507 | 2018-04-11 16:53:36 -0400 | [diff] [blame] | 3497 | 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] | 3498 | { |
J. Bruce Fields | 665d507 | 2018-04-11 16:53:36 -0400 | [diff] [blame] | 3499 | if (!cstate->session) |
Fengguang Wu | 51d87bc | 2018-03-22 13:37:20 +0800 | [diff] [blame] | 3500 | return false; |
J. Bruce Fields | 665d507 | 2018-04-11 16:53:36 -0400 | [diff] [blame] | 3501 | return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid)); |
J. Bruce Fields | 5d4cec2 | 2010-05-01 12:56:06 -0400 | [diff] [blame] | 3502 | } |
| 3503 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3504 | __be32 |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3505 | nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate, |
| 3506 | union nfsd4_op_u *u) |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3507 | { |
J. Bruce Fields | ca0552f | 2018-04-11 17:01:53 -0400 | [diff] [blame] | 3508 | struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid; |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 3509 | struct nfsd4_session *ses; |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 3510 | __be32 status; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 3511 | int ref_held_by_me = 0; |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3512 | struct net *net = SVC_NET(r); |
| 3513 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 3514 | |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 3515 | status = nfserr_not_only_op; |
J. Bruce Fields | ca0552f | 2018-04-11 17:01:53 -0400 | [diff] [blame] | 3516 | if (nfsd4_compound_in_session(cstate, sessionid)) { |
J. Bruce Fields | 5771635 | 2010-04-21 12:27:19 -0400 | [diff] [blame] | 3517 | if (!nfsd4_last_compound_op(r)) |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 3518 | goto out; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 3519 | ref_held_by_me++; |
J. Bruce Fields | 5771635 | 2010-04-21 12:27:19 -0400 | [diff] [blame] | 3520 | } |
J. Bruce Fields | ca0552f | 2018-04-11 17:01:53 -0400 | [diff] [blame] | 3521 | dump_sessionid(__func__, sessionid); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3522 | spin_lock(&nn->client_lock); |
J. Bruce Fields | ca0552f | 2018-04-11 17:01:53 -0400 | [diff] [blame] | 3523 | ses = find_in_sessionid_hashtbl(sessionid, net, &status); |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 3524 | if (!ses) |
| 3525 | goto out_client_lock; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3526 | status = nfserr_wrong_cred; |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 3527 | if (!nfsd4_mach_creds_match(ses->se_client, r)) |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3528 | goto out_put_session; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 3529 | status = mark_session_dead_locked(ses, 1 + ref_held_by_me); |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 3530 | if (status) |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 3531 | goto out_put_session; |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 3532 | unhash_session(ses); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3533 | spin_unlock(&nn->client_lock); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 3534 | |
J. Bruce Fields | 84f5f7c | 2010-12-09 15:52:19 -0500 | [diff] [blame] | 3535 | nfsd4_probe_callback_sync(ses->se_client); |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 3536 | |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3537 | spin_lock(&nn->client_lock); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 3538 | status = nfs_ok; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 3539 | out_put_session: |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3540 | nfsd4_put_session_locked(ses); |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 3541 | out_client_lock: |
| 3542 | spin_unlock(&nn->client_lock); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 3543 | out: |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 3544 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3545 | } |
| 3546 | |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 3547 | static struct nfsd4_conn *__nfsd4_find_conn(struct svc_xprt *xpt, struct nfsd4_session *s) |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 3548 | { |
| 3549 | struct nfsd4_conn *c; |
| 3550 | |
| 3551 | list_for_each_entry(c, &s->se_conns, cn_persession) { |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 3552 | if (c->cn_xprt == xpt) { |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 3553 | return c; |
| 3554 | } |
| 3555 | } |
| 3556 | return NULL; |
| 3557 | } |
| 3558 | |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3559 | 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] | 3560 | { |
| 3561 | struct nfs4_client *clp = ses->se_client; |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 3562 | struct nfsd4_conn *c; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3563 | __be32 status = nfs_ok; |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 3564 | int ret; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 3565 | |
| 3566 | spin_lock(&clp->cl_lock); |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 3567 | c = __nfsd4_find_conn(new->cn_xprt, ses); |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3568 | if (c) |
| 3569 | goto out_free; |
| 3570 | status = nfserr_conn_not_bound_to_session; |
| 3571 | if (clp->cl_mach_cred) |
| 3572 | goto out_free; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 3573 | __nfsd4_hash_conn(new, ses); |
| 3574 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 3575 | ret = nfsd4_register_conn(new); |
| 3576 | if (ret) |
| 3577 | /* oops; xprt is already down: */ |
| 3578 | nfsd4_conn_lost(&new->cn_xpt_user); |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3579 | return nfs_ok; |
| 3580 | out_free: |
| 3581 | spin_unlock(&clp->cl_lock); |
| 3582 | free_conn(new); |
| 3583 | return status; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 3584 | } |
| 3585 | |
Mi Jinlong | 868b89c | 2011-04-27 09:09:58 +0800 | [diff] [blame] | 3586 | static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session) |
| 3587 | { |
| 3588 | struct nfsd4_compoundargs *args = rqstp->rq_argp; |
| 3589 | |
| 3590 | return args->opcnt > session->se_fchannel.maxops; |
| 3591 | } |
| 3592 | |
Mi Jinlong | ae82a8d | 2011-07-14 14:56:02 +0800 | [diff] [blame] | 3593 | static bool nfsd4_request_too_big(struct svc_rqst *rqstp, |
| 3594 | struct nfsd4_session *session) |
| 3595 | { |
| 3596 | struct xdr_buf *xb = &rqstp->rq_arg; |
| 3597 | |
| 3598 | return xb->len > session->se_fchannel.maxreq_sz; |
| 3599 | } |
| 3600 | |
J. Bruce Fields | 53da6a5 | 2017-10-17 20:38:49 -0400 | [diff] [blame] | 3601 | static bool replay_matches_cache(struct svc_rqst *rqstp, |
| 3602 | struct nfsd4_sequence *seq, struct nfsd4_slot *slot) |
| 3603 | { |
| 3604 | struct nfsd4_compoundargs *argp = rqstp->rq_argp; |
| 3605 | |
| 3606 | if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) != |
| 3607 | (bool)seq->cachethis) |
| 3608 | return false; |
| 3609 | /* |
Scott Mayhew | 6e73e92 | 2019-10-09 15:11:37 -0400 | [diff] [blame] | 3610 | * If there's an error then the reply can have fewer ops than |
| 3611 | * the call. |
J. Bruce Fields | 53da6a5 | 2017-10-17 20:38:49 -0400 | [diff] [blame] | 3612 | */ |
Scott Mayhew | 6e73e92 | 2019-10-09 15:11:37 -0400 | [diff] [blame] | 3613 | if (slot->sl_opcnt < argp->opcnt && !slot->sl_status) |
| 3614 | return false; |
| 3615 | /* |
| 3616 | * But if we cached a reply with *more* ops than the call you're |
| 3617 | * sending us now, then this new call is clearly not really a |
| 3618 | * replay of the old one: |
| 3619 | */ |
| 3620 | if (slot->sl_opcnt > argp->opcnt) |
J. Bruce Fields | 53da6a5 | 2017-10-17 20:38:49 -0400 | [diff] [blame] | 3621 | return false; |
| 3622 | /* This is the only check explicitly called by spec: */ |
| 3623 | if (!same_creds(&rqstp->rq_cred, &slot->sl_cred)) |
| 3624 | return false; |
| 3625 | /* |
| 3626 | * There may be more comparisons we could actually do, but the |
| 3627 | * spec doesn't require us to catch every case where the calls |
| 3628 | * don't match (that would require caching the call as well as |
| 3629 | * the reply), so we don't bother. |
| 3630 | */ |
| 3631 | return true; |
| 3632 | } |
| 3633 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3634 | __be32 |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3635 | nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 3636 | union nfsd4_op_u *u) |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3637 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3638 | struct nfsd4_sequence *seq = &u->sequence; |
Andy Adamson | f9bb94c4 | 2009-04-03 08:28:12 +0300 | [diff] [blame] | 3639 | struct nfsd4_compoundres *resp = rqstp->rq_resp; |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 3640 | struct xdr_stream *xdr = &resp->xdr; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3641 | struct nfsd4_session *session; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3642 | struct nfs4_client *clp; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3643 | struct nfsd4_slot *slot; |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 3644 | struct nfsd4_conn *conn; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3645 | __be32 status; |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 3646 | int buflen; |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3647 | struct net *net = SVC_NET(rqstp); |
| 3648 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3649 | |
Andy Adamson | f9bb94c4 | 2009-04-03 08:28:12 +0300 | [diff] [blame] | 3650 | if (resp->opcnt != 1) |
| 3651 | return nfserr_sequence_pos; |
| 3652 | |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 3653 | /* |
| 3654 | * Will be either used or freed by nfsd4_sequence_check_conn |
| 3655 | * below. |
| 3656 | */ |
| 3657 | conn = alloc_conn(rqstp, NFS4_CDFC4_FORE); |
| 3658 | if (!conn) |
| 3659 | return nfserr_jukebox; |
| 3660 | |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3661 | spin_lock(&nn->client_lock); |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3662 | session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3663 | if (!session) |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3664 | goto out_no_session; |
| 3665 | clp = session->se_client; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3666 | |
Mi Jinlong | 868b89c | 2011-04-27 09:09:58 +0800 | [diff] [blame] | 3667 | status = nfserr_too_many_ops; |
| 3668 | if (nfsd4_session_too_many_ops(rqstp, session)) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 3669 | goto out_put_session; |
Mi Jinlong | 868b89c | 2011-04-27 09:09:58 +0800 | [diff] [blame] | 3670 | |
Mi Jinlong | ae82a8d | 2011-07-14 14:56:02 +0800 | [diff] [blame] | 3671 | status = nfserr_req_too_big; |
| 3672 | if (nfsd4_request_too_big(rqstp, session)) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 3673 | goto out_put_session; |
Mi Jinlong | ae82a8d | 2011-07-14 14:56:02 +0800 | [diff] [blame] | 3674 | |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3675 | status = nfserr_badslot; |
Alexandros Batsakis | 6c18ba9 | 2009-06-16 04:19:13 +0300 | [diff] [blame] | 3676 | if (seq->slotid >= session->se_fchannel.maxreqs) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 3677 | goto out_put_session; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3678 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 3679 | slot = session->se_slots[seq->slotid]; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3680 | dprintk("%s: slotid %d\n", __func__, seq->slotid); |
| 3681 | |
Andy Adamson | a8dfdae | 2009-08-28 08:45:02 -0400 | [diff] [blame] | 3682 | /* We do not negotiate the number of slots yet, so set the |
| 3683 | * maxslots to the session maxreqs which is used to encode |
| 3684 | * sr_highest_slotid and the sr_target_slot id to maxslots */ |
| 3685 | seq->maxslots = session->se_fchannel.maxreqs; |
| 3686 | |
J. Bruce Fields | 73e7948 | 2012-02-13 16:39:00 -0500 | [diff] [blame] | 3687 | status = check_slot_seqid(seq->seqid, slot->sl_seqid, |
| 3688 | slot->sl_flags & NFSD4_SLOT_INUSE); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3689 | if (status == nfserr_replay_cache) { |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 3690 | status = nfserr_seq_misordered; |
| 3691 | if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED)) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 3692 | goto out_put_session; |
J. Bruce Fields | 53da6a5 | 2017-10-17 20:38:49 -0400 | [diff] [blame] | 3693 | status = nfserr_seq_false_retry; |
| 3694 | if (!replay_matches_cache(rqstp, seq, slot)) |
| 3695 | goto out_put_session; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3696 | cstate->slot = slot; |
| 3697 | cstate->session = session; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3698 | cstate->clp = clp; |
Andy Adamson | da3846a | 2009-04-03 08:28:22 +0300 | [diff] [blame] | 3699 | /* Return the cached reply status and set cstate->status |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 3700 | * for nfsd4_proc_compound processing */ |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 3701 | status = nfsd4_replay_cache_entry(resp, seq); |
Andy Adamson | da3846a | 2009-04-03 08:28:22 +0300 | [diff] [blame] | 3702 | cstate->status = nfserr_replay_cache; |
Benny Halevy | aaf84eb | 2009-08-20 03:21:56 +0300 | [diff] [blame] | 3703 | goto out; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3704 | } |
| 3705 | if (status) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 3706 | goto out_put_session; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3707 | |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3708 | status = nfsd4_sequence_check_conn(conn, session); |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 3709 | conn = NULL; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3710 | if (status) |
| 3711 | goto out_put_session; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 3712 | |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 3713 | buflen = (seq->cachethis) ? |
| 3714 | session->se_fchannel.maxresp_cached : |
| 3715 | session->se_fchannel.maxresp_sz; |
| 3716 | status = (seq->cachethis) ? nfserr_rep_too_big_to_cache : |
| 3717 | nfserr_rep_too_big; |
J. Bruce Fields | a5cddc8 | 2014-05-12 18:10:58 -0400 | [diff] [blame] | 3718 | if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack)) |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 3719 | goto out_put_session; |
J. Bruce Fields | 32aaa62 | 2014-03-20 20:47:41 -0400 | [diff] [blame] | 3720 | svc_reserve(rqstp, buflen); |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 3721 | |
| 3722 | status = nfs_ok; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3723 | /* Success! bump slot seqid */ |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3724 | slot->sl_seqid = seq->seqid; |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 3725 | slot->sl_flags |= NFSD4_SLOT_INUSE; |
J. Bruce Fields | 73e7948 | 2012-02-13 16:39:00 -0500 | [diff] [blame] | 3726 | if (seq->cachethis) |
| 3727 | slot->sl_flags |= NFSD4_SLOT_CACHETHIS; |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 3728 | else |
| 3729 | slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3730 | |
| 3731 | cstate->slot = slot; |
| 3732 | cstate->session = session; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3733 | cstate->clp = clp; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3734 | |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3735 | out: |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3736 | switch (clp->cl_cb_state) { |
| 3737 | case NFSD4_CB_DOWN: |
| 3738 | seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN; |
| 3739 | break; |
| 3740 | case NFSD4_CB_FAULT: |
| 3741 | seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT; |
| 3742 | break; |
| 3743 | default: |
| 3744 | seq->status_flags = 0; |
Benny Halevy | aaf84eb | 2009-08-20 03:21:56 +0300 | [diff] [blame] | 3745 | } |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 3746 | if (!list_empty(&clp->cl_revoked)) |
| 3747 | seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3748 | out_no_session: |
Kinglong Mee | 3f42d2c | 2014-03-24 11:56:59 +0800 | [diff] [blame] | 3749 | if (conn) |
| 3750 | free_conn(conn); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3751 | spin_unlock(&nn->client_lock); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3752 | return status; |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 3753 | out_put_session: |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3754 | nfsd4_put_session_locked(session); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3755 | goto out_no_session; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3756 | } |
| 3757 | |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 3758 | void |
| 3759 | nfsd4_sequence_done(struct nfsd4_compoundres *resp) |
| 3760 | { |
| 3761 | struct nfsd4_compound_state *cs = &resp->cstate; |
| 3762 | |
| 3763 | if (nfsd4_has_session(cs)) { |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 3764 | if (cs->status != nfserr_replay_cache) { |
| 3765 | nfsd4_store_cache_entry(resp); |
| 3766 | cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE; |
| 3767 | } |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3768 | /* Drop session reference that was taken in nfsd4_sequence() */ |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 3769 | nfsd4_put_session(cs->session); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3770 | } else if (cs->clp) |
| 3771 | put_client_renew(cs->clp); |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 3772 | } |
| 3773 | |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3774 | __be32 |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3775 | nfsd4_destroy_clientid(struct svc_rqst *rqstp, |
| 3776 | struct nfsd4_compound_state *cstate, |
| 3777 | union nfsd4_op_u *u) |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3778 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3779 | struct nfsd4_destroy_clientid *dc = &u->destroy_clientid; |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3780 | struct nfs4_client *conf, *unconf; |
| 3781 | struct nfs4_client *clp = NULL; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3782 | __be32 status = 0; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 3783 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3784 | |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3785 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 3786 | unconf = find_unconfirmed_client(&dc->clientid, true, nn); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 3787 | conf = find_confirmed_client(&dc->clientid, true, nn); |
J. Bruce Fields | 7838904 | 2013-03-12 10:12:37 -0400 | [diff] [blame] | 3788 | WARN_ON_ONCE(conf && unconf); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3789 | |
| 3790 | if (conf) { |
J. Bruce Fields | c0293b0 | 2013-03-14 18:20:01 -0400 | [diff] [blame] | 3791 | if (client_has_state(conf)) { |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3792 | status = nfserr_clientid_busy; |
| 3793 | goto out; |
| 3794 | } |
Jeff Layton | fd699b8 | 2014-07-30 08:27:14 -0400 | [diff] [blame] | 3795 | status = mark_client_expired_locked(conf); |
| 3796 | if (status) |
| 3797 | goto out; |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3798 | clp = conf; |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3799 | } else if (unconf) |
| 3800 | clp = unconf; |
| 3801 | else { |
| 3802 | status = nfserr_stale_clientid; |
| 3803 | goto out; |
| 3804 | } |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 3805 | if (!nfsd4_mach_creds_match(clp, rqstp)) { |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3806 | clp = NULL; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3807 | status = nfserr_wrong_cred; |
| 3808 | goto out; |
| 3809 | } |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3810 | unhash_client_locked(clp); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3811 | out: |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3812 | spin_unlock(&nn->client_lock); |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3813 | if (clp) |
| 3814 | expire_client(clp); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3815 | return status; |
| 3816 | } |
| 3817 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3818 | __be32 |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3819 | nfsd4_reclaim_complete(struct svc_rqst *rqstp, |
| 3820 | struct nfsd4_compound_state *cstate, union nfsd4_op_u *u) |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 3821 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3822 | struct nfsd4_reclaim_complete *rc = &u->reclaim_complete; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3823 | __be32 status = 0; |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3824 | |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 3825 | if (rc->rca_one_fs) { |
| 3826 | if (!cstate->current_fh.fh_dentry) |
| 3827 | return nfserr_nofilehandle; |
| 3828 | /* |
| 3829 | * We don't take advantage of the rca_one_fs case. |
| 3830 | * That's OK, it's optional, we can safely ignore it. |
| 3831 | */ |
Christophe JAILLET | d28c442 | 2016-07-02 08:24:32 +0200 | [diff] [blame] | 3832 | return nfs_ok; |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 3833 | } |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3834 | |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3835 | status = nfserr_complete_already; |
Jeff Layton | a52d726 | 2012-03-21 09:52:02 -0400 | [diff] [blame] | 3836 | if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, |
| 3837 | &cstate->session->se_client->cl_flags)) |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3838 | goto out; |
| 3839 | |
| 3840 | status = nfserr_stale_clientid; |
| 3841 | if (is_client_expired(cstate->session->se_client)) |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 3842 | /* |
| 3843 | * The following error isn't really legal. |
| 3844 | * But we only get here if the client just explicitly |
| 3845 | * destroyed the client. Surely it no longer cares what |
| 3846 | * error it gets back on an operation for the dead |
| 3847 | * client. |
| 3848 | */ |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3849 | goto out; |
| 3850 | |
| 3851 | status = nfs_ok; |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 3852 | nfsd4_client_record_create(cstate->session->se_client); |
Scott Mayhew | 362063a | 2019-03-26 18:06:28 -0400 | [diff] [blame] | 3853 | inc_reclaim_complete(cstate->session->se_client); |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3854 | out: |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3855 | return status; |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 3856 | } |
| 3857 | |
| 3858 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 3859 | nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3860 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3861 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3862 | struct nfsd4_setclientid *setclid = &u->setclientid; |
J. Bruce Fields | a084daf | 2011-10-10 15:07:40 -0400 | [diff] [blame] | 3863 | struct xdr_netobj clname = setclid->se_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3864 | nfs4_verifier clverifier = setclid->se_verf; |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3865 | struct nfs4_client *conf, *new; |
| 3866 | struct nfs4_client *unconf = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3867 | __be32 status; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 3868 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
| 3869 | |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 3870 | new = create_client(clname, rqstp, &clverifier); |
| 3871 | if (new == NULL) |
| 3872 | return nfserr_jukebox; |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 3873 | /* Cases below refer to rfc 3530 section 14.2.33: */ |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3874 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 3875 | conf = find_confirmed_client_by_name(&clname, nn); |
J. Bruce Fields | 2b63482 | 2015-10-15 15:33:23 -0400 | [diff] [blame] | 3876 | if (conf && client_has_state(conf)) { |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 3877 | /* case 0: */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3878 | status = nfserr_clid_inuse; |
J. Bruce Fields | e203d50 | 2010-11-24 17:30:54 -0500 | [diff] [blame] | 3879 | if (clp_used_exchangeid(conf)) |
| 3880 | goto out; |
J. Bruce Fields | 026722c | 2009-03-18 15:06:26 -0400 | [diff] [blame] | 3881 | if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) { |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 3882 | char addr_str[INET6_ADDRSTRLEN]; |
| 3883 | rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str, |
| 3884 | sizeof(addr_str)); |
| 3885 | dprintk("NFSD: setclientid: string in use by client " |
| 3886 | "at %s\n", addr_str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3887 | goto out; |
| 3888 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3889 | } |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 3890 | unconf = find_unconfirmed_client_by_name(&clname, nn); |
J. Bruce Fields | 8f93071 | 2012-05-18 22:06:41 -0400 | [diff] [blame] | 3891 | if (unconf) |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3892 | unhash_client_locked(unconf); |
Kinglong Mee | 41eb167 | 2015-07-13 17:29:41 +0800 | [diff] [blame] | 3893 | if (conf && same_verf(&conf->cl_verifier, &clverifier)) { |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 3894 | /* case 1: probable callback update */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3895 | copy_clid(new, conf); |
Kinglong Mee | 41eb167 | 2015-07-13 17:29:41 +0800 | [diff] [blame] | 3896 | gen_confirm(new, nn); |
| 3897 | } else /* case 4 (new client) or cases 2, 3 (client reboot): */ |
J. Bruce Fields | e8a79fb | 2019-03-22 11:11:06 -0400 | [diff] [blame] | 3898 | ; |
J. Bruce Fields | 8323c3b | 2010-10-19 19:36:51 -0400 | [diff] [blame] | 3899 | new->cl_minorversion = 0; |
Takuma Umeya | 6f3d772 | 2010-12-15 14:09:01 +0900 | [diff] [blame] | 3900 | gen_callback(new, setclid, rqstp); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 3901 | add_to_unconfirmed(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3902 | setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot; |
| 3903 | setclid->se_clientid.cl_id = new->cl_clientid.cl_id; |
| 3904 | 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] | 3905 | new = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3906 | status = nfs_ok; |
| 3907 | out: |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3908 | spin_unlock(&nn->client_lock); |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 3909 | if (new) |
| 3910 | free_client(new); |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3911 | if (unconf) |
| 3912 | expire_client(unconf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3913 | return status; |
| 3914 | } |
| 3915 | |
| 3916 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3917 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 3918 | nfsd4_setclientid_confirm(struct svc_rqst *rqstp, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3919 | struct nfsd4_compound_state *cstate, |
| 3920 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3921 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3922 | struct nfsd4_setclientid_confirm *setclientid_confirm = |
| 3923 | &u->setclientid_confirm; |
NeilBrown | 21ab45a | 2005-06-23 22:04:14 -0700 | [diff] [blame] | 3924 | struct nfs4_client *conf, *unconf; |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3925 | struct nfs4_client *old = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3926 | nfs4_verifier confirm = setclientid_confirm->sc_confirm; |
| 3927 | clientid_t * clid = &setclientid_confirm->sc_clientid; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3928 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 3929 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3930 | |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 3931 | if (STALE_CLIENTID(clid, nn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3932 | return nfserr_stale_clientid; |
NeilBrown | 21ab45a | 2005-06-23 22:04:14 -0700 | [diff] [blame] | 3933 | |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3934 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 3935 | conf = find_confirmed_client(clid, false, nn); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 3936 | unconf = find_unconfirmed_client(clid, false, nn); |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 3937 | /* |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 3938 | * We try hard to give out unique clientid's, so if we get an |
| 3939 | * attempt to confirm the same clientid with a different cred, |
J. Bruce Fields | f984a7c | 2015-09-01 13:40:53 -0400 | [diff] [blame] | 3940 | * the client may be buggy; this should never happen. |
| 3941 | * |
| 3942 | * Nevertheless, RFC 7530 recommends INUSE for this case: |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 3943 | */ |
J. Bruce Fields | f984a7c | 2015-09-01 13:40:53 -0400 | [diff] [blame] | 3944 | status = nfserr_clid_inuse; |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 3945 | if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred)) |
| 3946 | goto out; |
| 3947 | if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred)) |
| 3948 | goto out; |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 3949 | /* cases below refer to rfc 3530 section 14.2.34: */ |
J. Bruce Fields | 90d700b | 2012-05-19 13:55:22 -0400 | [diff] [blame] | 3950 | if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) { |
J. Bruce Fields | 7d22fc1 | 2016-09-20 20:55:36 -0400 | [diff] [blame] | 3951 | if (conf && same_verf(&confirm, &conf->cl_confirm)) { |
| 3952 | /* case 2: probable retransmit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3953 | status = nfs_ok; |
J. Bruce Fields | 7d22fc1 | 2016-09-20 20:55:36 -0400 | [diff] [blame] | 3954 | } else /* case 4: client hasn't noticed we rebooted yet? */ |
J. Bruce Fields | 90d700b | 2012-05-19 13:55:22 -0400 | [diff] [blame] | 3955 | status = nfserr_stale_clientid; |
| 3956 | goto out; |
| 3957 | } |
| 3958 | status = nfs_ok; |
| 3959 | if (conf) { /* case 1: callback update */ |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3960 | old = unconf; |
| 3961 | unhash_client_locked(old); |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 3962 | nfsd4_change_callback(conf, &unconf->cl_cb_conn); |
J. Bruce Fields | 90d700b | 2012-05-19 13:55:22 -0400 | [diff] [blame] | 3963 | } else { /* case 3: normal case; new or rebooted client */ |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3964 | old = find_confirmed_client_by_name(&unconf->cl_name, nn); |
| 3965 | if (old) { |
J. Bruce Fields | 2b63482 | 2015-10-15 15:33:23 -0400 | [diff] [blame] | 3966 | status = nfserr_clid_inuse; |
| 3967 | if (client_has_state(old) |
| 3968 | && !same_creds(&unconf->cl_cred, |
| 3969 | &old->cl_cred)) |
| 3970 | goto out; |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3971 | status = mark_client_expired_locked(old); |
Jeff Layton | 7abea1e | 2014-07-30 08:27:13 -0400 | [diff] [blame] | 3972 | if (status) { |
| 3973 | old = NULL; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3974 | goto out; |
Jeff Layton | 7abea1e | 2014-07-30 08:27:13 -0400 | [diff] [blame] | 3975 | } |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3976 | } |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 3977 | move_to_confirmed(unconf); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3978 | conf = unconf; |
NeilBrown | 08e8987 | 2005-06-23 22:04:11 -0700 | [diff] [blame] | 3979 | } |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3980 | get_client_locked(conf); |
| 3981 | spin_unlock(&nn->client_lock); |
| 3982 | nfsd4_probe_callback(conf); |
| 3983 | spin_lock(&nn->client_lock); |
| 3984 | put_client_renew_locked(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3985 | out: |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3986 | spin_unlock(&nn->client_lock); |
| 3987 | if (old) |
| 3988 | expire_client(old); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3989 | return status; |
| 3990 | } |
| 3991 | |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3992 | static struct nfs4_file *nfsd4_alloc_file(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3993 | { |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3994 | return kmem_cache_alloc(file_slab, GFP_KERNEL); |
| 3995 | } |
| 3996 | |
| 3997 | /* OPEN Share state helper functions */ |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 3998 | static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval, |
| 3999 | struct nfs4_file *fp) |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 4000 | { |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 4001 | lockdep_assert_held(&state_lock); |
| 4002 | |
Elena Reshetova | 818a34e | 2017-10-20 12:53:30 +0300 | [diff] [blame] | 4003 | refcount_set(&fp->fi_ref, 1); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 4004 | spin_lock_init(&fp->fi_lock); |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 4005 | INIT_LIST_HEAD(&fp->fi_stateids); |
| 4006 | INIT_LIST_HEAD(&fp->fi_delegations); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 4007 | INIT_LIST_HEAD(&fp->fi_clnt_odstate); |
Trond Myklebust | e2cf80d | 2014-07-23 16:17:38 -0400 | [diff] [blame] | 4008 | fh_copy_shallow(&fp->fi_fhandle, fh); |
Jeff Layton | 0c637be | 2014-08-22 12:05:43 -0400 | [diff] [blame] | 4009 | fp->fi_deleg_file = NULL; |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 4010 | fp->fi_had_conflict = false; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4011 | fp->fi_share_deny = 0; |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 4012 | memset(fp->fi_fds, 0, sizeof(fp->fi_fds)); |
| 4013 | memset(fp->fi_access, 0, sizeof(fp->fi_access)); |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 4014 | #ifdef CONFIG_NFSD_PNFS |
| 4015 | INIT_LIST_HEAD(&fp->fi_lo_states); |
Christoph Hellwig | c5c707f | 2014-09-23 12:38:48 +0200 | [diff] [blame] | 4016 | atomic_set(&fp->fi_lo_recalls, 0); |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 4017 | #endif |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 4018 | hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4019 | } |
| 4020 | |
J. Bruce Fields | e8ff2a8 | 2007-08-01 15:30:59 -0400 | [diff] [blame] | 4021 | void |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 4022 | nfsd4_free_slabs(void) |
| 4023 | { |
Jeff Layton | 9258a2d | 2018-03-16 09:47:22 -0400 | [diff] [blame] | 4024 | kmem_cache_destroy(client_slab); |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 4025 | kmem_cache_destroy(openowner_slab); |
| 4026 | kmem_cache_destroy(lockowner_slab); |
| 4027 | kmem_cache_destroy(file_slab); |
| 4028 | kmem_cache_destroy(stateid_slab); |
| 4029 | kmem_cache_destroy(deleg_slab); |
Jeff Layton | 9258a2d | 2018-03-16 09:47:22 -0400 | [diff] [blame] | 4030 | kmem_cache_destroy(odstate_slab); |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 4031 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4032 | |
Bryan Schumaker | 7208339 | 2011-11-01 15:24:59 -0400 | [diff] [blame] | 4033 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4034 | nfsd4_init_slabs(void) |
| 4035 | { |
Jeff Layton | 9258a2d | 2018-03-16 09:47:22 -0400 | [diff] [blame] | 4036 | client_slab = kmem_cache_create("nfsd4_clients", |
| 4037 | sizeof(struct nfs4_client), 0, 0, NULL); |
| 4038 | if (client_slab == NULL) |
| 4039 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4040 | openowner_slab = kmem_cache_create("nfsd4_openowners", |
| 4041 | sizeof(struct nfs4_openowner), 0, 0, NULL); |
| 4042 | if (openowner_slab == NULL) |
Jeff Layton | 9258a2d | 2018-03-16 09:47:22 -0400 | [diff] [blame] | 4043 | goto out_free_client_slab; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4044 | lockowner_slab = kmem_cache_create("nfsd4_lockowners", |
Yanchuan Nian | 3c40794 | 2012-10-24 14:44:19 +0800 | [diff] [blame] | 4045 | sizeof(struct nfs4_lockowner), 0, 0, NULL); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4046 | if (lockowner_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 4047 | goto out_free_openowner_slab; |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 4048 | file_slab = kmem_cache_create("nfsd4_files", |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 4049 | sizeof(struct nfs4_file), 0, 0, NULL); |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 4050 | if (file_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 4051 | goto out_free_lockowner_slab; |
NeilBrown | 5ac049a | 2005-06-23 22:03:03 -0700 | [diff] [blame] | 4052 | stateid_slab = kmem_cache_create("nfsd4_stateids", |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4053 | sizeof(struct nfs4_ol_stateid), 0, 0, NULL); |
NeilBrown | 5ac049a | 2005-06-23 22:03:03 -0700 | [diff] [blame] | 4054 | if (stateid_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 4055 | goto out_free_file_slab; |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 4056 | deleg_slab = kmem_cache_create("nfsd4_delegations", |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 4057 | sizeof(struct nfs4_delegation), 0, 0, NULL); |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 4058 | if (deleg_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 4059 | goto out_free_stateid_slab; |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 4060 | odstate_slab = kmem_cache_create("nfsd4_odstate", |
| 4061 | sizeof(struct nfs4_clnt_odstate), 0, 0, NULL); |
| 4062 | if (odstate_slab == NULL) |
| 4063 | goto out_free_deleg_slab; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4064 | return 0; |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 4065 | |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 4066 | out_free_deleg_slab: |
| 4067 | kmem_cache_destroy(deleg_slab); |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 4068 | out_free_stateid_slab: |
| 4069 | kmem_cache_destroy(stateid_slab); |
| 4070 | out_free_file_slab: |
| 4071 | kmem_cache_destroy(file_slab); |
| 4072 | out_free_lockowner_slab: |
| 4073 | kmem_cache_destroy(lockowner_slab); |
| 4074 | out_free_openowner_slab: |
| 4075 | kmem_cache_destroy(openowner_slab); |
Jeff Layton | 9258a2d | 2018-03-16 09:47:22 -0400 | [diff] [blame] | 4076 | out_free_client_slab: |
| 4077 | kmem_cache_destroy(client_slab); |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 4078 | out: |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 4079 | dprintk("nfsd4: out of memory while initializing nfsv4\n"); |
| 4080 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4081 | } |
| 4082 | |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4083 | static void init_nfs4_replay(struct nfs4_replay *rp) |
| 4084 | { |
| 4085 | rp->rp_status = nfserr_serverfault; |
| 4086 | rp->rp_buflen = 0; |
| 4087 | rp->rp_buf = rp->rp_ibuf; |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 4088 | mutex_init(&rp->rp_mutex); |
| 4089 | } |
| 4090 | |
| 4091 | static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate, |
| 4092 | struct nfs4_stateowner *so) |
| 4093 | { |
| 4094 | if (!nfsd4_has_session(cstate)) { |
| 4095 | mutex_lock(&so->so_replay.rp_mutex); |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 4096 | cstate->replay_owner = nfs4_get_stateowner(so); |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 4097 | } |
| 4098 | } |
| 4099 | |
| 4100 | void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate) |
| 4101 | { |
| 4102 | struct nfs4_stateowner *so = cstate->replay_owner; |
| 4103 | |
| 4104 | if (so != NULL) { |
| 4105 | cstate->replay_owner = NULL; |
| 4106 | mutex_unlock(&so->so_replay.rp_mutex); |
| 4107 | nfs4_put_stateowner(so); |
| 4108 | } |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4109 | } |
| 4110 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4111 | 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] | 4112 | { |
| 4113 | struct nfs4_stateowner *sop; |
| 4114 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4115 | sop = kmem_cache_alloc(slab, GFP_KERNEL); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4116 | if (!sop) |
| 4117 | return NULL; |
| 4118 | |
J. Bruce Fields | 6f4859b | 2019-06-19 14:30:33 -0400 | [diff] [blame] | 4119 | xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4120 | if (!sop->so_owner.data) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4121 | kmem_cache_free(slab, sop); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4122 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4123 | } |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4124 | |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4125 | INIT_LIST_HEAD(&sop->so_stateids); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4126 | sop->so_client = clp; |
| 4127 | init_nfs4_replay(&sop->so_replay); |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 4128 | atomic_set(&sop->so_count, 1); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4129 | return sop; |
| 4130 | } |
| 4131 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4132 | 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] | 4133 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 4134 | lockdep_assert_held(&clp->cl_lock); |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 4135 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 4136 | list_add(&oo->oo_owner.so_strhash, |
| 4137 | &clp->cl_ownerstr_hashtbl[strhashval]); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4138 | list_add(&oo->oo_perclient, &clp->cl_openowners); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4139 | } |
| 4140 | |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 4141 | static void nfs4_unhash_openowner(struct nfs4_stateowner *so) |
| 4142 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 4143 | unhash_openowner_locked(openowner(so)); |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 4144 | } |
| 4145 | |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 4146 | static void nfs4_free_openowner(struct nfs4_stateowner *so) |
| 4147 | { |
| 4148 | struct nfs4_openowner *oo = openowner(so); |
| 4149 | |
| 4150 | kmem_cache_free(openowner_slab, oo); |
| 4151 | } |
| 4152 | |
| 4153 | static const struct nfs4_stateowner_operations openowner_ops = { |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 4154 | .so_unhash = nfs4_unhash_openowner, |
| 4155 | .so_free = nfs4_free_openowner, |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 4156 | }; |
| 4157 | |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4158 | static struct nfs4_ol_stateid * |
| 4159 | nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open) |
| 4160 | { |
| 4161 | struct nfs4_ol_stateid *local, *ret = NULL; |
| 4162 | struct nfs4_openowner *oo = open->op_openowner; |
| 4163 | |
| 4164 | lockdep_assert_held(&fp->fi_lock); |
| 4165 | |
| 4166 | list_for_each_entry(local, &fp->fi_stateids, st_perfile) { |
| 4167 | /* ignore lock owners */ |
| 4168 | if (local->st_stateowner->so_is_open_owner == 0) |
| 4169 | continue; |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 4170 | if (local->st_stateowner != &oo->oo_owner) |
| 4171 | continue; |
| 4172 | if (local->st_stid.sc_type == NFS4_OPEN_STID) { |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4173 | ret = local; |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 4174 | refcount_inc(&ret->st_stid.sc_count); |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4175 | break; |
| 4176 | } |
| 4177 | } |
| 4178 | return ret; |
| 4179 | } |
| 4180 | |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 4181 | static __be32 |
| 4182 | nfsd4_verify_open_stid(struct nfs4_stid *s) |
| 4183 | { |
| 4184 | __be32 ret = nfs_ok; |
| 4185 | |
| 4186 | switch (s->sc_type) { |
| 4187 | default: |
| 4188 | break; |
Trond Myklebust | 4f17641 | 2018-01-12 17:42:30 -0500 | [diff] [blame] | 4189 | case 0: |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 4190 | case NFS4_CLOSED_STID: |
| 4191 | case NFS4_CLOSED_DELEG_STID: |
| 4192 | ret = nfserr_bad_stateid; |
| 4193 | break; |
| 4194 | case NFS4_REVOKED_DELEG_STID: |
| 4195 | ret = nfserr_deleg_revoked; |
| 4196 | } |
| 4197 | return ret; |
| 4198 | } |
| 4199 | |
| 4200 | /* Lock the stateid st_mutex, and deal with races with CLOSE */ |
| 4201 | static __be32 |
| 4202 | nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp) |
| 4203 | { |
| 4204 | __be32 ret; |
| 4205 | |
Andrew Elble | 4f34bd0 | 2017-11-08 17:29:51 -0500 | [diff] [blame] | 4206 | mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX); |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 4207 | ret = nfsd4_verify_open_stid(&stp->st_stid); |
| 4208 | if (ret != nfs_ok) |
| 4209 | mutex_unlock(&stp->st_mutex); |
| 4210 | return ret; |
| 4211 | } |
| 4212 | |
| 4213 | static struct nfs4_ol_stateid * |
| 4214 | nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open) |
| 4215 | { |
| 4216 | struct nfs4_ol_stateid *stp; |
| 4217 | for (;;) { |
| 4218 | spin_lock(&fp->fi_lock); |
| 4219 | stp = nfsd4_find_existing_open(fp, open); |
| 4220 | spin_unlock(&fp->fi_lock); |
| 4221 | if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok) |
| 4222 | break; |
| 4223 | nfs4_put_stid(&stp->st_stid); |
| 4224 | } |
| 4225 | return stp; |
| 4226 | } |
| 4227 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4228 | static struct nfs4_openowner * |
Trond Myklebust | 13d6f66 | 2014-06-30 11:48:45 -0400 | [diff] [blame] | 4229 | alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open, |
Jeff Layton | db24b3b | 2014-06-30 11:48:36 -0400 | [diff] [blame] | 4230 | struct nfsd4_compound_state *cstate) |
| 4231 | { |
Trond Myklebust | 13d6f66 | 2014-06-30 11:48:45 -0400 | [diff] [blame] | 4232 | struct nfs4_client *clp = cstate->clp; |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 4233 | struct nfs4_openowner *oo, *ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4234 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4235 | oo = alloc_stateowner(openowner_slab, &open->op_owner, clp); |
| 4236 | if (!oo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4237 | return NULL; |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 4238 | oo->oo_owner.so_ops = &openowner_ops; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4239 | oo->oo_owner.so_is_open_owner = 1; |
| 4240 | oo->oo_owner.so_seqid = open->op_seqid; |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 4241 | oo->oo_flags = 0; |
Jeff Layton | db24b3b | 2014-06-30 11:48:36 -0400 | [diff] [blame] | 4242 | if (nfsd4_has_session(cstate)) |
| 4243 | oo->oo_flags |= NFS4_OO_CONFIRMED; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4244 | oo->oo_time = 0; |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 4245 | oo->oo_last_closed_stid = NULL; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4246 | INIT_LIST_HEAD(&oo->oo_close_lru); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 4247 | spin_lock(&clp->cl_lock); |
| 4248 | ret = find_openstateowner_str_locked(strhashval, open, clp); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 4249 | if (ret == NULL) { |
| 4250 | hash_openowner(oo, clp, strhashval); |
| 4251 | ret = oo; |
| 4252 | } else |
Kinglong Mee | d50ffde | 2015-07-16 12:05:07 +0800 | [diff] [blame] | 4253 | nfs4_free_stateowner(&oo->oo_owner); |
| 4254 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 4255 | spin_unlock(&clp->cl_lock); |
Jeff Layton | c595233 | 2015-03-23 10:53:42 -0400 | [diff] [blame] | 4256 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4257 | } |
| 4258 | |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4259 | static struct nfs4_ol_stateid * |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 4260 | init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open) |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4261 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4262 | |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4263 | struct nfs4_openowner *oo = open->op_openowner; |
| 4264 | struct nfs4_ol_stateid *retstp = NULL; |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 4265 | struct nfs4_ol_stateid *stp; |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4266 | |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 4267 | stp = open->op_stp; |
Oleg Drokin | 5cc1fb2 | 2016-06-14 23:28:05 -0400 | [diff] [blame] | 4268 | /* We are moving these outside of the spinlocks to avoid the warnings */ |
| 4269 | mutex_init(&stp->st_mutex); |
Andrew Elble | 4f34bd0 | 2017-11-08 17:29:51 -0500 | [diff] [blame] | 4270 | mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX); |
Oleg Drokin | 5cc1fb2 | 2016-06-14 23:28:05 -0400 | [diff] [blame] | 4271 | |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 4272 | retry: |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4273 | spin_lock(&oo->oo_owner.so_client->cl_lock); |
| 4274 | spin_lock(&fp->fi_lock); |
| 4275 | |
| 4276 | retstp = nfsd4_find_existing_open(fp, open); |
| 4277 | if (retstp) |
| 4278 | goto out_unlock; |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 4279 | |
| 4280 | open->op_stp = NULL; |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 4281 | refcount_inc(&stp->st_stid.sc_count); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 4282 | stp->st_stid.sc_type = NFS4_OPEN_STID; |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 4283 | INIT_LIST_HEAD(&stp->st_locks); |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 4284 | stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 4285 | get_nfs4_file(fp); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 4286 | stp->st_stid.sc_file = fp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4287 | stp->st_access_bmap = 0; |
| 4288 | stp->st_deny_bmap = 0; |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 4289 | stp->st_openstp = NULL; |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 4290 | list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 4291 | list_add(&stp->st_perfile, &fp->fi_stateids); |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4292 | |
| 4293 | out_unlock: |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 4294 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 4295 | spin_unlock(&oo->oo_owner.so_client->cl_lock); |
Oleg Drokin | 5cc1fb2 | 2016-06-14 23:28:05 -0400 | [diff] [blame] | 4296 | if (retstp) { |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 4297 | /* Handle races with CLOSE */ |
| 4298 | if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) { |
| 4299 | nfs4_put_stid(&retstp->st_stid); |
| 4300 | goto retry; |
| 4301 | } |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 4302 | /* To keep mutex tracking happy */ |
Oleg Drokin | 5cc1fb2 | 2016-06-14 23:28:05 -0400 | [diff] [blame] | 4303 | mutex_unlock(&stp->st_mutex); |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 4304 | stp = retstp; |
Oleg Drokin | 5cc1fb2 | 2016-06-14 23:28:05 -0400 | [diff] [blame] | 4305 | } |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 4306 | return stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4307 | } |
| 4308 | |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 4309 | /* |
| 4310 | * In the 4.0 case we need to keep the owners around a little while to handle |
| 4311 | * CLOSE replay. We still do need to release any file access that is held by |
| 4312 | * them before returning however. |
| 4313 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4314 | static void |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 4315 | move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4316 | { |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 4317 | struct nfs4_ol_stateid *last; |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 4318 | struct nfs4_openowner *oo = openowner(s->st_stateowner); |
| 4319 | struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net, |
| 4320 | nfsd_net_id); |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 4321 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4322 | dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4323 | |
Jeff Layton | b401be22 | 2014-07-29 21:34:33 -0400 | [diff] [blame] | 4324 | /* |
| 4325 | * We know that we hold one reference via nfsd4_close, and another |
| 4326 | * "persistent" reference for the client. If the refcount is higher |
| 4327 | * than 2, then there are still calls in progress that are using this |
| 4328 | * stateid. We can't put the sc_file reference until they are finished. |
| 4329 | * Wait for the refcount to drop to 2. Since it has been unhashed, |
| 4330 | * there should be no danger of the refcount going back up again at |
| 4331 | * this point. |
| 4332 | */ |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 4333 | wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2); |
Jeff Layton | b401be22 | 2014-07-29 21:34:33 -0400 | [diff] [blame] | 4334 | |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 4335 | release_all_access(s); |
| 4336 | if (s->st_stid.sc_file) { |
| 4337 | put_nfs4_file(s->st_stid.sc_file); |
| 4338 | s->st_stid.sc_file = NULL; |
| 4339 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 4340 | |
| 4341 | spin_lock(&nn->client_lock); |
| 4342 | last = oo->oo_last_closed_stid; |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 4343 | oo->oo_last_closed_stid = s; |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 4344 | list_move_tail(&oo->oo_close_lru, &nn->close_lru); |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 4345 | oo->oo_time = ktime_get_boottime_seconds(); |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 4346 | spin_unlock(&nn->client_lock); |
| 4347 | if (last) |
| 4348 | nfs4_put_stid(&last->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4349 | } |
| 4350 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4351 | /* search file_hashtbl[] for file */ |
| 4352 | static struct nfs4_file * |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 4353 | find_file_locked(struct knfsd_fh *fh, unsigned int hashval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4354 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4355 | struct nfs4_file *fp; |
| 4356 | |
Madhuparna Bhowmik | 36a8049 | 2020-02-13 20:33:31 +0530 | [diff] [blame] | 4357 | hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash, |
| 4358 | lockdep_is_held(&state_lock)) { |
Christoph Hellwig | 4d94c2e | 2014-08-14 08:41:48 +0200 | [diff] [blame] | 4359 | if (fh_match(&fp->fi_fhandle, fh)) { |
Elena Reshetova | 818a34e | 2017-10-20 12:53:30 +0300 | [diff] [blame] | 4360 | if (refcount_inc_not_zero(&fp->fi_ref)) |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 4361 | return fp; |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 4362 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4363 | } |
| 4364 | return NULL; |
| 4365 | } |
| 4366 | |
Christoph Hellwig | e6ba76e | 2014-08-14 08:50:16 +0200 | [diff] [blame] | 4367 | struct nfs4_file * |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 4368 | find_file(struct knfsd_fh *fh) |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 4369 | { |
| 4370 | struct nfs4_file *fp; |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 4371 | unsigned int hashval = file_hashval(fh); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 4372 | |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 4373 | rcu_read_lock(); |
| 4374 | fp = find_file_locked(fh, hashval); |
| 4375 | rcu_read_unlock(); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 4376 | return fp; |
| 4377 | } |
| 4378 | |
| 4379 | static struct nfs4_file * |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 4380 | find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh) |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 4381 | { |
| 4382 | struct nfs4_file *fp; |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 4383 | unsigned int hashval = file_hashval(fh); |
| 4384 | |
| 4385 | rcu_read_lock(); |
| 4386 | fp = find_file_locked(fh, hashval); |
| 4387 | rcu_read_unlock(); |
| 4388 | if (fp) |
| 4389 | return fp; |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 4390 | |
| 4391 | spin_lock(&state_lock); |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 4392 | fp = find_file_locked(fh, hashval); |
| 4393 | if (likely(fp == NULL)) { |
| 4394 | nfsd4_init_file(fh, hashval, new); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 4395 | fp = new; |
| 4396 | } |
| 4397 | spin_unlock(&state_lock); |
| 4398 | |
| 4399 | return fp; |
| 4400 | } |
| 4401 | |
J. Bruce Fields | 4f83aa3 | 2008-07-07 15:02:02 -0400 | [diff] [blame] | 4402 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4403 | * Called to check deny when READ with all zero stateid or |
| 4404 | * WRITE with all zero or all one stateid |
| 4405 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4406 | static __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4407 | nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type) |
| 4408 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4409 | struct nfs4_file *fp; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4410 | __be32 ret = nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4411 | |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 4412 | fp = find_file(¤t_fh->fh_handle); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 4413 | if (!fp) |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4414 | return ret; |
| 4415 | /* Check for conflicting share reservations */ |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 4416 | spin_lock(&fp->fi_lock); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4417 | if (fp->fi_share_deny & deny_type) |
| 4418 | ret = nfserr_locked; |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 4419 | spin_unlock(&fp->fi_lock); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 4420 | put_nfs4_file(fp); |
| 4421 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4422 | } |
| 4423 | |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 4424 | static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4425 | { |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 4426 | struct nfs4_delegation *dp = cb_to_delegation(cb); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 4427 | struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net, |
| 4428 | nfsd_net_id); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 4429 | |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 4430 | block_delegations(&dp->dl_stid.sc_file->fi_fhandle); |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 4431 | |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 4432 | /* |
| 4433 | * 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] | 4434 | * already holding inode->i_lock. |
| 4435 | * |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 4436 | * If the dl_time != 0, then we know that it has already been |
| 4437 | * queued for a lease break. Don't queue it again. |
| 4438 | */ |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 4439 | spin_lock(&state_lock); |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 4440 | if (dp->dl_time == 0) { |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 4441 | dp->dl_time = ktime_get_boottime_seconds(); |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 4442 | list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru); |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 4443 | } |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 4444 | spin_unlock(&state_lock); |
| 4445 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4446 | |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 4447 | static int nfsd4_cb_recall_done(struct nfsd4_callback *cb, |
| 4448 | struct rpc_task *task) |
| 4449 | { |
| 4450 | struct nfs4_delegation *dp = cb_to_delegation(cb); |
| 4451 | |
Andrew Elble | a457974 | 2015-08-31 12:06:41 -0400 | [diff] [blame] | 4452 | if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID) |
| 4453 | return 1; |
| 4454 | |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 4455 | switch (task->tk_status) { |
| 4456 | case 0: |
| 4457 | return 1; |
Scott Mayhew | 1c73b9d | 2019-05-02 13:32:12 -0400 | [diff] [blame] | 4458 | case -NFS4ERR_DELAY: |
| 4459 | rpc_delay(task, 2 * HZ); |
| 4460 | return 0; |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 4461 | case -EBADHANDLE: |
| 4462 | case -NFS4ERR_BAD_STATEID: |
| 4463 | /* |
| 4464 | * Race: client probably got cb_recall before open reply |
| 4465 | * granting delegation. |
| 4466 | */ |
| 4467 | if (dp->dl_retries--) { |
| 4468 | rpc_delay(task, 2 * HZ); |
| 4469 | return 0; |
| 4470 | } |
| 4471 | /*FALLTHRU*/ |
| 4472 | default: |
Scott Mayhew | 1c73b9d | 2019-05-02 13:32:12 -0400 | [diff] [blame] | 4473 | return 1; |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 4474 | } |
| 4475 | } |
| 4476 | |
| 4477 | static void nfsd4_cb_recall_release(struct nfsd4_callback *cb) |
| 4478 | { |
| 4479 | struct nfs4_delegation *dp = cb_to_delegation(cb); |
| 4480 | |
| 4481 | nfs4_put_stid(&dp->dl_stid); |
| 4482 | } |
| 4483 | |
Julia Lawall | c4cb897 | 2015-11-21 22:57:39 +0100 | [diff] [blame] | 4484 | static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = { |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 4485 | .prepare = nfsd4_cb_recall_prepare, |
| 4486 | .done = nfsd4_cb_recall_done, |
| 4487 | .release = nfsd4_cb_recall_release, |
| 4488 | }; |
| 4489 | |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 4490 | static void nfsd_break_one_deleg(struct nfs4_delegation *dp) |
| 4491 | { |
| 4492 | /* |
| 4493 | * We're assuming the state code never drops its reference |
| 4494 | * without first removing the lease. Since we're in this lease |
J. Bruce Fields | 4a269efb | 2018-04-13 17:34:35 -0400 | [diff] [blame] | 4495 | * callback (and since the lease code is serialized by the |
| 4496 | * i_lock) we know the server hasn't removed the lease yet, and |
| 4497 | * we know it's safe to take a reference. |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 4498 | */ |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 4499 | refcount_inc(&dp->dl_stid.sc_count); |
Christoph Hellwig | f0b5de1 | 2014-09-24 12:19:18 +0200 | [diff] [blame] | 4500 | nfsd4_run_cb(&dp->dl_recall); |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 4501 | } |
| 4502 | |
Jeff Layton | 1c8c601 | 2013-06-21 08:58:15 -0400 | [diff] [blame] | 4503 | /* Called from break_lease() with i_lock held. */ |
Jeff Layton | 4d01b7f | 2014-09-01 15:06:54 -0400 | [diff] [blame] | 4504 | static bool |
| 4505 | nfsd_break_deleg_cb(struct file_lock *fl) |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 4506 | { |
Jeff Layton | 4d01b7f | 2014-09-01 15:06:54 -0400 | [diff] [blame] | 4507 | bool ret = false; |
J. Bruce Fields | 653e514 | 2018-02-15 22:08:45 -0500 | [diff] [blame] | 4508 | struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner; |
| 4509 | struct nfs4_file *fp = dp->dl_stid.sc_file; |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 4510 | |
J. Bruce Fields | 0272e1f | 2007-09-12 18:56:12 -0400 | [diff] [blame] | 4511 | /* |
| 4512 | * 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] | 4513 | * we'll remove it ourself if a delegation isn't returned |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 4514 | * in time: |
J. Bruce Fields | 0272e1f | 2007-09-12 18:56:12 -0400 | [diff] [blame] | 4515 | */ |
| 4516 | fl->fl_break_time = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4517 | |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 4518 | spin_lock(&fp->fi_lock); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4519 | fp->fi_had_conflict = true; |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 4520 | nfsd_break_one_deleg(dp); |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 4521 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 4d01b7f | 2014-09-01 15:06:54 -0400 | [diff] [blame] | 4522 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4523 | } |
| 4524 | |
Jeff Layton | c45198e | 2014-09-01 07:12:07 -0400 | [diff] [blame] | 4525 | static int |
Jeff Layton | 7448cc3 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 4526 | nfsd_change_deleg_cb(struct file_lock *onlist, int arg, |
| 4527 | struct list_head *dispose) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4528 | { |
| 4529 | if (arg & F_UNLCK) |
Jeff Layton | c45198e | 2014-09-01 07:12:07 -0400 | [diff] [blame] | 4530 | return lease_modify(onlist, arg, dispose); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4531 | else |
| 4532 | return -EAGAIN; |
| 4533 | } |
| 4534 | |
Alexey Dobriyan | 7b02196 | 2009-09-21 17:01:12 -0700 | [diff] [blame] | 4535 | static const struct lock_manager_operations nfsd_lease_mng_ops = { |
J. Bruce Fields | 8fb47a4 | 2011-07-20 20:21:59 -0400 | [diff] [blame] | 4536 | .lm_break = nfsd_break_deleg_cb, |
| 4537 | .lm_change = nfsd_change_deleg_cb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4538 | }; |
| 4539 | |
J. Bruce Fields | 7a8711c | 2011-09-02 09:03:37 -0400 | [diff] [blame] | 4540 | static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid) |
| 4541 | { |
| 4542 | if (nfsd4_has_session(cstate)) |
| 4543 | return nfs_ok; |
| 4544 | if (seqid == so->so_seqid - 1) |
| 4545 | return nfserr_replay_me; |
| 4546 | if (seqid == so->so_seqid) |
| 4547 | return nfs_ok; |
| 4548 | return nfserr_bad_seqid; |
| 4549 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4550 | |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4551 | static __be32 lookup_clientid(clientid_t *clid, |
| 4552 | struct nfsd4_compound_state *cstate, |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 4553 | struct nfsd_net *nn, |
| 4554 | bool sessions) |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4555 | { |
| 4556 | struct nfs4_client *found; |
| 4557 | |
| 4558 | if (cstate->clp) { |
| 4559 | found = cstate->clp; |
| 4560 | if (!same_clid(&found->cl_clientid, clid)) |
| 4561 | return nfserr_stale_clientid; |
| 4562 | return nfs_ok; |
| 4563 | } |
| 4564 | |
| 4565 | if (STALE_CLIENTID(clid, nn)) |
| 4566 | return nfserr_stale_clientid; |
| 4567 | |
| 4568 | /* |
| 4569 | * For v4.1+ we get the client in the SEQUENCE op. If we don't have one |
| 4570 | * cached already then we know this is for is for v4.0 and "sessions" |
| 4571 | * will be false. |
| 4572 | */ |
| 4573 | WARN_ON_ONCE(cstate->session); |
Trond Myklebust | 3e339f9 | 2014-07-30 08:27:09 -0400 | [diff] [blame] | 4574 | spin_lock(&nn->client_lock); |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 4575 | found = find_confirmed_client(clid, sessions, nn); |
Trond Myklebust | 3e339f9 | 2014-07-30 08:27:09 -0400 | [diff] [blame] | 4576 | if (!found) { |
| 4577 | spin_unlock(&nn->client_lock); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4578 | return nfserr_expired; |
Trond Myklebust | 3e339f9 | 2014-07-30 08:27:09 -0400 | [diff] [blame] | 4579 | } |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 4580 | atomic_inc(&found->cl_rpc_users); |
Trond Myklebust | 3e339f9 | 2014-07-30 08:27:09 -0400 | [diff] [blame] | 4581 | spin_unlock(&nn->client_lock); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4582 | |
| 4583 | /* Cache the nfs4_client in cstate! */ |
| 4584 | cstate->clp = found; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4585 | return nfs_ok; |
| 4586 | } |
| 4587 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4588 | __be32 |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 4589 | nfsd4_process_open1(struct nfsd4_compound_state *cstate, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4590 | struct nfsd4_open *open, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4591 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4592 | clientid_t *clientid = &open->op_clientid; |
| 4593 | struct nfs4_client *clp = NULL; |
| 4594 | unsigned int strhashval; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4595 | struct nfs4_openowner *oo = NULL; |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 4596 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4597 | |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 4598 | if (STALE_CLIENTID(&open->op_clientid, nn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4599 | return nfserr_stale_clientid; |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 4600 | /* |
| 4601 | * In case we need it later, after we've already created the |
| 4602 | * file and don't want to risk a further failure: |
| 4603 | */ |
| 4604 | open->op_file = nfsd4_alloc_file(); |
| 4605 | if (open->op_file == NULL) |
| 4606 | return nfserr_jukebox; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4607 | |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 4608 | status = lookup_clientid(clientid, cstate, nn, false); |
Trond Myklebust | 2d91e89 | 2014-06-30 11:48:46 -0400 | [diff] [blame] | 4609 | if (status) |
| 4610 | return status; |
| 4611 | clp = cstate->clp; |
| 4612 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 4613 | strhashval = ownerstr_hashval(&open->op_owner); |
| 4614 | oo = find_openstateowner_str(strhashval, open, clp); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4615 | open->op_openowner = oo; |
| 4616 | if (!oo) { |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 4617 | goto new_owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4618 | } |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 4619 | if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) { |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 4620 | /* Replace unconfirmed owners without checking for replay. */ |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4621 | release_openowner(oo); |
| 4622 | open->op_openowner = NULL; |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 4623 | goto new_owner; |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 4624 | } |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 4625 | status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid); |
| 4626 | if (status) |
| 4627 | return status; |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 4628 | goto alloc_stateid; |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 4629 | new_owner: |
Trond Myklebust | 13d6f66 | 2014-06-30 11:48:45 -0400 | [diff] [blame] | 4630 | oo = alloc_init_open_stateowner(strhashval, open, cstate); |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 4631 | if (oo == NULL) |
| 4632 | return nfserr_jukebox; |
| 4633 | open->op_openowner = oo; |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 4634 | alloc_stateid: |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 4635 | open->op_stp = nfs4_alloc_open_stateid(clp); |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 4636 | if (!open->op_stp) |
| 4637 | return nfserr_jukebox; |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 4638 | |
| 4639 | if (nfsd4_has_session(cstate) && |
| 4640 | (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) { |
| 4641 | open->op_odstate = alloc_clnt_odstate(clp); |
| 4642 | if (!open->op_odstate) |
| 4643 | return nfserr_jukebox; |
| 4644 | } |
| 4645 | |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 4646 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4647 | } |
| 4648 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4649 | static inline __be32 |
NeilBrown | 4a6e43e | 2005-06-23 22:02:50 -0700 | [diff] [blame] | 4650 | nfs4_check_delegmode(struct nfs4_delegation *dp, int flags) |
| 4651 | { |
| 4652 | if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ)) |
| 4653 | return nfserr_openmode; |
| 4654 | else |
| 4655 | return nfs_ok; |
| 4656 | } |
| 4657 | |
Daniel Mack | c47d832 | 2011-05-16 16:38:14 +0200 | [diff] [blame] | 4658 | static int share_access_to_flags(u32 share_access) |
J. Bruce Fields | 24a0111 | 2010-05-18 20:01:35 -0400 | [diff] [blame] | 4659 | { |
J. Bruce Fields | 24a0111 | 2010-05-18 20:01:35 -0400 | [diff] [blame] | 4660 | return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE; |
| 4661 | } |
| 4662 | |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4663 | 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] | 4664 | { |
| 4665 | struct nfs4_stid *ret; |
| 4666 | |
Andrew Elble | 95da1b3a | 2017-11-03 14:06:31 -0400 | [diff] [blame] | 4667 | ret = find_stateid_by_type(cl, s, |
| 4668 | NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID); |
J. Bruce Fields | f459e45 | 2011-09-09 09:06:12 -0400 | [diff] [blame] | 4669 | if (!ret) |
| 4670 | return NULL; |
| 4671 | return delegstateid(ret); |
| 4672 | } |
| 4673 | |
J. Bruce Fields | 8b289b2 | 2011-10-19 11:52:12 -0400 | [diff] [blame] | 4674 | static bool nfsd4_is_deleg_cur(struct nfsd4_open *open) |
| 4675 | { |
| 4676 | return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR || |
| 4677 | open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH; |
| 4678 | } |
| 4679 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4680 | static __be32 |
J. Bruce Fields | 41d2266 | 2013-03-21 15:49:47 -0400 | [diff] [blame] | 4681 | nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open, |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 4682 | struct nfs4_delegation **dp) |
| 4683 | { |
| 4684 | int flags; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4685 | __be32 status = nfserr_bad_stateid; |
Trond Myklebust | dcd94cc | 2014-07-29 21:34:18 -0400 | [diff] [blame] | 4686 | struct nfs4_delegation *deleg; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 4687 | |
Trond Myklebust | dcd94cc | 2014-07-29 21:34:18 -0400 | [diff] [blame] | 4688 | deleg = find_deleg_stateid(cl, &open->op_delegate_stateid); |
| 4689 | if (deleg == NULL) |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 4690 | goto out; |
Andrew Elble | 95da1b3a | 2017-11-03 14:06:31 -0400 | [diff] [blame] | 4691 | if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) { |
| 4692 | nfs4_put_stid(&deleg->dl_stid); |
| 4693 | if (cl->cl_minorversion) |
| 4694 | status = nfserr_deleg_revoked; |
| 4695 | goto out; |
| 4696 | } |
J. Bruce Fields | 24a0111 | 2010-05-18 20:01:35 -0400 | [diff] [blame] | 4697 | flags = share_access_to_flags(open->op_share_access); |
Trond Myklebust | dcd94cc | 2014-07-29 21:34:18 -0400 | [diff] [blame] | 4698 | status = nfs4_check_delegmode(deleg, flags); |
| 4699 | if (status) { |
| 4700 | nfs4_put_stid(&deleg->dl_stid); |
| 4701 | goto out; |
| 4702 | } |
| 4703 | *dp = deleg; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 4704 | out: |
J. Bruce Fields | 8b289b2 | 2011-10-19 11:52:12 -0400 | [diff] [blame] | 4705 | if (!nfsd4_is_deleg_cur(open)) |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 4706 | return nfs_ok; |
| 4707 | if (status) |
| 4708 | return status; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 4709 | open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 4710 | return nfs_ok; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 4711 | } |
| 4712 | |
J. Bruce Fields | 21fb401 | 2010-07-28 12:21:23 -0400 | [diff] [blame] | 4713 | static inline int nfs4_access_to_access(u32 nfs4_access) |
| 4714 | { |
| 4715 | int flags = 0; |
| 4716 | |
| 4717 | if (nfs4_access & NFS4_SHARE_ACCESS_READ) |
| 4718 | flags |= NFSD_MAY_READ; |
| 4719 | if (nfs4_access & NFS4_SHARE_ACCESS_WRITE) |
| 4720 | flags |= NFSD_MAY_WRITE; |
| 4721 | return flags; |
| 4722 | } |
| 4723 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4724 | static inline __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4725 | nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh, |
| 4726 | struct nfsd4_open *open) |
| 4727 | { |
| 4728 | struct iattr iattr = { |
| 4729 | .ia_valid = ATTR_SIZE, |
| 4730 | .ia_size = 0, |
| 4731 | }; |
| 4732 | if (!open->op_truncate) |
| 4733 | return 0; |
| 4734 | if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE)) |
Al Viro | 9246585 | 2006-01-18 17:43:46 -0800 | [diff] [blame] | 4735 | return nfserr_inval; |
Arnd Bergmann | 2a1aa48 | 2019-11-03 17:50:18 +0100 | [diff] [blame] | 4736 | return nfsd_setattr(rqstp, fh, &iattr, 0, (time64_t)0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4737 | } |
| 4738 | |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4739 | 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] | 4740 | struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, |
| 4741 | struct nfsd4_open *open) |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4742 | { |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 4743 | struct nfsd_file *nf = NULL; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4744 | __be32 status; |
| 4745 | int oflag = nfs4_access_to_omode(open->op_share_access); |
| 4746 | int access = nfs4_access_to_access(open->op_share_access); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4747 | unsigned char old_access_bmap, old_deny_bmap; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4748 | |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4749 | spin_lock(&fp->fi_lock); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4750 | |
| 4751 | /* |
| 4752 | * Are we trying to set a deny mode that would conflict with |
| 4753 | * current access? |
| 4754 | */ |
| 4755 | status = nfs4_file_check_deny(fp, open->op_share_deny); |
| 4756 | if (status != nfs_ok) { |
| 4757 | spin_unlock(&fp->fi_lock); |
| 4758 | goto out; |
| 4759 | } |
| 4760 | |
| 4761 | /* set access to the file */ |
| 4762 | status = nfs4_file_get_access(fp, open->op_share_access); |
| 4763 | if (status != nfs_ok) { |
| 4764 | spin_unlock(&fp->fi_lock); |
| 4765 | goto out; |
| 4766 | } |
| 4767 | |
| 4768 | /* Set access bits in stateid */ |
| 4769 | old_access_bmap = stp->st_access_bmap; |
| 4770 | set_access(open->op_share_access, stp); |
| 4771 | |
| 4772 | /* Set new deny mask */ |
| 4773 | old_deny_bmap = stp->st_deny_bmap; |
| 4774 | set_deny(open->op_share_deny, stp); |
| 4775 | fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH); |
| 4776 | |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4777 | if (!fp->fi_fds[oflag]) { |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4778 | spin_unlock(&fp->fi_lock); |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 4779 | status = nfsd_file_acquire(rqstp, cur_fh, access, &nf); |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4780 | if (status) |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4781 | goto out_put_access; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4782 | spin_lock(&fp->fi_lock); |
| 4783 | if (!fp->fi_fds[oflag]) { |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 4784 | fp->fi_fds[oflag] = nf; |
| 4785 | nf = NULL; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4786 | } |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4787 | } |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4788 | spin_unlock(&fp->fi_lock); |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 4789 | if (nf) |
| 4790 | nfsd_file_put(nf); |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4791 | |
| 4792 | status = nfsd4_truncate(rqstp, cur_fh, open); |
| 4793 | if (status) |
| 4794 | goto out_put_access; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4795 | out: |
| 4796 | return status; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4797 | out_put_access: |
| 4798 | stp->st_access_bmap = old_access_bmap; |
| 4799 | nfs4_file_put_access(fp, open->op_share_access); |
| 4800 | reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp); |
| 4801 | goto out; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4802 | } |
| 4803 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4804 | static __be32 |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4805 | 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] | 4806 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4807 | __be32 status; |
Arnd Bergmann | 6ac7536 | 2015-05-12 23:31:29 +0200 | [diff] [blame] | 4808 | unsigned char old_deny_bmap = stp->st_deny_bmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4809 | |
Jeff Layton | 6eb3a1d | 2014-07-10 14:07:31 -0400 | [diff] [blame] | 4810 | if (!test_access(open->op_share_access, stp)) |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4811 | return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open); |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4812 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4813 | /* test and set deny mode */ |
| 4814 | spin_lock(&fp->fi_lock); |
| 4815 | status = nfs4_file_check_deny(fp, open->op_share_deny); |
| 4816 | if (status == nfs_ok) { |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4817 | set_deny(open->op_share_deny, stp); |
| 4818 | fp->fi_share_deny |= |
| 4819 | (open->op_share_deny & NFS4_SHARE_DENY_BOTH); |
| 4820 | } |
| 4821 | spin_unlock(&fp->fi_lock); |
| 4822 | |
| 4823 | if (status != nfs_ok) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4824 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4825 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4826 | status = nfsd4_truncate(rqstp, cur_fh, open); |
| 4827 | if (status != nfs_ok) |
| 4828 | reset_union_bmap_deny(old_deny_bmap, stp); |
| 4829 | return status; |
| 4830 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4831 | |
J. Bruce Fields | 14a24e9 | 2010-12-10 19:02:49 -0500 | [diff] [blame] | 4832 | /* Should we give out recallable state?: */ |
| 4833 | static bool nfsd4_cb_channel_good(struct nfs4_client *clp) |
| 4834 | { |
| 4835 | if (clp->cl_cb_state == NFSD4_CB_UP) |
| 4836 | return true; |
| 4837 | /* |
| 4838 | * In the sessions case, since we don't have to establish a |
| 4839 | * separate connection for callbacks, we assume it's OK |
| 4840 | * until we hear otherwise: |
| 4841 | */ |
| 4842 | return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN; |
| 4843 | } |
| 4844 | |
J. Bruce Fields | 653e514 | 2018-02-15 22:08:45 -0500 | [diff] [blame] | 4845 | static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, |
| 4846 | int flag) |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 4847 | { |
| 4848 | struct file_lock *fl; |
| 4849 | |
| 4850 | fl = locks_alloc_lock(); |
| 4851 | if (!fl) |
| 4852 | return NULL; |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 4853 | fl->fl_lmops = &nfsd_lease_mng_ops; |
J. Bruce Fields | 617588d | 2011-07-01 15:18:34 -0400 | [diff] [blame] | 4854 | fl->fl_flags = FL_DELEG; |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 4855 | fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK; |
| 4856 | fl->fl_end = OFFSET_MAX; |
J. Bruce Fields | 653e514 | 2018-02-15 22:08:45 -0500 | [diff] [blame] | 4857 | fl->fl_owner = (fl_owner_t)dp; |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 4858 | fl->fl_pid = current->tgid; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 4859 | 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] | 4860 | return fl; |
| 4861 | } |
| 4862 | |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 4863 | static struct nfs4_delegation * |
| 4864 | nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh, |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 4865 | struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate) |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 4866 | { |
J. Bruce Fields | 68b18f5 | 2018-02-19 11:38:33 -0500 | [diff] [blame] | 4867 | int status = 0; |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 4868 | struct nfs4_delegation *dp; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 4869 | struct nfsd_file *nf; |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 4870 | struct file_lock *fl; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4871 | |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 4872 | /* |
| 4873 | * The fi_had_conflict and nfs_get_existing_delegation checks |
| 4874 | * here are just optimizations; we'll need to recheck them at |
| 4875 | * the end: |
| 4876 | */ |
J. Bruce Fields | bf7bd3e | 2013-08-15 16:55:26 -0400 | [diff] [blame] | 4877 | if (fp->fi_had_conflict) |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 4878 | return ERR_PTR(-EAGAIN); |
| 4879 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 4880 | nf = find_readable_file(fp); |
| 4881 | if (!nf) { |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 4882 | /* We should always have a readable file here */ |
| 4883 | WARN_ON_ONCE(1); |
| 4884 | return ERR_PTR(-EBADF); |
| 4885 | } |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 4886 | spin_lock(&state_lock); |
| 4887 | spin_lock(&fp->fi_lock); |
J. Bruce Fields | 68b18f5 | 2018-02-19 11:38:33 -0500 | [diff] [blame] | 4888 | if (nfs4_delegation_exists(clp, fp)) |
| 4889 | status = -EAGAIN; |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 4890 | else if (!fp->fi_deleg_file) { |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 4891 | fp->fi_deleg_file = nf; |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 4892 | /* increment early to prevent fi_deleg_file from being |
| 4893 | * cleared */ |
| 4894 | fp->fi_delegees = 1; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 4895 | nf = NULL; |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 4896 | } else |
| 4897 | fp->fi_delegees++; |
| 4898 | spin_unlock(&fp->fi_lock); |
| 4899 | spin_unlock(&state_lock); |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 4900 | if (nf) |
| 4901 | nfsd_file_put(nf); |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 4902 | if (status) |
| 4903 | return ERR_PTR(status); |
| 4904 | |
| 4905 | status = -ENOMEM; |
| 4906 | dp = alloc_init_deleg(clp, fp, fh, odstate); |
| 4907 | if (!dp) |
| 4908 | goto out_delegees; |
| 4909 | |
| 4910 | fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ); |
| 4911 | if (!fl) |
Andrew Elble | bd8d725 | 2018-10-05 09:32:08 -0400 | [diff] [blame] | 4912 | goto out_clnt_odstate; |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 4913 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 4914 | 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] | 4915 | if (fl) |
| 4916 | locks_free_lock(fl); |
| 4917 | if (status) |
| 4918 | goto out_clnt_odstate; |
| 4919 | |
| 4920 | spin_lock(&state_lock); |
| 4921 | spin_lock(&fp->fi_lock); |
| 4922 | if (fp->fi_had_conflict) |
| 4923 | status = -EAGAIN; |
| 4924 | else |
| 4925 | status = hash_delegation_locked(dp, fp); |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 4926 | spin_unlock(&fp->fi_lock); |
| 4927 | spin_unlock(&state_lock); |
| 4928 | |
| 4929 | if (status) |
Andrew Elble | 692ad28 | 2018-04-18 17:04:37 -0400 | [diff] [blame] | 4930 | goto out_unlock; |
| 4931 | |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 4932 | return dp; |
Andrew Elble | 692ad28 | 2018-04-18 17:04:37 -0400 | [diff] [blame] | 4933 | out_unlock: |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 4934 | 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] | 4935 | out_clnt_odstate: |
| 4936 | put_clnt_odstate(dp->dl_clnt_odstate); |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 4937 | nfs4_put_stid(&dp->dl_stid); |
| 4938 | out_delegees: |
| 4939 | put_deleg_file(fp); |
| 4940 | return ERR_PTR(status); |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 4941 | } |
| 4942 | |
Benny Halevy | 4aa8913 | 2012-02-21 14:16:44 -0800 | [diff] [blame] | 4943 | static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status) |
| 4944 | { |
| 4945 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 4946 | if (status == -EAGAIN) |
| 4947 | open->op_why_no_deleg = WND4_CONTENTION; |
| 4948 | else { |
| 4949 | open->op_why_no_deleg = WND4_RESOURCE; |
| 4950 | switch (open->op_deleg_want) { |
| 4951 | case NFS4_SHARE_WANT_READ_DELEG: |
| 4952 | case NFS4_SHARE_WANT_WRITE_DELEG: |
| 4953 | case NFS4_SHARE_WANT_ANY_DELEG: |
| 4954 | break; |
| 4955 | case NFS4_SHARE_WANT_CANCEL: |
| 4956 | open->op_why_no_deleg = WND4_CANCELLED; |
| 4957 | break; |
| 4958 | case NFS4_SHARE_WANT_NO_DELEG: |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 4959 | WARN_ON_ONCE(1); |
Benny Halevy | 4aa8913 | 2012-02-21 14:16:44 -0800 | [diff] [blame] | 4960 | } |
| 4961 | } |
| 4962 | } |
| 4963 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4964 | /* |
| 4965 | * Attempt to hand out a delegation. |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4966 | * |
| 4967 | * Note we don't support write delegations, and won't until the vfs has |
| 4968 | * proper support for them. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4969 | */ |
| 4970 | static void |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame] | 4971 | nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, |
| 4972 | struct nfs4_ol_stateid *stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4973 | { |
| 4974 | struct nfs4_delegation *dp; |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame] | 4975 | struct nfs4_openowner *oo = openowner(stp->st_stateowner); |
| 4976 | struct nfs4_client *clp = stp->st_stid.sc_client; |
J. Bruce Fields | 14a24e9 | 2010-12-10 19:02:49 -0500 | [diff] [blame] | 4977 | int cb_up; |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4978 | int status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4979 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4980 | cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client); |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 4981 | open->op_recall = 0; |
| 4982 | switch (open->op_claim_type) { |
| 4983 | case NFS4_OPEN_CLAIM_PREVIOUS: |
J. Bruce Fields | 2bf2387 | 2010-03-08 12:37:27 -0500 | [diff] [blame] | 4984 | if (!cb_up) |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 4985 | open->op_recall = 1; |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4986 | if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ) |
| 4987 | goto out_no_deleg; |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 4988 | break; |
| 4989 | case NFS4_OPEN_CLAIM_NULL: |
Ming Chen | ed47b06 | 2014-01-09 21:26:10 +0000 | [diff] [blame] | 4990 | case NFS4_OPEN_CLAIM_FH: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4991 | /* |
| 4992 | * Let's not give out any delegations till everyone's |
J. Bruce Fields | c87fb4a | 2015-08-06 12:47:02 -0400 | [diff] [blame] | 4993 | * had the chance to reclaim theirs, *and* until |
| 4994 | * NLM locks have all been reclaimed: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4995 | */ |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame] | 4996 | if (locks_in_grace(clp->net)) |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4997 | goto out_no_deleg; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 4998 | if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED)) |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4999 | goto out_no_deleg; |
Steve Dickson | 9a0590a | 2013-05-15 14:51:49 -0400 | [diff] [blame] | 5000 | /* |
| 5001 | * Also, if the file was opened for write or |
| 5002 | * create, there's a good chance the client's |
| 5003 | * about to write to it, resulting in an |
| 5004 | * immediate recall (since we don't support |
| 5005 | * write delegations): |
| 5006 | */ |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 5007 | if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 5008 | goto out_no_deleg; |
| 5009 | if (open->op_create == NFS4_OPEN_CREATE) |
| 5010 | goto out_no_deleg; |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 5011 | break; |
| 5012 | default: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 5013 | goto out_no_deleg; |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 5014 | } |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 5015 | 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] | 5016 | if (IS_ERR(dp)) |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 5017 | goto out_no_deleg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5018 | |
J. Bruce Fields | d5477a8 | 2011-09-08 12:07:44 -0400 | [diff] [blame] | 5019 | 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] | 5020 | |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 5021 | dprintk("NFSD: delegation stateid=" STATEID_FMT "\n", |
J. Bruce Fields | d5477a8 | 2011-09-08 12:07:44 -0400 | [diff] [blame] | 5022 | STATEID_VAL(&dp->dl_stid.sc_stateid)); |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 5023 | open->op_delegate_type = NFS4_OPEN_DELEGATE_READ; |
Trond Myklebust | 67cb127 | 2014-07-29 21:34:17 -0400 | [diff] [blame] | 5024 | nfs4_put_stid(&dp->dl_stid); |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 5025 | return; |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 5026 | out_no_deleg: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 5027 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE; |
| 5028 | if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS && |
J. Bruce Fields | d08d32e | 2013-06-21 11:05:32 -0400 | [diff] [blame] | 5029 | open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) { |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 5030 | dprintk("NFSD: WARNING: refusing delegation reclaim\n"); |
J. Bruce Fields | d08d32e | 2013-06-21 11:05:32 -0400 | [diff] [blame] | 5031 | open->op_recall = 1; |
| 5032 | } |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 5033 | |
| 5034 | /* 4.1 client asking for a delegation? */ |
| 5035 | if (open->op_deleg_want) |
| 5036 | nfsd4_open_deleg_none_ext(open, status); |
| 5037 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5038 | } |
| 5039 | |
Benny Halevy | e27f49c | 2012-02-21 14:16:54 -0800 | [diff] [blame] | 5040 | static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open, |
| 5041 | struct nfs4_delegation *dp) |
| 5042 | { |
| 5043 | if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG && |
| 5044 | dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) { |
| 5045 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 5046 | open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE; |
| 5047 | } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG && |
| 5048 | dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) { |
| 5049 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 5050 | open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE; |
| 5051 | } |
| 5052 | /* Otherwise the client must be confused wanting a delegation |
| 5053 | * it already has, therefore we don't return |
| 5054 | * NFS4_OPEN_DELEGATE_NONE_EXT and reason. |
| 5055 | */ |
| 5056 | } |
| 5057 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5058 | __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5059 | nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open) |
| 5060 | { |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 5061 | struct nfsd4_compoundres *resp = rqstp->rq_resp; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5062 | struct nfs4_client *cl = open->op_openowner->oo_owner.so_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5063 | struct nfs4_file *fp = NULL; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5064 | struct nfs4_ol_stateid *stp = NULL; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 5065 | struct nfs4_delegation *dp = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5066 | __be32 status; |
Trond Myklebust | d8a1a00 | 2017-11-03 08:00:11 -0400 | [diff] [blame] | 5067 | bool new_stp = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5068 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5069 | /* |
| 5070 | * Lookup file; if found, lookup stateid and check open request, |
| 5071 | * and check for delegations in the process of being recalled. |
| 5072 | * If not found, create the nfs4_file struct |
| 5073 | */ |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 5074 | fp = find_or_add_file(open->op_file, ¤t_fh->fh_handle); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 5075 | if (fp != open->op_file) { |
J. Bruce Fields | 41d2266 | 2013-03-21 15:49:47 -0400 | [diff] [blame] | 5076 | status = nfs4_check_deleg(cl, open, &dp); |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 5077 | if (status) |
| 5078 | goto out; |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 5079 | stp = nfsd4_find_and_lock_existing_open(fp, open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5080 | } else { |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 5081 | open->op_file = NULL; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 5082 | status = nfserr_bad_stateid; |
J. Bruce Fields | 8b289b2 | 2011-10-19 11:52:12 -0400 | [diff] [blame] | 5083 | if (nfsd4_is_deleg_cur(open)) |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 5084 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5085 | } |
| 5086 | |
Trond Myklebust | d8a1a00 | 2017-11-03 08:00:11 -0400 | [diff] [blame] | 5087 | if (!stp) { |
| 5088 | stp = init_open_stateid(fp, open); |
| 5089 | if (!open->op_stp) |
| 5090 | new_stp = true; |
| 5091 | } |
| 5092 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5093 | /* |
| 5094 | * OPEN the file, or upgrade an existing OPEN. |
| 5095 | * If truncate fails, the OPEN fails. |
Trond Myklebust | d8a1a00 | 2017-11-03 08:00:11 -0400 | [diff] [blame] | 5096 | * |
| 5097 | * stp is already locked. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5098 | */ |
Trond Myklebust | d8a1a00 | 2017-11-03 08:00:11 -0400 | [diff] [blame] | 5099 | if (!new_stp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5100 | /* Stateid was found, this is an OPEN upgrade */ |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 5101 | status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open); |
Jeff Layton | 35a92fe | 2015-09-17 07:47:08 -0400 | [diff] [blame] | 5102 | if (status) { |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 5103 | mutex_unlock(&stp->st_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5104 | goto out; |
Jeff Layton | 35a92fe | 2015-09-17 07:47:08 -0400 | [diff] [blame] | 5105 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5106 | } else { |
Jeff Layton | 6eb3a1d | 2014-07-10 14:07:31 -0400 | [diff] [blame] | 5107 | status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open); |
| 5108 | if (status) { |
Trond Myklebust | d8a1a00 | 2017-11-03 08:00:11 -0400 | [diff] [blame] | 5109 | stp->st_stid.sc_type = NFS4_CLOSED_STID; |
Jeff Layton | 6eb3a1d | 2014-07-10 14:07:31 -0400 | [diff] [blame] | 5110 | release_open_stateid(stp); |
Trond Myklebust | d8a1a00 | 2017-11-03 08:00:11 -0400 | [diff] [blame] | 5111 | mutex_unlock(&stp->st_mutex); |
Jeff Layton | 6eb3a1d | 2014-07-10 14:07:31 -0400 | [diff] [blame] | 5112 | goto out; |
| 5113 | } |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 5114 | |
| 5115 | stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp, |
| 5116 | open->op_odstate); |
| 5117 | if (stp->st_clnt_odstate == open->op_odstate) |
| 5118 | open->op_odstate = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5119 | } |
Trond Myklebust | d8a1a00 | 2017-11-03 08:00:11 -0400 | [diff] [blame] | 5120 | |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 5121 | nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid); |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 5122 | mutex_unlock(&stp->st_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5123 | |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 5124 | if (nfsd4_has_session(&resp->cstate)) { |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 5125 | if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) { |
| 5126 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 5127 | open->op_why_no_deleg = WND4_NOT_WANTED; |
| 5128 | goto nodeleg; |
| 5129 | } |
| 5130 | } |
| 5131 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5132 | /* |
| 5133 | * Attempt to hand out a delegation. No error return, because the |
| 5134 | * OPEN succeeds even if we fail. |
| 5135 | */ |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame] | 5136 | nfs4_open_delegation(current_fh, open, stp); |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 5137 | nodeleg: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5138 | status = nfs_ok; |
| 5139 | |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 5140 | dprintk("%s: stateid=" STATEID_FMT "\n", __func__, |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5141 | STATEID_VAL(&stp->st_stid.sc_stateid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5142 | out: |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 5143 | /* 4.1 client trying to upgrade/downgrade delegation? */ |
| 5144 | if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp && |
Benny Halevy | e27f49c | 2012-02-21 14:16:54 -0800 | [diff] [blame] | 5145 | open->op_deleg_want) |
| 5146 | nfsd4_deleg_xgrade_none_ext(open, dp); |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 5147 | |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 5148 | if (fp) |
| 5149 | put_nfs4_file(fp); |
NeilBrown | 3751517 | 2005-07-07 17:59:16 -0700 | [diff] [blame] | 5150 | if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS) |
Kinglong Mee | 8718602 | 2015-07-13 17:32:59 +0800 | [diff] [blame] | 5151 | open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5152 | /* |
| 5153 | * To finish the open response, we just need to set the rflags. |
| 5154 | */ |
| 5155 | open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX; |
Jeff Layton | 19e4c34 | 2016-09-16 16:28:27 -0400 | [diff] [blame] | 5156 | if (nfsd4_has_session(&resp->cstate)) |
| 5157 | open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK; |
| 5158 | else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5159 | open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM; |
Jeff Layton | 19e4c34 | 2016-09-16 16:28:27 -0400 | [diff] [blame] | 5160 | |
Trond Myklebust | dcd94cc | 2014-07-29 21:34:18 -0400 | [diff] [blame] | 5161 | if (dp) |
| 5162 | nfs4_put_stid(&dp->dl_stid); |
Trond Myklebust | d6f2bc5 | 2014-07-29 21:34:19 -0400 | [diff] [blame] | 5163 | if (stp) |
| 5164 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5165 | |
| 5166 | return status; |
| 5167 | } |
| 5168 | |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 5169 | void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate, |
Jeff Layton | 4229789 | 2015-03-23 10:53:44 -0400 | [diff] [blame] | 5170 | struct nfsd4_open *open) |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 5171 | { |
| 5172 | if (open->op_openowner) { |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 5173 | struct nfs4_stateowner *so = &open->op_openowner->oo_owner; |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 5174 | |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 5175 | nfsd4_cstate_assign_replay(cstate, so); |
| 5176 | nfs4_put_stateowner(so); |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 5177 | } |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 5178 | if (open->op_file) |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 5179 | kmem_cache_free(file_slab, open->op_file); |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 5180 | if (open->op_stp) |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 5181 | nfs4_put_stid(&open->op_stp->st_stid); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 5182 | if (open->op_odstate) |
| 5183 | kmem_cache_free(odstate_slab, open->op_odstate); |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 5184 | } |
| 5185 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5186 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 5187 | nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5188 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5189 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5190 | clientid_t *clid = &u->renew; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5191 | struct nfs4_client *clp; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5192 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 5193 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5195 | dprintk("process_renew(%08x/%08x): starting\n", |
| 5196 | clid->cl_boot, clid->cl_id); |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 5197 | status = lookup_clientid(clid, cstate, nn, false); |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 5198 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5199 | goto out; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 5200 | clp = cstate->clp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5201 | status = nfserr_cb_path_down; |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 5202 | if (!list_empty(&clp->cl_delegations) |
J. Bruce Fields | 77a3569 | 2010-04-30 18:51:44 -0400 | [diff] [blame] | 5203 | && clp->cl_cb_state != NFSD4_CB_UP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5204 | goto out; |
| 5205 | status = nfs_ok; |
| 5206 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5207 | return status; |
| 5208 | } |
| 5209 | |
Jeff Layton | 7f5ef2e | 2014-09-12 16:40:21 -0400 | [diff] [blame] | 5210 | void |
Stanislav Kinsbursky | 12760c6 | 2012-11-14 18:22:12 +0300 | [diff] [blame] | 5211 | nfsd4_end_grace(struct nfsd_net *nn) |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 5212 | { |
Jeff Layton | 33dcc48 | 2012-04-10 11:08:48 -0400 | [diff] [blame] | 5213 | /* do nothing if grace period already ended */ |
Stanislav Kinsbursky | a51c84e | 2012-07-25 16:57:37 +0400 | [diff] [blame] | 5214 | if (nn->grace_ended) |
Jeff Layton | 33dcc48 | 2012-04-10 11:08:48 -0400 | [diff] [blame] | 5215 | return; |
| 5216 | |
Stanislav Kinsbursky | a51c84e | 2012-07-25 16:57:37 +0400 | [diff] [blame] | 5217 | nn->grace_ended = true; |
J. Bruce Fields | 70b2823 | 2014-09-16 17:37:32 -0400 | [diff] [blame] | 5218 | /* |
| 5219 | * If the server goes down again right now, an NFSv4 |
| 5220 | * client will still be allowed to reclaim after it comes back up, |
| 5221 | * even if it hasn't yet had a chance to reclaim state this time. |
| 5222 | * |
| 5223 | */ |
Jeff Layton | 919b804 | 2014-09-12 16:40:20 -0400 | [diff] [blame] | 5224 | nfsd4_record_grace_done(nn); |
J. Bruce Fields | 70b2823 | 2014-09-16 17:37:32 -0400 | [diff] [blame] | 5225 | /* |
| 5226 | * At this point, NFSv4 clients can still reclaim. But if the |
| 5227 | * server crashes, any that have not yet reclaimed will be out |
| 5228 | * of luck on the next boot. |
| 5229 | * |
| 5230 | * (NFSv4.1+ clients are considered to have reclaimed once they |
| 5231 | * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to |
| 5232 | * have reclaimed after their first OPEN.) |
| 5233 | */ |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 5234 | locks_end_grace(&nn->nfsd4_manager); |
J. Bruce Fields | 70b2823 | 2014-09-16 17:37:32 -0400 | [diff] [blame] | 5235 | /* |
| 5236 | * At this point, and once lockd and/or any other containers |
| 5237 | * exit their grace period, further reclaims will fail and |
| 5238 | * regular locking can resume. |
| 5239 | */ |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 5240 | } |
| 5241 | |
J. Bruce Fields | 03f318c | 2018-06-08 12:28:47 -0400 | [diff] [blame] | 5242 | /* |
| 5243 | * If we've waited a lease period but there are still clients trying to |
| 5244 | * reclaim, wait a little longer to give them a chance to finish. |
| 5245 | */ |
| 5246 | static bool clients_still_reclaiming(struct nfsd_net *nn) |
| 5247 | { |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 5248 | time64_t double_grace_period_end = nn->boot_time + |
| 5249 | 2 * nn->nfsd4_lease; |
J. Bruce Fields | 03f318c | 2018-06-08 12:28:47 -0400 | [diff] [blame] | 5250 | |
Scott Mayhew | 362063a | 2019-03-26 18:06:28 -0400 | [diff] [blame] | 5251 | if (nn->track_reclaim_completes && |
| 5252 | atomic_read(&nn->nr_reclaim_complete) == |
| 5253 | nn->reclaim_str_hashtbl_size) |
| 5254 | return false; |
J. Bruce Fields | 03f318c | 2018-06-08 12:28:47 -0400 | [diff] [blame] | 5255 | if (!nn->somebody_reclaimed) |
| 5256 | return false; |
| 5257 | nn->somebody_reclaimed = false; |
| 5258 | /* |
| 5259 | * If we've given them *two* lease times to reclaim, and they're |
| 5260 | * still not done, give up: |
| 5261 | */ |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 5262 | if (ktime_get_boottime_seconds() > double_grace_period_end) |
J. Bruce Fields | 03f318c | 2018-06-08 12:28:47 -0400 | [diff] [blame] | 5263 | return false; |
| 5264 | return true; |
| 5265 | } |
| 5266 | |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 5267 | static time64_t |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 5268 | nfs4_laundromat(struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5269 | { |
| 5270 | struct nfs4_client *clp; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5271 | struct nfs4_openowner *oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5272 | struct nfs4_delegation *dp; |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 5273 | struct nfs4_ol_stateid *stp; |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 5274 | struct nfsd4_blocked_lock *nbl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5275 | struct list_head *pos, *next, reaplist; |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 5276 | time64_t cutoff = ktime_get_boottime_seconds() - nn->nfsd4_lease; |
| 5277 | time64_t t, new_timeo = nn->nfsd4_lease; |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 5278 | struct nfs4_cpntf_state *cps; |
| 5279 | copy_stateid_t *cps_t; |
| 5280 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5281 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5282 | dprintk("NFSD: laundromat service - starting\n"); |
J. Bruce Fields | 03f318c | 2018-06-08 12:28:47 -0400 | [diff] [blame] | 5283 | |
| 5284 | if (clients_still_reclaiming(nn)) { |
| 5285 | new_timeo = 0; |
| 5286 | goto out; |
| 5287 | } |
Scott Mayhew | 362063a | 2019-03-26 18:06:28 -0400 | [diff] [blame] | 5288 | dprintk("NFSD: end of grace period\n"); |
Stanislav Kinsbursky | 12760c6 | 2012-11-14 18:22:12 +0300 | [diff] [blame] | 5289 | nfsd4_end_grace(nn); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 5290 | INIT_LIST_HEAD(&reaplist); |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 5291 | |
| 5292 | spin_lock(&nn->s2s_cp_lock); |
| 5293 | idr_for_each_entry(&nn->s2s_cp_stateids, cps_t, i) { |
| 5294 | cps = container_of(cps_t, struct nfs4_cpntf_state, cp_stateid); |
| 5295 | if (cps->cp_stateid.sc_type == NFS4_COPYNOTIFY_STID && |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 5296 | cps->cpntf_time > cutoff) |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 5297 | _free_cpntf_state_locked(nn, cps); |
| 5298 | } |
| 5299 | spin_unlock(&nn->s2s_cp_lock); |
| 5300 | |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 5301 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 5ed58bb | 2012-11-14 18:21:56 +0300 | [diff] [blame] | 5302 | list_for_each_safe(pos, next, &nn->client_lru) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5303 | clp = list_entry(pos, struct nfs4_client, cl_lru); |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 5304 | if (clp->cl_time > cutoff) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5305 | t = clp->cl_time - cutoff; |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 5306 | new_timeo = min(new_timeo, t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5307 | break; |
| 5308 | } |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 5309 | if (mark_client_expired_locked(clp)) { |
Benny Halevy | d768298 | 2010-05-12 00:13:54 +0300 | [diff] [blame] | 5310 | dprintk("NFSD: client in use (clientid %08x)\n", |
| 5311 | clp->cl_clientid.cl_id); |
| 5312 | continue; |
| 5313 | } |
Trond Myklebust | 4864af9 | 2014-07-30 08:27:03 -0400 | [diff] [blame] | 5314 | list_add(&clp->cl_lru, &reaplist); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 5315 | } |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 5316 | spin_unlock(&nn->client_lock); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 5317 | list_for_each_safe(pos, next, &reaplist) { |
| 5318 | clp = list_entry(pos, struct nfs4_client, cl_lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5319 | dprintk("NFSD: purging unused client (clientid %08x)\n", |
| 5320 | clp->cl_clientid.cl_id); |
Trond Myklebust | 4864af9 | 2014-07-30 08:27:03 -0400 | [diff] [blame] | 5321 | list_del_init(&clp->cl_lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5322 | expire_client(clp); |
| 5323 | } |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5324 | spin_lock(&state_lock); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 5325 | list_for_each_safe(pos, next, &nn->del_recall_lru) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5326 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 5327 | if (dp->dl_time > cutoff) { |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 5328 | t = dp->dl_time - cutoff; |
| 5329 | new_timeo = min(new_timeo, t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5330 | break; |
| 5331 | } |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 5332 | WARN_ON(!unhash_delegation_locked(dp)); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 5333 | list_add(&dp->dl_recall_lru, &reaplist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5334 | } |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5335 | spin_unlock(&state_lock); |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 5336 | while (!list_empty(&reaplist)) { |
| 5337 | dp = list_first_entry(&reaplist, struct nfs4_delegation, |
| 5338 | dl_recall_lru); |
| 5339 | list_del_init(&dp->dl_recall_lru); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 5340 | revoke_delegation(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5341 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 5342 | |
| 5343 | spin_lock(&nn->client_lock); |
| 5344 | while (!list_empty(&nn->close_lru)) { |
| 5345 | oo = list_first_entry(&nn->close_lru, struct nfs4_openowner, |
| 5346 | oo_close_lru); |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 5347 | if (oo->oo_time > cutoff) { |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 5348 | t = oo->oo_time - cutoff; |
| 5349 | new_timeo = min(new_timeo, t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5350 | break; |
| 5351 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 5352 | list_del_init(&oo->oo_close_lru); |
| 5353 | stp = oo->oo_last_closed_stid; |
| 5354 | oo->oo_last_closed_stid = NULL; |
| 5355 | spin_unlock(&nn->client_lock); |
| 5356 | nfs4_put_stid(&stp->st_stid); |
| 5357 | spin_lock(&nn->client_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5358 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 5359 | spin_unlock(&nn->client_lock); |
| 5360 | |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 5361 | /* |
| 5362 | * It's possible for a client to try and acquire an already held lock |
| 5363 | * that is being held for a long time, and then lose interest in it. |
| 5364 | * So, we clean out any un-revisited request after a lease period |
| 5365 | * under the assumption that the client is no longer interested. |
| 5366 | * |
| 5367 | * RFC5661, sec. 9.6 states that the client must not rely on getting |
| 5368 | * notifications and must continue to poll for locks, even when the |
| 5369 | * server supports them. Thus this shouldn't lead to clients blocking |
| 5370 | * indefinitely once the lock does become free. |
| 5371 | */ |
| 5372 | BUG_ON(!list_empty(&reaplist)); |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 5373 | spin_lock(&nn->blocked_locks_lock); |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 5374 | while (!list_empty(&nn->blocked_locks_lru)) { |
| 5375 | nbl = list_first_entry(&nn->blocked_locks_lru, |
| 5376 | struct nfsd4_blocked_lock, nbl_lru); |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 5377 | if (nbl->nbl_time > cutoff) { |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 5378 | t = nbl->nbl_time - cutoff; |
| 5379 | new_timeo = min(new_timeo, t); |
| 5380 | break; |
| 5381 | } |
| 5382 | list_move(&nbl->nbl_lru, &reaplist); |
| 5383 | list_del_init(&nbl->nbl_list); |
| 5384 | } |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 5385 | spin_unlock(&nn->blocked_locks_lock); |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 5386 | |
| 5387 | while (!list_empty(&reaplist)) { |
Naofumi Honda | 64ebe124 | 2017-11-09 10:57:16 -0500 | [diff] [blame] | 5388 | nbl = list_first_entry(&reaplist, |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 5389 | struct nfsd4_blocked_lock, nbl_lru); |
| 5390 | list_del_init(&nbl->nbl_lru); |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 5391 | free_blocked_lock(nbl); |
| 5392 | } |
J. Bruce Fields | 03f318c | 2018-06-08 12:28:47 -0400 | [diff] [blame] | 5393 | out: |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 5394 | new_timeo = max_t(time64_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT); |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 5395 | return new_timeo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5396 | } |
| 5397 | |
Harvey Harrison | a254b24 | 2008-02-20 12:49:00 -0800 | [diff] [blame] | 5398 | static struct workqueue_struct *laundry_wq; |
| 5399 | static void laundromat_main(struct work_struct *); |
Harvey Harrison | a254b24 | 2008-02-20 12:49:00 -0800 | [diff] [blame] | 5400 | |
| 5401 | static void |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 5402 | laundromat_main(struct work_struct *laundry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5403 | { |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 5404 | time64_t t; |
Geliang Tang | 2e55f3a | 2016-01-01 22:06:28 +0800 | [diff] [blame] | 5405 | struct delayed_work *dwork = to_delayed_work(laundry); |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 5406 | struct nfsd_net *nn = container_of(dwork, struct nfsd_net, |
| 5407 | laundromat_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5408 | |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 5409 | t = nfs4_laundromat(nn); |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 5410 | dprintk("NFSD: laundromat_main - sleeping for %lld seconds\n", t); |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 5411 | queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5412 | } |
| 5413 | |
Jeff Layton | 8fcd461 | 2015-07-30 06:57:46 -0400 | [diff] [blame] | 5414 | 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] | 5415 | { |
Jeff Layton | 8fcd461 | 2015-07-30 06:57:46 -0400 | [diff] [blame] | 5416 | if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle)) |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 5417 | return nfserr_bad_stateid; |
| 5418 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5419 | } |
| 5420 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5421 | static inline int |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 5422 | access_permit_read(struct nfs4_ol_stateid *stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5423 | { |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 5424 | return test_access(NFS4_SHARE_ACCESS_READ, stp) || |
| 5425 | test_access(NFS4_SHARE_ACCESS_BOTH, stp) || |
| 5426 | test_access(NFS4_SHARE_ACCESS_WRITE, stp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5427 | } |
| 5428 | |
| 5429 | static inline int |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 5430 | access_permit_write(struct nfs4_ol_stateid *stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5431 | { |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 5432 | return test_access(NFS4_SHARE_ACCESS_WRITE, stp) || |
| 5433 | test_access(NFS4_SHARE_ACCESS_BOTH, stp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5434 | } |
| 5435 | |
| 5436 | static |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5437 | __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5438 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5439 | __be32 status = nfserr_openmode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5440 | |
J. Bruce Fields | 0292191 | 2010-07-29 15:16:59 -0400 | [diff] [blame] | 5441 | /* For lock stateid's, we test the parent open, not the lock: */ |
| 5442 | if (stp->st_openstp) |
| 5443 | stp = stp->st_openstp; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 5444 | if ((flags & WR_STATE) && !access_permit_write(stp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5445 | goto out; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 5446 | if ((flags & RD_STATE) && !access_permit_read(stp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5447 | goto out; |
| 5448 | status = nfs_ok; |
| 5449 | out: |
| 5450 | return status; |
| 5451 | } |
| 5452 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5453 | static inline __be32 |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 5454 | 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] | 5455 | { |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 5456 | if (ONE_STATEID(stateid) && (flags & RD_STATE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5457 | return nfs_ok; |
J. Bruce Fields | c87fb4a | 2015-08-06 12:47:02 -0400 | [diff] [blame] | 5458 | else if (opens_in_grace(net)) { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 5459 | /* Answer in remaining cases depends on existence of |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5460 | * conflicting state; so we must wait out the grace period. */ |
| 5461 | return nfserr_grace; |
| 5462 | } else if (flags & WR_STATE) |
| 5463 | return nfs4_share_conflict(current_fh, |
| 5464 | NFS4_SHARE_DENY_WRITE); |
| 5465 | else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */ |
| 5466 | return nfs4_share_conflict(current_fh, |
| 5467 | NFS4_SHARE_DENY_READ); |
| 5468 | } |
| 5469 | |
| 5470 | /* |
| 5471 | * Allow READ/WRITE during grace period on recovered state only for files |
| 5472 | * that are not able to provide mandatory locking. |
| 5473 | */ |
| 5474 | static inline int |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 5475 | grace_disallows_io(struct net *net, struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5476 | { |
J. Bruce Fields | c87fb4a | 2015-08-06 12:47:02 -0400 | [diff] [blame] | 5477 | return opens_in_grace(net) && mandatory_lock(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5478 | } |
| 5479 | |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 5480 | 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] | 5481 | { |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 5482 | /* |
| 5483 | * When sessions are used the stateid generation number is ignored |
| 5484 | * when it is zero. |
| 5485 | */ |
J. Bruce Fields | 28dde24 | 2011-08-22 10:07:12 -0400 | [diff] [blame] | 5486 | if (has_session && in->si_generation == 0) |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 5487 | return nfs_ok; |
| 5488 | |
| 5489 | if (in->si_generation == ref->si_generation) |
| 5490 | return nfs_ok; |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 5491 | |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 5492 | /* 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] | 5493 | if (nfsd4_stateid_generation_after(in, ref)) |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 5494 | return nfserr_bad_stateid; |
| 5495 | /* |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 5496 | * However, we could see a stateid from the past, even from a |
| 5497 | * non-buggy client. For example, if the client sends a lock |
| 5498 | * while some IO is outstanding, the lock may bump si_generation |
| 5499 | * while the IO is still in flight. The client could avoid that |
| 5500 | * situation by waiting for responses on all the IO requests, |
| 5501 | * but better performance may result in retrying IO that |
| 5502 | * receives an old_stateid error if requests are rarely |
| 5503 | * reordered in flight: |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 5504 | */ |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 5505 | return nfserr_old_stateid; |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 5506 | } |
| 5507 | |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 5508 | static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session) |
| 5509 | { |
| 5510 | __be32 ret; |
| 5511 | |
| 5512 | spin_lock(&s->sc_lock); |
| 5513 | ret = nfsd4_verify_open_stid(s); |
| 5514 | if (ret == nfs_ok) |
| 5515 | ret = check_stateid_generation(in, &s->sc_stateid, has_session); |
| 5516 | spin_unlock(&s->sc_lock); |
| 5517 | return ret; |
| 5518 | } |
| 5519 | |
Christoph Hellwig | ebe9cb3 | 2015-04-28 15:41:15 +0200 | [diff] [blame] | 5520 | static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols) |
| 5521 | { |
| 5522 | if (ols->st_stateowner->so_is_open_owner && |
| 5523 | !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED)) |
| 5524 | return nfserr_bad_stateid; |
| 5525 | return nfs_ok; |
| 5526 | } |
| 5527 | |
Chuck Lever | 7df302f | 2012-05-29 13:56:37 -0400 | [diff] [blame] | 5528 | static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid) |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 5529 | { |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 5530 | struct nfs4_stid *s; |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5531 | __be32 status = nfserr_bad_stateid; |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 5532 | |
Andrew Elble | ae254dac | 2017-11-09 13:41:10 -0500 | [diff] [blame] | 5533 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) || |
| 5534 | CLOSE_STATEID(stateid)) |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5535 | return status; |
J. Bruce Fields | 663e36f | 2020-03-19 10:18:49 -0400 | [diff] [blame] | 5536 | if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5537 | return status; |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5538 | spin_lock(&cl->cl_lock); |
| 5539 | s = find_stateid_locked(cl, stateid); |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 5540 | if (!s) |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5541 | goto out_unlock; |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 5542 | status = nfsd4_stid_check_stateid_generation(stateid, s, 1); |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 5543 | if (status) |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5544 | goto out_unlock; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 5545 | switch (s->sc_type) { |
| 5546 | case NFS4_DELEG_STID: |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5547 | status = nfs_ok; |
| 5548 | break; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 5549 | case NFS4_REVOKED_DELEG_STID: |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5550 | status = nfserr_deleg_revoked; |
| 5551 | break; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 5552 | case NFS4_OPEN_STID: |
| 5553 | case NFS4_LOCK_STID: |
Christoph Hellwig | ebe9cb3 | 2015-04-28 15:41:15 +0200 | [diff] [blame] | 5554 | status = nfsd4_check_openowner_confirmed(openlockstateid(s)); |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5555 | break; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 5556 | default: |
| 5557 | printk("unknown stateid type %x\n", s->sc_type); |
Trond Myklebust | b0fc29d | 2014-07-16 10:31:59 -0400 | [diff] [blame] | 5558 | /* Fallthrough */ |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 5559 | case NFS4_CLOSED_STID: |
Trond Myklebust | b0fc29d | 2014-07-16 10:31:59 -0400 | [diff] [blame] | 5560 | case NFS4_CLOSED_DELEG_STID: |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5561 | status = nfserr_bad_stateid; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 5562 | } |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5563 | out_unlock: |
| 5564 | spin_unlock(&cl->cl_lock); |
| 5565 | return status; |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 5566 | } |
| 5567 | |
Christoph Hellwig | cd61c52 | 2014-08-14 08:44:57 +0200 | [diff] [blame] | 5568 | __be32 |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 5569 | nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate, |
| 5570 | stateid_t *stateid, unsigned char typemask, |
| 5571 | struct nfs4_stid **s, struct nfsd_net *nn) |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5572 | { |
J. Bruce Fields | 0eb6f20 | 2013-03-12 17:36:17 -0400 | [diff] [blame] | 5573 | __be32 status; |
Andrew Elble | 95da1b3a | 2017-11-03 14:06:31 -0400 | [diff] [blame] | 5574 | bool return_revoked = false; |
| 5575 | |
| 5576 | /* |
| 5577 | * only return revoked delegations if explicitly asked. |
| 5578 | * otherwise we report revoked or bad_stateid status. |
| 5579 | */ |
| 5580 | if (typemask & NFS4_REVOKED_DELEG_STID) |
| 5581 | return_revoked = true; |
| 5582 | else if (typemask & NFS4_DELEG_STID) |
| 5583 | typemask |= NFS4_REVOKED_DELEG_STID; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5584 | |
Andrew Elble | ae254dac | 2017-11-09 13:41:10 -0500 | [diff] [blame] | 5585 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) || |
| 5586 | CLOSE_STATEID(stateid)) |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5587 | return nfserr_bad_stateid; |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 5588 | status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn, |
| 5589 | false); |
Trond Myklebust | a8a7c67 | 2014-03-29 14:43:38 -0400 | [diff] [blame] | 5590 | if (status == nfserr_stale_clientid) { |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 5591 | if (cstate->session) |
Trond Myklebust | a8a7c67 | 2014-03-29 14:43:38 -0400 | [diff] [blame] | 5592 | return nfserr_bad_stateid; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5593 | return nfserr_stale_stateid; |
Trond Myklebust | a8a7c67 | 2014-03-29 14:43:38 -0400 | [diff] [blame] | 5594 | } |
J. Bruce Fields | 0eb6f20 | 2013-03-12 17:36:17 -0400 | [diff] [blame] | 5595 | if (status) |
| 5596 | return status; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 5597 | *s = find_stateid_by_type(cstate->clp, stateid, typemask); |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5598 | if (!*s) |
| 5599 | return nfserr_bad_stateid; |
Andrew Elble | 95da1b3a | 2017-11-03 14:06:31 -0400 | [diff] [blame] | 5600 | if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) { |
| 5601 | nfs4_put_stid(*s); |
| 5602 | if (cstate->minorversion) |
| 5603 | return nfserr_deleg_revoked; |
| 5604 | return nfserr_bad_stateid; |
| 5605 | } |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5606 | return nfs_ok; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5607 | } |
| 5608 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5609 | static struct nfsd_file * |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5610 | nfs4_find_file(struct nfs4_stid *s, int flags) |
| 5611 | { |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5612 | if (!s) |
| 5613 | return NULL; |
| 5614 | |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5615 | switch (s->sc_type) { |
| 5616 | case NFS4_DELEG_STID: |
| 5617 | if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file)) |
| 5618 | return NULL; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5619 | return nfsd_file_get(s->sc_file->fi_deleg_file); |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5620 | case NFS4_OPEN_STID: |
| 5621 | case NFS4_LOCK_STID: |
| 5622 | if (flags & RD_STATE) |
| 5623 | return find_readable_file(s->sc_file); |
| 5624 | else |
| 5625 | return find_writeable_file(s->sc_file); |
| 5626 | break; |
| 5627 | } |
| 5628 | |
| 5629 | return NULL; |
| 5630 | } |
| 5631 | |
| 5632 | static __be32 |
J. Bruce Fields | d8836f7 | 2018-11-05 20:04:06 -0500 | [diff] [blame] | 5633 | nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags) |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5634 | { |
| 5635 | __be32 status; |
| 5636 | |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5637 | status = nfsd4_check_openowner_confirmed(ols); |
| 5638 | if (status) |
| 5639 | return status; |
| 5640 | return nfs4_check_openmode(ols, flags); |
| 5641 | } |
| 5642 | |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5643 | static __be32 |
| 5644 | 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] | 5645 | struct nfsd_file **nfp, int flags) |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5646 | { |
| 5647 | int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5648 | struct nfsd_file *nf; |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5649 | __be32 status; |
| 5650 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5651 | nf = nfs4_find_file(s, flags); |
| 5652 | if (nf) { |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5653 | status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry, |
| 5654 | acc | NFSD_MAY_OWNER_OVERRIDE); |
Jeff Layton | 5c4583b | 2019-08-18 14:18:54 -0400 | [diff] [blame] | 5655 | if (status) { |
| 5656 | nfsd_file_put(nf); |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5657 | goto out; |
Jeff Layton | 5c4583b | 2019-08-18 14:18:54 -0400 | [diff] [blame] | 5658 | } |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5659 | } else { |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5660 | status = nfsd_file_acquire(rqstp, fhp, acc, &nf); |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5661 | if (status) |
| 5662 | return status; |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5663 | } |
Jeff Layton | 5c4583b | 2019-08-18 14:18:54 -0400 | [diff] [blame] | 5664 | *nfp = nf; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5665 | out: |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5666 | return status; |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5667 | } |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 5668 | static void |
| 5669 | _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps) |
| 5670 | { |
| 5671 | WARN_ON_ONCE(cps->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID); |
| 5672 | if (!refcount_dec_and_test(&cps->cp_stateid.sc_count)) |
| 5673 | return; |
| 5674 | list_del(&cps->cp_list); |
| 5675 | idr_remove(&nn->s2s_cp_stateids, |
| 5676 | cps->cp_stateid.stid.si_opaque.so_id); |
| 5677 | kfree(cps); |
| 5678 | } |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 5679 | /* |
| 5680 | * A READ from an inter server to server COPY will have a |
| 5681 | * copy stateid. Look up the copy notify stateid from the |
| 5682 | * idr structure and take a reference on it. |
| 5683 | */ |
Olga Kornievskaia | ce0887a | 2019-10-09 11:50:48 -0400 | [diff] [blame] | 5684 | __be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st, |
| 5685 | struct nfs4_client *clp, |
| 5686 | struct nfs4_cpntf_state **cps) |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 5687 | { |
| 5688 | copy_stateid_t *cps_t; |
| 5689 | struct nfs4_cpntf_state *state = NULL; |
| 5690 | |
| 5691 | if (st->si_opaque.so_clid.cl_id != nn->s2s_cp_cl_id) |
| 5692 | return nfserr_bad_stateid; |
| 5693 | spin_lock(&nn->s2s_cp_lock); |
| 5694 | cps_t = idr_find(&nn->s2s_cp_stateids, st->si_opaque.so_id); |
| 5695 | if (cps_t) { |
| 5696 | state = container_of(cps_t, struct nfs4_cpntf_state, |
| 5697 | cp_stateid); |
Dan Carpenter | 5277a79 | 2019-12-04 10:59:36 +0300 | [diff] [blame] | 5698 | if (state->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID) { |
| 5699 | state = NULL; |
| 5700 | goto unlock; |
| 5701 | } |
Olga Kornievskaia | ce0887a | 2019-10-09 11:50:48 -0400 | [diff] [blame] | 5702 | if (!clp) |
| 5703 | refcount_inc(&state->cp_stateid.sc_count); |
| 5704 | else |
| 5705 | _free_cpntf_state_locked(nn, state); |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 5706 | } |
Dan Carpenter | 5277a79 | 2019-12-04 10:59:36 +0300 | [diff] [blame] | 5707 | unlock: |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 5708 | spin_unlock(&nn->s2s_cp_lock); |
| 5709 | if (!state) |
| 5710 | return nfserr_bad_stateid; |
Olga Kornievskaia | ce0887a | 2019-10-09 11:50:48 -0400 | [diff] [blame] | 5711 | if (!clp && state) |
| 5712 | *cps = state; |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 5713 | return 0; |
| 5714 | } |
| 5715 | |
| 5716 | static __be32 find_cpntf_state(struct nfsd_net *nn, stateid_t *st, |
| 5717 | struct nfs4_stid **stid) |
| 5718 | { |
| 5719 | __be32 status; |
| 5720 | struct nfs4_cpntf_state *cps = NULL; |
| 5721 | struct nfsd4_compound_state cstate; |
| 5722 | |
Olga Kornievskaia | ce0887a | 2019-10-09 11:50:48 -0400 | [diff] [blame] | 5723 | status = manage_cpntf_state(nn, st, NULL, &cps); |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 5724 | if (status) |
| 5725 | return status; |
| 5726 | |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 5727 | cps->cpntf_time = ktime_get_boottime_seconds(); |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 5728 | memset(&cstate, 0, sizeof(cstate)); |
| 5729 | status = lookup_clientid(&cps->cp_p_clid, &cstate, nn, true); |
| 5730 | if (status) |
| 5731 | goto out; |
| 5732 | status = nfsd4_lookup_stateid(&cstate, &cps->cp_p_stateid, |
| 5733 | NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID, |
| 5734 | stid, nn); |
| 5735 | put_client_renew(cstate.clp); |
| 5736 | out: |
| 5737 | nfs4_put_cpntf_state(nn, cps); |
| 5738 | return status; |
| 5739 | } |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 5740 | |
| 5741 | void nfs4_put_cpntf_state(struct nfsd_net *nn, struct nfs4_cpntf_state *cps) |
| 5742 | { |
| 5743 | spin_lock(&nn->s2s_cp_lock); |
| 5744 | _free_cpntf_state_locked(nn, cps); |
| 5745 | spin_unlock(&nn->s2s_cp_lock); |
| 5746 | } |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5747 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5748 | /* |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5749 | * Checks for stateid operations |
| 5750 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5751 | __be32 |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5752 | nfs4_preprocess_stateid_op(struct svc_rqst *rqstp, |
Anna Schumaker | aa0d6ae | 2015-12-03 12:59:51 +0100 | [diff] [blame] | 5753 | struct nfsd4_compound_state *cstate, struct svc_fh *fhp, |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 5754 | stateid_t *stateid, int flags, struct nfsd_file **nfp, |
| 5755 | struct nfs4_stid **cstid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5756 | { |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5757 | struct inode *ino = d_inode(fhp->fh_dentry); |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5758 | struct net *net = SVC_NET(rqstp); |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5759 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5760 | struct nfs4_stid *s = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5761 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5762 | |
Jeff Layton | 5c4583b | 2019-08-18 14:18:54 -0400 | [diff] [blame] | 5763 | if (nfp) |
| 5764 | *nfp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5765 | |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 5766 | if (grace_disallows_io(net, ino)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5767 | return nfserr_grace; |
| 5768 | |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5769 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) { |
| 5770 | status = check_special_stateids(net, fhp, stateid, flags); |
| 5771 | goto done; |
| 5772 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5773 | |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 5774 | status = nfsd4_lookup_stateid(cstate, stateid, |
Jeff Layton | db24b3b | 2014-06-30 11:48:36 -0400 | [diff] [blame] | 5775 | NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID, |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 5776 | &s, nn); |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 5777 | if (status == nfserr_bad_stateid) |
| 5778 | status = find_cpntf_state(nn, stateid, &s); |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5779 | if (status) |
Trond Myklebust | c2d1d6a | 2014-07-30 08:27:25 -0400 | [diff] [blame] | 5780 | return status; |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 5781 | status = nfsd4_stid_check_stateid_generation(stateid, s, |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5782 | nfsd4_has_session(cstate)); |
J. Bruce Fields | 69064a2 | 2011-09-09 11:54:57 -0400 | [diff] [blame] | 5783 | if (status) |
| 5784 | goto out; |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5785 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 5786 | switch (s->sc_type) { |
| 5787 | case NFS4_DELEG_STID: |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5788 | status = nfs4_check_delegmode(delegstateid(s), flags); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 5789 | break; |
| 5790 | case NFS4_OPEN_STID: |
| 5791 | case NFS4_LOCK_STID: |
J. Bruce Fields | d8836f7 | 2018-11-05 20:04:06 -0500 | [diff] [blame] | 5792 | status = nfs4_check_olstateid(openlockstateid(s), flags); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 5793 | break; |
| 5794 | default: |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 5795 | status = nfserr_bad_stateid; |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5796 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5797 | } |
Jeff Layton | 8fcd461 | 2015-07-30 06:57:46 -0400 | [diff] [blame] | 5798 | if (status) |
| 5799 | goto out; |
| 5800 | status = nfs4_check_fh(fhp, s); |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5801 | |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5802 | done: |
Jeff Layton | 5c4583b | 2019-08-18 14:18:54 -0400 | [diff] [blame] | 5803 | if (status == nfs_ok && nfp) |
| 5804 | status = nfs4_check_file(rqstp, fhp, s, nfp, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5805 | out: |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 5806 | if (s) { |
| 5807 | if (!status && cstid) |
| 5808 | *cstid = s; |
| 5809 | else |
| 5810 | nfs4_put_stid(s); |
| 5811 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5812 | return status; |
| 5813 | } |
| 5814 | |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 5815 | /* |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 5816 | * Test if the stateid is valid |
| 5817 | */ |
| 5818 | __be32 |
| 5819 | nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5820 | union nfsd4_op_u *u) |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 5821 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5822 | struct nfsd4_test_stateid *test_stateid = &u->test_stateid; |
Bryan Schumaker | 03cfb42 | 2012-01-27 10:22:49 -0500 | [diff] [blame] | 5823 | struct nfsd4_test_stateid_id *stateid; |
| 5824 | struct nfs4_client *cl = cstate->session->se_client; |
| 5825 | |
Bryan Schumaker | 03cfb42 | 2012-01-27 10:22:49 -0500 | [diff] [blame] | 5826 | 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] | 5827 | stateid->ts_id_status = |
| 5828 | nfsd4_validate_stateid(cl, &stateid->ts_id_stateid); |
Bryan Schumaker | 03cfb42 | 2012-01-27 10:22:49 -0500 | [diff] [blame] | 5829 | |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 5830 | return nfs_ok; |
| 5831 | } |
| 5832 | |
Chuck Lever | 4269139 | 2016-08-11 10:37:30 -0400 | [diff] [blame] | 5833 | static __be32 |
| 5834 | nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s) |
| 5835 | { |
| 5836 | struct nfs4_ol_stateid *stp = openlockstateid(s); |
| 5837 | __be32 ret; |
| 5838 | |
Trond Myklebust | 659aefb | 2017-11-03 08:00:13 -0400 | [diff] [blame] | 5839 | ret = nfsd4_lock_ol_stateid(stp); |
| 5840 | if (ret) |
| 5841 | goto out_put_stid; |
Chuck Lever | 4269139 | 2016-08-11 10:37:30 -0400 | [diff] [blame] | 5842 | |
| 5843 | ret = check_stateid_generation(stateid, &s->sc_stateid, 1); |
| 5844 | if (ret) |
| 5845 | goto out; |
| 5846 | |
| 5847 | ret = nfserr_locks_held; |
| 5848 | if (check_for_locks(stp->st_stid.sc_file, |
| 5849 | lockowner(stp->st_stateowner))) |
| 5850 | goto out; |
| 5851 | |
| 5852 | release_lock_stateid(stp); |
| 5853 | ret = nfs_ok; |
| 5854 | |
| 5855 | out: |
| 5856 | mutex_unlock(&stp->st_mutex); |
Trond Myklebust | 659aefb | 2017-11-03 08:00:13 -0400 | [diff] [blame] | 5857 | out_put_stid: |
Chuck Lever | 4269139 | 2016-08-11 10:37:30 -0400 | [diff] [blame] | 5858 | nfs4_put_stid(s); |
| 5859 | return ret; |
| 5860 | } |
| 5861 | |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 5862 | __be32 |
| 5863 | nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5864 | union nfsd4_op_u *u) |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 5865 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5866 | struct nfsd4_free_stateid *free_stateid = &u->free_stateid; |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 5867 | stateid_t *stateid = &free_stateid->fr_stateid; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 5868 | struct nfs4_stid *s; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 5869 | struct nfs4_delegation *dp; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5870 | struct nfs4_client *cl = cstate->session->se_client; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 5871 | __be32 ret = nfserr_bad_stateid; |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 5872 | |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5873 | spin_lock(&cl->cl_lock); |
| 5874 | s = find_stateid_locked(cl, stateid); |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 5875 | if (!s) |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5876 | goto out_unlock; |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 5877 | spin_lock(&s->sc_lock); |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 5878 | switch (s->sc_type) { |
| 5879 | case NFS4_DELEG_STID: |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 5880 | ret = nfserr_locks_held; |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5881 | break; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 5882 | case NFS4_OPEN_STID: |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5883 | ret = check_stateid_generation(stateid, &s->sc_stateid, 1); |
| 5884 | if (ret) |
| 5885 | break; |
| 5886 | ret = nfserr_locks_held; |
| 5887 | break; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 5888 | case NFS4_LOCK_STID: |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 5889 | spin_unlock(&s->sc_lock); |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 5890 | refcount_inc(&s->sc_count); |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5891 | spin_unlock(&cl->cl_lock); |
Chuck Lever | 4269139 | 2016-08-11 10:37:30 -0400 | [diff] [blame] | 5892 | ret = nfsd4_free_lock_stateid(stateid, s); |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5893 | goto out; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 5894 | case NFS4_REVOKED_DELEG_STID: |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 5895 | spin_unlock(&s->sc_lock); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 5896 | dp = delegstateid(s); |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 5897 | list_del_init(&dp->dl_recall_lru); |
| 5898 | spin_unlock(&cl->cl_lock); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 5899 | nfs4_put_stid(s); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 5900 | ret = nfs_ok; |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5901 | goto out; |
| 5902 | /* Default falls through and returns nfserr_bad_stateid */ |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 5903 | } |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 5904 | spin_unlock(&s->sc_lock); |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5905 | out_unlock: |
| 5906 | spin_unlock(&cl->cl_lock); |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 5907 | out: |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 5908 | return ret; |
| 5909 | } |
| 5910 | |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 5911 | static inline int |
| 5912 | setlkflg (int type) |
| 5913 | { |
| 5914 | return (type == NFS4_READW_LT || type == NFS4_READ_LT) ? |
| 5915 | RD_STATE : WR_STATE; |
| 5916 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5917 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5918 | 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] | 5919 | { |
| 5920 | struct svc_fh *current_fh = &cstate->current_fh; |
| 5921 | struct nfs4_stateowner *sop = stp->st_stateowner; |
| 5922 | __be32 status; |
| 5923 | |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 5924 | status = nfsd4_check_seqid(cstate, sop, seqid); |
| 5925 | if (status) |
| 5926 | return status; |
Trond Myklebust | 9271d7e | 2017-11-03 08:00:15 -0400 | [diff] [blame] | 5927 | status = nfsd4_lock_ol_stateid(stp); |
| 5928 | if (status != nfs_ok) |
| 5929 | return status; |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 5930 | 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] | 5931 | if (status == nfs_ok) |
| 5932 | status = nfs4_check_fh(current_fh, &stp->st_stid); |
| 5933 | if (status != nfs_ok) |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 5934 | mutex_unlock(&stp->st_mutex); |
Jeff Layton | 35a92fe | 2015-09-17 07:47:08 -0400 | [diff] [blame] | 5935 | return status; |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 5936 | } |
| 5937 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5938 | /* |
| 5939 | * Checks for sequence id mutating operations. |
| 5940 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5941 | static __be32 |
Benny Halevy | dd453df | 2009-04-03 08:28:41 +0300 | [diff] [blame] | 5942 | nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, |
J. Bruce Fields | 2288d0e | 2011-09-06 15:50:21 -0400 | [diff] [blame] | 5943 | stateid_t *stateid, char typemask, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5944 | struct nfs4_ol_stateid **stpp, |
| 5945 | struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5946 | { |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 5947 | __be32 status; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5948 | struct nfs4_stid *s; |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 5949 | struct nfs4_ol_stateid *stp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5950 | |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 5951 | dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__, |
| 5952 | seqid, STATEID_VAL(stateid)); |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 5953 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5954 | *stpp = NULL; |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 5955 | status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn); |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 5956 | if (status) |
| 5957 | return status; |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 5958 | stp = openlockstateid(s); |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 5959 | nfsd4_cstate_assign_replay(cstate, stp->st_stateowner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5960 | |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 5961 | status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp); |
Trond Myklebust | fd91101 | 2014-07-29 21:34:24 -0400 | [diff] [blame] | 5962 | if (!status) |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 5963 | *stpp = stp; |
Trond Myklebust | fd91101 | 2014-07-29 21:34:24 -0400 | [diff] [blame] | 5964 | else |
| 5965 | nfs4_put_stid(&stp->st_stid); |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 5966 | return status; |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 5967 | } |
J. Bruce Fields | 39325bd | 2007-11-26 17:06:39 -0500 | [diff] [blame] | 5968 | |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5969 | static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, |
| 5970 | 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] | 5971 | { |
| 5972 | __be32 status; |
| 5973 | struct nfs4_openowner *oo; |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 5974 | struct nfs4_ol_stateid *stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5975 | |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 5976 | status = nfs4_preprocess_seqid_op(cstate, seqid, stateid, |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 5977 | NFS4_OPEN_STID, &stp, nn); |
J. Bruce Fields | 7a8711c | 2011-09-02 09:03:37 -0400 | [diff] [blame] | 5978 | if (status) |
| 5979 | return status; |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 5980 | oo = openowner(stp->st_stateowner); |
| 5981 | if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) { |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 5982 | mutex_unlock(&stp->st_mutex); |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 5983 | nfs4_put_stid(&stp->st_stid); |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 5984 | return nfserr_bad_stateid; |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 5985 | } |
| 5986 | *stpp = stp; |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 5987 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5988 | } |
| 5989 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5990 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 5991 | nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5992 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5993 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5994 | struct nfsd4_open_confirm *oc = &u->open_confirm; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5995 | __be32 status; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5996 | struct nfs4_openowner *oo; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5997 | struct nfs4_ol_stateid *stp; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5998 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5999 | |
Al Viro | a6a9f18 | 2013-09-16 10:57:01 -0400 | [diff] [blame] | 6000 | dprintk("NFSD: nfsd4_open_confirm on file %pd\n", |
| 6001 | cstate->current_fh.fh_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6002 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6003 | status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0); |
J. Bruce Fields | a8cddc5 | 2006-06-30 01:56:13 -0700 | [diff] [blame] | 6004 | if (status) |
| 6005 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6006 | |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 6007 | status = nfs4_preprocess_seqid_op(cstate, |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6008 | oc->oc_seqid, &oc->oc_req_stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6009 | NFS4_OPEN_STID, &stp, nn); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 6010 | if (status) |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 6011 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6012 | oo = openowner(stp->st_stateowner); |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 6013 | status = nfserr_bad_stateid; |
Jeff Layton | 35a92fe | 2015-09-17 07:47:08 -0400 | [diff] [blame] | 6014 | if (oo->oo_flags & NFS4_OO_CONFIRMED) { |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 6015 | mutex_unlock(&stp->st_mutex); |
Trond Myklebust | 2585fc7 | 2014-07-29 21:34:21 -0400 | [diff] [blame] | 6016 | goto put_stateid; |
Jeff Layton | 35a92fe | 2015-09-17 07:47:08 -0400 | [diff] [blame] | 6017 | } |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 6018 | oo->oo_flags |= NFS4_OO_CONFIRMED; |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 6019 | nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid); |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 6020 | mutex_unlock(&stp->st_mutex); |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 6021 | dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n", |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 6022 | __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid)); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 6023 | |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 6024 | nfsd4_client_record_create(oo->oo_owner.so_client); |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 6025 | status = nfs_ok; |
Trond Myklebust | 2585fc7 | 2014-07-29 21:34:21 -0400 | [diff] [blame] | 6026 | put_stateid: |
| 6027 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6028 | out: |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 6029 | nfsd4_bump_seqid(cstate, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6030 | return status; |
| 6031 | } |
| 6032 | |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 6033 | 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] | 6034 | { |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 6035 | if (!test_access(access, stp)) |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 6036 | return; |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 6037 | nfs4_file_put_access(stp->st_stid.sc_file, access); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 6038 | clear_access(access, stp); |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 6039 | } |
J. Bruce Fields | f197c27 | 2011-06-29 08:23:50 -0400 | [diff] [blame] | 6040 | |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 6041 | static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access) |
| 6042 | { |
| 6043 | switch (to_access) { |
| 6044 | case NFS4_SHARE_ACCESS_READ: |
| 6045 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE); |
| 6046 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); |
| 6047 | break; |
| 6048 | case NFS4_SHARE_ACCESS_WRITE: |
| 6049 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ); |
| 6050 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); |
| 6051 | break; |
| 6052 | case NFS4_SHARE_ACCESS_BOTH: |
| 6053 | break; |
| 6054 | default: |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 6055 | WARN_ON_ONCE(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6056 | } |
| 6057 | } |
| 6058 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6059 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6060 | nfsd4_open_downgrade(struct svc_rqst *rqstp, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6061 | struct nfsd4_compound_state *cstate, union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6062 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6063 | struct nfsd4_open_downgrade *od = &u->open_downgrade; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6064 | __be32 status; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 6065 | struct nfs4_ol_stateid *stp; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6066 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6067 | |
Al Viro | a6a9f18 | 2013-09-16 10:57:01 -0400 | [diff] [blame] | 6068 | dprintk("NFSD: nfsd4_open_downgrade on file %pd\n", |
| 6069 | cstate->current_fh.fh_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6070 | |
J. Bruce Fields | c30e92d | 2011-10-10 17:34:31 -0400 | [diff] [blame] | 6071 | /* We don't yet support WANT bits: */ |
Benny Halevy | 2c8bd7e | 2012-02-16 20:57:09 +0200 | [diff] [blame] | 6072 | if (od->od_deleg_want) |
| 6073 | dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__, |
| 6074 | od->od_deleg_want); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6075 | |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 6076 | status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6077 | &od->od_stateid, &stp, nn); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 6078 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6079 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6080 | status = nfserr_inval; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 6081 | if (!test_access(od->od_share_access, stp)) { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 6082 | 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] | 6083 | stp->st_access_bmap, od->od_share_access); |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 6084 | goto put_stateid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6085 | } |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 6086 | if (!test_deny(od->od_share_deny, stp)) { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 6087 | 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] | 6088 | stp->st_deny_bmap, od->od_share_deny); |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 6089 | goto put_stateid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6090 | } |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 6091 | nfs4_stateid_downgrade(stp, od->od_share_access); |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 6092 | reset_union_bmap_deny(od->od_share_deny, stp); |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 6093 | nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6094 | status = nfs_ok; |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 6095 | put_stateid: |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 6096 | mutex_unlock(&stp->st_mutex); |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 6097 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6098 | out: |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 6099 | nfsd4_bump_seqid(cstate, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6100 | return status; |
| 6101 | } |
| 6102 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 6103 | static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s) |
| 6104 | { |
Trond Myklebust | acf9295 | 2014-06-30 11:48:37 -0400 | [diff] [blame] | 6105 | struct nfs4_client *clp = s->st_stid.sc_client; |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 6106 | bool unhashed; |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 6107 | LIST_HEAD(reaplist); |
Trond Myklebust | acf9295 | 2014-06-30 11:48:37 -0400 | [diff] [blame] | 6108 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 6109 | spin_lock(&clp->cl_lock); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 6110 | unhashed = unhash_open_stateid(s, &reaplist); |
Trond Myklebust | acf9295 | 2014-06-30 11:48:37 -0400 | [diff] [blame] | 6111 | |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 6112 | if (clp->cl_minorversion) { |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 6113 | if (unhashed) |
| 6114 | put_ol_stateid_locked(s, &reaplist); |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 6115 | spin_unlock(&clp->cl_lock); |
| 6116 | free_ol_stateid_reaplist(&reaplist); |
| 6117 | } else { |
| 6118 | spin_unlock(&clp->cl_lock); |
| 6119 | free_ol_stateid_reaplist(&reaplist); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 6120 | if (unhashed) |
| 6121 | move_to_close_lru(s, clp->net); |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 6122 | } |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 6123 | } |
| 6124 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6125 | /* |
| 6126 | * nfs4_unlock_state() called after encode |
| 6127 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6128 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6129 | nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6130 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6131 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6132 | struct nfsd4_close *close = &u->close; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6133 | __be32 status; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 6134 | struct nfs4_ol_stateid *stp; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6135 | struct net *net = SVC_NET(rqstp); |
| 6136 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6137 | |
Al Viro | a6a9f18 | 2013-09-16 10:57:01 -0400 | [diff] [blame] | 6138 | dprintk("NFSD: nfsd4_close on file %pd\n", |
| 6139 | cstate->current_fh.fh_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6140 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 6141 | status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid, |
| 6142 | &close->cl_stateid, |
| 6143 | NFS4_OPEN_STID|NFS4_CLOSED_STID, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6144 | &stp, nn); |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 6145 | nfsd4_bump_seqid(cstate, status); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 6146 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6147 | goto out; |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 6148 | |
| 6149 | stp->st_stid.sc_type = NFS4_CLOSED_STID; |
Jeff Layton | bd2deca | 2018-02-15 15:16:18 -0500 | [diff] [blame] | 6150 | |
| 6151 | /* |
| 6152 | * Technically we don't _really_ have to increment or copy it, since |
| 6153 | * it should just be gone after this operation and we clobber the |
| 6154 | * copied value below, but we continue to do so here just to ensure |
| 6155 | * that racing ops see that there was a state change. |
| 6156 | */ |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 6157 | nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6158 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 6159 | nfsd4_close_open_stateid(stp); |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 6160 | mutex_unlock(&stp->st_mutex); |
Trond Myklebust | 8a0b589 | 2014-07-29 21:34:20 -0400 | [diff] [blame] | 6161 | |
Jeff Layton | bd2deca | 2018-02-15 15:16:18 -0500 | [diff] [blame] | 6162 | /* v4.1+ suggests that we send a special stateid in here, since the |
| 6163 | * clients should just ignore this anyway. Since this is not useful |
| 6164 | * for v4.0 clients either, we set it to the special close_stateid |
| 6165 | * universally. |
| 6166 | * |
| 6167 | * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5 |
| 6168 | */ |
| 6169 | memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid)); |
Trond Myklebust | fb500a7 | 2017-11-03 08:00:12 -0400 | [diff] [blame] | 6170 | |
Trond Myklebust | 8a0b589 | 2014-07-29 21:34:20 -0400 | [diff] [blame] | 6171 | /* put reference from nfs4_preprocess_seqid_op */ |
| 6172 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6173 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6174 | return status; |
| 6175 | } |
| 6176 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6177 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6178 | nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6179 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6180 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6181 | struct nfsd4_delegreturn *dr = &u->delegreturn; |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 6182 | struct nfs4_delegation *dp; |
| 6183 | stateid_t *stateid = &dr->dr_stateid; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 6184 | struct nfs4_stid *s; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6185 | __be32 status; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6186 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6187 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6188 | if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 6189 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6190 | |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 6191 | status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn); |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 6192 | if (status) |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 6193 | goto out; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 6194 | dp = delegstateid(s); |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 6195 | 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] | 6196 | if (status) |
Trond Myklebust | fd91101 | 2014-07-29 21:34:24 -0400 | [diff] [blame] | 6197 | goto put_stateid; |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 6198 | |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 6199 | destroy_delegation(dp); |
Trond Myklebust | fd91101 | 2014-07-29 21:34:24 -0400 | [diff] [blame] | 6200 | put_stateid: |
| 6201 | nfs4_put_stid(&dp->dl_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6202 | out: |
| 6203 | return status; |
| 6204 | } |
| 6205 | |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 6206 | static inline u64 |
| 6207 | end_offset(u64 start, u64 len) |
| 6208 | { |
| 6209 | u64 end; |
| 6210 | |
| 6211 | end = start + len; |
| 6212 | return end >= start ? end: NFS4_MAX_UINT64; |
| 6213 | } |
| 6214 | |
| 6215 | /* last octet in a range */ |
| 6216 | static inline u64 |
| 6217 | last_byte_offset(u64 start, u64 len) |
| 6218 | { |
| 6219 | u64 end; |
| 6220 | |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 6221 | WARN_ON_ONCE(!len); |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 6222 | end = start + len; |
| 6223 | return end > start ? end - 1: NFS4_MAX_UINT64; |
| 6224 | } |
| 6225 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6226 | /* |
| 6227 | * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that |
| 6228 | * we can't properly handle lock requests that go beyond the (2^63 - 1)-th |
| 6229 | * byte, because of sign extension problems. Since NFSv4 calls for 64-bit |
| 6230 | * locking, this prevents us from being completely protocol-compliant. The |
| 6231 | * real solution to this problem is to start using unsigned file offsets in |
| 6232 | * the VFS, but this is a very deep change! |
| 6233 | */ |
| 6234 | static inline void |
| 6235 | nfs4_transform_lock_offset(struct file_lock *lock) |
| 6236 | { |
| 6237 | if (lock->fl_start < 0) |
| 6238 | lock->fl_start = OFFSET_MAX; |
| 6239 | if (lock->fl_end < 0) |
| 6240 | lock->fl_end = OFFSET_MAX; |
| 6241 | } |
| 6242 | |
Jeff Layton | cae80b3 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 6243 | static fl_owner_t |
| 6244 | nfsd4_fl_get_owner(fl_owner_t owner) |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6245 | { |
Jeff Layton | cae80b3 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 6246 | struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner; |
| 6247 | |
| 6248 | nfs4_get_stateowner(&lo->lo_owner); |
| 6249 | return owner; |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6250 | } |
| 6251 | |
Jeff Layton | cae80b3 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 6252 | static void |
| 6253 | nfsd4_fl_put_owner(fl_owner_t owner) |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6254 | { |
Jeff Layton | cae80b3 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 6255 | struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner; |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6256 | |
Jeff Layton | cae80b3 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 6257 | if (lo) |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6258 | nfs4_put_stateowner(&lo->lo_owner); |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6259 | } |
| 6260 | |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6261 | static void |
| 6262 | nfsd4_lm_notify(struct file_lock *fl) |
| 6263 | { |
| 6264 | struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner; |
| 6265 | struct net *net = lo->lo_owner.so_client->net; |
| 6266 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 6267 | struct nfsd4_blocked_lock *nbl = container_of(fl, |
| 6268 | struct nfsd4_blocked_lock, nbl_lock); |
| 6269 | bool queue = false; |
| 6270 | |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 6271 | /* 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] | 6272 | spin_lock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6273 | if (!list_empty(&nbl->nbl_list)) { |
| 6274 | list_del_init(&nbl->nbl_list); |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 6275 | list_del_init(&nbl->nbl_lru); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6276 | queue = true; |
| 6277 | } |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 6278 | spin_unlock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6279 | |
| 6280 | if (queue) |
| 6281 | nfsd4_run_cb(&nbl->nbl_cb); |
| 6282 | } |
| 6283 | |
Alexey Dobriyan | 7b02196 | 2009-09-21 17:01:12 -0700 | [diff] [blame] | 6284 | static const struct lock_manager_operations nfsd_posix_mng_ops = { |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6285 | .lm_notify = nfsd4_lm_notify, |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6286 | .lm_get_owner = nfsd4_fl_get_owner, |
| 6287 | .lm_put_owner = nfsd4_fl_put_owner, |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 6288 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6289 | |
| 6290 | static inline void |
| 6291 | nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny) |
| 6292 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6293 | struct nfs4_lockowner *lo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6294 | |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 6295 | if (fl->fl_lmops == &nfsd_posix_mng_ops) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6296 | lo = (struct nfs4_lockowner *) fl->fl_owner; |
J. Bruce Fields | 6f4859b | 2019-06-19 14:30:33 -0400 | [diff] [blame] | 6297 | xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner, |
| 6298 | GFP_KERNEL); |
J. Bruce Fields | 7c13f34 | 2011-08-30 22:15:47 -0400 | [diff] [blame] | 6299 | if (!deny->ld_owner.data) |
| 6300 | /* We just don't care that much */ |
| 6301 | goto nevermind; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6302 | deny->ld_clientid = lo->lo_owner.so_client->cl_clientid; |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 6303 | } else { |
J. Bruce Fields | 7c13f34 | 2011-08-30 22:15:47 -0400 | [diff] [blame] | 6304 | nevermind: |
| 6305 | deny->ld_owner.len = 0; |
| 6306 | deny->ld_owner.data = NULL; |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 6307 | deny->ld_clientid.cl_boot = 0; |
| 6308 | deny->ld_clientid.cl_id = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6309 | } |
| 6310 | deny->ld_start = fl->fl_start; |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 6311 | deny->ld_length = NFS4_MAX_UINT64; |
| 6312 | if (fl->fl_end != NFS4_MAX_UINT64) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6313 | deny->ld_length = fl->fl_end - fl->fl_start + 1; |
| 6314 | deny->ld_type = NFS4_READ_LT; |
| 6315 | if (fl->fl_type != F_RDLCK) |
| 6316 | deny->ld_type = NFS4_WRITE_LT; |
| 6317 | } |
| 6318 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6319 | static struct nfs4_lockowner * |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 6320 | find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6321 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6322 | unsigned int strhashval = ownerstr_hashval(owner); |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame] | 6323 | struct nfs4_stateowner *so; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6324 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 6325 | lockdep_assert_held(&clp->cl_lock); |
| 6326 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6327 | list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval], |
| 6328 | so_strhash) { |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame] | 6329 | if (so->so_is_open_owner) |
| 6330 | continue; |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 6331 | if (same_owner_str(so, owner)) |
| 6332 | return lockowner(nfs4_get_stateowner(so)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6333 | } |
| 6334 | return NULL; |
| 6335 | } |
| 6336 | |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6337 | static struct nfs4_lockowner * |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 6338 | find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner) |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6339 | { |
| 6340 | struct nfs4_lockowner *lo; |
| 6341 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6342 | spin_lock(&clp->cl_lock); |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 6343 | lo = find_lockowner_str_locked(clp, owner); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6344 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6345 | return lo; |
| 6346 | } |
| 6347 | |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 6348 | static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop) |
| 6349 | { |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6350 | unhash_lockowner_locked(lockowner(sop)); |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 6351 | } |
| 6352 | |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 6353 | static void nfs4_free_lockowner(struct nfs4_stateowner *sop) |
| 6354 | { |
| 6355 | struct nfs4_lockowner *lo = lockowner(sop); |
| 6356 | |
| 6357 | kmem_cache_free(lockowner_slab, lo); |
| 6358 | } |
| 6359 | |
| 6360 | static const struct nfs4_stateowner_operations lockowner_ops = { |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 6361 | .so_unhash = nfs4_unhash_lockowner, |
| 6362 | .so_free = nfs4_free_lockowner, |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 6363 | }; |
| 6364 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6365 | /* |
| 6366 | * Alloc a lock owner structure. |
| 6367 | * 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] | 6368 | * occurred. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6369 | * |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 6370 | * strhashval = ownerstr_hashval |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6371 | */ |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6372 | static struct nfs4_lockowner * |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6373 | alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, |
| 6374 | struct nfs4_ol_stateid *open_stp, |
| 6375 | struct nfsd4_lock *lock) |
| 6376 | { |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6377 | struct nfs4_lockowner *lo, *ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6378 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6379 | lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp); |
| 6380 | if (!lo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6381 | return NULL; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6382 | INIT_LIST_HEAD(&lo->lo_blocked); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6383 | INIT_LIST_HEAD(&lo->lo_owner.so_stateids); |
| 6384 | lo->lo_owner.so_is_open_owner = 0; |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6385 | lo->lo_owner.so_seqid = lock->lk_new_lock_seqid; |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 6386 | lo->lo_owner.so_ops = &lockowner_ops; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6387 | spin_lock(&clp->cl_lock); |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 6388 | ret = find_lockowner_str_locked(clp, &lock->lk_new_owner); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6389 | if (ret == NULL) { |
| 6390 | list_add(&lo->lo_owner.so_strhash, |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6391 | &clp->cl_ownerstr_hashtbl[strhashval]); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6392 | ret = lo; |
| 6393 | } else |
Kinglong Mee | d50ffde | 2015-07-16 12:05:07 +0800 | [diff] [blame] | 6394 | nfs4_free_stateowner(&lo->lo_owner); |
| 6395 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6396 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 340f0ba | 2015-03-23 11:02:30 -0400 | [diff] [blame] | 6397 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6398 | } |
| 6399 | |
Trond Myklebust | fd1fd68 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6400 | static struct nfs4_ol_stateid * |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 6401 | find_lock_stateid(const struct nfs4_lockowner *lo, |
| 6402 | const struct nfs4_ol_stateid *ost) |
Trond Myklebust | fd1fd68 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6403 | { |
| 6404 | struct nfs4_ol_stateid *lst; |
Trond Myklebust | fd1fd68 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6405 | |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 6406 | lockdep_assert_held(&ost->st_stid.sc_client->cl_lock); |
Trond Myklebust | fd1fd68 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6407 | |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 6408 | /* If ost is not hashed, ost->st_locks will not be valid */ |
| 6409 | if (!nfs4_ol_stateid_unhashed(ost)) |
| 6410 | list_for_each_entry(lst, &ost->st_locks, st_locks) { |
| 6411 | if (lst->st_stateowner == &lo->lo_owner) { |
| 6412 | refcount_inc(&lst->st_stid.sc_count); |
| 6413 | return lst; |
| 6414 | } |
Trond Myklebust | fd1fd68 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6415 | } |
Trond Myklebust | fd1fd68 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6416 | return NULL; |
| 6417 | } |
| 6418 | |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6419 | static struct nfs4_ol_stateid * |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 6420 | init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo, |
| 6421 | struct nfs4_file *fp, struct inode *inode, |
| 6422 | struct nfs4_ol_stateid *open_stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6423 | { |
J. Bruce Fields | d3b313a | 2011-09-15 15:02:41 -0400 | [diff] [blame] | 6424 | struct nfs4_client *clp = lo->lo_owner.so_client; |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6425 | struct nfs4_ol_stateid *retstp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6426 | |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6427 | mutex_init(&stp->st_mutex); |
Andrew Elble | 4f34bd0 | 2017-11-08 17:29:51 -0500 | [diff] [blame] | 6428 | mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX); |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6429 | retry: |
| 6430 | spin_lock(&clp->cl_lock); |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 6431 | if (nfs4_ol_stateid_unhashed(open_stp)) |
| 6432 | goto out_close; |
| 6433 | retstp = find_lock_stateid(lo, open_stp); |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6434 | if (retstp) |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 6435 | goto out_found; |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 6436 | refcount_inc(&stp->st_stid.sc_count); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 6437 | stp->st_stid.sc_type = NFS4_LOCK_STID; |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 6438 | stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 6439 | get_nfs4_file(fp); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 6440 | stp->st_stid.sc_file = fp; |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 6441 | stp->st_access_bmap = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6442 | stp->st_deny_bmap = open_stp->st_deny_bmap; |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 6443 | stp->st_openstp = open_stp; |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 6444 | spin_lock(&fp->fi_lock); |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 6445 | list_add(&stp->st_locks, &open_stp->st_locks); |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 6446 | list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 6447 | list_add(&stp->st_perfile, &fp->fi_stateids); |
| 6448 | spin_unlock(&fp->fi_lock); |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6449 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6450 | return stp; |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 6451 | out_found: |
| 6452 | spin_unlock(&clp->cl_lock); |
| 6453 | if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) { |
| 6454 | nfs4_put_stid(&retstp->st_stid); |
| 6455 | goto retry; |
| 6456 | } |
| 6457 | /* To keep mutex tracking happy */ |
| 6458 | mutex_unlock(&stp->st_mutex); |
| 6459 | return retstp; |
| 6460 | out_close: |
| 6461 | spin_unlock(&clp->cl_lock); |
| 6462 | mutex_unlock(&stp->st_mutex); |
| 6463 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6464 | } |
| 6465 | |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 6466 | static struct nfs4_ol_stateid * |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 6467 | find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi, |
| 6468 | struct inode *inode, struct nfs4_ol_stateid *ost, |
| 6469 | bool *new) |
| 6470 | { |
| 6471 | struct nfs4_stid *ns = NULL; |
| 6472 | struct nfs4_ol_stateid *lst; |
| 6473 | struct nfs4_openowner *oo = openowner(ost->st_stateowner); |
| 6474 | struct nfs4_client *clp = oo->oo_owner.so_client; |
| 6475 | |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6476 | *new = false; |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 6477 | spin_lock(&clp->cl_lock); |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 6478 | lst = find_lock_stateid(lo, ost); |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 6479 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6480 | if (lst != NULL) { |
| 6481 | if (nfsd4_lock_ol_stateid(lst) == nfs_ok) |
| 6482 | goto out; |
| 6483 | nfs4_put_stid(&lst->st_stid); |
| 6484 | } |
| 6485 | ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid); |
| 6486 | if (ns == NULL) |
| 6487 | return NULL; |
| 6488 | |
| 6489 | lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost); |
| 6490 | if (lst == openlockstateid(ns)) |
| 6491 | *new = true; |
| 6492 | else |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 6493 | nfs4_put_stid(ns); |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6494 | out: |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 6495 | return lst; |
| 6496 | } |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 6497 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 6498 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6499 | check_lock_length(u64 offset, u64 length) |
| 6500 | { |
Kinglong Mee | e796931 | 2015-07-13 17:34:19 +0800 | [diff] [blame] | 6501 | return ((length == 0) || ((length != NFS4_MAX_UINT64) && |
| 6502 | (length > ~offset))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6503 | } |
| 6504 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 6505 | 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] | 6506 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 6507 | struct nfs4_file *fp = lock_stp->st_stid.sc_file; |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 6508 | |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 6509 | lockdep_assert_held(&fp->fi_lock); |
| 6510 | |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 6511 | if (test_access(access, lock_stp)) |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 6512 | return; |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 6513 | __nfs4_file_get_access(fp, access); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 6514 | set_access(access, lock_stp); |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 6515 | } |
| 6516 | |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 6517 | static __be32 |
| 6518 | lookup_or_create_lock_state(struct nfsd4_compound_state *cstate, |
| 6519 | struct nfs4_ol_stateid *ost, |
| 6520 | struct nfsd4_lock *lock, |
Jeff Layton | dd25793 | 2016-08-11 10:37:39 -0400 | [diff] [blame] | 6521 | struct nfs4_ol_stateid **plst, bool *new) |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6522 | { |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6523 | __be32 status; |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 6524 | struct nfs4_file *fi = ost->st_stid.sc_file; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6525 | struct nfs4_openowner *oo = openowner(ost->st_stateowner); |
| 6526 | struct nfs4_client *cl = oo->oo_owner.so_client; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 6527 | struct inode *inode = d_inode(cstate->current_fh.fh_dentry); |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6528 | struct nfs4_lockowner *lo; |
Jeff Layton | dd25793 | 2016-08-11 10:37:39 -0400 | [diff] [blame] | 6529 | struct nfs4_ol_stateid *lst; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6530 | unsigned int strhashval; |
| 6531 | |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 6532 | lo = find_lockowner_str(cl, &lock->lk_new_owner); |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 6533 | if (!lo) { |
Kinglong Mee | 76f6c9e | 2015-07-13 17:35:10 +0800 | [diff] [blame] | 6534 | strhashval = ownerstr_hashval(&lock->lk_new_owner); |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 6535 | lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock); |
| 6536 | if (lo == NULL) |
| 6537 | return nfserr_jukebox; |
| 6538 | } else { |
| 6539 | /* with an existing lockowner, seqids must be the same */ |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6540 | status = nfserr_bad_seqid; |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 6541 | if (!cstate->minorversion && |
| 6542 | lock->lk_new_lock_seqid != lo->lo_owner.so_seqid) |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6543 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6544 | } |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 6545 | |
Jeff Layton | dd25793 | 2016-08-11 10:37:39 -0400 | [diff] [blame] | 6546 | lst = find_or_create_lock_stateid(lo, fi, inode, ost, new); |
| 6547 | if (lst == NULL) { |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6548 | status = nfserr_jukebox; |
| 6549 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6550 | } |
Jeff Layton | dd25793 | 2016-08-11 10:37:39 -0400 | [diff] [blame] | 6551 | |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6552 | status = nfs_ok; |
Jeff Layton | dd25793 | 2016-08-11 10:37:39 -0400 | [diff] [blame] | 6553 | *plst = lst; |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6554 | out: |
| 6555 | nfs4_put_stateowner(&lo->lo_owner); |
| 6556 | return status; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6557 | } |
| 6558 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6559 | /* |
| 6560 | * LOCK operation |
| 6561 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6562 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6563 | nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6564 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6565 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6566 | struct nfsd4_lock *lock = &u->lock; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6567 | struct nfs4_openowner *open_sop = NULL; |
| 6568 | struct nfs4_lockowner *lock_sop = NULL; |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 6569 | struct nfs4_ol_stateid *lock_stp = NULL; |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 6570 | struct nfs4_ol_stateid *open_stp = NULL; |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 6571 | struct nfs4_file *fp; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6572 | struct nfsd_file *nf = NULL; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6573 | struct nfsd4_blocked_lock *nbl = NULL; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6574 | struct file_lock *file_lock = NULL; |
| 6575 | struct file_lock *conflock = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6576 | __be32 status = 0; |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 6577 | int lkflg; |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 6578 | int err; |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6579 | bool new = false; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6580 | unsigned char fl_type; |
| 6581 | unsigned int fl_flags = FL_POSIX; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6582 | struct net *net = SVC_NET(rqstp); |
| 6583 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6584 | |
| 6585 | dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n", |
| 6586 | (long long) lock->lk_offset, |
| 6587 | (long long) lock->lk_length); |
| 6588 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6589 | if (check_lock_length(lock->lk_offset, lock->lk_length)) |
| 6590 | return nfserr_inval; |
| 6591 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6592 | if ((status = fh_verify(rqstp, &cstate->current_fh, |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 6593 | S_IFREG, NFSD_MAY_LOCK))) { |
Andy Adamson | a6f6ef2 | 2006-01-18 17:43:17 -0800 | [diff] [blame] | 6594 | dprintk("NFSD: nfsd4_lock: permission denied!\n"); |
| 6595 | return status; |
| 6596 | } |
| 6597 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6598 | if (lock->lk_is_new) { |
J. Bruce Fields | 684e563 | 2011-11-04 17:08:10 -0400 | [diff] [blame] | 6599 | if (nfsd4_has_session(cstate)) |
| 6600 | /* See rfc 5661 18.10.3: given clientid is ignored: */ |
Kinglong Mee | 76f6c9e | 2015-07-13 17:35:10 +0800 | [diff] [blame] | 6601 | memcpy(&lock->lk_new_clientid, |
J. Bruce Fields | 684e563 | 2011-11-04 17:08:10 -0400 | [diff] [blame] | 6602 | &cstate->session->se_client->cl_clientid, |
| 6603 | sizeof(clientid_t)); |
| 6604 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6605 | status = nfserr_stale_clientid; |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 6606 | if (STALE_CLIENTID(&lock->lk_new_clientid, nn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6607 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6608 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6609 | /* validate and update open stateid and open seqid */ |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 6610 | status = nfs4_preprocess_confirmed_seqid_op(cstate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6611 | lock->lk_new_open_seqid, |
| 6612 | &lock->lk_new_open_stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6613 | &open_stp, nn); |
NeilBrown | 3751517 | 2005-07-07 17:59:16 -0700 | [diff] [blame] | 6614 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6615 | goto out; |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 6616 | mutex_unlock(&open_stp->st_mutex); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6617 | open_sop = openowner(open_stp->st_stateowner); |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 6618 | status = nfserr_bad_stateid; |
J. Bruce Fields | 684e563 | 2011-11-04 17:08:10 -0400 | [diff] [blame] | 6619 | if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid, |
Kinglong Mee | 76f6c9e | 2015-07-13 17:35:10 +0800 | [diff] [blame] | 6620 | &lock->lk_new_clientid)) |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 6621 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6622 | status = lookup_or_create_lock_state(cstate, open_stp, lock, |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6623 | &lock_stp, &new); |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 6624 | } else { |
Benny Halevy | dd453df | 2009-04-03 08:28:41 +0300 | [diff] [blame] | 6625 | status = nfs4_preprocess_seqid_op(cstate, |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6626 | lock->lk_old_lock_seqid, |
| 6627 | &lock->lk_old_lock_stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6628 | NFS4_LOCK_STID, &lock_stp, nn); |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 6629 | } |
J. Bruce Fields | e1aaa89 | 2012-06-06 16:01:37 -0400 | [diff] [blame] | 6630 | if (status) |
| 6631 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6632 | lock_sop = lockowner(lock_stp->st_stateowner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6633 | |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 6634 | lkflg = setlkflg(lock->lk_type); |
| 6635 | status = nfs4_check_openmode(lock_stp, lkflg); |
| 6636 | if (status) |
| 6637 | goto out; |
| 6638 | |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 6639 | status = nfserr_grace; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6640 | if (locks_in_grace(net) && !lock->lk_reclaim) |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 6641 | goto out; |
| 6642 | status = nfserr_no_grace; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6643 | if (!locks_in_grace(net) && lock->lk_reclaim) |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 6644 | goto out; |
| 6645 | |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 6646 | fp = lock_stp->st_stid.sc_file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6647 | switch (lock->lk_type) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6648 | case NFS4_READW_LT: |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6649 | if (nfsd4_has_session(cstate)) |
| 6650 | fl_flags |= FL_SLEEP; |
| 6651 | /* Fallthrough */ |
| 6652 | case NFS4_READ_LT: |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 6653 | spin_lock(&fp->fi_lock); |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6654 | nf = find_readable_file_locked(fp); |
| 6655 | if (nf) |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 6656 | get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ); |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 6657 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6658 | fl_type = F_RDLCK; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 6659 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6660 | case NFS4_WRITEW_LT: |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6661 | if (nfsd4_has_session(cstate)) |
| 6662 | fl_flags |= FL_SLEEP; |
| 6663 | /* Fallthrough */ |
| 6664 | case NFS4_WRITE_LT: |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 6665 | spin_lock(&fp->fi_lock); |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6666 | nf = find_writeable_file_locked(fp); |
| 6667 | if (nf) |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 6668 | get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE); |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 6669 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6670 | fl_type = F_WRLCK; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 6671 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6672 | default: |
| 6673 | status = nfserr_inval; |
| 6674 | goto out; |
| 6675 | } |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6676 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6677 | if (!nf) { |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 6678 | status = nfserr_openmode; |
| 6679 | goto out; |
| 6680 | } |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6681 | |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6682 | nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn); |
| 6683 | if (!nbl) { |
| 6684 | dprintk("NFSD: %s: unable to allocate block!\n", __func__); |
| 6685 | status = nfserr_jukebox; |
| 6686 | goto out; |
| 6687 | } |
| 6688 | |
| 6689 | file_lock = &nbl->nbl_lock; |
| 6690 | file_lock->fl_type = fl_type; |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6691 | 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] | 6692 | file_lock->fl_pid = current->tgid; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6693 | file_lock->fl_file = nf->nf_file; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6694 | file_lock->fl_flags = fl_flags; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6695 | file_lock->fl_lmops = &nfsd_posix_mng_ops; |
| 6696 | file_lock->fl_start = lock->lk_offset; |
| 6697 | file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length); |
| 6698 | nfs4_transform_lock_offset(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6699 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6700 | conflock = locks_alloc_lock(); |
| 6701 | if (!conflock) { |
| 6702 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 6703 | status = nfserr_jukebox; |
| 6704 | goto out; |
| 6705 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6706 | |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6707 | if (fl_flags & FL_SLEEP) { |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 6708 | nbl->nbl_time = ktime_get_boottime_seconds(); |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 6709 | spin_lock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6710 | list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked); |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 6711 | list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru); |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 6712 | spin_unlock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6713 | } |
| 6714 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6715 | err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6716 | switch (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6717 | case 0: /* success! */ |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 6718 | 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] | 6719 | status = 0; |
J. Bruce Fields | 03f318c | 2018-06-08 12:28:47 -0400 | [diff] [blame] | 6720 | if (lock->lk_reclaim) |
| 6721 | nn->somebody_reclaimed = true; |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 6722 | break; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6723 | case FILE_LOCK_DEFERRED: |
| 6724 | nbl = NULL; |
| 6725 | /* Fallthrough */ |
| 6726 | case -EAGAIN: /* conflock holds conflicting lock */ |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 6727 | status = nfserr_denied; |
| 6728 | dprintk("NFSD: nfsd4_lock: conflicting lock found!\n"); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6729 | nfs4_set_lock_denied(conflock, &lock->lk_denied); |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 6730 | break; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6731 | case -EDEADLK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6732 | status = nfserr_deadlock; |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 6733 | break; |
J. Bruce Fields | 3e77246 | 2011-08-10 19:07:33 -0400 | [diff] [blame] | 6734 | default: |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 6735 | 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] | 6736 | status = nfserrno(err); |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 6737 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6738 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6739 | out: |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6740 | if (nbl) { |
| 6741 | /* dequeue it if we queued it before */ |
| 6742 | if (fl_flags & FL_SLEEP) { |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 6743 | spin_lock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6744 | list_del_init(&nbl->nbl_list); |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 6745 | list_del_init(&nbl->nbl_lru); |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 6746 | spin_unlock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6747 | } |
| 6748 | free_blocked_lock(nbl); |
| 6749 | } |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6750 | if (nf) |
| 6751 | nfsd_file_put(nf); |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6752 | if (lock_stp) { |
| 6753 | /* Bump seqid manually if the 4.0 replay owner is openowner */ |
| 6754 | if (cstate->replay_owner && |
| 6755 | cstate->replay_owner != &lock_sop->lo_owner && |
| 6756 | seqid_mutating_err(ntohl(status))) |
| 6757 | lock_sop->lo_owner.so_seqid++; |
| 6758 | |
| 6759 | /* |
| 6760 | * If this is a new, never-before-used stateid, and we are |
| 6761 | * returning an error, then just go ahead and release it. |
| 6762 | */ |
J. Bruce Fields | 2502072 | 2018-01-17 16:25:59 -0500 | [diff] [blame] | 6763 | if (status && new) |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6764 | release_lock_stateid(lock_stp); |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6765 | |
| 6766 | mutex_unlock(&lock_stp->st_mutex); |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6767 | |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 6768 | nfs4_put_stid(&lock_stp->st_stid); |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6769 | } |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 6770 | if (open_stp) |
| 6771 | nfs4_put_stid(&open_stp->st_stid); |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 6772 | nfsd4_bump_seqid(cstate, status); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6773 | if (conflock) |
| 6774 | locks_free_lock(conflock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6775 | return status; |
| 6776 | } |
| 6777 | |
| 6778 | /* |
J. Bruce Fields | 55ef127 | 2008-12-20 11:58:38 -0800 | [diff] [blame] | 6779 | * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN, |
| 6780 | * so we do a temporary open here just to get an open file to pass to |
| 6781 | * vfs_test_lock. (Arguably perhaps test_lock should be done with an |
| 6782 | * inode operation.) |
| 6783 | */ |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 6784 | 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] | 6785 | { |
Jeff Layton | 6b556ca | 2019-08-18 14:18:55 -0400 | [diff] [blame] | 6786 | struct nfsd_file *nf; |
| 6787 | __be32 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf); |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 6788 | if (!err) { |
Jeff Layton | 6b556ca | 2019-08-18 14:18:55 -0400 | [diff] [blame] | 6789 | err = nfserrno(vfs_test_lock(nf->nf_file, lock)); |
| 6790 | nfsd_file_put(nf); |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 6791 | } |
J. Bruce Fields | 55ef127 | 2008-12-20 11:58:38 -0800 | [diff] [blame] | 6792 | return err; |
| 6793 | } |
| 6794 | |
| 6795 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6796 | * LOCKT operation |
| 6797 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6798 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6799 | nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6800 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6801 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6802 | struct nfsd4_lockt *lockt = &u->lockt; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6803 | struct file_lock *file_lock = NULL; |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6804 | struct nfs4_lockowner *lo = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6805 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 6806 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6807 | |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 6808 | if (locks_in_grace(SVC_NET(rqstp))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6809 | return nfserr_grace; |
| 6810 | |
| 6811 | if (check_lock_length(lockt->lt_offset, lockt->lt_length)) |
| 6812 | return nfserr_inval; |
| 6813 | |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 6814 | if (!nfsd4_has_session(cstate)) { |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 6815 | status = lookup_clientid(&lockt->lt_clientid, cstate, nn, |
| 6816 | false); |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 6817 | if (status) |
| 6818 | goto out; |
| 6819 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6820 | |
J. Bruce Fields | 75c096f | 2011-08-15 18:39:32 -0400 | [diff] [blame] | 6821 | if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6822 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6823 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6824 | file_lock = locks_alloc_lock(); |
| 6825 | if (!file_lock) { |
| 6826 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 6827 | status = nfserr_jukebox; |
| 6828 | goto out; |
| 6829 | } |
Kinglong Mee | 6cd9066 | 2014-08-15 08:02:55 +0800 | [diff] [blame] | 6830 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6831 | switch (lockt->lt_type) { |
| 6832 | case NFS4_READ_LT: |
| 6833 | case NFS4_READW_LT: |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6834 | file_lock->fl_type = F_RDLCK; |
Colin Ian King | f50c9d7 | 2018-11-03 16:12:53 +0000 | [diff] [blame] | 6835 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6836 | case NFS4_WRITE_LT: |
| 6837 | case NFS4_WRITEW_LT: |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6838 | file_lock->fl_type = F_WRLCK; |
Colin Ian King | f50c9d7 | 2018-11-03 16:12:53 +0000 | [diff] [blame] | 6839 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6840 | default: |
J. Bruce Fields | 2fdada0 | 2007-07-27 16:10:37 -0400 | [diff] [blame] | 6841 | dprintk("NFSD: nfs4_lockt: bad lock type!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6842 | status = nfserr_inval; |
Colin Ian King | f50c9d7 | 2018-11-03 16:12:53 +0000 | [diff] [blame] | 6843 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6844 | } |
| 6845 | |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 6846 | lo = find_lockowner_str(cstate->clp, &lockt->lt_owner); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6847 | if (lo) |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6848 | file_lock->fl_owner = (fl_owner_t)lo; |
| 6849 | file_lock->fl_pid = current->tgid; |
| 6850 | file_lock->fl_flags = FL_POSIX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6851 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6852 | file_lock->fl_start = lockt->lt_offset; |
| 6853 | 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] | 6854 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6855 | nfs4_transform_lock_offset(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6856 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6857 | status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock); |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 6858 | if (status) |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 6859 | goto out; |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 6860 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6861 | if (file_lock->fl_type != F_UNLCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6862 | status = nfserr_denied; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6863 | nfs4_set_lock_denied(file_lock, &lockt->lt_denied); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6864 | } |
| 6865 | out: |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6866 | if (lo) |
| 6867 | nfs4_put_stateowner(&lo->lo_owner); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6868 | if (file_lock) |
| 6869 | locks_free_lock(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6870 | return status; |
| 6871 | } |
| 6872 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6873 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6874 | nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6875 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6876 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6877 | struct nfsd4_locku *locku = &u->locku; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 6878 | struct nfs4_ol_stateid *stp; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6879 | struct nfsd_file *nf = NULL; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6880 | struct file_lock *file_lock = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6881 | __be32 status; |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 6882 | int err; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6883 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
| 6884 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6885 | dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n", |
| 6886 | (long long) locku->lu_offset, |
| 6887 | (long long) locku->lu_length); |
| 6888 | |
| 6889 | if (check_lock_length(locku->lu_offset, locku->lu_length)) |
| 6890 | return nfserr_inval; |
| 6891 | |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 6892 | status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6893 | &locku->lu_stateid, NFS4_LOCK_STID, |
| 6894 | &stp, nn); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 6895 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6896 | goto out; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6897 | nf = find_any_file(stp->st_stid.sc_file); |
| 6898 | if (!nf) { |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 6899 | status = nfserr_lock_range; |
Trond Myklebust | 858cc57 | 2014-07-29 21:34:16 -0400 | [diff] [blame] | 6900 | goto put_stateid; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 6901 | } |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6902 | file_lock = locks_alloc_lock(); |
| 6903 | if (!file_lock) { |
| 6904 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 6905 | status = nfserr_jukebox; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6906 | goto put_file; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6907 | } |
Kinglong Mee | 6cd9066 | 2014-08-15 08:02:55 +0800 | [diff] [blame] | 6908 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6909 | file_lock->fl_type = F_UNLCK; |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6910 | 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] | 6911 | file_lock->fl_pid = current->tgid; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6912 | file_lock->fl_file = nf->nf_file; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6913 | file_lock->fl_flags = FL_POSIX; |
| 6914 | file_lock->fl_lmops = &nfsd_posix_mng_ops; |
| 6915 | file_lock->fl_start = locku->lu_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6916 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6917 | file_lock->fl_end = last_byte_offset(locku->lu_offset, |
| 6918 | locku->lu_length); |
| 6919 | nfs4_transform_lock_offset(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6920 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6921 | err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL); |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 6922 | if (err) { |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 6923 | dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6924 | goto out_nfserr; |
| 6925 | } |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 6926 | nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid); |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6927 | put_file: |
| 6928 | nfsd_file_put(nf); |
Trond Myklebust | 858cc57 | 2014-07-29 21:34:16 -0400 | [diff] [blame] | 6929 | put_stateid: |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 6930 | mutex_unlock(&stp->st_mutex); |
Trond Myklebust | 858cc57 | 2014-07-29 21:34:16 -0400 | [diff] [blame] | 6931 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6932 | out: |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 6933 | nfsd4_bump_seqid(cstate, status); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6934 | if (file_lock) |
| 6935 | locks_free_lock(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6936 | return status; |
| 6937 | |
| 6938 | out_nfserr: |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 6939 | status = nfserrno(err); |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6940 | goto put_file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6941 | } |
| 6942 | |
| 6943 | /* |
| 6944 | * returns |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 6945 | * true: locks held by lockowner |
| 6946 | * false: no locks held by lockowner |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6947 | */ |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 6948 | static bool |
| 6949 | check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6950 | { |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 6951 | struct file_lock *fl; |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 6952 | int status = false; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6953 | struct nfsd_file *nf = find_any_file(fp); |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 6954 | struct inode *inode; |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 6955 | struct file_lock_context *flctx; |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 6956 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6957 | if (!nf) { |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 6958 | /* Any valid lock stateid should have some sort of access */ |
| 6959 | WARN_ON_ONCE(1); |
| 6960 | return status; |
| 6961 | } |
| 6962 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6963 | inode = locks_inode(nf->nf_file); |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 6964 | flctx = inode->i_flctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6965 | |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 6966 | if (flctx && !list_empty_careful(&flctx->flc_posix)) { |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 6967 | spin_lock(&flctx->flc_lock); |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 6968 | list_for_each_entry(fl, &flctx->flc_posix, fl_list) { |
| 6969 | if (fl->fl_owner == (fl_owner_t)lowner) { |
| 6970 | status = true; |
| 6971 | break; |
| 6972 | } |
J. Bruce Fields | 796dadf | 2006-01-18 17:43:22 -0800 | [diff] [blame] | 6973 | } |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 6974 | spin_unlock(&flctx->flc_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6975 | } |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6976 | nfsd_file_put(nf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6977 | return status; |
| 6978 | } |
| 6979 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6980 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 6981 | nfsd4_release_lockowner(struct svc_rqst *rqstp, |
| 6982 | struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6983 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6984 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6985 | struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6986 | clientid_t *clid = &rlockowner->rl_clientid; |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 6987 | struct nfs4_stateowner *sop; |
| 6988 | struct nfs4_lockowner *lo = NULL; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 6989 | struct nfs4_ol_stateid *stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6990 | struct xdr_netobj *owner = &rlockowner->rl_owner; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6991 | unsigned int hashval = ownerstr_hashval(owner); |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6992 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 6993 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6994 | struct nfs4_client *clp; |
Chuck Lever | 8858481 | 2016-07-13 16:40:14 -0400 | [diff] [blame] | 6995 | LIST_HEAD (reaplist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6996 | |
| 6997 | dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n", |
| 6998 | clid->cl_boot, clid->cl_id); |
| 6999 | |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 7000 | status = lookup_clientid(clid, cstate, nn, false); |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 7001 | if (status) |
Trond Myklebust | 51f5e78 | 2014-07-30 08:27:27 -0400 | [diff] [blame] | 7002 | return status; |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 7003 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 7004 | clp = cstate->clp; |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 7005 | /* Find the matching lock stateowner */ |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 7006 | spin_lock(&clp->cl_lock); |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 7007 | list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval], |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 7008 | so_strhash) { |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 7009 | |
| 7010 | if (sop->so_is_open_owner || !same_owner_str(sop, owner)) |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 7011 | continue; |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 7012 | |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 7013 | /* see if there are still any locks associated with it */ |
| 7014 | lo = lockowner(sop); |
| 7015 | list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) { |
| 7016 | if (check_for_locks(stp->st_stid.sc_file, lo)) { |
| 7017 | status = nfserr_locks_held; |
| 7018 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | 51f5e78 | 2014-07-30 08:27:27 -0400 | [diff] [blame] | 7019 | return status; |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 7020 | } |
Jeff Layton | 5adfd88 | 2014-07-29 21:34:31 -0400 | [diff] [blame] | 7021 | } |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 7022 | |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 7023 | nfs4_get_stateowner(sop); |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 7024 | break; |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 7025 | } |
Chuck Lever | 8858481 | 2016-07-13 16:40:14 -0400 | [diff] [blame] | 7026 | if (!lo) { |
| 7027 | spin_unlock(&clp->cl_lock); |
| 7028 | return status; |
| 7029 | } |
| 7030 | |
| 7031 | unhash_lockowner_locked(lo); |
| 7032 | while (!list_empty(&lo->lo_owner.so_stateids)) { |
| 7033 | stp = list_first_entry(&lo->lo_owner.so_stateids, |
| 7034 | struct nfs4_ol_stateid, |
| 7035 | st_perstateowner); |
| 7036 | WARN_ON(!unhash_lock_stateid(stp)); |
| 7037 | put_ol_stateid_locked(stp, &reaplist); |
| 7038 | } |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 7039 | spin_unlock(&clp->cl_lock); |
Chuck Lever | 8858481 | 2016-07-13 16:40:14 -0400 | [diff] [blame] | 7040 | free_ol_stateid_reaplist(&reaplist); |
Jeff Layton | 68ef3bc | 2018-03-16 11:32:02 -0400 | [diff] [blame] | 7041 | remove_blocked_locks(lo); |
Chuck Lever | 8858481 | 2016-07-13 16:40:14 -0400 | [diff] [blame] | 7042 | nfs4_put_stateowner(&lo->lo_owner); |
| 7043 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7044 | return status; |
| 7045 | } |
| 7046 | |
| 7047 | static inline struct nfs4_client_reclaim * |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 7048 | alloc_reclaim(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7049 | { |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 7050 | return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7051 | } |
| 7052 | |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 7053 | bool |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 7054 | nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn) |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 7055 | { |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 7056 | struct nfs4_client_reclaim *crp; |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 7057 | |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7058 | crp = nfsd4_find_reclaim_client(name, nn); |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 7059 | return (crp && crp->cr_clp); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 7060 | } |
| 7061 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7062 | /* |
| 7063 | * failure => all reset bets are off, nfserr_no_grace... |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 7064 | * |
| 7065 | * The caller is responsible for freeing name.data if NULL is returned (it |
| 7066 | * will be freed in nfs4_remove_reclaim_record in the normal case). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7067 | */ |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 7068 | struct nfs4_client_reclaim * |
Scott Mayhew | 6ee95d1 | 2019-09-09 16:10:31 -0400 | [diff] [blame] | 7069 | nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash, |
| 7070 | struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7071 | { |
| 7072 | unsigned int strhashval; |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 7073 | struct nfs4_client_reclaim *crp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7074 | |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 7075 | dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", name.len, name.data); |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 7076 | crp = alloc_reclaim(); |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 7077 | if (crp) { |
| 7078 | strhashval = clientstr_hashval(name); |
| 7079 | INIT_LIST_HEAD(&crp->cr_strhash); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7080 | list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]); |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 7081 | crp->cr_name.data = name.data; |
| 7082 | crp->cr_name.len = name.len; |
Scott Mayhew | 6ee95d1 | 2019-09-09 16:10:31 -0400 | [diff] [blame] | 7083 | crp->cr_princhash.data = princhash.data; |
| 7084 | crp->cr_princhash.len = princhash.len; |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 7085 | crp->cr_clp = NULL; |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7086 | nn->reclaim_str_hashtbl_size++; |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 7087 | } |
| 7088 | return crp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7089 | } |
| 7090 | |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 7091 | void |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7092 | 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] | 7093 | { |
| 7094 | list_del(&crp->cr_strhash); |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 7095 | kfree(crp->cr_name.data); |
Scott Mayhew | 6ee95d1 | 2019-09-09 16:10:31 -0400 | [diff] [blame] | 7096 | kfree(crp->cr_princhash.data); |
Jeff Layton | ce30e53 | 2012-11-12 15:00:53 -0500 | [diff] [blame] | 7097 | kfree(crp); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7098 | nn->reclaim_str_hashtbl_size--; |
Jeff Layton | ce30e53 | 2012-11-12 15:00:53 -0500 | [diff] [blame] | 7099 | } |
| 7100 | |
| 7101 | void |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7102 | nfs4_release_reclaim(struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7103 | { |
| 7104 | struct nfs4_client_reclaim *crp = NULL; |
| 7105 | int i; |
| 7106 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7107 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7108 | while (!list_empty(&nn->reclaim_str_hashtbl[i])) { |
| 7109 | crp = list_entry(nn->reclaim_str_hashtbl[i].next, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7110 | struct nfs4_client_reclaim, cr_strhash); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7111 | nfs4_remove_reclaim_record(crp, nn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7112 | } |
| 7113 | } |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 7114 | WARN_ON_ONCE(nn->reclaim_str_hashtbl_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7115 | } |
| 7116 | |
| 7117 | /* |
| 7118 | * called from OPEN, CLAIM_PREVIOUS with a new clientid. */ |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 7119 | struct nfs4_client_reclaim * |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 7120 | nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7121 | { |
| 7122 | unsigned int strhashval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7123 | struct nfs4_client_reclaim *crp = NULL; |
| 7124 | |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 7125 | dprintk("NFSD: nfs4_find_reclaim_client for name %.*s\n", name.len, name.data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7126 | |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 7127 | strhashval = clientstr_hashval(name); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7128 | list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) { |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 7129 | if (compare_blob(&crp->cr_name, &name) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7130 | return crp; |
| 7131 | } |
| 7132 | } |
| 7133 | return NULL; |
| 7134 | } |
| 7135 | |
| 7136 | /* |
| 7137 | * Called from OPEN. Look for clientid in reclaim list. |
| 7138 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 7139 | __be32 |
Trond Myklebust | 0fe492d | 2014-06-30 11:48:47 -0400 | [diff] [blame] | 7140 | nfs4_check_open_reclaim(clientid_t *clid, |
| 7141 | struct nfsd4_compound_state *cstate, |
| 7142 | struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7143 | { |
Trond Myklebust | 0fe492d | 2014-06-30 11:48:47 -0400 | [diff] [blame] | 7144 | __be32 status; |
Jeff Layton | a52d726 | 2012-03-21 09:52:02 -0400 | [diff] [blame] | 7145 | |
| 7146 | /* find clientid in conf_id_hashtbl */ |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 7147 | status = lookup_clientid(clid, cstate, nn, false); |
Trond Myklebust | 0fe492d | 2014-06-30 11:48:47 -0400 | [diff] [blame] | 7148 | if (status) |
Jeff Layton | a52d726 | 2012-03-21 09:52:02 -0400 | [diff] [blame] | 7149 | return nfserr_reclaim_bad; |
| 7150 | |
Jeff Layton | 3b3e7b7 | 2014-09-12 16:40:22 -0400 | [diff] [blame] | 7151 | if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags)) |
| 7152 | return nfserr_no_grace; |
| 7153 | |
Trond Myklebust | 0fe492d | 2014-06-30 11:48:47 -0400 | [diff] [blame] | 7154 | if (nfsd4_client_record_check(cstate->clp)) |
| 7155 | return nfserr_reclaim_bad; |
| 7156 | |
| 7157 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7158 | } |
| 7159 | |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 7160 | #ifdef CONFIG_NFSD_FAULT_INJECTION |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7161 | static inline void |
| 7162 | put_client(struct nfs4_client *clp) |
| 7163 | { |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 7164 | atomic_dec(&clp->cl_rpc_users); |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7165 | } |
| 7166 | |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7167 | static struct nfs4_client * |
| 7168 | nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size) |
| 7169 | { |
| 7170 | struct nfs4_client *clp; |
| 7171 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7172 | nfsd_net_id); |
| 7173 | |
| 7174 | if (!nfsd_netns_ready(nn)) |
| 7175 | return NULL; |
| 7176 | |
| 7177 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { |
| 7178 | if (memcmp(&clp->cl_addr, addr, addr_size) == 0) |
| 7179 | return clp; |
| 7180 | } |
| 7181 | return NULL; |
| 7182 | } |
| 7183 | |
Jeff Layton | 7ec0e36 | 2014-07-30 08:27:17 -0400 | [diff] [blame] | 7184 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7185 | nfsd_inject_print_clients(void) |
Jeff Layton | 7ec0e36 | 2014-07-30 08:27:17 -0400 | [diff] [blame] | 7186 | { |
| 7187 | struct nfs4_client *clp; |
| 7188 | u64 count = 0; |
| 7189 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7190 | nfsd_net_id); |
| 7191 | char buf[INET6_ADDRSTRLEN]; |
| 7192 | |
| 7193 | if (!nfsd_netns_ready(nn)) |
| 7194 | return 0; |
| 7195 | |
| 7196 | spin_lock(&nn->client_lock); |
| 7197 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { |
| 7198 | rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf)); |
| 7199 | pr_info("NFS Client: %s\n", buf); |
| 7200 | ++count; |
| 7201 | } |
| 7202 | spin_unlock(&nn->client_lock); |
| 7203 | |
| 7204 | return count; |
| 7205 | } |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 7206 | |
Jeff Layton | a0926d1 | 2014-07-30 08:27:18 -0400 | [diff] [blame] | 7207 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7208 | nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size) |
Jeff Layton | a0926d1 | 2014-07-30 08:27:18 -0400 | [diff] [blame] | 7209 | { |
| 7210 | u64 count = 0; |
| 7211 | struct nfs4_client *clp; |
| 7212 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7213 | nfsd_net_id); |
| 7214 | |
| 7215 | if (!nfsd_netns_ready(nn)) |
| 7216 | return count; |
| 7217 | |
| 7218 | spin_lock(&nn->client_lock); |
| 7219 | clp = nfsd_find_client(addr, addr_size); |
| 7220 | if (clp) { |
| 7221 | if (mark_client_expired_locked(clp) == nfs_ok) |
| 7222 | ++count; |
| 7223 | else |
| 7224 | clp = NULL; |
| 7225 | } |
| 7226 | spin_unlock(&nn->client_lock); |
| 7227 | |
| 7228 | if (clp) |
| 7229 | expire_client(clp); |
| 7230 | |
| 7231 | return count; |
| 7232 | } |
| 7233 | |
Jeff Layton | 69fc9ed | 2014-07-30 08:27:19 -0400 | [diff] [blame] | 7234 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7235 | nfsd_inject_forget_clients(u64 max) |
Jeff Layton | 69fc9ed | 2014-07-30 08:27:19 -0400 | [diff] [blame] | 7236 | { |
| 7237 | u64 count = 0; |
| 7238 | struct nfs4_client *clp, *next; |
| 7239 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7240 | nfsd_net_id); |
| 7241 | LIST_HEAD(reaplist); |
| 7242 | |
| 7243 | if (!nfsd_netns_ready(nn)) |
| 7244 | return count; |
| 7245 | |
| 7246 | spin_lock(&nn->client_lock); |
| 7247 | list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) { |
| 7248 | if (mark_client_expired_locked(clp) == nfs_ok) { |
| 7249 | list_add(&clp->cl_lru, &reaplist); |
| 7250 | if (max != 0 && ++count >= max) |
| 7251 | break; |
| 7252 | } |
| 7253 | } |
| 7254 | spin_unlock(&nn->client_lock); |
| 7255 | |
| 7256 | list_for_each_entry_safe(clp, next, &reaplist, cl_lru) |
| 7257 | expire_client(clp); |
| 7258 | |
| 7259 | return count; |
| 7260 | } |
| 7261 | |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 7262 | static void nfsd_print_count(struct nfs4_client *clp, unsigned int count, |
| 7263 | const char *type) |
| 7264 | { |
| 7265 | char buf[INET6_ADDRSTRLEN]; |
Bryan Schumaker | 0a5c33e | 2012-12-07 16:17:28 -0500 | [diff] [blame] | 7266 | rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf)); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 7267 | printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type); |
| 7268 | } |
| 7269 | |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7270 | static void |
| 7271 | nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst, |
| 7272 | struct list_head *collect) |
| 7273 | { |
| 7274 | struct nfs4_client *clp = lst->st_stid.sc_client; |
| 7275 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7276 | nfsd_net_id); |
| 7277 | |
| 7278 | if (!collect) |
| 7279 | return; |
| 7280 | |
| 7281 | lockdep_assert_held(&nn->client_lock); |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 7282 | atomic_inc(&clp->cl_rpc_users); |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7283 | list_add(&lst->st_locks, collect); |
| 7284 | } |
| 7285 | |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 7286 | static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max, |
Jeff Layton | 3738d50 | 2014-07-30 08:27:20 -0400 | [diff] [blame] | 7287 | struct list_head *collect, |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 7288 | bool (*func)(struct nfs4_ol_stateid *)) |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 7289 | { |
| 7290 | struct nfs4_openowner *oop; |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 7291 | struct nfs4_ol_stateid *stp, *st_next; |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 7292 | struct nfs4_ol_stateid *lst, *lst_next; |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 7293 | u64 count = 0; |
| 7294 | |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7295 | spin_lock(&clp->cl_lock); |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 7296 | list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) { |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 7297 | list_for_each_entry_safe(stp, st_next, |
| 7298 | &oop->oo_owner.so_stateids, st_perstateowner) { |
| 7299 | list_for_each_entry_safe(lst, lst_next, |
| 7300 | &stp->st_locks, st_locks) { |
Jeff Layton | 3738d50 | 2014-07-30 08:27:20 -0400 | [diff] [blame] | 7301 | if (func) { |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 7302 | if (func(lst)) |
| 7303 | nfsd_inject_add_lock_to_list(lst, |
| 7304 | collect); |
Jeff Layton | 3738d50 | 2014-07-30 08:27:20 -0400 | [diff] [blame] | 7305 | } |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7306 | ++count; |
| 7307 | /* |
| 7308 | * Despite the fact that these functions deal |
| 7309 | * with 64-bit integers for "count", we must |
| 7310 | * ensure that it doesn't blow up the |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 7311 | * clp->cl_rpc_users. Throw a warning if we |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7312 | * start to approach INT_MAX here. |
| 7313 | */ |
| 7314 | WARN_ON_ONCE(count == (INT_MAX / 2)); |
| 7315 | if (count == max) |
| 7316 | goto out; |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 7317 | } |
| 7318 | } |
| 7319 | } |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7320 | out: |
| 7321 | spin_unlock(&clp->cl_lock); |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 7322 | |
| 7323 | return count; |
| 7324 | } |
| 7325 | |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7326 | static u64 |
| 7327 | nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect, |
| 7328 | u64 max) |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 7329 | { |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7330 | return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid); |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 7331 | } |
| 7332 | |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7333 | static u64 |
| 7334 | nfsd_print_client_locks(struct nfs4_client *clp) |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 7335 | { |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7336 | u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 7337 | nfsd_print_count(clp, count, "locked files"); |
| 7338 | return count; |
| 7339 | } |
| 7340 | |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7341 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7342 | nfsd_inject_print_locks(void) |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7343 | { |
| 7344 | struct nfs4_client *clp; |
| 7345 | u64 count = 0; |
| 7346 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7347 | nfsd_net_id); |
| 7348 | |
| 7349 | if (!nfsd_netns_ready(nn)) |
| 7350 | return 0; |
| 7351 | |
| 7352 | spin_lock(&nn->client_lock); |
| 7353 | list_for_each_entry(clp, &nn->client_lru, cl_lru) |
| 7354 | count += nfsd_print_client_locks(clp); |
| 7355 | spin_unlock(&nn->client_lock); |
| 7356 | |
| 7357 | return count; |
| 7358 | } |
| 7359 | |
| 7360 | static void |
| 7361 | nfsd_reap_locks(struct list_head *reaplist) |
| 7362 | { |
| 7363 | struct nfs4_client *clp; |
| 7364 | struct nfs4_ol_stateid *stp, *next; |
| 7365 | |
| 7366 | list_for_each_entry_safe(stp, next, reaplist, st_locks) { |
| 7367 | list_del_init(&stp->st_locks); |
| 7368 | clp = stp->st_stid.sc_client; |
| 7369 | nfs4_put_stid(&stp->st_stid); |
| 7370 | put_client(clp); |
| 7371 | } |
| 7372 | } |
| 7373 | |
| 7374 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7375 | nfsd_inject_forget_client_locks(struct sockaddr_storage *addr, size_t addr_size) |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7376 | { |
| 7377 | unsigned int count = 0; |
| 7378 | struct nfs4_client *clp; |
| 7379 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7380 | nfsd_net_id); |
| 7381 | LIST_HEAD(reaplist); |
| 7382 | |
| 7383 | if (!nfsd_netns_ready(nn)) |
| 7384 | return count; |
| 7385 | |
| 7386 | spin_lock(&nn->client_lock); |
| 7387 | clp = nfsd_find_client(addr, addr_size); |
| 7388 | if (clp) |
| 7389 | count = nfsd_collect_client_locks(clp, &reaplist, 0); |
| 7390 | spin_unlock(&nn->client_lock); |
| 7391 | nfsd_reap_locks(&reaplist); |
| 7392 | return count; |
| 7393 | } |
| 7394 | |
| 7395 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7396 | nfsd_inject_forget_locks(u64 max) |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7397 | { |
| 7398 | u64 count = 0; |
| 7399 | struct nfs4_client *clp; |
| 7400 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7401 | nfsd_net_id); |
| 7402 | LIST_HEAD(reaplist); |
| 7403 | |
| 7404 | if (!nfsd_netns_ready(nn)) |
| 7405 | return count; |
| 7406 | |
| 7407 | spin_lock(&nn->client_lock); |
| 7408 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { |
| 7409 | count += nfsd_collect_client_locks(clp, &reaplist, max - count); |
| 7410 | if (max != 0 && count >= max) |
| 7411 | break; |
| 7412 | } |
| 7413 | spin_unlock(&nn->client_lock); |
| 7414 | nfsd_reap_locks(&reaplist); |
| 7415 | return count; |
| 7416 | } |
| 7417 | |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7418 | static u64 |
| 7419 | nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max, |
| 7420 | struct list_head *collect, |
| 7421 | void (*func)(struct nfs4_openowner *)) |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 7422 | { |
| 7423 | struct nfs4_openowner *oop, *next; |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7424 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7425 | nfsd_net_id); |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 7426 | u64 count = 0; |
| 7427 | |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7428 | lockdep_assert_held(&nn->client_lock); |
| 7429 | |
| 7430 | spin_lock(&clp->cl_lock); |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 7431 | list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) { |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7432 | if (func) { |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 7433 | func(oop); |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7434 | if (collect) { |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 7435 | atomic_inc(&clp->cl_rpc_users); |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7436 | list_add(&oop->oo_perclient, collect); |
| 7437 | } |
| 7438 | } |
| 7439 | ++count; |
| 7440 | /* |
| 7441 | * Despite the fact that these functions deal with |
| 7442 | * 64-bit integers for "count", we must ensure that |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 7443 | * it doesn't blow up the clp->cl_rpc_users. Throw a |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7444 | * warning if we start to approach INT_MAX here. |
| 7445 | */ |
| 7446 | WARN_ON_ONCE(count == (INT_MAX / 2)); |
| 7447 | if (count == max) |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 7448 | break; |
| 7449 | } |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7450 | spin_unlock(&clp->cl_lock); |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 7451 | |
| 7452 | return count; |
| 7453 | } |
| 7454 | |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7455 | static u64 |
| 7456 | nfsd_print_client_openowners(struct nfs4_client *clp) |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 7457 | { |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7458 | u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL); |
| 7459 | |
| 7460 | nfsd_print_count(clp, count, "openowners"); |
| 7461 | return count; |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 7462 | } |
| 7463 | |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7464 | static u64 |
| 7465 | nfsd_collect_client_openowners(struct nfs4_client *clp, |
| 7466 | struct list_head *collect, u64 max) |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 7467 | { |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7468 | return nfsd_foreach_client_openowner(clp, max, collect, |
| 7469 | unhash_openowner_locked); |
| 7470 | } |
| 7471 | |
| 7472 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7473 | nfsd_inject_print_openowners(void) |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7474 | { |
| 7475 | struct nfs4_client *clp; |
| 7476 | u64 count = 0; |
| 7477 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7478 | nfsd_net_id); |
| 7479 | |
| 7480 | if (!nfsd_netns_ready(nn)) |
| 7481 | return 0; |
| 7482 | |
| 7483 | spin_lock(&nn->client_lock); |
| 7484 | list_for_each_entry(clp, &nn->client_lru, cl_lru) |
| 7485 | count += nfsd_print_client_openowners(clp); |
| 7486 | spin_unlock(&nn->client_lock); |
| 7487 | |
| 7488 | return count; |
| 7489 | } |
| 7490 | |
| 7491 | static void |
| 7492 | nfsd_reap_openowners(struct list_head *reaplist) |
| 7493 | { |
| 7494 | struct nfs4_client *clp; |
| 7495 | struct nfs4_openowner *oop, *next; |
| 7496 | |
| 7497 | list_for_each_entry_safe(oop, next, reaplist, oo_perclient) { |
| 7498 | list_del_init(&oop->oo_perclient); |
| 7499 | clp = oop->oo_owner.so_client; |
| 7500 | release_openowner(oop); |
| 7501 | put_client(clp); |
| 7502 | } |
| 7503 | } |
| 7504 | |
| 7505 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7506 | nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr, |
| 7507 | size_t addr_size) |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7508 | { |
| 7509 | unsigned int count = 0; |
| 7510 | struct nfs4_client *clp; |
| 7511 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7512 | nfsd_net_id); |
| 7513 | LIST_HEAD(reaplist); |
| 7514 | |
| 7515 | if (!nfsd_netns_ready(nn)) |
| 7516 | return count; |
| 7517 | |
| 7518 | spin_lock(&nn->client_lock); |
| 7519 | clp = nfsd_find_client(addr, addr_size); |
| 7520 | if (clp) |
| 7521 | count = nfsd_collect_client_openowners(clp, &reaplist, 0); |
| 7522 | spin_unlock(&nn->client_lock); |
| 7523 | nfsd_reap_openowners(&reaplist); |
| 7524 | return count; |
| 7525 | } |
| 7526 | |
| 7527 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7528 | nfsd_inject_forget_openowners(u64 max) |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7529 | { |
| 7530 | u64 count = 0; |
| 7531 | struct nfs4_client *clp; |
| 7532 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7533 | nfsd_net_id); |
| 7534 | LIST_HEAD(reaplist); |
| 7535 | |
| 7536 | if (!nfsd_netns_ready(nn)) |
| 7537 | return count; |
| 7538 | |
| 7539 | spin_lock(&nn->client_lock); |
| 7540 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { |
| 7541 | count += nfsd_collect_client_openowners(clp, &reaplist, |
| 7542 | max - count); |
| 7543 | if (max != 0 && count >= max) |
| 7544 | break; |
| 7545 | } |
| 7546 | spin_unlock(&nn->client_lock); |
| 7547 | nfsd_reap_openowners(&reaplist); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 7548 | return count; |
| 7549 | } |
| 7550 | |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 7551 | static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max, |
| 7552 | struct list_head *victims) |
| 7553 | { |
| 7554 | struct nfs4_delegation *dp, *next; |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 7555 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7556 | nfsd_net_id); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 7557 | u64 count = 0; |
| 7558 | |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 7559 | lockdep_assert_held(&nn->client_lock); |
| 7560 | |
| 7561 | spin_lock(&state_lock); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 7562 | list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) { |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 7563 | if (victims) { |
| 7564 | /* |
| 7565 | * It's not safe to mess with delegations that have a |
| 7566 | * non-zero dl_time. They might have already been broken |
| 7567 | * and could be processed by the laundromat outside of |
| 7568 | * the state_lock. Just leave them be. |
| 7569 | */ |
| 7570 | if (dp->dl_time != 0) |
| 7571 | continue; |
| 7572 | |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 7573 | atomic_inc(&clp->cl_rpc_users); |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 7574 | WARN_ON(!unhash_delegation_locked(dp)); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 7575 | list_add(&dp->dl_recall_lru, victims); |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 7576 | } |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 7577 | ++count; |
| 7578 | /* |
| 7579 | * Despite the fact that these functions deal with |
| 7580 | * 64-bit integers for "count", we must ensure that |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 7581 | * it doesn't blow up the clp->cl_rpc_users. Throw a |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 7582 | * warning if we start to approach INT_MAX here. |
| 7583 | */ |
| 7584 | WARN_ON_ONCE(count == (INT_MAX / 2)); |
| 7585 | if (count == max) |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 7586 | break; |
| 7587 | } |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 7588 | spin_unlock(&state_lock); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 7589 | return count; |
| 7590 | } |
| 7591 | |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 7592 | static u64 |
| 7593 | nfsd_print_client_delegations(struct nfs4_client *clp) |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 7594 | { |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 7595 | u64 count = nfsd_find_all_delegations(clp, 0, NULL); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 7596 | |
| 7597 | nfsd_print_count(clp, count, "delegations"); |
| 7598 | return count; |
| 7599 | } |
| 7600 | |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 7601 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7602 | nfsd_inject_print_delegations(void) |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 7603 | { |
| 7604 | struct nfs4_client *clp; |
| 7605 | u64 count = 0; |
| 7606 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7607 | nfsd_net_id); |
| 7608 | |
| 7609 | if (!nfsd_netns_ready(nn)) |
| 7610 | return 0; |
| 7611 | |
| 7612 | spin_lock(&nn->client_lock); |
| 7613 | list_for_each_entry(clp, &nn->client_lru, cl_lru) |
| 7614 | count += nfsd_print_client_delegations(clp); |
| 7615 | spin_unlock(&nn->client_lock); |
| 7616 | |
| 7617 | return count; |
| 7618 | } |
| 7619 | |
| 7620 | static void |
| 7621 | nfsd_forget_delegations(struct list_head *reaplist) |
| 7622 | { |
| 7623 | struct nfs4_client *clp; |
| 7624 | struct nfs4_delegation *dp, *next; |
| 7625 | |
| 7626 | list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) { |
| 7627 | list_del_init(&dp->dl_recall_lru); |
| 7628 | clp = dp->dl_stid.sc_client; |
| 7629 | revoke_delegation(dp); |
| 7630 | put_client(clp); |
| 7631 | } |
| 7632 | } |
| 7633 | |
| 7634 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7635 | nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr, |
| 7636 | size_t addr_size) |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 7637 | { |
| 7638 | u64 count = 0; |
| 7639 | struct nfs4_client *clp; |
| 7640 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7641 | nfsd_net_id); |
| 7642 | LIST_HEAD(reaplist); |
| 7643 | |
| 7644 | if (!nfsd_netns_ready(nn)) |
| 7645 | return count; |
| 7646 | |
| 7647 | spin_lock(&nn->client_lock); |
| 7648 | clp = nfsd_find_client(addr, addr_size); |
| 7649 | if (clp) |
| 7650 | count = nfsd_find_all_delegations(clp, 0, &reaplist); |
| 7651 | spin_unlock(&nn->client_lock); |
| 7652 | |
| 7653 | nfsd_forget_delegations(&reaplist); |
| 7654 | return count; |
| 7655 | } |
| 7656 | |
| 7657 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7658 | nfsd_inject_forget_delegations(u64 max) |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 7659 | { |
| 7660 | u64 count = 0; |
| 7661 | struct nfs4_client *clp; |
| 7662 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7663 | nfsd_net_id); |
| 7664 | LIST_HEAD(reaplist); |
| 7665 | |
| 7666 | if (!nfsd_netns_ready(nn)) |
| 7667 | return count; |
| 7668 | |
| 7669 | spin_lock(&nn->client_lock); |
| 7670 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { |
| 7671 | count += nfsd_find_all_delegations(clp, max - count, &reaplist); |
| 7672 | if (max != 0 && count >= max) |
| 7673 | break; |
| 7674 | } |
| 7675 | spin_unlock(&nn->client_lock); |
| 7676 | nfsd_forget_delegations(&reaplist); |
| 7677 | return count; |
| 7678 | } |
| 7679 | |
| 7680 | static void |
| 7681 | nfsd_recall_delegations(struct list_head *reaplist) |
| 7682 | { |
| 7683 | struct nfs4_client *clp; |
| 7684 | struct nfs4_delegation *dp, *next; |
| 7685 | |
| 7686 | list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) { |
| 7687 | list_del_init(&dp->dl_recall_lru); |
| 7688 | clp = dp->dl_stid.sc_client; |
| 7689 | /* |
| 7690 | * We skipped all entries that had a zero dl_time before, |
| 7691 | * so we can now reset the dl_time back to 0. If a delegation |
| 7692 | * break comes in now, then it won't make any difference since |
| 7693 | * we're recalling it either way. |
| 7694 | */ |
| 7695 | spin_lock(&state_lock); |
| 7696 | dp->dl_time = 0; |
| 7697 | spin_unlock(&state_lock); |
| 7698 | nfsd_break_one_deleg(dp); |
| 7699 | put_client(clp); |
| 7700 | } |
| 7701 | } |
| 7702 | |
| 7703 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7704 | nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr, |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 7705 | size_t addr_size) |
| 7706 | { |
| 7707 | u64 count = 0; |
| 7708 | struct nfs4_client *clp; |
| 7709 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7710 | nfsd_net_id); |
| 7711 | LIST_HEAD(reaplist); |
| 7712 | |
| 7713 | if (!nfsd_netns_ready(nn)) |
| 7714 | return count; |
| 7715 | |
| 7716 | spin_lock(&nn->client_lock); |
| 7717 | clp = nfsd_find_client(addr, addr_size); |
| 7718 | if (clp) |
| 7719 | count = nfsd_find_all_delegations(clp, 0, &reaplist); |
| 7720 | spin_unlock(&nn->client_lock); |
| 7721 | |
| 7722 | nfsd_recall_delegations(&reaplist); |
| 7723 | return count; |
| 7724 | } |
| 7725 | |
| 7726 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7727 | nfsd_inject_recall_delegations(u64 max) |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 7728 | { |
| 7729 | u64 count = 0; |
| 7730 | struct nfs4_client *clp, *next; |
| 7731 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7732 | nfsd_net_id); |
| 7733 | LIST_HEAD(reaplist); |
| 7734 | |
| 7735 | if (!nfsd_netns_ready(nn)) |
| 7736 | return count; |
| 7737 | |
| 7738 | spin_lock(&nn->client_lock); |
| 7739 | list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) { |
| 7740 | count += nfsd_find_all_delegations(clp, max - count, &reaplist); |
| 7741 | if (max != 0 && ++count >= max) |
| 7742 | break; |
| 7743 | } |
| 7744 | spin_unlock(&nn->client_lock); |
| 7745 | nfsd_recall_delegations(&reaplist); |
| 7746 | return count; |
| 7747 | } |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 7748 | #endif /* CONFIG_NFSD_FAULT_INJECTION */ |
| 7749 | |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 7750 | /* |
| 7751 | * Since the lifetime of a delegation isn't limited to that of an open, a |
| 7752 | * client may quite reasonably hang on to a delegation as long as it has |
| 7753 | * the inode cached. This becomes an obvious problem the first time a |
| 7754 | * client's inode cache approaches the size of the server's total memory. |
| 7755 | * |
| 7756 | * For now we avoid this problem by imposing a hard limit on the number |
| 7757 | * of delegations, which varies according to the server's memory size. |
| 7758 | */ |
| 7759 | static void |
| 7760 | set_max_delegations(void) |
| 7761 | { |
| 7762 | /* |
| 7763 | * Allow at most 4 delegations per megabyte of RAM. Quick |
| 7764 | * estimates suggest that in the worst case (where every delegation |
| 7765 | * is for a different inode), a delegation could take about 1.5K, |
| 7766 | * giving a worst case usage of about 6% of memory. |
| 7767 | */ |
| 7768 | max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT); |
| 7769 | } |
| 7770 | |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 7771 | static int nfs4_state_create_net(struct net *net) |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7772 | { |
| 7773 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 7774 | int i; |
| 7775 | |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 7776 | nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE, |
| 7777 | sizeof(struct list_head), |
| 7778 | GFP_KERNEL); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7779 | if (!nn->conf_id_hashtbl) |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 7780 | goto err; |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 7781 | nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE, |
| 7782 | sizeof(struct list_head), |
| 7783 | GFP_KERNEL); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 7784 | if (!nn->unconf_id_hashtbl) |
| 7785 | goto err_unconf_id; |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 7786 | nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE, |
| 7787 | sizeof(struct list_head), |
| 7788 | GFP_KERNEL); |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 7789 | if (!nn->sessionid_hashtbl) |
| 7790 | goto err_sessionid; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7791 | |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 7792 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7793 | INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 7794 | INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 7795 | } |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 7796 | for (i = 0; i < SESSION_HASH_SIZE; i++) |
| 7797 | INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 7798 | nn->conf_name_tree = RB_ROOT; |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 7799 | nn->unconf_name_tree = RB_ROOT; |
Arnd Bergmann | 9cc7680 | 2019-10-31 15:53:13 +0100 | [diff] [blame] | 7800 | nn->boot_time = ktime_get_real_seconds(); |
Vasily Averin | 81833de | 2017-11-13 07:25:40 +0300 | [diff] [blame] | 7801 | nn->grace_ended = false; |
| 7802 | nn->nfsd4_manager.block_opens = true; |
| 7803 | INIT_LIST_HEAD(&nn->nfsd4_manager.list); |
Stanislav Kinsbursky | 5ed58bb | 2012-11-14 18:21:56 +0300 | [diff] [blame] | 7804 | INIT_LIST_HEAD(&nn->client_lru); |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 7805 | INIT_LIST_HEAD(&nn->close_lru); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 7806 | INIT_LIST_HEAD(&nn->del_recall_lru); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 7807 | spin_lock_init(&nn->client_lock); |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 7808 | spin_lock_init(&nn->s2s_cp_lock); |
| 7809 | idr_init(&nn->s2s_cp_stateids); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7810 | |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 7811 | spin_lock_init(&nn->blocked_locks_lock); |
| 7812 | INIT_LIST_HEAD(&nn->blocked_locks_lru); |
| 7813 | |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 7814 | INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 7815 | get_net(net); |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 7816 | |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7817 | return 0; |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 7818 | |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 7819 | err_sessionid: |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 7820 | kfree(nn->unconf_id_hashtbl); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 7821 | err_unconf_id: |
| 7822 | kfree(nn->conf_id_hashtbl); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 7823 | err: |
| 7824 | return -ENOMEM; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7825 | } |
| 7826 | |
| 7827 | static void |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 7828 | nfs4_state_destroy_net(struct net *net) |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7829 | { |
| 7830 | int i; |
| 7831 | struct nfs4_client *clp = NULL; |
| 7832 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 7833 | |
| 7834 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
| 7835 | while (!list_empty(&nn->conf_id_hashtbl[i])) { |
| 7836 | clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); |
| 7837 | destroy_client(clp); |
| 7838 | } |
| 7839 | } |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 7840 | |
Jeff Layton | 68ef3bc | 2018-03-16 11:32:02 -0400 | [diff] [blame] | 7841 | WARN_ON(!list_empty(&nn->blocked_locks_lru)); |
| 7842 | |
Kinglong Mee | 2b90563 | 2014-03-26 22:09:30 +0800 | [diff] [blame] | 7843 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
| 7844 | while (!list_empty(&nn->unconf_id_hashtbl[i])) { |
| 7845 | clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); |
| 7846 | destroy_client(clp); |
| 7847 | } |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 7848 | } |
| 7849 | |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 7850 | kfree(nn->sessionid_hashtbl); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 7851 | kfree(nn->unconf_id_hashtbl); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7852 | kfree(nn->conf_id_hashtbl); |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 7853 | put_net(net); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7854 | } |
| 7855 | |
Stanislav Kinsbursky | f252bc6 | 2012-11-26 15:22:18 +0300 | [diff] [blame] | 7856 | int |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 7857 | nfs4_state_start_net(struct net *net) |
NeilBrown | ac4d8ff2 | 2005-06-23 22:03:30 -0700 | [diff] [blame] | 7858 | { |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 7859 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 7860 | int ret; |
| 7861 | |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 7862 | ret = nfs4_state_create_net(net); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7863 | if (ret) |
| 7864 | return ret; |
Jeff Layton | d4318ac | 2014-09-12 16:40:21 -0400 | [diff] [blame] | 7865 | locks_start_grace(net, &nn->nfsd4_manager); |
| 7866 | nfsd4_client_tracking_init(net); |
Scott Mayhew | 362063a | 2019-03-26 18:06:28 -0400 | [diff] [blame] | 7867 | if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0) |
| 7868 | goto skip_grace; |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 7869 | printk(KERN_INFO "NFSD: starting %lld-second grace period (net %x)\n", |
Vasily Averin | 7e981a8 | 2017-11-06 16:46:04 +0300 | [diff] [blame] | 7870 | nn->nfsd4_grace, net->ns.inum); |
Stanislav Kinsbursky | 5284b44 | 2012-11-27 14:11:49 +0300 | [diff] [blame] | 7871 | queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 7872 | return 0; |
Scott Mayhew | 362063a | 2019-03-26 18:06:28 -0400 | [diff] [blame] | 7873 | |
| 7874 | skip_grace: |
| 7875 | printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n", |
| 7876 | net->ns.inum); |
| 7877 | queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ); |
| 7878 | nfsd4_end_grace(nn); |
| 7879 | return 0; |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 7880 | } |
| 7881 | |
| 7882 | /* initialization to perform when the nfsd service is started: */ |
| 7883 | |
| 7884 | int |
| 7885 | nfs4_state_start(void) |
| 7886 | { |
| 7887 | int ret; |
| 7888 | |
Jeff Layton | 51a5456 | 2015-08-20 07:17:01 -0400 | [diff] [blame] | 7889 | laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4"); |
Jeff Layton | a6d6b78 | 2012-03-05 11:42:36 -0500 | [diff] [blame] | 7890 | if (laundry_wq == NULL) { |
| 7891 | ret = -ENOMEM; |
Chuck Lever | a26dd64 | 2018-08-16 12:06:09 -0400 | [diff] [blame] | 7892 | goto out; |
Jeff Layton | a6d6b78 | 2012-03-05 11:42:36 -0500 | [diff] [blame] | 7893 | } |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 7894 | ret = nfsd4_create_callback_queue(); |
| 7895 | if (ret) |
| 7896 | goto out_free_laundry; |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 7897 | |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 7898 | set_max_delegations(); |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 7899 | return 0; |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 7900 | |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 7901 | out_free_laundry: |
| 7902 | destroy_workqueue(laundry_wq); |
Chuck Lever | a26dd64 | 2018-08-16 12:06:09 -0400 | [diff] [blame] | 7903 | out: |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 7904 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7905 | } |
| 7906 | |
Stanislav Kinsbursky | f252bc6 | 2012-11-26 15:22:18 +0300 | [diff] [blame] | 7907 | void |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 7908 | nfs4_state_shutdown_net(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7909 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7910 | struct nfs4_delegation *dp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7911 | struct list_head *pos, *next, reaplist; |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 7912 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7913 | |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 7914 | cancel_delayed_work_sync(&nn->laundromat_work); |
| 7915 | locks_end_grace(&nn->nfsd4_manager); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 7916 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7917 | INIT_LIST_HEAD(&reaplist); |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 7918 | spin_lock(&state_lock); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 7919 | list_for_each_safe(pos, next, &nn->del_recall_lru) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7920 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 7921 | WARN_ON(!unhash_delegation_locked(dp)); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 7922 | list_add(&dp->dl_recall_lru, &reaplist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7923 | } |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 7924 | spin_unlock(&state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7925 | list_for_each_safe(pos, next, &reaplist) { |
| 7926 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 7927 | list_del_init(&dp->dl_recall_lru); |
J. Bruce Fields | 0af6e69 | 2018-02-21 15:11:03 -0500 | [diff] [blame] | 7928 | destroy_unhashed_deleg(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7929 | } |
| 7930 | |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 7931 | nfsd4_client_tracking_exit(net); |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 7932 | nfs4_state_destroy_net(net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7933 | } |
| 7934 | |
| 7935 | void |
| 7936 | nfs4_state_shutdown(void) |
| 7937 | { |
NeilBrown | 5e8d5c2 | 2006-04-10 22:55:37 -0700 | [diff] [blame] | 7938 | destroy_workqueue(laundry_wq); |
J. Bruce Fields | c3935e3 | 2010-06-04 16:42:08 -0400 | [diff] [blame] | 7939 | nfsd4_destroy_callback_queue(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7940 | } |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7941 | |
| 7942 | static void |
| 7943 | get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) |
| 7944 | { |
Olga Kornievskaia | 51100d2 | 2018-09-13 13:58:24 -0400 | [diff] [blame] | 7945 | if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG) && |
| 7946 | CURRENT_STATEID(stateid)) |
Tigran Mkrtchyan | 37c593c | 2012-02-13 22:55:32 +0100 | [diff] [blame] | 7947 | memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t)); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7948 | } |
| 7949 | |
| 7950 | static void |
| 7951 | put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) |
| 7952 | { |
Tigran Mkrtchyan | 37c593c | 2012-02-13 22:55:32 +0100 | [diff] [blame] | 7953 | if (cstate->minorversion) { |
| 7954 | memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t)); |
Olga Kornievskaia | 51100d2 | 2018-09-13 13:58:24 -0400 | [diff] [blame] | 7955 | SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG); |
Tigran Mkrtchyan | 37c593c | 2012-02-13 22:55:32 +0100 | [diff] [blame] | 7956 | } |
| 7957 | } |
| 7958 | |
| 7959 | void |
| 7960 | clear_current_stateid(struct nfsd4_compound_state *cstate) |
| 7961 | { |
Olga Kornievskaia | 51100d2 | 2018-09-13 13:58:24 -0400 | [diff] [blame] | 7962 | CLEAR_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7963 | } |
| 7964 | |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 7965 | /* |
| 7966 | * functions to set current state id |
| 7967 | */ |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7968 | void |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7969 | nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, |
| 7970 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 7971 | { |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7972 | put_stateid(cstate, &u->open_downgrade.od_stateid); |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 7973 | } |
| 7974 | |
| 7975 | void |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7976 | nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, |
| 7977 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7978 | { |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7979 | put_stateid(cstate, &u->open.op_stateid); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7980 | } |
| 7981 | |
| 7982 | void |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7983 | nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, |
| 7984 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 7985 | { |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7986 | put_stateid(cstate, &u->close.cl_stateid); |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 7987 | } |
| 7988 | |
| 7989 | void |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7990 | nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, |
| 7991 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 7992 | { |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7993 | put_stateid(cstate, &u->lock.lk_resp_stateid); |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 7994 | } |
| 7995 | |
| 7996 | /* |
| 7997 | * functions to consume current state id |
| 7998 | */ |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 7999 | |
| 8000 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 8001 | nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, |
| 8002 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 8003 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 8004 | get_stateid(cstate, &u->open_downgrade.od_stateid); |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 8005 | } |
| 8006 | |
| 8007 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 8008 | nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, |
| 8009 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 8010 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 8011 | get_stateid(cstate, &u->delegreturn.dr_stateid); |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 8012 | } |
| 8013 | |
| 8014 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 8015 | nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, |
| 8016 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 8017 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 8018 | get_stateid(cstate, &u->free_stateid.fr_stateid); |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 8019 | } |
| 8020 | |
| 8021 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 8022 | nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, |
| 8023 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 8024 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 8025 | get_stateid(cstate, &u->setattr.sa_stateid); |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 8026 | } |
| 8027 | |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 8028 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 8029 | nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, |
| 8030 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 8031 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 8032 | get_stateid(cstate, &u->close.cl_stateid); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 8033 | } |
| 8034 | |
| 8035 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 8036 | nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, |
| 8037 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 8038 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 8039 | get_stateid(cstate, &u->locku.lu_stateid); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 8040 | } |
Tigran Mkrtchyan | 30813e2 | 2012-02-13 22:55:26 +0100 | [diff] [blame] | 8041 | |
| 8042 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 8043 | nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, |
| 8044 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 30813e2 | 2012-02-13 22:55:26 +0100 | [diff] [blame] | 8045 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 8046 | get_stateid(cstate, &u->read.rd_stateid); |
Tigran Mkrtchyan | 30813e2 | 2012-02-13 22:55:26 +0100 | [diff] [blame] | 8047 | } |
| 8048 | |
| 8049 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 8050 | nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, |
| 8051 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 30813e2 | 2012-02-13 22:55:26 +0100 | [diff] [blame] | 8052 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 8053 | get_stateid(cstate, &u->write.wr_stateid); |
Tigran Mkrtchyan | 30813e2 | 2012-02-13 22:55:26 +0100 | [diff] [blame] | 8054 | } |