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) { |
| 270 | fh_copy_shallow(&nbl->nbl_fh, fh); |
| 271 | locks_init_lock(&nbl->nbl_lock); |
| 272 | nfsd4_init_cb(&nbl->nbl_cb, lo->lo_owner.so_client, |
| 273 | &nfsd4_cb_notify_lock_ops, |
| 274 | NFSPROC4_CLNT_CB_NOTIFY_LOCK); |
| 275 | } |
| 276 | } |
| 277 | return nbl; |
| 278 | } |
| 279 | |
| 280 | static void |
| 281 | free_blocked_lock(struct nfsd4_blocked_lock *nbl) |
| 282 | { |
Jeff Layton | 6aaafc4 | 2019-04-22 12:34:23 -0400 | [diff] [blame] | 283 | locks_delete_block(&nbl->nbl_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 284 | locks_release_private(&nbl->nbl_lock); |
| 285 | kfree(nbl); |
| 286 | } |
| 287 | |
Jeff Layton | 68ef3bc | 2018-03-16 11:32:02 -0400 | [diff] [blame] | 288 | static void |
| 289 | remove_blocked_locks(struct nfs4_lockowner *lo) |
| 290 | { |
| 291 | struct nfs4_client *clp = lo->lo_owner.so_client; |
| 292 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 293 | struct nfsd4_blocked_lock *nbl; |
| 294 | LIST_HEAD(reaplist); |
| 295 | |
| 296 | /* Dequeue all blocked locks */ |
| 297 | spin_lock(&nn->blocked_locks_lock); |
| 298 | while (!list_empty(&lo->lo_blocked)) { |
| 299 | nbl = list_first_entry(&lo->lo_blocked, |
| 300 | struct nfsd4_blocked_lock, |
| 301 | nbl_list); |
| 302 | list_del_init(&nbl->nbl_list); |
| 303 | list_move(&nbl->nbl_lru, &reaplist); |
| 304 | } |
| 305 | spin_unlock(&nn->blocked_locks_lock); |
| 306 | |
| 307 | /* Now free them */ |
| 308 | while (!list_empty(&reaplist)) { |
| 309 | nbl = list_first_entry(&reaplist, struct nfsd4_blocked_lock, |
| 310 | nbl_lru); |
| 311 | list_del_init(&nbl->nbl_lru); |
Jeff Layton | 68ef3bc | 2018-03-16 11:32:02 -0400 | [diff] [blame] | 312 | free_blocked_lock(nbl); |
| 313 | } |
| 314 | } |
| 315 | |
Jeff Layton | f456458 | 2019-04-22 12:34:24 -0400 | [diff] [blame] | 316 | static void |
| 317 | nfsd4_cb_notify_lock_prepare(struct nfsd4_callback *cb) |
| 318 | { |
| 319 | struct nfsd4_blocked_lock *nbl = container_of(cb, |
| 320 | struct nfsd4_blocked_lock, nbl_cb); |
| 321 | locks_delete_block(&nbl->nbl_lock); |
| 322 | } |
| 323 | |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 324 | static int |
| 325 | nfsd4_cb_notify_lock_done(struct nfsd4_callback *cb, struct rpc_task *task) |
| 326 | { |
| 327 | /* |
| 328 | * Since this is just an optimization, we don't try very hard if it |
| 329 | * turns out not to succeed. We'll requeue it on NFS4ERR_DELAY, and |
| 330 | * just quit trying on anything else. |
| 331 | */ |
| 332 | switch (task->tk_status) { |
| 333 | case -NFS4ERR_DELAY: |
| 334 | rpc_delay(task, 1 * HZ); |
| 335 | return 0; |
| 336 | default: |
| 337 | return 1; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | static void |
| 342 | nfsd4_cb_notify_lock_release(struct nfsd4_callback *cb) |
| 343 | { |
| 344 | struct nfsd4_blocked_lock *nbl = container_of(cb, |
| 345 | struct nfsd4_blocked_lock, nbl_cb); |
| 346 | |
| 347 | free_blocked_lock(nbl); |
| 348 | } |
| 349 | |
| 350 | static const struct nfsd4_callback_ops nfsd4_cb_notify_lock_ops = { |
Jeff Layton | f456458 | 2019-04-22 12:34:24 -0400 | [diff] [blame] | 351 | .prepare = nfsd4_cb_notify_lock_prepare, |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 352 | .done = nfsd4_cb_notify_lock_done, |
| 353 | .release = nfsd4_cb_notify_lock_release, |
| 354 | }; |
| 355 | |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 356 | static inline struct nfs4_stateowner * |
| 357 | nfs4_get_stateowner(struct nfs4_stateowner *sop) |
| 358 | { |
| 359 | atomic_inc(&sop->so_count); |
| 360 | return sop; |
| 361 | } |
| 362 | |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 363 | static int |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 364 | same_owner_str(struct nfs4_stateowner *sop, struct xdr_netobj *owner) |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 365 | { |
| 366 | return (sop->so_owner.len == owner->len) && |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 367 | 0 == memcmp(sop->so_owner.data, owner->data, owner->len); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | static struct nfs4_openowner * |
| 371 | find_openstateowner_str_locked(unsigned int hashval, struct nfsd4_open *open, |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 372 | struct nfs4_client *clp) |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 373 | { |
| 374 | struct nfs4_stateowner *so; |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 375 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 376 | lockdep_assert_held(&clp->cl_lock); |
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 | list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[hashval], |
| 379 | so_strhash) { |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 380 | if (!so->so_is_open_owner) |
| 381 | continue; |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 382 | if (same_owner_str(so, &open->op_owner)) |
| 383 | return openowner(nfs4_get_stateowner(so)); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 384 | } |
| 385 | return NULL; |
| 386 | } |
| 387 | |
| 388 | static struct nfs4_openowner * |
| 389 | find_openstateowner_str(unsigned int hashval, struct nfsd4_open *open, |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 390 | struct nfs4_client *clp) |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 391 | { |
| 392 | struct nfs4_openowner *oo; |
| 393 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 394 | spin_lock(&clp->cl_lock); |
| 395 | oo = find_openstateowner_str_locked(hashval, open, clp); |
| 396 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 397 | return oo; |
| 398 | } |
| 399 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 400 | static inline u32 |
| 401 | opaque_hashval(const void *ptr, int nbytes) |
| 402 | { |
| 403 | unsigned char *cptr = (unsigned char *) ptr; |
| 404 | |
| 405 | u32 x = 0; |
| 406 | while (nbytes--) { |
| 407 | x *= 37; |
| 408 | x += *cptr++; |
| 409 | } |
| 410 | return x; |
| 411 | } |
| 412 | |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 413 | static void nfsd4_free_file_rcu(struct rcu_head *rcu) |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 414 | { |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 415 | struct nfs4_file *fp = container_of(rcu, struct nfs4_file, fi_rcu); |
| 416 | |
| 417 | kmem_cache_free(file_slab, fp); |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 418 | } |
| 419 | |
Christoph Hellwig | e6ba76e | 2014-08-14 08:50:16 +0200 | [diff] [blame] | 420 | void |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 421 | put_nfs4_file(struct nfs4_file *fi) |
| 422 | { |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 423 | might_lock(&state_lock); |
| 424 | |
Elena Reshetova | 818a34e | 2017-10-20 12:53:30 +0300 | [diff] [blame] | 425 | if (refcount_dec_and_lock(&fi->fi_ref, &state_lock)) { |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 426 | hlist_del_rcu(&fi->fi_hash); |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 427 | spin_unlock(&state_lock); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 428 | WARN_ON_ONCE(!list_empty(&fi->fi_clnt_odstate)); |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 429 | WARN_ON_ONCE(!list_empty(&fi->fi_delegations)); |
| 430 | call_rcu(&fi->fi_rcu, nfsd4_free_file_rcu); |
J. Bruce Fields | 8b671b8 | 2009-02-22 14:51:34 -0800 | [diff] [blame] | 431 | } |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 432 | } |
| 433 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 434 | static struct nfsd_file * |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 435 | __nfs4_get_fd(struct nfs4_file *f, int oflag) |
| 436 | { |
| 437 | if (f->fi_fds[oflag]) |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 438 | return nfsd_file_get(f->fi_fds[oflag]); |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 439 | return NULL; |
| 440 | } |
| 441 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 442 | static struct nfsd_file * |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 443 | find_writeable_file_locked(struct nfs4_file *f) |
| 444 | { |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 445 | struct nfsd_file *ret; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 446 | |
| 447 | lockdep_assert_held(&f->fi_lock); |
| 448 | |
| 449 | ret = __nfs4_get_fd(f, O_WRONLY); |
| 450 | if (!ret) |
| 451 | ret = __nfs4_get_fd(f, O_RDWR); |
| 452 | return ret; |
| 453 | } |
| 454 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 455 | static struct nfsd_file * |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 456 | find_writeable_file(struct nfs4_file *f) |
| 457 | { |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 458 | struct nfsd_file *ret; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 459 | |
| 460 | spin_lock(&f->fi_lock); |
| 461 | ret = find_writeable_file_locked(f); |
| 462 | spin_unlock(&f->fi_lock); |
| 463 | |
| 464 | return ret; |
| 465 | } |
| 466 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 467 | static struct nfsd_file * |
| 468 | find_readable_file_locked(struct nfs4_file *f) |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 469 | { |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 470 | struct nfsd_file *ret; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 471 | |
| 472 | lockdep_assert_held(&f->fi_lock); |
| 473 | |
| 474 | ret = __nfs4_get_fd(f, O_RDONLY); |
| 475 | if (!ret) |
| 476 | ret = __nfs4_get_fd(f, O_RDWR); |
| 477 | return ret; |
| 478 | } |
| 479 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 480 | static struct nfsd_file * |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 481 | find_readable_file(struct nfs4_file *f) |
| 482 | { |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 483 | struct nfsd_file *ret; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 484 | |
| 485 | spin_lock(&f->fi_lock); |
| 486 | ret = find_readable_file_locked(f); |
| 487 | spin_unlock(&f->fi_lock); |
| 488 | |
| 489 | return ret; |
| 490 | } |
| 491 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 492 | struct nfsd_file * |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 493 | find_any_file(struct nfs4_file *f) |
| 494 | { |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 495 | struct nfsd_file *ret; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 496 | |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 497 | if (!f) |
| 498 | return NULL; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 499 | spin_lock(&f->fi_lock); |
| 500 | ret = __nfs4_get_fd(f, O_RDWR); |
| 501 | if (!ret) { |
| 502 | ret = __nfs4_get_fd(f, O_WRONLY); |
| 503 | if (!ret) |
| 504 | ret = __nfs4_get_fd(f, O_RDONLY); |
| 505 | } |
| 506 | spin_unlock(&f->fi_lock); |
| 507 | return ret; |
| 508 | } |
| 509 | |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 510 | static atomic_long_t num_delegations; |
Zhang Yanfei | 697ce9b | 2013-02-22 16:35:47 -0800 | [diff] [blame] | 511 | unsigned long max_delegations; |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 512 | |
| 513 | /* |
| 514 | * Open owner state (share locks) |
| 515 | */ |
| 516 | |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 517 | /* hash tables for lock and open owners */ |
| 518 | #define OWNER_HASH_BITS 8 |
| 519 | #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS) |
| 520 | #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1) |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 521 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 522 | static unsigned int ownerstr_hashval(struct xdr_netobj *ownername) |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 523 | { |
| 524 | unsigned int ret; |
| 525 | |
| 526 | ret = opaque_hashval(ownername->data, ownername->len); |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 527 | return ret & OWNER_HASH_MASK; |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 528 | } |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 529 | |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 530 | /* hash table for nfs4_file */ |
| 531 | #define FILE_HASH_BITS 8 |
| 532 | #define FILE_HASH_SIZE (1 << FILE_HASH_BITS) |
Shan Wei | 3507958 | 2011-01-14 17:35:59 +0800 | [diff] [blame] | 533 | |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 534 | static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh) |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 535 | { |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 536 | return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0); |
| 537 | } |
| 538 | |
| 539 | static unsigned int file_hashval(struct knfsd_fh *fh) |
| 540 | { |
| 541 | return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1); |
| 542 | } |
| 543 | |
Jeff Layton | 89876f8 | 2013-04-02 09:01:59 -0400 | [diff] [blame] | 544 | static struct hlist_head file_hashtbl[FILE_HASH_SIZE]; |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 545 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 546 | static void |
| 547 | __nfs4_file_get_access(struct nfs4_file *fp, u32 access) |
J. Bruce Fields | 3477565 | 2013-08-23 17:55:18 -0400 | [diff] [blame] | 548 | { |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 549 | lockdep_assert_held(&fp->fi_lock); |
| 550 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 551 | if (access & NFS4_SHARE_ACCESS_WRITE) |
| 552 | atomic_inc(&fp->fi_access[O_WRONLY]); |
| 553 | if (access & NFS4_SHARE_ACCESS_READ) |
| 554 | atomic_inc(&fp->fi_access[O_RDONLY]); |
J. Bruce Fields | 3477565 | 2013-08-23 17:55:18 -0400 | [diff] [blame] | 555 | } |
| 556 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 557 | static __be32 |
| 558 | nfs4_file_get_access(struct nfs4_file *fp, u32 access) |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 559 | { |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 560 | lockdep_assert_held(&fp->fi_lock); |
| 561 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 562 | /* Does this access mode make sense? */ |
| 563 | if (access & ~NFS4_SHARE_ACCESS_BOTH) |
| 564 | return nfserr_inval; |
| 565 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 566 | /* Does it conflict with a deny mode already set? */ |
| 567 | if ((access & fp->fi_share_deny) != 0) |
| 568 | return nfserr_share_denied; |
| 569 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 570 | __nfs4_file_get_access(fp, access); |
| 571 | return nfs_ok; |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 572 | } |
| 573 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 574 | static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny) |
| 575 | { |
| 576 | /* Common case is that there is no deny mode. */ |
| 577 | if (deny) { |
| 578 | /* Does this deny mode make sense? */ |
| 579 | if (deny & ~NFS4_SHARE_DENY_BOTH) |
| 580 | return nfserr_inval; |
| 581 | |
| 582 | if ((deny & NFS4_SHARE_DENY_READ) && |
| 583 | atomic_read(&fp->fi_access[O_RDONLY])) |
| 584 | return nfserr_share_denied; |
| 585 | |
| 586 | if ((deny & NFS4_SHARE_DENY_WRITE) && |
| 587 | atomic_read(&fp->fi_access[O_WRONLY])) |
| 588 | return nfserr_share_denied; |
| 589 | } |
| 590 | return nfs_ok; |
| 591 | } |
| 592 | |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 593 | 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] | 594 | { |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 595 | might_lock(&fp->fi_lock); |
| 596 | |
| 597 | if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) { |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 598 | struct nfsd_file *f1 = NULL; |
| 599 | struct nfsd_file *f2 = NULL; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 600 | |
Jeff Layton | 6d338b5 | 2014-07-10 14:07:29 -0400 | [diff] [blame] | 601 | swap(f1, fp->fi_fds[oflag]); |
J. Bruce Fields | 0c7c3e6 | 2013-03-28 20:37:14 -0400 | [diff] [blame] | 602 | if (atomic_read(&fp->fi_access[1 - oflag]) == 0) |
Jeff Layton | 6d338b5 | 2014-07-10 14:07:29 -0400 | [diff] [blame] | 603 | swap(f2, fp->fi_fds[O_RDWR]); |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 604 | spin_unlock(&fp->fi_lock); |
| 605 | if (f1) |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 606 | nfsd_file_put(f1); |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 607 | if (f2) |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 608 | nfsd_file_put(f2); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 609 | } |
| 610 | } |
| 611 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 612 | 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] | 613 | { |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 614 | WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH); |
| 615 | |
| 616 | if (access & NFS4_SHARE_ACCESS_WRITE) |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 617 | __nfs4_file_put_access(fp, O_WRONLY); |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 618 | if (access & NFS4_SHARE_ACCESS_READ) |
| 619 | __nfs4_file_put_access(fp, O_RDONLY); |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 620 | } |
| 621 | |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 622 | /* |
| 623 | * Allocate a new open/delegation state counter. This is needed for |
| 624 | * pNFS for proper return on close semantics. |
| 625 | * |
| 626 | * Note that we only allocate it for pNFS-enabled exports, otherwise |
| 627 | * all pointers to struct nfs4_clnt_odstate are always NULL. |
| 628 | */ |
| 629 | static struct nfs4_clnt_odstate * |
| 630 | alloc_clnt_odstate(struct nfs4_client *clp) |
| 631 | { |
| 632 | struct nfs4_clnt_odstate *co; |
| 633 | |
| 634 | co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL); |
| 635 | if (co) { |
| 636 | co->co_client = clp; |
Elena Reshetova | cff7cb2 | 2017-10-20 12:53:29 +0300 | [diff] [blame] | 637 | refcount_set(&co->co_odcount, 1); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 638 | } |
| 639 | return co; |
| 640 | } |
| 641 | |
| 642 | static void |
| 643 | hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co) |
| 644 | { |
| 645 | struct nfs4_file *fp = co->co_file; |
| 646 | |
| 647 | lockdep_assert_held(&fp->fi_lock); |
| 648 | list_add(&co->co_perfile, &fp->fi_clnt_odstate); |
| 649 | } |
| 650 | |
| 651 | static inline void |
| 652 | get_clnt_odstate(struct nfs4_clnt_odstate *co) |
| 653 | { |
| 654 | if (co) |
Elena Reshetova | cff7cb2 | 2017-10-20 12:53:29 +0300 | [diff] [blame] | 655 | refcount_inc(&co->co_odcount); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | static void |
| 659 | put_clnt_odstate(struct nfs4_clnt_odstate *co) |
| 660 | { |
| 661 | struct nfs4_file *fp; |
| 662 | |
| 663 | if (!co) |
| 664 | return; |
| 665 | |
| 666 | fp = co->co_file; |
Elena Reshetova | cff7cb2 | 2017-10-20 12:53:29 +0300 | [diff] [blame] | 667 | if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) { |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 668 | list_del(&co->co_perfile); |
| 669 | spin_unlock(&fp->fi_lock); |
| 670 | |
| 671 | nfsd4_return_all_file_layouts(co->co_client, fp); |
| 672 | kmem_cache_free(odstate_slab, co); |
| 673 | } |
| 674 | } |
| 675 | |
| 676 | static struct nfs4_clnt_odstate * |
| 677 | find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new) |
| 678 | { |
| 679 | struct nfs4_clnt_odstate *co; |
| 680 | struct nfs4_client *cl; |
| 681 | |
| 682 | if (!new) |
| 683 | return NULL; |
| 684 | |
| 685 | cl = new->co_client; |
| 686 | |
| 687 | spin_lock(&fp->fi_lock); |
| 688 | list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) { |
| 689 | if (co->co_client == cl) { |
| 690 | get_clnt_odstate(co); |
| 691 | goto out; |
| 692 | } |
| 693 | } |
| 694 | co = new; |
| 695 | co->co_file = fp; |
| 696 | hash_clnt_odstate_locked(new); |
| 697 | out: |
| 698 | spin_unlock(&fp->fi_lock); |
| 699 | return co; |
| 700 | } |
| 701 | |
Kinglong Mee | d19fb70 | 2017-01-18 19:04:42 +0800 | [diff] [blame] | 702 | struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab, |
| 703 | void (*sc_free)(struct nfs4_stid *)) |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 704 | { |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 705 | struct nfs4_stid *stid; |
| 706 | int new_id; |
| 707 | |
Trond Myklebust | f833883 | 2014-07-25 07:34:19 -0400 | [diff] [blame] | 708 | stid = kmem_cache_zalloc(slab, GFP_KERNEL); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 709 | if (!stid) |
| 710 | return NULL; |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 711 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 712 | idr_preload(GFP_KERNEL); |
| 713 | spin_lock(&cl->cl_lock); |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 714 | /* Reserving 0 for start of file in nfsdfs "states" file: */ |
| 715 | 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] | 716 | spin_unlock(&cl->cl_lock); |
| 717 | idr_preload_end(); |
Tejun Heo | ebd6c70 | 2013-03-13 14:59:37 -0700 | [diff] [blame] | 718 | if (new_id < 0) |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 719 | goto out_free; |
Kinglong Mee | d19fb70 | 2017-01-18 19:04:42 +0800 | [diff] [blame] | 720 | |
| 721 | stid->sc_free = sc_free; |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 722 | stid->sc_client = cl; |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 723 | stid->sc_stateid.si_opaque.so_id = new_id; |
| 724 | stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid; |
| 725 | /* Will be incremented before return to client: */ |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 726 | refcount_set(&stid->sc_count, 1); |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 727 | spin_lock_init(&stid->sc_lock); |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 728 | INIT_LIST_HEAD(&stid->sc_cp_list); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 729 | |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 730 | /* |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 731 | * It shouldn't be a problem to reuse an opaque stateid value. |
| 732 | * I don't think it is for 4.1. But with 4.0 I worry that, for |
| 733 | * example, a stray write retransmission could be accepted by |
| 734 | * the server when it should have been rejected. Therefore, |
| 735 | * adopt a trick from the sctp code to attempt to maximize the |
| 736 | * amount of time until an id is reused, by ensuring they always |
| 737 | * "increase" (mod INT_MAX): |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 738 | */ |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 739 | return stid; |
| 740 | out_free: |
Wei Yongjun | 2c44a23 | 2013-04-09 14:15:31 +0800 | [diff] [blame] | 741 | kmem_cache_free(slab, stid); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 742 | return NULL; |
J. Bruce Fields | 2a74aba | 2011-09-23 17:20:02 -0400 | [diff] [blame] | 743 | } |
| 744 | |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 745 | /* |
| 746 | * Create a unique stateid_t to represent each COPY. |
| 747 | */ |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 748 | static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid, |
| 749 | unsigned char sc_type) |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 750 | { |
| 751 | int new_id; |
| 752 | |
Arnd Bergmann | 9cc7680 | 2019-10-31 15:53:13 +0100 | [diff] [blame] | 753 | stid->stid.si_opaque.so_clid.cl_boot = (u32)nn->boot_time; |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 754 | stid->stid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id; |
| 755 | stid->sc_type = sc_type; |
| 756 | |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 757 | idr_preload(GFP_KERNEL); |
| 758 | spin_lock(&nn->s2s_cp_lock); |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 759 | new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, stid, 0, 0, GFP_NOWAIT); |
| 760 | stid->stid.si_opaque.so_id = new_id; |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 761 | spin_unlock(&nn->s2s_cp_lock); |
| 762 | idr_preload_end(); |
| 763 | if (new_id < 0) |
| 764 | return 0; |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 765 | return 1; |
| 766 | } |
| 767 | |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 768 | int nfs4_init_copy_state(struct nfsd_net *nn, struct nfsd4_copy *copy) |
| 769 | { |
| 770 | return nfs4_init_cp_state(nn, ©->cp_stateid, NFS4_COPY_STID); |
| 771 | } |
| 772 | |
| 773 | struct nfs4_cpntf_state *nfs4_alloc_init_cpntf_state(struct nfsd_net *nn, |
| 774 | struct nfs4_stid *p_stid) |
| 775 | { |
| 776 | struct nfs4_cpntf_state *cps; |
| 777 | |
| 778 | cps = kzalloc(sizeof(struct nfs4_cpntf_state), GFP_KERNEL); |
| 779 | if (!cps) |
| 780 | return NULL; |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 781 | cps->cpntf_time = ktime_get_boottime_seconds(); |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 782 | refcount_set(&cps->cp_stateid.sc_count, 1); |
| 783 | if (!nfs4_init_cp_state(nn, &cps->cp_stateid, NFS4_COPYNOTIFY_STID)) |
| 784 | goto out_free; |
| 785 | spin_lock(&nn->s2s_cp_lock); |
| 786 | list_add(&cps->cp_list, &p_stid->sc_cp_list); |
| 787 | spin_unlock(&nn->s2s_cp_lock); |
| 788 | return cps; |
| 789 | out_free: |
| 790 | kfree(cps); |
| 791 | return NULL; |
| 792 | } |
| 793 | |
| 794 | void nfs4_free_copy_state(struct nfsd4_copy *copy) |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 795 | { |
| 796 | struct nfsd_net *nn; |
| 797 | |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 798 | WARN_ON_ONCE(copy->cp_stateid.sc_type != NFS4_COPY_STID); |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 799 | nn = net_generic(copy->cp_clp->net, nfsd_net_id); |
| 800 | spin_lock(&nn->s2s_cp_lock); |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 801 | idr_remove(&nn->s2s_cp_stateids, |
| 802 | copy->cp_stateid.stid.si_opaque.so_id); |
| 803 | spin_unlock(&nn->s2s_cp_lock); |
| 804 | } |
| 805 | |
| 806 | static void nfs4_free_cpntf_statelist(struct net *net, struct nfs4_stid *stid) |
| 807 | { |
| 808 | struct nfs4_cpntf_state *cps; |
| 809 | struct nfsd_net *nn; |
| 810 | |
| 811 | nn = net_generic(net, nfsd_net_id); |
| 812 | spin_lock(&nn->s2s_cp_lock); |
| 813 | while (!list_empty(&stid->sc_cp_list)) { |
| 814 | cps = list_first_entry(&stid->sc_cp_list, |
| 815 | struct nfs4_cpntf_state, cp_list); |
| 816 | _free_cpntf_state_locked(nn, cps); |
| 817 | } |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 818 | spin_unlock(&nn->s2s_cp_lock); |
| 819 | } |
| 820 | |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 821 | 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] | 822 | { |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 823 | struct nfs4_stid *stid; |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 824 | |
Kinglong Mee | d19fb70 | 2017-01-18 19:04:42 +0800 | [diff] [blame] | 825 | stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 826 | if (!stid) |
| 827 | return NULL; |
| 828 | |
Kinglong Mee | d19fb70 | 2017-01-18 19:04:42 +0800 | [diff] [blame] | 829 | return openlockstateid(stid); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 830 | } |
| 831 | |
| 832 | static void nfs4_free_deleg(struct nfs4_stid *stid) |
| 833 | { |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 834 | kmem_cache_free(deleg_slab, stid); |
| 835 | atomic_long_dec(&num_delegations); |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 836 | } |
| 837 | |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 838 | /* |
| 839 | * When we recall a delegation, we should be careful not to hand it |
| 840 | * out again straight away. |
| 841 | * To ensure this we keep a pair of bloom filters ('new' and 'old') |
| 842 | * in which the filehandles of recalled delegations are "stored". |
| 843 | * If a filehandle appear in either filter, a delegation is blocked. |
| 844 | * When a delegation is recalled, the filehandle is stored in the "new" |
| 845 | * filter. |
| 846 | * Every 30 seconds we swap the filters and clear the "new" one, |
| 847 | * unless both are empty of course. |
| 848 | * |
| 849 | * Each filter is 256 bits. We hash the filehandle to 32bit and use the |
| 850 | * low 3 bytes as hash-table indices. |
| 851 | * |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 852 | * 'blocked_delegations_lock', which is always taken in block_delegations(), |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 853 | * is used to manage concurrent access. Testing does not need the lock |
| 854 | * except when swapping the two filters. |
| 855 | */ |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 856 | static DEFINE_SPINLOCK(blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 857 | static struct bloom_pair { |
| 858 | int entries, old_entries; |
Arnd Bergmann | b3f255e | 2017-10-20 11:25:34 +0200 | [diff] [blame] | 859 | time64_t swap_time; |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 860 | int new; /* index into 'set' */ |
| 861 | DECLARE_BITMAP(set[2], 256); |
| 862 | } blocked_delegations; |
| 863 | |
| 864 | static int delegation_blocked(struct knfsd_fh *fh) |
| 865 | { |
| 866 | u32 hash; |
| 867 | struct bloom_pair *bd = &blocked_delegations; |
| 868 | |
| 869 | if (bd->entries == 0) |
| 870 | return 0; |
Arnd Bergmann | b3f255e | 2017-10-20 11:25:34 +0200 | [diff] [blame] | 871 | if (ktime_get_seconds() - bd->swap_time > 30) { |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 872 | spin_lock(&blocked_delegations_lock); |
Arnd Bergmann | b3f255e | 2017-10-20 11:25:34 +0200 | [diff] [blame] | 873 | if (ktime_get_seconds() - bd->swap_time > 30) { |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 874 | bd->entries -= bd->old_entries; |
| 875 | bd->old_entries = bd->entries; |
| 876 | memset(bd->set[bd->new], 0, |
| 877 | sizeof(bd->set[0])); |
| 878 | bd->new = 1-bd->new; |
Arnd Bergmann | b3f255e | 2017-10-20 11:25:34 +0200 | [diff] [blame] | 879 | bd->swap_time = ktime_get_seconds(); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 880 | } |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 881 | spin_unlock(&blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 882 | } |
Daniel Borkmann | 8754589 | 2014-12-10 16:33:11 +0100 | [diff] [blame] | 883 | hash = jhash(&fh->fh_base, fh->fh_size, 0); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 884 | if (test_bit(hash&255, bd->set[0]) && |
| 885 | test_bit((hash>>8)&255, bd->set[0]) && |
| 886 | test_bit((hash>>16)&255, bd->set[0])) |
| 887 | return 1; |
| 888 | |
| 889 | if (test_bit(hash&255, bd->set[1]) && |
| 890 | test_bit((hash>>8)&255, bd->set[1]) && |
| 891 | test_bit((hash>>16)&255, bd->set[1])) |
| 892 | return 1; |
| 893 | |
| 894 | return 0; |
| 895 | } |
| 896 | |
| 897 | static void block_delegations(struct knfsd_fh *fh) |
| 898 | { |
| 899 | u32 hash; |
| 900 | struct bloom_pair *bd = &blocked_delegations; |
| 901 | |
Daniel Borkmann | 8754589 | 2014-12-10 16:33:11 +0100 | [diff] [blame] | 902 | hash = jhash(&fh->fh_base, fh->fh_size, 0); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 903 | |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 904 | spin_lock(&blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 905 | __set_bit(hash&255, bd->set[bd->new]); |
| 906 | __set_bit((hash>>8)&255, bd->set[bd->new]); |
| 907 | __set_bit((hash>>16)&255, bd->set[bd->new]); |
| 908 | if (bd->entries == 0) |
Arnd Bergmann | b3f255e | 2017-10-20 11:25:34 +0200 | [diff] [blame] | 909 | bd->swap_time = ktime_get_seconds(); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 910 | bd->entries += 1; |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 911 | spin_unlock(&blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 912 | } |
| 913 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | static struct nfs4_delegation * |
J. Bruce Fields | 86d29b1 | 2018-02-21 15:27:28 -0500 | [diff] [blame] | 915 | alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp, |
| 916 | struct svc_fh *current_fh, |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 917 | struct nfs4_clnt_odstate *odstate) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 918 | { |
| 919 | struct nfs4_delegation *dp; |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 920 | long n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 921 | |
| 922 | dprintk("NFSD alloc_init_deleg\n"); |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 923 | n = atomic_long_inc_return(&num_delegations); |
| 924 | if (n < 0 || n > max_delegations) |
| 925 | goto out_dec; |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 926 | if (delegation_blocked(¤t_fh->fh_handle)) |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 927 | goto out_dec; |
Kinglong Mee | d19fb70 | 2017-01-18 19:04:42 +0800 | [diff] [blame] | 928 | dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg)); |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 929 | if (dp == NULL) |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 930 | goto out_dec; |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 931 | |
J. Bruce Fields | 2a74aba | 2011-09-23 17:20:02 -0400 | [diff] [blame] | 932 | /* |
| 933 | * delegation seqid's are never incremented. The 4.1 special |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 934 | * meaning of seqid 0 isn't meaningful, really, but let's avoid |
| 935 | * 0 anyway just for consistency and use 1: |
J. Bruce Fields | 2a74aba | 2011-09-23 17:20:02 -0400 | [diff] [blame] | 936 | */ |
| 937 | dp->dl_stid.sc_stateid.si_generation = 1; |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 938 | INIT_LIST_HEAD(&dp->dl_perfile); |
| 939 | INIT_LIST_HEAD(&dp->dl_perclnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | INIT_LIST_HEAD(&dp->dl_recall_lru); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 941 | dp->dl_clnt_odstate = odstate; |
| 942 | get_clnt_odstate(odstate); |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 943 | dp->dl_type = NFS4_OPEN_DELEGATE_READ; |
Christoph Hellwig | f0b5de1 | 2014-09-24 12:19:18 +0200 | [diff] [blame] | 944 | dp->dl_retries = 1; |
| 945 | nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client, |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 946 | &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL); |
J. Bruce Fields | 86d29b1 | 2018-02-21 15:27:28 -0500 | [diff] [blame] | 947 | get_nfs4_file(fp); |
| 948 | dp->dl_stid.sc_file = fp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 949 | return dp; |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 950 | out_dec: |
| 951 | atomic_long_dec(&num_delegations); |
| 952 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 953 | } |
| 954 | |
| 955 | void |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 956 | nfs4_put_stid(struct nfs4_stid *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 958 | struct nfs4_file *fp = s->sc_file; |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 959 | struct nfs4_client *clp = s->sc_client; |
| 960 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 961 | might_lock(&clp->cl_lock); |
| 962 | |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 963 | if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) { |
Jeff Layton | b401be22 | 2014-07-29 21:34:33 -0400 | [diff] [blame] | 964 | wake_up_all(&close_wq); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 965 | return; |
Jeff Layton | b401be22 | 2014-07-29 21:34:33 -0400 | [diff] [blame] | 966 | } |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 967 | idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id); |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 968 | nfs4_free_cpntf_statelist(clp->net, s); |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 969 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 970 | s->sc_free(s); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 971 | if (fp) |
| 972 | put_nfs4_file(fp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 973 | } |
| 974 | |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 975 | void |
| 976 | nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid) |
| 977 | { |
| 978 | stateid_t *src = &stid->sc_stateid; |
| 979 | |
| 980 | spin_lock(&stid->sc_lock); |
| 981 | if (unlikely(++src->si_generation == 0)) |
| 982 | src->si_generation = 1; |
| 983 | memcpy(dst, src, sizeof(*dst)); |
| 984 | spin_unlock(&stid->sc_lock); |
| 985 | } |
| 986 | |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 987 | static void put_deleg_file(struct nfs4_file *fp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | { |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 989 | struct nfsd_file *nf = NULL; |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 990 | |
| 991 | spin_lock(&fp->fi_lock); |
| 992 | if (--fp->fi_delegees == 0) |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 993 | swap(nf, fp->fi_deleg_file); |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 994 | spin_unlock(&fp->fi_lock); |
| 995 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 996 | if (nf) |
| 997 | nfsd_file_put(nf); |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp) |
| 1001 | { |
J. Bruce Fields | cba7b3d | 2018-02-15 15:50:49 -0500 | [diff] [blame] | 1002 | struct nfs4_file *fp = dp->dl_stid.sc_file; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 1003 | struct nfsd_file *nf = fp->fi_deleg_file; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 1004 | |
J. Bruce Fields | b8232d3 | 2018-02-15 15:29:15 -0500 | [diff] [blame] | 1005 | WARN_ON_ONCE(!fp->fi_delegees); |
| 1006 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 1007 | vfs_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp); |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 1008 | put_deleg_file(fp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | } |
| 1010 | |
J. Bruce Fields | 0af6e69 | 2018-02-21 15:11:03 -0500 | [diff] [blame] | 1011 | static void destroy_unhashed_deleg(struct nfs4_delegation *dp) |
| 1012 | { |
| 1013 | put_clnt_odstate(dp->dl_clnt_odstate); |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 1014 | nfs4_unlock_deleg_lease(dp); |
J. Bruce Fields | 0af6e69 | 2018-02-21 15:11:03 -0500 | [diff] [blame] | 1015 | nfs4_put_stid(&dp->dl_stid); |
| 1016 | } |
| 1017 | |
Christoph Hellwig | cd61c52 | 2014-08-14 08:44:57 +0200 | [diff] [blame] | 1018 | void nfs4_unhash_stid(struct nfs4_stid *s) |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 1019 | { |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 1020 | s->sc_type = 0; |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 1021 | } |
| 1022 | |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1023 | /** |
J. Bruce Fields | 68b18f5 | 2018-02-19 11:38:33 -0500 | [diff] [blame] | 1024 | * nfs4_delegation_exists - Discover if this delegation already exists |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1025 | * @clp: a pointer to the nfs4_client we're granting a delegation to |
| 1026 | * @fp: a pointer to the nfs4_file we're granting a delegation on |
| 1027 | * |
| 1028 | * Return: |
J. Bruce Fields | 68b18f5 | 2018-02-19 11:38:33 -0500 | [diff] [blame] | 1029 | * On success: true iff an existing delegation is found |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1030 | */ |
| 1031 | |
J. Bruce Fields | 68b18f5 | 2018-02-19 11:38:33 -0500 | [diff] [blame] | 1032 | static bool |
| 1033 | nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp) |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 1034 | { |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1035 | struct nfs4_delegation *searchdp = NULL; |
| 1036 | struct nfs4_client *searchclp = NULL; |
| 1037 | |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 1038 | lockdep_assert_held(&state_lock); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 1039 | lockdep_assert_held(&fp->fi_lock); |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 1040 | |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1041 | list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) { |
| 1042 | searchclp = searchdp->dl_stid.sc_client; |
| 1043 | if (clp == searchclp) { |
Fengguang Wu | 51d87bc | 2018-03-22 13:37:20 +0800 | [diff] [blame] | 1044 | return true; |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1045 | } |
| 1046 | } |
Fengguang Wu | 51d87bc | 2018-03-22 13:37:20 +0800 | [diff] [blame] | 1047 | return false; |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | /** |
| 1051 | * hash_delegation_locked - Add a delegation to the appropriate lists |
| 1052 | * @dp: a pointer to the nfs4_delegation we are adding. |
| 1053 | * @fp: a pointer to the nfs4_file we're granting a delegation on |
| 1054 | * |
| 1055 | * Return: |
| 1056 | * On success: NULL if the delegation was successfully hashed. |
| 1057 | * |
| 1058 | * On error: -EAGAIN if one was previously granted to this |
| 1059 | * nfs4_client for this nfs4_file. Delegation is not hashed. |
| 1060 | * |
| 1061 | */ |
| 1062 | |
| 1063 | static int |
| 1064 | hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp) |
| 1065 | { |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1066 | struct nfs4_client *clp = dp->dl_stid.sc_client; |
| 1067 | |
| 1068 | lockdep_assert_held(&state_lock); |
| 1069 | lockdep_assert_held(&fp->fi_lock); |
| 1070 | |
J. Bruce Fields | 68b18f5 | 2018-02-19 11:38:33 -0500 | [diff] [blame] | 1071 | if (nfs4_delegation_exists(clp, fp)) |
| 1072 | return -EAGAIN; |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 1073 | refcount_inc(&dp->dl_stid.sc_count); |
Benny Halevy | 3fb87d1 | 2014-05-30 09:09:31 -0400 | [diff] [blame] | 1074 | dp->dl_stid.sc_type = NFS4_DELEG_STID; |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 1075 | list_add(&dp->dl_perfile, &fp->fi_delegations); |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1076 | list_add(&dp->dl_perclnt, &clp->cl_delegations); |
| 1077 | return 0; |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 1078 | } |
| 1079 | |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 1080 | static bool |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 1081 | unhash_delegation_locked(struct nfs4_delegation *dp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 1083 | struct nfs4_file *fp = dp->dl_stid.sc_file; |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 1084 | |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 1085 | lockdep_assert_held(&state_lock); |
| 1086 | |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 1087 | if (list_empty(&dp->dl_perfile)) |
| 1088 | return false; |
| 1089 | |
Trond Myklebust | b0fc29d | 2014-07-16 10:31:59 -0400 | [diff] [blame] | 1090 | dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID; |
Jeff Layton | d55a166 | 2014-07-22 13:52:06 -0400 | [diff] [blame] | 1091 | /* Ensure that deleg break won't try to requeue it */ |
| 1092 | ++dp->dl_time; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 1093 | spin_lock(&fp->fi_lock); |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 1094 | list_del_init(&dp->dl_perclnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1095 | list_del_init(&dp->dl_recall_lru); |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 1096 | list_del_init(&dp->dl_perfile); |
| 1097 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 1098 | return true; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 1099 | } |
| 1100 | |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 1101 | static void destroy_delegation(struct nfs4_delegation *dp) |
| 1102 | { |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 1103 | bool unhashed; |
| 1104 | |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 1105 | spin_lock(&state_lock); |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 1106 | unhashed = unhash_delegation_locked(dp); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 1107 | spin_unlock(&state_lock); |
J. Bruce Fields | 0af6e69 | 2018-02-21 15:11:03 -0500 | [diff] [blame] | 1108 | if (unhashed) |
| 1109 | destroy_unhashed_deleg(dp); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 1110 | } |
| 1111 | |
| 1112 | static void revoke_delegation(struct nfs4_delegation *dp) |
| 1113 | { |
| 1114 | struct nfs4_client *clp = dp->dl_stid.sc_client; |
| 1115 | |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 1116 | WARN_ON(!list_empty(&dp->dl_recall_lru)); |
| 1117 | |
J. Bruce Fields | 0af6e69 | 2018-02-21 15:11:03 -0500 | [diff] [blame] | 1118 | if (clp->cl_minorversion) { |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 1119 | dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID; |
J. Bruce Fields | 0af6e69 | 2018-02-21 15:11:03 -0500 | [diff] [blame] | 1120 | refcount_inc(&dp->dl_stid.sc_count); |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 1121 | spin_lock(&clp->cl_lock); |
| 1122 | list_add(&dp->dl_recall_lru, &clp->cl_revoked); |
| 1123 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 1124 | } |
J. Bruce Fields | 0af6e69 | 2018-02-21 15:11:03 -0500 | [diff] [blame] | 1125 | destroy_unhashed_deleg(dp); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 1126 | } |
| 1127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1128 | /* |
| 1129 | * SETCLIENTID state |
| 1130 | */ |
| 1131 | |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 1132 | static unsigned int clientid_hashval(u32 id) |
| 1133 | { |
| 1134 | return id & CLIENT_HASH_MASK; |
| 1135 | } |
| 1136 | |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 1137 | static unsigned int clientstr_hashval(struct xdr_netobj name) |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 1138 | { |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 1139 | return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK; |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 1140 | } |
| 1141 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | /* |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1143 | * We store the NONE, READ, WRITE, and BOTH bits separately in the |
| 1144 | * st_{access,deny}_bmap field of the stateid, in order to track not |
| 1145 | * only what share bits are currently in force, but also what |
| 1146 | * combinations of share bits previous opens have used. This allows us |
| 1147 | * to enforce the recommendation of rfc 3530 14.2.19 that the server |
| 1148 | * return an error if the client attempt to downgrade to a combination |
| 1149 | * of share bits not explicable by closing some of its previous opens. |
| 1150 | * |
| 1151 | * XXX: This enforcement is actually incomplete, since we don't keep |
| 1152 | * track of access/deny bit combinations; so, e.g., we allow: |
| 1153 | * |
| 1154 | * OPEN allow read, deny write |
| 1155 | * OPEN allow both, deny none |
| 1156 | * DOWNGRADE allow read, deny none |
| 1157 | * |
| 1158 | * which we should reject. |
| 1159 | */ |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 1160 | static unsigned int |
| 1161 | bmap_to_share_mode(unsigned long bmap) { |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1162 | int i; |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 1163 | unsigned int access = 0; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1164 | |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1165 | for (i = 1; i < 4; i++) { |
| 1166 | if (test_bit(i, &bmap)) |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 1167 | access |= i; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1168 | } |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 1169 | return access; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1170 | } |
| 1171 | |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 1172 | /* set share access for a given stateid */ |
| 1173 | static inline void |
| 1174 | set_access(u32 access, struct nfs4_ol_stateid *stp) |
| 1175 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1176 | unsigned char mask = 1 << access; |
| 1177 | |
| 1178 | WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH); |
| 1179 | stp->st_access_bmap |= mask; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | /* clear share access for a given stateid */ |
| 1183 | static inline void |
| 1184 | clear_access(u32 access, struct nfs4_ol_stateid *stp) |
| 1185 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1186 | unsigned char mask = 1 << access; |
| 1187 | |
| 1188 | WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH); |
| 1189 | stp->st_access_bmap &= ~mask; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 1190 | } |
| 1191 | |
| 1192 | /* test whether a given stateid has access */ |
| 1193 | static inline bool |
| 1194 | test_access(u32 access, struct nfs4_ol_stateid *stp) |
| 1195 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1196 | unsigned char mask = 1 << access; |
| 1197 | |
| 1198 | return (bool)(stp->st_access_bmap & mask); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 1199 | } |
| 1200 | |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 1201 | /* set share deny for a given stateid */ |
| 1202 | static inline void |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1203 | set_deny(u32 deny, struct nfs4_ol_stateid *stp) |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 1204 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1205 | unsigned char mask = 1 << deny; |
| 1206 | |
| 1207 | WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH); |
| 1208 | stp->st_deny_bmap |= mask; |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 1209 | } |
| 1210 | |
| 1211 | /* clear share deny for a given stateid */ |
| 1212 | static inline void |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1213 | clear_deny(u32 deny, struct nfs4_ol_stateid *stp) |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 1214 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1215 | unsigned char mask = 1 << deny; |
| 1216 | |
| 1217 | WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH); |
| 1218 | stp->st_deny_bmap &= ~mask; |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 1219 | } |
| 1220 | |
| 1221 | /* test whether a given stateid is denying specific access */ |
| 1222 | static inline bool |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1223 | test_deny(u32 deny, struct nfs4_ol_stateid *stp) |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 1224 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1225 | unsigned char mask = 1 << deny; |
| 1226 | |
| 1227 | return (bool)(stp->st_deny_bmap & mask); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | static int nfs4_access_to_omode(u32 access) |
| 1231 | { |
J. Bruce Fields | 8f34a43 | 2010-09-02 15:23:16 -0400 | [diff] [blame] | 1232 | switch (access & NFS4_SHARE_ACCESS_BOTH) { |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1233 | case NFS4_SHARE_ACCESS_READ: |
| 1234 | return O_RDONLY; |
| 1235 | case NFS4_SHARE_ACCESS_WRITE: |
| 1236 | return O_WRONLY; |
| 1237 | case NFS4_SHARE_ACCESS_BOTH: |
| 1238 | return O_RDWR; |
| 1239 | } |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 1240 | WARN_ON_ONCE(1); |
| 1241 | return O_RDONLY; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1242 | } |
| 1243 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 1244 | /* |
| 1245 | * A stateid that had a deny mode associated with it is being released |
| 1246 | * or downgraded. Recalculate the deny mode on the file. |
| 1247 | */ |
| 1248 | static void |
| 1249 | recalculate_deny_mode(struct nfs4_file *fp) |
| 1250 | { |
| 1251 | struct nfs4_ol_stateid *stp; |
| 1252 | |
| 1253 | spin_lock(&fp->fi_lock); |
| 1254 | fp->fi_share_deny = 0; |
| 1255 | list_for_each_entry(stp, &fp->fi_stateids, st_perfile) |
| 1256 | fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap); |
| 1257 | spin_unlock(&fp->fi_lock); |
| 1258 | } |
| 1259 | |
| 1260 | static void |
| 1261 | reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp) |
| 1262 | { |
| 1263 | int i; |
| 1264 | bool change = false; |
| 1265 | |
| 1266 | for (i = 1; i < 4; i++) { |
| 1267 | if ((i & deny) != i) { |
| 1268 | change = true; |
| 1269 | clear_deny(i, stp); |
| 1270 | } |
| 1271 | } |
| 1272 | |
| 1273 | /* Recalculate per-file deny mode if there was a change */ |
| 1274 | if (change) |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 1275 | recalculate_deny_mode(stp->st_stid.sc_file); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 1276 | } |
| 1277 | |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 1278 | /* release all access and file references for a given stateid */ |
| 1279 | static void |
| 1280 | release_all_access(struct nfs4_ol_stateid *stp) |
| 1281 | { |
| 1282 | int i; |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 1283 | struct nfs4_file *fp = stp->st_stid.sc_file; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 1284 | |
| 1285 | if (fp && stp->st_deny_bmap != 0) |
| 1286 | recalculate_deny_mode(fp); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 1287 | |
| 1288 | for (i = 1; i < 4; i++) { |
| 1289 | if (test_access(i, stp)) |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 1290 | nfs4_file_put_access(stp->st_stid.sc_file, i); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 1291 | clear_access(i, stp); |
| 1292 | } |
| 1293 | } |
| 1294 | |
Kinglong Mee | d50ffde | 2015-07-16 12:05:07 +0800 | [diff] [blame] | 1295 | static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop) |
| 1296 | { |
| 1297 | kfree(sop->so_owner.data); |
| 1298 | sop->so_ops->so_free(sop); |
| 1299 | } |
| 1300 | |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 1301 | static void nfs4_put_stateowner(struct nfs4_stateowner *sop) |
| 1302 | { |
Jeff Layton | a819ecc | 2014-07-29 21:34:38 -0400 | [diff] [blame] | 1303 | struct nfs4_client *clp = sop->so_client; |
| 1304 | |
| 1305 | might_lock(&clp->cl_lock); |
| 1306 | |
| 1307 | if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock)) |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 1308 | return; |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 1309 | sop->so_ops->so_unhash(sop); |
Jeff Layton | a819ecc | 2014-07-29 21:34:38 -0400 | [diff] [blame] | 1310 | spin_unlock(&clp->cl_lock); |
Kinglong Mee | d50ffde | 2015-07-16 12:05:07 +0800 | [diff] [blame] | 1311 | nfs4_free_stateowner(sop); |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 1312 | } |
| 1313 | |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 1314 | static bool |
| 1315 | nfs4_ol_stateid_unhashed(const struct nfs4_ol_stateid *stp) |
| 1316 | { |
| 1317 | return list_empty(&stp->st_perfile); |
| 1318 | } |
| 1319 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1320 | static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1321 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 1322 | struct nfs4_file *fp = stp->st_stid.sc_file; |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 1323 | |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 1324 | lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock); |
| 1325 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1326 | if (list_empty(&stp->st_perfile)) |
| 1327 | return false; |
| 1328 | |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 1329 | spin_lock(&fp->fi_lock); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1330 | list_del_init(&stp->st_perfile); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 1331 | spin_unlock(&fp->fi_lock); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1332 | list_del(&stp->st_perstateowner); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1333 | return true; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1334 | } |
| 1335 | |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1336 | static void nfs4_free_ol_stateid(struct nfs4_stid *stid) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1337 | { |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1338 | struct nfs4_ol_stateid *stp = openlockstateid(stid); |
J. Bruce Fields | 4665e2b | 2011-09-06 14:50:49 -0400 | [diff] [blame] | 1339 | |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 1340 | put_clnt_odstate(stp->st_clnt_odstate); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1341 | release_all_access(stp); |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 1342 | if (stp->st_stateowner) |
| 1343 | nfs4_put_stateowner(stp->st_stateowner); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1344 | kmem_cache_free(stateid_slab, stid); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1345 | } |
| 1346 | |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 1347 | static void nfs4_free_lock_stateid(struct nfs4_stid *stid) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1348 | { |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 1349 | struct nfs4_ol_stateid *stp = openlockstateid(stid); |
| 1350 | struct nfs4_lockowner *lo = lockowner(stp->st_stateowner); |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 1351 | struct nfsd_file *nf; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1352 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 1353 | nf = find_any_file(stp->st_stid.sc_file); |
| 1354 | if (nf) { |
| 1355 | get_file(nf->nf_file); |
| 1356 | filp_close(nf->nf_file, (fl_owner_t)lo); |
| 1357 | nfsd_file_put(nf); |
| 1358 | } |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 1359 | nfs4_free_ol_stateid(stid); |
| 1360 | } |
| 1361 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1362 | /* |
| 1363 | * Put the persistent reference to an already unhashed generic stateid, while |
| 1364 | * holding the cl_lock. If it's the last reference, then put it onto the |
| 1365 | * reaplist for later destruction. |
| 1366 | */ |
| 1367 | static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp, |
| 1368 | struct list_head *reaplist) |
| 1369 | { |
| 1370 | struct nfs4_stid *s = &stp->st_stid; |
| 1371 | struct nfs4_client *clp = s->sc_client; |
| 1372 | |
| 1373 | lockdep_assert_held(&clp->cl_lock); |
| 1374 | |
| 1375 | WARN_ON_ONCE(!list_empty(&stp->st_locks)); |
| 1376 | |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 1377 | if (!refcount_dec_and_test(&s->sc_count)) { |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1378 | wake_up_all(&close_wq); |
| 1379 | return; |
| 1380 | } |
| 1381 | |
| 1382 | idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id); |
| 1383 | list_add(&stp->st_locks, reaplist); |
| 1384 | } |
| 1385 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1386 | static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp) |
Jeff Layton | 3c1c995 | 2014-07-29 21:34:39 -0400 | [diff] [blame] | 1387 | { |
Chuck Lever | f46c445 | 2016-10-29 18:19:03 -0400 | [diff] [blame] | 1388 | lockdep_assert_held(&stp->st_stid.sc_client->cl_lock); |
Jeff Layton | 3c1c995 | 2014-07-29 21:34:39 -0400 | [diff] [blame] | 1389 | |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 1390 | if (!unhash_ol_stateid(stp)) |
| 1391 | return false; |
Jeff Layton | 3c1c995 | 2014-07-29 21:34:39 -0400 | [diff] [blame] | 1392 | list_del_init(&stp->st_locks); |
Christoph Hellwig | cd61c52 | 2014-08-14 08:44:57 +0200 | [diff] [blame] | 1393 | nfs4_unhash_stid(&stp->st_stid); |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 1394 | return true; |
Jeff Layton | 3c1c995 | 2014-07-29 21:34:39 -0400 | [diff] [blame] | 1395 | } |
| 1396 | |
Jeff Layton | 5adfd88 | 2014-07-29 21:34:31 -0400 | [diff] [blame] | 1397 | static void release_lock_stateid(struct nfs4_ol_stateid *stp) |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 1398 | { |
Chuck Lever | f46c445 | 2016-10-29 18:19:03 -0400 | [diff] [blame] | 1399 | struct nfs4_client *clp = stp->st_stid.sc_client; |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1400 | bool unhashed; |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 1401 | |
Chuck Lever | f46c445 | 2016-10-29 18:19:03 -0400 | [diff] [blame] | 1402 | spin_lock(&clp->cl_lock); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1403 | unhashed = unhash_lock_stateid(stp); |
Chuck Lever | f46c445 | 2016-10-29 18:19:03 -0400 | [diff] [blame] | 1404 | spin_unlock(&clp->cl_lock); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1405 | if (unhashed) |
| 1406 | nfs4_put_stid(&stp->st_stid); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1407 | } |
| 1408 | |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 1409 | static void unhash_lockowner_locked(struct nfs4_lockowner *lo) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1410 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1411 | struct nfs4_client *clp = lo->lo_owner.so_client; |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 1412 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1413 | lockdep_assert_held(&clp->cl_lock); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 1414 | |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 1415 | list_del_init(&lo->lo_owner.so_strhash); |
| 1416 | } |
| 1417 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1418 | /* |
| 1419 | * Free a list of generic stateids that were collected earlier after being |
| 1420 | * fully unhashed. |
| 1421 | */ |
| 1422 | static void |
| 1423 | free_ol_stateid_reaplist(struct list_head *reaplist) |
| 1424 | { |
| 1425 | struct nfs4_ol_stateid *stp; |
Kinglong Mee | fb94d76 | 2014-08-05 21:20:27 +0800 | [diff] [blame] | 1426 | struct nfs4_file *fp; |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1427 | |
| 1428 | might_sleep(); |
| 1429 | |
| 1430 | while (!list_empty(reaplist)) { |
| 1431 | stp = list_first_entry(reaplist, struct nfs4_ol_stateid, |
| 1432 | st_locks); |
| 1433 | list_del(&stp->st_locks); |
Kinglong Mee | fb94d76 | 2014-08-05 21:20:27 +0800 | [diff] [blame] | 1434 | fp = stp->st_stid.sc_file; |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1435 | stp->st_stid.sc_free(&stp->st_stid); |
Kinglong Mee | fb94d76 | 2014-08-05 21:20:27 +0800 | [diff] [blame] | 1436 | if (fp) |
| 1437 | put_nfs4_file(fp); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1438 | } |
| 1439 | } |
| 1440 | |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 1441 | static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp, |
| 1442 | struct list_head *reaplist) |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 1443 | { |
| 1444 | struct nfs4_ol_stateid *stp; |
| 1445 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1446 | lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock); |
| 1447 | |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 1448 | while (!list_empty(&open_stp->st_locks)) { |
| 1449 | stp = list_entry(open_stp->st_locks.next, |
| 1450 | struct nfs4_ol_stateid, st_locks); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1451 | WARN_ON(!unhash_lock_stateid(stp)); |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 1452 | put_ol_stateid_locked(stp, reaplist); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1453 | } |
| 1454 | } |
| 1455 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1456 | static bool unhash_open_stateid(struct nfs4_ol_stateid *stp, |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 1457 | struct list_head *reaplist) |
J. Bruce Fields | 2283963 | 2009-01-11 14:27:17 -0500 | [diff] [blame] | 1458 | { |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1459 | lockdep_assert_held(&stp->st_stid.sc_client->cl_lock); |
| 1460 | |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 1461 | if (!unhash_ol_stateid(stp)) |
| 1462 | return false; |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 1463 | release_open_stateid_locks(stp, reaplist); |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 1464 | return true; |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 1465 | } |
| 1466 | |
| 1467 | static void release_open_stateid(struct nfs4_ol_stateid *stp) |
| 1468 | { |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1469 | LIST_HEAD(reaplist); |
| 1470 | |
| 1471 | spin_lock(&stp->st_stid.sc_client->cl_lock); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1472 | if (unhash_open_stateid(stp, &reaplist)) |
| 1473 | put_ol_stateid_locked(stp, &reaplist); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1474 | spin_unlock(&stp->st_stid.sc_client->cl_lock); |
| 1475 | free_ol_stateid_reaplist(&reaplist); |
J. Bruce Fields | 2283963 | 2009-01-11 14:27:17 -0500 | [diff] [blame] | 1476 | } |
| 1477 | |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1478 | static void unhash_openowner_locked(struct nfs4_openowner *oo) |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 1479 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1480 | struct nfs4_client *clp = oo->oo_owner.so_client; |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1481 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1482 | lockdep_assert_held(&clp->cl_lock); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1483 | |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 1484 | list_del_init(&oo->oo_owner.so_strhash); |
| 1485 | list_del_init(&oo->oo_perclient); |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 1486 | } |
| 1487 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1488 | static void release_last_closed_stateid(struct nfs4_openowner *oo) |
| 1489 | { |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 1490 | struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net, |
| 1491 | nfsd_net_id); |
| 1492 | struct nfs4_ol_stateid *s; |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1493 | |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 1494 | spin_lock(&nn->client_lock); |
| 1495 | s = oo->oo_last_closed_stid; |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1496 | if (s) { |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 1497 | list_del_init(&oo->oo_close_lru); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1498 | oo->oo_last_closed_stid = NULL; |
| 1499 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 1500 | spin_unlock(&nn->client_lock); |
| 1501 | if (s) |
| 1502 | nfs4_put_stid(&s->st_stid); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1503 | } |
| 1504 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1505 | static void release_openowner(struct nfs4_openowner *oo) |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 1506 | { |
| 1507 | struct nfs4_ol_stateid *stp; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1508 | struct nfs4_client *clp = oo->oo_owner.so_client; |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1509 | struct list_head reaplist; |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1510 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1511 | INIT_LIST_HEAD(&reaplist); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1512 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1513 | spin_lock(&clp->cl_lock); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1514 | unhash_openowner_locked(oo); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1515 | while (!list_empty(&oo->oo_owner.so_stateids)) { |
| 1516 | stp = list_first_entry(&oo->oo_owner.so_stateids, |
| 1517 | struct nfs4_ol_stateid, st_perstateowner); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1518 | if (unhash_open_stateid(stp, &reaplist)) |
| 1519 | put_ol_stateid_locked(stp, &reaplist); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1520 | } |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1521 | spin_unlock(&clp->cl_lock); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1522 | free_ol_stateid_reaplist(&reaplist); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1523 | release_last_closed_stateid(oo); |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 1524 | nfs4_put_stateowner(&oo->oo_owner); |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 1525 | } |
| 1526 | |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1527 | static inline int |
| 1528 | hash_sessionid(struct nfs4_sessionid *sessionid) |
| 1529 | { |
| 1530 | struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid; |
| 1531 | |
| 1532 | return sid->sequence % SESSION_HASH_SIZE; |
| 1533 | } |
| 1534 | |
Mark Salter | 135dd00 | 2015-04-06 09:46:00 -0400 | [diff] [blame] | 1535 | #ifdef CONFIG_SUNRPC_DEBUG |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1536 | static inline void |
| 1537 | dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) |
| 1538 | { |
| 1539 | u32 *ptr = (u32 *)(&sessionid->data[0]); |
| 1540 | dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]); |
| 1541 | } |
Trond Myklebust | 8f199b8 | 2012-03-20 15:11:17 -0400 | [diff] [blame] | 1542 | #else |
| 1543 | static inline void |
| 1544 | dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) |
| 1545 | { |
| 1546 | } |
| 1547 | #endif |
| 1548 | |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 1549 | /* |
| 1550 | * Bump the seqid on cstate->replay_owner, and clear replay_owner if it |
| 1551 | * won't be used for replay. |
| 1552 | */ |
| 1553 | void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr) |
| 1554 | { |
| 1555 | struct nfs4_stateowner *so = cstate->replay_owner; |
| 1556 | |
| 1557 | if (nfserr == nfserr_replay_me) |
| 1558 | return; |
| 1559 | |
| 1560 | if (!seqid_mutating_err(ntohl(nfserr))) { |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 1561 | nfsd4_cstate_clear_replay(cstate); |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 1562 | return; |
| 1563 | } |
| 1564 | if (!so) |
| 1565 | return; |
| 1566 | if (so->so_is_open_owner) |
| 1567 | release_last_closed_stateid(openowner(so)); |
| 1568 | so->so_seqid++; |
| 1569 | return; |
| 1570 | } |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1571 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1572 | static void |
| 1573 | gen_sessionid(struct nfsd4_session *ses) |
| 1574 | { |
| 1575 | struct nfs4_client *clp = ses->se_client; |
| 1576 | struct nfsd4_sessionid *sid; |
| 1577 | |
| 1578 | sid = (struct nfsd4_sessionid *)ses->se_sessionid.data; |
| 1579 | sid->clientid = clp->cl_clientid; |
| 1580 | sid->sequence = current_sessionid++; |
| 1581 | sid->reserved = 0; |
| 1582 | } |
| 1583 | |
| 1584 | /* |
Andy Adamson | a649637c7 | 2009-08-28 08:45:01 -0400 | [diff] [blame] | 1585 | * The protocol defines ca_maxresponssize_cached to include the size of |
| 1586 | * the rpc header, but all we need to cache is the data starting after |
| 1587 | * the end of the initial SEQUENCE operation--the rest we regenerate |
| 1588 | * each time. Therefore we can advertise a ca_maxresponssize_cached |
| 1589 | * value that is the number of bytes in our cache plus a few additional |
| 1590 | * bytes. In order to stay on the safe side, and not promise more than |
| 1591 | * we can cache, those additional bytes must be the minimum possible: 24 |
| 1592 | * bytes of rpc header (xid through accept state, with AUTH_NULL |
| 1593 | * verifier), 12 for the compound header (with zero-length tag), and 44 |
| 1594 | * for the SEQUENCE op response: |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1595 | */ |
Andy Adamson | a649637c7 | 2009-08-28 08:45:01 -0400 | [diff] [blame] | 1596 | #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44) |
| 1597 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1598 | static void |
| 1599 | free_session_slots(struct nfsd4_session *ses) |
| 1600 | { |
| 1601 | int i; |
| 1602 | |
J. Bruce Fields | 53da6a5 | 2017-10-17 20:38:49 -0400 | [diff] [blame] | 1603 | for (i = 0; i < ses->se_fchannel.maxreqs; i++) { |
| 1604 | free_svc_cred(&ses->se_slots[i]->sl_cred); |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1605 | kfree(ses->se_slots[i]); |
J. Bruce Fields | 53da6a5 | 2017-10-17 20:38:49 -0400 | [diff] [blame] | 1606 | } |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1607 | } |
| 1608 | |
J. Bruce Fields | efe0cb6 | 2009-10-24 20:52:16 -0400 | [diff] [blame] | 1609 | /* |
| 1610 | * We don't actually need to cache the rpc and session headers, so we |
| 1611 | * can allocate a little less for each slot: |
| 1612 | */ |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1613 | static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca) |
J. Bruce Fields | efe0cb6 | 2009-10-24 20:52:16 -0400 | [diff] [blame] | 1614 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1615 | u32 size; |
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 | if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ) |
| 1618 | size = 0; |
| 1619 | else |
| 1620 | size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ; |
| 1621 | return size + sizeof(struct nfsd4_slot); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1622 | } |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1623 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1624 | /* |
| 1625 | * XXX: If we run out of reserved DRC memory we could (up to a point) |
| 1626 | * re-negotiate active sessions and reduce their slot usage to make |
Justin P. Mattock | 42b2aa8 | 2011-11-28 20:31:00 -0800 | [diff] [blame] | 1627 | * 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] | 1628 | */ |
NeilBrown | 2030ca5 | 2019-09-20 16:36:45 +1000 | [diff] [blame] | 1629 | 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] | 1630 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1631 | u32 slotsize = slot_bytes(ca); |
| 1632 | u32 num = ca->maxreqs; |
J. Bruce Fields | c54f24e | 2019-02-21 10:47:00 -0500 | [diff] [blame] | 1633 | unsigned long avail, total_avail; |
NeilBrown | 2030ca5 | 2019-09-20 16:36:45 +1000 | [diff] [blame] | 1634 | unsigned int scale_factor; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1635 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1636 | spin_lock(&nfsd_drc_lock); |
NeilBrown | 7f49fd5d | 2019-09-20 16:33:16 +1000 | [diff] [blame] | 1637 | if (nfsd_drc_max_mem > nfsd_drc_mem_used) |
| 1638 | total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used; |
| 1639 | else |
| 1640 | /* We have handed out more space than we chose in |
| 1641 | * set_max_drc() to allow. That isn't really a |
| 1642 | * problem as long as that doesn't make us think we |
| 1643 | * have lots more due to integer overflow. |
| 1644 | */ |
| 1645 | total_avail = 0; |
J. Bruce Fields | c54f24e | 2019-02-21 10:47:00 -0500 | [diff] [blame] | 1646 | avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail); |
J. Bruce Fields | de766e5 | 2017-09-19 19:25:41 -0400 | [diff] [blame] | 1647 | /* |
NeilBrown | 2030ca5 | 2019-09-20 16:36:45 +1000 | [diff] [blame] | 1648 | * Never use more than a fraction of the remaining memory, |
NeilBrown | 7f49fd5d | 2019-09-20 16:33:16 +1000 | [diff] [blame] | 1649 | * unless it's the only way to give this client a slot. |
NeilBrown | 2030ca5 | 2019-09-20 16:36:45 +1000 | [diff] [blame] | 1650 | * The chosen fraction is either 1/8 or 1/number of threads, |
| 1651 | * whichever is smaller. This ensures there are adequate |
| 1652 | * slots to support multiple clients per thread. |
NeilBrown | 7f49fd5d | 2019-09-20 16:33:16 +1000 | [diff] [blame] | 1653 | * Give the client one slot even if that would require |
| 1654 | * over-allocation--it is better than failure. |
J. Bruce Fields | de766e5 | 2017-09-19 19:25:41 -0400 | [diff] [blame] | 1655 | */ |
NeilBrown | 2030ca5 | 2019-09-20 16:36:45 +1000 | [diff] [blame] | 1656 | scale_factor = max_t(unsigned int, 8, nn->nfsd_serv->sv_nrthreads); |
| 1657 | |
| 1658 | avail = clamp_t(unsigned long, avail, slotsize, |
| 1659 | total_avail/scale_factor); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1660 | num = min_t(int, num, avail / slotsize); |
NeilBrown | 7f49fd5d | 2019-09-20 16:33:16 +1000 | [diff] [blame] | 1661 | num = max_t(int, num, 1); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1662 | nfsd_drc_mem_used += num * slotsize; |
| 1663 | spin_unlock(&nfsd_drc_lock); |
| 1664 | |
| 1665 | return num; |
| 1666 | } |
| 1667 | |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1668 | static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1669 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1670 | int slotsize = slot_bytes(ca); |
| 1671 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1672 | spin_lock(&nfsd_drc_lock); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1673 | nfsd_drc_mem_used -= slotsize * ca->maxreqs; |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1674 | spin_unlock(&nfsd_drc_lock); |
| 1675 | } |
| 1676 | |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1677 | static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs, |
| 1678 | struct nfsd4_channel_attrs *battrs) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1679 | { |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1680 | int numslots = fattrs->maxreqs; |
| 1681 | int slotsize = slot_bytes(fattrs); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1682 | struct nfsd4_session *new; |
| 1683 | int mem, i; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1684 | |
J. Bruce Fields | c23753d | 2010-09-27 16:22:30 -0400 | [diff] [blame] | 1685 | 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] | 1686 | + sizeof(struct nfsd4_session) > PAGE_SIZE); |
| 1687 | mem = numslots * sizeof(struct nfsd4_slot *); |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1688 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1689 | new = kzalloc(sizeof(*new) + mem, GFP_KERNEL); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1690 | if (!new) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1691 | return NULL; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1692 | /* allocate each struct nfsd4_slot and data cache in one piece */ |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1693 | for (i = 0; i < numslots; i++) { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1694 | new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1695 | if (!new->se_slots[i]) |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1696 | goto out_free; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1697 | } |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1698 | |
| 1699 | memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs)); |
| 1700 | memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs)); |
| 1701 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1702 | return new; |
| 1703 | out_free: |
| 1704 | while (i--) |
| 1705 | kfree(new->se_slots[i]); |
| 1706 | kfree(new); |
| 1707 | return NULL; |
| 1708 | } |
| 1709 | |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1710 | static void free_conn(struct nfsd4_conn *c) |
| 1711 | { |
| 1712 | svc_xprt_put(c->cn_xprt); |
| 1713 | kfree(c); |
| 1714 | } |
| 1715 | |
| 1716 | static void nfsd4_conn_lost(struct svc_xpt_user *u) |
| 1717 | { |
| 1718 | struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user); |
| 1719 | struct nfs4_client *clp = c->cn_session->se_client; |
| 1720 | |
| 1721 | spin_lock(&clp->cl_lock); |
| 1722 | if (!list_empty(&c->cn_persession)) { |
| 1723 | list_del(&c->cn_persession); |
| 1724 | free_conn(c); |
| 1725 | } |
J. Bruce Fields | eea4980 | 2010-11-18 08:34:12 -0500 | [diff] [blame] | 1726 | nfsd4_probe_callback(clp); |
J. Bruce Fields | 2e4b723 | 2013-03-08 09:30:43 -0500 | [diff] [blame] | 1727 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1728 | } |
| 1729 | |
J. Bruce Fields | d29c374 | 2010-06-15 17:34:11 -0400 | [diff] [blame] | 1730 | 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] | 1731 | { |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1732 | struct nfsd4_conn *conn; |
| 1733 | |
| 1734 | conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL); |
| 1735 | if (!conn) |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1736 | return NULL; |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1737 | svc_xprt_get(rqstp->rq_xprt); |
| 1738 | conn->cn_xprt = rqstp->rq_xprt; |
J. Bruce Fields | d29c374 | 2010-06-15 17:34:11 -0400 | [diff] [blame] | 1739 | conn->cn_flags = flags; |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1740 | INIT_LIST_HEAD(&conn->cn_xpt_user.list); |
| 1741 | return conn; |
| 1742 | } |
| 1743 | |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 1744 | static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) |
| 1745 | { |
| 1746 | conn->cn_session = ses; |
| 1747 | list_add(&conn->cn_persession, &ses->se_conns); |
| 1748 | } |
| 1749 | |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1750 | static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) |
| 1751 | { |
| 1752 | struct nfs4_client *clp = ses->se_client; |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1753 | |
| 1754 | spin_lock(&clp->cl_lock); |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 1755 | __nfsd4_hash_conn(conn, ses); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1756 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1757 | } |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1758 | |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1759 | static int nfsd4_register_conn(struct nfsd4_conn *conn) |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1760 | { |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1761 | conn->cn_xpt_user.callback = nfsd4_conn_lost; |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1762 | return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user); |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1763 | } |
| 1764 | |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1765 | 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] | 1766 | { |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1767 | int ret; |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1768 | |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1769 | nfsd4_hash_conn(conn, ses); |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1770 | ret = nfsd4_register_conn(conn); |
| 1771 | if (ret) |
| 1772 | /* oops; xprt is already down: */ |
| 1773 | nfsd4_conn_lost(&conn->cn_xpt_user); |
J. Bruce Fields | 57a3714 | 2014-07-18 15:06:47 -0400 | [diff] [blame] | 1774 | /* We may have gained or lost a callback channel: */ |
| 1775 | nfsd4_probe_callback_sync(ses->se_client); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1776 | } |
| 1777 | |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1778 | 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] | 1779 | { |
| 1780 | u32 dir = NFS4_CDFC4_FORE; |
| 1781 | |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1782 | if (cses->flags & SESSION4_BACK_CHAN) |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 1783 | dir |= NFS4_CDFC4_BACK; |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1784 | return alloc_conn(rqstp, dir); |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 1785 | } |
| 1786 | |
| 1787 | /* must be called under client_lock */ |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1788 | static void nfsd4_del_conns(struct nfsd4_session *s) |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1789 | { |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1790 | struct nfs4_client *clp = s->se_client; |
| 1791 | struct nfsd4_conn *c; |
| 1792 | |
| 1793 | spin_lock(&clp->cl_lock); |
| 1794 | while (!list_empty(&s->se_conns)) { |
| 1795 | c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession); |
| 1796 | list_del_init(&c->cn_persession); |
| 1797 | spin_unlock(&clp->cl_lock); |
| 1798 | |
| 1799 | unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user); |
| 1800 | free_conn(c); |
| 1801 | |
| 1802 | spin_lock(&clp->cl_lock); |
| 1803 | } |
| 1804 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1805 | } |
| 1806 | |
J. Bruce Fields | 1377b69 | 2012-09-11 21:42:40 -0400 | [diff] [blame] | 1807 | static void __free_session(struct nfsd4_session *ses) |
| 1808 | { |
J. Bruce Fields | 1377b69 | 2012-09-11 21:42:40 -0400 | [diff] [blame] | 1809 | free_session_slots(ses); |
| 1810 | kfree(ses); |
| 1811 | } |
| 1812 | |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 1813 | static void free_session(struct nfsd4_session *ses) |
Benny Halevy | 508dc6e | 2012-02-23 17:40:52 -0800 | [diff] [blame] | 1814 | { |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1815 | nfsd4_del_conns(ses); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1816 | nfsd4_put_drc_mem(&ses->se_fchannel); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1817 | __free_session(ses); |
J. Bruce Fields | a827bcb | 2012-09-12 09:51:34 -0400 | [diff] [blame] | 1818 | } |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1819 | |
Fengguang Wu | 135ae82 | 2012-11-10 07:20:25 -0500 | [diff] [blame] | 1820 | 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] | 1821 | { |
J. Bruce Fields | a827bcb | 2012-09-12 09:51:34 -0400 | [diff] [blame] | 1822 | int idx; |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1823 | 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] | 1824 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1825 | new->se_client = clp; |
| 1826 | gen_sessionid(new); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1827 | |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1828 | INIT_LIST_HEAD(&new->se_conns); |
| 1829 | |
J. Bruce Fields | ac7c46f2 | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 1830 | new->se_cb_seq_nr = 1; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1831 | new->se_flags = cses->flags; |
J. Bruce Fields | 8b5ce5c | 2010-10-19 17:31:50 -0400 | [diff] [blame] | 1832 | new->se_cb_prog = cses->callback_prog; |
J. Bruce Fields | c6bb3ca | 2012-11-01 16:31:02 -0400 | [diff] [blame] | 1833 | new->se_cb_sec = cses->cb_sec; |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 1834 | atomic_set(&new->se_ref, 0); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1835 | idx = hash_sessionid(&new->se_sessionid); |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1836 | list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1837 | spin_lock(&clp->cl_lock); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1838 | list_add(&new->se_perclnt, &clp->cl_sessions); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1839 | spin_unlock(&clp->cl_lock); |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1840 | |
Chuck Lever | b0d2e42 | 2014-08-22 15:10:59 -0400 | [diff] [blame] | 1841 | { |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1842 | struct sockaddr *sa = svc_addr(rqstp); |
J. Bruce Fields | dcbeaa6 | 2010-06-15 17:25:45 -0400 | [diff] [blame] | 1843 | /* |
| 1844 | * This is a little silly; with sessions there's no real |
| 1845 | * use for the callback address. Use the peer address |
| 1846 | * as a reasonable default for now, but consider fixing |
| 1847 | * the rpc client not to require an address in the |
| 1848 | * future: |
| 1849 | */ |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1850 | rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa); |
| 1851 | clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa); |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1852 | } |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1853 | } |
| 1854 | |
Benny Halevy | 9089f1b | 2010-05-12 00:12:26 +0300 | [diff] [blame] | 1855 | /* caller must hold client_lock */ |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1856 | static struct nfsd4_session * |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 1857 | __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net) |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1858 | { |
| 1859 | struct nfsd4_session *elem; |
| 1860 | int idx; |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1861 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1862 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 1863 | lockdep_assert_held(&nn->client_lock); |
| 1864 | |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1865 | dump_sessionid(__func__, sessionid); |
| 1866 | idx = hash_sessionid(sessionid); |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1867 | /* Search in the appropriate list */ |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1868 | list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) { |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1869 | if (!memcmp(elem->se_sessionid.data, sessionid->data, |
| 1870 | NFS4_MAX_SESSIONID_LEN)) { |
| 1871 | return elem; |
| 1872 | } |
| 1873 | } |
| 1874 | |
| 1875 | dprintk("%s: session not found\n", __func__); |
| 1876 | return NULL; |
| 1877 | } |
| 1878 | |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 1879 | static struct nfsd4_session * |
| 1880 | find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net, |
| 1881 | __be32 *ret) |
| 1882 | { |
| 1883 | struct nfsd4_session *session; |
| 1884 | __be32 status = nfserr_badsession; |
| 1885 | |
| 1886 | session = __find_in_sessionid_hashtbl(sessionid, net); |
| 1887 | if (!session) |
| 1888 | goto out; |
| 1889 | status = nfsd4_get_session_locked(session); |
| 1890 | if (status) |
| 1891 | session = NULL; |
| 1892 | out: |
| 1893 | *ret = status; |
| 1894 | return session; |
| 1895 | } |
| 1896 | |
Benny Halevy | 9089f1b | 2010-05-12 00:12:26 +0300 | [diff] [blame] | 1897 | /* caller must hold client_lock */ |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1898 | static void |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1899 | unhash_session(struct nfsd4_session *ses) |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1900 | { |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 1901 | struct nfs4_client *clp = ses->se_client; |
| 1902 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 1903 | |
| 1904 | lockdep_assert_held(&nn->client_lock); |
| 1905 | |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1906 | list_del(&ses->se_hash); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1907 | spin_lock(&ses->se_client->cl_lock); |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1908 | list_del(&ses->se_perclnt); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1909 | spin_unlock(&ses->se_client->cl_lock); |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1910 | } |
| 1911 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1912 | /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */ |
| 1913 | static int |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 1914 | STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1915 | { |
J. Bruce Fields | bbc7f33 | 2015-01-20 11:51:26 -0500 | [diff] [blame] | 1916 | /* |
| 1917 | * We're assuming the clid was not given out from a boot |
| 1918 | * precisely 2^32 (about 136 years) before this one. That seems |
| 1919 | * a safe assumption: |
| 1920 | */ |
| 1921 | if (clid->cl_boot == (u32)nn->boot_time) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1922 | return 0; |
Arnd Bergmann | 9cc7680 | 2019-10-31 15:53:13 +0100 | [diff] [blame] | 1923 | dprintk("NFSD stale clientid (%08x/%08x) boot_time %08llx\n", |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 1924 | clid->cl_boot, clid->cl_id, nn->boot_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1925 | return 1; |
| 1926 | } |
| 1927 | |
| 1928 | /* |
| 1929 | * XXX Should we use a slab cache ? |
| 1930 | * This type of memory management is somewhat inefficient, but we use it |
| 1931 | * anyway since SETCLIENTID is not a common operation. |
| 1932 | */ |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1933 | static struct nfs4_client *alloc_client(struct xdr_netobj name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1934 | { |
| 1935 | struct nfs4_client *clp; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1936 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1937 | |
Jeff Layton | 9258a2d | 2018-03-16 09:47:22 -0400 | [diff] [blame] | 1938 | clp = kmem_cache_zalloc(client_slab, GFP_KERNEL); |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1939 | if (clp == NULL) |
| 1940 | return NULL; |
J. Bruce Fields | 6f4859b | 2019-06-19 14:30:33 -0400 | [diff] [blame] | 1941 | xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1942 | if (clp->cl_name.data == NULL) |
| 1943 | goto err_no_name; |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 1944 | clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE, |
| 1945 | sizeof(struct list_head), |
| 1946 | GFP_KERNEL); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1947 | if (!clp->cl_ownerstr_hashtbl) |
| 1948 | goto err_no_hashtbl; |
| 1949 | for (i = 0; i < OWNER_HASH_SIZE; i++) |
| 1950 | INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]); |
Trond Myklebust | 5694c93 | 2014-04-18 14:43:56 -0400 | [diff] [blame] | 1951 | INIT_LIST_HEAD(&clp->cl_sessions); |
| 1952 | idr_init(&clp->cl_stateids); |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 1953 | atomic_set(&clp->cl_rpc_users, 0); |
Trond Myklebust | 5694c93 | 2014-04-18 14:43:56 -0400 | [diff] [blame] | 1954 | clp->cl_cb_state = NFSD4_CB_UNKNOWN; |
| 1955 | INIT_LIST_HEAD(&clp->cl_idhash); |
| 1956 | INIT_LIST_HEAD(&clp->cl_openowners); |
| 1957 | INIT_LIST_HEAD(&clp->cl_delegations); |
| 1958 | INIT_LIST_HEAD(&clp->cl_lru); |
Trond Myklebust | 5694c93 | 2014-04-18 14:43:56 -0400 | [diff] [blame] | 1959 | INIT_LIST_HEAD(&clp->cl_revoked); |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 1960 | #ifdef CONFIG_NFSD_PNFS |
| 1961 | INIT_LIST_HEAD(&clp->cl_lo_states); |
| 1962 | #endif |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 1963 | INIT_LIST_HEAD(&clp->async_copies); |
| 1964 | spin_lock_init(&clp->async_lock); |
Trond Myklebust | 5694c93 | 2014-04-18 14:43:56 -0400 | [diff] [blame] | 1965 | spin_lock_init(&clp->cl_lock); |
| 1966 | rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1967 | return clp; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1968 | err_no_hashtbl: |
| 1969 | kfree(clp->cl_name.data); |
| 1970 | err_no_name: |
Jeff Layton | 9258a2d | 2018-03-16 09:47:22 -0400 | [diff] [blame] | 1971 | kmem_cache_free(client_slab, clp); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1972 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1973 | } |
| 1974 | |
J. Bruce Fields | 59f8e91 | 2019-03-20 20:03:02 -0400 | [diff] [blame] | 1975 | static void __free_client(struct kref *k) |
| 1976 | { |
J. Bruce Fields | e8a79fb | 2019-03-22 11:11:06 -0400 | [diff] [blame] | 1977 | struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref); |
| 1978 | 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] | 1979 | |
| 1980 | free_svc_cred(&clp->cl_cred); |
| 1981 | kfree(clp->cl_ownerstr_hashtbl); |
| 1982 | kfree(clp->cl_name.data); |
J. Bruce Fields | 7912344 | 2019-06-05 12:42:05 -0400 | [diff] [blame] | 1983 | kfree(clp->cl_nii_domain.data); |
| 1984 | kfree(clp->cl_nii_name.data); |
J. Bruce Fields | 59f8e91 | 2019-03-20 20:03:02 -0400 | [diff] [blame] | 1985 | idr_destroy(&clp->cl_stateids); |
J. Bruce Fields | 59f8e91 | 2019-03-20 20:03:02 -0400 | [diff] [blame] | 1986 | kmem_cache_free(client_slab, clp); |
| 1987 | } |
| 1988 | |
YueHaibing | 297e57a | 2019-07-08 15:29:33 +0800 | [diff] [blame] | 1989 | static void drop_client(struct nfs4_client *clp) |
J. Bruce Fields | 59f8e91 | 2019-03-20 20:03:02 -0400 | [diff] [blame] | 1990 | { |
J. Bruce Fields | e8a79fb | 2019-03-22 11:11:06 -0400 | [diff] [blame] | 1991 | kref_put(&clp->cl_nfsdfs.cl_ref, __free_client); |
J. Bruce Fields | 59f8e91 | 2019-03-20 20:03:02 -0400 | [diff] [blame] | 1992 | } |
| 1993 | |
Trond Myklebust | 4dd86e15 | 2014-04-18 14:43:58 -0400 | [diff] [blame] | 1994 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1995 | free_client(struct nfs4_client *clp) |
| 1996 | { |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 1997 | while (!list_empty(&clp->cl_sessions)) { |
| 1998 | struct nfsd4_session *ses; |
| 1999 | ses = list_entry(clp->cl_sessions.next, struct nfsd4_session, |
| 2000 | se_perclnt); |
| 2001 | list_del(&ses->se_perclnt); |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 2002 | WARN_ON_ONCE(atomic_read(&ses->se_ref)); |
| 2003 | free_session(ses); |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 2004 | } |
Trond Myklebust | 4cb57e3 | 2014-04-18 14:43:57 -0400 | [diff] [blame] | 2005 | rpc_destroy_wait_queue(&clp->cl_cb_waitq); |
J. Bruce Fields | 89c905b | 2019-06-19 12:43:11 -0400 | [diff] [blame] | 2006 | if (clp->cl_nfsd_dentry) { |
J. Bruce Fields | e8a79fb | 2019-03-22 11:11:06 -0400 | [diff] [blame] | 2007 | nfsd_client_rmdir(clp->cl_nfsd_dentry); |
J. Bruce Fields | 89c905b | 2019-06-19 12:43:11 -0400 | [diff] [blame] | 2008 | clp->cl_nfsd_dentry = NULL; |
| 2009 | wake_up_all(&expiry_wq); |
| 2010 | } |
J. Bruce Fields | 59f8e91 | 2019-03-20 20:03:02 -0400 | [diff] [blame] | 2011 | drop_client(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2012 | } |
| 2013 | |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 2014 | /* must be called under the client_lock */ |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 2015 | static void |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 2016 | unhash_client_locked(struct nfs4_client *clp) |
| 2017 | { |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 2018 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 2019 | struct nfsd4_session *ses; |
| 2020 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2021 | lockdep_assert_held(&nn->client_lock); |
| 2022 | |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 2023 | /* Mark the client as expired! */ |
| 2024 | clp->cl_time = 0; |
| 2025 | /* Make it invisible */ |
| 2026 | if (!list_empty(&clp->cl_idhash)) { |
| 2027 | list_del_init(&clp->cl_idhash); |
| 2028 | if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags)) |
| 2029 | rb_erase(&clp->cl_namenode, &nn->conf_name_tree); |
| 2030 | else |
| 2031 | rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); |
| 2032 | } |
| 2033 | list_del_init(&clp->cl_lru); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 2034 | spin_lock(&clp->cl_lock); |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 2035 | list_for_each_entry(ses, &clp->cl_sessions, se_perclnt) |
| 2036 | list_del_init(&ses->se_hash); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 2037 | spin_unlock(&clp->cl_lock); |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 2038 | } |
| 2039 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2040 | static void |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 2041 | unhash_client(struct nfs4_client *clp) |
| 2042 | { |
| 2043 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 2044 | |
| 2045 | spin_lock(&nn->client_lock); |
| 2046 | unhash_client_locked(clp); |
| 2047 | spin_unlock(&nn->client_lock); |
| 2048 | } |
| 2049 | |
Jeff Layton | 97403d9 | 2014-07-30 08:27:12 -0400 | [diff] [blame] | 2050 | static __be32 mark_client_expired_locked(struct nfs4_client *clp) |
| 2051 | { |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 2052 | if (atomic_read(&clp->cl_rpc_users)) |
Jeff Layton | 97403d9 | 2014-07-30 08:27:12 -0400 | [diff] [blame] | 2053 | return nfserr_jukebox; |
| 2054 | unhash_client_locked(clp); |
| 2055 | return nfs_ok; |
| 2056 | } |
| 2057 | |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 2058 | static void |
| 2059 | __destroy_client(struct nfs4_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2060 | { |
Jeff Layton | 68ef3bc | 2018-03-16 11:32:02 -0400 | [diff] [blame] | 2061 | int i; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2062 | struct nfs4_openowner *oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2063 | struct nfs4_delegation *dp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2064 | struct list_head reaplist; |
| 2065 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | INIT_LIST_HEAD(&reaplist); |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 2067 | spin_lock(&state_lock); |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 2068 | while (!list_empty(&clp->cl_delegations)) { |
| 2069 | dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt); |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 2070 | WARN_ON(!unhash_delegation_locked(dp)); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 2071 | list_add(&dp->dl_recall_lru, &reaplist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2072 | } |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 2073 | spin_unlock(&state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2074 | while (!list_empty(&reaplist)) { |
| 2075 | dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 2076 | list_del_init(&dp->dl_recall_lru); |
J. Bruce Fields | 0af6e69 | 2018-02-21 15:11:03 -0500 | [diff] [blame] | 2077 | destroy_unhashed_deleg(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2078 | } |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 2079 | while (!list_empty(&clp->cl_revoked)) { |
Andrew Elble | c876486 | 2015-02-25 17:46:27 -0500 | [diff] [blame] | 2080 | 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] | 2081 | list_del_init(&dp->dl_recall_lru); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 2082 | nfs4_put_stid(&dp->dl_stid); |
Benny Halevy | 956c4fe | 2013-10-29 11:39:12 +0200 | [diff] [blame] | 2083 | } |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 2084 | while (!list_empty(&clp->cl_openowners)) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2085 | oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient); |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 2086 | nfs4_get_stateowner(&oo->oo_owner); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2087 | release_openowner(oo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | } |
Jeff Layton | 68ef3bc | 2018-03-16 11:32:02 -0400 | [diff] [blame] | 2089 | for (i = 0; i < OWNER_HASH_SIZE; i++) { |
| 2090 | struct nfs4_stateowner *so, *tmp; |
| 2091 | |
| 2092 | list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i], |
| 2093 | so_strhash) { |
| 2094 | /* Should be no openowners at this point */ |
| 2095 | WARN_ON_ONCE(so->so_is_open_owner); |
| 2096 | remove_blocked_locks(lockowner(so)); |
| 2097 | } |
| 2098 | } |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 2099 | nfsd4_return_all_client_layouts(clp); |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 2100 | nfsd4_shutdown_copy(clp); |
J. Bruce Fields | 6ff8da0 | 2010-06-04 20:04:45 -0400 | [diff] [blame] | 2101 | nfsd4_shutdown_callback(clp); |
J. Bruce Fields | 2bf2387 | 2010-03-08 12:37:27 -0500 | [diff] [blame] | 2102 | if (clp->cl_cb_conn.cb_xprt) |
| 2103 | svc_xprt_put(clp->cl_cb_conn.cb_xprt); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2104 | free_client(clp); |
J. Bruce Fields | 89c905b | 2019-06-19 12:43:11 -0400 | [diff] [blame] | 2105 | wake_up_all(&expiry_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2106 | } |
| 2107 | |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 2108 | static void |
| 2109 | destroy_client(struct nfs4_client *clp) |
| 2110 | { |
| 2111 | unhash_client(clp); |
| 2112 | __destroy_client(clp); |
| 2113 | } |
| 2114 | |
Scott Mayhew | 362063a | 2019-03-26 18:06:28 -0400 | [diff] [blame] | 2115 | static void inc_reclaim_complete(struct nfs4_client *clp) |
| 2116 | { |
| 2117 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 2118 | |
| 2119 | if (!nn->track_reclaim_completes) |
| 2120 | return; |
| 2121 | if (!nfsd4_find_reclaim_client(clp->cl_name, nn)) |
| 2122 | return; |
| 2123 | if (atomic_inc_return(&nn->nr_reclaim_complete) == |
| 2124 | nn->reclaim_str_hashtbl_size) { |
| 2125 | printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n", |
| 2126 | clp->net->ns.inum); |
| 2127 | nfsd4_end_grace(nn); |
| 2128 | } |
| 2129 | } |
| 2130 | |
J. Bruce Fields | 0d22f68 | 2012-09-26 11:36:16 -0400 | [diff] [blame] | 2131 | static void expire_client(struct nfs4_client *clp) |
| 2132 | { |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 2133 | unhash_client(clp); |
J. Bruce Fields | 0d22f68 | 2012-09-26 11:36:16 -0400 | [diff] [blame] | 2134 | nfsd4_client_record_remove(clp); |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 2135 | __destroy_client(clp); |
J. Bruce Fields | 0d22f68 | 2012-09-26 11:36:16 -0400 | [diff] [blame] | 2136 | } |
| 2137 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 2138 | static void copy_verf(struct nfs4_client *target, nfs4_verifier *source) |
| 2139 | { |
| 2140 | memcpy(target->cl_verifier.data, source->data, |
| 2141 | sizeof(target->cl_verifier.data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2142 | } |
| 2143 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 2144 | static void copy_clid(struct nfs4_client *target, struct nfs4_client *source) |
| 2145 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2146 | target->cl_clientid.cl_boot = source->cl_clientid.cl_boot; |
| 2147 | target->cl_clientid.cl_id = source->cl_clientid.cl_id; |
| 2148 | } |
| 2149 | |
J. Bruce Fields | 5004385 | 2015-11-20 15:58:37 -0500 | [diff] [blame] | 2150 | static int copy_cred(struct svc_cred *target, struct svc_cred *source) |
| 2151 | { |
NeilBrown | 2f10fdc | 2017-03-23 16:57:36 +0800 | [diff] [blame] | 2152 | target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL); |
| 2153 | target->cr_raw_principal = kstrdup(source->cr_raw_principal, |
| 2154 | GFP_KERNEL); |
Chuck Lever | 9abdda5 | 2018-08-16 12:05:59 -0400 | [diff] [blame] | 2155 | target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL); |
| 2156 | if ((source->cr_principal && !target->cr_principal) || |
| 2157 | (source->cr_raw_principal && !target->cr_raw_principal) || |
| 2158 | (source->cr_targ_princ && !target->cr_targ_princ)) |
NeilBrown | 2f10fdc | 2017-03-23 16:57:36 +0800 | [diff] [blame] | 2159 | return -ENOMEM; |
J. Bruce Fields | 5004385 | 2015-11-20 15:58:37 -0500 | [diff] [blame] | 2160 | |
J. Bruce Fields | d5497fc | 2012-05-14 22:06:49 -0400 | [diff] [blame] | 2161 | target->cr_flavor = source->cr_flavor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2162 | target->cr_uid = source->cr_uid; |
| 2163 | target->cr_gid = source->cr_gid; |
| 2164 | target->cr_group_info = source->cr_group_info; |
| 2165 | get_group_info(target->cr_group_info); |
J. Bruce Fields | 0dc1531 | 2013-05-14 16:07:13 -0400 | [diff] [blame] | 2166 | target->cr_gss_mech = source->cr_gss_mech; |
| 2167 | if (source->cr_gss_mech) |
| 2168 | gss_mech_get(source->cr_gss_mech); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 2169 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2170 | } |
| 2171 | |
Rasmus Villemoes | ef17af2 | 2014-12-05 16:40:07 +0100 | [diff] [blame] | 2172 | static int |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2173 | compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2) |
| 2174 | { |
Rasmus Villemoes | ef17af2 | 2014-12-05 16:40:07 +0100 | [diff] [blame] | 2175 | if (o1->len < o2->len) |
| 2176 | return -1; |
| 2177 | if (o1->len > o2->len) |
| 2178 | return 1; |
| 2179 | return memcmp(o1->data, o2->data, o1->len); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2180 | } |
| 2181 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2182 | static int |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 2183 | same_verf(nfs4_verifier *v1, nfs4_verifier *v2) |
| 2184 | { |
| 2185 | return 0 == memcmp(v1->data, v2->data, sizeof(v1->data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2186 | } |
| 2187 | |
| 2188 | static int |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 2189 | same_clid(clientid_t *cl1, clientid_t *cl2) |
| 2190 | { |
| 2191 | 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] | 2192 | } |
| 2193 | |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 2194 | static bool groups_equal(struct group_info *g1, struct group_info *g2) |
| 2195 | { |
| 2196 | int i; |
| 2197 | |
| 2198 | if (g1->ngroups != g2->ngroups) |
| 2199 | return false; |
| 2200 | for (i=0; i<g1->ngroups; i++) |
Alexey Dobriyan | 81243ea | 2016-10-07 17:03:12 -0700 | [diff] [blame] | 2201 | if (!gid_eq(g1->gid[i], g2->gid[i])) |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 2202 | return false; |
| 2203 | return true; |
| 2204 | } |
| 2205 | |
J. Bruce Fields | 68eb350 | 2012-08-21 12:48:30 -0400 | [diff] [blame] | 2206 | /* |
| 2207 | * RFC 3530 language requires clid_inuse be returned when the |
| 2208 | * "principal" associated with a requests differs from that previously |
| 2209 | * used. We use uid, gid's, and gss principal string as our best |
| 2210 | * approximation. We also don't want to allow non-gss use of a client |
| 2211 | * established using gss: in theory cr_principal should catch that |
| 2212 | * change, but in practice cr_principal can be null even in the gss case |
| 2213 | * since gssd doesn't always pass down a principal string. |
| 2214 | */ |
| 2215 | static bool is_gss_cred(struct svc_cred *cr) |
| 2216 | { |
| 2217 | /* Is cr_flavor one of the gss "pseudoflavors"?: */ |
| 2218 | return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR); |
| 2219 | } |
| 2220 | |
| 2221 | |
Vivek Trivedi | 5559b50 | 2012-07-24 21:18:20 +0530 | [diff] [blame] | 2222 | static bool |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 2223 | same_creds(struct svc_cred *cr1, struct svc_cred *cr2) |
| 2224 | { |
J. Bruce Fields | 68eb350 | 2012-08-21 12:48:30 -0400 | [diff] [blame] | 2225 | if ((is_gss_cred(cr1) != is_gss_cred(cr2)) |
Eric W. Biederman | 6fab877 | 2013-02-02 06:53:11 -0800 | [diff] [blame] | 2226 | || (!uid_eq(cr1->cr_uid, cr2->cr_uid)) |
| 2227 | || (!gid_eq(cr1->cr_gid, cr2->cr_gid)) |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 2228 | || !groups_equal(cr1->cr_group_info, cr2->cr_group_info)) |
| 2229 | return false; |
Chuck Lever | 9abdda5 | 2018-08-16 12:05:59 -0400 | [diff] [blame] | 2230 | /* XXX: check that cr_targ_princ fields match ? */ |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 2231 | if (cr1->cr_principal == cr2->cr_principal) |
| 2232 | return true; |
| 2233 | if (!cr1->cr_principal || !cr2->cr_principal) |
| 2234 | return false; |
Vivek Trivedi | 5559b50 | 2012-07-24 21:18:20 +0530 | [diff] [blame] | 2235 | return 0 == strcmp(cr1->cr_principal, cr2->cr_principal); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2236 | } |
| 2237 | |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2238 | static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp) |
| 2239 | { |
| 2240 | struct svc_cred *cr = &rqstp->rq_cred; |
| 2241 | u32 service; |
| 2242 | |
J. Bruce Fields | c472059 | 2013-08-07 11:41:49 -0400 | [diff] [blame] | 2243 | if (!cr->cr_gss_mech) |
| 2244 | return false; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2245 | service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor); |
| 2246 | return service == RPC_GSS_SVC_INTEGRITY || |
| 2247 | service == RPC_GSS_SVC_PRIVACY; |
| 2248 | } |
| 2249 | |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 2250 | 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] | 2251 | { |
| 2252 | struct svc_cred *cr = &rqstp->rq_cred; |
| 2253 | |
| 2254 | if (!cl->cl_mach_cred) |
| 2255 | return true; |
| 2256 | if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech) |
| 2257 | return false; |
| 2258 | if (!svc_rqst_integrity_protected(rqstp)) |
| 2259 | return false; |
J. Bruce Fields | 414ca01 | 2015-11-20 10:48:02 -0500 | [diff] [blame] | 2260 | if (cl->cl_cred.cr_raw_principal) |
| 2261 | return 0 == strcmp(cl->cl_cred.cr_raw_principal, |
| 2262 | cr->cr_raw_principal); |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2263 | if (!cr->cr_principal) |
| 2264 | return false; |
| 2265 | return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal); |
| 2266 | } |
| 2267 | |
Jeff Layton | 294ac32 | 2014-07-30 08:27:15 -0400 | [diff] [blame] | 2268 | 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] | 2269 | { |
Chuck Lever | ab4684d | 2012-03-02 17:13:50 -0500 | [diff] [blame] | 2270 | __be32 verf[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2271 | |
Jeff Layton | f419992 | 2014-06-17 07:44:11 -0400 | [diff] [blame] | 2272 | /* |
| 2273 | * This is opaque to client, so no need to byte-swap. Use |
| 2274 | * __force to keep sparse happy |
| 2275 | */ |
Arnd Bergmann | 9104ae4 | 2019-11-04 16:45:30 +0100 | [diff] [blame] | 2276 | verf[0] = (__force __be32)(u32)ktime_get_real_seconds(); |
Kinglong Mee | 19311aa8 | 2015-07-18 07:33:31 +0800 | [diff] [blame] | 2277 | verf[1] = (__force __be32)nn->clverifier_counter++; |
Chuck Lever | ab4684d | 2012-03-02 17:13:50 -0500 | [diff] [blame] | 2278 | memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2279 | } |
| 2280 | |
Jeff Layton | 294ac32 | 2014-07-30 08:27:15 -0400 | [diff] [blame] | 2281 | static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn) |
| 2282 | { |
Arnd Bergmann | 9cc7680 | 2019-10-31 15:53:13 +0100 | [diff] [blame] | 2283 | clp->cl_clientid.cl_boot = (u32)nn->boot_time; |
Jeff Layton | 294ac32 | 2014-07-30 08:27:15 -0400 | [diff] [blame] | 2284 | clp->cl_clientid.cl_id = nn->clientid_counter++; |
| 2285 | gen_confirm(clp, nn); |
| 2286 | } |
| 2287 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 2288 | static struct nfs4_stid * |
| 2289 | find_stateid_locked(struct nfs4_client *cl, stateid_t *t) |
J. Bruce Fields | 4581d14 | 2011-09-06 14:56:09 -0400 | [diff] [blame] | 2290 | { |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 2291 | struct nfs4_stid *ret; |
| 2292 | |
| 2293 | ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id); |
| 2294 | if (!ret || !ret->sc_type) |
| 2295 | return NULL; |
| 2296 | return ret; |
J. Bruce Fields | 4d71ab8 | 2011-09-06 16:48:57 -0400 | [diff] [blame] | 2297 | } |
| 2298 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 2299 | static struct nfs4_stid * |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 2300 | 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] | 2301 | { |
| 2302 | struct nfs4_stid *s; |
| 2303 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 2304 | spin_lock(&cl->cl_lock); |
| 2305 | s = find_stateid_locked(cl, t); |
Trond Myklebust | 2d3f966 | 2014-07-29 21:34:25 -0400 | [diff] [blame] | 2306 | if (s != NULL) { |
| 2307 | if (typemask & s->sc_type) |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 2308 | refcount_inc(&s->sc_count); |
Trond Myklebust | 2d3f966 | 2014-07-29 21:34:25 -0400 | [diff] [blame] | 2309 | else |
| 2310 | s = NULL; |
| 2311 | } |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 2312 | spin_unlock(&cl->cl_lock); |
| 2313 | return s; |
J. Bruce Fields | 4581d14 | 2011-09-06 14:56:09 -0400 | [diff] [blame] | 2314 | } |
| 2315 | |
J. Bruce Fields | a204f25 | 2019-06-19 12:54:45 -0400 | [diff] [blame] | 2316 | static struct nfs4_client *get_nfsdfs_clp(struct inode *inode) |
| 2317 | { |
| 2318 | struct nfsdfs_client *nc; |
| 2319 | nc = get_nfsdfs_client(inode); |
| 2320 | if (!nc) |
| 2321 | return NULL; |
| 2322 | return container_of(nc, struct nfs4_client, cl_nfsdfs); |
| 2323 | } |
| 2324 | |
J. Bruce Fields | 169319f | 2019-06-19 12:39:46 -0400 | [diff] [blame] | 2325 | static void seq_quote_mem(struct seq_file *m, char *data, int len) |
| 2326 | { |
| 2327 | seq_printf(m, "\""); |
| 2328 | seq_escape_mem_ascii(m, data, len); |
| 2329 | seq_printf(m, "\""); |
| 2330 | } |
| 2331 | |
J. Bruce Fields | 97ad403 | 2019-04-09 15:56:57 -0400 | [diff] [blame] | 2332 | static int client_info_show(struct seq_file *m, void *v) |
| 2333 | { |
| 2334 | struct inode *inode = m->private; |
J. Bruce Fields | 97ad403 | 2019-04-09 15:56:57 -0400 | [diff] [blame] | 2335 | struct nfs4_client *clp; |
| 2336 | u64 clid; |
| 2337 | |
J. Bruce Fields | a204f25 | 2019-06-19 12:54:45 -0400 | [diff] [blame] | 2338 | clp = get_nfsdfs_clp(inode); |
| 2339 | if (!clp) |
J. Bruce Fields | 97ad403 | 2019-04-09 15:56:57 -0400 | [diff] [blame] | 2340 | return -ENXIO; |
J. Bruce Fields | 97ad403 | 2019-04-09 15:56:57 -0400 | [diff] [blame] | 2341 | memcpy(&clid, &clp->cl_clientid, sizeof(clid)); |
| 2342 | seq_printf(m, "clientid: 0x%llx\n", clid); |
J. Bruce Fields | 169319f | 2019-06-19 12:39:46 -0400 | [diff] [blame] | 2343 | seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr); |
| 2344 | seq_printf(m, "name: "); |
| 2345 | seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len); |
| 2346 | seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion); |
J. Bruce Fields | 7912344 | 2019-06-05 12:42:05 -0400 | [diff] [blame] | 2347 | if (clp->cl_nii_domain.data) { |
| 2348 | seq_printf(m, "Implementation domain: "); |
| 2349 | seq_quote_mem(m, clp->cl_nii_domain.data, |
| 2350 | clp->cl_nii_domain.len); |
| 2351 | seq_printf(m, "\nImplementation name: "); |
| 2352 | 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] | 2353 | seq_printf(m, "\nImplementation time: [%lld, %ld]\n", |
J. Bruce Fields | 7912344 | 2019-06-05 12:42:05 -0400 | [diff] [blame] | 2354 | clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec); |
| 2355 | } |
J. Bruce Fields | 97ad403 | 2019-04-09 15:56:57 -0400 | [diff] [blame] | 2356 | drop_client(clp); |
| 2357 | |
| 2358 | return 0; |
| 2359 | } |
| 2360 | |
| 2361 | static int client_info_open(struct inode *inode, struct file *file) |
| 2362 | { |
| 2363 | return single_open(file, client_info_show, inode); |
| 2364 | } |
| 2365 | |
| 2366 | static const struct file_operations client_info_fops = { |
| 2367 | .open = client_info_open, |
| 2368 | .read = seq_read, |
| 2369 | .llseek = seq_lseek, |
| 2370 | .release = single_release, |
| 2371 | }; |
| 2372 | |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2373 | static void *states_start(struct seq_file *s, loff_t *pos) |
| 2374 | __acquires(&clp->cl_lock) |
| 2375 | { |
| 2376 | struct nfs4_client *clp = s->private; |
| 2377 | unsigned long id = *pos; |
| 2378 | void *ret; |
| 2379 | |
| 2380 | spin_lock(&clp->cl_lock); |
| 2381 | ret = idr_get_next_ul(&clp->cl_stateids, &id); |
| 2382 | *pos = id; |
| 2383 | return ret; |
| 2384 | } |
| 2385 | |
| 2386 | static void *states_next(struct seq_file *s, void *v, loff_t *pos) |
| 2387 | { |
| 2388 | struct nfs4_client *clp = s->private; |
| 2389 | unsigned long id = *pos; |
| 2390 | void *ret; |
| 2391 | |
| 2392 | id = *pos; |
| 2393 | id++; |
| 2394 | ret = idr_get_next_ul(&clp->cl_stateids, &id); |
| 2395 | *pos = id; |
| 2396 | return ret; |
| 2397 | } |
| 2398 | |
| 2399 | static void states_stop(struct seq_file *s, void *v) |
| 2400 | __releases(&clp->cl_lock) |
| 2401 | { |
| 2402 | struct nfs4_client *clp = s->private; |
| 2403 | |
| 2404 | spin_unlock(&clp->cl_lock); |
| 2405 | } |
| 2406 | |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 2407 | 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] | 2408 | { |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 2409 | struct inode *inode = f->nf_inode; |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2410 | |
| 2411 | seq_printf(s, "superblock: \"%02x:%02x:%ld\"", |
| 2412 | MAJOR(inode->i_sb->s_dev), |
| 2413 | MINOR(inode->i_sb->s_dev), |
| 2414 | inode->i_ino); |
| 2415 | } |
| 2416 | |
| 2417 | static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo) |
| 2418 | { |
| 2419 | seq_printf(s, "owner: "); |
| 2420 | seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len); |
| 2421 | } |
| 2422 | |
| 2423 | static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st) |
| 2424 | { |
| 2425 | struct nfs4_ol_stateid *ols; |
| 2426 | struct nfs4_file *nf; |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 2427 | struct nfsd_file *file; |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2428 | struct nfs4_stateowner *oo; |
| 2429 | unsigned int access, deny; |
| 2430 | |
| 2431 | if (st->sc_type != NFS4_OPEN_STID && st->sc_type != NFS4_LOCK_STID) |
| 2432 | return 0; /* XXX: or SEQ_SKIP? */ |
| 2433 | ols = openlockstateid(st); |
| 2434 | oo = ols->st_stateowner; |
| 2435 | nf = st->sc_file; |
| 2436 | file = find_any_file(nf); |
| 2437 | |
| 2438 | seq_printf(s, "- 0x%16phN: { type: open, ", &st->sc_stateid); |
| 2439 | |
| 2440 | access = bmap_to_share_mode(ols->st_access_bmap); |
| 2441 | deny = bmap_to_share_mode(ols->st_deny_bmap); |
| 2442 | |
J. Bruce Fields | c4b77ed | 2019-10-03 12:33:08 -0400 | [diff] [blame] | 2443 | seq_printf(s, "access: %s%s, ", |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2444 | access & NFS4_SHARE_ACCESS_READ ? "r" : "-", |
| 2445 | access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-"); |
J. Bruce Fields | c4b77ed | 2019-10-03 12:33:08 -0400 | [diff] [blame] | 2446 | seq_printf(s, "deny: %s%s, ", |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2447 | deny & NFS4_SHARE_ACCESS_READ ? "r" : "-", |
| 2448 | deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-"); |
| 2449 | |
| 2450 | nfs4_show_superblock(s, file); |
| 2451 | seq_printf(s, ", "); |
| 2452 | nfs4_show_owner(s, oo); |
| 2453 | seq_printf(s, " }\n"); |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 2454 | nfsd_file_put(file); |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2455 | |
| 2456 | return 0; |
| 2457 | } |
| 2458 | |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2459 | static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st) |
| 2460 | { |
| 2461 | struct nfs4_ol_stateid *ols; |
| 2462 | struct nfs4_file *nf; |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 2463 | struct nfsd_file *file; |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2464 | struct nfs4_stateowner *oo; |
| 2465 | |
| 2466 | ols = openlockstateid(st); |
| 2467 | oo = ols->st_stateowner; |
| 2468 | nf = st->sc_file; |
| 2469 | file = find_any_file(nf); |
| 2470 | |
| 2471 | seq_printf(s, "- 0x%16phN: { type: lock, ", &st->sc_stateid); |
| 2472 | |
| 2473 | /* |
| 2474 | * Note: a lock stateid isn't really the same thing as a lock, |
| 2475 | * it's the locking state held by one owner on a file, and there |
| 2476 | * may be multiple (or no) lock ranges associated with it. |
| 2477 | * (Same for the matter is true of open stateids.) |
| 2478 | */ |
| 2479 | |
| 2480 | nfs4_show_superblock(s, file); |
| 2481 | /* XXX: open stateid? */ |
| 2482 | seq_printf(s, ", "); |
| 2483 | nfs4_show_owner(s, oo); |
| 2484 | seq_printf(s, " }\n"); |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 2485 | nfsd_file_put(file); |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2486 | |
| 2487 | return 0; |
| 2488 | } |
| 2489 | |
| 2490 | static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st) |
| 2491 | { |
| 2492 | struct nfs4_delegation *ds; |
| 2493 | struct nfs4_file *nf; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 2494 | struct nfsd_file *file; |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2495 | |
| 2496 | ds = delegstateid(st); |
| 2497 | nf = st->sc_file; |
| 2498 | file = nf->fi_deleg_file; |
| 2499 | |
| 2500 | seq_printf(s, "- 0x%16phN: { type: deleg, ", &st->sc_stateid); |
| 2501 | |
| 2502 | /* Kinda dead code as long as we only support read delegs: */ |
| 2503 | seq_printf(s, "access: %s, ", |
| 2504 | ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w"); |
| 2505 | |
| 2506 | /* XXX: lease time, whether it's being recalled. */ |
| 2507 | |
| 2508 | nfs4_show_superblock(s, file); |
| 2509 | seq_printf(s, " }\n"); |
| 2510 | |
| 2511 | return 0; |
| 2512 | } |
| 2513 | |
J. Bruce Fields | 0c4b62b | 2019-05-14 15:52:57 -0400 | [diff] [blame] | 2514 | static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st) |
| 2515 | { |
| 2516 | struct nfs4_layout_stateid *ls; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 2517 | struct nfsd_file *file; |
J. Bruce Fields | 0c4b62b | 2019-05-14 15:52:57 -0400 | [diff] [blame] | 2518 | |
| 2519 | ls = container_of(st, struct nfs4_layout_stateid, ls_stid); |
| 2520 | file = ls->ls_file; |
| 2521 | |
| 2522 | seq_printf(s, "- 0x%16phN: { type: layout, ", &st->sc_stateid); |
| 2523 | |
| 2524 | /* XXX: What else would be useful? */ |
| 2525 | |
| 2526 | nfs4_show_superblock(s, file); |
| 2527 | seq_printf(s, " }\n"); |
| 2528 | |
| 2529 | return 0; |
| 2530 | } |
| 2531 | |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2532 | static int states_show(struct seq_file *s, void *v) |
| 2533 | { |
| 2534 | struct nfs4_stid *st = v; |
| 2535 | |
| 2536 | switch (st->sc_type) { |
| 2537 | case NFS4_OPEN_STID: |
| 2538 | return nfs4_show_open(s, st); |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2539 | case NFS4_LOCK_STID: |
| 2540 | return nfs4_show_lock(s, st); |
| 2541 | case NFS4_DELEG_STID: |
| 2542 | return nfs4_show_deleg(s, st); |
J. Bruce Fields | 0c4b62b | 2019-05-14 15:52:57 -0400 | [diff] [blame] | 2543 | case NFS4_LAYOUT_STID: |
| 2544 | return nfs4_show_layout(s, st); |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2545 | default: |
| 2546 | return 0; /* XXX: or SEQ_SKIP? */ |
| 2547 | } |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2548 | /* XXX: copy stateids? */ |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2549 | } |
| 2550 | |
| 2551 | static struct seq_operations states_seq_ops = { |
| 2552 | .start = states_start, |
| 2553 | .next = states_next, |
| 2554 | .stop = states_stop, |
| 2555 | .show = states_show |
| 2556 | }; |
| 2557 | |
| 2558 | static int client_states_open(struct inode *inode, struct file *file) |
| 2559 | { |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2560 | struct seq_file *s; |
| 2561 | struct nfs4_client *clp; |
| 2562 | int ret; |
| 2563 | |
J. Bruce Fields | a204f25 | 2019-06-19 12:54:45 -0400 | [diff] [blame] | 2564 | clp = get_nfsdfs_clp(inode); |
| 2565 | if (!clp) |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2566 | return -ENXIO; |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2567 | |
| 2568 | ret = seq_open(file, &states_seq_ops); |
| 2569 | if (ret) |
| 2570 | return ret; |
| 2571 | s = file->private_data; |
| 2572 | s->private = clp; |
| 2573 | return 0; |
| 2574 | } |
| 2575 | |
| 2576 | static int client_opens_release(struct inode *inode, struct file *file) |
| 2577 | { |
| 2578 | struct seq_file *m = file->private_data; |
| 2579 | struct nfs4_client *clp = m->private; |
| 2580 | |
| 2581 | /* XXX: alternatively, we could get/drop in seq start/stop */ |
| 2582 | drop_client(clp); |
| 2583 | return 0; |
| 2584 | } |
| 2585 | |
| 2586 | static const struct file_operations client_states_fops = { |
| 2587 | .open = client_states_open, |
| 2588 | .read = seq_read, |
| 2589 | .llseek = seq_lseek, |
| 2590 | .release = client_opens_release, |
| 2591 | }; |
| 2592 | |
J. Bruce Fields | 89c905b | 2019-06-19 12:43:11 -0400 | [diff] [blame] | 2593 | /* |
| 2594 | * Normally we refuse to destroy clients that are in use, but here the |
| 2595 | * administrator is telling us to just do it. We also want to wait |
| 2596 | * so the caller has a guarantee that the client's locks are gone by |
| 2597 | * the time the write returns: |
| 2598 | */ |
YueHaibing | 297e57a | 2019-07-08 15:29:33 +0800 | [diff] [blame] | 2599 | static void force_expire_client(struct nfs4_client *clp) |
J. Bruce Fields | 89c905b | 2019-06-19 12:43:11 -0400 | [diff] [blame] | 2600 | { |
| 2601 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 2602 | bool already_expired; |
| 2603 | |
| 2604 | spin_lock(&clp->cl_lock); |
| 2605 | clp->cl_time = 0; |
| 2606 | spin_unlock(&clp->cl_lock); |
| 2607 | |
| 2608 | wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0); |
| 2609 | spin_lock(&nn->client_lock); |
| 2610 | already_expired = list_empty(&clp->cl_lru); |
| 2611 | if (!already_expired) |
| 2612 | unhash_client_locked(clp); |
| 2613 | spin_unlock(&nn->client_lock); |
| 2614 | |
| 2615 | if (!already_expired) |
| 2616 | expire_client(clp); |
| 2617 | else |
| 2618 | wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL); |
| 2619 | } |
| 2620 | |
| 2621 | static ssize_t client_ctl_write(struct file *file, const char __user *buf, |
| 2622 | size_t size, loff_t *pos) |
| 2623 | { |
| 2624 | char *data; |
| 2625 | struct nfs4_client *clp; |
| 2626 | |
| 2627 | data = simple_transaction_get(file, buf, size); |
| 2628 | if (IS_ERR(data)) |
| 2629 | return PTR_ERR(data); |
| 2630 | if (size != 7 || 0 != memcmp(data, "expire\n", 7)) |
| 2631 | return -EINVAL; |
| 2632 | clp = get_nfsdfs_clp(file_inode(file)); |
| 2633 | if (!clp) |
| 2634 | return -ENXIO; |
| 2635 | force_expire_client(clp); |
| 2636 | drop_client(clp); |
| 2637 | return 7; |
| 2638 | } |
| 2639 | |
| 2640 | static const struct file_operations client_ctl_fops = { |
| 2641 | .write = client_ctl_write, |
| 2642 | .release = simple_transaction_release, |
| 2643 | }; |
| 2644 | |
J. Bruce Fields | 97ad403 | 2019-04-09 15:56:57 -0400 | [diff] [blame] | 2645 | static const struct tree_descr client_files[] = { |
| 2646 | [0] = {"info", &client_info_fops, S_IRUSR}, |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2647 | [1] = {"states", &client_states_fops, S_IRUSR}, |
Petr Vorel | 6cbfad5 | 2020-03-13 13:39:57 +0100 | [diff] [blame^] | 2648 | [2] = {"ctl", &client_ctl_fops, S_IWUSR}, |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2649 | [3] = {""}, |
J. Bruce Fields | 97ad403 | 2019-04-09 15:56:57 -0400 | [diff] [blame] | 2650 | }; |
| 2651 | |
Jeff Layton | 2216d44 | 2012-11-12 15:00:57 -0500 | [diff] [blame] | 2652 | static struct nfs4_client *create_client(struct xdr_netobj name, |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2653 | struct svc_rqst *rqstp, nfs4_verifier *verf) |
| 2654 | { |
| 2655 | struct nfs4_client *clp; |
| 2656 | struct sockaddr *sa = svc_addr(rqstp); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 2657 | int ret; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2658 | struct net *net = SVC_NET(rqstp); |
J. Bruce Fields | e8a79fb | 2019-03-22 11:11:06 -0400 | [diff] [blame] | 2659 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2660 | |
| 2661 | clp = alloc_client(name); |
| 2662 | if (clp == NULL) |
| 2663 | return NULL; |
| 2664 | |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 2665 | ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred); |
| 2666 | if (ret) { |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 2667 | free_client(clp); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 2668 | return NULL; |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2669 | } |
J. Bruce Fields | e8a79fb | 2019-03-22 11:11:06 -0400 | [diff] [blame] | 2670 | gen_clid(clp, nn); |
| 2671 | kref_init(&clp->cl_nfsdfs.cl_ref); |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 2672 | 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] | 2673 | clp->cl_time = ktime_get_boottime_seconds(); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2674 | clear_bit(0, &clp->cl_cb_slot_busy); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2675 | copy_verf(clp, verf); |
J. Bruce Fields | 3bade24 | 2019-05-14 21:38:11 -0400 | [diff] [blame] | 2676 | memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage)); |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 2677 | clp->cl_cb_session = NULL; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2678 | clp->net = net; |
J. Bruce Fields | e8a79fb | 2019-03-22 11:11:06 -0400 | [diff] [blame] | 2679 | clp->cl_nfsd_dentry = nfsd_client_mkdir(nn, &clp->cl_nfsdfs, |
J. Bruce Fields | 97ad403 | 2019-04-09 15:56:57 -0400 | [diff] [blame] | 2680 | clp->cl_clientid.cl_id - nn->clientid_base, |
| 2681 | client_files); |
J. Bruce Fields | e8a79fb | 2019-03-22 11:11:06 -0400 | [diff] [blame] | 2682 | if (!clp->cl_nfsd_dentry) { |
| 2683 | free_client(clp); |
| 2684 | return NULL; |
| 2685 | } |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2686 | return clp; |
| 2687 | } |
| 2688 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 2689 | static void |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2690 | add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root) |
| 2691 | { |
| 2692 | struct rb_node **new = &(root->rb_node), *parent = NULL; |
| 2693 | struct nfs4_client *clp; |
| 2694 | |
| 2695 | while (*new) { |
| 2696 | clp = rb_entry(*new, struct nfs4_client, cl_namenode); |
| 2697 | parent = *new; |
| 2698 | |
| 2699 | if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0) |
| 2700 | new = &((*new)->rb_left); |
| 2701 | else |
| 2702 | new = &((*new)->rb_right); |
| 2703 | } |
| 2704 | |
| 2705 | rb_link_node(&new_clp->cl_namenode, parent, new); |
| 2706 | rb_insert_color(&new_clp->cl_namenode, root); |
| 2707 | } |
| 2708 | |
| 2709 | static struct nfs4_client * |
| 2710 | find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root) |
| 2711 | { |
Rasmus Villemoes | ef17af2 | 2014-12-05 16:40:07 +0100 | [diff] [blame] | 2712 | int cmp; |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2713 | struct rb_node *node = root->rb_node; |
| 2714 | struct nfs4_client *clp; |
| 2715 | |
| 2716 | while (node) { |
| 2717 | clp = rb_entry(node, struct nfs4_client, cl_namenode); |
| 2718 | cmp = compare_blob(&clp->cl_name, name); |
| 2719 | if (cmp > 0) |
| 2720 | node = node->rb_left; |
| 2721 | else if (cmp < 0) |
| 2722 | node = node->rb_right; |
| 2723 | else |
| 2724 | return clp; |
| 2725 | } |
| 2726 | return NULL; |
| 2727 | } |
| 2728 | |
| 2729 | static void |
| 2730 | add_to_unconfirmed(struct nfs4_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2731 | { |
| 2732 | unsigned int idhashval; |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 2733 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2734 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2735 | lockdep_assert_held(&nn->client_lock); |
| 2736 | |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2737 | clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2738 | add_clp_to_name_tree(clp, &nn->unconf_name_tree); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2739 | idhashval = clientid_hashval(clp->cl_clientid.cl_id); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 2740 | list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]); |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2741 | renew_client_locked(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2742 | } |
| 2743 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 2744 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2745 | move_to_confirmed(struct nfs4_client *clp) |
| 2746 | { |
| 2747 | unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2748 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2749 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2750 | lockdep_assert_held(&nn->client_lock); |
| 2751 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2752 | dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2753 | list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]); |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2754 | rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2755 | add_clp_to_name_tree(clp, &nn->conf_name_tree); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2756 | set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2757 | renew_client_locked(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2758 | } |
| 2759 | |
| 2760 | static struct nfs4_client * |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2761 | 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] | 2762 | { |
| 2763 | struct nfs4_client *clp; |
| 2764 | unsigned int idhashval = clientid_hashval(clid->cl_id); |
| 2765 | |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2766 | list_for_each_entry(clp, &tbl[idhashval], cl_idhash) { |
J. Bruce Fields | a50d2ad | 2011-10-12 16:24:27 -0400 | [diff] [blame] | 2767 | if (same_clid(&clp->cl_clientid, clid)) { |
J. Bruce Fields | d15c077 | 2012-09-13 16:19:31 -0400 | [diff] [blame] | 2768 | if ((bool)clp->cl_minorversion != sessions) |
| 2769 | return NULL; |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2770 | renew_client_locked(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2771 | return clp; |
J. Bruce Fields | a50d2ad | 2011-10-12 16:24:27 -0400 | [diff] [blame] | 2772 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2773 | } |
| 2774 | return NULL; |
| 2775 | } |
| 2776 | |
| 2777 | static struct nfs4_client * |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2778 | find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) |
| 2779 | { |
| 2780 | struct list_head *tbl = nn->conf_id_hashtbl; |
| 2781 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2782 | lockdep_assert_held(&nn->client_lock); |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2783 | return find_client_in_id_table(tbl, clid, sessions); |
| 2784 | } |
| 2785 | |
| 2786 | static struct nfs4_client * |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 2787 | find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2788 | { |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2789 | struct list_head *tbl = nn->unconf_id_hashtbl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2790 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2791 | lockdep_assert_held(&nn->client_lock); |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2792 | return find_client_in_id_table(tbl, clid, sessions); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2793 | } |
| 2794 | |
J. Bruce Fields | 6e5f15c | 2010-11-24 17:17:34 -0500 | [diff] [blame] | 2795 | static bool clp_used_exchangeid(struct nfs4_client *clp) |
Andy Adamson | a1bcecd | 2009-04-03 08:28:05 +0300 | [diff] [blame] | 2796 | { |
J. Bruce Fields | 6e5f15c | 2010-11-24 17:17:34 -0500 | [diff] [blame] | 2797 | return clp->cl_exchange_flags != 0; |
J. Bruce Fields | e203d50 | 2010-11-24 17:30:54 -0500 | [diff] [blame] | 2798 | } |
Andy Adamson | a1bcecd | 2009-04-03 08:28:05 +0300 | [diff] [blame] | 2799 | |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2800 | static struct nfs4_client * |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2801 | find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2802 | { |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2803 | lockdep_assert_held(&nn->client_lock); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2804 | return find_clp_in_name_tree(name, &nn->conf_name_tree); |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2805 | } |
| 2806 | |
| 2807 | static struct nfs4_client * |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2808 | find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2809 | { |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2810 | lockdep_assert_held(&nn->client_lock); |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2811 | return find_clp_in_name_tree(name, &nn->unconf_name_tree); |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2812 | } |
| 2813 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 2814 | static void |
Takuma Umeya | 6f3d772 | 2010-12-15 14:09:01 +0900 | [diff] [blame] | 2815 | 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] | 2816 | { |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2817 | struct nfs4_cb_conn *conn = &clp->cl_cb_conn; |
Takuma Umeya | 6f3d772 | 2010-12-15 14:09:01 +0900 | [diff] [blame] | 2818 | struct sockaddr *sa = svc_addr(rqstp); |
| 2819 | u32 scopeid = rpc_get_scope_id(sa); |
Jeff Layton | 7077ecb | 2009-08-14 12:57:58 -0400 | [diff] [blame] | 2820 | unsigned short expected_family; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2821 | |
Jeff Layton | 7077ecb | 2009-08-14 12:57:58 -0400 | [diff] [blame] | 2822 | /* Currently, we only support tcp and tcp6 for the callback channel */ |
| 2823 | if (se->se_callback_netid_len == 3 && |
| 2824 | !memcmp(se->se_callback_netid_val, "tcp", 3)) |
| 2825 | expected_family = AF_INET; |
| 2826 | else if (se->se_callback_netid_len == 4 && |
| 2827 | !memcmp(se->se_callback_netid_val, "tcp6", 4)) |
| 2828 | expected_family = AF_INET6; |
| 2829 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2830 | goto out_err; |
| 2831 | |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2832 | conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val, |
Jeff Layton | aa9a4ec | 2009-08-14 12:57:57 -0400 | [diff] [blame] | 2833 | se->se_callback_addr_len, |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2834 | (struct sockaddr *)&conn->cb_addr, |
| 2835 | sizeof(conn->cb_addr)); |
Jeff Layton | aa9a4ec | 2009-08-14 12:57:57 -0400 | [diff] [blame] | 2836 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2837 | if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2838 | goto out_err; |
Jeff Layton | aa9a4ec | 2009-08-14 12:57:57 -0400 | [diff] [blame] | 2839 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2840 | if (conn->cb_addr.ss_family == AF_INET6) |
| 2841 | ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid; |
Jeff Layton | fbf4665 | 2009-08-14 12:57:59 -0400 | [diff] [blame] | 2842 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2843 | conn->cb_prog = se->se_callback_prog; |
| 2844 | conn->cb_ident = se->se_callback_ident; |
Mi Jinlong | 849a1cf | 2011-08-30 17:18:41 +0800 | [diff] [blame] | 2845 | memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2846 | return; |
| 2847 | out_err: |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2848 | conn->cb_addr.ss_family = AF_UNSPEC; |
| 2849 | conn->cb_addrlen = 0; |
Rasmus Villemoes | 4ab495b | 2017-02-24 01:15:55 +0100 | [diff] [blame] | 2850 | dprintk("NFSD: this client (clientid %08x/%08x) " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2851 | "will not receive delegations\n", |
| 2852 | clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id); |
| 2853 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2854 | return; |
| 2855 | } |
| 2856 | |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2857 | /* |
J. Bruce Fields | 067e1ac | 2014-03-21 17:26:44 -0400 | [diff] [blame] | 2858 | * Cache a reply. nfsd4_check_resp_size() has bounded the cache size. |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2859 | */ |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 2860 | static void |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2861 | nfsd4_store_cache_entry(struct nfsd4_compoundres *resp) |
| 2862 | { |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 2863 | struct xdr_buf *buf = resp->xdr.buf; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2864 | struct nfsd4_slot *slot = resp->cstate.slot; |
| 2865 | unsigned int base; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2866 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2867 | dprintk("--> %s slot %p\n", __func__, slot); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2868 | |
J. Bruce Fields | 085def3 | 2017-10-18 16:17:18 -0400 | [diff] [blame] | 2869 | slot->sl_flags |= NFSD4_SLOT_INITIALIZED; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2870 | slot->sl_opcnt = resp->opcnt; |
| 2871 | slot->sl_status = resp->cstate.status; |
J. Bruce Fields | 53da6a5 | 2017-10-17 20:38:49 -0400 | [diff] [blame] | 2872 | free_svc_cred(&slot->sl_cred); |
| 2873 | copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred); |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 2874 | |
J. Bruce Fields | 085def3 | 2017-10-18 16:17:18 -0400 | [diff] [blame] | 2875 | if (!nfsd4_cache_this(resp)) { |
| 2876 | slot->sl_flags &= ~NFSD4_SLOT_CACHED; |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 2877 | return; |
| 2878 | } |
J. Bruce Fields | 085def3 | 2017-10-18 16:17:18 -0400 | [diff] [blame] | 2879 | slot->sl_flags |= NFSD4_SLOT_CACHED; |
| 2880 | |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 2881 | base = resp->cstate.data_offset; |
| 2882 | slot->sl_datalen = buf->len - base; |
| 2883 | 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] | 2884 | WARN(1, "%s: sessions DRC could not cache compound\n", |
| 2885 | __func__); |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2886 | return; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2887 | } |
| 2888 | |
| 2889 | /* |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2890 | * Encode the replay sequence operation from the slot values. |
| 2891 | * If cachethis is FALSE encode the uncached rep error on the next |
| 2892 | * operation which sets resp->p and increments resp->opcnt for |
| 2893 | * nfs4svc_encode_compoundres. |
| 2894 | * |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2895 | */ |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2896 | static __be32 |
| 2897 | nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args, |
| 2898 | struct nfsd4_compoundres *resp) |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2899 | { |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2900 | struct nfsd4_op *op; |
| 2901 | struct nfsd4_slot *slot = resp->cstate.slot; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2902 | |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2903 | /* Encode the replayed sequence operation */ |
| 2904 | op = &args->ops[resp->opcnt - 1]; |
| 2905 | nfsd4_encode_operation(resp, op); |
| 2906 | |
J. Bruce Fields | 085def3 | 2017-10-18 16:17:18 -0400 | [diff] [blame] | 2907 | if (slot->sl_flags & NFSD4_SLOT_CACHED) |
| 2908 | return op->status; |
| 2909 | if (args->opcnt == 1) { |
| 2910 | /* |
| 2911 | * The original operation wasn't a solo sequence--we |
| 2912 | * always cache those--so this retry must not match the |
| 2913 | * original: |
| 2914 | */ |
| 2915 | op->status = nfserr_seq_false_retry; |
| 2916 | } else { |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2917 | op = &args->ops[resp->opcnt++]; |
| 2918 | op->status = nfserr_retry_uncached_rep; |
| 2919 | nfsd4_encode_operation(resp, op); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2920 | } |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2921 | return op->status; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2922 | } |
| 2923 | |
| 2924 | /* |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2925 | * The sequence operation is not cached because we can use the slot and |
| 2926 | * session values. |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2927 | */ |
J. Bruce Fields | 3ca2eb9 | 2014-03-21 12:04:21 -0400 | [diff] [blame] | 2928 | static __be32 |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 2929 | nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp, |
| 2930 | struct nfsd4_sequence *seq) |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2931 | { |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2932 | struct nfsd4_slot *slot = resp->cstate.slot; |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 2933 | struct xdr_stream *xdr = &resp->xdr; |
| 2934 | __be32 *p; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2935 | __be32 status; |
| 2936 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2937 | dprintk("--> %s slot %p\n", __func__, slot); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2938 | |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2939 | status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp); |
J. Bruce Fields | 0da7b19 | 2014-03-21 11:43:34 -0400 | [diff] [blame] | 2940 | if (status) |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2941 | return status; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2942 | |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 2943 | p = xdr_reserve_space(xdr, slot->sl_datalen); |
| 2944 | if (!p) { |
| 2945 | WARN_ON_ONCE(1); |
| 2946 | return nfserr_serverfault; |
| 2947 | } |
| 2948 | xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen); |
| 2949 | xdr_commit_encode(xdr); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2950 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2951 | resp->opcnt = slot->sl_opcnt; |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 2952 | return slot->sl_status; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2953 | } |
| 2954 | |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2955 | /* |
| 2956 | * Set the exchange_id flags returned by the server. |
| 2957 | */ |
| 2958 | static void |
| 2959 | nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid) |
| 2960 | { |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 2961 | #ifdef CONFIG_NFSD_PNFS |
| 2962 | new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS; |
| 2963 | #else |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2964 | new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS; |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 2965 | #endif |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2966 | |
| 2967 | /* Referrals are supported, Migration is not. */ |
| 2968 | new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER; |
| 2969 | |
| 2970 | /* set the wire flags to return to client. */ |
| 2971 | clid->flags = new->cl_exchange_flags; |
| 2972 | } |
| 2973 | |
J. Bruce Fields | 4eaea13 | 2015-10-15 16:11:01 -0400 | [diff] [blame] | 2974 | static bool client_has_openowners(struct nfs4_client *clp) |
| 2975 | { |
| 2976 | struct nfs4_openowner *oo; |
| 2977 | |
| 2978 | list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) { |
| 2979 | if (!list_empty(&oo->oo_owner.so_stateids)) |
| 2980 | return true; |
| 2981 | } |
| 2982 | return false; |
| 2983 | } |
| 2984 | |
J. Bruce Fields | 631fc9e | 2012-05-14 15:57:23 -0400 | [diff] [blame] | 2985 | static bool client_has_state(struct nfs4_client *clp) |
| 2986 | { |
J. Bruce Fields | 4eaea13 | 2015-10-15 16:11:01 -0400 | [diff] [blame] | 2987 | return client_has_openowners(clp) |
Kinglong Mee | 47e970b | 2015-07-21 13:09:17 +0800 | [diff] [blame] | 2988 | #ifdef CONFIG_NFSD_PNFS |
| 2989 | || !list_empty(&clp->cl_lo_states) |
| 2990 | #endif |
J. Bruce Fields | 6eccece | 2012-05-29 16:37:44 -0400 | [diff] [blame] | 2991 | || !list_empty(&clp->cl_delegations) |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 2992 | || !list_empty(&clp->cl_sessions) |
| 2993 | || !list_empty(&clp->async_copies); |
J. Bruce Fields | 631fc9e | 2012-05-14 15:57:23 -0400 | [diff] [blame] | 2994 | } |
| 2995 | |
J. Bruce Fields | 7912344 | 2019-06-05 12:42:05 -0400 | [diff] [blame] | 2996 | static __be32 copy_impl_id(struct nfs4_client *clp, |
| 2997 | struct nfsd4_exchange_id *exid) |
| 2998 | { |
| 2999 | if (!exid->nii_domain.data) |
| 3000 | return 0; |
| 3001 | xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL); |
| 3002 | if (!clp->cl_nii_domain.data) |
| 3003 | return nfserr_jukebox; |
| 3004 | xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL); |
| 3005 | if (!clp->cl_nii_name.data) |
| 3006 | return nfserr_jukebox; |
Arnd Bergmann | e29f470 | 2019-10-31 14:52:43 +0100 | [diff] [blame] | 3007 | clp->cl_nii_time = exid->nii_time; |
J. Bruce Fields | 7912344 | 2019-06-05 12:42:05 -0400 | [diff] [blame] | 3008 | return 0; |
| 3009 | } |
| 3010 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3011 | __be32 |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3012 | nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 3013 | union nfsd4_op_u *u) |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3014 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3015 | struct nfsd4_exchange_id *exid = &u->exchange_id; |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3016 | struct nfs4_client *conf, *new; |
| 3017 | struct nfs4_client *unconf = NULL; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3018 | __be32 status; |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 3019 | char addr_str[INET6_ADDRSTRLEN]; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3020 | nfs4_verifier verf = exid->verifier; |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 3021 | struct sockaddr *sa = svc_addr(rqstp); |
J. Bruce Fields | 83e08fd | 2012-05-14 09:08:10 -0400 | [diff] [blame] | 3022 | bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 3023 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3024 | |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 3025 | rpc_ntop(sa, addr_str, sizeof(addr_str)); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3026 | dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p " |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 3027 | "ip_addr=%s flags %x, spa_how %d\n", |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3028 | __func__, rqstp, exid, exid->clname.len, exid->clname.data, |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 3029 | addr_str, exid->flags, exid->spa_how); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3030 | |
J. Bruce Fields | a084daf | 2011-10-10 15:07:40 -0400 | [diff] [blame] | 3031 | if (exid->flags & ~EXCHGID4_FLAG_MASK_A) |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3032 | return nfserr_inval; |
| 3033 | |
J. Bruce Fields | 50c7b94 | 2015-11-23 15:39:12 -0700 | [diff] [blame] | 3034 | new = create_client(exid->clname, rqstp, &verf); |
| 3035 | if (new == NULL) |
| 3036 | return nfserr_jukebox; |
J. Bruce Fields | 7912344 | 2019-06-05 12:42:05 -0400 | [diff] [blame] | 3037 | status = copy_impl_id(new, exid); |
| 3038 | if (status) |
| 3039 | goto out_nolock; |
J. Bruce Fields | 50c7b94 | 2015-11-23 15:39:12 -0700 | [diff] [blame] | 3040 | |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3041 | switch (exid->spa_how) { |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3042 | case SP4_MACH_CRED: |
Andrew Elble | ed94164 | 2016-06-15 12:52:09 -0400 | [diff] [blame] | 3043 | exid->spo_must_enforce[0] = 0; |
| 3044 | exid->spo_must_enforce[1] = ( |
| 3045 | 1 << (OP_BIND_CONN_TO_SESSION - 32) | |
| 3046 | 1 << (OP_EXCHANGE_ID - 32) | |
| 3047 | 1 << (OP_CREATE_SESSION - 32) | |
| 3048 | 1 << (OP_DESTROY_SESSION - 32) | |
| 3049 | 1 << (OP_DESTROY_CLIENTID - 32)); |
| 3050 | |
| 3051 | exid->spo_must_allow[0] &= (1 << (OP_CLOSE) | |
| 3052 | 1 << (OP_OPEN_DOWNGRADE) | |
| 3053 | 1 << (OP_LOCKU) | |
| 3054 | 1 << (OP_DELEGRETURN)); |
| 3055 | |
| 3056 | exid->spo_must_allow[1] &= ( |
| 3057 | 1 << (OP_TEST_STATEID - 32) | |
| 3058 | 1 << (OP_FREE_STATEID - 32)); |
J. Bruce Fields | 50c7b94 | 2015-11-23 15:39:12 -0700 | [diff] [blame] | 3059 | if (!svc_rqst_integrity_protected(rqstp)) { |
| 3060 | status = nfserr_inval; |
| 3061 | goto out_nolock; |
| 3062 | } |
J. Bruce Fields | 920dd9b | 2015-11-20 16:42:40 -0500 | [diff] [blame] | 3063 | /* |
| 3064 | * Sometimes userspace doesn't give us a principal. |
| 3065 | * Which is a bug, really. Anyway, we can't enforce |
| 3066 | * MACH_CRED in that case, better to give up now: |
| 3067 | */ |
J. Bruce Fields | 414ca01 | 2015-11-20 10:48:02 -0500 | [diff] [blame] | 3068 | if (!new->cl_cred.cr_principal && |
| 3069 | !new->cl_cred.cr_raw_principal) { |
J. Bruce Fields | 920dd9b | 2015-11-20 16:42:40 -0500 | [diff] [blame] | 3070 | status = nfserr_serverfault; |
| 3071 | goto out_nolock; |
| 3072 | } |
J. Bruce Fields | 50c7b94 | 2015-11-23 15:39:12 -0700 | [diff] [blame] | 3073 | new->cl_mach_cred = true; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3074 | case SP4_NONE: |
| 3075 | break; |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 3076 | default: /* checked by xdr code */ |
| 3077 | WARN_ON_ONCE(1); |
Gustavo A. R. Silva | 0a4c926 | 2019-01-23 02:48:28 -0600 | [diff] [blame] | 3078 | /* fall through */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3079 | case SP4_SSV: |
Kinglong Mee | 8edf4b0 | 2016-02-26 22:36:42 +0800 | [diff] [blame] | 3080 | status = nfserr_encr_alg_unsupp; |
| 3081 | goto out_nolock; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3082 | } |
| 3083 | |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 3084 | /* Cases below refer to rfc 5661 section 18.35.4: */ |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3085 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 3086 | conf = find_confirmed_client_by_name(&exid->clname, nn); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3087 | if (conf) { |
J. Bruce Fields | 83e08fd | 2012-05-14 09:08:10 -0400 | [diff] [blame] | 3088 | bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred); |
| 3089 | bool verfs_match = same_verf(&verf, &conf->cl_verifier); |
| 3090 | |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3091 | if (update) { |
| 3092 | if (!clp_used_exchangeid(conf)) { /* buggy client */ |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 3093 | status = nfserr_inval; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3094 | goto out; |
| 3095 | } |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 3096 | if (!nfsd4_mach_creds_match(conf, rqstp)) { |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3097 | status = nfserr_wrong_cred; |
| 3098 | goto out; |
| 3099 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3100 | if (!creds_match) { /* case 9 */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3101 | status = nfserr_perm; |
| 3102 | goto out; |
| 3103 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3104 | if (!verfs_match) { /* case 8 */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3105 | status = nfserr_not_same; |
| 3106 | goto out; |
| 3107 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3108 | /* case 6 */ |
| 3109 | exid->flags |= EXCHGID4_FLAG_CONFIRMED_R; |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3110 | goto out_copy; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3111 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3112 | if (!creds_match) { /* case 3 */ |
J. Bruce Fields | 631fc9e | 2012-05-14 15:57:23 -0400 | [diff] [blame] | 3113 | if (client_has_state(conf)) { |
| 3114 | status = nfserr_clid_inuse; |
| 3115 | goto out; |
| 3116 | } |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3117 | goto out_new; |
| 3118 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3119 | if (verfs_match) { /* case 2 */ |
J. Bruce Fields | 0f1ba0e | 2012-05-25 21:24:40 -0400 | [diff] [blame] | 3120 | conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R; |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3121 | goto out_copy; |
| 3122 | } |
| 3123 | /* case 5, client reboot */ |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3124 | conf = NULL; |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3125 | goto out_new; |
Mike Sager | 6ddbbbf | 2009-06-16 04:20:47 +0300 | [diff] [blame] | 3126 | } |
| 3127 | |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 3128 | if (update) { /* case 7 */ |
Mike Sager | 6ddbbbf | 2009-06-16 04:20:47 +0300 | [diff] [blame] | 3129 | status = nfserr_noent; |
| 3130 | goto out; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3131 | } |
| 3132 | |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 3133 | unconf = find_unconfirmed_client_by_name(&exid->clname, nn); |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 3134 | if (unconf) /* case 4, possible retry or client restart */ |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3135 | unhash_client_locked(unconf); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3136 | |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 3137 | /* case 1 (normal case) */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3138 | out_new: |
Jeff Layton | fd699b8 | 2014-07-30 08:27:14 -0400 | [diff] [blame] | 3139 | if (conf) { |
| 3140 | status = mark_client_expired_locked(conf); |
| 3141 | if (status) |
| 3142 | goto out; |
| 3143 | } |
J. Bruce Fields | 4f540e2 | 2013-05-07 11:57:52 -0400 | [diff] [blame] | 3144 | new->cl_minorversion = cstate->minorversion; |
Andrew Elble | ed94164 | 2016-06-15 12:52:09 -0400 | [diff] [blame] | 3145 | new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0]; |
| 3146 | 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] | 3147 | |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 3148 | add_to_unconfirmed(new); |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3149 | swap(new, conf); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3150 | out_copy: |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 3151 | exid->clientid.cl_boot = conf->cl_clientid.cl_boot; |
| 3152 | exid->clientid.cl_id = conf->cl_clientid.cl_id; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3153 | |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 3154 | exid->seqid = conf->cl_cs_slot.sl_seqid + 1; |
| 3155 | nfsd4_set_ex_flags(conf, exid); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3156 | |
| 3157 | dprintk("nfsd4_exchange_id seqid %d flags %x\n", |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 3158 | conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3159 | status = nfs_ok; |
| 3160 | |
| 3161 | out: |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3162 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | 50c7b94 | 2015-11-23 15:39:12 -0700 | [diff] [blame] | 3163 | out_nolock: |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 3164 | if (new) |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3165 | expire_client(new); |
| 3166 | if (unconf) |
| 3167 | expire_client(unconf); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3168 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3169 | } |
| 3170 | |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3171 | static __be32 |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 3172 | check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3173 | { |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 3174 | dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid, |
| 3175 | slot_seqid); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3176 | |
| 3177 | /* The slot is in use, and no response has been sent. */ |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 3178 | if (slot_inuse) { |
| 3179 | if (seqid == slot_seqid) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3180 | return nfserr_jukebox; |
| 3181 | else |
| 3182 | return nfserr_seq_misordered; |
| 3183 | } |
J. Bruce Fields | f6d8248 | 2012-02-13 16:13:41 -0500 | [diff] [blame] | 3184 | /* Note unsigned 32-bit arithmetic handles wraparound: */ |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 3185 | if (likely(seqid == slot_seqid + 1)) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3186 | return nfs_ok; |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 3187 | if (seqid == slot_seqid) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3188 | return nfserr_replay_cache; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3189 | return nfserr_seq_misordered; |
| 3190 | } |
| 3191 | |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 3192 | /* |
| 3193 | * Cache the create session result into the create session single DRC |
| 3194 | * slot cache by saving the xdr structure. sl_seqid has been set. |
| 3195 | * Do this for solo or embedded create session operations. |
| 3196 | */ |
| 3197 | static void |
| 3198 | nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses, |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3199 | struct nfsd4_clid_slot *slot, __be32 nfserr) |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 3200 | { |
| 3201 | slot->sl_status = nfserr; |
| 3202 | memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses)); |
| 3203 | } |
| 3204 | |
| 3205 | static __be32 |
| 3206 | nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses, |
| 3207 | struct nfsd4_clid_slot *slot) |
| 3208 | { |
| 3209 | memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses)); |
| 3210 | return slot->sl_status; |
| 3211 | } |
| 3212 | |
Mi Jinlong | 1b74c25 | 2011-07-14 14:50:17 +0800 | [diff] [blame] | 3213 | #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\ |
| 3214 | 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \ |
| 3215 | 1 + /* MIN tag is length with zero, only length */ \ |
| 3216 | 3 + /* version, opcount, opcode */ \ |
| 3217 | XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ |
| 3218 | /* seqid, slotID, slotID, cache */ \ |
| 3219 | 4 ) * sizeof(__be32)) |
| 3220 | |
| 3221 | #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\ |
| 3222 | 2 + /* verifier: AUTH_NULL, length 0 */\ |
| 3223 | 1 + /* status */ \ |
| 3224 | 1 + /* MIN tag is length with zero, only length */ \ |
| 3225 | 3 + /* opcount, opcode, opstatus*/ \ |
| 3226 | XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ |
| 3227 | /* seqid, slotID, slotID, slotID, status */ \ |
| 3228 | 5 ) * sizeof(__be32)) |
| 3229 | |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 3230 | 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] | 3231 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 3232 | u32 maxrpc = nn->nfsd_serv->sv_max_mesg; |
| 3233 | |
J. Bruce Fields | 373cd40 | 2013-04-08 15:42:12 -0400 | [diff] [blame] | 3234 | if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ) |
| 3235 | return nfserr_toosmall; |
| 3236 | if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ) |
| 3237 | return nfserr_toosmall; |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 3238 | ca->headerpadsz = 0; |
| 3239 | ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc); |
| 3240 | ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc); |
| 3241 | ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND); |
| 3242 | ca->maxresp_cached = min_t(u32, ca->maxresp_cached, |
| 3243 | NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ); |
| 3244 | ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION); |
| 3245 | /* |
| 3246 | * Note decreasing slot size below client's request may make it |
| 3247 | * difficult for client to function correctly, whereas |
| 3248 | * decreasing the number of slots will (just?) affect |
| 3249 | * performance. When short on memory we therefore prefer to |
| 3250 | * decrease number of slots instead of their size. Clients that |
| 3251 | * request larger slots than they need will get poor results: |
NeilBrown | 7f49fd5d | 2019-09-20 16:33:16 +1000 | [diff] [blame] | 3252 | * Note that we always allow at least one slot, because our |
| 3253 | * accounting is soft and provides no guarantees either way. |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 3254 | */ |
NeilBrown | 2030ca5 | 2019-09-20 16:36:45 +1000 | [diff] [blame] | 3255 | ca->maxreqs = nfsd4_get_drc_mem(ca, nn); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 3256 | |
J. Bruce Fields | 373cd40 | 2013-04-08 15:42:12 -0400 | [diff] [blame] | 3257 | return nfs_ok; |
Mi Jinlong | 1b74c25 | 2011-07-14 14:50:17 +0800 | [diff] [blame] | 3258 | } |
| 3259 | |
Chuck Lever | 4500632 | 2016-03-01 13:06:02 -0500 | [diff] [blame] | 3260 | /* |
| 3261 | * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now. |
| 3262 | * These are based on similar macros in linux/sunrpc/msg_prot.h . |
| 3263 | */ |
| 3264 | #define RPC_MAX_HEADER_WITH_AUTH_SYS \ |
| 3265 | (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK)) |
| 3266 | |
| 3267 | #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \ |
| 3268 | (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK)) |
| 3269 | |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 3270 | #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \ |
Chuck Lever | 4500632 | 2016-03-01 13:06:02 -0500 | [diff] [blame] | 3271 | RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32)) |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 3272 | #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \ |
Chuck Lever | 4500632 | 2016-03-01 13:06:02 -0500 | [diff] [blame] | 3273 | RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \ |
| 3274 | sizeof(__be32)) |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 3275 | |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 3276 | static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca) |
| 3277 | { |
| 3278 | ca->headerpadsz = 0; |
| 3279 | |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 3280 | if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ) |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 3281 | return nfserr_toosmall; |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 3282 | if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ) |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 3283 | return nfserr_toosmall; |
| 3284 | ca->maxresp_cached = 0; |
| 3285 | if (ca->maxops < 2) |
| 3286 | return nfserr_toosmall; |
| 3287 | |
| 3288 | return nfs_ok; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3289 | } |
| 3290 | |
J. Bruce Fields | b78724b | 2013-05-15 17:34:39 -0400 | [diff] [blame] | 3291 | static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs) |
| 3292 | { |
| 3293 | switch (cbs->flavor) { |
| 3294 | case RPC_AUTH_NULL: |
| 3295 | case RPC_AUTH_UNIX: |
| 3296 | return nfs_ok; |
| 3297 | default: |
| 3298 | /* |
| 3299 | * GSS case: the spec doesn't allow us to return this |
| 3300 | * error. But it also doesn't allow us not to support |
| 3301 | * GSS. |
| 3302 | * I'd rather this fail hard than return some error the |
| 3303 | * client might think it can already handle: |
| 3304 | */ |
| 3305 | return nfserr_encr_alg_unsupp; |
| 3306 | } |
| 3307 | } |
| 3308 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3309 | __be32 |
| 3310 | nfsd4_create_session(struct svc_rqst *rqstp, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3311 | struct nfsd4_compound_state *cstate, union nfsd4_op_u *u) |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3312 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3313 | struct nfsd4_create_session *cr_ses = &u->create_session; |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 3314 | struct sockaddr *sa = svc_addr(rqstp); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3315 | struct nfs4_client *conf, *unconf; |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3316 | struct nfs4_client *old = NULL; |
J. Bruce Fields | ac7c46f2 | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 3317 | struct nfsd4_session *new; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3318 | struct nfsd4_conn *conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 3319 | struct nfsd4_clid_slot *cs_slot = NULL; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3320 | __be32 status = 0; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 3321 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3322 | |
Mi Jinlong | a62573d | 2011-03-23 17:57:07 +0800 | [diff] [blame] | 3323 | if (cr_ses->flags & ~SESSION4_FLAG_MASK_A) |
| 3324 | return nfserr_inval; |
J. Bruce Fields | b78724b | 2013-05-15 17:34:39 -0400 | [diff] [blame] | 3325 | status = nfsd4_check_cb_sec(&cr_ses->cb_sec); |
| 3326 | if (status) |
| 3327 | return status; |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 3328 | status = check_forechannel_attrs(&cr_ses->fore_channel, nn); |
J. Bruce Fields | 373cd40 | 2013-04-08 15:42:12 -0400 | [diff] [blame] | 3329 | if (status) |
| 3330 | return status; |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 3331 | status = check_backchannel_attrs(&cr_ses->back_channel); |
| 3332 | if (status) |
Kinglong Mee | f403e45 | 2013-12-23 17:31:21 +0800 | [diff] [blame] | 3333 | goto out_release_drc_mem; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3334 | status = nfserr_jukebox; |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 3335 | new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 3336 | if (!new) |
| 3337 | goto out_release_drc_mem; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3338 | conn = alloc_conn_from_crses(rqstp, cr_ses); |
| 3339 | if (!conn) |
| 3340 | goto out_free_session; |
Mi Jinlong | a62573d | 2011-03-23 17:57:07 +0800 | [diff] [blame] | 3341 | |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3342 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 3343 | unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 3344 | conf = find_confirmed_client(&cr_ses->clientid, true, nn); |
J. Bruce Fields | 7838904 | 2013-03-12 10:12:37 -0400 | [diff] [blame] | 3345 | WARN_ON_ONCE(conf && unconf); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3346 | |
| 3347 | if (conf) { |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3348 | status = nfserr_wrong_cred; |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 3349 | if (!nfsd4_mach_creds_match(conf, rqstp)) |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3350 | goto out_free_conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 3351 | cs_slot = &conf->cl_cs_slot; |
| 3352 | status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); |
Kinglong Mee | f5e22bb | 2015-07-13 17:32:34 +0800 | [diff] [blame] | 3353 | if (status) { |
| 3354 | if (status == nfserr_replay_cache) |
| 3355 | status = nfsd4_replay_create_session(cr_ses, cs_slot); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3356 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3357 | } |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3358 | } else if (unconf) { |
| 3359 | if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) || |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 3360 | !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) { |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3361 | status = nfserr_clid_inuse; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3362 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3363 | } |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3364 | status = nfserr_wrong_cred; |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 3365 | if (!nfsd4_mach_creds_match(unconf, rqstp)) |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3366 | goto out_free_conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 3367 | cs_slot = &unconf->cl_cs_slot; |
| 3368 | status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); |
Andy Adamson | 38eb76a | 2009-04-03 08:28:32 +0300 | [diff] [blame] | 3369 | if (status) { |
| 3370 | /* an unconfirmed replay returns misordered */ |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3371 | status = nfserr_seq_misordered; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3372 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3373 | } |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 3374 | old = find_confirmed_client_by_name(&unconf->cl_name, nn); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3375 | if (old) { |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3376 | status = mark_client_expired_locked(old); |
Jeff Layton | 7abea1e | 2014-07-30 08:27:13 -0400 | [diff] [blame] | 3377 | if (status) { |
| 3378 | old = NULL; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3379 | goto out_free_conn; |
Jeff Layton | 7abea1e | 2014-07-30 08:27:13 -0400 | [diff] [blame] | 3380 | } |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3381 | } |
J. Bruce Fields | 8f9d3d3 | 2012-09-12 14:41:31 -0400 | [diff] [blame] | 3382 | move_to_confirmed(unconf); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3383 | conf = unconf; |
| 3384 | } else { |
| 3385 | status = nfserr_stale_clientid; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3386 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3387 | } |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3388 | status = nfs_ok; |
Chuck Lever | 4ce85c8 | 2016-03-01 13:05:27 -0500 | [diff] [blame] | 3389 | /* Persistent sessions are not supported */ |
J. Bruce Fields | 408b79b | 2010-04-15 15:11:09 -0400 | [diff] [blame] | 3390 | cr_ses->flags &= ~SESSION4_PERSIST; |
Chuck Lever | 4ce85c8 | 2016-03-01 13:05:27 -0500 | [diff] [blame] | 3391 | /* Upshifting from TCP to RDMA is not supported */ |
J. Bruce Fields | 408b79b | 2010-04-15 15:11:09 -0400 | [diff] [blame] | 3392 | cr_ses->flags &= ~SESSION4_RDMA; |
| 3393 | |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3394 | init_session(rqstp, new, conf, cr_ses); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3395 | nfsd4_get_session_locked(new); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3396 | |
J. Bruce Fields | ac7c46f2 | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 3397 | memcpy(cr_ses->sessionid.data, new->se_sessionid.data, |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3398 | NFS4_MAX_SESSIONID_LEN); |
J. Bruce Fields | 86c3e16 | 2010-10-02 17:04:00 -0400 | [diff] [blame] | 3399 | cs_slot->sl_seqid++; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 3400 | cr_ses->seqid = cs_slot->sl_seqid; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3401 | |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3402 | /* cache solo and embedded create sessions under the client_lock */ |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 3403 | nfsd4_cache_create_session(cr_ses, cs_slot, status); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3404 | spin_unlock(&nn->client_lock); |
| 3405 | /* init connection and backchannel */ |
| 3406 | nfsd4_init_conn(rqstp, conn, new); |
| 3407 | nfsd4_put_session(new); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3408 | if (old) |
| 3409 | expire_client(old); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3410 | return status; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3411 | out_free_conn: |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3412 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3413 | free_conn(conn); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3414 | if (old) |
| 3415 | expire_client(old); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3416 | out_free_session: |
| 3417 | __free_session(new); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 3418 | out_release_drc_mem: |
| 3419 | nfsd4_put_drc_mem(&cr_ses->fore_channel); |
J. Bruce Fields | 1ca5079 | 2013-03-14 18:12:03 -0400 | [diff] [blame] | 3420 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3421 | } |
| 3422 | |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 3423 | static __be32 nfsd4_map_bcts_dir(u32 *dir) |
| 3424 | { |
| 3425 | switch (*dir) { |
| 3426 | case NFS4_CDFC4_FORE: |
| 3427 | case NFS4_CDFC4_BACK: |
| 3428 | return nfs_ok; |
| 3429 | case NFS4_CDFC4_FORE_OR_BOTH: |
| 3430 | case NFS4_CDFC4_BACK_OR_BOTH: |
| 3431 | *dir = NFS4_CDFC4_BOTH; |
| 3432 | return nfs_ok; |
zhengbin | fc5fc5d | 2019-12-19 17:29:20 +0800 | [diff] [blame] | 3433 | } |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 3434 | return nfserr_inval; |
| 3435 | } |
| 3436 | |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3437 | __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, |
| 3438 | struct nfsd4_compound_state *cstate, |
| 3439 | union nfsd4_op_u *u) |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 3440 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3441 | struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl; |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 3442 | struct nfsd4_session *session = cstate->session; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3443 | 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] | 3444 | __be32 status; |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 3445 | |
J. Bruce Fields | b78724b | 2013-05-15 17:34:39 -0400 | [diff] [blame] | 3446 | status = nfsd4_check_cb_sec(&bc->bc_cb_sec); |
| 3447 | if (status) |
| 3448 | return status; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3449 | spin_lock(&nn->client_lock); |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 3450 | session->se_cb_prog = bc->bc_cb_program; |
| 3451 | session->se_cb_sec = bc->bc_cb_sec; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3452 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 3453 | |
| 3454 | nfsd4_probe_callback(session->se_client); |
| 3455 | |
| 3456 | return nfs_ok; |
| 3457 | } |
| 3458 | |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 3459 | __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp, |
| 3460 | struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3461 | union nfsd4_op_u *u) |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 3462 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3463 | 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] | 3464 | __be32 status; |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 3465 | struct nfsd4_conn *conn; |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 3466 | struct nfsd4_session *session; |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3467 | struct net *net = SVC_NET(rqstp); |
| 3468 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 3469 | |
| 3470 | if (!nfsd4_last_compound_op(rqstp)) |
| 3471 | return nfserr_not_only_op; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3472 | spin_lock(&nn->client_lock); |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3473 | session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3474 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 3475 | if (!session) |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3476 | goto out_no_session; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3477 | status = nfserr_wrong_cred; |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 3478 | if (!nfsd4_mach_creds_match(session->se_client, rqstp)) |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3479 | goto out; |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 3480 | status = nfsd4_map_bcts_dir(&bcts->dir); |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 3481 | if (status) |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 3482 | goto out; |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 3483 | conn = alloc_conn(rqstp, bcts->dir); |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 3484 | status = nfserr_jukebox; |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 3485 | if (!conn) |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 3486 | goto out; |
| 3487 | nfsd4_init_conn(rqstp, conn, session); |
| 3488 | status = nfs_ok; |
| 3489 | out: |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3490 | nfsd4_put_session(session); |
| 3491 | out_no_session: |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 3492 | return status; |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 3493 | } |
| 3494 | |
J. Bruce Fields | 665d507 | 2018-04-11 16:53:36 -0400 | [diff] [blame] | 3495 | 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] | 3496 | { |
J. Bruce Fields | 665d507 | 2018-04-11 16:53:36 -0400 | [diff] [blame] | 3497 | if (!cstate->session) |
Fengguang Wu | 51d87bc | 2018-03-22 13:37:20 +0800 | [diff] [blame] | 3498 | return false; |
J. Bruce Fields | 665d507 | 2018-04-11 16:53:36 -0400 | [diff] [blame] | 3499 | return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid)); |
J. Bruce Fields | 5d4cec2 | 2010-05-01 12:56:06 -0400 | [diff] [blame] | 3500 | } |
| 3501 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3502 | __be32 |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3503 | nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate, |
| 3504 | union nfsd4_op_u *u) |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3505 | { |
J. Bruce Fields | ca0552f | 2018-04-11 17:01:53 -0400 | [diff] [blame] | 3506 | struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid; |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 3507 | struct nfsd4_session *ses; |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 3508 | __be32 status; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 3509 | int ref_held_by_me = 0; |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3510 | struct net *net = SVC_NET(r); |
| 3511 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 3512 | |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 3513 | status = nfserr_not_only_op; |
J. Bruce Fields | ca0552f | 2018-04-11 17:01:53 -0400 | [diff] [blame] | 3514 | if (nfsd4_compound_in_session(cstate, sessionid)) { |
J. Bruce Fields | 5771635 | 2010-04-21 12:27:19 -0400 | [diff] [blame] | 3515 | if (!nfsd4_last_compound_op(r)) |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 3516 | goto out; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 3517 | ref_held_by_me++; |
J. Bruce Fields | 5771635 | 2010-04-21 12:27:19 -0400 | [diff] [blame] | 3518 | } |
J. Bruce Fields | ca0552f | 2018-04-11 17:01:53 -0400 | [diff] [blame] | 3519 | dump_sessionid(__func__, sessionid); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3520 | spin_lock(&nn->client_lock); |
J. Bruce Fields | ca0552f | 2018-04-11 17:01:53 -0400 | [diff] [blame] | 3521 | ses = find_in_sessionid_hashtbl(sessionid, net, &status); |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 3522 | if (!ses) |
| 3523 | goto out_client_lock; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3524 | status = nfserr_wrong_cred; |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 3525 | if (!nfsd4_mach_creds_match(ses->se_client, r)) |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3526 | goto out_put_session; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 3527 | status = mark_session_dead_locked(ses, 1 + ref_held_by_me); |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 3528 | if (status) |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 3529 | goto out_put_session; |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 3530 | unhash_session(ses); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3531 | spin_unlock(&nn->client_lock); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 3532 | |
J. Bruce Fields | 84f5f7c | 2010-12-09 15:52:19 -0500 | [diff] [blame] | 3533 | nfsd4_probe_callback_sync(ses->se_client); |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 3534 | |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3535 | spin_lock(&nn->client_lock); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 3536 | status = nfs_ok; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 3537 | out_put_session: |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3538 | nfsd4_put_session_locked(ses); |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 3539 | out_client_lock: |
| 3540 | spin_unlock(&nn->client_lock); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 3541 | out: |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 3542 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3543 | } |
| 3544 | |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 3545 | 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] | 3546 | { |
| 3547 | struct nfsd4_conn *c; |
| 3548 | |
| 3549 | list_for_each_entry(c, &s->se_conns, cn_persession) { |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 3550 | if (c->cn_xprt == xpt) { |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 3551 | return c; |
| 3552 | } |
| 3553 | } |
| 3554 | return NULL; |
| 3555 | } |
| 3556 | |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3557 | 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] | 3558 | { |
| 3559 | struct nfs4_client *clp = ses->se_client; |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 3560 | struct nfsd4_conn *c; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3561 | __be32 status = nfs_ok; |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 3562 | int ret; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 3563 | |
| 3564 | spin_lock(&clp->cl_lock); |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 3565 | c = __nfsd4_find_conn(new->cn_xprt, ses); |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3566 | if (c) |
| 3567 | goto out_free; |
| 3568 | status = nfserr_conn_not_bound_to_session; |
| 3569 | if (clp->cl_mach_cred) |
| 3570 | goto out_free; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 3571 | __nfsd4_hash_conn(new, ses); |
| 3572 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 3573 | ret = nfsd4_register_conn(new); |
| 3574 | if (ret) |
| 3575 | /* oops; xprt is already down: */ |
| 3576 | nfsd4_conn_lost(&new->cn_xpt_user); |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3577 | return nfs_ok; |
| 3578 | out_free: |
| 3579 | spin_unlock(&clp->cl_lock); |
| 3580 | free_conn(new); |
| 3581 | return status; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 3582 | } |
| 3583 | |
Mi Jinlong | 868b89c | 2011-04-27 09:09:58 +0800 | [diff] [blame] | 3584 | static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session) |
| 3585 | { |
| 3586 | struct nfsd4_compoundargs *args = rqstp->rq_argp; |
| 3587 | |
| 3588 | return args->opcnt > session->se_fchannel.maxops; |
| 3589 | } |
| 3590 | |
Mi Jinlong | ae82a8d | 2011-07-14 14:56:02 +0800 | [diff] [blame] | 3591 | static bool nfsd4_request_too_big(struct svc_rqst *rqstp, |
| 3592 | struct nfsd4_session *session) |
| 3593 | { |
| 3594 | struct xdr_buf *xb = &rqstp->rq_arg; |
| 3595 | |
| 3596 | return xb->len > session->se_fchannel.maxreq_sz; |
| 3597 | } |
| 3598 | |
J. Bruce Fields | 53da6a5 | 2017-10-17 20:38:49 -0400 | [diff] [blame] | 3599 | static bool replay_matches_cache(struct svc_rqst *rqstp, |
| 3600 | struct nfsd4_sequence *seq, struct nfsd4_slot *slot) |
| 3601 | { |
| 3602 | struct nfsd4_compoundargs *argp = rqstp->rq_argp; |
| 3603 | |
| 3604 | if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) != |
| 3605 | (bool)seq->cachethis) |
| 3606 | return false; |
| 3607 | /* |
Scott Mayhew | 6e73e92 | 2019-10-09 15:11:37 -0400 | [diff] [blame] | 3608 | * If there's an error then the reply can have fewer ops than |
| 3609 | * the call. |
J. Bruce Fields | 53da6a5 | 2017-10-17 20:38:49 -0400 | [diff] [blame] | 3610 | */ |
Scott Mayhew | 6e73e92 | 2019-10-09 15:11:37 -0400 | [diff] [blame] | 3611 | if (slot->sl_opcnt < argp->opcnt && !slot->sl_status) |
| 3612 | return false; |
| 3613 | /* |
| 3614 | * But if we cached a reply with *more* ops than the call you're |
| 3615 | * sending us now, then this new call is clearly not really a |
| 3616 | * replay of the old one: |
| 3617 | */ |
| 3618 | if (slot->sl_opcnt > argp->opcnt) |
J. Bruce Fields | 53da6a5 | 2017-10-17 20:38:49 -0400 | [diff] [blame] | 3619 | return false; |
| 3620 | /* This is the only check explicitly called by spec: */ |
| 3621 | if (!same_creds(&rqstp->rq_cred, &slot->sl_cred)) |
| 3622 | return false; |
| 3623 | /* |
| 3624 | * There may be more comparisons we could actually do, but the |
| 3625 | * spec doesn't require us to catch every case where the calls |
| 3626 | * don't match (that would require caching the call as well as |
| 3627 | * the reply), so we don't bother. |
| 3628 | */ |
| 3629 | return true; |
| 3630 | } |
| 3631 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3632 | __be32 |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3633 | nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 3634 | union nfsd4_op_u *u) |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3635 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3636 | struct nfsd4_sequence *seq = &u->sequence; |
Andy Adamson | f9bb94c4 | 2009-04-03 08:28:12 +0300 | [diff] [blame] | 3637 | struct nfsd4_compoundres *resp = rqstp->rq_resp; |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 3638 | struct xdr_stream *xdr = &resp->xdr; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3639 | struct nfsd4_session *session; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3640 | struct nfs4_client *clp; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3641 | struct nfsd4_slot *slot; |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 3642 | struct nfsd4_conn *conn; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3643 | __be32 status; |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 3644 | int buflen; |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3645 | struct net *net = SVC_NET(rqstp); |
| 3646 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3647 | |
Andy Adamson | f9bb94c4 | 2009-04-03 08:28:12 +0300 | [diff] [blame] | 3648 | if (resp->opcnt != 1) |
| 3649 | return nfserr_sequence_pos; |
| 3650 | |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 3651 | /* |
| 3652 | * Will be either used or freed by nfsd4_sequence_check_conn |
| 3653 | * below. |
| 3654 | */ |
| 3655 | conn = alloc_conn(rqstp, NFS4_CDFC4_FORE); |
| 3656 | if (!conn) |
| 3657 | return nfserr_jukebox; |
| 3658 | |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3659 | spin_lock(&nn->client_lock); |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3660 | session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3661 | if (!session) |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3662 | goto out_no_session; |
| 3663 | clp = session->se_client; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3664 | |
Mi Jinlong | 868b89c | 2011-04-27 09:09:58 +0800 | [diff] [blame] | 3665 | status = nfserr_too_many_ops; |
| 3666 | if (nfsd4_session_too_many_ops(rqstp, session)) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 3667 | goto out_put_session; |
Mi Jinlong | 868b89c | 2011-04-27 09:09:58 +0800 | [diff] [blame] | 3668 | |
Mi Jinlong | ae82a8d | 2011-07-14 14:56:02 +0800 | [diff] [blame] | 3669 | status = nfserr_req_too_big; |
| 3670 | if (nfsd4_request_too_big(rqstp, session)) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 3671 | goto out_put_session; |
Mi Jinlong | ae82a8d | 2011-07-14 14:56:02 +0800 | [diff] [blame] | 3672 | |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3673 | status = nfserr_badslot; |
Alexandros Batsakis | 6c18ba9 | 2009-06-16 04:19:13 +0300 | [diff] [blame] | 3674 | if (seq->slotid >= session->se_fchannel.maxreqs) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 3675 | goto out_put_session; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3676 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 3677 | slot = session->se_slots[seq->slotid]; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3678 | dprintk("%s: slotid %d\n", __func__, seq->slotid); |
| 3679 | |
Andy Adamson | a8dfdae | 2009-08-28 08:45:02 -0400 | [diff] [blame] | 3680 | /* We do not negotiate the number of slots yet, so set the |
| 3681 | * maxslots to the session maxreqs which is used to encode |
| 3682 | * sr_highest_slotid and the sr_target_slot id to maxslots */ |
| 3683 | seq->maxslots = session->se_fchannel.maxreqs; |
| 3684 | |
J. Bruce Fields | 73e7948 | 2012-02-13 16:39:00 -0500 | [diff] [blame] | 3685 | status = check_slot_seqid(seq->seqid, slot->sl_seqid, |
| 3686 | slot->sl_flags & NFSD4_SLOT_INUSE); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3687 | if (status == nfserr_replay_cache) { |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 3688 | status = nfserr_seq_misordered; |
| 3689 | if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED)) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 3690 | goto out_put_session; |
J. Bruce Fields | 53da6a5 | 2017-10-17 20:38:49 -0400 | [diff] [blame] | 3691 | status = nfserr_seq_false_retry; |
| 3692 | if (!replay_matches_cache(rqstp, seq, slot)) |
| 3693 | goto out_put_session; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3694 | cstate->slot = slot; |
| 3695 | cstate->session = session; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3696 | cstate->clp = clp; |
Andy Adamson | da3846a | 2009-04-03 08:28:22 +0300 | [diff] [blame] | 3697 | /* Return the cached reply status and set cstate->status |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 3698 | * for nfsd4_proc_compound processing */ |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 3699 | status = nfsd4_replay_cache_entry(resp, seq); |
Andy Adamson | da3846a | 2009-04-03 08:28:22 +0300 | [diff] [blame] | 3700 | cstate->status = nfserr_replay_cache; |
Benny Halevy | aaf84eb | 2009-08-20 03:21:56 +0300 | [diff] [blame] | 3701 | goto out; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3702 | } |
| 3703 | if (status) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 3704 | goto out_put_session; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3705 | |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3706 | status = nfsd4_sequence_check_conn(conn, session); |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 3707 | conn = NULL; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3708 | if (status) |
| 3709 | goto out_put_session; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 3710 | |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 3711 | buflen = (seq->cachethis) ? |
| 3712 | session->se_fchannel.maxresp_cached : |
| 3713 | session->se_fchannel.maxresp_sz; |
| 3714 | status = (seq->cachethis) ? nfserr_rep_too_big_to_cache : |
| 3715 | nfserr_rep_too_big; |
J. Bruce Fields | a5cddc8 | 2014-05-12 18:10:58 -0400 | [diff] [blame] | 3716 | if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack)) |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 3717 | goto out_put_session; |
J. Bruce Fields | 32aaa62 | 2014-03-20 20:47:41 -0400 | [diff] [blame] | 3718 | svc_reserve(rqstp, buflen); |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 3719 | |
| 3720 | status = nfs_ok; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3721 | /* Success! bump slot seqid */ |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3722 | slot->sl_seqid = seq->seqid; |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 3723 | slot->sl_flags |= NFSD4_SLOT_INUSE; |
J. Bruce Fields | 73e7948 | 2012-02-13 16:39:00 -0500 | [diff] [blame] | 3724 | if (seq->cachethis) |
| 3725 | slot->sl_flags |= NFSD4_SLOT_CACHETHIS; |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 3726 | else |
| 3727 | slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3728 | |
| 3729 | cstate->slot = slot; |
| 3730 | cstate->session = session; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3731 | cstate->clp = clp; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3732 | |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3733 | out: |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3734 | switch (clp->cl_cb_state) { |
| 3735 | case NFSD4_CB_DOWN: |
| 3736 | seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN; |
| 3737 | break; |
| 3738 | case NFSD4_CB_FAULT: |
| 3739 | seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT; |
| 3740 | break; |
| 3741 | default: |
| 3742 | seq->status_flags = 0; |
Benny Halevy | aaf84eb | 2009-08-20 03:21:56 +0300 | [diff] [blame] | 3743 | } |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 3744 | if (!list_empty(&clp->cl_revoked)) |
| 3745 | seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3746 | out_no_session: |
Kinglong Mee | 3f42d2c | 2014-03-24 11:56:59 +0800 | [diff] [blame] | 3747 | if (conn) |
| 3748 | free_conn(conn); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3749 | spin_unlock(&nn->client_lock); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3750 | return status; |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 3751 | out_put_session: |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3752 | nfsd4_put_session_locked(session); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3753 | goto out_no_session; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3754 | } |
| 3755 | |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 3756 | void |
| 3757 | nfsd4_sequence_done(struct nfsd4_compoundres *resp) |
| 3758 | { |
| 3759 | struct nfsd4_compound_state *cs = &resp->cstate; |
| 3760 | |
| 3761 | if (nfsd4_has_session(cs)) { |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 3762 | if (cs->status != nfserr_replay_cache) { |
| 3763 | nfsd4_store_cache_entry(resp); |
| 3764 | cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE; |
| 3765 | } |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3766 | /* Drop session reference that was taken in nfsd4_sequence() */ |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 3767 | nfsd4_put_session(cs->session); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3768 | } else if (cs->clp) |
| 3769 | put_client_renew(cs->clp); |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 3770 | } |
| 3771 | |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3772 | __be32 |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3773 | nfsd4_destroy_clientid(struct svc_rqst *rqstp, |
| 3774 | struct nfsd4_compound_state *cstate, |
| 3775 | union nfsd4_op_u *u) |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3776 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3777 | struct nfsd4_destroy_clientid *dc = &u->destroy_clientid; |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3778 | struct nfs4_client *conf, *unconf; |
| 3779 | struct nfs4_client *clp = NULL; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3780 | __be32 status = 0; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 3781 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3782 | |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3783 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 3784 | unconf = find_unconfirmed_client(&dc->clientid, true, nn); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 3785 | conf = find_confirmed_client(&dc->clientid, true, nn); |
J. Bruce Fields | 7838904 | 2013-03-12 10:12:37 -0400 | [diff] [blame] | 3786 | WARN_ON_ONCE(conf && unconf); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3787 | |
| 3788 | if (conf) { |
J. Bruce Fields | c0293b0 | 2013-03-14 18:20:01 -0400 | [diff] [blame] | 3789 | if (client_has_state(conf)) { |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3790 | status = nfserr_clientid_busy; |
| 3791 | goto out; |
| 3792 | } |
Jeff Layton | fd699b8 | 2014-07-30 08:27:14 -0400 | [diff] [blame] | 3793 | status = mark_client_expired_locked(conf); |
| 3794 | if (status) |
| 3795 | goto out; |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3796 | clp = conf; |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3797 | } else if (unconf) |
| 3798 | clp = unconf; |
| 3799 | else { |
| 3800 | status = nfserr_stale_clientid; |
| 3801 | goto out; |
| 3802 | } |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 3803 | if (!nfsd4_mach_creds_match(clp, rqstp)) { |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3804 | clp = NULL; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3805 | status = nfserr_wrong_cred; |
| 3806 | goto out; |
| 3807 | } |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3808 | unhash_client_locked(clp); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3809 | out: |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3810 | spin_unlock(&nn->client_lock); |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3811 | if (clp) |
| 3812 | expire_client(clp); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3813 | return status; |
| 3814 | } |
| 3815 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3816 | __be32 |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3817 | nfsd4_reclaim_complete(struct svc_rqst *rqstp, |
| 3818 | struct nfsd4_compound_state *cstate, union nfsd4_op_u *u) |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 3819 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3820 | struct nfsd4_reclaim_complete *rc = &u->reclaim_complete; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3821 | __be32 status = 0; |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3822 | |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 3823 | if (rc->rca_one_fs) { |
| 3824 | if (!cstate->current_fh.fh_dentry) |
| 3825 | return nfserr_nofilehandle; |
| 3826 | /* |
| 3827 | * We don't take advantage of the rca_one_fs case. |
| 3828 | * That's OK, it's optional, we can safely ignore it. |
| 3829 | */ |
Christophe JAILLET | d28c442 | 2016-07-02 08:24:32 +0200 | [diff] [blame] | 3830 | return nfs_ok; |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 3831 | } |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3832 | |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3833 | status = nfserr_complete_already; |
Jeff Layton | a52d726 | 2012-03-21 09:52:02 -0400 | [diff] [blame] | 3834 | if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, |
| 3835 | &cstate->session->se_client->cl_flags)) |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3836 | goto out; |
| 3837 | |
| 3838 | status = nfserr_stale_clientid; |
| 3839 | if (is_client_expired(cstate->session->se_client)) |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 3840 | /* |
| 3841 | * The following error isn't really legal. |
| 3842 | * But we only get here if the client just explicitly |
| 3843 | * destroyed the client. Surely it no longer cares what |
| 3844 | * error it gets back on an operation for the dead |
| 3845 | * client. |
| 3846 | */ |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3847 | goto out; |
| 3848 | |
| 3849 | status = nfs_ok; |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 3850 | nfsd4_client_record_create(cstate->session->se_client); |
Scott Mayhew | 362063a | 2019-03-26 18:06:28 -0400 | [diff] [blame] | 3851 | inc_reclaim_complete(cstate->session->se_client); |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3852 | out: |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3853 | return status; |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 3854 | } |
| 3855 | |
| 3856 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 3857 | nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3858 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3859 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3860 | struct nfsd4_setclientid *setclid = &u->setclientid; |
J. Bruce Fields | a084daf | 2011-10-10 15:07:40 -0400 | [diff] [blame] | 3861 | struct xdr_netobj clname = setclid->se_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3862 | nfs4_verifier clverifier = setclid->se_verf; |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3863 | struct nfs4_client *conf, *new; |
| 3864 | struct nfs4_client *unconf = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3865 | __be32 status; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 3866 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
| 3867 | |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 3868 | new = create_client(clname, rqstp, &clverifier); |
| 3869 | if (new == NULL) |
| 3870 | return nfserr_jukebox; |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 3871 | /* Cases below refer to rfc 3530 section 14.2.33: */ |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3872 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 3873 | conf = find_confirmed_client_by_name(&clname, nn); |
J. Bruce Fields | 2b63482 | 2015-10-15 15:33:23 -0400 | [diff] [blame] | 3874 | if (conf && client_has_state(conf)) { |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 3875 | /* case 0: */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3876 | status = nfserr_clid_inuse; |
J. Bruce Fields | e203d50 | 2010-11-24 17:30:54 -0500 | [diff] [blame] | 3877 | if (clp_used_exchangeid(conf)) |
| 3878 | goto out; |
J. Bruce Fields | 026722c | 2009-03-18 15:06:26 -0400 | [diff] [blame] | 3879 | if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) { |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 3880 | char addr_str[INET6_ADDRSTRLEN]; |
| 3881 | rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str, |
| 3882 | sizeof(addr_str)); |
| 3883 | dprintk("NFSD: setclientid: string in use by client " |
| 3884 | "at %s\n", addr_str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3885 | goto out; |
| 3886 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3887 | } |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 3888 | unconf = find_unconfirmed_client_by_name(&clname, nn); |
J. Bruce Fields | 8f93071 | 2012-05-18 22:06:41 -0400 | [diff] [blame] | 3889 | if (unconf) |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3890 | unhash_client_locked(unconf); |
Kinglong Mee | 41eb167 | 2015-07-13 17:29:41 +0800 | [diff] [blame] | 3891 | if (conf && same_verf(&conf->cl_verifier, &clverifier)) { |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 3892 | /* case 1: probable callback update */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3893 | copy_clid(new, conf); |
Kinglong Mee | 41eb167 | 2015-07-13 17:29:41 +0800 | [diff] [blame] | 3894 | gen_confirm(new, nn); |
| 3895 | } else /* case 4 (new client) or cases 2, 3 (client reboot): */ |
J. Bruce Fields | e8a79fb | 2019-03-22 11:11:06 -0400 | [diff] [blame] | 3896 | ; |
J. Bruce Fields | 8323c3b | 2010-10-19 19:36:51 -0400 | [diff] [blame] | 3897 | new->cl_minorversion = 0; |
Takuma Umeya | 6f3d772 | 2010-12-15 14:09:01 +0900 | [diff] [blame] | 3898 | gen_callback(new, setclid, rqstp); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 3899 | add_to_unconfirmed(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3900 | setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot; |
| 3901 | setclid->se_clientid.cl_id = new->cl_clientid.cl_id; |
| 3902 | 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] | 3903 | new = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3904 | status = nfs_ok; |
| 3905 | out: |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3906 | spin_unlock(&nn->client_lock); |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 3907 | if (new) |
| 3908 | free_client(new); |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3909 | if (unconf) |
| 3910 | expire_client(unconf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3911 | return status; |
| 3912 | } |
| 3913 | |
| 3914 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3915 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 3916 | nfsd4_setclientid_confirm(struct svc_rqst *rqstp, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3917 | struct nfsd4_compound_state *cstate, |
| 3918 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3919 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3920 | struct nfsd4_setclientid_confirm *setclientid_confirm = |
| 3921 | &u->setclientid_confirm; |
NeilBrown | 21ab45a | 2005-06-23 22:04:14 -0700 | [diff] [blame] | 3922 | struct nfs4_client *conf, *unconf; |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3923 | struct nfs4_client *old = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3924 | nfs4_verifier confirm = setclientid_confirm->sc_confirm; |
| 3925 | clientid_t * clid = &setclientid_confirm->sc_clientid; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3926 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 3927 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3928 | |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 3929 | if (STALE_CLIENTID(clid, nn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3930 | return nfserr_stale_clientid; |
NeilBrown | 21ab45a | 2005-06-23 22:04:14 -0700 | [diff] [blame] | 3931 | |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3932 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 3933 | conf = find_confirmed_client(clid, false, nn); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 3934 | unconf = find_unconfirmed_client(clid, false, nn); |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 3935 | /* |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 3936 | * We try hard to give out unique clientid's, so if we get an |
| 3937 | * attempt to confirm the same clientid with a different cred, |
J. Bruce Fields | f984a7c | 2015-09-01 13:40:53 -0400 | [diff] [blame] | 3938 | * the client may be buggy; this should never happen. |
| 3939 | * |
| 3940 | * Nevertheless, RFC 7530 recommends INUSE for this case: |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 3941 | */ |
J. Bruce Fields | f984a7c | 2015-09-01 13:40:53 -0400 | [diff] [blame] | 3942 | status = nfserr_clid_inuse; |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 3943 | if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred)) |
| 3944 | goto out; |
| 3945 | if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred)) |
| 3946 | goto out; |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 3947 | /* cases below refer to rfc 3530 section 14.2.34: */ |
J. Bruce Fields | 90d700b | 2012-05-19 13:55:22 -0400 | [diff] [blame] | 3948 | if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) { |
J. Bruce Fields | 7d22fc1 | 2016-09-20 20:55:36 -0400 | [diff] [blame] | 3949 | if (conf && same_verf(&confirm, &conf->cl_confirm)) { |
| 3950 | /* case 2: probable retransmit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3951 | status = nfs_ok; |
J. Bruce Fields | 7d22fc1 | 2016-09-20 20:55:36 -0400 | [diff] [blame] | 3952 | } else /* case 4: client hasn't noticed we rebooted yet? */ |
J. Bruce Fields | 90d700b | 2012-05-19 13:55:22 -0400 | [diff] [blame] | 3953 | status = nfserr_stale_clientid; |
| 3954 | goto out; |
| 3955 | } |
| 3956 | status = nfs_ok; |
| 3957 | if (conf) { /* case 1: callback update */ |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3958 | old = unconf; |
| 3959 | unhash_client_locked(old); |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 3960 | nfsd4_change_callback(conf, &unconf->cl_cb_conn); |
J. Bruce Fields | 90d700b | 2012-05-19 13:55:22 -0400 | [diff] [blame] | 3961 | } else { /* case 3: normal case; new or rebooted client */ |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3962 | old = find_confirmed_client_by_name(&unconf->cl_name, nn); |
| 3963 | if (old) { |
J. Bruce Fields | 2b63482 | 2015-10-15 15:33:23 -0400 | [diff] [blame] | 3964 | status = nfserr_clid_inuse; |
| 3965 | if (client_has_state(old) |
| 3966 | && !same_creds(&unconf->cl_cred, |
| 3967 | &old->cl_cred)) |
| 3968 | goto out; |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3969 | status = mark_client_expired_locked(old); |
Jeff Layton | 7abea1e | 2014-07-30 08:27:13 -0400 | [diff] [blame] | 3970 | if (status) { |
| 3971 | old = NULL; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3972 | goto out; |
Jeff Layton | 7abea1e | 2014-07-30 08:27:13 -0400 | [diff] [blame] | 3973 | } |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3974 | } |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 3975 | move_to_confirmed(unconf); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3976 | conf = unconf; |
NeilBrown | 08e8987 | 2005-06-23 22:04:11 -0700 | [diff] [blame] | 3977 | } |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3978 | get_client_locked(conf); |
| 3979 | spin_unlock(&nn->client_lock); |
| 3980 | nfsd4_probe_callback(conf); |
| 3981 | spin_lock(&nn->client_lock); |
| 3982 | put_client_renew_locked(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3983 | out: |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3984 | spin_unlock(&nn->client_lock); |
| 3985 | if (old) |
| 3986 | expire_client(old); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3987 | return status; |
| 3988 | } |
| 3989 | |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3990 | static struct nfs4_file *nfsd4_alloc_file(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3991 | { |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3992 | return kmem_cache_alloc(file_slab, GFP_KERNEL); |
| 3993 | } |
| 3994 | |
| 3995 | /* OPEN Share state helper functions */ |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 3996 | static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval, |
| 3997 | struct nfs4_file *fp) |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3998 | { |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3999 | lockdep_assert_held(&state_lock); |
| 4000 | |
Elena Reshetova | 818a34e | 2017-10-20 12:53:30 +0300 | [diff] [blame] | 4001 | refcount_set(&fp->fi_ref, 1); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 4002 | spin_lock_init(&fp->fi_lock); |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 4003 | INIT_LIST_HEAD(&fp->fi_stateids); |
| 4004 | INIT_LIST_HEAD(&fp->fi_delegations); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 4005 | INIT_LIST_HEAD(&fp->fi_clnt_odstate); |
Trond Myklebust | e2cf80d | 2014-07-23 16:17:38 -0400 | [diff] [blame] | 4006 | fh_copy_shallow(&fp->fi_fhandle, fh); |
Jeff Layton | 0c637be | 2014-08-22 12:05:43 -0400 | [diff] [blame] | 4007 | fp->fi_deleg_file = NULL; |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 4008 | fp->fi_had_conflict = false; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4009 | fp->fi_share_deny = 0; |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 4010 | memset(fp->fi_fds, 0, sizeof(fp->fi_fds)); |
| 4011 | memset(fp->fi_access, 0, sizeof(fp->fi_access)); |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 4012 | #ifdef CONFIG_NFSD_PNFS |
| 4013 | INIT_LIST_HEAD(&fp->fi_lo_states); |
Christoph Hellwig | c5c707f | 2014-09-23 12:38:48 +0200 | [diff] [blame] | 4014 | atomic_set(&fp->fi_lo_recalls, 0); |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 4015 | #endif |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 4016 | hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4017 | } |
| 4018 | |
J. Bruce Fields | e8ff2a8 | 2007-08-01 15:30:59 -0400 | [diff] [blame] | 4019 | void |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 4020 | nfsd4_free_slabs(void) |
| 4021 | { |
Jeff Layton | 9258a2d | 2018-03-16 09:47:22 -0400 | [diff] [blame] | 4022 | kmem_cache_destroy(client_slab); |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 4023 | kmem_cache_destroy(openowner_slab); |
| 4024 | kmem_cache_destroy(lockowner_slab); |
| 4025 | kmem_cache_destroy(file_slab); |
| 4026 | kmem_cache_destroy(stateid_slab); |
| 4027 | kmem_cache_destroy(deleg_slab); |
Jeff Layton | 9258a2d | 2018-03-16 09:47:22 -0400 | [diff] [blame] | 4028 | kmem_cache_destroy(odstate_slab); |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 4029 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4030 | |
Bryan Schumaker | 7208339 | 2011-11-01 15:24:59 -0400 | [diff] [blame] | 4031 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4032 | nfsd4_init_slabs(void) |
| 4033 | { |
Jeff Layton | 9258a2d | 2018-03-16 09:47:22 -0400 | [diff] [blame] | 4034 | client_slab = kmem_cache_create("nfsd4_clients", |
| 4035 | sizeof(struct nfs4_client), 0, 0, NULL); |
| 4036 | if (client_slab == NULL) |
| 4037 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4038 | openowner_slab = kmem_cache_create("nfsd4_openowners", |
| 4039 | sizeof(struct nfs4_openowner), 0, 0, NULL); |
| 4040 | if (openowner_slab == NULL) |
Jeff Layton | 9258a2d | 2018-03-16 09:47:22 -0400 | [diff] [blame] | 4041 | goto out_free_client_slab; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4042 | lockowner_slab = kmem_cache_create("nfsd4_lockowners", |
Yanchuan Nian | 3c40794 | 2012-10-24 14:44:19 +0800 | [diff] [blame] | 4043 | sizeof(struct nfs4_lockowner), 0, 0, NULL); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4044 | if (lockowner_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 4045 | goto out_free_openowner_slab; |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 4046 | file_slab = kmem_cache_create("nfsd4_files", |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 4047 | sizeof(struct nfs4_file), 0, 0, NULL); |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 4048 | if (file_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 4049 | goto out_free_lockowner_slab; |
NeilBrown | 5ac049a | 2005-06-23 22:03:03 -0700 | [diff] [blame] | 4050 | stateid_slab = kmem_cache_create("nfsd4_stateids", |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4051 | sizeof(struct nfs4_ol_stateid), 0, 0, NULL); |
NeilBrown | 5ac049a | 2005-06-23 22:03:03 -0700 | [diff] [blame] | 4052 | if (stateid_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 4053 | goto out_free_file_slab; |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 4054 | deleg_slab = kmem_cache_create("nfsd4_delegations", |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 4055 | sizeof(struct nfs4_delegation), 0, 0, NULL); |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 4056 | if (deleg_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 4057 | goto out_free_stateid_slab; |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 4058 | odstate_slab = kmem_cache_create("nfsd4_odstate", |
| 4059 | sizeof(struct nfs4_clnt_odstate), 0, 0, NULL); |
| 4060 | if (odstate_slab == NULL) |
| 4061 | goto out_free_deleg_slab; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4062 | return 0; |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 4063 | |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 4064 | out_free_deleg_slab: |
| 4065 | kmem_cache_destroy(deleg_slab); |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 4066 | out_free_stateid_slab: |
| 4067 | kmem_cache_destroy(stateid_slab); |
| 4068 | out_free_file_slab: |
| 4069 | kmem_cache_destroy(file_slab); |
| 4070 | out_free_lockowner_slab: |
| 4071 | kmem_cache_destroy(lockowner_slab); |
| 4072 | out_free_openowner_slab: |
| 4073 | kmem_cache_destroy(openowner_slab); |
Jeff Layton | 9258a2d | 2018-03-16 09:47:22 -0400 | [diff] [blame] | 4074 | out_free_client_slab: |
| 4075 | kmem_cache_destroy(client_slab); |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 4076 | out: |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 4077 | dprintk("nfsd4: out of memory while initializing nfsv4\n"); |
| 4078 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4079 | } |
| 4080 | |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4081 | static void init_nfs4_replay(struct nfs4_replay *rp) |
| 4082 | { |
| 4083 | rp->rp_status = nfserr_serverfault; |
| 4084 | rp->rp_buflen = 0; |
| 4085 | rp->rp_buf = rp->rp_ibuf; |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 4086 | mutex_init(&rp->rp_mutex); |
| 4087 | } |
| 4088 | |
| 4089 | static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate, |
| 4090 | struct nfs4_stateowner *so) |
| 4091 | { |
| 4092 | if (!nfsd4_has_session(cstate)) { |
| 4093 | mutex_lock(&so->so_replay.rp_mutex); |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 4094 | cstate->replay_owner = nfs4_get_stateowner(so); |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 4095 | } |
| 4096 | } |
| 4097 | |
| 4098 | void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate) |
| 4099 | { |
| 4100 | struct nfs4_stateowner *so = cstate->replay_owner; |
| 4101 | |
| 4102 | if (so != NULL) { |
| 4103 | cstate->replay_owner = NULL; |
| 4104 | mutex_unlock(&so->so_replay.rp_mutex); |
| 4105 | nfs4_put_stateowner(so); |
| 4106 | } |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4107 | } |
| 4108 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4109 | 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] | 4110 | { |
| 4111 | struct nfs4_stateowner *sop; |
| 4112 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4113 | sop = kmem_cache_alloc(slab, GFP_KERNEL); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4114 | if (!sop) |
| 4115 | return NULL; |
| 4116 | |
J. Bruce Fields | 6f4859b | 2019-06-19 14:30:33 -0400 | [diff] [blame] | 4117 | xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4118 | if (!sop->so_owner.data) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4119 | kmem_cache_free(slab, sop); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4120 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4121 | } |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4122 | |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4123 | INIT_LIST_HEAD(&sop->so_stateids); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4124 | sop->so_client = clp; |
| 4125 | init_nfs4_replay(&sop->so_replay); |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 4126 | atomic_set(&sop->so_count, 1); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4127 | return sop; |
| 4128 | } |
| 4129 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4130 | 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] | 4131 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 4132 | lockdep_assert_held(&clp->cl_lock); |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 4133 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 4134 | list_add(&oo->oo_owner.so_strhash, |
| 4135 | &clp->cl_ownerstr_hashtbl[strhashval]); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4136 | list_add(&oo->oo_perclient, &clp->cl_openowners); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4137 | } |
| 4138 | |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 4139 | static void nfs4_unhash_openowner(struct nfs4_stateowner *so) |
| 4140 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 4141 | unhash_openowner_locked(openowner(so)); |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 4142 | } |
| 4143 | |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 4144 | static void nfs4_free_openowner(struct nfs4_stateowner *so) |
| 4145 | { |
| 4146 | struct nfs4_openowner *oo = openowner(so); |
| 4147 | |
| 4148 | kmem_cache_free(openowner_slab, oo); |
| 4149 | } |
| 4150 | |
| 4151 | static const struct nfs4_stateowner_operations openowner_ops = { |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 4152 | .so_unhash = nfs4_unhash_openowner, |
| 4153 | .so_free = nfs4_free_openowner, |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 4154 | }; |
| 4155 | |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4156 | static struct nfs4_ol_stateid * |
| 4157 | nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open) |
| 4158 | { |
| 4159 | struct nfs4_ol_stateid *local, *ret = NULL; |
| 4160 | struct nfs4_openowner *oo = open->op_openowner; |
| 4161 | |
| 4162 | lockdep_assert_held(&fp->fi_lock); |
| 4163 | |
| 4164 | list_for_each_entry(local, &fp->fi_stateids, st_perfile) { |
| 4165 | /* ignore lock owners */ |
| 4166 | if (local->st_stateowner->so_is_open_owner == 0) |
| 4167 | continue; |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 4168 | if (local->st_stateowner != &oo->oo_owner) |
| 4169 | continue; |
| 4170 | if (local->st_stid.sc_type == NFS4_OPEN_STID) { |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4171 | ret = local; |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 4172 | refcount_inc(&ret->st_stid.sc_count); |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4173 | break; |
| 4174 | } |
| 4175 | } |
| 4176 | return ret; |
| 4177 | } |
| 4178 | |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 4179 | static __be32 |
| 4180 | nfsd4_verify_open_stid(struct nfs4_stid *s) |
| 4181 | { |
| 4182 | __be32 ret = nfs_ok; |
| 4183 | |
| 4184 | switch (s->sc_type) { |
| 4185 | default: |
| 4186 | break; |
Trond Myklebust | 4f17641 | 2018-01-12 17:42:30 -0500 | [diff] [blame] | 4187 | case 0: |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 4188 | case NFS4_CLOSED_STID: |
| 4189 | case NFS4_CLOSED_DELEG_STID: |
| 4190 | ret = nfserr_bad_stateid; |
| 4191 | break; |
| 4192 | case NFS4_REVOKED_DELEG_STID: |
| 4193 | ret = nfserr_deleg_revoked; |
| 4194 | } |
| 4195 | return ret; |
| 4196 | } |
| 4197 | |
| 4198 | /* Lock the stateid st_mutex, and deal with races with CLOSE */ |
| 4199 | static __be32 |
| 4200 | nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp) |
| 4201 | { |
| 4202 | __be32 ret; |
| 4203 | |
Andrew Elble | 4f34bd0 | 2017-11-08 17:29:51 -0500 | [diff] [blame] | 4204 | mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX); |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 4205 | ret = nfsd4_verify_open_stid(&stp->st_stid); |
| 4206 | if (ret != nfs_ok) |
| 4207 | mutex_unlock(&stp->st_mutex); |
| 4208 | return ret; |
| 4209 | } |
| 4210 | |
| 4211 | static struct nfs4_ol_stateid * |
| 4212 | nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open) |
| 4213 | { |
| 4214 | struct nfs4_ol_stateid *stp; |
| 4215 | for (;;) { |
| 4216 | spin_lock(&fp->fi_lock); |
| 4217 | stp = nfsd4_find_existing_open(fp, open); |
| 4218 | spin_unlock(&fp->fi_lock); |
| 4219 | if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok) |
| 4220 | break; |
| 4221 | nfs4_put_stid(&stp->st_stid); |
| 4222 | } |
| 4223 | return stp; |
| 4224 | } |
| 4225 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4226 | static struct nfs4_openowner * |
Trond Myklebust | 13d6f66 | 2014-06-30 11:48:45 -0400 | [diff] [blame] | 4227 | alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open, |
Jeff Layton | db24b3b | 2014-06-30 11:48:36 -0400 | [diff] [blame] | 4228 | struct nfsd4_compound_state *cstate) |
| 4229 | { |
Trond Myklebust | 13d6f66 | 2014-06-30 11:48:45 -0400 | [diff] [blame] | 4230 | struct nfs4_client *clp = cstate->clp; |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 4231 | struct nfs4_openowner *oo, *ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4232 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4233 | oo = alloc_stateowner(openowner_slab, &open->op_owner, clp); |
| 4234 | if (!oo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4235 | return NULL; |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 4236 | oo->oo_owner.so_ops = &openowner_ops; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4237 | oo->oo_owner.so_is_open_owner = 1; |
| 4238 | oo->oo_owner.so_seqid = open->op_seqid; |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 4239 | oo->oo_flags = 0; |
Jeff Layton | db24b3b | 2014-06-30 11:48:36 -0400 | [diff] [blame] | 4240 | if (nfsd4_has_session(cstate)) |
| 4241 | oo->oo_flags |= NFS4_OO_CONFIRMED; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4242 | oo->oo_time = 0; |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 4243 | oo->oo_last_closed_stid = NULL; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4244 | INIT_LIST_HEAD(&oo->oo_close_lru); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 4245 | spin_lock(&clp->cl_lock); |
| 4246 | ret = find_openstateowner_str_locked(strhashval, open, clp); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 4247 | if (ret == NULL) { |
| 4248 | hash_openowner(oo, clp, strhashval); |
| 4249 | ret = oo; |
| 4250 | } else |
Kinglong Mee | d50ffde | 2015-07-16 12:05:07 +0800 | [diff] [blame] | 4251 | nfs4_free_stateowner(&oo->oo_owner); |
| 4252 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 4253 | spin_unlock(&clp->cl_lock); |
Jeff Layton | c595233 | 2015-03-23 10:53:42 -0400 | [diff] [blame] | 4254 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4255 | } |
| 4256 | |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4257 | static struct nfs4_ol_stateid * |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 4258 | init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open) |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4259 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4260 | |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4261 | struct nfs4_openowner *oo = open->op_openowner; |
| 4262 | struct nfs4_ol_stateid *retstp = NULL; |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 4263 | struct nfs4_ol_stateid *stp; |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4264 | |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 4265 | stp = open->op_stp; |
Oleg Drokin | 5cc1fb2 | 2016-06-14 23:28:05 -0400 | [diff] [blame] | 4266 | /* We are moving these outside of the spinlocks to avoid the warnings */ |
| 4267 | mutex_init(&stp->st_mutex); |
Andrew Elble | 4f34bd0 | 2017-11-08 17:29:51 -0500 | [diff] [blame] | 4268 | mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX); |
Oleg Drokin | 5cc1fb2 | 2016-06-14 23:28:05 -0400 | [diff] [blame] | 4269 | |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 4270 | retry: |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4271 | spin_lock(&oo->oo_owner.so_client->cl_lock); |
| 4272 | spin_lock(&fp->fi_lock); |
| 4273 | |
| 4274 | retstp = nfsd4_find_existing_open(fp, open); |
| 4275 | if (retstp) |
| 4276 | goto out_unlock; |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 4277 | |
| 4278 | open->op_stp = NULL; |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 4279 | refcount_inc(&stp->st_stid.sc_count); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 4280 | stp->st_stid.sc_type = NFS4_OPEN_STID; |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 4281 | INIT_LIST_HEAD(&stp->st_locks); |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 4282 | stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 4283 | get_nfs4_file(fp); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 4284 | stp->st_stid.sc_file = fp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4285 | stp->st_access_bmap = 0; |
| 4286 | stp->st_deny_bmap = 0; |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 4287 | stp->st_openstp = NULL; |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 4288 | list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 4289 | list_add(&stp->st_perfile, &fp->fi_stateids); |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4290 | |
| 4291 | out_unlock: |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 4292 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 4293 | spin_unlock(&oo->oo_owner.so_client->cl_lock); |
Oleg Drokin | 5cc1fb2 | 2016-06-14 23:28:05 -0400 | [diff] [blame] | 4294 | if (retstp) { |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 4295 | /* Handle races with CLOSE */ |
| 4296 | if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) { |
| 4297 | nfs4_put_stid(&retstp->st_stid); |
| 4298 | goto retry; |
| 4299 | } |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 4300 | /* To keep mutex tracking happy */ |
Oleg Drokin | 5cc1fb2 | 2016-06-14 23:28:05 -0400 | [diff] [blame] | 4301 | mutex_unlock(&stp->st_mutex); |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 4302 | stp = retstp; |
Oleg Drokin | 5cc1fb2 | 2016-06-14 23:28:05 -0400 | [diff] [blame] | 4303 | } |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 4304 | return stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4305 | } |
| 4306 | |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 4307 | /* |
| 4308 | * In the 4.0 case we need to keep the owners around a little while to handle |
| 4309 | * CLOSE replay. We still do need to release any file access that is held by |
| 4310 | * them before returning however. |
| 4311 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4312 | static void |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 4313 | move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4314 | { |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 4315 | struct nfs4_ol_stateid *last; |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 4316 | struct nfs4_openowner *oo = openowner(s->st_stateowner); |
| 4317 | struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net, |
| 4318 | nfsd_net_id); |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 4319 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4320 | dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4321 | |
Jeff Layton | b401be22 | 2014-07-29 21:34:33 -0400 | [diff] [blame] | 4322 | /* |
| 4323 | * We know that we hold one reference via nfsd4_close, and another |
| 4324 | * "persistent" reference for the client. If the refcount is higher |
| 4325 | * than 2, then there are still calls in progress that are using this |
| 4326 | * stateid. We can't put the sc_file reference until they are finished. |
| 4327 | * Wait for the refcount to drop to 2. Since it has been unhashed, |
| 4328 | * there should be no danger of the refcount going back up again at |
| 4329 | * this point. |
| 4330 | */ |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 4331 | wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2); |
Jeff Layton | b401be22 | 2014-07-29 21:34:33 -0400 | [diff] [blame] | 4332 | |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 4333 | release_all_access(s); |
| 4334 | if (s->st_stid.sc_file) { |
| 4335 | put_nfs4_file(s->st_stid.sc_file); |
| 4336 | s->st_stid.sc_file = NULL; |
| 4337 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 4338 | |
| 4339 | spin_lock(&nn->client_lock); |
| 4340 | last = oo->oo_last_closed_stid; |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 4341 | oo->oo_last_closed_stid = s; |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 4342 | list_move_tail(&oo->oo_close_lru, &nn->close_lru); |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 4343 | oo->oo_time = ktime_get_boottime_seconds(); |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 4344 | spin_unlock(&nn->client_lock); |
| 4345 | if (last) |
| 4346 | nfs4_put_stid(&last->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4347 | } |
| 4348 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4349 | /* search file_hashtbl[] for file */ |
| 4350 | static struct nfs4_file * |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 4351 | find_file_locked(struct knfsd_fh *fh, unsigned int hashval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4352 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4353 | struct nfs4_file *fp; |
| 4354 | |
Madhuparna Bhowmik | 36a8049 | 2020-02-13 20:33:31 +0530 | [diff] [blame] | 4355 | hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash, |
| 4356 | lockdep_is_held(&state_lock)) { |
Christoph Hellwig | 4d94c2e | 2014-08-14 08:41:48 +0200 | [diff] [blame] | 4357 | if (fh_match(&fp->fi_fhandle, fh)) { |
Elena Reshetova | 818a34e | 2017-10-20 12:53:30 +0300 | [diff] [blame] | 4358 | if (refcount_inc_not_zero(&fp->fi_ref)) |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 4359 | return fp; |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 4360 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4361 | } |
| 4362 | return NULL; |
| 4363 | } |
| 4364 | |
Christoph Hellwig | e6ba76e | 2014-08-14 08:50:16 +0200 | [diff] [blame] | 4365 | struct nfs4_file * |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 4366 | find_file(struct knfsd_fh *fh) |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 4367 | { |
| 4368 | struct nfs4_file *fp; |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 4369 | unsigned int hashval = file_hashval(fh); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 4370 | |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 4371 | rcu_read_lock(); |
| 4372 | fp = find_file_locked(fh, hashval); |
| 4373 | rcu_read_unlock(); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 4374 | return fp; |
| 4375 | } |
| 4376 | |
| 4377 | static struct nfs4_file * |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 4378 | find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh) |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 4379 | { |
| 4380 | struct nfs4_file *fp; |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 4381 | unsigned int hashval = file_hashval(fh); |
| 4382 | |
| 4383 | rcu_read_lock(); |
| 4384 | fp = find_file_locked(fh, hashval); |
| 4385 | rcu_read_unlock(); |
| 4386 | if (fp) |
| 4387 | return fp; |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 4388 | |
| 4389 | spin_lock(&state_lock); |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 4390 | fp = find_file_locked(fh, hashval); |
| 4391 | if (likely(fp == NULL)) { |
| 4392 | nfsd4_init_file(fh, hashval, new); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 4393 | fp = new; |
| 4394 | } |
| 4395 | spin_unlock(&state_lock); |
| 4396 | |
| 4397 | return fp; |
| 4398 | } |
| 4399 | |
J. Bruce Fields | 4f83aa3 | 2008-07-07 15:02:02 -0400 | [diff] [blame] | 4400 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4401 | * Called to check deny when READ with all zero stateid or |
| 4402 | * WRITE with all zero or all one stateid |
| 4403 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4404 | static __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4405 | nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type) |
| 4406 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4407 | struct nfs4_file *fp; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4408 | __be32 ret = nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4409 | |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 4410 | fp = find_file(¤t_fh->fh_handle); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 4411 | if (!fp) |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4412 | return ret; |
| 4413 | /* Check for conflicting share reservations */ |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 4414 | spin_lock(&fp->fi_lock); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4415 | if (fp->fi_share_deny & deny_type) |
| 4416 | ret = nfserr_locked; |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 4417 | spin_unlock(&fp->fi_lock); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 4418 | put_nfs4_file(fp); |
| 4419 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4420 | } |
| 4421 | |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 4422 | static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4423 | { |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 4424 | struct nfs4_delegation *dp = cb_to_delegation(cb); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 4425 | struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net, |
| 4426 | nfsd_net_id); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 4427 | |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 4428 | block_delegations(&dp->dl_stid.sc_file->fi_fhandle); |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 4429 | |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 4430 | /* |
| 4431 | * 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] | 4432 | * already holding inode->i_lock. |
| 4433 | * |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 4434 | * If the dl_time != 0, then we know that it has already been |
| 4435 | * queued for a lease break. Don't queue it again. |
| 4436 | */ |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 4437 | spin_lock(&state_lock); |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 4438 | if (dp->dl_time == 0) { |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 4439 | dp->dl_time = ktime_get_boottime_seconds(); |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 4440 | list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru); |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 4441 | } |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 4442 | spin_unlock(&state_lock); |
| 4443 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4444 | |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 4445 | static int nfsd4_cb_recall_done(struct nfsd4_callback *cb, |
| 4446 | struct rpc_task *task) |
| 4447 | { |
| 4448 | struct nfs4_delegation *dp = cb_to_delegation(cb); |
| 4449 | |
Andrew Elble | a457974 | 2015-08-31 12:06:41 -0400 | [diff] [blame] | 4450 | if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID) |
| 4451 | return 1; |
| 4452 | |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 4453 | switch (task->tk_status) { |
| 4454 | case 0: |
| 4455 | return 1; |
Scott Mayhew | 1c73b9d | 2019-05-02 13:32:12 -0400 | [diff] [blame] | 4456 | case -NFS4ERR_DELAY: |
| 4457 | rpc_delay(task, 2 * HZ); |
| 4458 | return 0; |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 4459 | case -EBADHANDLE: |
| 4460 | case -NFS4ERR_BAD_STATEID: |
| 4461 | /* |
| 4462 | * Race: client probably got cb_recall before open reply |
| 4463 | * granting delegation. |
| 4464 | */ |
| 4465 | if (dp->dl_retries--) { |
| 4466 | rpc_delay(task, 2 * HZ); |
| 4467 | return 0; |
| 4468 | } |
| 4469 | /*FALLTHRU*/ |
| 4470 | default: |
Scott Mayhew | 1c73b9d | 2019-05-02 13:32:12 -0400 | [diff] [blame] | 4471 | return 1; |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 4472 | } |
| 4473 | } |
| 4474 | |
| 4475 | static void nfsd4_cb_recall_release(struct nfsd4_callback *cb) |
| 4476 | { |
| 4477 | struct nfs4_delegation *dp = cb_to_delegation(cb); |
| 4478 | |
| 4479 | nfs4_put_stid(&dp->dl_stid); |
| 4480 | } |
| 4481 | |
Julia Lawall | c4cb897 | 2015-11-21 22:57:39 +0100 | [diff] [blame] | 4482 | static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = { |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 4483 | .prepare = nfsd4_cb_recall_prepare, |
| 4484 | .done = nfsd4_cb_recall_done, |
| 4485 | .release = nfsd4_cb_recall_release, |
| 4486 | }; |
| 4487 | |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 4488 | static void nfsd_break_one_deleg(struct nfs4_delegation *dp) |
| 4489 | { |
| 4490 | /* |
| 4491 | * We're assuming the state code never drops its reference |
| 4492 | * without first removing the lease. Since we're in this lease |
J. Bruce Fields | 4a269efb | 2018-04-13 17:34:35 -0400 | [diff] [blame] | 4493 | * callback (and since the lease code is serialized by the |
| 4494 | * i_lock) we know the server hasn't removed the lease yet, and |
| 4495 | * we know it's safe to take a reference. |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 4496 | */ |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 4497 | refcount_inc(&dp->dl_stid.sc_count); |
Christoph Hellwig | f0b5de1 | 2014-09-24 12:19:18 +0200 | [diff] [blame] | 4498 | nfsd4_run_cb(&dp->dl_recall); |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 4499 | } |
| 4500 | |
Jeff Layton | 1c8c601 | 2013-06-21 08:58:15 -0400 | [diff] [blame] | 4501 | /* Called from break_lease() with i_lock held. */ |
Jeff Layton | 4d01b7f | 2014-09-01 15:06:54 -0400 | [diff] [blame] | 4502 | static bool |
| 4503 | nfsd_break_deleg_cb(struct file_lock *fl) |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 4504 | { |
Jeff Layton | 4d01b7f | 2014-09-01 15:06:54 -0400 | [diff] [blame] | 4505 | bool ret = false; |
J. Bruce Fields | 653e514 | 2018-02-15 22:08:45 -0500 | [diff] [blame] | 4506 | struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner; |
| 4507 | struct nfs4_file *fp = dp->dl_stid.sc_file; |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 4508 | |
J. Bruce Fields | 0272e1f | 2007-09-12 18:56:12 -0400 | [diff] [blame] | 4509 | /* |
| 4510 | * 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] | 4511 | * we'll remove it ourself if a delegation isn't returned |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 4512 | * in time: |
J. Bruce Fields | 0272e1f | 2007-09-12 18:56:12 -0400 | [diff] [blame] | 4513 | */ |
| 4514 | fl->fl_break_time = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4515 | |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 4516 | spin_lock(&fp->fi_lock); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4517 | fp->fi_had_conflict = true; |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 4518 | nfsd_break_one_deleg(dp); |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 4519 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 4d01b7f | 2014-09-01 15:06:54 -0400 | [diff] [blame] | 4520 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4521 | } |
| 4522 | |
Jeff Layton | c45198e | 2014-09-01 07:12:07 -0400 | [diff] [blame] | 4523 | static int |
Jeff Layton | 7448cc3 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 4524 | nfsd_change_deleg_cb(struct file_lock *onlist, int arg, |
| 4525 | struct list_head *dispose) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4526 | { |
| 4527 | if (arg & F_UNLCK) |
Jeff Layton | c45198e | 2014-09-01 07:12:07 -0400 | [diff] [blame] | 4528 | return lease_modify(onlist, arg, dispose); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4529 | else |
| 4530 | return -EAGAIN; |
| 4531 | } |
| 4532 | |
Alexey Dobriyan | 7b02196 | 2009-09-21 17:01:12 -0700 | [diff] [blame] | 4533 | static const struct lock_manager_operations nfsd_lease_mng_ops = { |
J. Bruce Fields | 8fb47a4 | 2011-07-20 20:21:59 -0400 | [diff] [blame] | 4534 | .lm_break = nfsd_break_deleg_cb, |
| 4535 | .lm_change = nfsd_change_deleg_cb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4536 | }; |
| 4537 | |
J. Bruce Fields | 7a8711c | 2011-09-02 09:03:37 -0400 | [diff] [blame] | 4538 | static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid) |
| 4539 | { |
| 4540 | if (nfsd4_has_session(cstate)) |
| 4541 | return nfs_ok; |
| 4542 | if (seqid == so->so_seqid - 1) |
| 4543 | return nfserr_replay_me; |
| 4544 | if (seqid == so->so_seqid) |
| 4545 | return nfs_ok; |
| 4546 | return nfserr_bad_seqid; |
| 4547 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4548 | |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4549 | static __be32 lookup_clientid(clientid_t *clid, |
| 4550 | struct nfsd4_compound_state *cstate, |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 4551 | struct nfsd_net *nn, |
| 4552 | bool sessions) |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4553 | { |
| 4554 | struct nfs4_client *found; |
| 4555 | |
| 4556 | if (cstate->clp) { |
| 4557 | found = cstate->clp; |
| 4558 | if (!same_clid(&found->cl_clientid, clid)) |
| 4559 | return nfserr_stale_clientid; |
| 4560 | return nfs_ok; |
| 4561 | } |
| 4562 | |
| 4563 | if (STALE_CLIENTID(clid, nn)) |
| 4564 | return nfserr_stale_clientid; |
| 4565 | |
| 4566 | /* |
| 4567 | * For v4.1+ we get the client in the SEQUENCE op. If we don't have one |
| 4568 | * cached already then we know this is for is for v4.0 and "sessions" |
| 4569 | * will be false. |
| 4570 | */ |
| 4571 | WARN_ON_ONCE(cstate->session); |
Trond Myklebust | 3e339f9 | 2014-07-30 08:27:09 -0400 | [diff] [blame] | 4572 | spin_lock(&nn->client_lock); |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 4573 | found = find_confirmed_client(clid, sessions, nn); |
Trond Myklebust | 3e339f9 | 2014-07-30 08:27:09 -0400 | [diff] [blame] | 4574 | if (!found) { |
| 4575 | spin_unlock(&nn->client_lock); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4576 | return nfserr_expired; |
Trond Myklebust | 3e339f9 | 2014-07-30 08:27:09 -0400 | [diff] [blame] | 4577 | } |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 4578 | atomic_inc(&found->cl_rpc_users); |
Trond Myklebust | 3e339f9 | 2014-07-30 08:27:09 -0400 | [diff] [blame] | 4579 | spin_unlock(&nn->client_lock); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4580 | |
| 4581 | /* Cache the nfs4_client in cstate! */ |
| 4582 | cstate->clp = found; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4583 | return nfs_ok; |
| 4584 | } |
| 4585 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4586 | __be32 |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 4587 | nfsd4_process_open1(struct nfsd4_compound_state *cstate, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4588 | struct nfsd4_open *open, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4589 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4590 | clientid_t *clientid = &open->op_clientid; |
| 4591 | struct nfs4_client *clp = NULL; |
| 4592 | unsigned int strhashval; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4593 | struct nfs4_openowner *oo = NULL; |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 4594 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4595 | |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 4596 | if (STALE_CLIENTID(&open->op_clientid, nn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4597 | return nfserr_stale_clientid; |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 4598 | /* |
| 4599 | * In case we need it later, after we've already created the |
| 4600 | * file and don't want to risk a further failure: |
| 4601 | */ |
| 4602 | open->op_file = nfsd4_alloc_file(); |
| 4603 | if (open->op_file == NULL) |
| 4604 | return nfserr_jukebox; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4605 | |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 4606 | status = lookup_clientid(clientid, cstate, nn, false); |
Trond Myklebust | 2d91e89 | 2014-06-30 11:48:46 -0400 | [diff] [blame] | 4607 | if (status) |
| 4608 | return status; |
| 4609 | clp = cstate->clp; |
| 4610 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 4611 | strhashval = ownerstr_hashval(&open->op_owner); |
| 4612 | oo = find_openstateowner_str(strhashval, open, clp); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4613 | open->op_openowner = oo; |
| 4614 | if (!oo) { |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 4615 | goto new_owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4616 | } |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 4617 | if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) { |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 4618 | /* Replace unconfirmed owners without checking for replay. */ |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4619 | release_openowner(oo); |
| 4620 | open->op_openowner = NULL; |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 4621 | goto new_owner; |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 4622 | } |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 4623 | status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid); |
| 4624 | if (status) |
| 4625 | return status; |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 4626 | goto alloc_stateid; |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 4627 | new_owner: |
Trond Myklebust | 13d6f66 | 2014-06-30 11:48:45 -0400 | [diff] [blame] | 4628 | oo = alloc_init_open_stateowner(strhashval, open, cstate); |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 4629 | if (oo == NULL) |
| 4630 | return nfserr_jukebox; |
| 4631 | open->op_openowner = oo; |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 4632 | alloc_stateid: |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 4633 | open->op_stp = nfs4_alloc_open_stateid(clp); |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 4634 | if (!open->op_stp) |
| 4635 | return nfserr_jukebox; |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 4636 | |
| 4637 | if (nfsd4_has_session(cstate) && |
| 4638 | (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) { |
| 4639 | open->op_odstate = alloc_clnt_odstate(clp); |
| 4640 | if (!open->op_odstate) |
| 4641 | return nfserr_jukebox; |
| 4642 | } |
| 4643 | |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 4644 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4645 | } |
| 4646 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4647 | static inline __be32 |
NeilBrown | 4a6e43e | 2005-06-23 22:02:50 -0700 | [diff] [blame] | 4648 | nfs4_check_delegmode(struct nfs4_delegation *dp, int flags) |
| 4649 | { |
| 4650 | if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ)) |
| 4651 | return nfserr_openmode; |
| 4652 | else |
| 4653 | return nfs_ok; |
| 4654 | } |
| 4655 | |
Daniel Mack | c47d832 | 2011-05-16 16:38:14 +0200 | [diff] [blame] | 4656 | static int share_access_to_flags(u32 share_access) |
J. Bruce Fields | 24a0111 | 2010-05-18 20:01:35 -0400 | [diff] [blame] | 4657 | { |
J. Bruce Fields | 24a0111 | 2010-05-18 20:01:35 -0400 | [diff] [blame] | 4658 | return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE; |
| 4659 | } |
| 4660 | |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4661 | 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] | 4662 | { |
| 4663 | struct nfs4_stid *ret; |
| 4664 | |
Andrew Elble | 95da1b3a | 2017-11-03 14:06:31 -0400 | [diff] [blame] | 4665 | ret = find_stateid_by_type(cl, s, |
| 4666 | NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID); |
J. Bruce Fields | f459e45 | 2011-09-09 09:06:12 -0400 | [diff] [blame] | 4667 | if (!ret) |
| 4668 | return NULL; |
| 4669 | return delegstateid(ret); |
| 4670 | } |
| 4671 | |
J. Bruce Fields | 8b289b2 | 2011-10-19 11:52:12 -0400 | [diff] [blame] | 4672 | static bool nfsd4_is_deleg_cur(struct nfsd4_open *open) |
| 4673 | { |
| 4674 | return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR || |
| 4675 | open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH; |
| 4676 | } |
| 4677 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4678 | static __be32 |
J. Bruce Fields | 41d2266 | 2013-03-21 15:49:47 -0400 | [diff] [blame] | 4679 | nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open, |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 4680 | struct nfs4_delegation **dp) |
| 4681 | { |
| 4682 | int flags; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4683 | __be32 status = nfserr_bad_stateid; |
Trond Myklebust | dcd94cc | 2014-07-29 21:34:18 -0400 | [diff] [blame] | 4684 | struct nfs4_delegation *deleg; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 4685 | |
Trond Myklebust | dcd94cc | 2014-07-29 21:34:18 -0400 | [diff] [blame] | 4686 | deleg = find_deleg_stateid(cl, &open->op_delegate_stateid); |
| 4687 | if (deleg == NULL) |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 4688 | goto out; |
Andrew Elble | 95da1b3a | 2017-11-03 14:06:31 -0400 | [diff] [blame] | 4689 | if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) { |
| 4690 | nfs4_put_stid(&deleg->dl_stid); |
| 4691 | if (cl->cl_minorversion) |
| 4692 | status = nfserr_deleg_revoked; |
| 4693 | goto out; |
| 4694 | } |
J. Bruce Fields | 24a0111 | 2010-05-18 20:01:35 -0400 | [diff] [blame] | 4695 | flags = share_access_to_flags(open->op_share_access); |
Trond Myklebust | dcd94cc | 2014-07-29 21:34:18 -0400 | [diff] [blame] | 4696 | status = nfs4_check_delegmode(deleg, flags); |
| 4697 | if (status) { |
| 4698 | nfs4_put_stid(&deleg->dl_stid); |
| 4699 | goto out; |
| 4700 | } |
| 4701 | *dp = deleg; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 4702 | out: |
J. Bruce Fields | 8b289b2 | 2011-10-19 11:52:12 -0400 | [diff] [blame] | 4703 | if (!nfsd4_is_deleg_cur(open)) |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 4704 | return nfs_ok; |
| 4705 | if (status) |
| 4706 | return status; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 4707 | open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 4708 | return nfs_ok; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 4709 | } |
| 4710 | |
J. Bruce Fields | 21fb401 | 2010-07-28 12:21:23 -0400 | [diff] [blame] | 4711 | static inline int nfs4_access_to_access(u32 nfs4_access) |
| 4712 | { |
| 4713 | int flags = 0; |
| 4714 | |
| 4715 | if (nfs4_access & NFS4_SHARE_ACCESS_READ) |
| 4716 | flags |= NFSD_MAY_READ; |
| 4717 | if (nfs4_access & NFS4_SHARE_ACCESS_WRITE) |
| 4718 | flags |= NFSD_MAY_WRITE; |
| 4719 | return flags; |
| 4720 | } |
| 4721 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4722 | static inline __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4723 | nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh, |
| 4724 | struct nfsd4_open *open) |
| 4725 | { |
| 4726 | struct iattr iattr = { |
| 4727 | .ia_valid = ATTR_SIZE, |
| 4728 | .ia_size = 0, |
| 4729 | }; |
| 4730 | if (!open->op_truncate) |
| 4731 | return 0; |
| 4732 | if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE)) |
Al Viro | 9246585 | 2006-01-18 17:43:46 -0800 | [diff] [blame] | 4733 | return nfserr_inval; |
Arnd Bergmann | 2a1aa48 | 2019-11-03 17:50:18 +0100 | [diff] [blame] | 4734 | return nfsd_setattr(rqstp, fh, &iattr, 0, (time64_t)0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4735 | } |
| 4736 | |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4737 | 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] | 4738 | struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, |
| 4739 | struct nfsd4_open *open) |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4740 | { |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 4741 | struct nfsd_file *nf = NULL; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4742 | __be32 status; |
| 4743 | int oflag = nfs4_access_to_omode(open->op_share_access); |
| 4744 | int access = nfs4_access_to_access(open->op_share_access); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4745 | unsigned char old_access_bmap, old_deny_bmap; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4746 | |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4747 | spin_lock(&fp->fi_lock); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4748 | |
| 4749 | /* |
| 4750 | * Are we trying to set a deny mode that would conflict with |
| 4751 | * current access? |
| 4752 | */ |
| 4753 | status = nfs4_file_check_deny(fp, open->op_share_deny); |
| 4754 | if (status != nfs_ok) { |
| 4755 | spin_unlock(&fp->fi_lock); |
| 4756 | goto out; |
| 4757 | } |
| 4758 | |
| 4759 | /* set access to the file */ |
| 4760 | status = nfs4_file_get_access(fp, open->op_share_access); |
| 4761 | if (status != nfs_ok) { |
| 4762 | spin_unlock(&fp->fi_lock); |
| 4763 | goto out; |
| 4764 | } |
| 4765 | |
| 4766 | /* Set access bits in stateid */ |
| 4767 | old_access_bmap = stp->st_access_bmap; |
| 4768 | set_access(open->op_share_access, stp); |
| 4769 | |
| 4770 | /* Set new deny mask */ |
| 4771 | old_deny_bmap = stp->st_deny_bmap; |
| 4772 | set_deny(open->op_share_deny, stp); |
| 4773 | fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH); |
| 4774 | |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4775 | if (!fp->fi_fds[oflag]) { |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4776 | spin_unlock(&fp->fi_lock); |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 4777 | status = nfsd_file_acquire(rqstp, cur_fh, access, &nf); |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4778 | if (status) |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4779 | goto out_put_access; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4780 | spin_lock(&fp->fi_lock); |
| 4781 | if (!fp->fi_fds[oflag]) { |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 4782 | fp->fi_fds[oflag] = nf; |
| 4783 | nf = NULL; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4784 | } |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4785 | } |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4786 | spin_unlock(&fp->fi_lock); |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 4787 | if (nf) |
| 4788 | nfsd_file_put(nf); |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4789 | |
| 4790 | status = nfsd4_truncate(rqstp, cur_fh, open); |
| 4791 | if (status) |
| 4792 | goto out_put_access; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4793 | out: |
| 4794 | return status; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4795 | out_put_access: |
| 4796 | stp->st_access_bmap = old_access_bmap; |
| 4797 | nfs4_file_put_access(fp, open->op_share_access); |
| 4798 | reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp); |
| 4799 | goto out; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4800 | } |
| 4801 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4802 | static __be32 |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4803 | 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] | 4804 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4805 | __be32 status; |
Arnd Bergmann | 6ac7536 | 2015-05-12 23:31:29 +0200 | [diff] [blame] | 4806 | unsigned char old_deny_bmap = stp->st_deny_bmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4807 | |
Jeff Layton | 6eb3a1d | 2014-07-10 14:07:31 -0400 | [diff] [blame] | 4808 | if (!test_access(open->op_share_access, stp)) |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4809 | return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open); |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4810 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4811 | /* test and set deny mode */ |
| 4812 | spin_lock(&fp->fi_lock); |
| 4813 | status = nfs4_file_check_deny(fp, open->op_share_deny); |
| 4814 | if (status == nfs_ok) { |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4815 | set_deny(open->op_share_deny, stp); |
| 4816 | fp->fi_share_deny |= |
| 4817 | (open->op_share_deny & NFS4_SHARE_DENY_BOTH); |
| 4818 | } |
| 4819 | spin_unlock(&fp->fi_lock); |
| 4820 | |
| 4821 | if (status != nfs_ok) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4822 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4823 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4824 | status = nfsd4_truncate(rqstp, cur_fh, open); |
| 4825 | if (status != nfs_ok) |
| 4826 | reset_union_bmap_deny(old_deny_bmap, stp); |
| 4827 | return status; |
| 4828 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4829 | |
J. Bruce Fields | 14a24e9 | 2010-12-10 19:02:49 -0500 | [diff] [blame] | 4830 | /* Should we give out recallable state?: */ |
| 4831 | static bool nfsd4_cb_channel_good(struct nfs4_client *clp) |
| 4832 | { |
| 4833 | if (clp->cl_cb_state == NFSD4_CB_UP) |
| 4834 | return true; |
| 4835 | /* |
| 4836 | * In the sessions case, since we don't have to establish a |
| 4837 | * separate connection for callbacks, we assume it's OK |
| 4838 | * until we hear otherwise: |
| 4839 | */ |
| 4840 | return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN; |
| 4841 | } |
| 4842 | |
J. Bruce Fields | 653e514 | 2018-02-15 22:08:45 -0500 | [diff] [blame] | 4843 | static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, |
| 4844 | int flag) |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 4845 | { |
| 4846 | struct file_lock *fl; |
| 4847 | |
| 4848 | fl = locks_alloc_lock(); |
| 4849 | if (!fl) |
| 4850 | return NULL; |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 4851 | fl->fl_lmops = &nfsd_lease_mng_ops; |
J. Bruce Fields | 617588d | 2011-07-01 15:18:34 -0400 | [diff] [blame] | 4852 | fl->fl_flags = FL_DELEG; |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 4853 | fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK; |
| 4854 | fl->fl_end = OFFSET_MAX; |
J. Bruce Fields | 653e514 | 2018-02-15 22:08:45 -0500 | [diff] [blame] | 4855 | fl->fl_owner = (fl_owner_t)dp; |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 4856 | fl->fl_pid = current->tgid; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 4857 | 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] | 4858 | return fl; |
| 4859 | } |
| 4860 | |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 4861 | static struct nfs4_delegation * |
| 4862 | nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh, |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 4863 | struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate) |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 4864 | { |
J. Bruce Fields | 68b18f5 | 2018-02-19 11:38:33 -0500 | [diff] [blame] | 4865 | int status = 0; |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 4866 | struct nfs4_delegation *dp; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 4867 | struct nfsd_file *nf; |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 4868 | struct file_lock *fl; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4869 | |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 4870 | /* |
| 4871 | * The fi_had_conflict and nfs_get_existing_delegation checks |
| 4872 | * here are just optimizations; we'll need to recheck them at |
| 4873 | * the end: |
| 4874 | */ |
J. Bruce Fields | bf7bd3e | 2013-08-15 16:55:26 -0400 | [diff] [blame] | 4875 | if (fp->fi_had_conflict) |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 4876 | return ERR_PTR(-EAGAIN); |
| 4877 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 4878 | nf = find_readable_file(fp); |
| 4879 | if (!nf) { |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 4880 | /* We should always have a readable file here */ |
| 4881 | WARN_ON_ONCE(1); |
| 4882 | return ERR_PTR(-EBADF); |
| 4883 | } |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 4884 | spin_lock(&state_lock); |
| 4885 | spin_lock(&fp->fi_lock); |
J. Bruce Fields | 68b18f5 | 2018-02-19 11:38:33 -0500 | [diff] [blame] | 4886 | if (nfs4_delegation_exists(clp, fp)) |
| 4887 | status = -EAGAIN; |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 4888 | else if (!fp->fi_deleg_file) { |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 4889 | fp->fi_deleg_file = nf; |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 4890 | /* increment early to prevent fi_deleg_file from being |
| 4891 | * cleared */ |
| 4892 | fp->fi_delegees = 1; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 4893 | nf = NULL; |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 4894 | } else |
| 4895 | fp->fi_delegees++; |
| 4896 | spin_unlock(&fp->fi_lock); |
| 4897 | spin_unlock(&state_lock); |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 4898 | if (nf) |
| 4899 | nfsd_file_put(nf); |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 4900 | if (status) |
| 4901 | return ERR_PTR(status); |
| 4902 | |
| 4903 | status = -ENOMEM; |
| 4904 | dp = alloc_init_deleg(clp, fp, fh, odstate); |
| 4905 | if (!dp) |
| 4906 | goto out_delegees; |
| 4907 | |
| 4908 | fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ); |
| 4909 | if (!fl) |
Andrew Elble | bd8d725 | 2018-10-05 09:32:08 -0400 | [diff] [blame] | 4910 | goto out_clnt_odstate; |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 4911 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 4912 | 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] | 4913 | if (fl) |
| 4914 | locks_free_lock(fl); |
| 4915 | if (status) |
| 4916 | goto out_clnt_odstate; |
| 4917 | |
| 4918 | spin_lock(&state_lock); |
| 4919 | spin_lock(&fp->fi_lock); |
| 4920 | if (fp->fi_had_conflict) |
| 4921 | status = -EAGAIN; |
| 4922 | else |
| 4923 | status = hash_delegation_locked(dp, fp); |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 4924 | spin_unlock(&fp->fi_lock); |
| 4925 | spin_unlock(&state_lock); |
| 4926 | |
| 4927 | if (status) |
Andrew Elble | 692ad28 | 2018-04-18 17:04:37 -0400 | [diff] [blame] | 4928 | goto out_unlock; |
| 4929 | |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 4930 | return dp; |
Andrew Elble | 692ad28 | 2018-04-18 17:04:37 -0400 | [diff] [blame] | 4931 | out_unlock: |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 4932 | 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] | 4933 | out_clnt_odstate: |
| 4934 | put_clnt_odstate(dp->dl_clnt_odstate); |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 4935 | nfs4_put_stid(&dp->dl_stid); |
| 4936 | out_delegees: |
| 4937 | put_deleg_file(fp); |
| 4938 | return ERR_PTR(status); |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 4939 | } |
| 4940 | |
Benny Halevy | 4aa8913 | 2012-02-21 14:16:44 -0800 | [diff] [blame] | 4941 | static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status) |
| 4942 | { |
| 4943 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 4944 | if (status == -EAGAIN) |
| 4945 | open->op_why_no_deleg = WND4_CONTENTION; |
| 4946 | else { |
| 4947 | open->op_why_no_deleg = WND4_RESOURCE; |
| 4948 | switch (open->op_deleg_want) { |
| 4949 | case NFS4_SHARE_WANT_READ_DELEG: |
| 4950 | case NFS4_SHARE_WANT_WRITE_DELEG: |
| 4951 | case NFS4_SHARE_WANT_ANY_DELEG: |
| 4952 | break; |
| 4953 | case NFS4_SHARE_WANT_CANCEL: |
| 4954 | open->op_why_no_deleg = WND4_CANCELLED; |
| 4955 | break; |
| 4956 | case NFS4_SHARE_WANT_NO_DELEG: |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 4957 | WARN_ON_ONCE(1); |
Benny Halevy | 4aa8913 | 2012-02-21 14:16:44 -0800 | [diff] [blame] | 4958 | } |
| 4959 | } |
| 4960 | } |
| 4961 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4962 | /* |
| 4963 | * Attempt to hand out a delegation. |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4964 | * |
| 4965 | * Note we don't support write delegations, and won't until the vfs has |
| 4966 | * proper support for them. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4967 | */ |
| 4968 | static void |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame] | 4969 | nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, |
| 4970 | struct nfs4_ol_stateid *stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4971 | { |
| 4972 | struct nfs4_delegation *dp; |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame] | 4973 | struct nfs4_openowner *oo = openowner(stp->st_stateowner); |
| 4974 | struct nfs4_client *clp = stp->st_stid.sc_client; |
J. Bruce Fields | 14a24e9 | 2010-12-10 19:02:49 -0500 | [diff] [blame] | 4975 | int cb_up; |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4976 | int status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4977 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4978 | cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client); |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 4979 | open->op_recall = 0; |
| 4980 | switch (open->op_claim_type) { |
| 4981 | case NFS4_OPEN_CLAIM_PREVIOUS: |
J. Bruce Fields | 2bf2387 | 2010-03-08 12:37:27 -0500 | [diff] [blame] | 4982 | if (!cb_up) |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 4983 | open->op_recall = 1; |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4984 | if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ) |
| 4985 | goto out_no_deleg; |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 4986 | break; |
| 4987 | case NFS4_OPEN_CLAIM_NULL: |
Ming Chen | ed47b06 | 2014-01-09 21:26:10 +0000 | [diff] [blame] | 4988 | case NFS4_OPEN_CLAIM_FH: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4989 | /* |
| 4990 | * Let's not give out any delegations till everyone's |
J. Bruce Fields | c87fb4a | 2015-08-06 12:47:02 -0400 | [diff] [blame] | 4991 | * had the chance to reclaim theirs, *and* until |
| 4992 | * NLM locks have all been reclaimed: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4993 | */ |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame] | 4994 | if (locks_in_grace(clp->net)) |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4995 | goto out_no_deleg; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 4996 | if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED)) |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4997 | goto out_no_deleg; |
Steve Dickson | 9a0590a | 2013-05-15 14:51:49 -0400 | [diff] [blame] | 4998 | /* |
| 4999 | * Also, if the file was opened for write or |
| 5000 | * create, there's a good chance the client's |
| 5001 | * about to write to it, resulting in an |
| 5002 | * immediate recall (since we don't support |
| 5003 | * write delegations): |
| 5004 | */ |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 5005 | if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 5006 | goto out_no_deleg; |
| 5007 | if (open->op_create == NFS4_OPEN_CREATE) |
| 5008 | goto out_no_deleg; |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 5009 | break; |
| 5010 | default: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 5011 | goto out_no_deleg; |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 5012 | } |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 5013 | 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] | 5014 | if (IS_ERR(dp)) |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 5015 | goto out_no_deleg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5016 | |
J. Bruce Fields | d5477a8 | 2011-09-08 12:07:44 -0400 | [diff] [blame] | 5017 | 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] | 5018 | |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 5019 | dprintk("NFSD: delegation stateid=" STATEID_FMT "\n", |
J. Bruce Fields | d5477a8 | 2011-09-08 12:07:44 -0400 | [diff] [blame] | 5020 | STATEID_VAL(&dp->dl_stid.sc_stateid)); |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 5021 | open->op_delegate_type = NFS4_OPEN_DELEGATE_READ; |
Trond Myklebust | 67cb127 | 2014-07-29 21:34:17 -0400 | [diff] [blame] | 5022 | nfs4_put_stid(&dp->dl_stid); |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 5023 | return; |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 5024 | out_no_deleg: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 5025 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE; |
| 5026 | if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS && |
J. Bruce Fields | d08d32e | 2013-06-21 11:05:32 -0400 | [diff] [blame] | 5027 | open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) { |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 5028 | dprintk("NFSD: WARNING: refusing delegation reclaim\n"); |
J. Bruce Fields | d08d32e | 2013-06-21 11:05:32 -0400 | [diff] [blame] | 5029 | open->op_recall = 1; |
| 5030 | } |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 5031 | |
| 5032 | /* 4.1 client asking for a delegation? */ |
| 5033 | if (open->op_deleg_want) |
| 5034 | nfsd4_open_deleg_none_ext(open, status); |
| 5035 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5036 | } |
| 5037 | |
Benny Halevy | e27f49c | 2012-02-21 14:16:54 -0800 | [diff] [blame] | 5038 | static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open, |
| 5039 | struct nfs4_delegation *dp) |
| 5040 | { |
| 5041 | if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG && |
| 5042 | dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) { |
| 5043 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 5044 | open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE; |
| 5045 | } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG && |
| 5046 | dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) { |
| 5047 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 5048 | open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE; |
| 5049 | } |
| 5050 | /* Otherwise the client must be confused wanting a delegation |
| 5051 | * it already has, therefore we don't return |
| 5052 | * NFS4_OPEN_DELEGATE_NONE_EXT and reason. |
| 5053 | */ |
| 5054 | } |
| 5055 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5056 | __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5057 | nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open) |
| 5058 | { |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 5059 | struct nfsd4_compoundres *resp = rqstp->rq_resp; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5060 | struct nfs4_client *cl = open->op_openowner->oo_owner.so_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5061 | struct nfs4_file *fp = NULL; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5062 | struct nfs4_ol_stateid *stp = NULL; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 5063 | struct nfs4_delegation *dp = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5064 | __be32 status; |
Trond Myklebust | d8a1a00 | 2017-11-03 08:00:11 -0400 | [diff] [blame] | 5065 | bool new_stp = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5066 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5067 | /* |
| 5068 | * Lookup file; if found, lookup stateid and check open request, |
| 5069 | * and check for delegations in the process of being recalled. |
| 5070 | * If not found, create the nfs4_file struct |
| 5071 | */ |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 5072 | fp = find_or_add_file(open->op_file, ¤t_fh->fh_handle); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 5073 | if (fp != open->op_file) { |
J. Bruce Fields | 41d2266 | 2013-03-21 15:49:47 -0400 | [diff] [blame] | 5074 | status = nfs4_check_deleg(cl, open, &dp); |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 5075 | if (status) |
| 5076 | goto out; |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 5077 | stp = nfsd4_find_and_lock_existing_open(fp, open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5078 | } else { |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 5079 | open->op_file = NULL; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 5080 | status = nfserr_bad_stateid; |
J. Bruce Fields | 8b289b2 | 2011-10-19 11:52:12 -0400 | [diff] [blame] | 5081 | if (nfsd4_is_deleg_cur(open)) |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 5082 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5083 | } |
| 5084 | |
Trond Myklebust | d8a1a00 | 2017-11-03 08:00:11 -0400 | [diff] [blame] | 5085 | if (!stp) { |
| 5086 | stp = init_open_stateid(fp, open); |
| 5087 | if (!open->op_stp) |
| 5088 | new_stp = true; |
| 5089 | } |
| 5090 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5091 | /* |
| 5092 | * OPEN the file, or upgrade an existing OPEN. |
| 5093 | * If truncate fails, the OPEN fails. |
Trond Myklebust | d8a1a00 | 2017-11-03 08:00:11 -0400 | [diff] [blame] | 5094 | * |
| 5095 | * stp is already locked. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5096 | */ |
Trond Myklebust | d8a1a00 | 2017-11-03 08:00:11 -0400 | [diff] [blame] | 5097 | if (!new_stp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5098 | /* Stateid was found, this is an OPEN upgrade */ |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 5099 | status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open); |
Jeff Layton | 35a92fe | 2015-09-17 07:47:08 -0400 | [diff] [blame] | 5100 | if (status) { |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 5101 | mutex_unlock(&stp->st_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5102 | goto out; |
Jeff Layton | 35a92fe | 2015-09-17 07:47:08 -0400 | [diff] [blame] | 5103 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5104 | } else { |
Jeff Layton | 6eb3a1d | 2014-07-10 14:07:31 -0400 | [diff] [blame] | 5105 | status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open); |
| 5106 | if (status) { |
Trond Myklebust | d8a1a00 | 2017-11-03 08:00:11 -0400 | [diff] [blame] | 5107 | stp->st_stid.sc_type = NFS4_CLOSED_STID; |
Jeff Layton | 6eb3a1d | 2014-07-10 14:07:31 -0400 | [diff] [blame] | 5108 | release_open_stateid(stp); |
Trond Myklebust | d8a1a00 | 2017-11-03 08:00:11 -0400 | [diff] [blame] | 5109 | mutex_unlock(&stp->st_mutex); |
Jeff Layton | 6eb3a1d | 2014-07-10 14:07:31 -0400 | [diff] [blame] | 5110 | goto out; |
| 5111 | } |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 5112 | |
| 5113 | stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp, |
| 5114 | open->op_odstate); |
| 5115 | if (stp->st_clnt_odstate == open->op_odstate) |
| 5116 | open->op_odstate = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5117 | } |
Trond Myklebust | d8a1a00 | 2017-11-03 08:00:11 -0400 | [diff] [blame] | 5118 | |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 5119 | nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid); |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 5120 | mutex_unlock(&stp->st_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5121 | |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 5122 | if (nfsd4_has_session(&resp->cstate)) { |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 5123 | if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) { |
| 5124 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 5125 | open->op_why_no_deleg = WND4_NOT_WANTED; |
| 5126 | goto nodeleg; |
| 5127 | } |
| 5128 | } |
| 5129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5130 | /* |
| 5131 | * Attempt to hand out a delegation. No error return, because the |
| 5132 | * OPEN succeeds even if we fail. |
| 5133 | */ |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame] | 5134 | nfs4_open_delegation(current_fh, open, stp); |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 5135 | nodeleg: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5136 | status = nfs_ok; |
| 5137 | |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 5138 | dprintk("%s: stateid=" STATEID_FMT "\n", __func__, |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5139 | STATEID_VAL(&stp->st_stid.sc_stateid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5140 | out: |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 5141 | /* 4.1 client trying to upgrade/downgrade delegation? */ |
| 5142 | if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp && |
Benny Halevy | e27f49c | 2012-02-21 14:16:54 -0800 | [diff] [blame] | 5143 | open->op_deleg_want) |
| 5144 | nfsd4_deleg_xgrade_none_ext(open, dp); |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 5145 | |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 5146 | if (fp) |
| 5147 | put_nfs4_file(fp); |
NeilBrown | 3751517 | 2005-07-07 17:59:16 -0700 | [diff] [blame] | 5148 | if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS) |
Kinglong Mee | 8718602 | 2015-07-13 17:32:59 +0800 | [diff] [blame] | 5149 | open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5150 | /* |
| 5151 | * To finish the open response, we just need to set the rflags. |
| 5152 | */ |
| 5153 | open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX; |
Jeff Layton | 19e4c34 | 2016-09-16 16:28:27 -0400 | [diff] [blame] | 5154 | if (nfsd4_has_session(&resp->cstate)) |
| 5155 | open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK; |
| 5156 | else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5157 | open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM; |
Jeff Layton | 19e4c34 | 2016-09-16 16:28:27 -0400 | [diff] [blame] | 5158 | |
Trond Myklebust | dcd94cc | 2014-07-29 21:34:18 -0400 | [diff] [blame] | 5159 | if (dp) |
| 5160 | nfs4_put_stid(&dp->dl_stid); |
Trond Myklebust | d6f2bc5 | 2014-07-29 21:34:19 -0400 | [diff] [blame] | 5161 | if (stp) |
| 5162 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5163 | |
| 5164 | return status; |
| 5165 | } |
| 5166 | |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 5167 | void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate, |
Jeff Layton | 4229789 | 2015-03-23 10:53:44 -0400 | [diff] [blame] | 5168 | struct nfsd4_open *open) |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 5169 | { |
| 5170 | if (open->op_openowner) { |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 5171 | struct nfs4_stateowner *so = &open->op_openowner->oo_owner; |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 5172 | |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 5173 | nfsd4_cstate_assign_replay(cstate, so); |
| 5174 | nfs4_put_stateowner(so); |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 5175 | } |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 5176 | if (open->op_file) |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 5177 | kmem_cache_free(file_slab, open->op_file); |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 5178 | if (open->op_stp) |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 5179 | nfs4_put_stid(&open->op_stp->st_stid); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 5180 | if (open->op_odstate) |
| 5181 | kmem_cache_free(odstate_slab, open->op_odstate); |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 5182 | } |
| 5183 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5184 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 5185 | nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5186 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5187 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5188 | clientid_t *clid = &u->renew; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5189 | struct nfs4_client *clp; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5190 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 5191 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5192 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5193 | dprintk("process_renew(%08x/%08x): starting\n", |
| 5194 | clid->cl_boot, clid->cl_id); |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 5195 | status = lookup_clientid(clid, cstate, nn, false); |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 5196 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5197 | goto out; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 5198 | clp = cstate->clp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5199 | status = nfserr_cb_path_down; |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 5200 | if (!list_empty(&clp->cl_delegations) |
J. Bruce Fields | 77a3569 | 2010-04-30 18:51:44 -0400 | [diff] [blame] | 5201 | && clp->cl_cb_state != NFSD4_CB_UP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5202 | goto out; |
| 5203 | status = nfs_ok; |
| 5204 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5205 | return status; |
| 5206 | } |
| 5207 | |
Jeff Layton | 7f5ef2e | 2014-09-12 16:40:21 -0400 | [diff] [blame] | 5208 | void |
Stanislav Kinsbursky | 12760c6 | 2012-11-14 18:22:12 +0300 | [diff] [blame] | 5209 | nfsd4_end_grace(struct nfsd_net *nn) |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 5210 | { |
Jeff Layton | 33dcc48 | 2012-04-10 11:08:48 -0400 | [diff] [blame] | 5211 | /* do nothing if grace period already ended */ |
Stanislav Kinsbursky | a51c84e | 2012-07-25 16:57:37 +0400 | [diff] [blame] | 5212 | if (nn->grace_ended) |
Jeff Layton | 33dcc48 | 2012-04-10 11:08:48 -0400 | [diff] [blame] | 5213 | return; |
| 5214 | |
Stanislav Kinsbursky | a51c84e | 2012-07-25 16:57:37 +0400 | [diff] [blame] | 5215 | nn->grace_ended = true; |
J. Bruce Fields | 70b2823 | 2014-09-16 17:37:32 -0400 | [diff] [blame] | 5216 | /* |
| 5217 | * If the server goes down again right now, an NFSv4 |
| 5218 | * client will still be allowed to reclaim after it comes back up, |
| 5219 | * even if it hasn't yet had a chance to reclaim state this time. |
| 5220 | * |
| 5221 | */ |
Jeff Layton | 919b804 | 2014-09-12 16:40:20 -0400 | [diff] [blame] | 5222 | nfsd4_record_grace_done(nn); |
J. Bruce Fields | 70b2823 | 2014-09-16 17:37:32 -0400 | [diff] [blame] | 5223 | /* |
| 5224 | * At this point, NFSv4 clients can still reclaim. But if the |
| 5225 | * server crashes, any that have not yet reclaimed will be out |
| 5226 | * of luck on the next boot. |
| 5227 | * |
| 5228 | * (NFSv4.1+ clients are considered to have reclaimed once they |
| 5229 | * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to |
| 5230 | * have reclaimed after their first OPEN.) |
| 5231 | */ |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 5232 | locks_end_grace(&nn->nfsd4_manager); |
J. Bruce Fields | 70b2823 | 2014-09-16 17:37:32 -0400 | [diff] [blame] | 5233 | /* |
| 5234 | * At this point, and once lockd and/or any other containers |
| 5235 | * exit their grace period, further reclaims will fail and |
| 5236 | * regular locking can resume. |
| 5237 | */ |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 5238 | } |
| 5239 | |
J. Bruce Fields | 03f318c | 2018-06-08 12:28:47 -0400 | [diff] [blame] | 5240 | /* |
| 5241 | * If we've waited a lease period but there are still clients trying to |
| 5242 | * reclaim, wait a little longer to give them a chance to finish. |
| 5243 | */ |
| 5244 | static bool clients_still_reclaiming(struct nfsd_net *nn) |
| 5245 | { |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 5246 | time64_t double_grace_period_end = nn->boot_time + |
| 5247 | 2 * nn->nfsd4_lease; |
J. Bruce Fields | 03f318c | 2018-06-08 12:28:47 -0400 | [diff] [blame] | 5248 | |
Scott Mayhew | 362063a | 2019-03-26 18:06:28 -0400 | [diff] [blame] | 5249 | if (nn->track_reclaim_completes && |
| 5250 | atomic_read(&nn->nr_reclaim_complete) == |
| 5251 | nn->reclaim_str_hashtbl_size) |
| 5252 | return false; |
J. Bruce Fields | 03f318c | 2018-06-08 12:28:47 -0400 | [diff] [blame] | 5253 | if (!nn->somebody_reclaimed) |
| 5254 | return false; |
| 5255 | nn->somebody_reclaimed = false; |
| 5256 | /* |
| 5257 | * If we've given them *two* lease times to reclaim, and they're |
| 5258 | * still not done, give up: |
| 5259 | */ |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 5260 | if (ktime_get_boottime_seconds() > double_grace_period_end) |
J. Bruce Fields | 03f318c | 2018-06-08 12:28:47 -0400 | [diff] [blame] | 5261 | return false; |
| 5262 | return true; |
| 5263 | } |
| 5264 | |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 5265 | static time64_t |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 5266 | nfs4_laundromat(struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5267 | { |
| 5268 | struct nfs4_client *clp; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5269 | struct nfs4_openowner *oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5270 | struct nfs4_delegation *dp; |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 5271 | struct nfs4_ol_stateid *stp; |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 5272 | struct nfsd4_blocked_lock *nbl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5273 | struct list_head *pos, *next, reaplist; |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 5274 | time64_t cutoff = ktime_get_boottime_seconds() - nn->nfsd4_lease; |
| 5275 | time64_t t, new_timeo = nn->nfsd4_lease; |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 5276 | struct nfs4_cpntf_state *cps; |
| 5277 | copy_stateid_t *cps_t; |
| 5278 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5279 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5280 | dprintk("NFSD: laundromat service - starting\n"); |
J. Bruce Fields | 03f318c | 2018-06-08 12:28:47 -0400 | [diff] [blame] | 5281 | |
| 5282 | if (clients_still_reclaiming(nn)) { |
| 5283 | new_timeo = 0; |
| 5284 | goto out; |
| 5285 | } |
Scott Mayhew | 362063a | 2019-03-26 18:06:28 -0400 | [diff] [blame] | 5286 | dprintk("NFSD: end of grace period\n"); |
Stanislav Kinsbursky | 12760c6 | 2012-11-14 18:22:12 +0300 | [diff] [blame] | 5287 | nfsd4_end_grace(nn); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 5288 | INIT_LIST_HEAD(&reaplist); |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 5289 | |
| 5290 | spin_lock(&nn->s2s_cp_lock); |
| 5291 | idr_for_each_entry(&nn->s2s_cp_stateids, cps_t, i) { |
| 5292 | cps = container_of(cps_t, struct nfs4_cpntf_state, cp_stateid); |
| 5293 | if (cps->cp_stateid.sc_type == NFS4_COPYNOTIFY_STID && |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 5294 | cps->cpntf_time > cutoff) |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 5295 | _free_cpntf_state_locked(nn, cps); |
| 5296 | } |
| 5297 | spin_unlock(&nn->s2s_cp_lock); |
| 5298 | |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 5299 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 5ed58bb | 2012-11-14 18:21:56 +0300 | [diff] [blame] | 5300 | list_for_each_safe(pos, next, &nn->client_lru) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5301 | clp = list_entry(pos, struct nfs4_client, cl_lru); |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 5302 | if (clp->cl_time > cutoff) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5303 | t = clp->cl_time - cutoff; |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 5304 | new_timeo = min(new_timeo, t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5305 | break; |
| 5306 | } |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 5307 | if (mark_client_expired_locked(clp)) { |
Benny Halevy | d768298 | 2010-05-12 00:13:54 +0300 | [diff] [blame] | 5308 | dprintk("NFSD: client in use (clientid %08x)\n", |
| 5309 | clp->cl_clientid.cl_id); |
| 5310 | continue; |
| 5311 | } |
Trond Myklebust | 4864af9 | 2014-07-30 08:27:03 -0400 | [diff] [blame] | 5312 | list_add(&clp->cl_lru, &reaplist); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 5313 | } |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 5314 | spin_unlock(&nn->client_lock); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 5315 | list_for_each_safe(pos, next, &reaplist) { |
| 5316 | clp = list_entry(pos, struct nfs4_client, cl_lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5317 | dprintk("NFSD: purging unused client (clientid %08x)\n", |
| 5318 | clp->cl_clientid.cl_id); |
Trond Myklebust | 4864af9 | 2014-07-30 08:27:03 -0400 | [diff] [blame] | 5319 | list_del_init(&clp->cl_lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5320 | expire_client(clp); |
| 5321 | } |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5322 | spin_lock(&state_lock); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 5323 | list_for_each_safe(pos, next, &nn->del_recall_lru) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5324 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 5325 | if (dp->dl_time > cutoff) { |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 5326 | t = dp->dl_time - cutoff; |
| 5327 | new_timeo = min(new_timeo, t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5328 | break; |
| 5329 | } |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 5330 | WARN_ON(!unhash_delegation_locked(dp)); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 5331 | list_add(&dp->dl_recall_lru, &reaplist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5332 | } |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5333 | spin_unlock(&state_lock); |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 5334 | while (!list_empty(&reaplist)) { |
| 5335 | dp = list_first_entry(&reaplist, struct nfs4_delegation, |
| 5336 | dl_recall_lru); |
| 5337 | list_del_init(&dp->dl_recall_lru); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 5338 | revoke_delegation(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5339 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 5340 | |
| 5341 | spin_lock(&nn->client_lock); |
| 5342 | while (!list_empty(&nn->close_lru)) { |
| 5343 | oo = list_first_entry(&nn->close_lru, struct nfs4_openowner, |
| 5344 | oo_close_lru); |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 5345 | if (oo->oo_time > cutoff) { |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 5346 | t = oo->oo_time - cutoff; |
| 5347 | new_timeo = min(new_timeo, t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5348 | break; |
| 5349 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 5350 | list_del_init(&oo->oo_close_lru); |
| 5351 | stp = oo->oo_last_closed_stid; |
| 5352 | oo->oo_last_closed_stid = NULL; |
| 5353 | spin_unlock(&nn->client_lock); |
| 5354 | nfs4_put_stid(&stp->st_stid); |
| 5355 | spin_lock(&nn->client_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5356 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 5357 | spin_unlock(&nn->client_lock); |
| 5358 | |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 5359 | /* |
| 5360 | * It's possible for a client to try and acquire an already held lock |
| 5361 | * that is being held for a long time, and then lose interest in it. |
| 5362 | * So, we clean out any un-revisited request after a lease period |
| 5363 | * under the assumption that the client is no longer interested. |
| 5364 | * |
| 5365 | * RFC5661, sec. 9.6 states that the client must not rely on getting |
| 5366 | * notifications and must continue to poll for locks, even when the |
| 5367 | * server supports them. Thus this shouldn't lead to clients blocking |
| 5368 | * indefinitely once the lock does become free. |
| 5369 | */ |
| 5370 | BUG_ON(!list_empty(&reaplist)); |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 5371 | spin_lock(&nn->blocked_locks_lock); |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 5372 | while (!list_empty(&nn->blocked_locks_lru)) { |
| 5373 | nbl = list_first_entry(&nn->blocked_locks_lru, |
| 5374 | struct nfsd4_blocked_lock, nbl_lru); |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 5375 | if (nbl->nbl_time > cutoff) { |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 5376 | t = nbl->nbl_time - cutoff; |
| 5377 | new_timeo = min(new_timeo, t); |
| 5378 | break; |
| 5379 | } |
| 5380 | list_move(&nbl->nbl_lru, &reaplist); |
| 5381 | list_del_init(&nbl->nbl_list); |
| 5382 | } |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 5383 | spin_unlock(&nn->blocked_locks_lock); |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 5384 | |
| 5385 | while (!list_empty(&reaplist)) { |
Naofumi Honda | 64ebe124 | 2017-11-09 10:57:16 -0500 | [diff] [blame] | 5386 | nbl = list_first_entry(&reaplist, |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 5387 | struct nfsd4_blocked_lock, nbl_lru); |
| 5388 | list_del_init(&nbl->nbl_lru); |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 5389 | free_blocked_lock(nbl); |
| 5390 | } |
J. Bruce Fields | 03f318c | 2018-06-08 12:28:47 -0400 | [diff] [blame] | 5391 | out: |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 5392 | new_timeo = max_t(time64_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT); |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 5393 | return new_timeo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5394 | } |
| 5395 | |
Harvey Harrison | a254b24 | 2008-02-20 12:49:00 -0800 | [diff] [blame] | 5396 | static struct workqueue_struct *laundry_wq; |
| 5397 | static void laundromat_main(struct work_struct *); |
Harvey Harrison | a254b24 | 2008-02-20 12:49:00 -0800 | [diff] [blame] | 5398 | |
| 5399 | static void |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 5400 | laundromat_main(struct work_struct *laundry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5401 | { |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 5402 | time64_t t; |
Geliang Tang | 2e55f3a | 2016-01-01 22:06:28 +0800 | [diff] [blame] | 5403 | struct delayed_work *dwork = to_delayed_work(laundry); |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 5404 | struct nfsd_net *nn = container_of(dwork, struct nfsd_net, |
| 5405 | laundromat_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5406 | |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 5407 | t = nfs4_laundromat(nn); |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 5408 | dprintk("NFSD: laundromat_main - sleeping for %lld seconds\n", t); |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 5409 | queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5410 | } |
| 5411 | |
Jeff Layton | 8fcd461 | 2015-07-30 06:57:46 -0400 | [diff] [blame] | 5412 | 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] | 5413 | { |
Jeff Layton | 8fcd461 | 2015-07-30 06:57:46 -0400 | [diff] [blame] | 5414 | if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle)) |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 5415 | return nfserr_bad_stateid; |
| 5416 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5417 | } |
| 5418 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5419 | static inline int |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 5420 | access_permit_read(struct nfs4_ol_stateid *stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5421 | { |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 5422 | return test_access(NFS4_SHARE_ACCESS_READ, stp) || |
| 5423 | test_access(NFS4_SHARE_ACCESS_BOTH, stp) || |
| 5424 | test_access(NFS4_SHARE_ACCESS_WRITE, stp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5425 | } |
| 5426 | |
| 5427 | static inline int |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 5428 | access_permit_write(struct nfs4_ol_stateid *stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5429 | { |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 5430 | return test_access(NFS4_SHARE_ACCESS_WRITE, stp) || |
| 5431 | test_access(NFS4_SHARE_ACCESS_BOTH, stp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5432 | } |
| 5433 | |
| 5434 | static |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5435 | __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5436 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5437 | __be32 status = nfserr_openmode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5438 | |
J. Bruce Fields | 0292191 | 2010-07-29 15:16:59 -0400 | [diff] [blame] | 5439 | /* For lock stateid's, we test the parent open, not the lock: */ |
| 5440 | if (stp->st_openstp) |
| 5441 | stp = stp->st_openstp; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 5442 | if ((flags & WR_STATE) && !access_permit_write(stp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5443 | goto out; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 5444 | if ((flags & RD_STATE) && !access_permit_read(stp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5445 | goto out; |
| 5446 | status = nfs_ok; |
| 5447 | out: |
| 5448 | return status; |
| 5449 | } |
| 5450 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5451 | static inline __be32 |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 5452 | 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] | 5453 | { |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 5454 | if (ONE_STATEID(stateid) && (flags & RD_STATE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5455 | return nfs_ok; |
J. Bruce Fields | c87fb4a | 2015-08-06 12:47:02 -0400 | [diff] [blame] | 5456 | else if (opens_in_grace(net)) { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 5457 | /* Answer in remaining cases depends on existence of |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5458 | * conflicting state; so we must wait out the grace period. */ |
| 5459 | return nfserr_grace; |
| 5460 | } else if (flags & WR_STATE) |
| 5461 | return nfs4_share_conflict(current_fh, |
| 5462 | NFS4_SHARE_DENY_WRITE); |
| 5463 | else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */ |
| 5464 | return nfs4_share_conflict(current_fh, |
| 5465 | NFS4_SHARE_DENY_READ); |
| 5466 | } |
| 5467 | |
| 5468 | /* |
| 5469 | * Allow READ/WRITE during grace period on recovered state only for files |
| 5470 | * that are not able to provide mandatory locking. |
| 5471 | */ |
| 5472 | static inline int |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 5473 | grace_disallows_io(struct net *net, struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5474 | { |
J. Bruce Fields | c87fb4a | 2015-08-06 12:47:02 -0400 | [diff] [blame] | 5475 | return opens_in_grace(net) && mandatory_lock(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5476 | } |
| 5477 | |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 5478 | 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] | 5479 | { |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 5480 | /* |
| 5481 | * When sessions are used the stateid generation number is ignored |
| 5482 | * when it is zero. |
| 5483 | */ |
J. Bruce Fields | 28dde24 | 2011-08-22 10:07:12 -0400 | [diff] [blame] | 5484 | if (has_session && in->si_generation == 0) |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 5485 | return nfs_ok; |
| 5486 | |
| 5487 | if (in->si_generation == ref->si_generation) |
| 5488 | return nfs_ok; |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 5489 | |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 5490 | /* 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] | 5491 | if (nfsd4_stateid_generation_after(in, ref)) |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 5492 | return nfserr_bad_stateid; |
| 5493 | /* |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 5494 | * However, we could see a stateid from the past, even from a |
| 5495 | * non-buggy client. For example, if the client sends a lock |
| 5496 | * while some IO is outstanding, the lock may bump si_generation |
| 5497 | * while the IO is still in flight. The client could avoid that |
| 5498 | * situation by waiting for responses on all the IO requests, |
| 5499 | * but better performance may result in retrying IO that |
| 5500 | * receives an old_stateid error if requests are rarely |
| 5501 | * reordered in flight: |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 5502 | */ |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 5503 | return nfserr_old_stateid; |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 5504 | } |
| 5505 | |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 5506 | static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session) |
| 5507 | { |
| 5508 | __be32 ret; |
| 5509 | |
| 5510 | spin_lock(&s->sc_lock); |
| 5511 | ret = nfsd4_verify_open_stid(s); |
| 5512 | if (ret == nfs_ok) |
| 5513 | ret = check_stateid_generation(in, &s->sc_stateid, has_session); |
| 5514 | spin_unlock(&s->sc_lock); |
| 5515 | return ret; |
| 5516 | } |
| 5517 | |
Christoph Hellwig | ebe9cb3 | 2015-04-28 15:41:15 +0200 | [diff] [blame] | 5518 | static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols) |
| 5519 | { |
| 5520 | if (ols->st_stateowner->so_is_open_owner && |
| 5521 | !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED)) |
| 5522 | return nfserr_bad_stateid; |
| 5523 | return nfs_ok; |
| 5524 | } |
| 5525 | |
Chuck Lever | 7df302f | 2012-05-29 13:56:37 -0400 | [diff] [blame] | 5526 | static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid) |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 5527 | { |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 5528 | struct nfs4_stid *s; |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5529 | __be32 status = nfserr_bad_stateid; |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 5530 | |
Andrew Elble | ae254dac | 2017-11-09 13:41:10 -0500 | [diff] [blame] | 5531 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) || |
| 5532 | CLOSE_STATEID(stateid)) |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5533 | return status; |
Chuck Lever | 7df302f | 2012-05-29 13:56:37 -0400 | [diff] [blame] | 5534 | /* Client debugging aid. */ |
| 5535 | if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) { |
| 5536 | char addr_str[INET6_ADDRSTRLEN]; |
| 5537 | rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str, |
| 5538 | sizeof(addr_str)); |
| 5539 | pr_warn_ratelimited("NFSD: client %s testing state ID " |
| 5540 | "with incorrect client ID\n", addr_str); |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5541 | return status; |
Chuck Lever | 7df302f | 2012-05-29 13:56:37 -0400 | [diff] [blame] | 5542 | } |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5543 | spin_lock(&cl->cl_lock); |
| 5544 | s = find_stateid_locked(cl, stateid); |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 5545 | if (!s) |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5546 | goto out_unlock; |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 5547 | status = nfsd4_stid_check_stateid_generation(stateid, s, 1); |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 5548 | if (status) |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5549 | goto out_unlock; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 5550 | switch (s->sc_type) { |
| 5551 | case NFS4_DELEG_STID: |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5552 | status = nfs_ok; |
| 5553 | break; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 5554 | case NFS4_REVOKED_DELEG_STID: |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5555 | status = nfserr_deleg_revoked; |
| 5556 | break; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 5557 | case NFS4_OPEN_STID: |
| 5558 | case NFS4_LOCK_STID: |
Christoph Hellwig | ebe9cb3 | 2015-04-28 15:41:15 +0200 | [diff] [blame] | 5559 | status = nfsd4_check_openowner_confirmed(openlockstateid(s)); |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5560 | break; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 5561 | default: |
| 5562 | printk("unknown stateid type %x\n", s->sc_type); |
Trond Myklebust | b0fc29d | 2014-07-16 10:31:59 -0400 | [diff] [blame] | 5563 | /* Fallthrough */ |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 5564 | case NFS4_CLOSED_STID: |
Trond Myklebust | b0fc29d | 2014-07-16 10:31:59 -0400 | [diff] [blame] | 5565 | case NFS4_CLOSED_DELEG_STID: |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5566 | status = nfserr_bad_stateid; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 5567 | } |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5568 | out_unlock: |
| 5569 | spin_unlock(&cl->cl_lock); |
| 5570 | return status; |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 5571 | } |
| 5572 | |
Christoph Hellwig | cd61c52 | 2014-08-14 08:44:57 +0200 | [diff] [blame] | 5573 | __be32 |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 5574 | nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate, |
| 5575 | stateid_t *stateid, unsigned char typemask, |
| 5576 | struct nfs4_stid **s, struct nfsd_net *nn) |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5577 | { |
J. Bruce Fields | 0eb6f20 | 2013-03-12 17:36:17 -0400 | [diff] [blame] | 5578 | __be32 status; |
Andrew Elble | 95da1b3a | 2017-11-03 14:06:31 -0400 | [diff] [blame] | 5579 | bool return_revoked = false; |
| 5580 | |
| 5581 | /* |
| 5582 | * only return revoked delegations if explicitly asked. |
| 5583 | * otherwise we report revoked or bad_stateid status. |
| 5584 | */ |
| 5585 | if (typemask & NFS4_REVOKED_DELEG_STID) |
| 5586 | return_revoked = true; |
| 5587 | else if (typemask & NFS4_DELEG_STID) |
| 5588 | typemask |= NFS4_REVOKED_DELEG_STID; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5589 | |
Andrew Elble | ae254dac | 2017-11-09 13:41:10 -0500 | [diff] [blame] | 5590 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) || |
| 5591 | CLOSE_STATEID(stateid)) |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5592 | return nfserr_bad_stateid; |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 5593 | status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn, |
| 5594 | false); |
Trond Myklebust | a8a7c67 | 2014-03-29 14:43:38 -0400 | [diff] [blame] | 5595 | if (status == nfserr_stale_clientid) { |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 5596 | if (cstate->session) |
Trond Myklebust | a8a7c67 | 2014-03-29 14:43:38 -0400 | [diff] [blame] | 5597 | return nfserr_bad_stateid; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5598 | return nfserr_stale_stateid; |
Trond Myklebust | a8a7c67 | 2014-03-29 14:43:38 -0400 | [diff] [blame] | 5599 | } |
J. Bruce Fields | 0eb6f20 | 2013-03-12 17:36:17 -0400 | [diff] [blame] | 5600 | if (status) |
| 5601 | return status; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 5602 | *s = find_stateid_by_type(cstate->clp, stateid, typemask); |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5603 | if (!*s) |
| 5604 | return nfserr_bad_stateid; |
Andrew Elble | 95da1b3a | 2017-11-03 14:06:31 -0400 | [diff] [blame] | 5605 | if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) { |
| 5606 | nfs4_put_stid(*s); |
| 5607 | if (cstate->minorversion) |
| 5608 | return nfserr_deleg_revoked; |
| 5609 | return nfserr_bad_stateid; |
| 5610 | } |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5611 | return nfs_ok; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5612 | } |
| 5613 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5614 | static struct nfsd_file * |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5615 | nfs4_find_file(struct nfs4_stid *s, int flags) |
| 5616 | { |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5617 | if (!s) |
| 5618 | return NULL; |
| 5619 | |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5620 | switch (s->sc_type) { |
| 5621 | case NFS4_DELEG_STID: |
| 5622 | if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file)) |
| 5623 | return NULL; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5624 | return nfsd_file_get(s->sc_file->fi_deleg_file); |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5625 | case NFS4_OPEN_STID: |
| 5626 | case NFS4_LOCK_STID: |
| 5627 | if (flags & RD_STATE) |
| 5628 | return find_readable_file(s->sc_file); |
| 5629 | else |
| 5630 | return find_writeable_file(s->sc_file); |
| 5631 | break; |
| 5632 | } |
| 5633 | |
| 5634 | return NULL; |
| 5635 | } |
| 5636 | |
| 5637 | static __be32 |
J. Bruce Fields | d8836f7 | 2018-11-05 20:04:06 -0500 | [diff] [blame] | 5638 | nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags) |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5639 | { |
| 5640 | __be32 status; |
| 5641 | |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5642 | status = nfsd4_check_openowner_confirmed(ols); |
| 5643 | if (status) |
| 5644 | return status; |
| 5645 | return nfs4_check_openmode(ols, flags); |
| 5646 | } |
| 5647 | |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5648 | static __be32 |
| 5649 | 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] | 5650 | struct nfsd_file **nfp, int flags) |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5651 | { |
| 5652 | int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5653 | struct nfsd_file *nf; |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5654 | __be32 status; |
| 5655 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5656 | nf = nfs4_find_file(s, flags); |
| 5657 | if (nf) { |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5658 | status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry, |
| 5659 | acc | NFSD_MAY_OWNER_OVERRIDE); |
Jeff Layton | 5c4583b | 2019-08-18 14:18:54 -0400 | [diff] [blame] | 5660 | if (status) { |
| 5661 | nfsd_file_put(nf); |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5662 | goto out; |
Jeff Layton | 5c4583b | 2019-08-18 14:18:54 -0400 | [diff] [blame] | 5663 | } |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5664 | } else { |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5665 | status = nfsd_file_acquire(rqstp, fhp, acc, &nf); |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5666 | if (status) |
| 5667 | return status; |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5668 | } |
Jeff Layton | 5c4583b | 2019-08-18 14:18:54 -0400 | [diff] [blame] | 5669 | *nfp = nf; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5670 | out: |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5671 | return status; |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5672 | } |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 5673 | static void |
| 5674 | _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps) |
| 5675 | { |
| 5676 | WARN_ON_ONCE(cps->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID); |
| 5677 | if (!refcount_dec_and_test(&cps->cp_stateid.sc_count)) |
| 5678 | return; |
| 5679 | list_del(&cps->cp_list); |
| 5680 | idr_remove(&nn->s2s_cp_stateids, |
| 5681 | cps->cp_stateid.stid.si_opaque.so_id); |
| 5682 | kfree(cps); |
| 5683 | } |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 5684 | /* |
| 5685 | * A READ from an inter server to server COPY will have a |
| 5686 | * copy stateid. Look up the copy notify stateid from the |
| 5687 | * idr structure and take a reference on it. |
| 5688 | */ |
Olga Kornievskaia | ce0887a | 2019-10-09 11:50:48 -0400 | [diff] [blame] | 5689 | __be32 manage_cpntf_state(struct nfsd_net *nn, stateid_t *st, |
| 5690 | struct nfs4_client *clp, |
| 5691 | struct nfs4_cpntf_state **cps) |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 5692 | { |
| 5693 | copy_stateid_t *cps_t; |
| 5694 | struct nfs4_cpntf_state *state = NULL; |
| 5695 | |
| 5696 | if (st->si_opaque.so_clid.cl_id != nn->s2s_cp_cl_id) |
| 5697 | return nfserr_bad_stateid; |
| 5698 | spin_lock(&nn->s2s_cp_lock); |
| 5699 | cps_t = idr_find(&nn->s2s_cp_stateids, st->si_opaque.so_id); |
| 5700 | if (cps_t) { |
| 5701 | state = container_of(cps_t, struct nfs4_cpntf_state, |
| 5702 | cp_stateid); |
Dan Carpenter | 5277a79 | 2019-12-04 10:59:36 +0300 | [diff] [blame] | 5703 | if (state->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID) { |
| 5704 | state = NULL; |
| 5705 | goto unlock; |
| 5706 | } |
Olga Kornievskaia | ce0887a | 2019-10-09 11:50:48 -0400 | [diff] [blame] | 5707 | if (!clp) |
| 5708 | refcount_inc(&state->cp_stateid.sc_count); |
| 5709 | else |
| 5710 | _free_cpntf_state_locked(nn, state); |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 5711 | } |
Dan Carpenter | 5277a79 | 2019-12-04 10:59:36 +0300 | [diff] [blame] | 5712 | unlock: |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 5713 | spin_unlock(&nn->s2s_cp_lock); |
| 5714 | if (!state) |
| 5715 | return nfserr_bad_stateid; |
Olga Kornievskaia | ce0887a | 2019-10-09 11:50:48 -0400 | [diff] [blame] | 5716 | if (!clp && state) |
| 5717 | *cps = state; |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 5718 | return 0; |
| 5719 | } |
| 5720 | |
| 5721 | static __be32 find_cpntf_state(struct nfsd_net *nn, stateid_t *st, |
| 5722 | struct nfs4_stid **stid) |
| 5723 | { |
| 5724 | __be32 status; |
| 5725 | struct nfs4_cpntf_state *cps = NULL; |
| 5726 | struct nfsd4_compound_state cstate; |
| 5727 | |
Olga Kornievskaia | ce0887a | 2019-10-09 11:50:48 -0400 | [diff] [blame] | 5728 | status = manage_cpntf_state(nn, st, NULL, &cps); |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 5729 | if (status) |
| 5730 | return status; |
| 5731 | |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 5732 | cps->cpntf_time = ktime_get_boottime_seconds(); |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 5733 | memset(&cstate, 0, sizeof(cstate)); |
| 5734 | status = lookup_clientid(&cps->cp_p_clid, &cstate, nn, true); |
| 5735 | if (status) |
| 5736 | goto out; |
| 5737 | status = nfsd4_lookup_stateid(&cstate, &cps->cp_p_stateid, |
| 5738 | NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID, |
| 5739 | stid, nn); |
| 5740 | put_client_renew(cstate.clp); |
| 5741 | out: |
| 5742 | nfs4_put_cpntf_state(nn, cps); |
| 5743 | return status; |
| 5744 | } |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 5745 | |
| 5746 | void nfs4_put_cpntf_state(struct nfsd_net *nn, struct nfs4_cpntf_state *cps) |
| 5747 | { |
| 5748 | spin_lock(&nn->s2s_cp_lock); |
| 5749 | _free_cpntf_state_locked(nn, cps); |
| 5750 | spin_unlock(&nn->s2s_cp_lock); |
| 5751 | } |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5752 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5753 | /* |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5754 | * Checks for stateid operations |
| 5755 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5756 | __be32 |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5757 | nfs4_preprocess_stateid_op(struct svc_rqst *rqstp, |
Anna Schumaker | aa0d6ae | 2015-12-03 12:59:51 +0100 | [diff] [blame] | 5758 | struct nfsd4_compound_state *cstate, struct svc_fh *fhp, |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 5759 | stateid_t *stateid, int flags, struct nfsd_file **nfp, |
| 5760 | struct nfs4_stid **cstid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5761 | { |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5762 | struct inode *ino = d_inode(fhp->fh_dentry); |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5763 | struct net *net = SVC_NET(rqstp); |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5764 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5765 | struct nfs4_stid *s = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5766 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5767 | |
Jeff Layton | 5c4583b | 2019-08-18 14:18:54 -0400 | [diff] [blame] | 5768 | if (nfp) |
| 5769 | *nfp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5770 | |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 5771 | if (grace_disallows_io(net, ino)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5772 | return nfserr_grace; |
| 5773 | |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5774 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) { |
| 5775 | status = check_special_stateids(net, fhp, stateid, flags); |
| 5776 | goto done; |
| 5777 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5778 | |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 5779 | status = nfsd4_lookup_stateid(cstate, stateid, |
Jeff Layton | db24b3b | 2014-06-30 11:48:36 -0400 | [diff] [blame] | 5780 | NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID, |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 5781 | &s, nn); |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 5782 | if (status == nfserr_bad_stateid) |
| 5783 | status = find_cpntf_state(nn, stateid, &s); |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5784 | if (status) |
Trond Myklebust | c2d1d6a | 2014-07-30 08:27:25 -0400 | [diff] [blame] | 5785 | return status; |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 5786 | status = nfsd4_stid_check_stateid_generation(stateid, s, |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5787 | nfsd4_has_session(cstate)); |
J. Bruce Fields | 69064a2 | 2011-09-09 11:54:57 -0400 | [diff] [blame] | 5788 | if (status) |
| 5789 | goto out; |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5790 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 5791 | switch (s->sc_type) { |
| 5792 | case NFS4_DELEG_STID: |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5793 | status = nfs4_check_delegmode(delegstateid(s), flags); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 5794 | break; |
| 5795 | case NFS4_OPEN_STID: |
| 5796 | case NFS4_LOCK_STID: |
J. Bruce Fields | d8836f7 | 2018-11-05 20:04:06 -0500 | [diff] [blame] | 5797 | status = nfs4_check_olstateid(openlockstateid(s), flags); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 5798 | break; |
| 5799 | default: |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 5800 | status = nfserr_bad_stateid; |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5801 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5802 | } |
Jeff Layton | 8fcd461 | 2015-07-30 06:57:46 -0400 | [diff] [blame] | 5803 | if (status) |
| 5804 | goto out; |
| 5805 | status = nfs4_check_fh(fhp, s); |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5806 | |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5807 | done: |
Jeff Layton | 5c4583b | 2019-08-18 14:18:54 -0400 | [diff] [blame] | 5808 | if (status == nfs_ok && nfp) |
| 5809 | status = nfs4_check_file(rqstp, fhp, s, nfp, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5810 | out: |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame] | 5811 | if (s) { |
| 5812 | if (!status && cstid) |
| 5813 | *cstid = s; |
| 5814 | else |
| 5815 | nfs4_put_stid(s); |
| 5816 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5817 | return status; |
| 5818 | } |
| 5819 | |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 5820 | /* |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 5821 | * Test if the stateid is valid |
| 5822 | */ |
| 5823 | __be32 |
| 5824 | nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5825 | union nfsd4_op_u *u) |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 5826 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5827 | struct nfsd4_test_stateid *test_stateid = &u->test_stateid; |
Bryan Schumaker | 03cfb42 | 2012-01-27 10:22:49 -0500 | [diff] [blame] | 5828 | struct nfsd4_test_stateid_id *stateid; |
| 5829 | struct nfs4_client *cl = cstate->session->se_client; |
| 5830 | |
Bryan Schumaker | 03cfb42 | 2012-01-27 10:22:49 -0500 | [diff] [blame] | 5831 | 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] | 5832 | stateid->ts_id_status = |
| 5833 | nfsd4_validate_stateid(cl, &stateid->ts_id_stateid); |
Bryan Schumaker | 03cfb42 | 2012-01-27 10:22:49 -0500 | [diff] [blame] | 5834 | |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 5835 | return nfs_ok; |
| 5836 | } |
| 5837 | |
Chuck Lever | 4269139 | 2016-08-11 10:37:30 -0400 | [diff] [blame] | 5838 | static __be32 |
| 5839 | nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s) |
| 5840 | { |
| 5841 | struct nfs4_ol_stateid *stp = openlockstateid(s); |
| 5842 | __be32 ret; |
| 5843 | |
Trond Myklebust | 659aefb | 2017-11-03 08:00:13 -0400 | [diff] [blame] | 5844 | ret = nfsd4_lock_ol_stateid(stp); |
| 5845 | if (ret) |
| 5846 | goto out_put_stid; |
Chuck Lever | 4269139 | 2016-08-11 10:37:30 -0400 | [diff] [blame] | 5847 | |
| 5848 | ret = check_stateid_generation(stateid, &s->sc_stateid, 1); |
| 5849 | if (ret) |
| 5850 | goto out; |
| 5851 | |
| 5852 | ret = nfserr_locks_held; |
| 5853 | if (check_for_locks(stp->st_stid.sc_file, |
| 5854 | lockowner(stp->st_stateowner))) |
| 5855 | goto out; |
| 5856 | |
| 5857 | release_lock_stateid(stp); |
| 5858 | ret = nfs_ok; |
| 5859 | |
| 5860 | out: |
| 5861 | mutex_unlock(&stp->st_mutex); |
Trond Myklebust | 659aefb | 2017-11-03 08:00:13 -0400 | [diff] [blame] | 5862 | out_put_stid: |
Chuck Lever | 4269139 | 2016-08-11 10:37:30 -0400 | [diff] [blame] | 5863 | nfs4_put_stid(s); |
| 5864 | return ret; |
| 5865 | } |
| 5866 | |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 5867 | __be32 |
| 5868 | nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5869 | union nfsd4_op_u *u) |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 5870 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5871 | struct nfsd4_free_stateid *free_stateid = &u->free_stateid; |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 5872 | stateid_t *stateid = &free_stateid->fr_stateid; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 5873 | struct nfs4_stid *s; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 5874 | struct nfs4_delegation *dp; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5875 | struct nfs4_client *cl = cstate->session->se_client; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 5876 | __be32 ret = nfserr_bad_stateid; |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 5877 | |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5878 | spin_lock(&cl->cl_lock); |
| 5879 | s = find_stateid_locked(cl, stateid); |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 5880 | if (!s) |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5881 | goto out_unlock; |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 5882 | spin_lock(&s->sc_lock); |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 5883 | switch (s->sc_type) { |
| 5884 | case NFS4_DELEG_STID: |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 5885 | ret = nfserr_locks_held; |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5886 | break; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 5887 | case NFS4_OPEN_STID: |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5888 | ret = check_stateid_generation(stateid, &s->sc_stateid, 1); |
| 5889 | if (ret) |
| 5890 | break; |
| 5891 | ret = nfserr_locks_held; |
| 5892 | break; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 5893 | case NFS4_LOCK_STID: |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 5894 | spin_unlock(&s->sc_lock); |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 5895 | refcount_inc(&s->sc_count); |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5896 | spin_unlock(&cl->cl_lock); |
Chuck Lever | 4269139 | 2016-08-11 10:37:30 -0400 | [diff] [blame] | 5897 | ret = nfsd4_free_lock_stateid(stateid, s); |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5898 | goto out; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 5899 | case NFS4_REVOKED_DELEG_STID: |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 5900 | spin_unlock(&s->sc_lock); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 5901 | dp = delegstateid(s); |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 5902 | list_del_init(&dp->dl_recall_lru); |
| 5903 | spin_unlock(&cl->cl_lock); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 5904 | nfs4_put_stid(s); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 5905 | ret = nfs_ok; |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5906 | goto out; |
| 5907 | /* Default falls through and returns nfserr_bad_stateid */ |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 5908 | } |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 5909 | spin_unlock(&s->sc_lock); |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5910 | out_unlock: |
| 5911 | spin_unlock(&cl->cl_lock); |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 5912 | out: |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 5913 | return ret; |
| 5914 | } |
| 5915 | |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 5916 | static inline int |
| 5917 | setlkflg (int type) |
| 5918 | { |
| 5919 | return (type == NFS4_READW_LT || type == NFS4_READ_LT) ? |
| 5920 | RD_STATE : WR_STATE; |
| 5921 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5922 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5923 | 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] | 5924 | { |
| 5925 | struct svc_fh *current_fh = &cstate->current_fh; |
| 5926 | struct nfs4_stateowner *sop = stp->st_stateowner; |
| 5927 | __be32 status; |
| 5928 | |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 5929 | status = nfsd4_check_seqid(cstate, sop, seqid); |
| 5930 | if (status) |
| 5931 | return status; |
Trond Myklebust | 9271d7e | 2017-11-03 08:00:15 -0400 | [diff] [blame] | 5932 | status = nfsd4_lock_ol_stateid(stp); |
| 5933 | if (status != nfs_ok) |
| 5934 | return status; |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 5935 | 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] | 5936 | if (status == nfs_ok) |
| 5937 | status = nfs4_check_fh(current_fh, &stp->st_stid); |
| 5938 | if (status != nfs_ok) |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 5939 | mutex_unlock(&stp->st_mutex); |
Jeff Layton | 35a92fe | 2015-09-17 07:47:08 -0400 | [diff] [blame] | 5940 | return status; |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 5941 | } |
| 5942 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5943 | /* |
| 5944 | * Checks for sequence id mutating operations. |
| 5945 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5946 | static __be32 |
Benny Halevy | dd453df | 2009-04-03 08:28:41 +0300 | [diff] [blame] | 5947 | nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, |
J. Bruce Fields | 2288d0e | 2011-09-06 15:50:21 -0400 | [diff] [blame] | 5948 | stateid_t *stateid, char typemask, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5949 | struct nfs4_ol_stateid **stpp, |
| 5950 | struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5951 | { |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 5952 | __be32 status; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5953 | struct nfs4_stid *s; |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 5954 | struct nfs4_ol_stateid *stp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5955 | |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 5956 | dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__, |
| 5957 | seqid, STATEID_VAL(stateid)); |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 5958 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5959 | *stpp = NULL; |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 5960 | status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn); |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 5961 | if (status) |
| 5962 | return status; |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 5963 | stp = openlockstateid(s); |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 5964 | nfsd4_cstate_assign_replay(cstate, stp->st_stateowner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5965 | |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 5966 | status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp); |
Trond Myklebust | fd91101 | 2014-07-29 21:34:24 -0400 | [diff] [blame] | 5967 | if (!status) |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 5968 | *stpp = stp; |
Trond Myklebust | fd91101 | 2014-07-29 21:34:24 -0400 | [diff] [blame] | 5969 | else |
| 5970 | nfs4_put_stid(&stp->st_stid); |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 5971 | return status; |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 5972 | } |
J. Bruce Fields | 39325bd | 2007-11-26 17:06:39 -0500 | [diff] [blame] | 5973 | |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5974 | static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, |
| 5975 | 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] | 5976 | { |
| 5977 | __be32 status; |
| 5978 | struct nfs4_openowner *oo; |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 5979 | struct nfs4_ol_stateid *stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5980 | |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 5981 | status = nfs4_preprocess_seqid_op(cstate, seqid, stateid, |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 5982 | NFS4_OPEN_STID, &stp, nn); |
J. Bruce Fields | 7a8711c | 2011-09-02 09:03:37 -0400 | [diff] [blame] | 5983 | if (status) |
| 5984 | return status; |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 5985 | oo = openowner(stp->st_stateowner); |
| 5986 | if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) { |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 5987 | mutex_unlock(&stp->st_mutex); |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 5988 | nfs4_put_stid(&stp->st_stid); |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 5989 | return nfserr_bad_stateid; |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 5990 | } |
| 5991 | *stpp = stp; |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 5992 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5993 | } |
| 5994 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5995 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 5996 | nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5997 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5998 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5999 | struct nfsd4_open_confirm *oc = &u->open_confirm; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6000 | __be32 status; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6001 | struct nfs4_openowner *oo; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 6002 | struct nfs4_ol_stateid *stp; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6003 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6004 | |
Al Viro | a6a9f18 | 2013-09-16 10:57:01 -0400 | [diff] [blame] | 6005 | dprintk("NFSD: nfsd4_open_confirm on file %pd\n", |
| 6006 | cstate->current_fh.fh_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6007 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6008 | status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0); |
J. Bruce Fields | a8cddc5 | 2006-06-30 01:56:13 -0700 | [diff] [blame] | 6009 | if (status) |
| 6010 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6011 | |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 6012 | status = nfs4_preprocess_seqid_op(cstate, |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6013 | oc->oc_seqid, &oc->oc_req_stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6014 | NFS4_OPEN_STID, &stp, nn); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 6015 | if (status) |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 6016 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6017 | oo = openowner(stp->st_stateowner); |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 6018 | status = nfserr_bad_stateid; |
Jeff Layton | 35a92fe | 2015-09-17 07:47:08 -0400 | [diff] [blame] | 6019 | if (oo->oo_flags & NFS4_OO_CONFIRMED) { |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 6020 | mutex_unlock(&stp->st_mutex); |
Trond Myklebust | 2585fc7 | 2014-07-29 21:34:21 -0400 | [diff] [blame] | 6021 | goto put_stateid; |
Jeff Layton | 35a92fe | 2015-09-17 07:47:08 -0400 | [diff] [blame] | 6022 | } |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 6023 | oo->oo_flags |= NFS4_OO_CONFIRMED; |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 6024 | nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid); |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 6025 | mutex_unlock(&stp->st_mutex); |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 6026 | dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n", |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 6027 | __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid)); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 6028 | |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 6029 | nfsd4_client_record_create(oo->oo_owner.so_client); |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 6030 | status = nfs_ok; |
Trond Myklebust | 2585fc7 | 2014-07-29 21:34:21 -0400 | [diff] [blame] | 6031 | put_stateid: |
| 6032 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6033 | out: |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 6034 | nfsd4_bump_seqid(cstate, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6035 | return status; |
| 6036 | } |
| 6037 | |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 6038 | 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] | 6039 | { |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 6040 | if (!test_access(access, stp)) |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 6041 | return; |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 6042 | nfs4_file_put_access(stp->st_stid.sc_file, access); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 6043 | clear_access(access, stp); |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 6044 | } |
J. Bruce Fields | f197c27 | 2011-06-29 08:23:50 -0400 | [diff] [blame] | 6045 | |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 6046 | static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access) |
| 6047 | { |
| 6048 | switch (to_access) { |
| 6049 | case NFS4_SHARE_ACCESS_READ: |
| 6050 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE); |
| 6051 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); |
| 6052 | break; |
| 6053 | case NFS4_SHARE_ACCESS_WRITE: |
| 6054 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ); |
| 6055 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); |
| 6056 | break; |
| 6057 | case NFS4_SHARE_ACCESS_BOTH: |
| 6058 | break; |
| 6059 | default: |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 6060 | WARN_ON_ONCE(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6061 | } |
| 6062 | } |
| 6063 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6064 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6065 | nfsd4_open_downgrade(struct svc_rqst *rqstp, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6066 | struct nfsd4_compound_state *cstate, union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6067 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6068 | struct nfsd4_open_downgrade *od = &u->open_downgrade; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6069 | __be32 status; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 6070 | struct nfs4_ol_stateid *stp; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6071 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6072 | |
Al Viro | a6a9f18 | 2013-09-16 10:57:01 -0400 | [diff] [blame] | 6073 | dprintk("NFSD: nfsd4_open_downgrade on file %pd\n", |
| 6074 | cstate->current_fh.fh_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6075 | |
J. Bruce Fields | c30e92d | 2011-10-10 17:34:31 -0400 | [diff] [blame] | 6076 | /* We don't yet support WANT bits: */ |
Benny Halevy | 2c8bd7e | 2012-02-16 20:57:09 +0200 | [diff] [blame] | 6077 | if (od->od_deleg_want) |
| 6078 | dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__, |
| 6079 | od->od_deleg_want); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6080 | |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 6081 | status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6082 | &od->od_stateid, &stp, nn); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 6083 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6084 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6085 | status = nfserr_inval; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 6086 | if (!test_access(od->od_share_access, stp)) { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 6087 | 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] | 6088 | stp->st_access_bmap, od->od_share_access); |
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 | } |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 6091 | if (!test_deny(od->od_share_deny, stp)) { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 6092 | 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] | 6093 | stp->st_deny_bmap, od->od_share_deny); |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 6094 | goto put_stateid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6095 | } |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 6096 | nfs4_stateid_downgrade(stp, od->od_share_access); |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 6097 | reset_union_bmap_deny(od->od_share_deny, stp); |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 6098 | nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6099 | status = nfs_ok; |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 6100 | put_stateid: |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 6101 | mutex_unlock(&stp->st_mutex); |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 6102 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6103 | out: |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 6104 | nfsd4_bump_seqid(cstate, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6105 | return status; |
| 6106 | } |
| 6107 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 6108 | static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s) |
| 6109 | { |
Trond Myklebust | acf9295 | 2014-06-30 11:48:37 -0400 | [diff] [blame] | 6110 | struct nfs4_client *clp = s->st_stid.sc_client; |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 6111 | bool unhashed; |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 6112 | LIST_HEAD(reaplist); |
Trond Myklebust | acf9295 | 2014-06-30 11:48:37 -0400 | [diff] [blame] | 6113 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 6114 | spin_lock(&clp->cl_lock); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 6115 | unhashed = unhash_open_stateid(s, &reaplist); |
Trond Myklebust | acf9295 | 2014-06-30 11:48:37 -0400 | [diff] [blame] | 6116 | |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 6117 | if (clp->cl_minorversion) { |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 6118 | if (unhashed) |
| 6119 | put_ol_stateid_locked(s, &reaplist); |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 6120 | spin_unlock(&clp->cl_lock); |
| 6121 | free_ol_stateid_reaplist(&reaplist); |
| 6122 | } else { |
| 6123 | spin_unlock(&clp->cl_lock); |
| 6124 | free_ol_stateid_reaplist(&reaplist); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 6125 | if (unhashed) |
| 6126 | move_to_close_lru(s, clp->net); |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 6127 | } |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 6128 | } |
| 6129 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6130 | /* |
| 6131 | * nfs4_unlock_state() called after encode |
| 6132 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6133 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6134 | nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6135 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6136 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6137 | struct nfsd4_close *close = &u->close; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6138 | __be32 status; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 6139 | struct nfs4_ol_stateid *stp; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6140 | struct net *net = SVC_NET(rqstp); |
| 6141 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6142 | |
Al Viro | a6a9f18 | 2013-09-16 10:57:01 -0400 | [diff] [blame] | 6143 | dprintk("NFSD: nfsd4_close on file %pd\n", |
| 6144 | cstate->current_fh.fh_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6145 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 6146 | status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid, |
| 6147 | &close->cl_stateid, |
| 6148 | NFS4_OPEN_STID|NFS4_CLOSED_STID, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6149 | &stp, nn); |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 6150 | nfsd4_bump_seqid(cstate, status); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 6151 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6152 | goto out; |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 6153 | |
| 6154 | stp->st_stid.sc_type = NFS4_CLOSED_STID; |
Jeff Layton | bd2deca | 2018-02-15 15:16:18 -0500 | [diff] [blame] | 6155 | |
| 6156 | /* |
| 6157 | * Technically we don't _really_ have to increment or copy it, since |
| 6158 | * it should just be gone after this operation and we clobber the |
| 6159 | * copied value below, but we continue to do so here just to ensure |
| 6160 | * that racing ops see that there was a state change. |
| 6161 | */ |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 6162 | nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6163 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 6164 | nfsd4_close_open_stateid(stp); |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 6165 | mutex_unlock(&stp->st_mutex); |
Trond Myklebust | 8a0b589 | 2014-07-29 21:34:20 -0400 | [diff] [blame] | 6166 | |
Jeff Layton | bd2deca | 2018-02-15 15:16:18 -0500 | [diff] [blame] | 6167 | /* v4.1+ suggests that we send a special stateid in here, since the |
| 6168 | * clients should just ignore this anyway. Since this is not useful |
| 6169 | * for v4.0 clients either, we set it to the special close_stateid |
| 6170 | * universally. |
| 6171 | * |
| 6172 | * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5 |
| 6173 | */ |
| 6174 | memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid)); |
Trond Myklebust | fb500a7 | 2017-11-03 08:00:12 -0400 | [diff] [blame] | 6175 | |
Trond Myklebust | 8a0b589 | 2014-07-29 21:34:20 -0400 | [diff] [blame] | 6176 | /* put reference from nfs4_preprocess_seqid_op */ |
| 6177 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6178 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6179 | return status; |
| 6180 | } |
| 6181 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6182 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6183 | nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6184 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6185 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6186 | struct nfsd4_delegreturn *dr = &u->delegreturn; |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 6187 | struct nfs4_delegation *dp; |
| 6188 | stateid_t *stateid = &dr->dr_stateid; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 6189 | struct nfs4_stid *s; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6190 | __be32 status; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6191 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6192 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6193 | if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 6194 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6195 | |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 6196 | status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn); |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 6197 | if (status) |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 6198 | goto out; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 6199 | dp = delegstateid(s); |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 6200 | 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] | 6201 | if (status) |
Trond Myklebust | fd91101 | 2014-07-29 21:34:24 -0400 | [diff] [blame] | 6202 | goto put_stateid; |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 6203 | |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 6204 | destroy_delegation(dp); |
Trond Myklebust | fd91101 | 2014-07-29 21:34:24 -0400 | [diff] [blame] | 6205 | put_stateid: |
| 6206 | nfs4_put_stid(&dp->dl_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6207 | out: |
| 6208 | return status; |
| 6209 | } |
| 6210 | |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 6211 | static inline u64 |
| 6212 | end_offset(u64 start, u64 len) |
| 6213 | { |
| 6214 | u64 end; |
| 6215 | |
| 6216 | end = start + len; |
| 6217 | return end >= start ? end: NFS4_MAX_UINT64; |
| 6218 | } |
| 6219 | |
| 6220 | /* last octet in a range */ |
| 6221 | static inline u64 |
| 6222 | last_byte_offset(u64 start, u64 len) |
| 6223 | { |
| 6224 | u64 end; |
| 6225 | |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 6226 | WARN_ON_ONCE(!len); |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 6227 | end = start + len; |
| 6228 | return end > start ? end - 1: NFS4_MAX_UINT64; |
| 6229 | } |
| 6230 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6231 | /* |
| 6232 | * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that |
| 6233 | * we can't properly handle lock requests that go beyond the (2^63 - 1)-th |
| 6234 | * byte, because of sign extension problems. Since NFSv4 calls for 64-bit |
| 6235 | * locking, this prevents us from being completely protocol-compliant. The |
| 6236 | * real solution to this problem is to start using unsigned file offsets in |
| 6237 | * the VFS, but this is a very deep change! |
| 6238 | */ |
| 6239 | static inline void |
| 6240 | nfs4_transform_lock_offset(struct file_lock *lock) |
| 6241 | { |
| 6242 | if (lock->fl_start < 0) |
| 6243 | lock->fl_start = OFFSET_MAX; |
| 6244 | if (lock->fl_end < 0) |
| 6245 | lock->fl_end = OFFSET_MAX; |
| 6246 | } |
| 6247 | |
Jeff Layton | cae80b3 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 6248 | static fl_owner_t |
| 6249 | nfsd4_fl_get_owner(fl_owner_t owner) |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6250 | { |
Jeff Layton | cae80b3 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 6251 | struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner; |
| 6252 | |
| 6253 | nfs4_get_stateowner(&lo->lo_owner); |
| 6254 | return owner; |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6255 | } |
| 6256 | |
Jeff Layton | cae80b3 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 6257 | static void |
| 6258 | nfsd4_fl_put_owner(fl_owner_t owner) |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6259 | { |
Jeff Layton | cae80b3 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 6260 | struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner; |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6261 | |
Jeff Layton | cae80b3 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 6262 | if (lo) |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6263 | nfs4_put_stateowner(&lo->lo_owner); |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6264 | } |
| 6265 | |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6266 | static void |
| 6267 | nfsd4_lm_notify(struct file_lock *fl) |
| 6268 | { |
| 6269 | struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner; |
| 6270 | struct net *net = lo->lo_owner.so_client->net; |
| 6271 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 6272 | struct nfsd4_blocked_lock *nbl = container_of(fl, |
| 6273 | struct nfsd4_blocked_lock, nbl_lock); |
| 6274 | bool queue = false; |
| 6275 | |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 6276 | /* 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] | 6277 | spin_lock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6278 | if (!list_empty(&nbl->nbl_list)) { |
| 6279 | list_del_init(&nbl->nbl_list); |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 6280 | list_del_init(&nbl->nbl_lru); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6281 | queue = true; |
| 6282 | } |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 6283 | spin_unlock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6284 | |
| 6285 | if (queue) |
| 6286 | nfsd4_run_cb(&nbl->nbl_cb); |
| 6287 | } |
| 6288 | |
Alexey Dobriyan | 7b02196 | 2009-09-21 17:01:12 -0700 | [diff] [blame] | 6289 | static const struct lock_manager_operations nfsd_posix_mng_ops = { |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6290 | .lm_notify = nfsd4_lm_notify, |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6291 | .lm_get_owner = nfsd4_fl_get_owner, |
| 6292 | .lm_put_owner = nfsd4_fl_put_owner, |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 6293 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6294 | |
| 6295 | static inline void |
| 6296 | nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny) |
| 6297 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6298 | struct nfs4_lockowner *lo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6299 | |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 6300 | if (fl->fl_lmops == &nfsd_posix_mng_ops) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6301 | lo = (struct nfs4_lockowner *) fl->fl_owner; |
J. Bruce Fields | 6f4859b | 2019-06-19 14:30:33 -0400 | [diff] [blame] | 6302 | xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner, |
| 6303 | GFP_KERNEL); |
J. Bruce Fields | 7c13f34 | 2011-08-30 22:15:47 -0400 | [diff] [blame] | 6304 | if (!deny->ld_owner.data) |
| 6305 | /* We just don't care that much */ |
| 6306 | goto nevermind; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6307 | deny->ld_clientid = lo->lo_owner.so_client->cl_clientid; |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 6308 | } else { |
J. Bruce Fields | 7c13f34 | 2011-08-30 22:15:47 -0400 | [diff] [blame] | 6309 | nevermind: |
| 6310 | deny->ld_owner.len = 0; |
| 6311 | deny->ld_owner.data = NULL; |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 6312 | deny->ld_clientid.cl_boot = 0; |
| 6313 | deny->ld_clientid.cl_id = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6314 | } |
| 6315 | deny->ld_start = fl->fl_start; |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 6316 | deny->ld_length = NFS4_MAX_UINT64; |
| 6317 | if (fl->fl_end != NFS4_MAX_UINT64) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6318 | deny->ld_length = fl->fl_end - fl->fl_start + 1; |
| 6319 | deny->ld_type = NFS4_READ_LT; |
| 6320 | if (fl->fl_type != F_RDLCK) |
| 6321 | deny->ld_type = NFS4_WRITE_LT; |
| 6322 | } |
| 6323 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6324 | static struct nfs4_lockowner * |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 6325 | find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6326 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6327 | unsigned int strhashval = ownerstr_hashval(owner); |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame] | 6328 | struct nfs4_stateowner *so; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6329 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 6330 | lockdep_assert_held(&clp->cl_lock); |
| 6331 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6332 | list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval], |
| 6333 | so_strhash) { |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame] | 6334 | if (so->so_is_open_owner) |
| 6335 | continue; |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 6336 | if (same_owner_str(so, owner)) |
| 6337 | return lockowner(nfs4_get_stateowner(so)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6338 | } |
| 6339 | return NULL; |
| 6340 | } |
| 6341 | |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6342 | static struct nfs4_lockowner * |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 6343 | find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner) |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6344 | { |
| 6345 | struct nfs4_lockowner *lo; |
| 6346 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6347 | spin_lock(&clp->cl_lock); |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 6348 | lo = find_lockowner_str_locked(clp, owner); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6349 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6350 | return lo; |
| 6351 | } |
| 6352 | |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 6353 | static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop) |
| 6354 | { |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6355 | unhash_lockowner_locked(lockowner(sop)); |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 6356 | } |
| 6357 | |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 6358 | static void nfs4_free_lockowner(struct nfs4_stateowner *sop) |
| 6359 | { |
| 6360 | struct nfs4_lockowner *lo = lockowner(sop); |
| 6361 | |
| 6362 | kmem_cache_free(lockowner_slab, lo); |
| 6363 | } |
| 6364 | |
| 6365 | static const struct nfs4_stateowner_operations lockowner_ops = { |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 6366 | .so_unhash = nfs4_unhash_lockowner, |
| 6367 | .so_free = nfs4_free_lockowner, |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 6368 | }; |
| 6369 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6370 | /* |
| 6371 | * Alloc a lock owner structure. |
| 6372 | * 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] | 6373 | * occurred. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6374 | * |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 6375 | * strhashval = ownerstr_hashval |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6376 | */ |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6377 | static struct nfs4_lockowner * |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6378 | alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, |
| 6379 | struct nfs4_ol_stateid *open_stp, |
| 6380 | struct nfsd4_lock *lock) |
| 6381 | { |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6382 | struct nfs4_lockowner *lo, *ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6383 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6384 | lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp); |
| 6385 | if (!lo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6386 | return NULL; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6387 | INIT_LIST_HEAD(&lo->lo_blocked); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6388 | INIT_LIST_HEAD(&lo->lo_owner.so_stateids); |
| 6389 | lo->lo_owner.so_is_open_owner = 0; |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6390 | lo->lo_owner.so_seqid = lock->lk_new_lock_seqid; |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 6391 | lo->lo_owner.so_ops = &lockowner_ops; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6392 | spin_lock(&clp->cl_lock); |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 6393 | ret = find_lockowner_str_locked(clp, &lock->lk_new_owner); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6394 | if (ret == NULL) { |
| 6395 | list_add(&lo->lo_owner.so_strhash, |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6396 | &clp->cl_ownerstr_hashtbl[strhashval]); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6397 | ret = lo; |
| 6398 | } else |
Kinglong Mee | d50ffde | 2015-07-16 12:05:07 +0800 | [diff] [blame] | 6399 | nfs4_free_stateowner(&lo->lo_owner); |
| 6400 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6401 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 340f0ba | 2015-03-23 11:02:30 -0400 | [diff] [blame] | 6402 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6403 | } |
| 6404 | |
Trond Myklebust | fd1fd68 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6405 | static struct nfs4_ol_stateid * |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 6406 | find_lock_stateid(const struct nfs4_lockowner *lo, |
| 6407 | const struct nfs4_ol_stateid *ost) |
Trond Myklebust | fd1fd68 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6408 | { |
| 6409 | struct nfs4_ol_stateid *lst; |
Trond Myklebust | fd1fd68 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6410 | |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 6411 | lockdep_assert_held(&ost->st_stid.sc_client->cl_lock); |
Trond Myklebust | fd1fd68 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6412 | |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 6413 | /* If ost is not hashed, ost->st_locks will not be valid */ |
| 6414 | if (!nfs4_ol_stateid_unhashed(ost)) |
| 6415 | list_for_each_entry(lst, &ost->st_locks, st_locks) { |
| 6416 | if (lst->st_stateowner == &lo->lo_owner) { |
| 6417 | refcount_inc(&lst->st_stid.sc_count); |
| 6418 | return lst; |
| 6419 | } |
Trond Myklebust | fd1fd68 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6420 | } |
Trond Myklebust | fd1fd68 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6421 | return NULL; |
| 6422 | } |
| 6423 | |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6424 | static struct nfs4_ol_stateid * |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 6425 | init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo, |
| 6426 | struct nfs4_file *fp, struct inode *inode, |
| 6427 | struct nfs4_ol_stateid *open_stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6428 | { |
J. Bruce Fields | d3b313a | 2011-09-15 15:02:41 -0400 | [diff] [blame] | 6429 | struct nfs4_client *clp = lo->lo_owner.so_client; |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6430 | struct nfs4_ol_stateid *retstp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6431 | |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6432 | mutex_init(&stp->st_mutex); |
Andrew Elble | 4f34bd0 | 2017-11-08 17:29:51 -0500 | [diff] [blame] | 6433 | mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX); |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6434 | retry: |
| 6435 | spin_lock(&clp->cl_lock); |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 6436 | if (nfs4_ol_stateid_unhashed(open_stp)) |
| 6437 | goto out_close; |
| 6438 | retstp = find_lock_stateid(lo, open_stp); |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6439 | if (retstp) |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 6440 | goto out_found; |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 6441 | refcount_inc(&stp->st_stid.sc_count); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 6442 | stp->st_stid.sc_type = NFS4_LOCK_STID; |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 6443 | stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 6444 | get_nfs4_file(fp); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 6445 | stp->st_stid.sc_file = fp; |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 6446 | stp->st_access_bmap = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6447 | stp->st_deny_bmap = open_stp->st_deny_bmap; |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 6448 | stp->st_openstp = open_stp; |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 6449 | spin_lock(&fp->fi_lock); |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 6450 | list_add(&stp->st_locks, &open_stp->st_locks); |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 6451 | list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 6452 | list_add(&stp->st_perfile, &fp->fi_stateids); |
| 6453 | spin_unlock(&fp->fi_lock); |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6454 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6455 | return stp; |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 6456 | out_found: |
| 6457 | spin_unlock(&clp->cl_lock); |
| 6458 | if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) { |
| 6459 | nfs4_put_stid(&retstp->st_stid); |
| 6460 | goto retry; |
| 6461 | } |
| 6462 | /* To keep mutex tracking happy */ |
| 6463 | mutex_unlock(&stp->st_mutex); |
| 6464 | return retstp; |
| 6465 | out_close: |
| 6466 | spin_unlock(&clp->cl_lock); |
| 6467 | mutex_unlock(&stp->st_mutex); |
| 6468 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6469 | } |
| 6470 | |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 6471 | static struct nfs4_ol_stateid * |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 6472 | find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi, |
| 6473 | struct inode *inode, struct nfs4_ol_stateid *ost, |
| 6474 | bool *new) |
| 6475 | { |
| 6476 | struct nfs4_stid *ns = NULL; |
| 6477 | struct nfs4_ol_stateid *lst; |
| 6478 | struct nfs4_openowner *oo = openowner(ost->st_stateowner); |
| 6479 | struct nfs4_client *clp = oo->oo_owner.so_client; |
| 6480 | |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6481 | *new = false; |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 6482 | spin_lock(&clp->cl_lock); |
Trond Myklebust | a451b12 | 2020-03-01 18:21:38 -0500 | [diff] [blame] | 6483 | lst = find_lock_stateid(lo, ost); |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 6484 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6485 | if (lst != NULL) { |
| 6486 | if (nfsd4_lock_ol_stateid(lst) == nfs_ok) |
| 6487 | goto out; |
| 6488 | nfs4_put_stid(&lst->st_stid); |
| 6489 | } |
| 6490 | ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid); |
| 6491 | if (ns == NULL) |
| 6492 | return NULL; |
| 6493 | |
| 6494 | lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost); |
| 6495 | if (lst == openlockstateid(ns)) |
| 6496 | *new = true; |
| 6497 | else |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 6498 | nfs4_put_stid(ns); |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6499 | out: |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 6500 | return lst; |
| 6501 | } |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 6502 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 6503 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6504 | check_lock_length(u64 offset, u64 length) |
| 6505 | { |
Kinglong Mee | e796931 | 2015-07-13 17:34:19 +0800 | [diff] [blame] | 6506 | return ((length == 0) || ((length != NFS4_MAX_UINT64) && |
| 6507 | (length > ~offset))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6508 | } |
| 6509 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 6510 | 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] | 6511 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 6512 | struct nfs4_file *fp = lock_stp->st_stid.sc_file; |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 6513 | |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 6514 | lockdep_assert_held(&fp->fi_lock); |
| 6515 | |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 6516 | if (test_access(access, lock_stp)) |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 6517 | return; |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 6518 | __nfs4_file_get_access(fp, access); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 6519 | set_access(access, lock_stp); |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 6520 | } |
| 6521 | |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 6522 | static __be32 |
| 6523 | lookup_or_create_lock_state(struct nfsd4_compound_state *cstate, |
| 6524 | struct nfs4_ol_stateid *ost, |
| 6525 | struct nfsd4_lock *lock, |
Jeff Layton | dd25793 | 2016-08-11 10:37:39 -0400 | [diff] [blame] | 6526 | struct nfs4_ol_stateid **plst, bool *new) |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6527 | { |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6528 | __be32 status; |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 6529 | struct nfs4_file *fi = ost->st_stid.sc_file; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6530 | struct nfs4_openowner *oo = openowner(ost->st_stateowner); |
| 6531 | struct nfs4_client *cl = oo->oo_owner.so_client; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 6532 | struct inode *inode = d_inode(cstate->current_fh.fh_dentry); |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6533 | struct nfs4_lockowner *lo; |
Jeff Layton | dd25793 | 2016-08-11 10:37:39 -0400 | [diff] [blame] | 6534 | struct nfs4_ol_stateid *lst; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6535 | unsigned int strhashval; |
| 6536 | |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 6537 | lo = find_lockowner_str(cl, &lock->lk_new_owner); |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 6538 | if (!lo) { |
Kinglong Mee | 76f6c9e | 2015-07-13 17:35:10 +0800 | [diff] [blame] | 6539 | strhashval = ownerstr_hashval(&lock->lk_new_owner); |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 6540 | lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock); |
| 6541 | if (lo == NULL) |
| 6542 | return nfserr_jukebox; |
| 6543 | } else { |
| 6544 | /* with an existing lockowner, seqids must be the same */ |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6545 | status = nfserr_bad_seqid; |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 6546 | if (!cstate->minorversion && |
| 6547 | lock->lk_new_lock_seqid != lo->lo_owner.so_seqid) |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6548 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6549 | } |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 6550 | |
Jeff Layton | dd25793 | 2016-08-11 10:37:39 -0400 | [diff] [blame] | 6551 | lst = find_or_create_lock_stateid(lo, fi, inode, ost, new); |
| 6552 | if (lst == NULL) { |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6553 | status = nfserr_jukebox; |
| 6554 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6555 | } |
Jeff Layton | dd25793 | 2016-08-11 10:37:39 -0400 | [diff] [blame] | 6556 | |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6557 | status = nfs_ok; |
Jeff Layton | dd25793 | 2016-08-11 10:37:39 -0400 | [diff] [blame] | 6558 | *plst = lst; |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6559 | out: |
| 6560 | nfs4_put_stateowner(&lo->lo_owner); |
| 6561 | return status; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6562 | } |
| 6563 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6564 | /* |
| 6565 | * LOCK operation |
| 6566 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6567 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6568 | nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6569 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6570 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6571 | struct nfsd4_lock *lock = &u->lock; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6572 | struct nfs4_openowner *open_sop = NULL; |
| 6573 | struct nfs4_lockowner *lock_sop = NULL; |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 6574 | struct nfs4_ol_stateid *lock_stp = NULL; |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 6575 | struct nfs4_ol_stateid *open_stp = NULL; |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 6576 | struct nfs4_file *fp; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6577 | struct nfsd_file *nf = NULL; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6578 | struct nfsd4_blocked_lock *nbl = NULL; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6579 | struct file_lock *file_lock = NULL; |
| 6580 | struct file_lock *conflock = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6581 | __be32 status = 0; |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 6582 | int lkflg; |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 6583 | int err; |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6584 | bool new = false; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6585 | unsigned char fl_type; |
| 6586 | unsigned int fl_flags = FL_POSIX; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6587 | struct net *net = SVC_NET(rqstp); |
| 6588 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6589 | |
| 6590 | dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n", |
| 6591 | (long long) lock->lk_offset, |
| 6592 | (long long) lock->lk_length); |
| 6593 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6594 | if (check_lock_length(lock->lk_offset, lock->lk_length)) |
| 6595 | return nfserr_inval; |
| 6596 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6597 | if ((status = fh_verify(rqstp, &cstate->current_fh, |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 6598 | S_IFREG, NFSD_MAY_LOCK))) { |
Andy Adamson | a6f6ef2 | 2006-01-18 17:43:17 -0800 | [diff] [blame] | 6599 | dprintk("NFSD: nfsd4_lock: permission denied!\n"); |
| 6600 | return status; |
| 6601 | } |
| 6602 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6603 | if (lock->lk_is_new) { |
J. Bruce Fields | 684e563 | 2011-11-04 17:08:10 -0400 | [diff] [blame] | 6604 | if (nfsd4_has_session(cstate)) |
| 6605 | /* See rfc 5661 18.10.3: given clientid is ignored: */ |
Kinglong Mee | 76f6c9e | 2015-07-13 17:35:10 +0800 | [diff] [blame] | 6606 | memcpy(&lock->lk_new_clientid, |
J. Bruce Fields | 684e563 | 2011-11-04 17:08:10 -0400 | [diff] [blame] | 6607 | &cstate->session->se_client->cl_clientid, |
| 6608 | sizeof(clientid_t)); |
| 6609 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6610 | status = nfserr_stale_clientid; |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 6611 | if (STALE_CLIENTID(&lock->lk_new_clientid, nn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6612 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6613 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6614 | /* validate and update open stateid and open seqid */ |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 6615 | status = nfs4_preprocess_confirmed_seqid_op(cstate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6616 | lock->lk_new_open_seqid, |
| 6617 | &lock->lk_new_open_stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6618 | &open_stp, nn); |
NeilBrown | 3751517 | 2005-07-07 17:59:16 -0700 | [diff] [blame] | 6619 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6620 | goto out; |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 6621 | mutex_unlock(&open_stp->st_mutex); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6622 | open_sop = openowner(open_stp->st_stateowner); |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 6623 | status = nfserr_bad_stateid; |
J. Bruce Fields | 684e563 | 2011-11-04 17:08:10 -0400 | [diff] [blame] | 6624 | if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid, |
Kinglong Mee | 76f6c9e | 2015-07-13 17:35:10 +0800 | [diff] [blame] | 6625 | &lock->lk_new_clientid)) |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 6626 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6627 | status = lookup_or_create_lock_state(cstate, open_stp, lock, |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6628 | &lock_stp, &new); |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 6629 | } else { |
Benny Halevy | dd453df | 2009-04-03 08:28:41 +0300 | [diff] [blame] | 6630 | status = nfs4_preprocess_seqid_op(cstate, |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6631 | lock->lk_old_lock_seqid, |
| 6632 | &lock->lk_old_lock_stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6633 | NFS4_LOCK_STID, &lock_stp, nn); |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 6634 | } |
J. Bruce Fields | e1aaa89 | 2012-06-06 16:01:37 -0400 | [diff] [blame] | 6635 | if (status) |
| 6636 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6637 | lock_sop = lockowner(lock_stp->st_stateowner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6638 | |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 6639 | lkflg = setlkflg(lock->lk_type); |
| 6640 | status = nfs4_check_openmode(lock_stp, lkflg); |
| 6641 | if (status) |
| 6642 | goto out; |
| 6643 | |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 6644 | status = nfserr_grace; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6645 | if (locks_in_grace(net) && !lock->lk_reclaim) |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 6646 | goto out; |
| 6647 | status = nfserr_no_grace; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6648 | if (!locks_in_grace(net) && lock->lk_reclaim) |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 6649 | goto out; |
| 6650 | |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 6651 | fp = lock_stp->st_stid.sc_file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6652 | switch (lock->lk_type) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6653 | case NFS4_READW_LT: |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6654 | if (nfsd4_has_session(cstate)) |
| 6655 | fl_flags |= FL_SLEEP; |
| 6656 | /* Fallthrough */ |
| 6657 | case NFS4_READ_LT: |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 6658 | spin_lock(&fp->fi_lock); |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6659 | nf = find_readable_file_locked(fp); |
| 6660 | if (nf) |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 6661 | get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ); |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 6662 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6663 | fl_type = F_RDLCK; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 6664 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6665 | case NFS4_WRITEW_LT: |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6666 | if (nfsd4_has_session(cstate)) |
| 6667 | fl_flags |= FL_SLEEP; |
| 6668 | /* Fallthrough */ |
| 6669 | case NFS4_WRITE_LT: |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 6670 | spin_lock(&fp->fi_lock); |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6671 | nf = find_writeable_file_locked(fp); |
| 6672 | if (nf) |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 6673 | get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE); |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 6674 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6675 | fl_type = F_WRLCK; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 6676 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6677 | default: |
| 6678 | status = nfserr_inval; |
| 6679 | goto out; |
| 6680 | } |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6681 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6682 | if (!nf) { |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 6683 | status = nfserr_openmode; |
| 6684 | goto out; |
| 6685 | } |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6686 | |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6687 | nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn); |
| 6688 | if (!nbl) { |
| 6689 | dprintk("NFSD: %s: unable to allocate block!\n", __func__); |
| 6690 | status = nfserr_jukebox; |
| 6691 | goto out; |
| 6692 | } |
| 6693 | |
| 6694 | file_lock = &nbl->nbl_lock; |
| 6695 | file_lock->fl_type = fl_type; |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6696 | 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] | 6697 | file_lock->fl_pid = current->tgid; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6698 | file_lock->fl_file = nf->nf_file; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6699 | file_lock->fl_flags = fl_flags; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6700 | file_lock->fl_lmops = &nfsd_posix_mng_ops; |
| 6701 | file_lock->fl_start = lock->lk_offset; |
| 6702 | file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length); |
| 6703 | nfs4_transform_lock_offset(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6704 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6705 | conflock = locks_alloc_lock(); |
| 6706 | if (!conflock) { |
| 6707 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 6708 | status = nfserr_jukebox; |
| 6709 | goto out; |
| 6710 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6711 | |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6712 | if (fl_flags & FL_SLEEP) { |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 6713 | nbl->nbl_time = ktime_get_boottime_seconds(); |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 6714 | spin_lock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6715 | list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked); |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 6716 | list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru); |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 6717 | spin_unlock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6718 | } |
| 6719 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6720 | err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6721 | switch (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6722 | case 0: /* success! */ |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 6723 | 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] | 6724 | status = 0; |
J. Bruce Fields | 03f318c | 2018-06-08 12:28:47 -0400 | [diff] [blame] | 6725 | if (lock->lk_reclaim) |
| 6726 | nn->somebody_reclaimed = true; |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 6727 | break; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6728 | case FILE_LOCK_DEFERRED: |
| 6729 | nbl = NULL; |
| 6730 | /* Fallthrough */ |
| 6731 | case -EAGAIN: /* conflock holds conflicting lock */ |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 6732 | status = nfserr_denied; |
| 6733 | dprintk("NFSD: nfsd4_lock: conflicting lock found!\n"); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6734 | nfs4_set_lock_denied(conflock, &lock->lk_denied); |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 6735 | break; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6736 | case -EDEADLK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6737 | status = nfserr_deadlock; |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 6738 | break; |
J. Bruce Fields | 3e77246 | 2011-08-10 19:07:33 -0400 | [diff] [blame] | 6739 | default: |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 6740 | 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] | 6741 | status = nfserrno(err); |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 6742 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6743 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6744 | out: |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6745 | if (nbl) { |
| 6746 | /* dequeue it if we queued it before */ |
| 6747 | if (fl_flags & FL_SLEEP) { |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 6748 | spin_lock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6749 | list_del_init(&nbl->nbl_list); |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 6750 | list_del_init(&nbl->nbl_lru); |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 6751 | spin_unlock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6752 | } |
| 6753 | free_blocked_lock(nbl); |
| 6754 | } |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6755 | if (nf) |
| 6756 | nfsd_file_put(nf); |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6757 | if (lock_stp) { |
| 6758 | /* Bump seqid manually if the 4.0 replay owner is openowner */ |
| 6759 | if (cstate->replay_owner && |
| 6760 | cstate->replay_owner != &lock_sop->lo_owner && |
| 6761 | seqid_mutating_err(ntohl(status))) |
| 6762 | lock_sop->lo_owner.so_seqid++; |
| 6763 | |
| 6764 | /* |
| 6765 | * If this is a new, never-before-used stateid, and we are |
| 6766 | * returning an error, then just go ahead and release it. |
| 6767 | */ |
J. Bruce Fields | 2502072 | 2018-01-17 16:25:59 -0500 | [diff] [blame] | 6768 | if (status && new) |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6769 | release_lock_stateid(lock_stp); |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6770 | |
| 6771 | mutex_unlock(&lock_stp->st_mutex); |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6772 | |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 6773 | nfs4_put_stid(&lock_stp->st_stid); |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6774 | } |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 6775 | if (open_stp) |
| 6776 | nfs4_put_stid(&open_stp->st_stid); |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 6777 | nfsd4_bump_seqid(cstate, status); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6778 | if (conflock) |
| 6779 | locks_free_lock(conflock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6780 | return status; |
| 6781 | } |
| 6782 | |
| 6783 | /* |
J. Bruce Fields | 55ef127 | 2008-12-20 11:58:38 -0800 | [diff] [blame] | 6784 | * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN, |
| 6785 | * so we do a temporary open here just to get an open file to pass to |
| 6786 | * vfs_test_lock. (Arguably perhaps test_lock should be done with an |
| 6787 | * inode operation.) |
| 6788 | */ |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 6789 | 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] | 6790 | { |
Jeff Layton | 6b556ca | 2019-08-18 14:18:55 -0400 | [diff] [blame] | 6791 | struct nfsd_file *nf; |
| 6792 | __be32 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf); |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 6793 | if (!err) { |
Jeff Layton | 6b556ca | 2019-08-18 14:18:55 -0400 | [diff] [blame] | 6794 | err = nfserrno(vfs_test_lock(nf->nf_file, lock)); |
| 6795 | nfsd_file_put(nf); |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 6796 | } |
J. Bruce Fields | 55ef127 | 2008-12-20 11:58:38 -0800 | [diff] [blame] | 6797 | return err; |
| 6798 | } |
| 6799 | |
| 6800 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6801 | * LOCKT operation |
| 6802 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6803 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6804 | nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6805 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6806 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6807 | struct nfsd4_lockt *lockt = &u->lockt; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6808 | struct file_lock *file_lock = NULL; |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6809 | struct nfs4_lockowner *lo = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6810 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 6811 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6812 | |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 6813 | if (locks_in_grace(SVC_NET(rqstp))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6814 | return nfserr_grace; |
| 6815 | |
| 6816 | if (check_lock_length(lockt->lt_offset, lockt->lt_length)) |
| 6817 | return nfserr_inval; |
| 6818 | |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 6819 | if (!nfsd4_has_session(cstate)) { |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 6820 | status = lookup_clientid(&lockt->lt_clientid, cstate, nn, |
| 6821 | false); |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 6822 | if (status) |
| 6823 | goto out; |
| 6824 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6825 | |
J. Bruce Fields | 75c096f | 2011-08-15 18:39:32 -0400 | [diff] [blame] | 6826 | if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6827 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6828 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6829 | file_lock = locks_alloc_lock(); |
| 6830 | if (!file_lock) { |
| 6831 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 6832 | status = nfserr_jukebox; |
| 6833 | goto out; |
| 6834 | } |
Kinglong Mee | 6cd9066 | 2014-08-15 08:02:55 +0800 | [diff] [blame] | 6835 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6836 | switch (lockt->lt_type) { |
| 6837 | case NFS4_READ_LT: |
| 6838 | case NFS4_READW_LT: |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6839 | file_lock->fl_type = F_RDLCK; |
Colin Ian King | f50c9d7 | 2018-11-03 16:12:53 +0000 | [diff] [blame] | 6840 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6841 | case NFS4_WRITE_LT: |
| 6842 | case NFS4_WRITEW_LT: |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6843 | file_lock->fl_type = F_WRLCK; |
Colin Ian King | f50c9d7 | 2018-11-03 16:12:53 +0000 | [diff] [blame] | 6844 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6845 | default: |
J. Bruce Fields | 2fdada0 | 2007-07-27 16:10:37 -0400 | [diff] [blame] | 6846 | dprintk("NFSD: nfs4_lockt: bad lock type!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6847 | status = nfserr_inval; |
Colin Ian King | f50c9d7 | 2018-11-03 16:12:53 +0000 | [diff] [blame] | 6848 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6849 | } |
| 6850 | |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 6851 | lo = find_lockowner_str(cstate->clp, &lockt->lt_owner); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6852 | if (lo) |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6853 | file_lock->fl_owner = (fl_owner_t)lo; |
| 6854 | file_lock->fl_pid = current->tgid; |
| 6855 | file_lock->fl_flags = FL_POSIX; |
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 | file_lock->fl_start = lockt->lt_offset; |
| 6858 | 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] | 6859 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6860 | nfs4_transform_lock_offset(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6861 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6862 | status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock); |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 6863 | if (status) |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 6864 | goto out; |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 6865 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6866 | if (file_lock->fl_type != F_UNLCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6867 | status = nfserr_denied; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6868 | nfs4_set_lock_denied(file_lock, &lockt->lt_denied); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6869 | } |
| 6870 | out: |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6871 | if (lo) |
| 6872 | nfs4_put_stateowner(&lo->lo_owner); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6873 | if (file_lock) |
| 6874 | locks_free_lock(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6875 | return status; |
| 6876 | } |
| 6877 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6878 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6879 | nfsd4_locku(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6880 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6881 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6882 | struct nfsd4_locku *locku = &u->locku; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 6883 | struct nfs4_ol_stateid *stp; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6884 | struct nfsd_file *nf = NULL; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6885 | struct file_lock *file_lock = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6886 | __be32 status; |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 6887 | int err; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6888 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
| 6889 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6890 | dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n", |
| 6891 | (long long) locku->lu_offset, |
| 6892 | (long long) locku->lu_length); |
| 6893 | |
| 6894 | if (check_lock_length(locku->lu_offset, locku->lu_length)) |
| 6895 | return nfserr_inval; |
| 6896 | |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 6897 | status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6898 | &locku->lu_stateid, NFS4_LOCK_STID, |
| 6899 | &stp, nn); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 6900 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6901 | goto out; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6902 | nf = find_any_file(stp->st_stid.sc_file); |
| 6903 | if (!nf) { |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 6904 | status = nfserr_lock_range; |
Trond Myklebust | 858cc57 | 2014-07-29 21:34:16 -0400 | [diff] [blame] | 6905 | goto put_stateid; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 6906 | } |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6907 | file_lock = locks_alloc_lock(); |
| 6908 | if (!file_lock) { |
| 6909 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 6910 | status = nfserr_jukebox; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6911 | goto put_file; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6912 | } |
Kinglong Mee | 6cd9066 | 2014-08-15 08:02:55 +0800 | [diff] [blame] | 6913 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6914 | file_lock->fl_type = F_UNLCK; |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6915 | 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] | 6916 | file_lock->fl_pid = current->tgid; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6917 | file_lock->fl_file = nf->nf_file; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6918 | file_lock->fl_flags = FL_POSIX; |
| 6919 | file_lock->fl_lmops = &nfsd_posix_mng_ops; |
| 6920 | file_lock->fl_start = locku->lu_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6921 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6922 | file_lock->fl_end = last_byte_offset(locku->lu_offset, |
| 6923 | locku->lu_length); |
| 6924 | nfs4_transform_lock_offset(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6925 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6926 | err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL); |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 6927 | if (err) { |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 6928 | dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6929 | goto out_nfserr; |
| 6930 | } |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 6931 | nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid); |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6932 | put_file: |
| 6933 | nfsd_file_put(nf); |
Trond Myklebust | 858cc57 | 2014-07-29 21:34:16 -0400 | [diff] [blame] | 6934 | put_stateid: |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 6935 | mutex_unlock(&stp->st_mutex); |
Trond Myklebust | 858cc57 | 2014-07-29 21:34:16 -0400 | [diff] [blame] | 6936 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6937 | out: |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 6938 | nfsd4_bump_seqid(cstate, status); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6939 | if (file_lock) |
| 6940 | locks_free_lock(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6941 | return status; |
| 6942 | |
| 6943 | out_nfserr: |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 6944 | status = nfserrno(err); |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6945 | goto put_file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6946 | } |
| 6947 | |
| 6948 | /* |
| 6949 | * returns |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 6950 | * true: locks held by lockowner |
| 6951 | * false: no locks held by lockowner |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6952 | */ |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 6953 | static bool |
| 6954 | check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6955 | { |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 6956 | struct file_lock *fl; |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 6957 | int status = false; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6958 | struct nfsd_file *nf = find_any_file(fp); |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 6959 | struct inode *inode; |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 6960 | struct file_lock_context *flctx; |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 6961 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6962 | if (!nf) { |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 6963 | /* Any valid lock stateid should have some sort of access */ |
| 6964 | WARN_ON_ONCE(1); |
| 6965 | return status; |
| 6966 | } |
| 6967 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6968 | inode = locks_inode(nf->nf_file); |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 6969 | flctx = inode->i_flctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6970 | |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 6971 | if (flctx && !list_empty_careful(&flctx->flc_posix)) { |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 6972 | spin_lock(&flctx->flc_lock); |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 6973 | list_for_each_entry(fl, &flctx->flc_posix, fl_list) { |
| 6974 | if (fl->fl_owner == (fl_owner_t)lowner) { |
| 6975 | status = true; |
| 6976 | break; |
| 6977 | } |
J. Bruce Fields | 796dadf | 2006-01-18 17:43:22 -0800 | [diff] [blame] | 6978 | } |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 6979 | spin_unlock(&flctx->flc_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6980 | } |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6981 | nfsd_file_put(nf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6982 | return status; |
| 6983 | } |
| 6984 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6985 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 6986 | nfsd4_release_lockowner(struct svc_rqst *rqstp, |
| 6987 | struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6988 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6989 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6990 | struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6991 | clientid_t *clid = &rlockowner->rl_clientid; |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 6992 | struct nfs4_stateowner *sop; |
| 6993 | struct nfs4_lockowner *lo = NULL; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 6994 | struct nfs4_ol_stateid *stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6995 | struct xdr_netobj *owner = &rlockowner->rl_owner; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6996 | unsigned int hashval = ownerstr_hashval(owner); |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6997 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 6998 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6999 | struct nfs4_client *clp; |
Chuck Lever | 8858481 | 2016-07-13 16:40:14 -0400 | [diff] [blame] | 7000 | LIST_HEAD (reaplist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7001 | |
| 7002 | dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n", |
| 7003 | clid->cl_boot, clid->cl_id); |
| 7004 | |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 7005 | status = lookup_clientid(clid, cstate, nn, false); |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 7006 | if (status) |
Trond Myklebust | 51f5e78 | 2014-07-30 08:27:27 -0400 | [diff] [blame] | 7007 | return status; |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 7008 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 7009 | clp = cstate->clp; |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 7010 | /* Find the matching lock stateowner */ |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 7011 | spin_lock(&clp->cl_lock); |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 7012 | list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval], |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 7013 | so_strhash) { |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 7014 | |
| 7015 | if (sop->so_is_open_owner || !same_owner_str(sop, owner)) |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 7016 | continue; |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 7017 | |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 7018 | /* see if there are still any locks associated with it */ |
| 7019 | lo = lockowner(sop); |
| 7020 | list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) { |
| 7021 | if (check_for_locks(stp->st_stid.sc_file, lo)) { |
| 7022 | status = nfserr_locks_held; |
| 7023 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | 51f5e78 | 2014-07-30 08:27:27 -0400 | [diff] [blame] | 7024 | return status; |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 7025 | } |
Jeff Layton | 5adfd88 | 2014-07-29 21:34:31 -0400 | [diff] [blame] | 7026 | } |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 7027 | |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 7028 | nfs4_get_stateowner(sop); |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 7029 | break; |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 7030 | } |
Chuck Lever | 8858481 | 2016-07-13 16:40:14 -0400 | [diff] [blame] | 7031 | if (!lo) { |
| 7032 | spin_unlock(&clp->cl_lock); |
| 7033 | return status; |
| 7034 | } |
| 7035 | |
| 7036 | unhash_lockowner_locked(lo); |
| 7037 | while (!list_empty(&lo->lo_owner.so_stateids)) { |
| 7038 | stp = list_first_entry(&lo->lo_owner.so_stateids, |
| 7039 | struct nfs4_ol_stateid, |
| 7040 | st_perstateowner); |
| 7041 | WARN_ON(!unhash_lock_stateid(stp)); |
| 7042 | put_ol_stateid_locked(stp, &reaplist); |
| 7043 | } |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 7044 | spin_unlock(&clp->cl_lock); |
Chuck Lever | 8858481 | 2016-07-13 16:40:14 -0400 | [diff] [blame] | 7045 | free_ol_stateid_reaplist(&reaplist); |
Jeff Layton | 68ef3bc | 2018-03-16 11:32:02 -0400 | [diff] [blame] | 7046 | remove_blocked_locks(lo); |
Chuck Lever | 8858481 | 2016-07-13 16:40:14 -0400 | [diff] [blame] | 7047 | nfs4_put_stateowner(&lo->lo_owner); |
| 7048 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7049 | return status; |
| 7050 | } |
| 7051 | |
| 7052 | static inline struct nfs4_client_reclaim * |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 7053 | alloc_reclaim(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7054 | { |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 7055 | return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7056 | } |
| 7057 | |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 7058 | bool |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 7059 | nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn) |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 7060 | { |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 7061 | struct nfs4_client_reclaim *crp; |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 7062 | |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7063 | crp = nfsd4_find_reclaim_client(name, nn); |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 7064 | return (crp && crp->cr_clp); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 7065 | } |
| 7066 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7067 | /* |
| 7068 | * failure => all reset bets are off, nfserr_no_grace... |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 7069 | * |
| 7070 | * The caller is responsible for freeing name.data if NULL is returned (it |
| 7071 | * will be freed in nfs4_remove_reclaim_record in the normal case). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7072 | */ |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 7073 | struct nfs4_client_reclaim * |
Scott Mayhew | 6ee95d1 | 2019-09-09 16:10:31 -0400 | [diff] [blame] | 7074 | nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash, |
| 7075 | struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7076 | { |
| 7077 | unsigned int strhashval; |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 7078 | struct nfs4_client_reclaim *crp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7079 | |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 7080 | dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", name.len, name.data); |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 7081 | crp = alloc_reclaim(); |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 7082 | if (crp) { |
| 7083 | strhashval = clientstr_hashval(name); |
| 7084 | INIT_LIST_HEAD(&crp->cr_strhash); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7085 | list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]); |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 7086 | crp->cr_name.data = name.data; |
| 7087 | crp->cr_name.len = name.len; |
Scott Mayhew | 6ee95d1 | 2019-09-09 16:10:31 -0400 | [diff] [blame] | 7088 | crp->cr_princhash.data = princhash.data; |
| 7089 | crp->cr_princhash.len = princhash.len; |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 7090 | crp->cr_clp = NULL; |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7091 | nn->reclaim_str_hashtbl_size++; |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 7092 | } |
| 7093 | return crp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7094 | } |
| 7095 | |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 7096 | void |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7097 | 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] | 7098 | { |
| 7099 | list_del(&crp->cr_strhash); |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 7100 | kfree(crp->cr_name.data); |
Scott Mayhew | 6ee95d1 | 2019-09-09 16:10:31 -0400 | [diff] [blame] | 7101 | kfree(crp->cr_princhash.data); |
Jeff Layton | ce30e53 | 2012-11-12 15:00:53 -0500 | [diff] [blame] | 7102 | kfree(crp); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7103 | nn->reclaim_str_hashtbl_size--; |
Jeff Layton | ce30e53 | 2012-11-12 15:00:53 -0500 | [diff] [blame] | 7104 | } |
| 7105 | |
| 7106 | void |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7107 | nfs4_release_reclaim(struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7108 | { |
| 7109 | struct nfs4_client_reclaim *crp = NULL; |
| 7110 | int i; |
| 7111 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7112 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7113 | while (!list_empty(&nn->reclaim_str_hashtbl[i])) { |
| 7114 | crp = list_entry(nn->reclaim_str_hashtbl[i].next, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7115 | struct nfs4_client_reclaim, cr_strhash); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7116 | nfs4_remove_reclaim_record(crp, nn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7117 | } |
| 7118 | } |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 7119 | WARN_ON_ONCE(nn->reclaim_str_hashtbl_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7120 | } |
| 7121 | |
| 7122 | /* |
| 7123 | * called from OPEN, CLAIM_PREVIOUS with a new clientid. */ |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 7124 | struct nfs4_client_reclaim * |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 7125 | nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7126 | { |
| 7127 | unsigned int strhashval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7128 | struct nfs4_client_reclaim *crp = NULL; |
| 7129 | |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 7130 | 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] | 7131 | |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 7132 | strhashval = clientstr_hashval(name); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7133 | list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) { |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 7134 | if (compare_blob(&crp->cr_name, &name) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7135 | return crp; |
| 7136 | } |
| 7137 | } |
| 7138 | return NULL; |
| 7139 | } |
| 7140 | |
| 7141 | /* |
| 7142 | * Called from OPEN. Look for clientid in reclaim list. |
| 7143 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 7144 | __be32 |
Trond Myklebust | 0fe492d | 2014-06-30 11:48:47 -0400 | [diff] [blame] | 7145 | nfs4_check_open_reclaim(clientid_t *clid, |
| 7146 | struct nfsd4_compound_state *cstate, |
| 7147 | struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7148 | { |
Trond Myklebust | 0fe492d | 2014-06-30 11:48:47 -0400 | [diff] [blame] | 7149 | __be32 status; |
Jeff Layton | a52d726 | 2012-03-21 09:52:02 -0400 | [diff] [blame] | 7150 | |
| 7151 | /* find clientid in conf_id_hashtbl */ |
Olga Kornievskaia | b734220 | 2019-09-06 15:17:21 -0400 | [diff] [blame] | 7152 | status = lookup_clientid(clid, cstate, nn, false); |
Trond Myklebust | 0fe492d | 2014-06-30 11:48:47 -0400 | [diff] [blame] | 7153 | if (status) |
Jeff Layton | a52d726 | 2012-03-21 09:52:02 -0400 | [diff] [blame] | 7154 | return nfserr_reclaim_bad; |
| 7155 | |
Jeff Layton | 3b3e7b7 | 2014-09-12 16:40:22 -0400 | [diff] [blame] | 7156 | if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags)) |
| 7157 | return nfserr_no_grace; |
| 7158 | |
Trond Myklebust | 0fe492d | 2014-06-30 11:48:47 -0400 | [diff] [blame] | 7159 | if (nfsd4_client_record_check(cstate->clp)) |
| 7160 | return nfserr_reclaim_bad; |
| 7161 | |
| 7162 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7163 | } |
| 7164 | |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 7165 | #ifdef CONFIG_NFSD_FAULT_INJECTION |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7166 | static inline void |
| 7167 | put_client(struct nfs4_client *clp) |
| 7168 | { |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 7169 | atomic_dec(&clp->cl_rpc_users); |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7170 | } |
| 7171 | |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7172 | static struct nfs4_client * |
| 7173 | nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size) |
| 7174 | { |
| 7175 | struct nfs4_client *clp; |
| 7176 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7177 | nfsd_net_id); |
| 7178 | |
| 7179 | if (!nfsd_netns_ready(nn)) |
| 7180 | return NULL; |
| 7181 | |
| 7182 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { |
| 7183 | if (memcmp(&clp->cl_addr, addr, addr_size) == 0) |
| 7184 | return clp; |
| 7185 | } |
| 7186 | return NULL; |
| 7187 | } |
| 7188 | |
Jeff Layton | 7ec0e36 | 2014-07-30 08:27:17 -0400 | [diff] [blame] | 7189 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7190 | nfsd_inject_print_clients(void) |
Jeff Layton | 7ec0e36 | 2014-07-30 08:27:17 -0400 | [diff] [blame] | 7191 | { |
| 7192 | struct nfs4_client *clp; |
| 7193 | u64 count = 0; |
| 7194 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7195 | nfsd_net_id); |
| 7196 | char buf[INET6_ADDRSTRLEN]; |
| 7197 | |
| 7198 | if (!nfsd_netns_ready(nn)) |
| 7199 | return 0; |
| 7200 | |
| 7201 | spin_lock(&nn->client_lock); |
| 7202 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { |
| 7203 | rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf)); |
| 7204 | pr_info("NFS Client: %s\n", buf); |
| 7205 | ++count; |
| 7206 | } |
| 7207 | spin_unlock(&nn->client_lock); |
| 7208 | |
| 7209 | return count; |
| 7210 | } |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 7211 | |
Jeff Layton | a0926d1 | 2014-07-30 08:27:18 -0400 | [diff] [blame] | 7212 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7213 | nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size) |
Jeff Layton | a0926d1 | 2014-07-30 08:27:18 -0400 | [diff] [blame] | 7214 | { |
| 7215 | u64 count = 0; |
| 7216 | struct nfs4_client *clp; |
| 7217 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7218 | nfsd_net_id); |
| 7219 | |
| 7220 | if (!nfsd_netns_ready(nn)) |
| 7221 | return count; |
| 7222 | |
| 7223 | spin_lock(&nn->client_lock); |
| 7224 | clp = nfsd_find_client(addr, addr_size); |
| 7225 | if (clp) { |
| 7226 | if (mark_client_expired_locked(clp) == nfs_ok) |
| 7227 | ++count; |
| 7228 | else |
| 7229 | clp = NULL; |
| 7230 | } |
| 7231 | spin_unlock(&nn->client_lock); |
| 7232 | |
| 7233 | if (clp) |
| 7234 | expire_client(clp); |
| 7235 | |
| 7236 | return count; |
| 7237 | } |
| 7238 | |
Jeff Layton | 69fc9ed | 2014-07-30 08:27:19 -0400 | [diff] [blame] | 7239 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7240 | nfsd_inject_forget_clients(u64 max) |
Jeff Layton | 69fc9ed | 2014-07-30 08:27:19 -0400 | [diff] [blame] | 7241 | { |
| 7242 | u64 count = 0; |
| 7243 | struct nfs4_client *clp, *next; |
| 7244 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7245 | nfsd_net_id); |
| 7246 | LIST_HEAD(reaplist); |
| 7247 | |
| 7248 | if (!nfsd_netns_ready(nn)) |
| 7249 | return count; |
| 7250 | |
| 7251 | spin_lock(&nn->client_lock); |
| 7252 | list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) { |
| 7253 | if (mark_client_expired_locked(clp) == nfs_ok) { |
| 7254 | list_add(&clp->cl_lru, &reaplist); |
| 7255 | if (max != 0 && ++count >= max) |
| 7256 | break; |
| 7257 | } |
| 7258 | } |
| 7259 | spin_unlock(&nn->client_lock); |
| 7260 | |
| 7261 | list_for_each_entry_safe(clp, next, &reaplist, cl_lru) |
| 7262 | expire_client(clp); |
| 7263 | |
| 7264 | return count; |
| 7265 | } |
| 7266 | |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 7267 | static void nfsd_print_count(struct nfs4_client *clp, unsigned int count, |
| 7268 | const char *type) |
| 7269 | { |
| 7270 | char buf[INET6_ADDRSTRLEN]; |
Bryan Schumaker | 0a5c33e | 2012-12-07 16:17:28 -0500 | [diff] [blame] | 7271 | rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf)); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 7272 | printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type); |
| 7273 | } |
| 7274 | |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7275 | static void |
| 7276 | nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst, |
| 7277 | struct list_head *collect) |
| 7278 | { |
| 7279 | struct nfs4_client *clp = lst->st_stid.sc_client; |
| 7280 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7281 | nfsd_net_id); |
| 7282 | |
| 7283 | if (!collect) |
| 7284 | return; |
| 7285 | |
| 7286 | lockdep_assert_held(&nn->client_lock); |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 7287 | atomic_inc(&clp->cl_rpc_users); |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7288 | list_add(&lst->st_locks, collect); |
| 7289 | } |
| 7290 | |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 7291 | static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max, |
Jeff Layton | 3738d50 | 2014-07-30 08:27:20 -0400 | [diff] [blame] | 7292 | struct list_head *collect, |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 7293 | bool (*func)(struct nfs4_ol_stateid *)) |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 7294 | { |
| 7295 | struct nfs4_openowner *oop; |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 7296 | struct nfs4_ol_stateid *stp, *st_next; |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 7297 | struct nfs4_ol_stateid *lst, *lst_next; |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 7298 | u64 count = 0; |
| 7299 | |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7300 | spin_lock(&clp->cl_lock); |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 7301 | list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) { |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 7302 | list_for_each_entry_safe(stp, st_next, |
| 7303 | &oop->oo_owner.so_stateids, st_perstateowner) { |
| 7304 | list_for_each_entry_safe(lst, lst_next, |
| 7305 | &stp->st_locks, st_locks) { |
Jeff Layton | 3738d50 | 2014-07-30 08:27:20 -0400 | [diff] [blame] | 7306 | if (func) { |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 7307 | if (func(lst)) |
| 7308 | nfsd_inject_add_lock_to_list(lst, |
| 7309 | collect); |
Jeff Layton | 3738d50 | 2014-07-30 08:27:20 -0400 | [diff] [blame] | 7310 | } |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7311 | ++count; |
| 7312 | /* |
| 7313 | * Despite the fact that these functions deal |
| 7314 | * with 64-bit integers for "count", we must |
| 7315 | * ensure that it doesn't blow up the |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 7316 | * clp->cl_rpc_users. Throw a warning if we |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7317 | * start to approach INT_MAX here. |
| 7318 | */ |
| 7319 | WARN_ON_ONCE(count == (INT_MAX / 2)); |
| 7320 | if (count == max) |
| 7321 | goto out; |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 7322 | } |
| 7323 | } |
| 7324 | } |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7325 | out: |
| 7326 | spin_unlock(&clp->cl_lock); |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 7327 | |
| 7328 | return count; |
| 7329 | } |
| 7330 | |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7331 | static u64 |
| 7332 | nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect, |
| 7333 | u64 max) |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 7334 | { |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7335 | return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid); |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 7336 | } |
| 7337 | |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7338 | static u64 |
| 7339 | nfsd_print_client_locks(struct nfs4_client *clp) |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 7340 | { |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7341 | u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 7342 | nfsd_print_count(clp, count, "locked files"); |
| 7343 | return count; |
| 7344 | } |
| 7345 | |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7346 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7347 | nfsd_inject_print_locks(void) |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7348 | { |
| 7349 | struct nfs4_client *clp; |
| 7350 | u64 count = 0; |
| 7351 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7352 | nfsd_net_id); |
| 7353 | |
| 7354 | if (!nfsd_netns_ready(nn)) |
| 7355 | return 0; |
| 7356 | |
| 7357 | spin_lock(&nn->client_lock); |
| 7358 | list_for_each_entry(clp, &nn->client_lru, cl_lru) |
| 7359 | count += nfsd_print_client_locks(clp); |
| 7360 | spin_unlock(&nn->client_lock); |
| 7361 | |
| 7362 | return count; |
| 7363 | } |
| 7364 | |
| 7365 | static void |
| 7366 | nfsd_reap_locks(struct list_head *reaplist) |
| 7367 | { |
| 7368 | struct nfs4_client *clp; |
| 7369 | struct nfs4_ol_stateid *stp, *next; |
| 7370 | |
| 7371 | list_for_each_entry_safe(stp, next, reaplist, st_locks) { |
| 7372 | list_del_init(&stp->st_locks); |
| 7373 | clp = stp->st_stid.sc_client; |
| 7374 | nfs4_put_stid(&stp->st_stid); |
| 7375 | put_client(clp); |
| 7376 | } |
| 7377 | } |
| 7378 | |
| 7379 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7380 | 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] | 7381 | { |
| 7382 | unsigned int count = 0; |
| 7383 | struct nfs4_client *clp; |
| 7384 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7385 | nfsd_net_id); |
| 7386 | LIST_HEAD(reaplist); |
| 7387 | |
| 7388 | if (!nfsd_netns_ready(nn)) |
| 7389 | return count; |
| 7390 | |
| 7391 | spin_lock(&nn->client_lock); |
| 7392 | clp = nfsd_find_client(addr, addr_size); |
| 7393 | if (clp) |
| 7394 | count = nfsd_collect_client_locks(clp, &reaplist, 0); |
| 7395 | spin_unlock(&nn->client_lock); |
| 7396 | nfsd_reap_locks(&reaplist); |
| 7397 | return count; |
| 7398 | } |
| 7399 | |
| 7400 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7401 | nfsd_inject_forget_locks(u64 max) |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7402 | { |
| 7403 | u64 count = 0; |
| 7404 | struct nfs4_client *clp; |
| 7405 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7406 | nfsd_net_id); |
| 7407 | LIST_HEAD(reaplist); |
| 7408 | |
| 7409 | if (!nfsd_netns_ready(nn)) |
| 7410 | return count; |
| 7411 | |
| 7412 | spin_lock(&nn->client_lock); |
| 7413 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { |
| 7414 | count += nfsd_collect_client_locks(clp, &reaplist, max - count); |
| 7415 | if (max != 0 && count >= max) |
| 7416 | break; |
| 7417 | } |
| 7418 | spin_unlock(&nn->client_lock); |
| 7419 | nfsd_reap_locks(&reaplist); |
| 7420 | return count; |
| 7421 | } |
| 7422 | |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7423 | static u64 |
| 7424 | nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max, |
| 7425 | struct list_head *collect, |
| 7426 | void (*func)(struct nfs4_openowner *)) |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 7427 | { |
| 7428 | struct nfs4_openowner *oop, *next; |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7429 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7430 | nfsd_net_id); |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 7431 | u64 count = 0; |
| 7432 | |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7433 | lockdep_assert_held(&nn->client_lock); |
| 7434 | |
| 7435 | spin_lock(&clp->cl_lock); |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 7436 | list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) { |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7437 | if (func) { |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 7438 | func(oop); |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7439 | if (collect) { |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 7440 | atomic_inc(&clp->cl_rpc_users); |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7441 | list_add(&oop->oo_perclient, collect); |
| 7442 | } |
| 7443 | } |
| 7444 | ++count; |
| 7445 | /* |
| 7446 | * Despite the fact that these functions deal with |
| 7447 | * 64-bit integers for "count", we must ensure that |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 7448 | * it doesn't blow up the clp->cl_rpc_users. Throw a |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7449 | * warning if we start to approach INT_MAX here. |
| 7450 | */ |
| 7451 | WARN_ON_ONCE(count == (INT_MAX / 2)); |
| 7452 | if (count == max) |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 7453 | break; |
| 7454 | } |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7455 | spin_unlock(&clp->cl_lock); |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 7456 | |
| 7457 | return count; |
| 7458 | } |
| 7459 | |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7460 | static u64 |
| 7461 | nfsd_print_client_openowners(struct nfs4_client *clp) |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 7462 | { |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7463 | u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL); |
| 7464 | |
| 7465 | nfsd_print_count(clp, count, "openowners"); |
| 7466 | return count; |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 7467 | } |
| 7468 | |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7469 | static u64 |
| 7470 | nfsd_collect_client_openowners(struct nfs4_client *clp, |
| 7471 | struct list_head *collect, u64 max) |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 7472 | { |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7473 | return nfsd_foreach_client_openowner(clp, max, collect, |
| 7474 | unhash_openowner_locked); |
| 7475 | } |
| 7476 | |
| 7477 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7478 | nfsd_inject_print_openowners(void) |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7479 | { |
| 7480 | struct nfs4_client *clp; |
| 7481 | u64 count = 0; |
| 7482 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7483 | nfsd_net_id); |
| 7484 | |
| 7485 | if (!nfsd_netns_ready(nn)) |
| 7486 | return 0; |
| 7487 | |
| 7488 | spin_lock(&nn->client_lock); |
| 7489 | list_for_each_entry(clp, &nn->client_lru, cl_lru) |
| 7490 | count += nfsd_print_client_openowners(clp); |
| 7491 | spin_unlock(&nn->client_lock); |
| 7492 | |
| 7493 | return count; |
| 7494 | } |
| 7495 | |
| 7496 | static void |
| 7497 | nfsd_reap_openowners(struct list_head *reaplist) |
| 7498 | { |
| 7499 | struct nfs4_client *clp; |
| 7500 | struct nfs4_openowner *oop, *next; |
| 7501 | |
| 7502 | list_for_each_entry_safe(oop, next, reaplist, oo_perclient) { |
| 7503 | list_del_init(&oop->oo_perclient); |
| 7504 | clp = oop->oo_owner.so_client; |
| 7505 | release_openowner(oop); |
| 7506 | put_client(clp); |
| 7507 | } |
| 7508 | } |
| 7509 | |
| 7510 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7511 | nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr, |
| 7512 | size_t addr_size) |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7513 | { |
| 7514 | unsigned int count = 0; |
| 7515 | struct nfs4_client *clp; |
| 7516 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7517 | nfsd_net_id); |
| 7518 | LIST_HEAD(reaplist); |
| 7519 | |
| 7520 | if (!nfsd_netns_ready(nn)) |
| 7521 | return count; |
| 7522 | |
| 7523 | spin_lock(&nn->client_lock); |
| 7524 | clp = nfsd_find_client(addr, addr_size); |
| 7525 | if (clp) |
| 7526 | count = nfsd_collect_client_openowners(clp, &reaplist, 0); |
| 7527 | spin_unlock(&nn->client_lock); |
| 7528 | nfsd_reap_openowners(&reaplist); |
| 7529 | return count; |
| 7530 | } |
| 7531 | |
| 7532 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7533 | nfsd_inject_forget_openowners(u64 max) |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7534 | { |
| 7535 | u64 count = 0; |
| 7536 | struct nfs4_client *clp; |
| 7537 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7538 | nfsd_net_id); |
| 7539 | LIST_HEAD(reaplist); |
| 7540 | |
| 7541 | if (!nfsd_netns_ready(nn)) |
| 7542 | return count; |
| 7543 | |
| 7544 | spin_lock(&nn->client_lock); |
| 7545 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { |
| 7546 | count += nfsd_collect_client_openowners(clp, &reaplist, |
| 7547 | max - count); |
| 7548 | if (max != 0 && count >= max) |
| 7549 | break; |
| 7550 | } |
| 7551 | spin_unlock(&nn->client_lock); |
| 7552 | nfsd_reap_openowners(&reaplist); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 7553 | return count; |
| 7554 | } |
| 7555 | |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 7556 | static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max, |
| 7557 | struct list_head *victims) |
| 7558 | { |
| 7559 | struct nfs4_delegation *dp, *next; |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 7560 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7561 | nfsd_net_id); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 7562 | u64 count = 0; |
| 7563 | |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 7564 | lockdep_assert_held(&nn->client_lock); |
| 7565 | |
| 7566 | spin_lock(&state_lock); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 7567 | list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) { |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 7568 | if (victims) { |
| 7569 | /* |
| 7570 | * It's not safe to mess with delegations that have a |
| 7571 | * non-zero dl_time. They might have already been broken |
| 7572 | * and could be processed by the laundromat outside of |
| 7573 | * the state_lock. Just leave them be. |
| 7574 | */ |
| 7575 | if (dp->dl_time != 0) |
| 7576 | continue; |
| 7577 | |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 7578 | atomic_inc(&clp->cl_rpc_users); |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 7579 | WARN_ON(!unhash_delegation_locked(dp)); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 7580 | list_add(&dp->dl_recall_lru, victims); |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 7581 | } |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 7582 | ++count; |
| 7583 | /* |
| 7584 | * Despite the fact that these functions deal with |
| 7585 | * 64-bit integers for "count", we must ensure that |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 7586 | * it doesn't blow up the clp->cl_rpc_users. Throw a |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 7587 | * warning if we start to approach INT_MAX here. |
| 7588 | */ |
| 7589 | WARN_ON_ONCE(count == (INT_MAX / 2)); |
| 7590 | if (count == max) |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 7591 | break; |
| 7592 | } |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 7593 | spin_unlock(&state_lock); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 7594 | return count; |
| 7595 | } |
| 7596 | |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 7597 | static u64 |
| 7598 | nfsd_print_client_delegations(struct nfs4_client *clp) |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 7599 | { |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 7600 | u64 count = nfsd_find_all_delegations(clp, 0, NULL); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 7601 | |
| 7602 | nfsd_print_count(clp, count, "delegations"); |
| 7603 | return count; |
| 7604 | } |
| 7605 | |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 7606 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7607 | nfsd_inject_print_delegations(void) |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 7608 | { |
| 7609 | struct nfs4_client *clp; |
| 7610 | u64 count = 0; |
| 7611 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7612 | nfsd_net_id); |
| 7613 | |
| 7614 | if (!nfsd_netns_ready(nn)) |
| 7615 | return 0; |
| 7616 | |
| 7617 | spin_lock(&nn->client_lock); |
| 7618 | list_for_each_entry(clp, &nn->client_lru, cl_lru) |
| 7619 | count += nfsd_print_client_delegations(clp); |
| 7620 | spin_unlock(&nn->client_lock); |
| 7621 | |
| 7622 | return count; |
| 7623 | } |
| 7624 | |
| 7625 | static void |
| 7626 | nfsd_forget_delegations(struct list_head *reaplist) |
| 7627 | { |
| 7628 | struct nfs4_client *clp; |
| 7629 | struct nfs4_delegation *dp, *next; |
| 7630 | |
| 7631 | list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) { |
| 7632 | list_del_init(&dp->dl_recall_lru); |
| 7633 | clp = dp->dl_stid.sc_client; |
| 7634 | revoke_delegation(dp); |
| 7635 | put_client(clp); |
| 7636 | } |
| 7637 | } |
| 7638 | |
| 7639 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7640 | nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr, |
| 7641 | size_t addr_size) |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 7642 | { |
| 7643 | u64 count = 0; |
| 7644 | struct nfs4_client *clp; |
| 7645 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7646 | nfsd_net_id); |
| 7647 | LIST_HEAD(reaplist); |
| 7648 | |
| 7649 | if (!nfsd_netns_ready(nn)) |
| 7650 | return count; |
| 7651 | |
| 7652 | spin_lock(&nn->client_lock); |
| 7653 | clp = nfsd_find_client(addr, addr_size); |
| 7654 | if (clp) |
| 7655 | count = nfsd_find_all_delegations(clp, 0, &reaplist); |
| 7656 | spin_unlock(&nn->client_lock); |
| 7657 | |
| 7658 | nfsd_forget_delegations(&reaplist); |
| 7659 | return count; |
| 7660 | } |
| 7661 | |
| 7662 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7663 | nfsd_inject_forget_delegations(u64 max) |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 7664 | { |
| 7665 | u64 count = 0; |
| 7666 | struct nfs4_client *clp; |
| 7667 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7668 | nfsd_net_id); |
| 7669 | LIST_HEAD(reaplist); |
| 7670 | |
| 7671 | if (!nfsd_netns_ready(nn)) |
| 7672 | return count; |
| 7673 | |
| 7674 | spin_lock(&nn->client_lock); |
| 7675 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { |
| 7676 | count += nfsd_find_all_delegations(clp, max - count, &reaplist); |
| 7677 | if (max != 0 && count >= max) |
| 7678 | break; |
| 7679 | } |
| 7680 | spin_unlock(&nn->client_lock); |
| 7681 | nfsd_forget_delegations(&reaplist); |
| 7682 | return count; |
| 7683 | } |
| 7684 | |
| 7685 | static void |
| 7686 | nfsd_recall_delegations(struct list_head *reaplist) |
| 7687 | { |
| 7688 | struct nfs4_client *clp; |
| 7689 | struct nfs4_delegation *dp, *next; |
| 7690 | |
| 7691 | list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) { |
| 7692 | list_del_init(&dp->dl_recall_lru); |
| 7693 | clp = dp->dl_stid.sc_client; |
| 7694 | /* |
| 7695 | * We skipped all entries that had a zero dl_time before, |
| 7696 | * so we can now reset the dl_time back to 0. If a delegation |
| 7697 | * break comes in now, then it won't make any difference since |
| 7698 | * we're recalling it either way. |
| 7699 | */ |
| 7700 | spin_lock(&state_lock); |
| 7701 | dp->dl_time = 0; |
| 7702 | spin_unlock(&state_lock); |
| 7703 | nfsd_break_one_deleg(dp); |
| 7704 | put_client(clp); |
| 7705 | } |
| 7706 | } |
| 7707 | |
| 7708 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7709 | nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr, |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 7710 | size_t addr_size) |
| 7711 | { |
| 7712 | u64 count = 0; |
| 7713 | struct nfs4_client *clp; |
| 7714 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7715 | nfsd_net_id); |
| 7716 | LIST_HEAD(reaplist); |
| 7717 | |
| 7718 | if (!nfsd_netns_ready(nn)) |
| 7719 | return count; |
| 7720 | |
| 7721 | spin_lock(&nn->client_lock); |
| 7722 | clp = nfsd_find_client(addr, addr_size); |
| 7723 | if (clp) |
| 7724 | count = nfsd_find_all_delegations(clp, 0, &reaplist); |
| 7725 | spin_unlock(&nn->client_lock); |
| 7726 | |
| 7727 | nfsd_recall_delegations(&reaplist); |
| 7728 | return count; |
| 7729 | } |
| 7730 | |
| 7731 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7732 | nfsd_inject_recall_delegations(u64 max) |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 7733 | { |
| 7734 | u64 count = 0; |
| 7735 | struct nfs4_client *clp, *next; |
| 7736 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7737 | nfsd_net_id); |
| 7738 | LIST_HEAD(reaplist); |
| 7739 | |
| 7740 | if (!nfsd_netns_ready(nn)) |
| 7741 | return count; |
| 7742 | |
| 7743 | spin_lock(&nn->client_lock); |
| 7744 | list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) { |
| 7745 | count += nfsd_find_all_delegations(clp, max - count, &reaplist); |
| 7746 | if (max != 0 && ++count >= max) |
| 7747 | break; |
| 7748 | } |
| 7749 | spin_unlock(&nn->client_lock); |
| 7750 | nfsd_recall_delegations(&reaplist); |
| 7751 | return count; |
| 7752 | } |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 7753 | #endif /* CONFIG_NFSD_FAULT_INJECTION */ |
| 7754 | |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 7755 | /* |
| 7756 | * Since the lifetime of a delegation isn't limited to that of an open, a |
| 7757 | * client may quite reasonably hang on to a delegation as long as it has |
| 7758 | * the inode cached. This becomes an obvious problem the first time a |
| 7759 | * client's inode cache approaches the size of the server's total memory. |
| 7760 | * |
| 7761 | * For now we avoid this problem by imposing a hard limit on the number |
| 7762 | * of delegations, which varies according to the server's memory size. |
| 7763 | */ |
| 7764 | static void |
| 7765 | set_max_delegations(void) |
| 7766 | { |
| 7767 | /* |
| 7768 | * Allow at most 4 delegations per megabyte of RAM. Quick |
| 7769 | * estimates suggest that in the worst case (where every delegation |
| 7770 | * is for a different inode), a delegation could take about 1.5K, |
| 7771 | * giving a worst case usage of about 6% of memory. |
| 7772 | */ |
| 7773 | max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT); |
| 7774 | } |
| 7775 | |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 7776 | static int nfs4_state_create_net(struct net *net) |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7777 | { |
| 7778 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 7779 | int i; |
| 7780 | |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 7781 | nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE, |
| 7782 | sizeof(struct list_head), |
| 7783 | GFP_KERNEL); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7784 | if (!nn->conf_id_hashtbl) |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 7785 | goto err; |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 7786 | nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE, |
| 7787 | sizeof(struct list_head), |
| 7788 | GFP_KERNEL); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 7789 | if (!nn->unconf_id_hashtbl) |
| 7790 | goto err_unconf_id; |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 7791 | nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE, |
| 7792 | sizeof(struct list_head), |
| 7793 | GFP_KERNEL); |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 7794 | if (!nn->sessionid_hashtbl) |
| 7795 | goto err_sessionid; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7796 | |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 7797 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7798 | INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 7799 | INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 7800 | } |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 7801 | for (i = 0; i < SESSION_HASH_SIZE; i++) |
| 7802 | INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 7803 | nn->conf_name_tree = RB_ROOT; |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 7804 | nn->unconf_name_tree = RB_ROOT; |
Arnd Bergmann | 9cc7680 | 2019-10-31 15:53:13 +0100 | [diff] [blame] | 7805 | nn->boot_time = ktime_get_real_seconds(); |
Vasily Averin | 81833de | 2017-11-13 07:25:40 +0300 | [diff] [blame] | 7806 | nn->grace_ended = false; |
| 7807 | nn->nfsd4_manager.block_opens = true; |
| 7808 | INIT_LIST_HEAD(&nn->nfsd4_manager.list); |
Stanislav Kinsbursky | 5ed58bb | 2012-11-14 18:21:56 +0300 | [diff] [blame] | 7809 | INIT_LIST_HEAD(&nn->client_lru); |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 7810 | INIT_LIST_HEAD(&nn->close_lru); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 7811 | INIT_LIST_HEAD(&nn->del_recall_lru); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 7812 | spin_lock_init(&nn->client_lock); |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 7813 | spin_lock_init(&nn->s2s_cp_lock); |
| 7814 | idr_init(&nn->s2s_cp_stateids); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7815 | |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 7816 | spin_lock_init(&nn->blocked_locks_lock); |
| 7817 | INIT_LIST_HEAD(&nn->blocked_locks_lru); |
| 7818 | |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 7819 | INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 7820 | get_net(net); |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 7821 | |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7822 | return 0; |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 7823 | |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 7824 | err_sessionid: |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 7825 | kfree(nn->unconf_id_hashtbl); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 7826 | err_unconf_id: |
| 7827 | kfree(nn->conf_id_hashtbl); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 7828 | err: |
| 7829 | return -ENOMEM; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7830 | } |
| 7831 | |
| 7832 | static void |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 7833 | nfs4_state_destroy_net(struct net *net) |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7834 | { |
| 7835 | int i; |
| 7836 | struct nfs4_client *clp = NULL; |
| 7837 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 7838 | |
| 7839 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
| 7840 | while (!list_empty(&nn->conf_id_hashtbl[i])) { |
| 7841 | clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); |
| 7842 | destroy_client(clp); |
| 7843 | } |
| 7844 | } |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 7845 | |
Jeff Layton | 68ef3bc | 2018-03-16 11:32:02 -0400 | [diff] [blame] | 7846 | WARN_ON(!list_empty(&nn->blocked_locks_lru)); |
| 7847 | |
Kinglong Mee | 2b90563 | 2014-03-26 22:09:30 +0800 | [diff] [blame] | 7848 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
| 7849 | while (!list_empty(&nn->unconf_id_hashtbl[i])) { |
| 7850 | clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); |
| 7851 | destroy_client(clp); |
| 7852 | } |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 7853 | } |
| 7854 | |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 7855 | kfree(nn->sessionid_hashtbl); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 7856 | kfree(nn->unconf_id_hashtbl); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7857 | kfree(nn->conf_id_hashtbl); |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 7858 | put_net(net); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7859 | } |
| 7860 | |
Stanislav Kinsbursky | f252bc6 | 2012-11-26 15:22:18 +0300 | [diff] [blame] | 7861 | int |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 7862 | nfs4_state_start_net(struct net *net) |
NeilBrown | ac4d8ff2 | 2005-06-23 22:03:30 -0700 | [diff] [blame] | 7863 | { |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 7864 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 7865 | int ret; |
| 7866 | |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 7867 | ret = nfs4_state_create_net(net); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7868 | if (ret) |
| 7869 | return ret; |
Jeff Layton | d4318ac | 2014-09-12 16:40:21 -0400 | [diff] [blame] | 7870 | locks_start_grace(net, &nn->nfsd4_manager); |
| 7871 | nfsd4_client_tracking_init(net); |
Scott Mayhew | 362063a | 2019-03-26 18:06:28 -0400 | [diff] [blame] | 7872 | if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0) |
| 7873 | goto skip_grace; |
Arnd Bergmann | 20b7d86 | 2019-11-04 16:31:52 +0100 | [diff] [blame] | 7874 | printk(KERN_INFO "NFSD: starting %lld-second grace period (net %x)\n", |
Vasily Averin | 7e981a8 | 2017-11-06 16:46:04 +0300 | [diff] [blame] | 7875 | nn->nfsd4_grace, net->ns.inum); |
Stanislav Kinsbursky | 5284b44 | 2012-11-27 14:11:49 +0300 | [diff] [blame] | 7876 | queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 7877 | return 0; |
Scott Mayhew | 362063a | 2019-03-26 18:06:28 -0400 | [diff] [blame] | 7878 | |
| 7879 | skip_grace: |
| 7880 | printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n", |
| 7881 | net->ns.inum); |
| 7882 | queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ); |
| 7883 | nfsd4_end_grace(nn); |
| 7884 | return 0; |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 7885 | } |
| 7886 | |
| 7887 | /* initialization to perform when the nfsd service is started: */ |
| 7888 | |
| 7889 | int |
| 7890 | nfs4_state_start(void) |
| 7891 | { |
| 7892 | int ret; |
| 7893 | |
Jeff Layton | 51a5456 | 2015-08-20 07:17:01 -0400 | [diff] [blame] | 7894 | laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4"); |
Jeff Layton | a6d6b78 | 2012-03-05 11:42:36 -0500 | [diff] [blame] | 7895 | if (laundry_wq == NULL) { |
| 7896 | ret = -ENOMEM; |
Chuck Lever | a26dd64 | 2018-08-16 12:06:09 -0400 | [diff] [blame] | 7897 | goto out; |
Jeff Layton | a6d6b78 | 2012-03-05 11:42:36 -0500 | [diff] [blame] | 7898 | } |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 7899 | ret = nfsd4_create_callback_queue(); |
| 7900 | if (ret) |
| 7901 | goto out_free_laundry; |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 7902 | |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 7903 | set_max_delegations(); |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 7904 | return 0; |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 7905 | |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 7906 | out_free_laundry: |
| 7907 | destroy_workqueue(laundry_wq); |
Chuck Lever | a26dd64 | 2018-08-16 12:06:09 -0400 | [diff] [blame] | 7908 | out: |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 7909 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7910 | } |
| 7911 | |
Stanislav Kinsbursky | f252bc6 | 2012-11-26 15:22:18 +0300 | [diff] [blame] | 7912 | void |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 7913 | nfs4_state_shutdown_net(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7914 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7915 | struct nfs4_delegation *dp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7916 | struct list_head *pos, *next, reaplist; |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 7917 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7918 | |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 7919 | cancel_delayed_work_sync(&nn->laundromat_work); |
| 7920 | locks_end_grace(&nn->nfsd4_manager); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 7921 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7922 | INIT_LIST_HEAD(&reaplist); |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 7923 | spin_lock(&state_lock); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 7924 | list_for_each_safe(pos, next, &nn->del_recall_lru) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7925 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 7926 | WARN_ON(!unhash_delegation_locked(dp)); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 7927 | list_add(&dp->dl_recall_lru, &reaplist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7928 | } |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 7929 | spin_unlock(&state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7930 | list_for_each_safe(pos, next, &reaplist) { |
| 7931 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 7932 | list_del_init(&dp->dl_recall_lru); |
J. Bruce Fields | 0af6e69 | 2018-02-21 15:11:03 -0500 | [diff] [blame] | 7933 | destroy_unhashed_deleg(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7934 | } |
| 7935 | |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 7936 | nfsd4_client_tracking_exit(net); |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 7937 | nfs4_state_destroy_net(net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7938 | } |
| 7939 | |
| 7940 | void |
| 7941 | nfs4_state_shutdown(void) |
| 7942 | { |
NeilBrown | 5e8d5c2 | 2006-04-10 22:55:37 -0700 | [diff] [blame] | 7943 | destroy_workqueue(laundry_wq); |
J. Bruce Fields | c3935e3 | 2010-06-04 16:42:08 -0400 | [diff] [blame] | 7944 | nfsd4_destroy_callback_queue(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7945 | } |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7946 | |
| 7947 | static void |
| 7948 | get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) |
| 7949 | { |
Olga Kornievskaia | 51100d2 | 2018-09-13 13:58:24 -0400 | [diff] [blame] | 7950 | if (HAS_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG) && |
| 7951 | CURRENT_STATEID(stateid)) |
Tigran Mkrtchyan | 37c593c | 2012-02-13 22:55:32 +0100 | [diff] [blame] | 7952 | memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t)); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7953 | } |
| 7954 | |
| 7955 | static void |
| 7956 | put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) |
| 7957 | { |
Tigran Mkrtchyan | 37c593c | 2012-02-13 22:55:32 +0100 | [diff] [blame] | 7958 | if (cstate->minorversion) { |
| 7959 | memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t)); |
Olga Kornievskaia | 51100d2 | 2018-09-13 13:58:24 -0400 | [diff] [blame] | 7960 | SET_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG); |
Tigran Mkrtchyan | 37c593c | 2012-02-13 22:55:32 +0100 | [diff] [blame] | 7961 | } |
| 7962 | } |
| 7963 | |
| 7964 | void |
| 7965 | clear_current_stateid(struct nfsd4_compound_state *cstate) |
| 7966 | { |
Olga Kornievskaia | 51100d2 | 2018-09-13 13:58:24 -0400 | [diff] [blame] | 7967 | CLEAR_CSTATE_FLAG(cstate, CURRENT_STATE_ID_FLAG); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7968 | } |
| 7969 | |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 7970 | /* |
| 7971 | * functions to set current state id |
| 7972 | */ |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7973 | void |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7974 | nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, |
| 7975 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 7976 | { |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7977 | put_stateid(cstate, &u->open_downgrade.od_stateid); |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 7978 | } |
| 7979 | |
| 7980 | void |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7981 | nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, |
| 7982 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7983 | { |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7984 | put_stateid(cstate, &u->open.op_stateid); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7985 | } |
| 7986 | |
| 7987 | void |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7988 | nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, |
| 7989 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 7990 | { |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7991 | put_stateid(cstate, &u->close.cl_stateid); |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 7992 | } |
| 7993 | |
| 7994 | void |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7995 | nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, |
| 7996 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 7997 | { |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7998 | put_stateid(cstate, &u->lock.lk_resp_stateid); |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 7999 | } |
| 8000 | |
| 8001 | /* |
| 8002 | * functions to consume current state id |
| 8003 | */ |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 8004 | |
| 8005 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 8006 | nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, |
| 8007 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 8008 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 8009 | get_stateid(cstate, &u->open_downgrade.od_stateid); |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 8010 | } |
| 8011 | |
| 8012 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 8013 | nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, |
| 8014 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 8015 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 8016 | get_stateid(cstate, &u->delegreturn.dr_stateid); |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 8017 | } |
| 8018 | |
| 8019 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 8020 | nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, |
| 8021 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 8022 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 8023 | get_stateid(cstate, &u->free_stateid.fr_stateid); |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 8024 | } |
| 8025 | |
| 8026 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 8027 | nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, |
| 8028 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 8029 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 8030 | get_stateid(cstate, &u->setattr.sa_stateid); |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 8031 | } |
| 8032 | |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 8033 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 8034 | nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, |
| 8035 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 8036 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 8037 | get_stateid(cstate, &u->close.cl_stateid); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 8038 | } |
| 8039 | |
| 8040 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 8041 | nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, |
| 8042 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 8043 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 8044 | get_stateid(cstate, &u->locku.lu_stateid); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 8045 | } |
Tigran Mkrtchyan | 30813e2 | 2012-02-13 22:55:26 +0100 | [diff] [blame] | 8046 | |
| 8047 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 8048 | nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, |
| 8049 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 30813e2 | 2012-02-13 22:55:26 +0100 | [diff] [blame] | 8050 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 8051 | get_stateid(cstate, &u->read.rd_stateid); |
Tigran Mkrtchyan | 30813e2 | 2012-02-13 22:55:26 +0100 | [diff] [blame] | 8052 | } |
| 8053 | |
| 8054 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 8055 | nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, |
| 8056 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 30813e2 | 2012-02-13 22:55:26 +0100 | [diff] [blame] | 8057 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 8058 | get_stateid(cstate, &u->write.wr_stateid); |
Tigran Mkrtchyan | 30813e2 | 2012-02-13 22:55:26 +0100 | [diff] [blame] | 8059 | } |