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); |
| 174 | clp->cl_time = get_seconds(); |
| 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 | |
| 497 | spin_lock(&f->fi_lock); |
| 498 | ret = __nfs4_get_fd(f, O_RDWR); |
| 499 | if (!ret) { |
| 500 | ret = __nfs4_get_fd(f, O_WRONLY); |
| 501 | if (!ret) |
| 502 | ret = __nfs4_get_fd(f, O_RDONLY); |
| 503 | } |
| 504 | spin_unlock(&f->fi_lock); |
| 505 | return ret; |
| 506 | } |
| 507 | |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 508 | static atomic_long_t num_delegations; |
Zhang Yanfei | 697ce9b | 2013-02-22 16:35:47 -0800 | [diff] [blame] | 509 | unsigned long max_delegations; |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 510 | |
| 511 | /* |
| 512 | * Open owner state (share locks) |
| 513 | */ |
| 514 | |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 515 | /* hash tables for lock and open owners */ |
| 516 | #define OWNER_HASH_BITS 8 |
| 517 | #define OWNER_HASH_SIZE (1 << OWNER_HASH_BITS) |
| 518 | #define OWNER_HASH_MASK (OWNER_HASH_SIZE - 1) |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 519 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 520 | static unsigned int ownerstr_hashval(struct xdr_netobj *ownername) |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 521 | { |
| 522 | unsigned int ret; |
| 523 | |
| 524 | ret = opaque_hashval(ownername->data, ownername->len); |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 525 | return ret & OWNER_HASH_MASK; |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 526 | } |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 527 | |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 528 | /* hash table for nfs4_file */ |
| 529 | #define FILE_HASH_BITS 8 |
| 530 | #define FILE_HASH_SIZE (1 << FILE_HASH_BITS) |
Shan Wei | 3507958 | 2011-01-14 17:35:59 +0800 | [diff] [blame] | 531 | |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 532 | static unsigned int nfsd_fh_hashval(struct knfsd_fh *fh) |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 533 | { |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 534 | return jhash2(fh->fh_base.fh_pad, XDR_QUADLEN(fh->fh_size), 0); |
| 535 | } |
| 536 | |
| 537 | static unsigned int file_hashval(struct knfsd_fh *fh) |
| 538 | { |
| 539 | return nfsd_fh_hashval(fh) & (FILE_HASH_SIZE - 1); |
| 540 | } |
| 541 | |
Jeff Layton | 89876f8 | 2013-04-02 09:01:59 -0400 | [diff] [blame] | 542 | static struct hlist_head file_hashtbl[FILE_HASH_SIZE]; |
NeilBrown | ef0f339 | 2006-04-10 22:55:41 -0700 | [diff] [blame] | 543 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 544 | static void |
| 545 | __nfs4_file_get_access(struct nfs4_file *fp, u32 access) |
J. Bruce Fields | 3477565 | 2013-08-23 17:55:18 -0400 | [diff] [blame] | 546 | { |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 547 | lockdep_assert_held(&fp->fi_lock); |
| 548 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 549 | if (access & NFS4_SHARE_ACCESS_WRITE) |
| 550 | atomic_inc(&fp->fi_access[O_WRONLY]); |
| 551 | if (access & NFS4_SHARE_ACCESS_READ) |
| 552 | atomic_inc(&fp->fi_access[O_RDONLY]); |
J. Bruce Fields | 3477565 | 2013-08-23 17:55:18 -0400 | [diff] [blame] | 553 | } |
| 554 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 555 | static __be32 |
| 556 | nfs4_file_get_access(struct nfs4_file *fp, u32 access) |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 557 | { |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 558 | lockdep_assert_held(&fp->fi_lock); |
| 559 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 560 | /* Does this access mode make sense? */ |
| 561 | if (access & ~NFS4_SHARE_ACCESS_BOTH) |
| 562 | return nfserr_inval; |
| 563 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 564 | /* Does it conflict with a deny mode already set? */ |
| 565 | if ((access & fp->fi_share_deny) != 0) |
| 566 | return nfserr_share_denied; |
| 567 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 568 | __nfs4_file_get_access(fp, access); |
| 569 | return nfs_ok; |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 570 | } |
| 571 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 572 | static __be32 nfs4_file_check_deny(struct nfs4_file *fp, u32 deny) |
| 573 | { |
| 574 | /* Common case is that there is no deny mode. */ |
| 575 | if (deny) { |
| 576 | /* Does this deny mode make sense? */ |
| 577 | if (deny & ~NFS4_SHARE_DENY_BOTH) |
| 578 | return nfserr_inval; |
| 579 | |
| 580 | if ((deny & NFS4_SHARE_DENY_READ) && |
| 581 | atomic_read(&fp->fi_access[O_RDONLY])) |
| 582 | return nfserr_share_denied; |
| 583 | |
| 584 | if ((deny & NFS4_SHARE_DENY_WRITE) && |
| 585 | atomic_read(&fp->fi_access[O_WRONLY])) |
| 586 | return nfserr_share_denied; |
| 587 | } |
| 588 | return nfs_ok; |
| 589 | } |
| 590 | |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 591 | 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] | 592 | { |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 593 | might_lock(&fp->fi_lock); |
| 594 | |
| 595 | if (atomic_dec_and_lock(&fp->fi_access[oflag], &fp->fi_lock)) { |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 596 | struct nfsd_file *f1 = NULL; |
| 597 | struct nfsd_file *f2 = NULL; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 598 | |
Jeff Layton | 6d338b5 | 2014-07-10 14:07:29 -0400 | [diff] [blame] | 599 | swap(f1, fp->fi_fds[oflag]); |
J. Bruce Fields | 0c7c3e6 | 2013-03-28 20:37:14 -0400 | [diff] [blame] | 600 | if (atomic_read(&fp->fi_access[1 - oflag]) == 0) |
Jeff Layton | 6d338b5 | 2014-07-10 14:07:29 -0400 | [diff] [blame] | 601 | swap(f2, fp->fi_fds[O_RDWR]); |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 602 | spin_unlock(&fp->fi_lock); |
| 603 | if (f1) |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 604 | nfsd_file_put(f1); |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 605 | if (f2) |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 606 | nfsd_file_put(f2); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 607 | } |
| 608 | } |
| 609 | |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 610 | 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] | 611 | { |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 612 | WARN_ON_ONCE(access & ~NFS4_SHARE_ACCESS_BOTH); |
| 613 | |
| 614 | if (access & NFS4_SHARE_ACCESS_WRITE) |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 615 | __nfs4_file_put_access(fp, O_WRONLY); |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 616 | if (access & NFS4_SHARE_ACCESS_READ) |
| 617 | __nfs4_file_put_access(fp, O_RDONLY); |
J. Bruce Fields | 998db52 | 2010-08-07 09:21:41 -0400 | [diff] [blame] | 618 | } |
| 619 | |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 620 | /* |
| 621 | * Allocate a new open/delegation state counter. This is needed for |
| 622 | * pNFS for proper return on close semantics. |
| 623 | * |
| 624 | * Note that we only allocate it for pNFS-enabled exports, otherwise |
| 625 | * all pointers to struct nfs4_clnt_odstate are always NULL. |
| 626 | */ |
| 627 | static struct nfs4_clnt_odstate * |
| 628 | alloc_clnt_odstate(struct nfs4_client *clp) |
| 629 | { |
| 630 | struct nfs4_clnt_odstate *co; |
| 631 | |
| 632 | co = kmem_cache_zalloc(odstate_slab, GFP_KERNEL); |
| 633 | if (co) { |
| 634 | co->co_client = clp; |
Elena Reshetova | cff7cb2 | 2017-10-20 12:53:29 +0300 | [diff] [blame] | 635 | refcount_set(&co->co_odcount, 1); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 636 | } |
| 637 | return co; |
| 638 | } |
| 639 | |
| 640 | static void |
| 641 | hash_clnt_odstate_locked(struct nfs4_clnt_odstate *co) |
| 642 | { |
| 643 | struct nfs4_file *fp = co->co_file; |
| 644 | |
| 645 | lockdep_assert_held(&fp->fi_lock); |
| 646 | list_add(&co->co_perfile, &fp->fi_clnt_odstate); |
| 647 | } |
| 648 | |
| 649 | static inline void |
| 650 | get_clnt_odstate(struct nfs4_clnt_odstate *co) |
| 651 | { |
| 652 | if (co) |
Elena Reshetova | cff7cb2 | 2017-10-20 12:53:29 +0300 | [diff] [blame] | 653 | refcount_inc(&co->co_odcount); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | static void |
| 657 | put_clnt_odstate(struct nfs4_clnt_odstate *co) |
| 658 | { |
| 659 | struct nfs4_file *fp; |
| 660 | |
| 661 | if (!co) |
| 662 | return; |
| 663 | |
| 664 | fp = co->co_file; |
Elena Reshetova | cff7cb2 | 2017-10-20 12:53:29 +0300 | [diff] [blame] | 665 | if (refcount_dec_and_lock(&co->co_odcount, &fp->fi_lock)) { |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 666 | list_del(&co->co_perfile); |
| 667 | spin_unlock(&fp->fi_lock); |
| 668 | |
| 669 | nfsd4_return_all_file_layouts(co->co_client, fp); |
| 670 | kmem_cache_free(odstate_slab, co); |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | static struct nfs4_clnt_odstate * |
| 675 | find_or_hash_clnt_odstate(struct nfs4_file *fp, struct nfs4_clnt_odstate *new) |
| 676 | { |
| 677 | struct nfs4_clnt_odstate *co; |
| 678 | struct nfs4_client *cl; |
| 679 | |
| 680 | if (!new) |
| 681 | return NULL; |
| 682 | |
| 683 | cl = new->co_client; |
| 684 | |
| 685 | spin_lock(&fp->fi_lock); |
| 686 | list_for_each_entry(co, &fp->fi_clnt_odstate, co_perfile) { |
| 687 | if (co->co_client == cl) { |
| 688 | get_clnt_odstate(co); |
| 689 | goto out; |
| 690 | } |
| 691 | } |
| 692 | co = new; |
| 693 | co->co_file = fp; |
| 694 | hash_clnt_odstate_locked(new); |
| 695 | out: |
| 696 | spin_unlock(&fp->fi_lock); |
| 697 | return co; |
| 698 | } |
| 699 | |
Kinglong Mee | d19fb70 | 2017-01-18 19:04:42 +0800 | [diff] [blame] | 700 | struct nfs4_stid *nfs4_alloc_stid(struct nfs4_client *cl, struct kmem_cache *slab, |
| 701 | void (*sc_free)(struct nfs4_stid *)) |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 702 | { |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 703 | struct nfs4_stid *stid; |
| 704 | int new_id; |
| 705 | |
Trond Myklebust | f833883 | 2014-07-25 07:34:19 -0400 | [diff] [blame] | 706 | stid = kmem_cache_zalloc(slab, GFP_KERNEL); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 707 | if (!stid) |
| 708 | return NULL; |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 709 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 710 | idr_preload(GFP_KERNEL); |
| 711 | spin_lock(&cl->cl_lock); |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 712 | /* Reserving 0 for start of file in nfsdfs "states" file: */ |
| 713 | 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] | 714 | spin_unlock(&cl->cl_lock); |
| 715 | idr_preload_end(); |
Tejun Heo | ebd6c70 | 2013-03-13 14:59:37 -0700 | [diff] [blame] | 716 | if (new_id < 0) |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 717 | goto out_free; |
Kinglong Mee | d19fb70 | 2017-01-18 19:04:42 +0800 | [diff] [blame] | 718 | |
| 719 | stid->sc_free = sc_free; |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 720 | stid->sc_client = cl; |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 721 | stid->sc_stateid.si_opaque.so_id = new_id; |
| 722 | stid->sc_stateid.si_opaque.so_clid = cl->cl_clientid; |
| 723 | /* Will be incremented before return to client: */ |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 724 | refcount_set(&stid->sc_count, 1); |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 725 | spin_lock_init(&stid->sc_lock); |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame^] | 726 | INIT_LIST_HEAD(&stid->sc_cp_list); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 727 | |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 728 | /* |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 729 | * It shouldn't be a problem to reuse an opaque stateid value. |
| 730 | * I don't think it is for 4.1. But with 4.0 I worry that, for |
| 731 | * example, a stray write retransmission could be accepted by |
| 732 | * the server when it should have been rejected. Therefore, |
| 733 | * adopt a trick from the sctp code to attempt to maximize the |
| 734 | * amount of time until an id is reused, by ensuring they always |
| 735 | * "increase" (mod INT_MAX): |
J. Bruce Fields | 996e093 | 2011-10-17 11:14:48 -0400 | [diff] [blame] | 736 | */ |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 737 | return stid; |
| 738 | out_free: |
Wei Yongjun | 2c44a23 | 2013-04-09 14:15:31 +0800 | [diff] [blame] | 739 | kmem_cache_free(slab, stid); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 740 | return NULL; |
J. Bruce Fields | 2a74aba | 2011-09-23 17:20:02 -0400 | [diff] [blame] | 741 | } |
| 742 | |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 743 | /* |
| 744 | * Create a unique stateid_t to represent each COPY. |
| 745 | */ |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame^] | 746 | static int nfs4_init_cp_state(struct nfsd_net *nn, copy_stateid_t *stid, |
| 747 | unsigned char sc_type) |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 748 | { |
| 749 | int new_id; |
| 750 | |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame^] | 751 | stid->stid.si_opaque.so_clid.cl_boot = nn->boot_time; |
| 752 | stid->stid.si_opaque.so_clid.cl_id = nn->s2s_cp_cl_id; |
| 753 | stid->sc_type = sc_type; |
| 754 | |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 755 | idr_preload(GFP_KERNEL); |
| 756 | spin_lock(&nn->s2s_cp_lock); |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame^] | 757 | new_id = idr_alloc_cyclic(&nn->s2s_cp_stateids, stid, 0, 0, GFP_NOWAIT); |
| 758 | stid->stid.si_opaque.so_id = new_id; |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 759 | spin_unlock(&nn->s2s_cp_lock); |
| 760 | idr_preload_end(); |
| 761 | if (new_id < 0) |
| 762 | return 0; |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 763 | return 1; |
| 764 | } |
| 765 | |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame^] | 766 | int nfs4_init_copy_state(struct nfsd_net *nn, struct nfsd4_copy *copy) |
| 767 | { |
| 768 | return nfs4_init_cp_state(nn, ©->cp_stateid, NFS4_COPY_STID); |
| 769 | } |
| 770 | |
| 771 | struct nfs4_cpntf_state *nfs4_alloc_init_cpntf_state(struct nfsd_net *nn, |
| 772 | struct nfs4_stid *p_stid) |
| 773 | { |
| 774 | struct nfs4_cpntf_state *cps; |
| 775 | |
| 776 | cps = kzalloc(sizeof(struct nfs4_cpntf_state), GFP_KERNEL); |
| 777 | if (!cps) |
| 778 | return NULL; |
| 779 | cps->cpntf_time = get_seconds(); |
| 780 | refcount_set(&cps->cp_stateid.sc_count, 1); |
| 781 | if (!nfs4_init_cp_state(nn, &cps->cp_stateid, NFS4_COPYNOTIFY_STID)) |
| 782 | goto out_free; |
| 783 | spin_lock(&nn->s2s_cp_lock); |
| 784 | list_add(&cps->cp_list, &p_stid->sc_cp_list); |
| 785 | spin_unlock(&nn->s2s_cp_lock); |
| 786 | return cps; |
| 787 | out_free: |
| 788 | kfree(cps); |
| 789 | return NULL; |
| 790 | } |
| 791 | |
| 792 | void nfs4_free_copy_state(struct nfsd4_copy *copy) |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 793 | { |
| 794 | struct nfsd_net *nn; |
| 795 | |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame^] | 796 | WARN_ON_ONCE(copy->cp_stateid.sc_type != NFS4_COPY_STID); |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 797 | nn = net_generic(copy->cp_clp->net, nfsd_net_id); |
| 798 | spin_lock(&nn->s2s_cp_lock); |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame^] | 799 | idr_remove(&nn->s2s_cp_stateids, |
| 800 | copy->cp_stateid.stid.si_opaque.so_id); |
| 801 | spin_unlock(&nn->s2s_cp_lock); |
| 802 | } |
| 803 | |
| 804 | static void nfs4_free_cpntf_statelist(struct net *net, struct nfs4_stid *stid) |
| 805 | { |
| 806 | struct nfs4_cpntf_state *cps; |
| 807 | struct nfsd_net *nn; |
| 808 | |
| 809 | nn = net_generic(net, nfsd_net_id); |
| 810 | spin_lock(&nn->s2s_cp_lock); |
| 811 | while (!list_empty(&stid->sc_cp_list)) { |
| 812 | cps = list_first_entry(&stid->sc_cp_list, |
| 813 | struct nfs4_cpntf_state, cp_list); |
| 814 | _free_cpntf_state_locked(nn, cps); |
| 815 | } |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 816 | spin_unlock(&nn->s2s_cp_lock); |
| 817 | } |
| 818 | |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 819 | 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] | 820 | { |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 821 | struct nfs4_stid *stid; |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 822 | |
Kinglong Mee | d19fb70 | 2017-01-18 19:04:42 +0800 | [diff] [blame] | 823 | stid = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_ol_stateid); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 824 | if (!stid) |
| 825 | return NULL; |
| 826 | |
Kinglong Mee | d19fb70 | 2017-01-18 19:04:42 +0800 | [diff] [blame] | 827 | return openlockstateid(stid); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 828 | } |
| 829 | |
| 830 | static void nfs4_free_deleg(struct nfs4_stid *stid) |
| 831 | { |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 832 | kmem_cache_free(deleg_slab, stid); |
| 833 | atomic_long_dec(&num_delegations); |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 834 | } |
| 835 | |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 836 | /* |
| 837 | * When we recall a delegation, we should be careful not to hand it |
| 838 | * out again straight away. |
| 839 | * To ensure this we keep a pair of bloom filters ('new' and 'old') |
| 840 | * in which the filehandles of recalled delegations are "stored". |
| 841 | * If a filehandle appear in either filter, a delegation is blocked. |
| 842 | * When a delegation is recalled, the filehandle is stored in the "new" |
| 843 | * filter. |
| 844 | * Every 30 seconds we swap the filters and clear the "new" one, |
| 845 | * unless both are empty of course. |
| 846 | * |
| 847 | * Each filter is 256 bits. We hash the filehandle to 32bit and use the |
| 848 | * low 3 bytes as hash-table indices. |
| 849 | * |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 850 | * 'blocked_delegations_lock', which is always taken in block_delegations(), |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 851 | * is used to manage concurrent access. Testing does not need the lock |
| 852 | * except when swapping the two filters. |
| 853 | */ |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 854 | static DEFINE_SPINLOCK(blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 855 | static struct bloom_pair { |
| 856 | int entries, old_entries; |
| 857 | time_t swap_time; |
| 858 | int new; /* index into 'set' */ |
| 859 | DECLARE_BITMAP(set[2], 256); |
| 860 | } blocked_delegations; |
| 861 | |
| 862 | static int delegation_blocked(struct knfsd_fh *fh) |
| 863 | { |
| 864 | u32 hash; |
| 865 | struct bloom_pair *bd = &blocked_delegations; |
| 866 | |
| 867 | if (bd->entries == 0) |
| 868 | return 0; |
| 869 | if (seconds_since_boot() - bd->swap_time > 30) { |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 870 | spin_lock(&blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 871 | if (seconds_since_boot() - bd->swap_time > 30) { |
| 872 | bd->entries -= bd->old_entries; |
| 873 | bd->old_entries = bd->entries; |
| 874 | memset(bd->set[bd->new], 0, |
| 875 | sizeof(bd->set[0])); |
| 876 | bd->new = 1-bd->new; |
| 877 | bd->swap_time = seconds_since_boot(); |
| 878 | } |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 879 | spin_unlock(&blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 880 | } |
Daniel Borkmann | 8754589 | 2014-12-10 16:33:11 +0100 | [diff] [blame] | 881 | hash = jhash(&fh->fh_base, fh->fh_size, 0); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 882 | if (test_bit(hash&255, bd->set[0]) && |
| 883 | test_bit((hash>>8)&255, bd->set[0]) && |
| 884 | test_bit((hash>>16)&255, bd->set[0])) |
| 885 | return 1; |
| 886 | |
| 887 | if (test_bit(hash&255, bd->set[1]) && |
| 888 | test_bit((hash>>8)&255, bd->set[1]) && |
| 889 | test_bit((hash>>16)&255, bd->set[1])) |
| 890 | return 1; |
| 891 | |
| 892 | return 0; |
| 893 | } |
| 894 | |
| 895 | static void block_delegations(struct knfsd_fh *fh) |
| 896 | { |
| 897 | u32 hash; |
| 898 | struct bloom_pair *bd = &blocked_delegations; |
| 899 | |
Daniel Borkmann | 8754589 | 2014-12-10 16:33:11 +0100 | [diff] [blame] | 900 | hash = jhash(&fh->fh_base, fh->fh_size, 0); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 901 | |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 902 | spin_lock(&blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 903 | __set_bit(hash&255, bd->set[bd->new]); |
| 904 | __set_bit((hash>>8)&255, bd->set[bd->new]); |
| 905 | __set_bit((hash>>16)&255, bd->set[bd->new]); |
| 906 | if (bd->entries == 0) |
| 907 | bd->swap_time = seconds_since_boot(); |
| 908 | bd->entries += 1; |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 909 | spin_unlock(&blocked_delegations_lock); |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 910 | } |
| 911 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | static struct nfs4_delegation * |
J. Bruce Fields | 86d29b1 | 2018-02-21 15:27:28 -0500 | [diff] [blame] | 913 | alloc_init_deleg(struct nfs4_client *clp, struct nfs4_file *fp, |
| 914 | struct svc_fh *current_fh, |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 915 | struct nfs4_clnt_odstate *odstate) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | { |
| 917 | struct nfs4_delegation *dp; |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 918 | long n; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 919 | |
| 920 | dprintk("NFSD alloc_init_deleg\n"); |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 921 | n = atomic_long_inc_return(&num_delegations); |
| 922 | if (n < 0 || n > max_delegations) |
| 923 | goto out_dec; |
NeilBrown | 6282cd5 | 2014-06-04 17:39:26 +1000 | [diff] [blame] | 924 | if (delegation_blocked(¤t_fh->fh_handle)) |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 925 | goto out_dec; |
Kinglong Mee | d19fb70 | 2017-01-18 19:04:42 +0800 | [diff] [blame] | 926 | dp = delegstateid(nfs4_alloc_stid(clp, deleg_slab, nfs4_free_deleg)); |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 927 | if (dp == NULL) |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 928 | goto out_dec; |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 929 | |
J. Bruce Fields | 2a74aba | 2011-09-23 17:20:02 -0400 | [diff] [blame] | 930 | /* |
| 931 | * delegation seqid's are never incremented. The 4.1 special |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 932 | * meaning of seqid 0 isn't meaningful, really, but let's avoid |
| 933 | * 0 anyway just for consistency and use 1: |
J. Bruce Fields | 2a74aba | 2011-09-23 17:20:02 -0400 | [diff] [blame] | 934 | */ |
| 935 | dp->dl_stid.sc_stateid.si_generation = 1; |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 936 | INIT_LIST_HEAD(&dp->dl_perfile); |
| 937 | INIT_LIST_HEAD(&dp->dl_perclnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | INIT_LIST_HEAD(&dp->dl_recall_lru); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 939 | dp->dl_clnt_odstate = odstate; |
| 940 | get_clnt_odstate(odstate); |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 941 | dp->dl_type = NFS4_OPEN_DELEGATE_READ; |
Christoph Hellwig | f0b5de1 | 2014-09-24 12:19:18 +0200 | [diff] [blame] | 942 | dp->dl_retries = 1; |
| 943 | nfsd4_init_cb(&dp->dl_recall, dp->dl_stid.sc_client, |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 944 | &nfsd4_cb_recall_ops, NFSPROC4_CLNT_CB_RECALL); |
J. Bruce Fields | 86d29b1 | 2018-02-21 15:27:28 -0500 | [diff] [blame] | 945 | get_nfs4_file(fp); |
| 946 | dp->dl_stid.sc_file = fp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | return dp; |
Trond Myklebust | 02a3508 | 2014-07-25 07:34:22 -0400 | [diff] [blame] | 948 | out_dec: |
| 949 | atomic_long_dec(&num_delegations); |
| 950 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | void |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 954 | nfs4_put_stid(struct nfs4_stid *s) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 956 | struct nfs4_file *fp = s->sc_file; |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 957 | struct nfs4_client *clp = s->sc_client; |
| 958 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 959 | might_lock(&clp->cl_lock); |
| 960 | |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 961 | if (!refcount_dec_and_lock(&s->sc_count, &clp->cl_lock)) { |
Jeff Layton | b401be22 | 2014-07-29 21:34:33 -0400 | [diff] [blame] | 962 | wake_up_all(&close_wq); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 963 | return; |
Jeff Layton | b401be22 | 2014-07-29 21:34:33 -0400 | [diff] [blame] | 964 | } |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 965 | idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id); |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame^] | 966 | nfs4_free_cpntf_statelist(clp->net, s); |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 967 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 968 | s->sc_free(s); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 969 | if (fp) |
| 970 | put_nfs4_file(fp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | } |
| 972 | |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 973 | void |
| 974 | nfs4_inc_and_copy_stateid(stateid_t *dst, struct nfs4_stid *stid) |
| 975 | { |
| 976 | stateid_t *src = &stid->sc_stateid; |
| 977 | |
| 978 | spin_lock(&stid->sc_lock); |
| 979 | if (unlikely(++src->si_generation == 0)) |
| 980 | src->si_generation = 1; |
| 981 | memcpy(dst, src, sizeof(*dst)); |
| 982 | spin_unlock(&stid->sc_lock); |
| 983 | } |
| 984 | |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 985 | static void put_deleg_file(struct nfs4_file *fp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 986 | { |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 987 | struct nfsd_file *nf = NULL; |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 988 | |
| 989 | spin_lock(&fp->fi_lock); |
| 990 | if (--fp->fi_delegees == 0) |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 991 | swap(nf, fp->fi_deleg_file); |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 992 | spin_unlock(&fp->fi_lock); |
| 993 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 994 | if (nf) |
| 995 | nfsd_file_put(nf); |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | static void nfs4_unlock_deleg_lease(struct nfs4_delegation *dp) |
| 999 | { |
J. Bruce Fields | cba7b3d | 2018-02-15 15:50:49 -0500 | [diff] [blame] | 1000 | struct nfs4_file *fp = dp->dl_stid.sc_file; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 1001 | struct nfsd_file *nf = fp->fi_deleg_file; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 1002 | |
J. Bruce Fields | b8232d3 | 2018-02-15 15:29:15 -0500 | [diff] [blame] | 1003 | WARN_ON_ONCE(!fp->fi_delegees); |
| 1004 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 1005 | vfs_setlease(nf->nf_file, F_UNLCK, NULL, (void **)&dp); |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 1006 | put_deleg_file(fp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | } |
| 1008 | |
J. Bruce Fields | 0af6e69 | 2018-02-21 15:11:03 -0500 | [diff] [blame] | 1009 | static void destroy_unhashed_deleg(struct nfs4_delegation *dp) |
| 1010 | { |
| 1011 | put_clnt_odstate(dp->dl_clnt_odstate); |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 1012 | nfs4_unlock_deleg_lease(dp); |
J. Bruce Fields | 0af6e69 | 2018-02-21 15:11:03 -0500 | [diff] [blame] | 1013 | nfs4_put_stid(&dp->dl_stid); |
| 1014 | } |
| 1015 | |
Christoph Hellwig | cd61c52 | 2014-08-14 08:44:57 +0200 | [diff] [blame] | 1016 | void nfs4_unhash_stid(struct nfs4_stid *s) |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 1017 | { |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 1018 | s->sc_type = 0; |
J. Bruce Fields | 6136d2b | 2011-09-23 16:21:15 -0400 | [diff] [blame] | 1019 | } |
| 1020 | |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1021 | /** |
J. Bruce Fields | 68b18f5 | 2018-02-19 11:38:33 -0500 | [diff] [blame] | 1022 | * nfs4_delegation_exists - Discover if this delegation already exists |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1023 | * @clp: a pointer to the nfs4_client we're granting a delegation to |
| 1024 | * @fp: a pointer to the nfs4_file we're granting a delegation on |
| 1025 | * |
| 1026 | * Return: |
J. Bruce Fields | 68b18f5 | 2018-02-19 11:38:33 -0500 | [diff] [blame] | 1027 | * On success: true iff an existing delegation is found |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1028 | */ |
| 1029 | |
J. Bruce Fields | 68b18f5 | 2018-02-19 11:38:33 -0500 | [diff] [blame] | 1030 | static bool |
| 1031 | nfs4_delegation_exists(struct nfs4_client *clp, struct nfs4_file *fp) |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 1032 | { |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1033 | struct nfs4_delegation *searchdp = NULL; |
| 1034 | struct nfs4_client *searchclp = NULL; |
| 1035 | |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 1036 | lockdep_assert_held(&state_lock); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 1037 | lockdep_assert_held(&fp->fi_lock); |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 1038 | |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1039 | list_for_each_entry(searchdp, &fp->fi_delegations, dl_perfile) { |
| 1040 | searchclp = searchdp->dl_stid.sc_client; |
| 1041 | if (clp == searchclp) { |
Fengguang Wu | 51d87bc | 2018-03-22 13:37:20 +0800 | [diff] [blame] | 1042 | return true; |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1043 | } |
| 1044 | } |
Fengguang Wu | 51d87bc | 2018-03-22 13:37:20 +0800 | [diff] [blame] | 1045 | return false; |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1046 | } |
| 1047 | |
| 1048 | /** |
| 1049 | * hash_delegation_locked - Add a delegation to the appropriate lists |
| 1050 | * @dp: a pointer to the nfs4_delegation we are adding. |
| 1051 | * @fp: a pointer to the nfs4_file we're granting a delegation on |
| 1052 | * |
| 1053 | * Return: |
| 1054 | * On success: NULL if the delegation was successfully hashed. |
| 1055 | * |
| 1056 | * On error: -EAGAIN if one was previously granted to this |
| 1057 | * nfs4_client for this nfs4_file. Delegation is not hashed. |
| 1058 | * |
| 1059 | */ |
| 1060 | |
| 1061 | static int |
| 1062 | hash_delegation_locked(struct nfs4_delegation *dp, struct nfs4_file *fp) |
| 1063 | { |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1064 | struct nfs4_client *clp = dp->dl_stid.sc_client; |
| 1065 | |
| 1066 | lockdep_assert_held(&state_lock); |
| 1067 | lockdep_assert_held(&fp->fi_lock); |
| 1068 | |
J. Bruce Fields | 68b18f5 | 2018-02-19 11:38:33 -0500 | [diff] [blame] | 1069 | if (nfs4_delegation_exists(clp, fp)) |
| 1070 | return -EAGAIN; |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 1071 | refcount_inc(&dp->dl_stid.sc_count); |
Benny Halevy | 3fb87d1 | 2014-05-30 09:09:31 -0400 | [diff] [blame] | 1072 | dp->dl_stid.sc_type = NFS4_DELEG_STID; |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 1073 | list_add(&dp->dl_perfile, &fp->fi_delegations); |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 1074 | list_add(&dp->dl_perclnt, &clp->cl_delegations); |
| 1075 | return 0; |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 1076 | } |
| 1077 | |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 1078 | static bool |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 1079 | unhash_delegation_locked(struct nfs4_delegation *dp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1080 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 1081 | struct nfs4_file *fp = dp->dl_stid.sc_file; |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 1082 | |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 1083 | lockdep_assert_held(&state_lock); |
| 1084 | |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 1085 | if (list_empty(&dp->dl_perfile)) |
| 1086 | return false; |
| 1087 | |
Trond Myklebust | b0fc29d | 2014-07-16 10:31:59 -0400 | [diff] [blame] | 1088 | dp->dl_stid.sc_type = NFS4_CLOSED_DELEG_STID; |
Jeff Layton | d55a166 | 2014-07-22 13:52:06 -0400 | [diff] [blame] | 1089 | /* Ensure that deleg break won't try to requeue it */ |
| 1090 | ++dp->dl_time; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 1091 | spin_lock(&fp->fi_lock); |
Benny Halevy | 931ee56 | 2014-05-30 09:09:27 -0400 | [diff] [blame] | 1092 | list_del_init(&dp->dl_perclnt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1093 | list_del_init(&dp->dl_recall_lru); |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 1094 | list_del_init(&dp->dl_perfile); |
| 1095 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 1096 | return true; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 1097 | } |
| 1098 | |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 1099 | static void destroy_delegation(struct nfs4_delegation *dp) |
| 1100 | { |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 1101 | bool unhashed; |
| 1102 | |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 1103 | spin_lock(&state_lock); |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 1104 | unhashed = unhash_delegation_locked(dp); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 1105 | spin_unlock(&state_lock); |
J. Bruce Fields | 0af6e69 | 2018-02-21 15:11:03 -0500 | [diff] [blame] | 1106 | if (unhashed) |
| 1107 | destroy_unhashed_deleg(dp); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 1108 | } |
| 1109 | |
| 1110 | static void revoke_delegation(struct nfs4_delegation *dp) |
| 1111 | { |
| 1112 | struct nfs4_client *clp = dp->dl_stid.sc_client; |
| 1113 | |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 1114 | WARN_ON(!list_empty(&dp->dl_recall_lru)); |
| 1115 | |
J. Bruce Fields | 0af6e69 | 2018-02-21 15:11:03 -0500 | [diff] [blame] | 1116 | if (clp->cl_minorversion) { |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 1117 | dp->dl_stid.sc_type = NFS4_REVOKED_DELEG_STID; |
J. Bruce Fields | 0af6e69 | 2018-02-21 15:11:03 -0500 | [diff] [blame] | 1118 | refcount_inc(&dp->dl_stid.sc_count); |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 1119 | spin_lock(&clp->cl_lock); |
| 1120 | list_add(&dp->dl_recall_lru, &clp->cl_revoked); |
| 1121 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 1122 | } |
J. Bruce Fields | 0af6e69 | 2018-02-21 15:11:03 -0500 | [diff] [blame] | 1123 | destroy_unhashed_deleg(dp); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 1124 | } |
| 1125 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | /* |
| 1127 | * SETCLIENTID state |
| 1128 | */ |
| 1129 | |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 1130 | static unsigned int clientid_hashval(u32 id) |
| 1131 | { |
| 1132 | return id & CLIENT_HASH_MASK; |
| 1133 | } |
| 1134 | |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 1135 | static unsigned int clientstr_hashval(struct xdr_netobj name) |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 1136 | { |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 1137 | return opaque_hashval(name.data, 8) & CLIENT_HASH_MASK; |
J. Bruce Fields | ddc04c4 | 2011-07-30 23:46:29 -0400 | [diff] [blame] | 1138 | } |
| 1139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1140 | /* |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1141 | * We store the NONE, READ, WRITE, and BOTH bits separately in the |
| 1142 | * st_{access,deny}_bmap field of the stateid, in order to track not |
| 1143 | * only what share bits are currently in force, but also what |
| 1144 | * combinations of share bits previous opens have used. This allows us |
| 1145 | * to enforce the recommendation of rfc 3530 14.2.19 that the server |
| 1146 | * return an error if the client attempt to downgrade to a combination |
| 1147 | * of share bits not explicable by closing some of its previous opens. |
| 1148 | * |
| 1149 | * XXX: This enforcement is actually incomplete, since we don't keep |
| 1150 | * track of access/deny bit combinations; so, e.g., we allow: |
| 1151 | * |
| 1152 | * OPEN allow read, deny write |
| 1153 | * OPEN allow both, deny none |
| 1154 | * DOWNGRADE allow read, deny none |
| 1155 | * |
| 1156 | * which we should reject. |
| 1157 | */ |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 1158 | static unsigned int |
| 1159 | bmap_to_share_mode(unsigned long bmap) { |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1160 | int i; |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 1161 | unsigned int access = 0; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1162 | |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1163 | for (i = 1; i < 4; i++) { |
| 1164 | if (test_bit(i, &bmap)) |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 1165 | access |= i; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1166 | } |
Jeff Layton | 5ae037e | 2012-05-11 09:45:11 -0400 | [diff] [blame] | 1167 | return access; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1168 | } |
| 1169 | |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 1170 | /* set share access for a given stateid */ |
| 1171 | static inline void |
| 1172 | set_access(u32 access, struct nfs4_ol_stateid *stp) |
| 1173 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1174 | unsigned char mask = 1 << access; |
| 1175 | |
| 1176 | WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH); |
| 1177 | stp->st_access_bmap |= mask; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 1178 | } |
| 1179 | |
| 1180 | /* clear share access for a given stateid */ |
| 1181 | static inline void |
| 1182 | clear_access(u32 access, struct nfs4_ol_stateid *stp) |
| 1183 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1184 | unsigned char mask = 1 << access; |
| 1185 | |
| 1186 | WARN_ON_ONCE(access > NFS4_SHARE_ACCESS_BOTH); |
| 1187 | stp->st_access_bmap &= ~mask; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 1188 | } |
| 1189 | |
| 1190 | /* test whether a given stateid has access */ |
| 1191 | static inline bool |
| 1192 | test_access(u32 access, struct nfs4_ol_stateid *stp) |
| 1193 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1194 | unsigned char mask = 1 << access; |
| 1195 | |
| 1196 | return (bool)(stp->st_access_bmap & mask); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 1197 | } |
| 1198 | |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 1199 | /* set share deny for a given stateid */ |
| 1200 | static inline void |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1201 | set_deny(u32 deny, struct nfs4_ol_stateid *stp) |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 1202 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1203 | unsigned char mask = 1 << deny; |
| 1204 | |
| 1205 | WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH); |
| 1206 | stp->st_deny_bmap |= mask; |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 1207 | } |
| 1208 | |
| 1209 | /* clear share deny for a given stateid */ |
| 1210 | static inline void |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1211 | clear_deny(u32 deny, struct nfs4_ol_stateid *stp) |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 1212 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1213 | unsigned char mask = 1 << deny; |
| 1214 | |
| 1215 | WARN_ON_ONCE(deny > NFS4_SHARE_DENY_BOTH); |
| 1216 | stp->st_deny_bmap &= ~mask; |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | /* test whether a given stateid is denying specific access */ |
| 1220 | static inline bool |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1221 | test_deny(u32 deny, struct nfs4_ol_stateid *stp) |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 1222 | { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 1223 | unsigned char mask = 1 << deny; |
| 1224 | |
| 1225 | return (bool)(stp->st_deny_bmap & mask); |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1226 | } |
| 1227 | |
| 1228 | static int nfs4_access_to_omode(u32 access) |
| 1229 | { |
J. Bruce Fields | 8f34a43 | 2010-09-02 15:23:16 -0400 | [diff] [blame] | 1230 | switch (access & NFS4_SHARE_ACCESS_BOTH) { |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1231 | case NFS4_SHARE_ACCESS_READ: |
| 1232 | return O_RDONLY; |
| 1233 | case NFS4_SHARE_ACCESS_WRITE: |
| 1234 | return O_WRONLY; |
| 1235 | case NFS4_SHARE_ACCESS_BOTH: |
| 1236 | return O_RDWR; |
| 1237 | } |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 1238 | WARN_ON_ONCE(1); |
| 1239 | return O_RDONLY; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 1240 | } |
| 1241 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 1242 | /* |
| 1243 | * A stateid that had a deny mode associated with it is being released |
| 1244 | * or downgraded. Recalculate the deny mode on the file. |
| 1245 | */ |
| 1246 | static void |
| 1247 | recalculate_deny_mode(struct nfs4_file *fp) |
| 1248 | { |
| 1249 | struct nfs4_ol_stateid *stp; |
| 1250 | |
| 1251 | spin_lock(&fp->fi_lock); |
| 1252 | fp->fi_share_deny = 0; |
| 1253 | list_for_each_entry(stp, &fp->fi_stateids, st_perfile) |
| 1254 | fp->fi_share_deny |= bmap_to_share_mode(stp->st_deny_bmap); |
| 1255 | spin_unlock(&fp->fi_lock); |
| 1256 | } |
| 1257 | |
| 1258 | static void |
| 1259 | reset_union_bmap_deny(u32 deny, struct nfs4_ol_stateid *stp) |
| 1260 | { |
| 1261 | int i; |
| 1262 | bool change = false; |
| 1263 | |
| 1264 | for (i = 1; i < 4; i++) { |
| 1265 | if ((i & deny) != i) { |
| 1266 | change = true; |
| 1267 | clear_deny(i, stp); |
| 1268 | } |
| 1269 | } |
| 1270 | |
| 1271 | /* Recalculate per-file deny mode if there was a change */ |
| 1272 | if (change) |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 1273 | recalculate_deny_mode(stp->st_stid.sc_file); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 1274 | } |
| 1275 | |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 1276 | /* release all access and file references for a given stateid */ |
| 1277 | static void |
| 1278 | release_all_access(struct nfs4_ol_stateid *stp) |
| 1279 | { |
| 1280 | int i; |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 1281 | struct nfs4_file *fp = stp->st_stid.sc_file; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 1282 | |
| 1283 | if (fp && stp->st_deny_bmap != 0) |
| 1284 | recalculate_deny_mode(fp); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 1285 | |
| 1286 | for (i = 1; i < 4; i++) { |
| 1287 | if (test_access(i, stp)) |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 1288 | nfs4_file_put_access(stp->st_stid.sc_file, i); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 1289 | clear_access(i, stp); |
| 1290 | } |
| 1291 | } |
| 1292 | |
Kinglong Mee | d50ffde | 2015-07-16 12:05:07 +0800 | [diff] [blame] | 1293 | static inline void nfs4_free_stateowner(struct nfs4_stateowner *sop) |
| 1294 | { |
| 1295 | kfree(sop->so_owner.data); |
| 1296 | sop->so_ops->so_free(sop); |
| 1297 | } |
| 1298 | |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 1299 | static void nfs4_put_stateowner(struct nfs4_stateowner *sop) |
| 1300 | { |
Jeff Layton | a819ecc | 2014-07-29 21:34:38 -0400 | [diff] [blame] | 1301 | struct nfs4_client *clp = sop->so_client; |
| 1302 | |
| 1303 | might_lock(&clp->cl_lock); |
| 1304 | |
| 1305 | if (!atomic_dec_and_lock(&sop->so_count, &clp->cl_lock)) |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 1306 | return; |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 1307 | sop->so_ops->so_unhash(sop); |
Jeff Layton | a819ecc | 2014-07-29 21:34:38 -0400 | [diff] [blame] | 1308 | spin_unlock(&clp->cl_lock); |
Kinglong Mee | d50ffde | 2015-07-16 12:05:07 +0800 | [diff] [blame] | 1309 | nfs4_free_stateowner(sop); |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 1310 | } |
| 1311 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1312 | static bool unhash_ol_stateid(struct nfs4_ol_stateid *stp) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1313 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 1314 | struct nfs4_file *fp = stp->st_stid.sc_file; |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 1315 | |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 1316 | lockdep_assert_held(&stp->st_stateowner->so_client->cl_lock); |
| 1317 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1318 | if (list_empty(&stp->st_perfile)) |
| 1319 | return false; |
| 1320 | |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 1321 | spin_lock(&fp->fi_lock); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1322 | list_del_init(&stp->st_perfile); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 1323 | spin_unlock(&fp->fi_lock); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1324 | list_del(&stp->st_perstateowner); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1325 | return true; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1326 | } |
| 1327 | |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1328 | static void nfs4_free_ol_stateid(struct nfs4_stid *stid) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1329 | { |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1330 | struct nfs4_ol_stateid *stp = openlockstateid(stid); |
J. Bruce Fields | 4665e2b | 2011-09-06 14:50:49 -0400 | [diff] [blame] | 1331 | |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 1332 | put_clnt_odstate(stp->st_clnt_odstate); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1333 | release_all_access(stp); |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 1334 | if (stp->st_stateowner) |
| 1335 | nfs4_put_stateowner(stp->st_stateowner); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 1336 | kmem_cache_free(stateid_slab, stid); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1337 | } |
| 1338 | |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 1339 | static void nfs4_free_lock_stateid(struct nfs4_stid *stid) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1340 | { |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 1341 | struct nfs4_ol_stateid *stp = openlockstateid(stid); |
| 1342 | struct nfs4_lockowner *lo = lockowner(stp->st_stateowner); |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 1343 | struct nfsd_file *nf; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1344 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 1345 | nf = find_any_file(stp->st_stid.sc_file); |
| 1346 | if (nf) { |
| 1347 | get_file(nf->nf_file); |
| 1348 | filp_close(nf->nf_file, (fl_owner_t)lo); |
| 1349 | nfsd_file_put(nf); |
| 1350 | } |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 1351 | nfs4_free_ol_stateid(stid); |
| 1352 | } |
| 1353 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1354 | /* |
| 1355 | * Put the persistent reference to an already unhashed generic stateid, while |
| 1356 | * holding the cl_lock. If it's the last reference, then put it onto the |
| 1357 | * reaplist for later destruction. |
| 1358 | */ |
| 1359 | static void put_ol_stateid_locked(struct nfs4_ol_stateid *stp, |
| 1360 | struct list_head *reaplist) |
| 1361 | { |
| 1362 | struct nfs4_stid *s = &stp->st_stid; |
| 1363 | struct nfs4_client *clp = s->sc_client; |
| 1364 | |
| 1365 | lockdep_assert_held(&clp->cl_lock); |
| 1366 | |
| 1367 | WARN_ON_ONCE(!list_empty(&stp->st_locks)); |
| 1368 | |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 1369 | if (!refcount_dec_and_test(&s->sc_count)) { |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1370 | wake_up_all(&close_wq); |
| 1371 | return; |
| 1372 | } |
| 1373 | |
| 1374 | idr_remove(&clp->cl_stateids, s->sc_stateid.si_opaque.so_id); |
| 1375 | list_add(&stp->st_locks, reaplist); |
| 1376 | } |
| 1377 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1378 | static bool unhash_lock_stateid(struct nfs4_ol_stateid *stp) |
Jeff Layton | 3c1c995 | 2014-07-29 21:34:39 -0400 | [diff] [blame] | 1379 | { |
Chuck Lever | f46c445 | 2016-10-29 18:19:03 -0400 | [diff] [blame] | 1380 | lockdep_assert_held(&stp->st_stid.sc_client->cl_lock); |
Jeff Layton | 3c1c995 | 2014-07-29 21:34:39 -0400 | [diff] [blame] | 1381 | |
| 1382 | list_del_init(&stp->st_locks); |
Christoph Hellwig | cd61c52 | 2014-08-14 08:44:57 +0200 | [diff] [blame] | 1383 | nfs4_unhash_stid(&stp->st_stid); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1384 | return unhash_ol_stateid(stp); |
Jeff Layton | 3c1c995 | 2014-07-29 21:34:39 -0400 | [diff] [blame] | 1385 | } |
| 1386 | |
Jeff Layton | 5adfd88 | 2014-07-29 21:34:31 -0400 | [diff] [blame] | 1387 | static void release_lock_stateid(struct nfs4_ol_stateid *stp) |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 1388 | { |
Chuck Lever | f46c445 | 2016-10-29 18:19:03 -0400 | [diff] [blame] | 1389 | struct nfs4_client *clp = stp->st_stid.sc_client; |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1390 | bool unhashed; |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 1391 | |
Chuck Lever | f46c445 | 2016-10-29 18:19:03 -0400 | [diff] [blame] | 1392 | spin_lock(&clp->cl_lock); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1393 | unhashed = unhash_lock_stateid(stp); |
Chuck Lever | f46c445 | 2016-10-29 18:19:03 -0400 | [diff] [blame] | 1394 | spin_unlock(&clp->cl_lock); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1395 | if (unhashed) |
| 1396 | nfs4_put_stid(&stp->st_stid); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1397 | } |
| 1398 | |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 1399 | static void unhash_lockowner_locked(struct nfs4_lockowner *lo) |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1400 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1401 | struct nfs4_client *clp = lo->lo_owner.so_client; |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 1402 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1403 | lockdep_assert_held(&clp->cl_lock); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 1404 | |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 1405 | list_del_init(&lo->lo_owner.so_strhash); |
| 1406 | } |
| 1407 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1408 | /* |
| 1409 | * Free a list of generic stateids that were collected earlier after being |
| 1410 | * fully unhashed. |
| 1411 | */ |
| 1412 | static void |
| 1413 | free_ol_stateid_reaplist(struct list_head *reaplist) |
| 1414 | { |
| 1415 | struct nfs4_ol_stateid *stp; |
Kinglong Mee | fb94d76 | 2014-08-05 21:20:27 +0800 | [diff] [blame] | 1416 | struct nfs4_file *fp; |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1417 | |
| 1418 | might_sleep(); |
| 1419 | |
| 1420 | while (!list_empty(reaplist)) { |
| 1421 | stp = list_first_entry(reaplist, struct nfs4_ol_stateid, |
| 1422 | st_locks); |
| 1423 | list_del(&stp->st_locks); |
Kinglong Mee | fb94d76 | 2014-08-05 21:20:27 +0800 | [diff] [blame] | 1424 | fp = stp->st_stid.sc_file; |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1425 | stp->st_stid.sc_free(&stp->st_stid); |
Kinglong Mee | fb94d76 | 2014-08-05 21:20:27 +0800 | [diff] [blame] | 1426 | if (fp) |
| 1427 | put_nfs4_file(fp); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1428 | } |
| 1429 | } |
| 1430 | |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 1431 | static void release_open_stateid_locks(struct nfs4_ol_stateid *open_stp, |
| 1432 | struct list_head *reaplist) |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 1433 | { |
| 1434 | struct nfs4_ol_stateid *stp; |
| 1435 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1436 | lockdep_assert_held(&open_stp->st_stid.sc_client->cl_lock); |
| 1437 | |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 1438 | while (!list_empty(&open_stp->st_locks)) { |
| 1439 | stp = list_entry(open_stp->st_locks.next, |
| 1440 | struct nfs4_ol_stateid, st_locks); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1441 | WARN_ON(!unhash_lock_stateid(stp)); |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 1442 | put_ol_stateid_locked(stp, reaplist); |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 1443 | } |
| 1444 | } |
| 1445 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1446 | static bool unhash_open_stateid(struct nfs4_ol_stateid *stp, |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 1447 | struct list_head *reaplist) |
J. Bruce Fields | 2283963 | 2009-01-11 14:27:17 -0500 | [diff] [blame] | 1448 | { |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1449 | bool unhashed; |
| 1450 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1451 | lockdep_assert_held(&stp->st_stid.sc_client->cl_lock); |
| 1452 | |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1453 | unhashed = unhash_ol_stateid(stp); |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 1454 | release_open_stateid_locks(stp, reaplist); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1455 | return unhashed; |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 1456 | } |
| 1457 | |
| 1458 | static void release_open_stateid(struct nfs4_ol_stateid *stp) |
| 1459 | { |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1460 | LIST_HEAD(reaplist); |
| 1461 | |
| 1462 | spin_lock(&stp->st_stid.sc_client->cl_lock); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1463 | if (unhash_open_stateid(stp, &reaplist)) |
| 1464 | put_ol_stateid_locked(stp, &reaplist); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1465 | spin_unlock(&stp->st_stid.sc_client->cl_lock); |
| 1466 | free_ol_stateid_reaplist(&reaplist); |
J. Bruce Fields | 2283963 | 2009-01-11 14:27:17 -0500 | [diff] [blame] | 1467 | } |
| 1468 | |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1469 | static void unhash_openowner_locked(struct nfs4_openowner *oo) |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 1470 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1471 | struct nfs4_client *clp = oo->oo_owner.so_client; |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1472 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1473 | lockdep_assert_held(&clp->cl_lock); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1474 | |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 1475 | list_del_init(&oo->oo_owner.so_strhash); |
| 1476 | list_del_init(&oo->oo_perclient); |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 1477 | } |
| 1478 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1479 | static void release_last_closed_stateid(struct nfs4_openowner *oo) |
| 1480 | { |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 1481 | struct nfsd_net *nn = net_generic(oo->oo_owner.so_client->net, |
| 1482 | nfsd_net_id); |
| 1483 | struct nfs4_ol_stateid *s; |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1484 | |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 1485 | spin_lock(&nn->client_lock); |
| 1486 | s = oo->oo_last_closed_stid; |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1487 | if (s) { |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 1488 | list_del_init(&oo->oo_close_lru); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1489 | oo->oo_last_closed_stid = NULL; |
| 1490 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 1491 | spin_unlock(&nn->client_lock); |
| 1492 | if (s) |
| 1493 | nfs4_put_stid(&s->st_stid); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1494 | } |
| 1495 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1496 | static void release_openowner(struct nfs4_openowner *oo) |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 1497 | { |
| 1498 | struct nfs4_ol_stateid *stp; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1499 | struct nfs4_client *clp = oo->oo_owner.so_client; |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1500 | struct list_head reaplist; |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1501 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1502 | INIT_LIST_HEAD(&reaplist); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1503 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1504 | spin_lock(&clp->cl_lock); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 1505 | unhash_openowner_locked(oo); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1506 | while (!list_empty(&oo->oo_owner.so_stateids)) { |
| 1507 | stp = list_first_entry(&oo->oo_owner.so_stateids, |
| 1508 | struct nfs4_ol_stateid, st_perstateowner); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 1509 | if (unhash_open_stateid(stp, &reaplist)) |
| 1510 | put_ol_stateid_locked(stp, &reaplist); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1511 | } |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1512 | spin_unlock(&clp->cl_lock); |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 1513 | free_ol_stateid_reaplist(&reaplist); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 1514 | release_last_closed_stateid(oo); |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 1515 | nfs4_put_stateowner(&oo->oo_owner); |
J. Bruce Fields | f1d110c | 2009-01-11 14:37:31 -0500 | [diff] [blame] | 1516 | } |
| 1517 | |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1518 | static inline int |
| 1519 | hash_sessionid(struct nfs4_sessionid *sessionid) |
| 1520 | { |
| 1521 | struct nfsd4_sessionid *sid = (struct nfsd4_sessionid *)sessionid; |
| 1522 | |
| 1523 | return sid->sequence % SESSION_HASH_SIZE; |
| 1524 | } |
| 1525 | |
Mark Salter | 135dd00 | 2015-04-06 09:46:00 -0400 | [diff] [blame] | 1526 | #ifdef CONFIG_SUNRPC_DEBUG |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1527 | static inline void |
| 1528 | dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) |
| 1529 | { |
| 1530 | u32 *ptr = (u32 *)(&sessionid->data[0]); |
| 1531 | dprintk("%s: %u:%u:%u:%u\n", fn, ptr[0], ptr[1], ptr[2], ptr[3]); |
| 1532 | } |
Trond Myklebust | 8f199b8 | 2012-03-20 15:11:17 -0400 | [diff] [blame] | 1533 | #else |
| 1534 | static inline void |
| 1535 | dump_sessionid(const char *fn, struct nfs4_sessionid *sessionid) |
| 1536 | { |
| 1537 | } |
| 1538 | #endif |
| 1539 | |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 1540 | /* |
| 1541 | * Bump the seqid on cstate->replay_owner, and clear replay_owner if it |
| 1542 | * won't be used for replay. |
| 1543 | */ |
| 1544 | void nfsd4_bump_seqid(struct nfsd4_compound_state *cstate, __be32 nfserr) |
| 1545 | { |
| 1546 | struct nfs4_stateowner *so = cstate->replay_owner; |
| 1547 | |
| 1548 | if (nfserr == nfserr_replay_me) |
| 1549 | return; |
| 1550 | |
| 1551 | if (!seqid_mutating_err(ntohl(nfserr))) { |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 1552 | nfsd4_cstate_clear_replay(cstate); |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 1553 | return; |
| 1554 | } |
| 1555 | if (!so) |
| 1556 | return; |
| 1557 | if (so->so_is_open_owner) |
| 1558 | release_last_closed_stateid(openowner(so)); |
| 1559 | so->so_seqid++; |
| 1560 | return; |
| 1561 | } |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1562 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1563 | static void |
| 1564 | gen_sessionid(struct nfsd4_session *ses) |
| 1565 | { |
| 1566 | struct nfs4_client *clp = ses->se_client; |
| 1567 | struct nfsd4_sessionid *sid; |
| 1568 | |
| 1569 | sid = (struct nfsd4_sessionid *)ses->se_sessionid.data; |
| 1570 | sid->clientid = clp->cl_clientid; |
| 1571 | sid->sequence = current_sessionid++; |
| 1572 | sid->reserved = 0; |
| 1573 | } |
| 1574 | |
| 1575 | /* |
Andy Adamson | a649637c7 | 2009-08-28 08:45:01 -0400 | [diff] [blame] | 1576 | * The protocol defines ca_maxresponssize_cached to include the size of |
| 1577 | * the rpc header, but all we need to cache is the data starting after |
| 1578 | * the end of the initial SEQUENCE operation--the rest we regenerate |
| 1579 | * each time. Therefore we can advertise a ca_maxresponssize_cached |
| 1580 | * value that is the number of bytes in our cache plus a few additional |
| 1581 | * bytes. In order to stay on the safe side, and not promise more than |
| 1582 | * we can cache, those additional bytes must be the minimum possible: 24 |
| 1583 | * bytes of rpc header (xid through accept state, with AUTH_NULL |
| 1584 | * verifier), 12 for the compound header (with zero-length tag), and 44 |
| 1585 | * for the SEQUENCE op response: |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1586 | */ |
Andy Adamson | a649637c7 | 2009-08-28 08:45:01 -0400 | [diff] [blame] | 1587 | #define NFSD_MIN_HDR_SEQ_SZ (24 + 12 + 44) |
| 1588 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1589 | static void |
| 1590 | free_session_slots(struct nfsd4_session *ses) |
| 1591 | { |
| 1592 | int i; |
| 1593 | |
J. Bruce Fields | 53da6a5 | 2017-10-17 20:38:49 -0400 | [diff] [blame] | 1594 | for (i = 0; i < ses->se_fchannel.maxreqs; i++) { |
| 1595 | free_svc_cred(&ses->se_slots[i]->sl_cred); |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1596 | kfree(ses->se_slots[i]); |
J. Bruce Fields | 53da6a5 | 2017-10-17 20:38:49 -0400 | [diff] [blame] | 1597 | } |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1598 | } |
| 1599 | |
J. Bruce Fields | efe0cb6 | 2009-10-24 20:52:16 -0400 | [diff] [blame] | 1600 | /* |
| 1601 | * We don't actually need to cache the rpc and session headers, so we |
| 1602 | * can allocate a little less for each slot: |
| 1603 | */ |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1604 | static inline u32 slot_bytes(struct nfsd4_channel_attrs *ca) |
J. Bruce Fields | efe0cb6 | 2009-10-24 20:52:16 -0400 | [diff] [blame] | 1605 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1606 | u32 size; |
J. Bruce Fields | efe0cb6 | 2009-10-24 20:52:16 -0400 | [diff] [blame] | 1607 | |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1608 | if (ca->maxresp_cached < NFSD_MIN_HDR_SEQ_SZ) |
| 1609 | size = 0; |
| 1610 | else |
| 1611 | size = ca->maxresp_cached - NFSD_MIN_HDR_SEQ_SZ; |
| 1612 | return size + sizeof(struct nfsd4_slot); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1613 | } |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1614 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1615 | /* |
| 1616 | * XXX: If we run out of reserved DRC memory we could (up to a point) |
| 1617 | * re-negotiate active sessions and reduce their slot usage to make |
Justin P. Mattock | 42b2aa8 | 2011-11-28 20:31:00 -0800 | [diff] [blame] | 1618 | * 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] | 1619 | */ |
NeilBrown | 2030ca5 | 2019-09-20 16:36:45 +1000 | [diff] [blame] | 1620 | 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] | 1621 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1622 | u32 slotsize = slot_bytes(ca); |
| 1623 | u32 num = ca->maxreqs; |
J. Bruce Fields | c54f24e | 2019-02-21 10:47:00 -0500 | [diff] [blame] | 1624 | unsigned long avail, total_avail; |
NeilBrown | 2030ca5 | 2019-09-20 16:36:45 +1000 | [diff] [blame] | 1625 | unsigned int scale_factor; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1626 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1627 | spin_lock(&nfsd_drc_lock); |
NeilBrown | 7f49fd5d | 2019-09-20 16:33:16 +1000 | [diff] [blame] | 1628 | if (nfsd_drc_max_mem > nfsd_drc_mem_used) |
| 1629 | total_avail = nfsd_drc_max_mem - nfsd_drc_mem_used; |
| 1630 | else |
| 1631 | /* We have handed out more space than we chose in |
| 1632 | * set_max_drc() to allow. That isn't really a |
| 1633 | * problem as long as that doesn't make us think we |
| 1634 | * have lots more due to integer overflow. |
| 1635 | */ |
| 1636 | total_avail = 0; |
J. Bruce Fields | c54f24e | 2019-02-21 10:47:00 -0500 | [diff] [blame] | 1637 | avail = min((unsigned long)NFSD_MAX_MEM_PER_SESSION, total_avail); |
J. Bruce Fields | de766e5 | 2017-09-19 19:25:41 -0400 | [diff] [blame] | 1638 | /* |
NeilBrown | 2030ca5 | 2019-09-20 16:36:45 +1000 | [diff] [blame] | 1639 | * Never use more than a fraction of the remaining memory, |
NeilBrown | 7f49fd5d | 2019-09-20 16:33:16 +1000 | [diff] [blame] | 1640 | * unless it's the only way to give this client a slot. |
NeilBrown | 2030ca5 | 2019-09-20 16:36:45 +1000 | [diff] [blame] | 1641 | * The chosen fraction is either 1/8 or 1/number of threads, |
| 1642 | * whichever is smaller. This ensures there are adequate |
| 1643 | * slots to support multiple clients per thread. |
NeilBrown | 7f49fd5d | 2019-09-20 16:33:16 +1000 | [diff] [blame] | 1644 | * Give the client one slot even if that would require |
| 1645 | * over-allocation--it is better than failure. |
J. Bruce Fields | de766e5 | 2017-09-19 19:25:41 -0400 | [diff] [blame] | 1646 | */ |
NeilBrown | 2030ca5 | 2019-09-20 16:36:45 +1000 | [diff] [blame] | 1647 | scale_factor = max_t(unsigned int, 8, nn->nfsd_serv->sv_nrthreads); |
| 1648 | |
| 1649 | avail = clamp_t(unsigned long, avail, slotsize, |
| 1650 | total_avail/scale_factor); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1651 | num = min_t(int, num, avail / slotsize); |
NeilBrown | 7f49fd5d | 2019-09-20 16:33:16 +1000 | [diff] [blame] | 1652 | num = max_t(int, num, 1); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1653 | nfsd_drc_mem_used += num * slotsize; |
| 1654 | spin_unlock(&nfsd_drc_lock); |
| 1655 | |
| 1656 | return num; |
| 1657 | } |
| 1658 | |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1659 | static void nfsd4_put_drc_mem(struct nfsd4_channel_attrs *ca) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1660 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1661 | int slotsize = slot_bytes(ca); |
| 1662 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1663 | spin_lock(&nfsd_drc_lock); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1664 | nfsd_drc_mem_used -= slotsize * ca->maxreqs; |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1665 | spin_unlock(&nfsd_drc_lock); |
| 1666 | } |
| 1667 | |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1668 | static struct nfsd4_session *alloc_session(struct nfsd4_channel_attrs *fattrs, |
| 1669 | struct nfsd4_channel_attrs *battrs) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1670 | { |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1671 | int numslots = fattrs->maxreqs; |
| 1672 | int slotsize = slot_bytes(fattrs); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1673 | struct nfsd4_session *new; |
| 1674 | int mem, i; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1675 | |
J. Bruce Fields | c23753d | 2010-09-27 16:22:30 -0400 | [diff] [blame] | 1676 | 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] | 1677 | + sizeof(struct nfsd4_session) > PAGE_SIZE); |
| 1678 | mem = numslots * sizeof(struct nfsd4_slot *); |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1679 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1680 | new = kzalloc(sizeof(*new) + mem, GFP_KERNEL); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1681 | if (!new) |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1682 | return NULL; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1683 | /* allocate each struct nfsd4_slot and data cache in one piece */ |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1684 | for (i = 0; i < numslots; i++) { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1685 | new->se_slots[i] = kzalloc(slotsize, GFP_KERNEL); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1686 | if (!new->se_slots[i]) |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1687 | goto out_free; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1688 | } |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1689 | |
| 1690 | memcpy(&new->se_fchannel, fattrs, sizeof(struct nfsd4_channel_attrs)); |
| 1691 | memcpy(&new->se_bchannel, battrs, sizeof(struct nfsd4_channel_attrs)); |
| 1692 | |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1693 | return new; |
| 1694 | out_free: |
| 1695 | while (i--) |
| 1696 | kfree(new->se_slots[i]); |
| 1697 | kfree(new); |
| 1698 | return NULL; |
| 1699 | } |
| 1700 | |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1701 | static void free_conn(struct nfsd4_conn *c) |
| 1702 | { |
| 1703 | svc_xprt_put(c->cn_xprt); |
| 1704 | kfree(c); |
| 1705 | } |
| 1706 | |
| 1707 | static void nfsd4_conn_lost(struct svc_xpt_user *u) |
| 1708 | { |
| 1709 | struct nfsd4_conn *c = container_of(u, struct nfsd4_conn, cn_xpt_user); |
| 1710 | struct nfs4_client *clp = c->cn_session->se_client; |
| 1711 | |
| 1712 | spin_lock(&clp->cl_lock); |
| 1713 | if (!list_empty(&c->cn_persession)) { |
| 1714 | list_del(&c->cn_persession); |
| 1715 | free_conn(c); |
| 1716 | } |
J. Bruce Fields | eea4980 | 2010-11-18 08:34:12 -0500 | [diff] [blame] | 1717 | nfsd4_probe_callback(clp); |
J. Bruce Fields | 2e4b723 | 2013-03-08 09:30:43 -0500 | [diff] [blame] | 1718 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1719 | } |
| 1720 | |
J. Bruce Fields | d29c374 | 2010-06-15 17:34:11 -0400 | [diff] [blame] | 1721 | 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] | 1722 | { |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1723 | struct nfsd4_conn *conn; |
| 1724 | |
| 1725 | conn = kmalloc(sizeof(struct nfsd4_conn), GFP_KERNEL); |
| 1726 | if (!conn) |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1727 | return NULL; |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1728 | svc_xprt_get(rqstp->rq_xprt); |
| 1729 | conn->cn_xprt = rqstp->rq_xprt; |
J. Bruce Fields | d29c374 | 2010-06-15 17:34:11 -0400 | [diff] [blame] | 1730 | conn->cn_flags = flags; |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1731 | INIT_LIST_HEAD(&conn->cn_xpt_user.list); |
| 1732 | return conn; |
| 1733 | } |
| 1734 | |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 1735 | static void __nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) |
| 1736 | { |
| 1737 | conn->cn_session = ses; |
| 1738 | list_add(&conn->cn_persession, &ses->se_conns); |
| 1739 | } |
| 1740 | |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1741 | static void nfsd4_hash_conn(struct nfsd4_conn *conn, struct nfsd4_session *ses) |
| 1742 | { |
| 1743 | struct nfs4_client *clp = ses->se_client; |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1744 | |
| 1745 | spin_lock(&clp->cl_lock); |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 1746 | __nfsd4_hash_conn(conn, ses); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1747 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1748 | } |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1749 | |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1750 | static int nfsd4_register_conn(struct nfsd4_conn *conn) |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1751 | { |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1752 | conn->cn_xpt_user.callback = nfsd4_conn_lost; |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1753 | return register_xpt_user(conn->cn_xprt, &conn->cn_xpt_user); |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1754 | } |
| 1755 | |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1756 | 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] | 1757 | { |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1758 | int ret; |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1759 | |
J. Bruce Fields | db90681 | 2010-09-29 15:29:32 -0400 | [diff] [blame] | 1760 | nfsd4_hash_conn(conn, ses); |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 1761 | ret = nfsd4_register_conn(conn); |
| 1762 | if (ret) |
| 1763 | /* oops; xprt is already down: */ |
| 1764 | nfsd4_conn_lost(&conn->cn_xpt_user); |
J. Bruce Fields | 57a3714 | 2014-07-18 15:06:47 -0400 | [diff] [blame] | 1765 | /* We may have gained or lost a callback channel: */ |
| 1766 | nfsd4_probe_callback_sync(ses->se_client); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1767 | } |
| 1768 | |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1769 | 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] | 1770 | { |
| 1771 | u32 dir = NFS4_CDFC4_FORE; |
| 1772 | |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1773 | if (cses->flags & SESSION4_BACK_CHAN) |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 1774 | dir |= NFS4_CDFC4_BACK; |
J. Bruce Fields | e1ff371 | 2012-09-11 17:10:25 -0400 | [diff] [blame] | 1775 | return alloc_conn(rqstp, dir); |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 1776 | } |
| 1777 | |
| 1778 | /* must be called under client_lock */ |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1779 | static void nfsd4_del_conns(struct nfsd4_session *s) |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1780 | { |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 1781 | struct nfs4_client *clp = s->se_client; |
| 1782 | struct nfsd4_conn *c; |
| 1783 | |
| 1784 | spin_lock(&clp->cl_lock); |
| 1785 | while (!list_empty(&s->se_conns)) { |
| 1786 | c = list_first_entry(&s->se_conns, struct nfsd4_conn, cn_persession); |
| 1787 | list_del_init(&c->cn_persession); |
| 1788 | spin_unlock(&clp->cl_lock); |
| 1789 | |
| 1790 | unregister_xpt_user(c->cn_xprt, &c->cn_xpt_user); |
| 1791 | free_conn(c); |
| 1792 | |
| 1793 | spin_lock(&clp->cl_lock); |
| 1794 | } |
| 1795 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1796 | } |
| 1797 | |
J. Bruce Fields | 1377b69 | 2012-09-11 21:42:40 -0400 | [diff] [blame] | 1798 | static void __free_session(struct nfsd4_session *ses) |
| 1799 | { |
J. Bruce Fields | 1377b69 | 2012-09-11 21:42:40 -0400 | [diff] [blame] | 1800 | free_session_slots(ses); |
| 1801 | kfree(ses); |
| 1802 | } |
| 1803 | |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 1804 | static void free_session(struct nfsd4_session *ses) |
Benny Halevy | 508dc6e | 2012-02-23 17:40:52 -0800 | [diff] [blame] | 1805 | { |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1806 | nfsd4_del_conns(ses); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 1807 | nfsd4_put_drc_mem(&ses->se_fchannel); |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1808 | __free_session(ses); |
J. Bruce Fields | a827bcb | 2012-09-12 09:51:34 -0400 | [diff] [blame] | 1809 | } |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 1810 | |
Fengguang Wu | 135ae82 | 2012-11-10 07:20:25 -0500 | [diff] [blame] | 1811 | 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] | 1812 | { |
J. Bruce Fields | a827bcb | 2012-09-12 09:51:34 -0400 | [diff] [blame] | 1813 | int idx; |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1814 | 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] | 1815 | |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1816 | new->se_client = clp; |
| 1817 | gen_sessionid(new); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1818 | |
J. Bruce Fields | c766251 | 2010-06-06 18:12:14 -0400 | [diff] [blame] | 1819 | INIT_LIST_HEAD(&new->se_conns); |
| 1820 | |
J. Bruce Fields | ac7c46f2 | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 1821 | new->se_cb_seq_nr = 1; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1822 | new->se_flags = cses->flags; |
J. Bruce Fields | 8b5ce5c | 2010-10-19 17:31:50 -0400 | [diff] [blame] | 1823 | new->se_cb_prog = cses->callback_prog; |
J. Bruce Fields | c6bb3ca | 2012-11-01 16:31:02 -0400 | [diff] [blame] | 1824 | new->se_cb_sec = cses->cb_sec; |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 1825 | atomic_set(&new->se_ref, 0); |
J. Bruce Fields | 5b6feee | 2010-09-27 17:12:05 -0400 | [diff] [blame] | 1826 | idx = hash_sessionid(&new->se_sessionid); |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1827 | list_add(&new->se_hash, &nn->sessionid_hashtbl[idx]); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1828 | spin_lock(&clp->cl_lock); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1829 | list_add(&new->se_perclnt, &clp->cl_sessions); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1830 | spin_unlock(&clp->cl_lock); |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 1831 | |
Chuck Lever | b0d2e42 | 2014-08-22 15:10:59 -0400 | [diff] [blame] | 1832 | { |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1833 | struct sockaddr *sa = svc_addr(rqstp); |
J. Bruce Fields | dcbeaa6 | 2010-06-15 17:25:45 -0400 | [diff] [blame] | 1834 | /* |
| 1835 | * This is a little silly; with sessions there's no real |
| 1836 | * use for the callback address. Use the peer address |
| 1837 | * as a reasonable default for now, but consider fixing |
| 1838 | * the rpc client not to require an address in the |
| 1839 | * future: |
| 1840 | */ |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1841 | rpc_copy_addr((struct sockaddr *)&clp->cl_cb_conn.cb_addr, sa); |
| 1842 | clp->cl_cb_conn.cb_addrlen = svc_addr_len(sa); |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 1843 | } |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 1844 | } |
| 1845 | |
Benny Halevy | 9089f1b | 2010-05-12 00:12:26 +0300 | [diff] [blame] | 1846 | /* caller must hold client_lock */ |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1847 | static struct nfsd4_session * |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 1848 | __find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net) |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1849 | { |
| 1850 | struct nfsd4_session *elem; |
| 1851 | int idx; |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1852 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1853 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 1854 | lockdep_assert_held(&nn->client_lock); |
| 1855 | |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1856 | dump_sessionid(__func__, sessionid); |
| 1857 | idx = hash_sessionid(sessionid); |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1858 | /* Search in the appropriate list */ |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 1859 | list_for_each_entry(elem, &nn->sessionid_hashtbl[idx], se_hash) { |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1860 | if (!memcmp(elem->se_sessionid.data, sessionid->data, |
| 1861 | NFS4_MAX_SESSIONID_LEN)) { |
| 1862 | return elem; |
| 1863 | } |
| 1864 | } |
| 1865 | |
| 1866 | dprintk("%s: session not found\n", __func__); |
| 1867 | return NULL; |
| 1868 | } |
| 1869 | |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 1870 | static struct nfsd4_session * |
| 1871 | find_in_sessionid_hashtbl(struct nfs4_sessionid *sessionid, struct net *net, |
| 1872 | __be32 *ret) |
| 1873 | { |
| 1874 | struct nfsd4_session *session; |
| 1875 | __be32 status = nfserr_badsession; |
| 1876 | |
| 1877 | session = __find_in_sessionid_hashtbl(sessionid, net); |
| 1878 | if (!session) |
| 1879 | goto out; |
| 1880 | status = nfsd4_get_session_locked(session); |
| 1881 | if (status) |
| 1882 | session = NULL; |
| 1883 | out: |
| 1884 | *ret = status; |
| 1885 | return session; |
| 1886 | } |
| 1887 | |
Benny Halevy | 9089f1b | 2010-05-12 00:12:26 +0300 | [diff] [blame] | 1888 | /* caller must hold client_lock */ |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1889 | static void |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1890 | unhash_session(struct nfsd4_session *ses) |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1891 | { |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 1892 | struct nfs4_client *clp = ses->se_client; |
| 1893 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 1894 | |
| 1895 | lockdep_assert_held(&nn->client_lock); |
| 1896 | |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1897 | list_del(&ses->se_hash); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1898 | spin_lock(&ses->se_client->cl_lock); |
Andy Adamson | 7116ed6 | 2009-04-03 08:27:43 +0300 | [diff] [blame] | 1899 | list_del(&ses->se_perclnt); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 1900 | spin_unlock(&ses->se_client->cl_lock); |
Marc Eshel | 5282fd7 | 2009-04-03 08:27:52 +0300 | [diff] [blame] | 1901 | } |
| 1902 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1903 | /* SETCLIENTID and SETCLIENTID_CONFIRM Helper functions */ |
| 1904 | static int |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 1905 | STALE_CLIENTID(clientid_t *clid, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1906 | { |
J. Bruce Fields | bbc7f33 | 2015-01-20 11:51:26 -0500 | [diff] [blame] | 1907 | /* |
| 1908 | * We're assuming the clid was not given out from a boot |
| 1909 | * precisely 2^32 (about 136 years) before this one. That seems |
| 1910 | * a safe assumption: |
| 1911 | */ |
| 1912 | if (clid->cl_boot == (u32)nn->boot_time) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1913 | return 0; |
Andy Adamson | 60adfc5 | 2009-04-03 08:28:50 +0300 | [diff] [blame] | 1914 | dprintk("NFSD stale clientid (%08x/%08x) boot_time %08lx\n", |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 1915 | clid->cl_boot, clid->cl_id, nn->boot_time); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1916 | return 1; |
| 1917 | } |
| 1918 | |
| 1919 | /* |
| 1920 | * XXX Should we use a slab cache ? |
| 1921 | * This type of memory management is somewhat inefficient, but we use it |
| 1922 | * anyway since SETCLIENTID is not a common operation. |
| 1923 | */ |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1924 | static struct nfs4_client *alloc_client(struct xdr_netobj name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1925 | { |
| 1926 | struct nfs4_client *clp; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1927 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1928 | |
Jeff Layton | 9258a2d | 2018-03-16 09:47:22 -0400 | [diff] [blame] | 1929 | clp = kmem_cache_zalloc(client_slab, GFP_KERNEL); |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 1930 | if (clp == NULL) |
| 1931 | return NULL; |
J. Bruce Fields | 6f4859b | 2019-06-19 14:30:33 -0400 | [diff] [blame] | 1932 | xdr_netobj_dup(&clp->cl_name, &name, GFP_KERNEL); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1933 | if (clp->cl_name.data == NULL) |
| 1934 | goto err_no_name; |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 1935 | clp->cl_ownerstr_hashtbl = kmalloc_array(OWNER_HASH_SIZE, |
| 1936 | sizeof(struct list_head), |
| 1937 | GFP_KERNEL); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1938 | if (!clp->cl_ownerstr_hashtbl) |
| 1939 | goto err_no_hashtbl; |
| 1940 | for (i = 0; i < OWNER_HASH_SIZE; i++) |
| 1941 | INIT_LIST_HEAD(&clp->cl_ownerstr_hashtbl[i]); |
Trond Myklebust | 5694c93 | 2014-04-18 14:43:56 -0400 | [diff] [blame] | 1942 | INIT_LIST_HEAD(&clp->cl_sessions); |
| 1943 | idr_init(&clp->cl_stateids); |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 1944 | atomic_set(&clp->cl_rpc_users, 0); |
Trond Myklebust | 5694c93 | 2014-04-18 14:43:56 -0400 | [diff] [blame] | 1945 | clp->cl_cb_state = NFSD4_CB_UNKNOWN; |
| 1946 | INIT_LIST_HEAD(&clp->cl_idhash); |
| 1947 | INIT_LIST_HEAD(&clp->cl_openowners); |
| 1948 | INIT_LIST_HEAD(&clp->cl_delegations); |
| 1949 | INIT_LIST_HEAD(&clp->cl_lru); |
Trond Myklebust | 5694c93 | 2014-04-18 14:43:56 -0400 | [diff] [blame] | 1950 | INIT_LIST_HEAD(&clp->cl_revoked); |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 1951 | #ifdef CONFIG_NFSD_PNFS |
| 1952 | INIT_LIST_HEAD(&clp->cl_lo_states); |
| 1953 | #endif |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 1954 | INIT_LIST_HEAD(&clp->async_copies); |
| 1955 | spin_lock_init(&clp->async_lock); |
Trond Myklebust | 5694c93 | 2014-04-18 14:43:56 -0400 | [diff] [blame] | 1956 | spin_lock_init(&clp->cl_lock); |
| 1957 | rpc_init_wait_queue(&clp->cl_cb_waitq, "Backchannel slot table"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1958 | return clp; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1959 | err_no_hashtbl: |
| 1960 | kfree(clp->cl_name.data); |
| 1961 | err_no_name: |
Jeff Layton | 9258a2d | 2018-03-16 09:47:22 -0400 | [diff] [blame] | 1962 | kmem_cache_free(client_slab, clp); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 1963 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1964 | } |
| 1965 | |
J. Bruce Fields | 59f8e91 | 2019-03-20 20:03:02 -0400 | [diff] [blame] | 1966 | static void __free_client(struct kref *k) |
| 1967 | { |
J. Bruce Fields | e8a79fb | 2019-03-22 11:11:06 -0400 | [diff] [blame] | 1968 | struct nfsdfs_client *c = container_of(k, struct nfsdfs_client, cl_ref); |
| 1969 | 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] | 1970 | |
| 1971 | free_svc_cred(&clp->cl_cred); |
| 1972 | kfree(clp->cl_ownerstr_hashtbl); |
| 1973 | kfree(clp->cl_name.data); |
J. Bruce Fields | 7912344 | 2019-06-05 12:42:05 -0400 | [diff] [blame] | 1974 | kfree(clp->cl_nii_domain.data); |
| 1975 | kfree(clp->cl_nii_name.data); |
J. Bruce Fields | 59f8e91 | 2019-03-20 20:03:02 -0400 | [diff] [blame] | 1976 | idr_destroy(&clp->cl_stateids); |
J. Bruce Fields | 59f8e91 | 2019-03-20 20:03:02 -0400 | [diff] [blame] | 1977 | kmem_cache_free(client_slab, clp); |
| 1978 | } |
| 1979 | |
YueHaibing | 297e57a | 2019-07-08 15:29:33 +0800 | [diff] [blame] | 1980 | static void drop_client(struct nfs4_client *clp) |
J. Bruce Fields | 59f8e91 | 2019-03-20 20:03:02 -0400 | [diff] [blame] | 1981 | { |
J. Bruce Fields | e8a79fb | 2019-03-22 11:11:06 -0400 | [diff] [blame] | 1982 | kref_put(&clp->cl_nfsdfs.cl_ref, __free_client); |
J. Bruce Fields | 59f8e91 | 2019-03-20 20:03:02 -0400 | [diff] [blame] | 1983 | } |
| 1984 | |
Trond Myklebust | 4dd86e15 | 2014-04-18 14:43:58 -0400 | [diff] [blame] | 1985 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1986 | free_client(struct nfs4_client *clp) |
| 1987 | { |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 1988 | while (!list_empty(&clp->cl_sessions)) { |
| 1989 | struct nfsd4_session *ses; |
| 1990 | ses = list_entry(clp->cl_sessions.next, struct nfsd4_session, |
| 1991 | se_perclnt); |
| 1992 | list_del(&ses->se_perclnt); |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 1993 | WARN_ON_ONCE(atomic_read(&ses->se_ref)); |
| 1994 | free_session(ses); |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 1995 | } |
Trond Myklebust | 4cb57e3 | 2014-04-18 14:43:57 -0400 | [diff] [blame] | 1996 | rpc_destroy_wait_queue(&clp->cl_cb_waitq); |
J. Bruce Fields | 89c905b | 2019-06-19 12:43:11 -0400 | [diff] [blame] | 1997 | if (clp->cl_nfsd_dentry) { |
J. Bruce Fields | e8a79fb | 2019-03-22 11:11:06 -0400 | [diff] [blame] | 1998 | nfsd_client_rmdir(clp->cl_nfsd_dentry); |
J. Bruce Fields | 89c905b | 2019-06-19 12:43:11 -0400 | [diff] [blame] | 1999 | clp->cl_nfsd_dentry = NULL; |
| 2000 | wake_up_all(&expiry_wq); |
| 2001 | } |
J. Bruce Fields | 59f8e91 | 2019-03-20 20:03:02 -0400 | [diff] [blame] | 2002 | drop_client(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2003 | } |
| 2004 | |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 2005 | /* must be called under the client_lock */ |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 2006 | static void |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 2007 | unhash_client_locked(struct nfs4_client *clp) |
| 2008 | { |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 2009 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 2010 | struct nfsd4_session *ses; |
| 2011 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2012 | lockdep_assert_held(&nn->client_lock); |
| 2013 | |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 2014 | /* Mark the client as expired! */ |
| 2015 | clp->cl_time = 0; |
| 2016 | /* Make it invisible */ |
| 2017 | if (!list_empty(&clp->cl_idhash)) { |
| 2018 | list_del_init(&clp->cl_idhash); |
| 2019 | if (test_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags)) |
| 2020 | rb_erase(&clp->cl_namenode, &nn->conf_name_tree); |
| 2021 | else |
| 2022 | rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); |
| 2023 | } |
| 2024 | list_del_init(&clp->cl_lru); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 2025 | spin_lock(&clp->cl_lock); |
J. Bruce Fields | 792c95d | 2010-10-12 19:55:25 -0400 | [diff] [blame] | 2026 | list_for_each_entry(ses, &clp->cl_sessions, se_perclnt) |
| 2027 | list_del_init(&ses->se_hash); |
J. Bruce Fields | 4c64937 | 2010-06-15 14:22:37 -0400 | [diff] [blame] | 2028 | spin_unlock(&clp->cl_lock); |
Benny Halevy | 84d38ac | 2010-05-12 00:13:16 +0300 | [diff] [blame] | 2029 | } |
| 2030 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2031 | static void |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 2032 | unhash_client(struct nfs4_client *clp) |
| 2033 | { |
| 2034 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 2035 | |
| 2036 | spin_lock(&nn->client_lock); |
| 2037 | unhash_client_locked(clp); |
| 2038 | spin_unlock(&nn->client_lock); |
| 2039 | } |
| 2040 | |
Jeff Layton | 97403d9 | 2014-07-30 08:27:12 -0400 | [diff] [blame] | 2041 | static __be32 mark_client_expired_locked(struct nfs4_client *clp) |
| 2042 | { |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 2043 | if (atomic_read(&clp->cl_rpc_users)) |
Jeff Layton | 97403d9 | 2014-07-30 08:27:12 -0400 | [diff] [blame] | 2044 | return nfserr_jukebox; |
| 2045 | unhash_client_locked(clp); |
| 2046 | return nfs_ok; |
| 2047 | } |
| 2048 | |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 2049 | static void |
| 2050 | __destroy_client(struct nfs4_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2051 | { |
Jeff Layton | 68ef3bc | 2018-03-16 11:32:02 -0400 | [diff] [blame] | 2052 | int i; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2053 | struct nfs4_openowner *oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2054 | struct nfs4_delegation *dp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2055 | struct list_head reaplist; |
| 2056 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2057 | INIT_LIST_HEAD(&reaplist); |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 2058 | spin_lock(&state_lock); |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 2059 | while (!list_empty(&clp->cl_delegations)) { |
| 2060 | dp = list_entry(clp->cl_delegations.next, struct nfs4_delegation, dl_perclnt); |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 2061 | WARN_ON(!unhash_delegation_locked(dp)); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 2062 | list_add(&dp->dl_recall_lru, &reaplist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2063 | } |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 2064 | spin_unlock(&state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2065 | while (!list_empty(&reaplist)) { |
| 2066 | dp = list_entry(reaplist.next, struct nfs4_delegation, dl_recall_lru); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 2067 | list_del_init(&dp->dl_recall_lru); |
J. Bruce Fields | 0af6e69 | 2018-02-21 15:11:03 -0500 | [diff] [blame] | 2068 | destroy_unhashed_deleg(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2069 | } |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 2070 | while (!list_empty(&clp->cl_revoked)) { |
Andrew Elble | c876486 | 2015-02-25 17:46:27 -0500 | [diff] [blame] | 2071 | 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] | 2072 | list_del_init(&dp->dl_recall_lru); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 2073 | nfs4_put_stid(&dp->dl_stid); |
Benny Halevy | 956c4fe | 2013-10-29 11:39:12 +0200 | [diff] [blame] | 2074 | } |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 2075 | while (!list_empty(&clp->cl_openowners)) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2076 | oo = list_entry(clp->cl_openowners.next, struct nfs4_openowner, oo_perclient); |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 2077 | nfs4_get_stateowner(&oo->oo_owner); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 2078 | release_openowner(oo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2079 | } |
Jeff Layton | 68ef3bc | 2018-03-16 11:32:02 -0400 | [diff] [blame] | 2080 | for (i = 0; i < OWNER_HASH_SIZE; i++) { |
| 2081 | struct nfs4_stateowner *so, *tmp; |
| 2082 | |
| 2083 | list_for_each_entry_safe(so, tmp, &clp->cl_ownerstr_hashtbl[i], |
| 2084 | so_strhash) { |
| 2085 | /* Should be no openowners at this point */ |
| 2086 | WARN_ON_ONCE(so->so_is_open_owner); |
| 2087 | remove_blocked_locks(lockowner(so)); |
| 2088 | } |
| 2089 | } |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 2090 | nfsd4_return_all_client_layouts(clp); |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 2091 | nfsd4_shutdown_copy(clp); |
J. Bruce Fields | 6ff8da0 | 2010-06-04 20:04:45 -0400 | [diff] [blame] | 2092 | nfsd4_shutdown_callback(clp); |
J. Bruce Fields | 2bf2387 | 2010-03-08 12:37:27 -0500 | [diff] [blame] | 2093 | if (clp->cl_cb_conn.cb_xprt) |
| 2094 | svc_xprt_put(clp->cl_cb_conn.cb_xprt); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 2095 | free_client(clp); |
J. Bruce Fields | 89c905b | 2019-06-19 12:43:11 -0400 | [diff] [blame] | 2096 | wake_up_all(&expiry_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2097 | } |
| 2098 | |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 2099 | static void |
| 2100 | destroy_client(struct nfs4_client *clp) |
| 2101 | { |
| 2102 | unhash_client(clp); |
| 2103 | __destroy_client(clp); |
| 2104 | } |
| 2105 | |
Scott Mayhew | 362063a | 2019-03-26 18:06:28 -0400 | [diff] [blame] | 2106 | static void inc_reclaim_complete(struct nfs4_client *clp) |
| 2107 | { |
| 2108 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 2109 | |
| 2110 | if (!nn->track_reclaim_completes) |
| 2111 | return; |
| 2112 | if (!nfsd4_find_reclaim_client(clp->cl_name, nn)) |
| 2113 | return; |
| 2114 | if (atomic_inc_return(&nn->nr_reclaim_complete) == |
| 2115 | nn->reclaim_str_hashtbl_size) { |
| 2116 | printk(KERN_INFO "NFSD: all clients done reclaiming, ending NFSv4 grace period (net %x)\n", |
| 2117 | clp->net->ns.inum); |
| 2118 | nfsd4_end_grace(nn); |
| 2119 | } |
| 2120 | } |
| 2121 | |
J. Bruce Fields | 0d22f68 | 2012-09-26 11:36:16 -0400 | [diff] [blame] | 2122 | static void expire_client(struct nfs4_client *clp) |
| 2123 | { |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 2124 | unhash_client(clp); |
J. Bruce Fields | 0d22f68 | 2012-09-26 11:36:16 -0400 | [diff] [blame] | 2125 | nfsd4_client_record_remove(clp); |
Trond Myklebust | 4beb345 | 2014-07-30 08:27:02 -0400 | [diff] [blame] | 2126 | __destroy_client(clp); |
J. Bruce Fields | 0d22f68 | 2012-09-26 11:36:16 -0400 | [diff] [blame] | 2127 | } |
| 2128 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 2129 | static void copy_verf(struct nfs4_client *target, nfs4_verifier *source) |
| 2130 | { |
| 2131 | memcpy(target->cl_verifier.data, source->data, |
| 2132 | sizeof(target->cl_verifier.data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2133 | } |
| 2134 | |
J. Bruce Fields | 35bba9a | 2007-11-21 22:07:08 -0500 | [diff] [blame] | 2135 | static void copy_clid(struct nfs4_client *target, struct nfs4_client *source) |
| 2136 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2137 | target->cl_clientid.cl_boot = source->cl_clientid.cl_boot; |
| 2138 | target->cl_clientid.cl_id = source->cl_clientid.cl_id; |
| 2139 | } |
| 2140 | |
J. Bruce Fields | 5004385 | 2015-11-20 15:58:37 -0500 | [diff] [blame] | 2141 | static int copy_cred(struct svc_cred *target, struct svc_cred *source) |
| 2142 | { |
NeilBrown | 2f10fdc | 2017-03-23 16:57:36 +0800 | [diff] [blame] | 2143 | target->cr_principal = kstrdup(source->cr_principal, GFP_KERNEL); |
| 2144 | target->cr_raw_principal = kstrdup(source->cr_raw_principal, |
| 2145 | GFP_KERNEL); |
Chuck Lever | 9abdda5 | 2018-08-16 12:05:59 -0400 | [diff] [blame] | 2146 | target->cr_targ_princ = kstrdup(source->cr_targ_princ, GFP_KERNEL); |
| 2147 | if ((source->cr_principal && !target->cr_principal) || |
| 2148 | (source->cr_raw_principal && !target->cr_raw_principal) || |
| 2149 | (source->cr_targ_princ && !target->cr_targ_princ)) |
NeilBrown | 2f10fdc | 2017-03-23 16:57:36 +0800 | [diff] [blame] | 2150 | return -ENOMEM; |
J. Bruce Fields | 5004385 | 2015-11-20 15:58:37 -0500 | [diff] [blame] | 2151 | |
J. Bruce Fields | d5497fc | 2012-05-14 22:06:49 -0400 | [diff] [blame] | 2152 | target->cr_flavor = source->cr_flavor; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2153 | target->cr_uid = source->cr_uid; |
| 2154 | target->cr_gid = source->cr_gid; |
| 2155 | target->cr_group_info = source->cr_group_info; |
| 2156 | get_group_info(target->cr_group_info); |
J. Bruce Fields | 0dc1531 | 2013-05-14 16:07:13 -0400 | [diff] [blame] | 2157 | target->cr_gss_mech = source->cr_gss_mech; |
| 2158 | if (source->cr_gss_mech) |
| 2159 | gss_mech_get(source->cr_gss_mech); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 2160 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2161 | } |
| 2162 | |
Rasmus Villemoes | ef17af2 | 2014-12-05 16:40:07 +0100 | [diff] [blame] | 2163 | static int |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2164 | compare_blob(const struct xdr_netobj *o1, const struct xdr_netobj *o2) |
| 2165 | { |
Rasmus Villemoes | ef17af2 | 2014-12-05 16:40:07 +0100 | [diff] [blame] | 2166 | if (o1->len < o2->len) |
| 2167 | return -1; |
| 2168 | if (o1->len > o2->len) |
| 2169 | return 1; |
| 2170 | return memcmp(o1->data, o2->data, o1->len); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2171 | } |
| 2172 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2173 | static int |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 2174 | same_verf(nfs4_verifier *v1, nfs4_verifier *v2) |
| 2175 | { |
| 2176 | return 0 == memcmp(v1->data, v2->data, sizeof(v1->data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2177 | } |
| 2178 | |
| 2179 | static int |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 2180 | same_clid(clientid_t *cl1, clientid_t *cl2) |
| 2181 | { |
| 2182 | 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] | 2183 | } |
| 2184 | |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 2185 | static bool groups_equal(struct group_info *g1, struct group_info *g2) |
| 2186 | { |
| 2187 | int i; |
| 2188 | |
| 2189 | if (g1->ngroups != g2->ngroups) |
| 2190 | return false; |
| 2191 | for (i=0; i<g1->ngroups; i++) |
Alexey Dobriyan | 81243ea | 2016-10-07 17:03:12 -0700 | [diff] [blame] | 2192 | if (!gid_eq(g1->gid[i], g2->gid[i])) |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 2193 | return false; |
| 2194 | return true; |
| 2195 | } |
| 2196 | |
J. Bruce Fields | 68eb350 | 2012-08-21 12:48:30 -0400 | [diff] [blame] | 2197 | /* |
| 2198 | * RFC 3530 language requires clid_inuse be returned when the |
| 2199 | * "principal" associated with a requests differs from that previously |
| 2200 | * used. We use uid, gid's, and gss principal string as our best |
| 2201 | * approximation. We also don't want to allow non-gss use of a client |
| 2202 | * established using gss: in theory cr_principal should catch that |
| 2203 | * change, but in practice cr_principal can be null even in the gss case |
| 2204 | * since gssd doesn't always pass down a principal string. |
| 2205 | */ |
| 2206 | static bool is_gss_cred(struct svc_cred *cr) |
| 2207 | { |
| 2208 | /* Is cr_flavor one of the gss "pseudoflavors"?: */ |
| 2209 | return (cr->cr_flavor > RPC_AUTH_MAXFLAVOR); |
| 2210 | } |
| 2211 | |
| 2212 | |
Vivek Trivedi | 5559b50 | 2012-07-24 21:18:20 +0530 | [diff] [blame] | 2213 | static bool |
J. Bruce Fields | 599e0a2 | 2007-07-26 17:04:54 -0400 | [diff] [blame] | 2214 | same_creds(struct svc_cred *cr1, struct svc_cred *cr2) |
| 2215 | { |
J. Bruce Fields | 68eb350 | 2012-08-21 12:48:30 -0400 | [diff] [blame] | 2216 | if ((is_gss_cred(cr1) != is_gss_cred(cr2)) |
Eric W. Biederman | 6fab877 | 2013-02-02 06:53:11 -0800 | [diff] [blame] | 2217 | || (!uid_eq(cr1->cr_uid, cr2->cr_uid)) |
| 2218 | || (!gid_eq(cr1->cr_gid, cr2->cr_gid)) |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 2219 | || !groups_equal(cr1->cr_group_info, cr2->cr_group_info)) |
| 2220 | return false; |
Chuck Lever | 9abdda5 | 2018-08-16 12:05:59 -0400 | [diff] [blame] | 2221 | /* XXX: check that cr_targ_princ fields match ? */ |
J. Bruce Fields | 8fbba96 | 2012-05-14 21:20:54 -0400 | [diff] [blame] | 2222 | if (cr1->cr_principal == cr2->cr_principal) |
| 2223 | return true; |
| 2224 | if (!cr1->cr_principal || !cr2->cr_principal) |
| 2225 | return false; |
Vivek Trivedi | 5559b50 | 2012-07-24 21:18:20 +0530 | [diff] [blame] | 2226 | return 0 == strcmp(cr1->cr_principal, cr2->cr_principal); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2227 | } |
| 2228 | |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2229 | static bool svc_rqst_integrity_protected(struct svc_rqst *rqstp) |
| 2230 | { |
| 2231 | struct svc_cred *cr = &rqstp->rq_cred; |
| 2232 | u32 service; |
| 2233 | |
J. Bruce Fields | c472059 | 2013-08-07 11:41:49 -0400 | [diff] [blame] | 2234 | if (!cr->cr_gss_mech) |
| 2235 | return false; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2236 | service = gss_pseudoflavor_to_service(cr->cr_gss_mech, cr->cr_flavor); |
| 2237 | return service == RPC_GSS_SVC_INTEGRITY || |
| 2238 | service == RPC_GSS_SVC_PRIVACY; |
| 2239 | } |
| 2240 | |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 2241 | 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] | 2242 | { |
| 2243 | struct svc_cred *cr = &rqstp->rq_cred; |
| 2244 | |
| 2245 | if (!cl->cl_mach_cred) |
| 2246 | return true; |
| 2247 | if (cl->cl_cred.cr_gss_mech != cr->cr_gss_mech) |
| 2248 | return false; |
| 2249 | if (!svc_rqst_integrity_protected(rqstp)) |
| 2250 | return false; |
J. Bruce Fields | 414ca01 | 2015-11-20 10:48:02 -0500 | [diff] [blame] | 2251 | if (cl->cl_cred.cr_raw_principal) |
| 2252 | return 0 == strcmp(cl->cl_cred.cr_raw_principal, |
| 2253 | cr->cr_raw_principal); |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 2254 | if (!cr->cr_principal) |
| 2255 | return false; |
| 2256 | return 0 == strcmp(cl->cl_cred.cr_principal, cr->cr_principal); |
| 2257 | } |
| 2258 | |
Jeff Layton | 294ac32 | 2014-07-30 08:27:15 -0400 | [diff] [blame] | 2259 | 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] | 2260 | { |
Chuck Lever | ab4684d | 2012-03-02 17:13:50 -0500 | [diff] [blame] | 2261 | __be32 verf[2]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2262 | |
Jeff Layton | f419992 | 2014-06-17 07:44:11 -0400 | [diff] [blame] | 2263 | /* |
| 2264 | * This is opaque to client, so no need to byte-swap. Use |
| 2265 | * __force to keep sparse happy |
| 2266 | */ |
| 2267 | verf[0] = (__force __be32)get_seconds(); |
Kinglong Mee | 19311aa8 | 2015-07-18 07:33:31 +0800 | [diff] [blame] | 2268 | verf[1] = (__force __be32)nn->clverifier_counter++; |
Chuck Lever | ab4684d | 2012-03-02 17:13:50 -0500 | [diff] [blame] | 2269 | memcpy(clp->cl_confirm.data, verf, sizeof(clp->cl_confirm.data)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2270 | } |
| 2271 | |
Jeff Layton | 294ac32 | 2014-07-30 08:27:15 -0400 | [diff] [blame] | 2272 | static void gen_clid(struct nfs4_client *clp, struct nfsd_net *nn) |
| 2273 | { |
| 2274 | clp->cl_clientid.cl_boot = nn->boot_time; |
| 2275 | clp->cl_clientid.cl_id = nn->clientid_counter++; |
| 2276 | gen_confirm(clp, nn); |
| 2277 | } |
| 2278 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 2279 | static struct nfs4_stid * |
| 2280 | find_stateid_locked(struct nfs4_client *cl, stateid_t *t) |
J. Bruce Fields | 4581d14 | 2011-09-06 14:56:09 -0400 | [diff] [blame] | 2281 | { |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 2282 | struct nfs4_stid *ret; |
| 2283 | |
| 2284 | ret = idr_find(&cl->cl_stateids, t->si_opaque.so_id); |
| 2285 | if (!ret || !ret->sc_type) |
| 2286 | return NULL; |
| 2287 | return ret; |
J. Bruce Fields | 4d71ab8 | 2011-09-06 16:48:57 -0400 | [diff] [blame] | 2288 | } |
| 2289 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 2290 | static struct nfs4_stid * |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 2291 | 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] | 2292 | { |
| 2293 | struct nfs4_stid *s; |
| 2294 | |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 2295 | spin_lock(&cl->cl_lock); |
| 2296 | s = find_stateid_locked(cl, t); |
Trond Myklebust | 2d3f966 | 2014-07-29 21:34:25 -0400 | [diff] [blame] | 2297 | if (s != NULL) { |
| 2298 | if (typemask & s->sc_type) |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 2299 | refcount_inc(&s->sc_count); |
Trond Myklebust | 2d3f966 | 2014-07-29 21:34:25 -0400 | [diff] [blame] | 2300 | else |
| 2301 | s = NULL; |
| 2302 | } |
Jeff Layton | 4770d72 | 2014-07-29 21:34:10 -0400 | [diff] [blame] | 2303 | spin_unlock(&cl->cl_lock); |
| 2304 | return s; |
J. Bruce Fields | 4581d14 | 2011-09-06 14:56:09 -0400 | [diff] [blame] | 2305 | } |
| 2306 | |
J. Bruce Fields | a204f25 | 2019-06-19 12:54:45 -0400 | [diff] [blame] | 2307 | static struct nfs4_client *get_nfsdfs_clp(struct inode *inode) |
| 2308 | { |
| 2309 | struct nfsdfs_client *nc; |
| 2310 | nc = get_nfsdfs_client(inode); |
| 2311 | if (!nc) |
| 2312 | return NULL; |
| 2313 | return container_of(nc, struct nfs4_client, cl_nfsdfs); |
| 2314 | } |
| 2315 | |
J. Bruce Fields | 169319f | 2019-06-19 12:39:46 -0400 | [diff] [blame] | 2316 | static void seq_quote_mem(struct seq_file *m, char *data, int len) |
| 2317 | { |
| 2318 | seq_printf(m, "\""); |
| 2319 | seq_escape_mem_ascii(m, data, len); |
| 2320 | seq_printf(m, "\""); |
| 2321 | } |
| 2322 | |
J. Bruce Fields | 97ad403 | 2019-04-09 15:56:57 -0400 | [diff] [blame] | 2323 | static int client_info_show(struct seq_file *m, void *v) |
| 2324 | { |
| 2325 | struct inode *inode = m->private; |
J. Bruce Fields | 97ad403 | 2019-04-09 15:56:57 -0400 | [diff] [blame] | 2326 | struct nfs4_client *clp; |
| 2327 | u64 clid; |
| 2328 | |
J. Bruce Fields | a204f25 | 2019-06-19 12:54:45 -0400 | [diff] [blame] | 2329 | clp = get_nfsdfs_clp(inode); |
| 2330 | if (!clp) |
J. Bruce Fields | 97ad403 | 2019-04-09 15:56:57 -0400 | [diff] [blame] | 2331 | return -ENXIO; |
J. Bruce Fields | 97ad403 | 2019-04-09 15:56:57 -0400 | [diff] [blame] | 2332 | memcpy(&clid, &clp->cl_clientid, sizeof(clid)); |
| 2333 | seq_printf(m, "clientid: 0x%llx\n", clid); |
J. Bruce Fields | 169319f | 2019-06-19 12:39:46 -0400 | [diff] [blame] | 2334 | seq_printf(m, "address: \"%pISpc\"\n", (struct sockaddr *)&clp->cl_addr); |
| 2335 | seq_printf(m, "name: "); |
| 2336 | seq_quote_mem(m, clp->cl_name.data, clp->cl_name.len); |
| 2337 | seq_printf(m, "\nminor version: %d\n", clp->cl_minorversion); |
J. Bruce Fields | 7912344 | 2019-06-05 12:42:05 -0400 | [diff] [blame] | 2338 | if (clp->cl_nii_domain.data) { |
| 2339 | seq_printf(m, "Implementation domain: "); |
| 2340 | seq_quote_mem(m, clp->cl_nii_domain.data, |
| 2341 | clp->cl_nii_domain.len); |
| 2342 | seq_printf(m, "\nImplementation name: "); |
| 2343 | seq_quote_mem(m, clp->cl_nii_name.data, clp->cl_nii_name.len); |
| 2344 | seq_printf(m, "\nImplementation time: [%ld, %ld]\n", |
| 2345 | clp->cl_nii_time.tv_sec, clp->cl_nii_time.tv_nsec); |
| 2346 | } |
J. Bruce Fields | 97ad403 | 2019-04-09 15:56:57 -0400 | [diff] [blame] | 2347 | drop_client(clp); |
| 2348 | |
| 2349 | return 0; |
| 2350 | } |
| 2351 | |
| 2352 | static int client_info_open(struct inode *inode, struct file *file) |
| 2353 | { |
| 2354 | return single_open(file, client_info_show, inode); |
| 2355 | } |
| 2356 | |
| 2357 | static const struct file_operations client_info_fops = { |
| 2358 | .open = client_info_open, |
| 2359 | .read = seq_read, |
| 2360 | .llseek = seq_lseek, |
| 2361 | .release = single_release, |
| 2362 | }; |
| 2363 | |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2364 | static void *states_start(struct seq_file *s, loff_t *pos) |
| 2365 | __acquires(&clp->cl_lock) |
| 2366 | { |
| 2367 | struct nfs4_client *clp = s->private; |
| 2368 | unsigned long id = *pos; |
| 2369 | void *ret; |
| 2370 | |
| 2371 | spin_lock(&clp->cl_lock); |
| 2372 | ret = idr_get_next_ul(&clp->cl_stateids, &id); |
| 2373 | *pos = id; |
| 2374 | return ret; |
| 2375 | } |
| 2376 | |
| 2377 | static void *states_next(struct seq_file *s, void *v, loff_t *pos) |
| 2378 | { |
| 2379 | struct nfs4_client *clp = s->private; |
| 2380 | unsigned long id = *pos; |
| 2381 | void *ret; |
| 2382 | |
| 2383 | id = *pos; |
| 2384 | id++; |
| 2385 | ret = idr_get_next_ul(&clp->cl_stateids, &id); |
| 2386 | *pos = id; |
| 2387 | return ret; |
| 2388 | } |
| 2389 | |
| 2390 | static void states_stop(struct seq_file *s, void *v) |
| 2391 | __releases(&clp->cl_lock) |
| 2392 | { |
| 2393 | struct nfs4_client *clp = s->private; |
| 2394 | |
| 2395 | spin_unlock(&clp->cl_lock); |
| 2396 | } |
| 2397 | |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 2398 | 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] | 2399 | { |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 2400 | struct inode *inode = f->nf_inode; |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2401 | |
| 2402 | seq_printf(s, "superblock: \"%02x:%02x:%ld\"", |
| 2403 | MAJOR(inode->i_sb->s_dev), |
| 2404 | MINOR(inode->i_sb->s_dev), |
| 2405 | inode->i_ino); |
| 2406 | } |
| 2407 | |
| 2408 | static void nfs4_show_owner(struct seq_file *s, struct nfs4_stateowner *oo) |
| 2409 | { |
| 2410 | seq_printf(s, "owner: "); |
| 2411 | seq_quote_mem(s, oo->so_owner.data, oo->so_owner.len); |
| 2412 | } |
| 2413 | |
| 2414 | static int nfs4_show_open(struct seq_file *s, struct nfs4_stid *st) |
| 2415 | { |
| 2416 | struct nfs4_ol_stateid *ols; |
| 2417 | struct nfs4_file *nf; |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 2418 | struct nfsd_file *file; |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2419 | struct nfs4_stateowner *oo; |
| 2420 | unsigned int access, deny; |
| 2421 | |
| 2422 | if (st->sc_type != NFS4_OPEN_STID && st->sc_type != NFS4_LOCK_STID) |
| 2423 | return 0; /* XXX: or SEQ_SKIP? */ |
| 2424 | ols = openlockstateid(st); |
| 2425 | oo = ols->st_stateowner; |
| 2426 | nf = st->sc_file; |
| 2427 | file = find_any_file(nf); |
| 2428 | |
| 2429 | seq_printf(s, "- 0x%16phN: { type: open, ", &st->sc_stateid); |
| 2430 | |
| 2431 | access = bmap_to_share_mode(ols->st_access_bmap); |
| 2432 | deny = bmap_to_share_mode(ols->st_deny_bmap); |
| 2433 | |
J. Bruce Fields | c4b77ed | 2019-10-03 12:33:08 -0400 | [diff] [blame] | 2434 | seq_printf(s, "access: %s%s, ", |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2435 | access & NFS4_SHARE_ACCESS_READ ? "r" : "-", |
| 2436 | access & NFS4_SHARE_ACCESS_WRITE ? "w" : "-"); |
J. Bruce Fields | c4b77ed | 2019-10-03 12:33:08 -0400 | [diff] [blame] | 2437 | seq_printf(s, "deny: %s%s, ", |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2438 | deny & NFS4_SHARE_ACCESS_READ ? "r" : "-", |
| 2439 | deny & NFS4_SHARE_ACCESS_WRITE ? "w" : "-"); |
| 2440 | |
| 2441 | nfs4_show_superblock(s, file); |
| 2442 | seq_printf(s, ", "); |
| 2443 | nfs4_show_owner(s, oo); |
| 2444 | seq_printf(s, " }\n"); |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 2445 | nfsd_file_put(file); |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2446 | |
| 2447 | return 0; |
| 2448 | } |
| 2449 | |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2450 | static int nfs4_show_lock(struct seq_file *s, struct nfs4_stid *st) |
| 2451 | { |
| 2452 | struct nfs4_ol_stateid *ols; |
| 2453 | struct nfs4_file *nf; |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 2454 | struct nfsd_file *file; |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2455 | struct nfs4_stateowner *oo; |
| 2456 | |
| 2457 | ols = openlockstateid(st); |
| 2458 | oo = ols->st_stateowner; |
| 2459 | nf = st->sc_file; |
| 2460 | file = find_any_file(nf); |
| 2461 | |
| 2462 | seq_printf(s, "- 0x%16phN: { type: lock, ", &st->sc_stateid); |
| 2463 | |
| 2464 | /* |
| 2465 | * Note: a lock stateid isn't really the same thing as a lock, |
| 2466 | * it's the locking state held by one owner on a file, and there |
| 2467 | * may be multiple (or no) lock ranges associated with it. |
| 2468 | * (Same for the matter is true of open stateids.) |
| 2469 | */ |
| 2470 | |
| 2471 | nfs4_show_superblock(s, file); |
| 2472 | /* XXX: open stateid? */ |
| 2473 | seq_printf(s, ", "); |
| 2474 | nfs4_show_owner(s, oo); |
| 2475 | seq_printf(s, " }\n"); |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 2476 | nfsd_file_put(file); |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2477 | |
| 2478 | return 0; |
| 2479 | } |
| 2480 | |
| 2481 | static int nfs4_show_deleg(struct seq_file *s, struct nfs4_stid *st) |
| 2482 | { |
| 2483 | struct nfs4_delegation *ds; |
| 2484 | struct nfs4_file *nf; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 2485 | struct nfsd_file *file; |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2486 | |
| 2487 | ds = delegstateid(st); |
| 2488 | nf = st->sc_file; |
| 2489 | file = nf->fi_deleg_file; |
| 2490 | |
| 2491 | seq_printf(s, "- 0x%16phN: { type: deleg, ", &st->sc_stateid); |
| 2492 | |
| 2493 | /* Kinda dead code as long as we only support read delegs: */ |
| 2494 | seq_printf(s, "access: %s, ", |
| 2495 | ds->dl_type == NFS4_OPEN_DELEGATE_READ ? "r" : "w"); |
| 2496 | |
| 2497 | /* XXX: lease time, whether it's being recalled. */ |
| 2498 | |
| 2499 | nfs4_show_superblock(s, file); |
| 2500 | seq_printf(s, " }\n"); |
| 2501 | |
| 2502 | return 0; |
| 2503 | } |
| 2504 | |
J. Bruce Fields | 0c4b62b | 2019-05-14 15:52:57 -0400 | [diff] [blame] | 2505 | static int nfs4_show_layout(struct seq_file *s, struct nfs4_stid *st) |
| 2506 | { |
| 2507 | struct nfs4_layout_stateid *ls; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 2508 | struct nfsd_file *file; |
J. Bruce Fields | 0c4b62b | 2019-05-14 15:52:57 -0400 | [diff] [blame] | 2509 | |
| 2510 | ls = container_of(st, struct nfs4_layout_stateid, ls_stid); |
| 2511 | file = ls->ls_file; |
| 2512 | |
| 2513 | seq_printf(s, "- 0x%16phN: { type: layout, ", &st->sc_stateid); |
| 2514 | |
| 2515 | /* XXX: What else would be useful? */ |
| 2516 | |
| 2517 | nfs4_show_superblock(s, file); |
| 2518 | seq_printf(s, " }\n"); |
| 2519 | |
| 2520 | return 0; |
| 2521 | } |
| 2522 | |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2523 | static int states_show(struct seq_file *s, void *v) |
| 2524 | { |
| 2525 | struct nfs4_stid *st = v; |
| 2526 | |
| 2527 | switch (st->sc_type) { |
| 2528 | case NFS4_OPEN_STID: |
| 2529 | return nfs4_show_open(s, st); |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2530 | case NFS4_LOCK_STID: |
| 2531 | return nfs4_show_lock(s, st); |
| 2532 | case NFS4_DELEG_STID: |
| 2533 | return nfs4_show_deleg(s, st); |
J. Bruce Fields | 0c4b62b | 2019-05-14 15:52:57 -0400 | [diff] [blame] | 2534 | case NFS4_LAYOUT_STID: |
| 2535 | return nfs4_show_layout(s, st); |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2536 | default: |
| 2537 | return 0; /* XXX: or SEQ_SKIP? */ |
| 2538 | } |
J. Bruce Fields | 16d36e0 | 2019-05-10 15:27:50 -0400 | [diff] [blame] | 2539 | /* XXX: copy stateids? */ |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2540 | } |
| 2541 | |
| 2542 | static struct seq_operations states_seq_ops = { |
| 2543 | .start = states_start, |
| 2544 | .next = states_next, |
| 2545 | .stop = states_stop, |
| 2546 | .show = states_show |
| 2547 | }; |
| 2548 | |
| 2549 | static int client_states_open(struct inode *inode, struct file *file) |
| 2550 | { |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2551 | struct seq_file *s; |
| 2552 | struct nfs4_client *clp; |
| 2553 | int ret; |
| 2554 | |
J. Bruce Fields | a204f25 | 2019-06-19 12:54:45 -0400 | [diff] [blame] | 2555 | clp = get_nfsdfs_clp(inode); |
| 2556 | if (!clp) |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2557 | return -ENXIO; |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2558 | |
| 2559 | ret = seq_open(file, &states_seq_ops); |
| 2560 | if (ret) |
| 2561 | return ret; |
| 2562 | s = file->private_data; |
| 2563 | s->private = clp; |
| 2564 | return 0; |
| 2565 | } |
| 2566 | |
| 2567 | static int client_opens_release(struct inode *inode, struct file *file) |
| 2568 | { |
| 2569 | struct seq_file *m = file->private_data; |
| 2570 | struct nfs4_client *clp = m->private; |
| 2571 | |
| 2572 | /* XXX: alternatively, we could get/drop in seq start/stop */ |
| 2573 | drop_client(clp); |
| 2574 | return 0; |
| 2575 | } |
| 2576 | |
| 2577 | static const struct file_operations client_states_fops = { |
| 2578 | .open = client_states_open, |
| 2579 | .read = seq_read, |
| 2580 | .llseek = seq_lseek, |
| 2581 | .release = client_opens_release, |
| 2582 | }; |
| 2583 | |
J. Bruce Fields | 89c905b | 2019-06-19 12:43:11 -0400 | [diff] [blame] | 2584 | /* |
| 2585 | * Normally we refuse to destroy clients that are in use, but here the |
| 2586 | * administrator is telling us to just do it. We also want to wait |
| 2587 | * so the caller has a guarantee that the client's locks are gone by |
| 2588 | * the time the write returns: |
| 2589 | */ |
YueHaibing | 297e57a | 2019-07-08 15:29:33 +0800 | [diff] [blame] | 2590 | static void force_expire_client(struct nfs4_client *clp) |
J. Bruce Fields | 89c905b | 2019-06-19 12:43:11 -0400 | [diff] [blame] | 2591 | { |
| 2592 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
| 2593 | bool already_expired; |
| 2594 | |
| 2595 | spin_lock(&clp->cl_lock); |
| 2596 | clp->cl_time = 0; |
| 2597 | spin_unlock(&clp->cl_lock); |
| 2598 | |
| 2599 | wait_event(expiry_wq, atomic_read(&clp->cl_rpc_users) == 0); |
| 2600 | spin_lock(&nn->client_lock); |
| 2601 | already_expired = list_empty(&clp->cl_lru); |
| 2602 | if (!already_expired) |
| 2603 | unhash_client_locked(clp); |
| 2604 | spin_unlock(&nn->client_lock); |
| 2605 | |
| 2606 | if (!already_expired) |
| 2607 | expire_client(clp); |
| 2608 | else |
| 2609 | wait_event(expiry_wq, clp->cl_nfsd_dentry == NULL); |
| 2610 | } |
| 2611 | |
| 2612 | static ssize_t client_ctl_write(struct file *file, const char __user *buf, |
| 2613 | size_t size, loff_t *pos) |
| 2614 | { |
| 2615 | char *data; |
| 2616 | struct nfs4_client *clp; |
| 2617 | |
| 2618 | data = simple_transaction_get(file, buf, size); |
| 2619 | if (IS_ERR(data)) |
| 2620 | return PTR_ERR(data); |
| 2621 | if (size != 7 || 0 != memcmp(data, "expire\n", 7)) |
| 2622 | return -EINVAL; |
| 2623 | clp = get_nfsdfs_clp(file_inode(file)); |
| 2624 | if (!clp) |
| 2625 | return -ENXIO; |
| 2626 | force_expire_client(clp); |
| 2627 | drop_client(clp); |
| 2628 | return 7; |
| 2629 | } |
| 2630 | |
| 2631 | static const struct file_operations client_ctl_fops = { |
| 2632 | .write = client_ctl_write, |
| 2633 | .release = simple_transaction_release, |
| 2634 | }; |
| 2635 | |
J. Bruce Fields | 97ad403 | 2019-04-09 15:56:57 -0400 | [diff] [blame] | 2636 | static const struct tree_descr client_files[] = { |
| 2637 | [0] = {"info", &client_info_fops, S_IRUSR}, |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2638 | [1] = {"states", &client_states_fops, S_IRUSR}, |
J. Bruce Fields | 89c905b | 2019-06-19 12:43:11 -0400 | [diff] [blame] | 2639 | [2] = {"ctl", &client_ctl_fops, S_IRUSR|S_IWUSR}, |
J. Bruce Fields | 78599c4 | 2019-04-22 15:26:23 -0400 | [diff] [blame] | 2640 | [3] = {""}, |
J. Bruce Fields | 97ad403 | 2019-04-09 15:56:57 -0400 | [diff] [blame] | 2641 | }; |
| 2642 | |
Jeff Layton | 2216d44 | 2012-11-12 15:00:57 -0500 | [diff] [blame] | 2643 | static struct nfs4_client *create_client(struct xdr_netobj name, |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2644 | struct svc_rqst *rqstp, nfs4_verifier *verf) |
| 2645 | { |
| 2646 | struct nfs4_client *clp; |
| 2647 | struct sockaddr *sa = svc_addr(rqstp); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 2648 | int ret; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2649 | struct net *net = SVC_NET(rqstp); |
J. Bruce Fields | e8a79fb | 2019-03-22 11:11:06 -0400 | [diff] [blame] | 2650 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2651 | |
| 2652 | clp = alloc_client(name); |
| 2653 | if (clp == NULL) |
| 2654 | return NULL; |
| 2655 | |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 2656 | ret = copy_cred(&clp->cl_cred, &rqstp->rq_cred); |
| 2657 | if (ret) { |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 2658 | free_client(clp); |
J. Bruce Fields | 03a4e1f | 2012-05-14 19:55:22 -0400 | [diff] [blame] | 2659 | return NULL; |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2660 | } |
J. Bruce Fields | e8a79fb | 2019-03-22 11:11:06 -0400 | [diff] [blame] | 2661 | gen_clid(clp, nn); |
| 2662 | kref_init(&clp->cl_nfsdfs.cl_ref); |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 2663 | nfsd4_init_cb(&clp->cl_cb_null, clp, NULL, NFSPROC4_CLNT_CB_NULL); |
Benny Halevy | 07cd490 | 2010-05-12 00:13:41 +0300 | [diff] [blame] | 2664 | clp->cl_time = get_seconds(); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2665 | clear_bit(0, &clp->cl_cb_slot_busy); |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2666 | copy_verf(clp, verf); |
J. Bruce Fields | 3bade24 | 2019-05-14 21:38:11 -0400 | [diff] [blame] | 2667 | memcpy(&clp->cl_addr, sa, sizeof(struct sockaddr_storage)); |
J. Bruce Fields | edd7678 | 2010-06-14 22:26:31 -0400 | [diff] [blame] | 2668 | clp->cl_cb_session = NULL; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2669 | clp->net = net; |
J. Bruce Fields | e8a79fb | 2019-03-22 11:11:06 -0400 | [diff] [blame] | 2670 | clp->cl_nfsd_dentry = nfsd_client_mkdir(nn, &clp->cl_nfsdfs, |
J. Bruce Fields | 97ad403 | 2019-04-09 15:56:57 -0400 | [diff] [blame] | 2671 | clp->cl_clientid.cl_id - nn->clientid_base, |
| 2672 | client_files); |
J. Bruce Fields | e8a79fb | 2019-03-22 11:11:06 -0400 | [diff] [blame] | 2673 | if (!clp->cl_nfsd_dentry) { |
| 2674 | free_client(clp); |
| 2675 | return NULL; |
| 2676 | } |
Ricardo Labiaga | b09333c | 2009-09-10 12:27:34 +0300 | [diff] [blame] | 2677 | return clp; |
| 2678 | } |
| 2679 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 2680 | static void |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2681 | add_clp_to_name_tree(struct nfs4_client *new_clp, struct rb_root *root) |
| 2682 | { |
| 2683 | struct rb_node **new = &(root->rb_node), *parent = NULL; |
| 2684 | struct nfs4_client *clp; |
| 2685 | |
| 2686 | while (*new) { |
| 2687 | clp = rb_entry(*new, struct nfs4_client, cl_namenode); |
| 2688 | parent = *new; |
| 2689 | |
| 2690 | if (compare_blob(&clp->cl_name, &new_clp->cl_name) > 0) |
| 2691 | new = &((*new)->rb_left); |
| 2692 | else |
| 2693 | new = &((*new)->rb_right); |
| 2694 | } |
| 2695 | |
| 2696 | rb_link_node(&new_clp->cl_namenode, parent, new); |
| 2697 | rb_insert_color(&new_clp->cl_namenode, root); |
| 2698 | } |
| 2699 | |
| 2700 | static struct nfs4_client * |
| 2701 | find_clp_in_name_tree(struct xdr_netobj *name, struct rb_root *root) |
| 2702 | { |
Rasmus Villemoes | ef17af2 | 2014-12-05 16:40:07 +0100 | [diff] [blame] | 2703 | int cmp; |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2704 | struct rb_node *node = root->rb_node; |
| 2705 | struct nfs4_client *clp; |
| 2706 | |
| 2707 | while (node) { |
| 2708 | clp = rb_entry(node, struct nfs4_client, cl_namenode); |
| 2709 | cmp = compare_blob(&clp->cl_name, name); |
| 2710 | if (cmp > 0) |
| 2711 | node = node->rb_left; |
| 2712 | else if (cmp < 0) |
| 2713 | node = node->rb_right; |
| 2714 | else |
| 2715 | return clp; |
| 2716 | } |
| 2717 | return NULL; |
| 2718 | } |
| 2719 | |
| 2720 | static void |
| 2721 | add_to_unconfirmed(struct nfs4_client *clp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2722 | { |
| 2723 | unsigned int idhashval; |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 2724 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2725 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2726 | lockdep_assert_held(&nn->client_lock); |
| 2727 | |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2728 | clear_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2729 | add_clp_to_name_tree(clp, &nn->unconf_name_tree); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2730 | idhashval = clientid_hashval(clp->cl_clientid.cl_id); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 2731 | list_add(&clp->cl_idhash, &nn->unconf_id_hashtbl[idhashval]); |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2732 | renew_client_locked(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2733 | } |
| 2734 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 2735 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2736 | move_to_confirmed(struct nfs4_client *clp) |
| 2737 | { |
| 2738 | unsigned int idhashval = clientid_hashval(clp->cl_clientid.cl_id); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2739 | struct nfsd_net *nn = net_generic(clp->net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2740 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2741 | lockdep_assert_held(&nn->client_lock); |
| 2742 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2743 | dprintk("NFSD: move_to_confirm nfs4_client %p\n", clp); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 2744 | list_move(&clp->cl_idhash, &nn->conf_id_hashtbl[idhashval]); |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2745 | rb_erase(&clp->cl_namenode, &nn->unconf_name_tree); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2746 | add_clp_to_name_tree(clp, &nn->conf_name_tree); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 2747 | set_bit(NFSD4_CLIENT_CONFIRMED, &clp->cl_flags); |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2748 | renew_client_locked(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2749 | } |
| 2750 | |
| 2751 | static struct nfs4_client * |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2752 | 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] | 2753 | { |
| 2754 | struct nfs4_client *clp; |
| 2755 | unsigned int idhashval = clientid_hashval(clid->cl_id); |
| 2756 | |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2757 | list_for_each_entry(clp, &tbl[idhashval], cl_idhash) { |
J. Bruce Fields | a50d2ad | 2011-10-12 16:24:27 -0400 | [diff] [blame] | 2758 | if (same_clid(&clp->cl_clientid, clid)) { |
J. Bruce Fields | d15c077 | 2012-09-13 16:19:31 -0400 | [diff] [blame] | 2759 | if ((bool)clp->cl_minorversion != sessions) |
| 2760 | return NULL; |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 2761 | renew_client_locked(clp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2762 | return clp; |
J. Bruce Fields | a50d2ad | 2011-10-12 16:24:27 -0400 | [diff] [blame] | 2763 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2764 | } |
| 2765 | return NULL; |
| 2766 | } |
| 2767 | |
| 2768 | static struct nfs4_client * |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2769 | find_confirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) |
| 2770 | { |
| 2771 | struct list_head *tbl = nn->conf_id_hashtbl; |
| 2772 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2773 | lockdep_assert_held(&nn->client_lock); |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2774 | return find_client_in_id_table(tbl, clid, sessions); |
| 2775 | } |
| 2776 | |
| 2777 | static struct nfs4_client * |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 2778 | find_unconfirmed_client(clientid_t *clid, bool sessions, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2779 | { |
J. Bruce Fields | bfa85e83 | 2013-03-14 18:24:52 -0400 | [diff] [blame] | 2780 | struct list_head *tbl = nn->unconf_id_hashtbl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2784 | } |
| 2785 | |
J. Bruce Fields | 6e5f15c | 2010-11-24 17:17:34 -0500 | [diff] [blame] | 2786 | static bool clp_used_exchangeid(struct nfs4_client *clp) |
Andy Adamson | a1bcecd | 2009-04-03 08:28:05 +0300 | [diff] [blame] | 2787 | { |
J. Bruce Fields | 6e5f15c | 2010-11-24 17:17:34 -0500 | [diff] [blame] | 2788 | return clp->cl_exchange_flags != 0; |
J. Bruce Fields | e203d50 | 2010-11-24 17:30:54 -0500 | [diff] [blame] | 2789 | } |
Andy Adamson | a1bcecd | 2009-04-03 08:28:05 +0300 | [diff] [blame] | 2790 | |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2791 | static struct nfs4_client * |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2792 | find_confirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2793 | { |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2794 | lockdep_assert_held(&nn->client_lock); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 2795 | return find_clp_in_name_tree(name, &nn->conf_name_tree); |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2796 | } |
| 2797 | |
| 2798 | static struct nfs4_client * |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2799 | find_unconfirmed_client_by_name(struct xdr_netobj *name, struct nfsd_net *nn) |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2800 | { |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 2801 | lockdep_assert_held(&nn->client_lock); |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 2802 | return find_clp_in_name_tree(name, &nn->unconf_name_tree); |
NeilBrown | 28ce605 | 2005-06-23 22:03:56 -0700 | [diff] [blame] | 2803 | } |
| 2804 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 2805 | static void |
Takuma Umeya | 6f3d772 | 2010-12-15 14:09:01 +0900 | [diff] [blame] | 2806 | 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] | 2807 | { |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2808 | struct nfs4_cb_conn *conn = &clp->cl_cb_conn; |
Takuma Umeya | 6f3d772 | 2010-12-15 14:09:01 +0900 | [diff] [blame] | 2809 | struct sockaddr *sa = svc_addr(rqstp); |
| 2810 | u32 scopeid = rpc_get_scope_id(sa); |
Jeff Layton | 7077ecb | 2009-08-14 12:57:58 -0400 | [diff] [blame] | 2811 | unsigned short expected_family; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2812 | |
Jeff Layton | 7077ecb | 2009-08-14 12:57:58 -0400 | [diff] [blame] | 2813 | /* Currently, we only support tcp and tcp6 for the callback channel */ |
| 2814 | if (se->se_callback_netid_len == 3 && |
| 2815 | !memcmp(se->se_callback_netid_val, "tcp", 3)) |
| 2816 | expected_family = AF_INET; |
| 2817 | else if (se->se_callback_netid_len == 4 && |
| 2818 | !memcmp(se->se_callback_netid_val, "tcp6", 4)) |
| 2819 | expected_family = AF_INET6; |
| 2820 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2821 | goto out_err; |
| 2822 | |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 2823 | conn->cb_addrlen = rpc_uaddr2sockaddr(clp->net, se->se_callback_addr_val, |
Jeff Layton | aa9a4ec | 2009-08-14 12:57:57 -0400 | [diff] [blame] | 2824 | se->se_callback_addr_len, |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2825 | (struct sockaddr *)&conn->cb_addr, |
| 2826 | sizeof(conn->cb_addr)); |
Jeff Layton | aa9a4ec | 2009-08-14 12:57:57 -0400 | [diff] [blame] | 2827 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2828 | if (!conn->cb_addrlen || conn->cb_addr.ss_family != expected_family) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2829 | goto out_err; |
Jeff Layton | aa9a4ec | 2009-08-14 12:57:57 -0400 | [diff] [blame] | 2830 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2831 | if (conn->cb_addr.ss_family == AF_INET6) |
| 2832 | ((struct sockaddr_in6 *)&conn->cb_addr)->sin6_scope_id = scopeid; |
Jeff Layton | fbf4665 | 2009-08-14 12:57:59 -0400 | [diff] [blame] | 2833 | |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2834 | conn->cb_prog = se->se_callback_prog; |
| 2835 | conn->cb_ident = se->se_callback_ident; |
Mi Jinlong | 849a1cf | 2011-08-30 17:18:41 +0800 | [diff] [blame] | 2836 | memcpy(&conn->cb_saddr, &rqstp->rq_daddr, rqstp->rq_daddrlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2837 | return; |
| 2838 | out_err: |
J. Bruce Fields | 07263f1 | 2010-05-31 19:09:40 -0400 | [diff] [blame] | 2839 | conn->cb_addr.ss_family = AF_UNSPEC; |
| 2840 | conn->cb_addrlen = 0; |
Rasmus Villemoes | 4ab495b | 2017-02-24 01:15:55 +0100 | [diff] [blame] | 2841 | dprintk("NFSD: this client (clientid %08x/%08x) " |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2842 | "will not receive delegations\n", |
| 2843 | clp->cl_clientid.cl_boot, clp->cl_clientid.cl_id); |
| 2844 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2845 | return; |
| 2846 | } |
| 2847 | |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2848 | /* |
J. Bruce Fields | 067e1ac | 2014-03-21 17:26:44 -0400 | [diff] [blame] | 2849 | * Cache a reply. nfsd4_check_resp_size() has bounded the cache size. |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2850 | */ |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 2851 | static void |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2852 | nfsd4_store_cache_entry(struct nfsd4_compoundres *resp) |
| 2853 | { |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 2854 | struct xdr_buf *buf = resp->xdr.buf; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2855 | struct nfsd4_slot *slot = resp->cstate.slot; |
| 2856 | unsigned int base; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2857 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2858 | dprintk("--> %s slot %p\n", __func__, slot); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2859 | |
J. Bruce Fields | 085def3 | 2017-10-18 16:17:18 -0400 | [diff] [blame] | 2860 | slot->sl_flags |= NFSD4_SLOT_INITIALIZED; |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2861 | slot->sl_opcnt = resp->opcnt; |
| 2862 | slot->sl_status = resp->cstate.status; |
J. Bruce Fields | 53da6a5 | 2017-10-17 20:38:49 -0400 | [diff] [blame] | 2863 | free_svc_cred(&slot->sl_cred); |
| 2864 | copy_cred(&slot->sl_cred, &resp->rqstp->rq_cred); |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 2865 | |
J. Bruce Fields | 085def3 | 2017-10-18 16:17:18 -0400 | [diff] [blame] | 2866 | if (!nfsd4_cache_this(resp)) { |
| 2867 | slot->sl_flags &= ~NFSD4_SLOT_CACHED; |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 2868 | return; |
| 2869 | } |
J. Bruce Fields | 085def3 | 2017-10-18 16:17:18 -0400 | [diff] [blame] | 2870 | slot->sl_flags |= NFSD4_SLOT_CACHED; |
| 2871 | |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 2872 | base = resp->cstate.data_offset; |
| 2873 | slot->sl_datalen = buf->len - base; |
| 2874 | 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] | 2875 | WARN(1, "%s: sessions DRC could not cache compound\n", |
| 2876 | __func__); |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2877 | return; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2878 | } |
| 2879 | |
| 2880 | /* |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2881 | * Encode the replay sequence operation from the slot values. |
| 2882 | * If cachethis is FALSE encode the uncached rep error on the next |
| 2883 | * operation which sets resp->p and increments resp->opcnt for |
| 2884 | * nfs4svc_encode_compoundres. |
| 2885 | * |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2886 | */ |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2887 | static __be32 |
| 2888 | nfsd4_enc_sequence_replay(struct nfsd4_compoundargs *args, |
| 2889 | struct nfsd4_compoundres *resp) |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2890 | { |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2891 | struct nfsd4_op *op; |
| 2892 | struct nfsd4_slot *slot = resp->cstate.slot; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2893 | |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2894 | /* Encode the replayed sequence operation */ |
| 2895 | op = &args->ops[resp->opcnt - 1]; |
| 2896 | nfsd4_encode_operation(resp, op); |
| 2897 | |
J. Bruce Fields | 085def3 | 2017-10-18 16:17:18 -0400 | [diff] [blame] | 2898 | if (slot->sl_flags & NFSD4_SLOT_CACHED) |
| 2899 | return op->status; |
| 2900 | if (args->opcnt == 1) { |
| 2901 | /* |
| 2902 | * The original operation wasn't a solo sequence--we |
| 2903 | * always cache those--so this retry must not match the |
| 2904 | * original: |
| 2905 | */ |
| 2906 | op->status = nfserr_seq_false_retry; |
| 2907 | } else { |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2908 | op = &args->ops[resp->opcnt++]; |
| 2909 | op->status = nfserr_retry_uncached_rep; |
| 2910 | nfsd4_encode_operation(resp, op); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2911 | } |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2912 | return op->status; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2913 | } |
| 2914 | |
| 2915 | /* |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2916 | * The sequence operation is not cached because we can use the slot and |
| 2917 | * session values. |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2918 | */ |
J. Bruce Fields | 3ca2eb9 | 2014-03-21 12:04:21 -0400 | [diff] [blame] | 2919 | static __be32 |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 2920 | nfsd4_replay_cache_entry(struct nfsd4_compoundres *resp, |
| 2921 | struct nfsd4_sequence *seq) |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2922 | { |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2923 | struct nfsd4_slot *slot = resp->cstate.slot; |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 2924 | struct xdr_stream *xdr = &resp->xdr; |
| 2925 | __be32 *p; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2926 | __be32 status; |
| 2927 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2928 | dprintk("--> %s slot %p\n", __func__, slot); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2929 | |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2930 | status = nfsd4_enc_sequence_replay(resp->rqstp->rq_argp, resp); |
J. Bruce Fields | 0da7b19 | 2014-03-21 11:43:34 -0400 | [diff] [blame] | 2931 | if (status) |
Andy Adamson | abfabf8 | 2009-07-23 19:02:18 -0400 | [diff] [blame] | 2932 | return status; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2933 | |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 2934 | p = xdr_reserve_space(xdr, slot->sl_datalen); |
| 2935 | if (!p) { |
| 2936 | WARN_ON_ONCE(1); |
| 2937 | return nfserr_serverfault; |
| 2938 | } |
| 2939 | xdr_encode_opaque_fixed(p, slot->sl_data, slot->sl_datalen); |
| 2940 | xdr_commit_encode(xdr); |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2941 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 2942 | resp->opcnt = slot->sl_opcnt; |
J. Bruce Fields | f523601 | 2014-03-21 17:57:57 -0400 | [diff] [blame] | 2943 | return slot->sl_status; |
Andy Adamson | 074fe89 | 2009-04-03 08:28:15 +0300 | [diff] [blame] | 2944 | } |
| 2945 | |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2946 | /* |
| 2947 | * Set the exchange_id flags returned by the server. |
| 2948 | */ |
| 2949 | static void |
| 2950 | nfsd4_set_ex_flags(struct nfs4_client *new, struct nfsd4_exchange_id *clid) |
| 2951 | { |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 2952 | #ifdef CONFIG_NFSD_PNFS |
| 2953 | new->cl_exchange_flags |= EXCHGID4_FLAG_USE_PNFS_MDS; |
| 2954 | #else |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2955 | new->cl_exchange_flags |= EXCHGID4_FLAG_USE_NON_PNFS; |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 2956 | #endif |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 2957 | |
| 2958 | /* Referrals are supported, Migration is not. */ |
| 2959 | new->cl_exchange_flags |= EXCHGID4_FLAG_SUPP_MOVED_REFER; |
| 2960 | |
| 2961 | /* set the wire flags to return to client. */ |
| 2962 | clid->flags = new->cl_exchange_flags; |
| 2963 | } |
| 2964 | |
J. Bruce Fields | 4eaea13 | 2015-10-15 16:11:01 -0400 | [diff] [blame] | 2965 | static bool client_has_openowners(struct nfs4_client *clp) |
| 2966 | { |
| 2967 | struct nfs4_openowner *oo; |
| 2968 | |
| 2969 | list_for_each_entry(oo, &clp->cl_openowners, oo_perclient) { |
| 2970 | if (!list_empty(&oo->oo_owner.so_stateids)) |
| 2971 | return true; |
| 2972 | } |
| 2973 | return false; |
| 2974 | } |
| 2975 | |
J. Bruce Fields | 631fc9e | 2012-05-14 15:57:23 -0400 | [diff] [blame] | 2976 | static bool client_has_state(struct nfs4_client *clp) |
| 2977 | { |
J. Bruce Fields | 4eaea13 | 2015-10-15 16:11:01 -0400 | [diff] [blame] | 2978 | return client_has_openowners(clp) |
Kinglong Mee | 47e970b | 2015-07-21 13:09:17 +0800 | [diff] [blame] | 2979 | #ifdef CONFIG_NFSD_PNFS |
| 2980 | || !list_empty(&clp->cl_lo_states) |
| 2981 | #endif |
J. Bruce Fields | 6eccece | 2012-05-29 16:37:44 -0400 | [diff] [blame] | 2982 | || !list_empty(&clp->cl_delegations) |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 2983 | || !list_empty(&clp->cl_sessions) |
| 2984 | || !list_empty(&clp->async_copies); |
J. Bruce Fields | 631fc9e | 2012-05-14 15:57:23 -0400 | [diff] [blame] | 2985 | } |
| 2986 | |
J. Bruce Fields | 7912344 | 2019-06-05 12:42:05 -0400 | [diff] [blame] | 2987 | static __be32 copy_impl_id(struct nfs4_client *clp, |
| 2988 | struct nfsd4_exchange_id *exid) |
| 2989 | { |
| 2990 | if (!exid->nii_domain.data) |
| 2991 | return 0; |
| 2992 | xdr_netobj_dup(&clp->cl_nii_domain, &exid->nii_domain, GFP_KERNEL); |
| 2993 | if (!clp->cl_nii_domain.data) |
| 2994 | return nfserr_jukebox; |
| 2995 | xdr_netobj_dup(&clp->cl_nii_name, &exid->nii_name, GFP_KERNEL); |
| 2996 | if (!clp->cl_nii_name.data) |
| 2997 | return nfserr_jukebox; |
| 2998 | clp->cl_nii_time.tv_sec = exid->nii_time.tv_sec; |
| 2999 | clp->cl_nii_time.tv_nsec = exid->nii_time.tv_nsec; |
| 3000 | return 0; |
| 3001 | } |
| 3002 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3003 | __be32 |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3004 | nfsd4_exchange_id(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 3005 | union nfsd4_op_u *u) |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3006 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3007 | struct nfsd4_exchange_id *exid = &u->exchange_id; |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3008 | struct nfs4_client *conf, *new; |
| 3009 | struct nfs4_client *unconf = NULL; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3010 | __be32 status; |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 3011 | char addr_str[INET6_ADDRSTRLEN]; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3012 | nfs4_verifier verf = exid->verifier; |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 3013 | struct sockaddr *sa = svc_addr(rqstp); |
J. Bruce Fields | 83e08fd | 2012-05-14 09:08:10 -0400 | [diff] [blame] | 3014 | bool update = exid->flags & EXCHGID4_FLAG_UPD_CONFIRMED_REC_A; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 3015 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3016 | |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 3017 | rpc_ntop(sa, addr_str, sizeof(addr_str)); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3018 | dprintk("%s rqstp=%p exid=%p clname.len=%u clname.data=%p " |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 3019 | "ip_addr=%s flags %x, spa_how %d\n", |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3020 | __func__, rqstp, exid, exid->clname.len, exid->clname.data, |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 3021 | addr_str, exid->flags, exid->spa_how); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3022 | |
J. Bruce Fields | a084daf | 2011-10-10 15:07:40 -0400 | [diff] [blame] | 3023 | if (exid->flags & ~EXCHGID4_FLAG_MASK_A) |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3024 | return nfserr_inval; |
| 3025 | |
J. Bruce Fields | 50c7b94 | 2015-11-23 15:39:12 -0700 | [diff] [blame] | 3026 | new = create_client(exid->clname, rqstp, &verf); |
| 3027 | if (new == NULL) |
| 3028 | return nfserr_jukebox; |
J. Bruce Fields | 7912344 | 2019-06-05 12:42:05 -0400 | [diff] [blame] | 3029 | status = copy_impl_id(new, exid); |
| 3030 | if (status) |
| 3031 | goto out_nolock; |
J. Bruce Fields | 50c7b94 | 2015-11-23 15:39:12 -0700 | [diff] [blame] | 3032 | |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3033 | switch (exid->spa_how) { |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3034 | case SP4_MACH_CRED: |
Andrew Elble | ed94164 | 2016-06-15 12:52:09 -0400 | [diff] [blame] | 3035 | exid->spo_must_enforce[0] = 0; |
| 3036 | exid->spo_must_enforce[1] = ( |
| 3037 | 1 << (OP_BIND_CONN_TO_SESSION - 32) | |
| 3038 | 1 << (OP_EXCHANGE_ID - 32) | |
| 3039 | 1 << (OP_CREATE_SESSION - 32) | |
| 3040 | 1 << (OP_DESTROY_SESSION - 32) | |
| 3041 | 1 << (OP_DESTROY_CLIENTID - 32)); |
| 3042 | |
| 3043 | exid->spo_must_allow[0] &= (1 << (OP_CLOSE) | |
| 3044 | 1 << (OP_OPEN_DOWNGRADE) | |
| 3045 | 1 << (OP_LOCKU) | |
| 3046 | 1 << (OP_DELEGRETURN)); |
| 3047 | |
| 3048 | exid->spo_must_allow[1] &= ( |
| 3049 | 1 << (OP_TEST_STATEID - 32) | |
| 3050 | 1 << (OP_FREE_STATEID - 32)); |
J. Bruce Fields | 50c7b94 | 2015-11-23 15:39:12 -0700 | [diff] [blame] | 3051 | if (!svc_rqst_integrity_protected(rqstp)) { |
| 3052 | status = nfserr_inval; |
| 3053 | goto out_nolock; |
| 3054 | } |
J. Bruce Fields | 920dd9b | 2015-11-20 16:42:40 -0500 | [diff] [blame] | 3055 | /* |
| 3056 | * Sometimes userspace doesn't give us a principal. |
| 3057 | * Which is a bug, really. Anyway, we can't enforce |
| 3058 | * MACH_CRED in that case, better to give up now: |
| 3059 | */ |
J. Bruce Fields | 414ca01 | 2015-11-20 10:48:02 -0500 | [diff] [blame] | 3060 | if (!new->cl_cred.cr_principal && |
| 3061 | !new->cl_cred.cr_raw_principal) { |
J. Bruce Fields | 920dd9b | 2015-11-20 16:42:40 -0500 | [diff] [blame] | 3062 | status = nfserr_serverfault; |
| 3063 | goto out_nolock; |
| 3064 | } |
J. Bruce Fields | 50c7b94 | 2015-11-23 15:39:12 -0700 | [diff] [blame] | 3065 | new->cl_mach_cred = true; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3066 | case SP4_NONE: |
| 3067 | break; |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 3068 | default: /* checked by xdr code */ |
| 3069 | WARN_ON_ONCE(1); |
Gustavo A. R. Silva | 0a4c926 | 2019-01-23 02:48:28 -0600 | [diff] [blame] | 3070 | /* fall through */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3071 | case SP4_SSV: |
Kinglong Mee | 8edf4b0 | 2016-02-26 22:36:42 +0800 | [diff] [blame] | 3072 | status = nfserr_encr_alg_unsupp; |
| 3073 | goto out_nolock; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3074 | } |
| 3075 | |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 3076 | /* Cases below refer to rfc 5661 section 18.35.4: */ |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3077 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 3078 | conf = find_confirmed_client_by_name(&exid->clname, nn); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3079 | if (conf) { |
J. Bruce Fields | 83e08fd | 2012-05-14 09:08:10 -0400 | [diff] [blame] | 3080 | bool creds_match = same_creds(&conf->cl_cred, &rqstp->rq_cred); |
| 3081 | bool verfs_match = same_verf(&verf, &conf->cl_verifier); |
| 3082 | |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3083 | if (update) { |
| 3084 | if (!clp_used_exchangeid(conf)) { /* buggy client */ |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 3085 | status = nfserr_inval; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3086 | goto out; |
| 3087 | } |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 3088 | if (!nfsd4_mach_creds_match(conf, rqstp)) { |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3089 | status = nfserr_wrong_cred; |
| 3090 | goto out; |
| 3091 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3092 | if (!creds_match) { /* case 9 */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3093 | status = nfserr_perm; |
| 3094 | goto out; |
| 3095 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3096 | if (!verfs_match) { /* case 8 */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3097 | status = nfserr_not_same; |
| 3098 | goto out; |
| 3099 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3100 | /* case 6 */ |
| 3101 | exid->flags |= EXCHGID4_FLAG_CONFIRMED_R; |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3102 | goto out_copy; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3103 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3104 | if (!creds_match) { /* case 3 */ |
J. Bruce Fields | 631fc9e | 2012-05-14 15:57:23 -0400 | [diff] [blame] | 3105 | if (client_has_state(conf)) { |
| 3106 | status = nfserr_clid_inuse; |
| 3107 | goto out; |
| 3108 | } |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3109 | goto out_new; |
| 3110 | } |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3111 | if (verfs_match) { /* case 2 */ |
J. Bruce Fields | 0f1ba0e | 2012-05-25 21:24:40 -0400 | [diff] [blame] | 3112 | conf->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R; |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3113 | goto out_copy; |
| 3114 | } |
| 3115 | /* case 5, client reboot */ |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3116 | conf = NULL; |
J. Bruce Fields | 136e658 | 2012-05-12 20:37:23 -0400 | [diff] [blame] | 3117 | goto out_new; |
Mike Sager | 6ddbbbf | 2009-06-16 04:20:47 +0300 | [diff] [blame] | 3118 | } |
| 3119 | |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 3120 | if (update) { /* case 7 */ |
Mike Sager | 6ddbbbf | 2009-06-16 04:20:47 +0300 | [diff] [blame] | 3121 | status = nfserr_noent; |
| 3122 | goto out; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3123 | } |
| 3124 | |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 3125 | unconf = find_unconfirmed_client_by_name(&exid->clname, nn); |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 3126 | if (unconf) /* case 4, possible retry or client restart */ |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3127 | unhash_client_locked(unconf); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3128 | |
J. Bruce Fields | 2dbb269 | 2012-05-14 09:47:11 -0400 | [diff] [blame] | 3129 | /* case 1 (normal case) */ |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3130 | out_new: |
Jeff Layton | fd699b8 | 2014-07-30 08:27:14 -0400 | [diff] [blame] | 3131 | if (conf) { |
| 3132 | status = mark_client_expired_locked(conf); |
| 3133 | if (status) |
| 3134 | goto out; |
| 3135 | } |
J. Bruce Fields | 4f540e2 | 2013-05-07 11:57:52 -0400 | [diff] [blame] | 3136 | new->cl_minorversion = cstate->minorversion; |
Andrew Elble | ed94164 | 2016-06-15 12:52:09 -0400 | [diff] [blame] | 3137 | new->cl_spo_must_allow.u.words[0] = exid->spo_must_allow[0]; |
| 3138 | 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] | 3139 | |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 3140 | add_to_unconfirmed(new); |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3141 | swap(new, conf); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3142 | out_copy: |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 3143 | exid->clientid.cl_boot = conf->cl_clientid.cl_boot; |
| 3144 | exid->clientid.cl_id = conf->cl_clientid.cl_id; |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3145 | |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 3146 | exid->seqid = conf->cl_cs_slot.sl_seqid + 1; |
| 3147 | nfsd4_set_ex_flags(conf, exid); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3148 | |
| 3149 | dprintk("nfsd4_exchange_id seqid %d flags %x\n", |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 3150 | conf->cl_cs_slot.sl_seqid, conf->cl_exchange_flags); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3151 | status = nfs_ok; |
| 3152 | |
| 3153 | out: |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3154 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | 50c7b94 | 2015-11-23 15:39:12 -0700 | [diff] [blame] | 3155 | out_nolock: |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 3156 | if (new) |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3157 | expire_client(new); |
| 3158 | if (unconf) |
| 3159 | expire_client(unconf); |
Andy Adamson | 0733d21 | 2009-04-03 08:28:01 +0300 | [diff] [blame] | 3160 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3161 | } |
| 3162 | |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3163 | static __be32 |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 3164 | check_slot_seqid(u32 seqid, u32 slot_seqid, int slot_inuse) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3165 | { |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 3166 | dprintk("%s enter. seqid %d slot_seqid %d\n", __func__, seqid, |
| 3167 | slot_seqid); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3168 | |
| 3169 | /* The slot is in use, and no response has been sent. */ |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 3170 | if (slot_inuse) { |
| 3171 | if (seqid == slot_seqid) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3172 | return nfserr_jukebox; |
| 3173 | else |
| 3174 | return nfserr_seq_misordered; |
| 3175 | } |
J. Bruce Fields | f6d8248 | 2012-02-13 16:13:41 -0500 | [diff] [blame] | 3176 | /* Note unsigned 32-bit arithmetic handles wraparound: */ |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 3177 | if (likely(seqid == slot_seqid + 1)) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3178 | return nfs_ok; |
Andy Adamson | 88e588d | 2009-07-23 19:02:15 -0400 | [diff] [blame] | 3179 | if (seqid == slot_seqid) |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3180 | return nfserr_replay_cache; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3181 | return nfserr_seq_misordered; |
| 3182 | } |
| 3183 | |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 3184 | /* |
| 3185 | * Cache the create session result into the create session single DRC |
| 3186 | * slot cache by saving the xdr structure. sl_seqid has been set. |
| 3187 | * Do this for solo or embedded create session operations. |
| 3188 | */ |
| 3189 | static void |
| 3190 | nfsd4_cache_create_session(struct nfsd4_create_session *cr_ses, |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3191 | struct nfsd4_clid_slot *slot, __be32 nfserr) |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 3192 | { |
| 3193 | slot->sl_status = nfserr; |
| 3194 | memcpy(&slot->sl_cr_ses, cr_ses, sizeof(*cr_ses)); |
| 3195 | } |
| 3196 | |
| 3197 | static __be32 |
| 3198 | nfsd4_replay_create_session(struct nfsd4_create_session *cr_ses, |
| 3199 | struct nfsd4_clid_slot *slot) |
| 3200 | { |
| 3201 | memcpy(cr_ses, &slot->sl_cr_ses, sizeof(*cr_ses)); |
| 3202 | return slot->sl_status; |
| 3203 | } |
| 3204 | |
Mi Jinlong | 1b74c25 | 2011-07-14 14:50:17 +0800 | [diff] [blame] | 3205 | #define NFSD_MIN_REQ_HDR_SEQ_SZ ((\ |
| 3206 | 2 * 2 + /* credential,verifier: AUTH_NULL, length 0 */ \ |
| 3207 | 1 + /* MIN tag is length with zero, only length */ \ |
| 3208 | 3 + /* version, opcount, opcode */ \ |
| 3209 | XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ |
| 3210 | /* seqid, slotID, slotID, cache */ \ |
| 3211 | 4 ) * sizeof(__be32)) |
| 3212 | |
| 3213 | #define NFSD_MIN_RESP_HDR_SEQ_SZ ((\ |
| 3214 | 2 + /* verifier: AUTH_NULL, length 0 */\ |
| 3215 | 1 + /* status */ \ |
| 3216 | 1 + /* MIN tag is length with zero, only length */ \ |
| 3217 | 3 + /* opcount, opcode, opstatus*/ \ |
| 3218 | XDR_QUADLEN(NFS4_MAX_SESSIONID_LEN) + \ |
| 3219 | /* seqid, slotID, slotID, slotID, status */ \ |
| 3220 | 5 ) * sizeof(__be32)) |
| 3221 | |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 3222 | 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] | 3223 | { |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 3224 | u32 maxrpc = nn->nfsd_serv->sv_max_mesg; |
| 3225 | |
J. Bruce Fields | 373cd40 | 2013-04-08 15:42:12 -0400 | [diff] [blame] | 3226 | if (ca->maxreq_sz < NFSD_MIN_REQ_HDR_SEQ_SZ) |
| 3227 | return nfserr_toosmall; |
| 3228 | if (ca->maxresp_sz < NFSD_MIN_RESP_HDR_SEQ_SZ) |
| 3229 | return nfserr_toosmall; |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 3230 | ca->headerpadsz = 0; |
| 3231 | ca->maxreq_sz = min_t(u32, ca->maxreq_sz, maxrpc); |
| 3232 | ca->maxresp_sz = min_t(u32, ca->maxresp_sz, maxrpc); |
| 3233 | ca->maxops = min_t(u32, ca->maxops, NFSD_MAX_OPS_PER_COMPOUND); |
| 3234 | ca->maxresp_cached = min_t(u32, ca->maxresp_cached, |
| 3235 | NFSD_SLOT_CACHE_SIZE + NFSD_MIN_HDR_SEQ_SZ); |
| 3236 | ca->maxreqs = min_t(u32, ca->maxreqs, NFSD_MAX_SLOTS_PER_SESSION); |
| 3237 | /* |
| 3238 | * Note decreasing slot size below client's request may make it |
| 3239 | * difficult for client to function correctly, whereas |
| 3240 | * decreasing the number of slots will (just?) affect |
| 3241 | * performance. When short on memory we therefore prefer to |
| 3242 | * decrease number of slots instead of their size. Clients that |
| 3243 | * request larger slots than they need will get poor results: |
NeilBrown | 7f49fd5d | 2019-09-20 16:33:16 +1000 | [diff] [blame] | 3244 | * Note that we always allow at least one slot, because our |
| 3245 | * accounting is soft and provides no guarantees either way. |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 3246 | */ |
NeilBrown | 2030ca5 | 2019-09-20 16:36:45 +1000 | [diff] [blame] | 3247 | ca->maxreqs = nfsd4_get_drc_mem(ca, nn); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 3248 | |
J. Bruce Fields | 373cd40 | 2013-04-08 15:42:12 -0400 | [diff] [blame] | 3249 | return nfs_ok; |
Mi Jinlong | 1b74c25 | 2011-07-14 14:50:17 +0800 | [diff] [blame] | 3250 | } |
| 3251 | |
Chuck Lever | 4500632 | 2016-03-01 13:06:02 -0500 | [diff] [blame] | 3252 | /* |
| 3253 | * Server's NFSv4.1 backchannel support is AUTH_SYS-only for now. |
| 3254 | * These are based on similar macros in linux/sunrpc/msg_prot.h . |
| 3255 | */ |
| 3256 | #define RPC_MAX_HEADER_WITH_AUTH_SYS \ |
| 3257 | (RPC_CALLHDRSIZE + 2 * (2 + UNX_CALLSLACK)) |
| 3258 | |
| 3259 | #define RPC_MAX_REPHEADER_WITH_AUTH_SYS \ |
| 3260 | (RPC_REPHDRSIZE + (2 + NUL_REPLYSLACK)) |
| 3261 | |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 3262 | #define NFSD_CB_MAX_REQ_SZ ((NFS4_enc_cb_recall_sz + \ |
Chuck Lever | 4500632 | 2016-03-01 13:06:02 -0500 | [diff] [blame] | 3263 | RPC_MAX_HEADER_WITH_AUTH_SYS) * sizeof(__be32)) |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 3264 | #define NFSD_CB_MAX_RESP_SZ ((NFS4_dec_cb_recall_sz + \ |
Chuck Lever | 4500632 | 2016-03-01 13:06:02 -0500 | [diff] [blame] | 3265 | RPC_MAX_REPHEADER_WITH_AUTH_SYS) * \ |
| 3266 | sizeof(__be32)) |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 3267 | |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 3268 | static __be32 check_backchannel_attrs(struct nfsd4_channel_attrs *ca) |
| 3269 | { |
| 3270 | ca->headerpadsz = 0; |
| 3271 | |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 3272 | if (ca->maxreq_sz < NFSD_CB_MAX_REQ_SZ) |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 3273 | return nfserr_toosmall; |
Kinglong Mee | 8a89163 | 2013-12-23 18:11:02 +0800 | [diff] [blame] | 3274 | if (ca->maxresp_sz < NFSD_CB_MAX_RESP_SZ) |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 3275 | return nfserr_toosmall; |
| 3276 | ca->maxresp_cached = 0; |
| 3277 | if (ca->maxops < 2) |
| 3278 | return nfserr_toosmall; |
| 3279 | |
| 3280 | return nfs_ok; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3281 | } |
| 3282 | |
J. Bruce Fields | b78724b | 2013-05-15 17:34:39 -0400 | [diff] [blame] | 3283 | static __be32 nfsd4_check_cb_sec(struct nfsd4_cb_sec *cbs) |
| 3284 | { |
| 3285 | switch (cbs->flavor) { |
| 3286 | case RPC_AUTH_NULL: |
| 3287 | case RPC_AUTH_UNIX: |
| 3288 | return nfs_ok; |
| 3289 | default: |
| 3290 | /* |
| 3291 | * GSS case: the spec doesn't allow us to return this |
| 3292 | * error. But it also doesn't allow us not to support |
| 3293 | * GSS. |
| 3294 | * I'd rather this fail hard than return some error the |
| 3295 | * client might think it can already handle: |
| 3296 | */ |
| 3297 | return nfserr_encr_alg_unsupp; |
| 3298 | } |
| 3299 | } |
| 3300 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3301 | __be32 |
| 3302 | nfsd4_create_session(struct svc_rqst *rqstp, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3303 | struct nfsd4_compound_state *cstate, union nfsd4_op_u *u) |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3304 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3305 | struct nfsd4_create_session *cr_ses = &u->create_session; |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 3306 | struct sockaddr *sa = svc_addr(rqstp); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3307 | struct nfs4_client *conf, *unconf; |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3308 | struct nfs4_client *old = NULL; |
J. Bruce Fields | ac7c46f2 | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 3309 | struct nfsd4_session *new; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3310 | struct nfsd4_conn *conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 3311 | struct nfsd4_clid_slot *cs_slot = NULL; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3312 | __be32 status = 0; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 3313 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3314 | |
Mi Jinlong | a62573d | 2011-03-23 17:57:07 +0800 | [diff] [blame] | 3315 | if (cr_ses->flags & ~SESSION4_FLAG_MASK_A) |
| 3316 | return nfserr_inval; |
J. Bruce Fields | b78724b | 2013-05-15 17:34:39 -0400 | [diff] [blame] | 3317 | status = nfsd4_check_cb_sec(&cr_ses->cb_sec); |
| 3318 | if (status) |
| 3319 | return status; |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 3320 | status = check_forechannel_attrs(&cr_ses->fore_channel, nn); |
J. Bruce Fields | 373cd40 | 2013-04-08 15:42:12 -0400 | [diff] [blame] | 3321 | if (status) |
| 3322 | return status; |
J. Bruce Fields | 06b332a | 2013-04-09 11:34:36 -0400 | [diff] [blame] | 3323 | status = check_backchannel_attrs(&cr_ses->back_channel); |
| 3324 | if (status) |
Kinglong Mee | f403e45 | 2013-12-23 17:31:21 +0800 | [diff] [blame] | 3325 | goto out_release_drc_mem; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3326 | status = nfserr_jukebox; |
Kinglong Mee | 60810e5 | 2014-01-01 00:35:47 +0800 | [diff] [blame] | 3327 | new = alloc_session(&cr_ses->fore_channel, &cr_ses->back_channel); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 3328 | if (!new) |
| 3329 | goto out_release_drc_mem; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3330 | conn = alloc_conn_from_crses(rqstp, cr_ses); |
| 3331 | if (!conn) |
| 3332 | goto out_free_session; |
Mi Jinlong | a62573d | 2011-03-23 17:57:07 +0800 | [diff] [blame] | 3333 | |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3334 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 3335 | unconf = find_unconfirmed_client(&cr_ses->clientid, true, nn); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 3336 | conf = find_confirmed_client(&cr_ses->clientid, true, nn); |
J. Bruce Fields | 7838904 | 2013-03-12 10:12:37 -0400 | [diff] [blame] | 3337 | WARN_ON_ONCE(conf && unconf); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3338 | |
| 3339 | if (conf) { |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3340 | status = nfserr_wrong_cred; |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 3341 | if (!nfsd4_mach_creds_match(conf, rqstp)) |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3342 | goto out_free_conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 3343 | cs_slot = &conf->cl_cs_slot; |
| 3344 | status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); |
Kinglong Mee | f5e22bb | 2015-07-13 17:32:34 +0800 | [diff] [blame] | 3345 | if (status) { |
| 3346 | if (status == nfserr_replay_cache) |
| 3347 | status = nfsd4_replay_create_session(cr_ses, cs_slot); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3348 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3349 | } |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3350 | } else if (unconf) { |
| 3351 | if (!same_creds(&unconf->cl_cred, &rqstp->rq_cred) || |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 3352 | !rpc_cmp_addr(sa, (struct sockaddr *) &unconf->cl_addr)) { |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3353 | status = nfserr_clid_inuse; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3354 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3355 | } |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3356 | status = nfserr_wrong_cred; |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 3357 | if (!nfsd4_mach_creds_match(unconf, rqstp)) |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3358 | goto out_free_conn; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 3359 | cs_slot = &unconf->cl_cs_slot; |
| 3360 | status = check_slot_seqid(cr_ses->seqid, cs_slot->sl_seqid, 0); |
Andy Adamson | 38eb76a | 2009-04-03 08:28:32 +0300 | [diff] [blame] | 3361 | if (status) { |
| 3362 | /* an unconfirmed replay returns misordered */ |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3363 | status = nfserr_seq_misordered; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3364 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3365 | } |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 3366 | old = find_confirmed_client_by_name(&unconf->cl_name, nn); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3367 | if (old) { |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3368 | status = mark_client_expired_locked(old); |
Jeff Layton | 7abea1e | 2014-07-30 08:27:13 -0400 | [diff] [blame] | 3369 | if (status) { |
| 3370 | old = NULL; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3371 | goto out_free_conn; |
Jeff Layton | 7abea1e | 2014-07-30 08:27:13 -0400 | [diff] [blame] | 3372 | } |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3373 | } |
J. Bruce Fields | 8f9d3d3 | 2012-09-12 14:41:31 -0400 | [diff] [blame] | 3374 | move_to_confirmed(unconf); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3375 | conf = unconf; |
| 3376 | } else { |
| 3377 | status = nfserr_stale_clientid; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3378 | goto out_free_conn; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3379 | } |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3380 | status = nfs_ok; |
Chuck Lever | 4ce85c8 | 2016-03-01 13:05:27 -0500 | [diff] [blame] | 3381 | /* Persistent sessions are not supported */ |
J. Bruce Fields | 408b79b | 2010-04-15 15:11:09 -0400 | [diff] [blame] | 3382 | cr_ses->flags &= ~SESSION4_PERSIST; |
Chuck Lever | 4ce85c8 | 2016-03-01 13:05:27 -0500 | [diff] [blame] | 3383 | /* Upshifting from TCP to RDMA is not supported */ |
J. Bruce Fields | 408b79b | 2010-04-15 15:11:09 -0400 | [diff] [blame] | 3384 | cr_ses->flags &= ~SESSION4_RDMA; |
| 3385 | |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3386 | init_session(rqstp, new, conf, cr_ses); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3387 | nfsd4_get_session_locked(new); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3388 | |
J. Bruce Fields | ac7c46f2 | 2010-06-14 19:01:57 -0400 | [diff] [blame] | 3389 | memcpy(cr_ses->sessionid.data, new->se_sessionid.data, |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3390 | NFS4_MAX_SESSIONID_LEN); |
J. Bruce Fields | 86c3e16 | 2010-10-02 17:04:00 -0400 | [diff] [blame] | 3391 | cs_slot->sl_seqid++; |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 3392 | cr_ses->seqid = cs_slot->sl_seqid; |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3393 | |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3394 | /* cache solo and embedded create sessions under the client_lock */ |
Andy Adamson | 49557cc | 2009-07-23 19:02:16 -0400 | [diff] [blame] | 3395 | nfsd4_cache_create_session(cr_ses, cs_slot, status); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3396 | spin_unlock(&nn->client_lock); |
| 3397 | /* init connection and backchannel */ |
| 3398 | nfsd4_init_conn(rqstp, conn, new); |
| 3399 | nfsd4_put_session(new); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3400 | if (old) |
| 3401 | expire_client(old); |
Andy Adamson | ec6b5d7 | 2009-04-03 08:28:28 +0300 | [diff] [blame] | 3402 | return status; |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3403 | out_free_conn: |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3404 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3405 | free_conn(conn); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3406 | if (old) |
| 3407 | expire_client(old); |
J. Bruce Fields | 81f0b2a | 2012-09-12 11:04:33 -0400 | [diff] [blame] | 3408 | out_free_session: |
| 3409 | __free_session(new); |
J. Bruce Fields | 55c760c | 2013-04-08 16:44:14 -0400 | [diff] [blame] | 3410 | out_release_drc_mem: |
| 3411 | nfsd4_put_drc_mem(&cr_ses->fore_channel); |
J. Bruce Fields | 1ca5079 | 2013-03-14 18:12:03 -0400 | [diff] [blame] | 3412 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3413 | } |
| 3414 | |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 3415 | static __be32 nfsd4_map_bcts_dir(u32 *dir) |
| 3416 | { |
| 3417 | switch (*dir) { |
| 3418 | case NFS4_CDFC4_FORE: |
| 3419 | case NFS4_CDFC4_BACK: |
| 3420 | return nfs_ok; |
| 3421 | case NFS4_CDFC4_FORE_OR_BOTH: |
| 3422 | case NFS4_CDFC4_BACK_OR_BOTH: |
| 3423 | *dir = NFS4_CDFC4_BOTH; |
| 3424 | return nfs_ok; |
| 3425 | }; |
| 3426 | return nfserr_inval; |
| 3427 | } |
| 3428 | |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3429 | __be32 nfsd4_backchannel_ctl(struct svc_rqst *rqstp, |
| 3430 | struct nfsd4_compound_state *cstate, |
| 3431 | union nfsd4_op_u *u) |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 3432 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3433 | struct nfsd4_backchannel_ctl *bc = &u->backchannel_ctl; |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 3434 | struct nfsd4_session *session = cstate->session; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3435 | 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] | 3436 | __be32 status; |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 3437 | |
J. Bruce Fields | b78724b | 2013-05-15 17:34:39 -0400 | [diff] [blame] | 3438 | status = nfsd4_check_cb_sec(&bc->bc_cb_sec); |
| 3439 | if (status) |
| 3440 | return status; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3441 | spin_lock(&nn->client_lock); |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 3442 | session->se_cb_prog = bc->bc_cb_program; |
| 3443 | session->se_cb_sec = bc->bc_cb_sec; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3444 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | cb73a9f | 2012-11-01 18:09:48 -0400 | [diff] [blame] | 3445 | |
| 3446 | nfsd4_probe_callback(session->se_client); |
| 3447 | |
| 3448 | return nfs_ok; |
| 3449 | } |
| 3450 | |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 3451 | __be32 nfsd4_bind_conn_to_session(struct svc_rqst *rqstp, |
| 3452 | struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3453 | union nfsd4_op_u *u) |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 3454 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3455 | 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] | 3456 | __be32 status; |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 3457 | struct nfsd4_conn *conn; |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 3458 | struct nfsd4_session *session; |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3459 | struct net *net = SVC_NET(rqstp); |
| 3460 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 3461 | |
| 3462 | if (!nfsd4_last_compound_op(rqstp)) |
| 3463 | return nfserr_not_only_op; |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3464 | spin_lock(&nn->client_lock); |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3465 | session = find_in_sessionid_hashtbl(&bcts->sessionid, net, &status); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3466 | spin_unlock(&nn->client_lock); |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 3467 | if (!session) |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3468 | goto out_no_session; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3469 | status = nfserr_wrong_cred; |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 3470 | if (!nfsd4_mach_creds_match(session->se_client, rqstp)) |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3471 | goto out; |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 3472 | status = nfsd4_map_bcts_dir(&bcts->dir); |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 3473 | if (status) |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 3474 | goto out; |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 3475 | conn = alloc_conn(rqstp, bcts->dir); |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 3476 | status = nfserr_jukebox; |
J. Bruce Fields | 3ba6367 | 2012-09-11 15:37:40 -0400 | [diff] [blame] | 3477 | if (!conn) |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 3478 | goto out; |
| 3479 | nfsd4_init_conn(rqstp, conn, session); |
| 3480 | status = nfs_ok; |
| 3481 | out: |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3482 | nfsd4_put_session(session); |
| 3483 | out_no_session: |
J. Bruce Fields | 4f6e6c1 | 2013-03-18 17:31:30 -0400 | [diff] [blame] | 3484 | return status; |
J. Bruce Fields | 1d1bc8f | 2010-10-04 23:12:59 -0400 | [diff] [blame] | 3485 | } |
| 3486 | |
J. Bruce Fields | 665d507 | 2018-04-11 16:53:36 -0400 | [diff] [blame] | 3487 | 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] | 3488 | { |
J. Bruce Fields | 665d507 | 2018-04-11 16:53:36 -0400 | [diff] [blame] | 3489 | if (!cstate->session) |
Fengguang Wu | 51d87bc | 2018-03-22 13:37:20 +0800 | [diff] [blame] | 3490 | return false; |
J. Bruce Fields | 665d507 | 2018-04-11 16:53:36 -0400 | [diff] [blame] | 3491 | return !memcmp(sid, &cstate->session->se_sessionid, sizeof(*sid)); |
J. Bruce Fields | 5d4cec2 | 2010-05-01 12:56:06 -0400 | [diff] [blame] | 3492 | } |
| 3493 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3494 | __be32 |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3495 | nfsd4_destroy_session(struct svc_rqst *r, struct nfsd4_compound_state *cstate, |
| 3496 | union nfsd4_op_u *u) |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3497 | { |
J. Bruce Fields | ca0552f | 2018-04-11 17:01:53 -0400 | [diff] [blame] | 3498 | struct nfs4_sessionid *sessionid = &u->destroy_session.sessionid; |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 3499 | struct nfsd4_session *ses; |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 3500 | __be32 status; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 3501 | int ref_held_by_me = 0; |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3502 | struct net *net = SVC_NET(r); |
| 3503 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 3504 | |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 3505 | status = nfserr_not_only_op; |
J. Bruce Fields | ca0552f | 2018-04-11 17:01:53 -0400 | [diff] [blame] | 3506 | if (nfsd4_compound_in_session(cstate, sessionid)) { |
J. Bruce Fields | 5771635 | 2010-04-21 12:27:19 -0400 | [diff] [blame] | 3507 | if (!nfsd4_last_compound_op(r)) |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 3508 | goto out; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 3509 | ref_held_by_me++; |
J. Bruce Fields | 5771635 | 2010-04-21 12:27:19 -0400 | [diff] [blame] | 3510 | } |
J. Bruce Fields | ca0552f | 2018-04-11 17:01:53 -0400 | [diff] [blame] | 3511 | dump_sessionid(__func__, sessionid); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3512 | spin_lock(&nn->client_lock); |
J. Bruce Fields | ca0552f | 2018-04-11 17:01:53 -0400 | [diff] [blame] | 3513 | ses = find_in_sessionid_hashtbl(sessionid, net, &status); |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 3514 | if (!ses) |
| 3515 | goto out_client_lock; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3516 | status = nfserr_wrong_cred; |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 3517 | if (!nfsd4_mach_creds_match(ses->se_client, r)) |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3518 | goto out_put_session; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 3519 | status = mark_session_dead_locked(ses, 1 + ref_held_by_me); |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 3520 | if (status) |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 3521 | goto out_put_session; |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 3522 | unhash_session(ses); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3523 | spin_unlock(&nn->client_lock); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 3524 | |
J. Bruce Fields | 84f5f7c | 2010-12-09 15:52:19 -0500 | [diff] [blame] | 3525 | nfsd4_probe_callback_sync(ses->se_client); |
J. Bruce Fields | 19cf5c0 | 2010-06-06 18:37:16 -0400 | [diff] [blame] | 3526 | |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3527 | spin_lock(&nn->client_lock); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 3528 | status = nfs_ok; |
J. Bruce Fields | f0f51f5 | 2013-06-18 14:26:02 -0400 | [diff] [blame] | 3529 | out_put_session: |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3530 | nfsd4_put_session_locked(ses); |
J. Bruce Fields | abcdff0 | 2013-03-14 19:55:33 -0400 | [diff] [blame] | 3531 | out_client_lock: |
| 3532 | spin_unlock(&nn->client_lock); |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 3533 | out: |
Benny Halevy | e10e0cf | 2009-04-03 08:28:38 +0300 | [diff] [blame] | 3534 | return status; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3535 | } |
| 3536 | |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 3537 | 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] | 3538 | { |
| 3539 | struct nfsd4_conn *c; |
| 3540 | |
| 3541 | list_for_each_entry(c, &s->se_conns, cn_persession) { |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 3542 | if (c->cn_xprt == xpt) { |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 3543 | return c; |
| 3544 | } |
| 3545 | } |
| 3546 | return NULL; |
| 3547 | } |
| 3548 | |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3549 | 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] | 3550 | { |
| 3551 | struct nfs4_client *clp = ses->se_client; |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 3552 | struct nfsd4_conn *c; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3553 | __be32 status = nfs_ok; |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 3554 | int ret; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 3555 | |
| 3556 | spin_lock(&clp->cl_lock); |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 3557 | c = __nfsd4_find_conn(new->cn_xprt, ses); |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3558 | if (c) |
| 3559 | goto out_free; |
| 3560 | status = nfserr_conn_not_bound_to_session; |
| 3561 | if (clp->cl_mach_cred) |
| 3562 | goto out_free; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 3563 | __nfsd4_hash_conn(new, ses); |
| 3564 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 21b75b0 | 2010-10-26 10:07:17 -0400 | [diff] [blame] | 3565 | ret = nfsd4_register_conn(new); |
| 3566 | if (ret) |
| 3567 | /* oops; xprt is already down: */ |
| 3568 | nfsd4_conn_lost(&new->cn_xpt_user); |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3569 | return nfs_ok; |
| 3570 | out_free: |
| 3571 | spin_unlock(&clp->cl_lock); |
| 3572 | free_conn(new); |
| 3573 | return status; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 3574 | } |
| 3575 | |
Mi Jinlong | 868b89c | 2011-04-27 09:09:58 +0800 | [diff] [blame] | 3576 | static bool nfsd4_session_too_many_ops(struct svc_rqst *rqstp, struct nfsd4_session *session) |
| 3577 | { |
| 3578 | struct nfsd4_compoundargs *args = rqstp->rq_argp; |
| 3579 | |
| 3580 | return args->opcnt > session->se_fchannel.maxops; |
| 3581 | } |
| 3582 | |
Mi Jinlong | ae82a8d | 2011-07-14 14:56:02 +0800 | [diff] [blame] | 3583 | static bool nfsd4_request_too_big(struct svc_rqst *rqstp, |
| 3584 | struct nfsd4_session *session) |
| 3585 | { |
| 3586 | struct xdr_buf *xb = &rqstp->rq_arg; |
| 3587 | |
| 3588 | return xb->len > session->se_fchannel.maxreq_sz; |
| 3589 | } |
| 3590 | |
J. Bruce Fields | 53da6a5 | 2017-10-17 20:38:49 -0400 | [diff] [blame] | 3591 | static bool replay_matches_cache(struct svc_rqst *rqstp, |
| 3592 | struct nfsd4_sequence *seq, struct nfsd4_slot *slot) |
| 3593 | { |
| 3594 | struct nfsd4_compoundargs *argp = rqstp->rq_argp; |
| 3595 | |
| 3596 | if ((bool)(slot->sl_flags & NFSD4_SLOT_CACHETHIS) != |
| 3597 | (bool)seq->cachethis) |
| 3598 | return false; |
| 3599 | /* |
Scott Mayhew | 6e73e92 | 2019-10-09 15:11:37 -0400 | [diff] [blame] | 3600 | * If there's an error then the reply can have fewer ops than |
| 3601 | * the call. |
J. Bruce Fields | 53da6a5 | 2017-10-17 20:38:49 -0400 | [diff] [blame] | 3602 | */ |
Scott Mayhew | 6e73e92 | 2019-10-09 15:11:37 -0400 | [diff] [blame] | 3603 | if (slot->sl_opcnt < argp->opcnt && !slot->sl_status) |
| 3604 | return false; |
| 3605 | /* |
| 3606 | * But if we cached a reply with *more* ops than the call you're |
| 3607 | * sending us now, then this new call is clearly not really a |
| 3608 | * replay of the old one: |
| 3609 | */ |
| 3610 | if (slot->sl_opcnt > argp->opcnt) |
J. Bruce Fields | 53da6a5 | 2017-10-17 20:38:49 -0400 | [diff] [blame] | 3611 | return false; |
| 3612 | /* This is the only check explicitly called by spec: */ |
| 3613 | if (!same_creds(&rqstp->rq_cred, &slot->sl_cred)) |
| 3614 | return false; |
| 3615 | /* |
| 3616 | * There may be more comparisons we could actually do, but the |
| 3617 | * spec doesn't require us to catch every case where the calls |
| 3618 | * don't match (that would require caching the call as well as |
| 3619 | * the reply), so we don't bother. |
| 3620 | */ |
| 3621 | return true; |
| 3622 | } |
| 3623 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3624 | __be32 |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3625 | nfsd4_sequence(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
| 3626 | union nfsd4_op_u *u) |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3627 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3628 | struct nfsd4_sequence *seq = &u->sequence; |
Andy Adamson | f9bb94c4 | 2009-04-03 08:28:12 +0300 | [diff] [blame] | 3629 | struct nfsd4_compoundres *resp = rqstp->rq_resp; |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 3630 | struct xdr_stream *xdr = &resp->xdr; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3631 | struct nfsd4_session *session; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3632 | struct nfs4_client *clp; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3633 | struct nfsd4_slot *slot; |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 3634 | struct nfsd4_conn *conn; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3635 | __be32 status; |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 3636 | int buflen; |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3637 | struct net *net = SVC_NET(rqstp); |
| 3638 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3639 | |
Andy Adamson | f9bb94c4 | 2009-04-03 08:28:12 +0300 | [diff] [blame] | 3640 | if (resp->opcnt != 1) |
| 3641 | return nfserr_sequence_pos; |
| 3642 | |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 3643 | /* |
| 3644 | * Will be either used or freed by nfsd4_sequence_check_conn |
| 3645 | * below. |
| 3646 | */ |
| 3647 | conn = alloc_conn(rqstp, NFS4_CDFC4_FORE); |
| 3648 | if (!conn) |
| 3649 | return nfserr_jukebox; |
| 3650 | |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3651 | spin_lock(&nn->client_lock); |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3652 | session = find_in_sessionid_hashtbl(&seq->sessionid, net, &status); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3653 | if (!session) |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3654 | goto out_no_session; |
| 3655 | clp = session->se_client; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3656 | |
Mi Jinlong | 868b89c | 2011-04-27 09:09:58 +0800 | [diff] [blame] | 3657 | status = nfserr_too_many_ops; |
| 3658 | if (nfsd4_session_too_many_ops(rqstp, session)) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 3659 | goto out_put_session; |
Mi Jinlong | 868b89c | 2011-04-27 09:09:58 +0800 | [diff] [blame] | 3660 | |
Mi Jinlong | ae82a8d | 2011-07-14 14:56:02 +0800 | [diff] [blame] | 3661 | status = nfserr_req_too_big; |
| 3662 | if (nfsd4_request_too_big(rqstp, session)) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 3663 | goto out_put_session; |
Mi Jinlong | ae82a8d | 2011-07-14 14:56:02 +0800 | [diff] [blame] | 3664 | |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3665 | status = nfserr_badslot; |
Alexandros Batsakis | 6c18ba9 | 2009-06-16 04:19:13 +0300 | [diff] [blame] | 3666 | if (seq->slotid >= session->se_fchannel.maxreqs) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 3667 | goto out_put_session; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3668 | |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 3669 | slot = session->se_slots[seq->slotid]; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3670 | dprintk("%s: slotid %d\n", __func__, seq->slotid); |
| 3671 | |
Andy Adamson | a8dfdae | 2009-08-28 08:45:02 -0400 | [diff] [blame] | 3672 | /* We do not negotiate the number of slots yet, so set the |
| 3673 | * maxslots to the session maxreqs which is used to encode |
| 3674 | * sr_highest_slotid and the sr_target_slot id to maxslots */ |
| 3675 | seq->maxslots = session->se_fchannel.maxreqs; |
| 3676 | |
J. Bruce Fields | 73e7948 | 2012-02-13 16:39:00 -0500 | [diff] [blame] | 3677 | status = check_slot_seqid(seq->seqid, slot->sl_seqid, |
| 3678 | slot->sl_flags & NFSD4_SLOT_INUSE); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3679 | if (status == nfserr_replay_cache) { |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 3680 | status = nfserr_seq_misordered; |
| 3681 | if (!(slot->sl_flags & NFSD4_SLOT_INITIALIZED)) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 3682 | goto out_put_session; |
J. Bruce Fields | 53da6a5 | 2017-10-17 20:38:49 -0400 | [diff] [blame] | 3683 | status = nfserr_seq_false_retry; |
| 3684 | if (!replay_matches_cache(rqstp, seq, slot)) |
| 3685 | goto out_put_session; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3686 | cstate->slot = slot; |
| 3687 | cstate->session = session; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3688 | cstate->clp = clp; |
Andy Adamson | da3846a | 2009-04-03 08:28:22 +0300 | [diff] [blame] | 3689 | /* Return the cached reply status and set cstate->status |
Andy Adamson | 557ce26 | 2009-08-28 08:45:04 -0400 | [diff] [blame] | 3690 | * for nfsd4_proc_compound processing */ |
Andy Adamson | bf864a3 | 2009-04-03 08:28:35 +0300 | [diff] [blame] | 3691 | status = nfsd4_replay_cache_entry(resp, seq); |
Andy Adamson | da3846a | 2009-04-03 08:28:22 +0300 | [diff] [blame] | 3692 | cstate->status = nfserr_replay_cache; |
Benny Halevy | aaf84eb | 2009-08-20 03:21:56 +0300 | [diff] [blame] | 3693 | goto out; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3694 | } |
| 3695 | if (status) |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 3696 | goto out_put_session; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3697 | |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3698 | status = nfsd4_sequence_check_conn(conn, session); |
J. Bruce Fields | a663bdd | 2010-10-21 17:17:31 -0400 | [diff] [blame] | 3699 | conn = NULL; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3700 | if (status) |
| 3701 | goto out_put_session; |
J. Bruce Fields | 328ead2 | 2010-09-29 16:11:06 -0400 | [diff] [blame] | 3702 | |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 3703 | buflen = (seq->cachethis) ? |
| 3704 | session->se_fchannel.maxresp_cached : |
| 3705 | session->se_fchannel.maxresp_sz; |
| 3706 | status = (seq->cachethis) ? nfserr_rep_too_big_to_cache : |
| 3707 | nfserr_rep_too_big; |
J. Bruce Fields | a5cddc8 | 2014-05-12 18:10:58 -0400 | [diff] [blame] | 3708 | if (xdr_restrict_buflen(xdr, buflen - rqstp->rq_auth_slack)) |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 3709 | goto out_put_session; |
J. Bruce Fields | 32aaa62 | 2014-03-20 20:47:41 -0400 | [diff] [blame] | 3710 | svc_reserve(rqstp, buflen); |
J. Bruce Fields | 47ee529 | 2014-03-12 21:39:35 -0400 | [diff] [blame] | 3711 | |
| 3712 | status = nfs_ok; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3713 | /* Success! bump slot seqid */ |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3714 | slot->sl_seqid = seq->seqid; |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 3715 | slot->sl_flags |= NFSD4_SLOT_INUSE; |
J. Bruce Fields | 73e7948 | 2012-02-13 16:39:00 -0500 | [diff] [blame] | 3716 | if (seq->cachethis) |
| 3717 | slot->sl_flags |= NFSD4_SLOT_CACHETHIS; |
J. Bruce Fields | bf5c43c | 2012-02-13 16:56:19 -0500 | [diff] [blame] | 3718 | else |
| 3719 | slot->sl_flags &= ~NFSD4_SLOT_CACHETHIS; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3720 | |
| 3721 | cstate->slot = slot; |
| 3722 | cstate->session = session; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3723 | cstate->clp = clp; |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3724 | |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3725 | out: |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3726 | switch (clp->cl_cb_state) { |
| 3727 | case NFSD4_CB_DOWN: |
| 3728 | seq->status_flags = SEQ4_STATUS_CB_PATH_DOWN; |
| 3729 | break; |
| 3730 | case NFSD4_CB_FAULT: |
| 3731 | seq->status_flags = SEQ4_STATUS_BACKCHANNEL_FAULT; |
| 3732 | break; |
| 3733 | default: |
| 3734 | seq->status_flags = 0; |
Benny Halevy | aaf84eb | 2009-08-20 03:21:56 +0300 | [diff] [blame] | 3735 | } |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 3736 | if (!list_empty(&clp->cl_revoked)) |
| 3737 | seq->status_flags |= SEQ4_STATUS_RECALLABLE_STATE_REVOKED; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3738 | out_no_session: |
Kinglong Mee | 3f42d2c | 2014-03-24 11:56:59 +0800 | [diff] [blame] | 3739 | if (conn) |
| 3740 | free_conn(conn); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 3741 | spin_unlock(&nn->client_lock); |
Benny Halevy | b85d4c0 | 2009-04-03 08:28:08 +0300 | [diff] [blame] | 3742 | return status; |
J. Bruce Fields | 66b2b9b | 2013-03-19 12:05:39 -0400 | [diff] [blame] | 3743 | out_put_session: |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3744 | nfsd4_put_session_locked(session); |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3745 | goto out_no_session; |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3746 | } |
| 3747 | |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 3748 | void |
| 3749 | nfsd4_sequence_done(struct nfsd4_compoundres *resp) |
| 3750 | { |
| 3751 | struct nfsd4_compound_state *cs = &resp->cstate; |
| 3752 | |
| 3753 | if (nfsd4_has_session(cs)) { |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 3754 | if (cs->status != nfserr_replay_cache) { |
| 3755 | nfsd4_store_cache_entry(resp); |
| 3756 | cs->slot->sl_flags &= ~NFSD4_SLOT_INUSE; |
| 3757 | } |
Trond Myklebust | d4e19e70 | 2014-06-30 11:48:42 -0400 | [diff] [blame] | 3758 | /* Drop session reference that was taken in nfsd4_sequence() */ |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 3759 | nfsd4_put_session(cs->session); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 3760 | } else if (cs->clp) |
| 3761 | put_client_renew(cs->clp); |
Trond Myklebust | b607664 | 2014-06-30 11:48:35 -0400 | [diff] [blame] | 3762 | } |
| 3763 | |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3764 | __be32 |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3765 | nfsd4_destroy_clientid(struct svc_rqst *rqstp, |
| 3766 | struct nfsd4_compound_state *cstate, |
| 3767 | union nfsd4_op_u *u) |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3768 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3769 | struct nfsd4_destroy_clientid *dc = &u->destroy_clientid; |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3770 | struct nfs4_client *conf, *unconf; |
| 3771 | struct nfs4_client *clp = NULL; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3772 | __be32 status = 0; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 3773 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3774 | |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3775 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 3776 | unconf = find_unconfirmed_client(&dc->clientid, true, nn); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 3777 | conf = find_confirmed_client(&dc->clientid, true, nn); |
J. Bruce Fields | 7838904 | 2013-03-12 10:12:37 -0400 | [diff] [blame] | 3778 | WARN_ON_ONCE(conf && unconf); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3779 | |
| 3780 | if (conf) { |
J. Bruce Fields | c0293b0 | 2013-03-14 18:20:01 -0400 | [diff] [blame] | 3781 | if (client_has_state(conf)) { |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3782 | status = nfserr_clientid_busy; |
| 3783 | goto out; |
| 3784 | } |
Jeff Layton | fd699b8 | 2014-07-30 08:27:14 -0400 | [diff] [blame] | 3785 | status = mark_client_expired_locked(conf); |
| 3786 | if (status) |
| 3787 | goto out; |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3788 | clp = conf; |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3789 | } else if (unconf) |
| 3790 | clp = unconf; |
| 3791 | else { |
| 3792 | status = nfserr_stale_clientid; |
| 3793 | goto out; |
| 3794 | } |
Andrew Elble | dedeb13 | 2016-06-15 12:52:08 -0400 | [diff] [blame] | 3795 | if (!nfsd4_mach_creds_match(clp, rqstp)) { |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3796 | clp = NULL; |
J. Bruce Fields | 57266a6 | 2013-04-13 14:27:29 -0400 | [diff] [blame] | 3797 | status = nfserr_wrong_cred; |
| 3798 | goto out; |
| 3799 | } |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3800 | unhash_client_locked(clp); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3801 | out: |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3802 | spin_unlock(&nn->client_lock); |
Trond Myklebust | 6b10ad1 | 2014-07-30 08:27:08 -0400 | [diff] [blame] | 3803 | if (clp) |
| 3804 | expire_client(clp); |
Mi Jinlong | 345c284 | 2011-10-20 17:51:39 +0800 | [diff] [blame] | 3805 | return status; |
| 3806 | } |
| 3807 | |
Andy Adamson | 069b6ad | 2009-04-03 08:27:58 +0300 | [diff] [blame] | 3808 | __be32 |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3809 | nfsd4_reclaim_complete(struct svc_rqst *rqstp, |
| 3810 | struct nfsd4_compound_state *cstate, union nfsd4_op_u *u) |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 3811 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3812 | struct nfsd4_reclaim_complete *rc = &u->reclaim_complete; |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 3813 | __be32 status = 0; |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3814 | |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 3815 | if (rc->rca_one_fs) { |
| 3816 | if (!cstate->current_fh.fh_dentry) |
| 3817 | return nfserr_nofilehandle; |
| 3818 | /* |
| 3819 | * We don't take advantage of the rca_one_fs case. |
| 3820 | * That's OK, it's optional, we can safely ignore it. |
| 3821 | */ |
Christophe JAILLET | d28c442 | 2016-07-02 08:24:32 +0200 | [diff] [blame] | 3822 | return nfs_ok; |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 3823 | } |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3824 | |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3825 | status = nfserr_complete_already; |
Jeff Layton | a52d726 | 2012-03-21 09:52:02 -0400 | [diff] [blame] | 3826 | if (test_and_set_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, |
| 3827 | &cstate->session->se_client->cl_flags)) |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3828 | goto out; |
| 3829 | |
| 3830 | status = nfserr_stale_clientid; |
| 3831 | if (is_client_expired(cstate->session->se_client)) |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 3832 | /* |
| 3833 | * The following error isn't really legal. |
| 3834 | * But we only get here if the client just explicitly |
| 3835 | * destroyed the client. Surely it no longer cares what |
| 3836 | * error it gets back on an operation for the dead |
| 3837 | * client. |
| 3838 | */ |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3839 | goto out; |
| 3840 | |
| 3841 | status = nfs_ok; |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 3842 | nfsd4_client_record_create(cstate->session->se_client); |
Scott Mayhew | 362063a | 2019-03-26 18:06:28 -0400 | [diff] [blame] | 3843 | inc_reclaim_complete(cstate->session->se_client); |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3844 | out: |
Mi Jinlong | bcecf1c | 2011-04-27 09:14:30 +0800 | [diff] [blame] | 3845 | return status; |
J. Bruce Fields | 4dc6ec0 | 2010-04-19 15:11:28 -0400 | [diff] [blame] | 3846 | } |
| 3847 | |
| 3848 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 3849 | nfsd4_setclientid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3850 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3851 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3852 | struct nfsd4_setclientid *setclid = &u->setclientid; |
J. Bruce Fields | a084daf | 2011-10-10 15:07:40 -0400 | [diff] [blame] | 3853 | struct xdr_netobj clname = setclid->se_name; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3854 | nfs4_verifier clverifier = setclid->se_verf; |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3855 | struct nfs4_client *conf, *new; |
| 3856 | struct nfs4_client *unconf = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3857 | __be32 status; |
Stanislav Kinsbursky | c212cec | 2012-11-14 18:21:10 +0300 | [diff] [blame] | 3858 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
| 3859 | |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 3860 | new = create_client(clname, rqstp, &clverifier); |
| 3861 | if (new == NULL) |
| 3862 | return nfserr_jukebox; |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 3863 | /* Cases below refer to rfc 3530 section 14.2.33: */ |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3864 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 3865 | conf = find_confirmed_client_by_name(&clname, nn); |
J. Bruce Fields | 2b63482 | 2015-10-15 15:33:23 -0400 | [diff] [blame] | 3866 | if (conf && client_has_state(conf)) { |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 3867 | /* case 0: */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3868 | status = nfserr_clid_inuse; |
J. Bruce Fields | e203d50 | 2010-11-24 17:30:54 -0500 | [diff] [blame] | 3869 | if (clp_used_exchangeid(conf)) |
| 3870 | goto out; |
J. Bruce Fields | 026722c | 2009-03-18 15:06:26 -0400 | [diff] [blame] | 3871 | if (!same_creds(&conf->cl_cred, &rqstp->rq_cred)) { |
Jeff Layton | 363168b | 2009-08-14 12:57:56 -0400 | [diff] [blame] | 3872 | char addr_str[INET6_ADDRSTRLEN]; |
| 3873 | rpc_ntop((struct sockaddr *) &conf->cl_addr, addr_str, |
| 3874 | sizeof(addr_str)); |
| 3875 | dprintk("NFSD: setclientid: string in use by client " |
| 3876 | "at %s\n", addr_str); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3877 | goto out; |
| 3878 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3879 | } |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 3880 | unconf = find_unconfirmed_client_by_name(&clname, nn); |
J. Bruce Fields | 8f93071 | 2012-05-18 22:06:41 -0400 | [diff] [blame] | 3881 | if (unconf) |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3882 | unhash_client_locked(unconf); |
Kinglong Mee | 41eb167 | 2015-07-13 17:29:41 +0800 | [diff] [blame] | 3883 | if (conf && same_verf(&conf->cl_verifier, &clverifier)) { |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 3884 | /* case 1: probable callback update */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3885 | copy_clid(new, conf); |
Kinglong Mee | 41eb167 | 2015-07-13 17:29:41 +0800 | [diff] [blame] | 3886 | gen_confirm(new, nn); |
| 3887 | } else /* case 4 (new client) or cases 2, 3 (client reboot): */ |
J. Bruce Fields | e8a79fb | 2019-03-22 11:11:06 -0400 | [diff] [blame] | 3888 | ; |
J. Bruce Fields | 8323c3b | 2010-10-19 19:36:51 -0400 | [diff] [blame] | 3889 | new->cl_minorversion = 0; |
Takuma Umeya | 6f3d772 | 2010-12-15 14:09:01 +0900 | [diff] [blame] | 3890 | gen_callback(new, setclid, rqstp); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 3891 | add_to_unconfirmed(new); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3892 | setclid->se_clientid.cl_boot = new->cl_clientid.cl_boot; |
| 3893 | setclid->se_clientid.cl_id = new->cl_clientid.cl_id; |
| 3894 | 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] | 3895 | new = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3896 | status = nfs_ok; |
| 3897 | out: |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3898 | spin_unlock(&nn->client_lock); |
Trond Myklebust | 5cc40fd | 2014-07-30 08:27:05 -0400 | [diff] [blame] | 3899 | if (new) |
| 3900 | free_client(new); |
Trond Myklebust | 3dbacee | 2014-07-30 08:27:06 -0400 | [diff] [blame] | 3901 | if (unconf) |
| 3902 | expire_client(unconf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3903 | return status; |
| 3904 | } |
| 3905 | |
| 3906 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3907 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 3908 | nfsd4_setclientid_confirm(struct svc_rqst *rqstp, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3909 | struct nfsd4_compound_state *cstate, |
| 3910 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3911 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 3912 | struct nfsd4_setclientid_confirm *setclientid_confirm = |
| 3913 | &u->setclientid_confirm; |
NeilBrown | 21ab45a | 2005-06-23 22:04:14 -0700 | [diff] [blame] | 3914 | struct nfs4_client *conf, *unconf; |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3915 | struct nfs4_client *old = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3916 | nfs4_verifier confirm = setclientid_confirm->sc_confirm; |
| 3917 | clientid_t * clid = &setclientid_confirm->sc_clientid; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 3918 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 3919 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3920 | |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 3921 | if (STALE_CLIENTID(clid, nn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3922 | return nfserr_stale_clientid; |
NeilBrown | 21ab45a | 2005-06-23 22:04:14 -0700 | [diff] [blame] | 3923 | |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3924 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 3925 | conf = find_confirmed_client(clid, false, nn); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 3926 | unconf = find_unconfirmed_client(clid, false, nn); |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 3927 | /* |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 3928 | * We try hard to give out unique clientid's, so if we get an |
| 3929 | * attempt to confirm the same clientid with a different cred, |
J. Bruce Fields | f984a7c | 2015-09-01 13:40:53 -0400 | [diff] [blame] | 3930 | * the client may be buggy; this should never happen. |
| 3931 | * |
| 3932 | * Nevertheless, RFC 7530 recommends INUSE for this case: |
J. Bruce Fields | a186e76 | 2007-11-20 16:11:27 -0500 | [diff] [blame] | 3933 | */ |
J. Bruce Fields | f984a7c | 2015-09-01 13:40:53 -0400 | [diff] [blame] | 3934 | status = nfserr_clid_inuse; |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 3935 | if (unconf && !same_creds(&unconf->cl_cred, &rqstp->rq_cred)) |
| 3936 | goto out; |
| 3937 | if (conf && !same_creds(&conf->cl_cred, &rqstp->rq_cred)) |
| 3938 | goto out; |
J. Bruce Fields | 63db463 | 2012-05-18 21:54:19 -0400 | [diff] [blame] | 3939 | /* cases below refer to rfc 3530 section 14.2.34: */ |
J. Bruce Fields | 90d700b | 2012-05-19 13:55:22 -0400 | [diff] [blame] | 3940 | if (!unconf || !same_verf(&confirm, &unconf->cl_confirm)) { |
J. Bruce Fields | 7d22fc1 | 2016-09-20 20:55:36 -0400 | [diff] [blame] | 3941 | if (conf && same_verf(&confirm, &conf->cl_confirm)) { |
| 3942 | /* case 2: probable retransmit */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3943 | status = nfs_ok; |
J. Bruce Fields | 7d22fc1 | 2016-09-20 20:55:36 -0400 | [diff] [blame] | 3944 | } else /* case 4: client hasn't noticed we rebooted yet? */ |
J. Bruce Fields | 90d700b | 2012-05-19 13:55:22 -0400 | [diff] [blame] | 3945 | status = nfserr_stale_clientid; |
| 3946 | goto out; |
| 3947 | } |
| 3948 | status = nfs_ok; |
| 3949 | if (conf) { /* case 1: callback update */ |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3950 | old = unconf; |
| 3951 | unhash_client_locked(old); |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 3952 | nfsd4_change_callback(conf, &unconf->cl_cb_conn); |
J. Bruce Fields | 90d700b | 2012-05-19 13:55:22 -0400 | [diff] [blame] | 3953 | } else { /* case 3: normal case; new or rebooted client */ |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3954 | old = find_confirmed_client_by_name(&unconf->cl_name, nn); |
| 3955 | if (old) { |
J. Bruce Fields | 2b63482 | 2015-10-15 15:33:23 -0400 | [diff] [blame] | 3956 | status = nfserr_clid_inuse; |
| 3957 | if (client_has_state(old) |
| 3958 | && !same_creds(&unconf->cl_cred, |
| 3959 | &old->cl_cred)) |
| 3960 | goto out; |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3961 | status = mark_client_expired_locked(old); |
Jeff Layton | 7abea1e | 2014-07-30 08:27:13 -0400 | [diff] [blame] | 3962 | if (status) { |
| 3963 | old = NULL; |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3964 | goto out; |
Jeff Layton | 7abea1e | 2014-07-30 08:27:13 -0400 | [diff] [blame] | 3965 | } |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 3966 | } |
J. Bruce Fields | 8695b90 | 2012-05-19 10:05:58 -0400 | [diff] [blame] | 3967 | move_to_confirmed(unconf); |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3968 | conf = unconf; |
NeilBrown | 08e8987 | 2005-06-23 22:04:11 -0700 | [diff] [blame] | 3969 | } |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3970 | get_client_locked(conf); |
| 3971 | spin_unlock(&nn->client_lock); |
| 3972 | nfsd4_probe_callback(conf); |
| 3973 | spin_lock(&nn->client_lock); |
| 3974 | put_client_renew_locked(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3975 | out: |
Jeff Layton | d20c11d | 2014-07-30 08:27:07 -0400 | [diff] [blame] | 3976 | spin_unlock(&nn->client_lock); |
| 3977 | if (old) |
| 3978 | expire_client(old); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3979 | return status; |
| 3980 | } |
| 3981 | |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3982 | static struct nfs4_file *nfsd4_alloc_file(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3983 | { |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3984 | return kmem_cache_alloc(file_slab, GFP_KERNEL); |
| 3985 | } |
| 3986 | |
| 3987 | /* OPEN Share state helper functions */ |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 3988 | static void nfsd4_init_file(struct knfsd_fh *fh, unsigned int hashval, |
| 3989 | struct nfs4_file *fp) |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3990 | { |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 3991 | lockdep_assert_held(&state_lock); |
| 3992 | |
Elena Reshetova | 818a34e | 2017-10-20 12:53:30 +0300 | [diff] [blame] | 3993 | refcount_set(&fp->fi_ref, 1); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 3994 | spin_lock_init(&fp->fi_lock); |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 3995 | INIT_LIST_HEAD(&fp->fi_stateids); |
| 3996 | INIT_LIST_HEAD(&fp->fi_delegations); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 3997 | INIT_LIST_HEAD(&fp->fi_clnt_odstate); |
Trond Myklebust | e2cf80d | 2014-07-23 16:17:38 -0400 | [diff] [blame] | 3998 | fh_copy_shallow(&fp->fi_fhandle, fh); |
Jeff Layton | 0c637be | 2014-08-22 12:05:43 -0400 | [diff] [blame] | 3999 | fp->fi_deleg_file = NULL; |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 4000 | fp->fi_had_conflict = false; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4001 | fp->fi_share_deny = 0; |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 4002 | memset(fp->fi_fds, 0, sizeof(fp->fi_fds)); |
| 4003 | memset(fp->fi_access, 0, sizeof(fp->fi_access)); |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 4004 | #ifdef CONFIG_NFSD_PNFS |
| 4005 | INIT_LIST_HEAD(&fp->fi_lo_states); |
Christoph Hellwig | c5c707f | 2014-09-23 12:38:48 +0200 | [diff] [blame] | 4006 | atomic_set(&fp->fi_lo_recalls, 0); |
Christoph Hellwig | 9cf514c | 2014-05-05 13:11:59 +0200 | [diff] [blame] | 4007 | #endif |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 4008 | hlist_add_head_rcu(&fp->fi_hash, &file_hashtbl[hashval]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4009 | } |
| 4010 | |
J. Bruce Fields | e8ff2a8 | 2007-08-01 15:30:59 -0400 | [diff] [blame] | 4011 | void |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 4012 | nfsd4_free_slabs(void) |
| 4013 | { |
Jeff Layton | 9258a2d | 2018-03-16 09:47:22 -0400 | [diff] [blame] | 4014 | kmem_cache_destroy(client_slab); |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 4015 | kmem_cache_destroy(openowner_slab); |
| 4016 | kmem_cache_destroy(lockowner_slab); |
| 4017 | kmem_cache_destroy(file_slab); |
| 4018 | kmem_cache_destroy(stateid_slab); |
| 4019 | kmem_cache_destroy(deleg_slab); |
Jeff Layton | 9258a2d | 2018-03-16 09:47:22 -0400 | [diff] [blame] | 4020 | kmem_cache_destroy(odstate_slab); |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 4021 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4022 | |
Bryan Schumaker | 7208339 | 2011-11-01 15:24:59 -0400 | [diff] [blame] | 4023 | int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4024 | nfsd4_init_slabs(void) |
| 4025 | { |
Jeff Layton | 9258a2d | 2018-03-16 09:47:22 -0400 | [diff] [blame] | 4026 | client_slab = kmem_cache_create("nfsd4_clients", |
| 4027 | sizeof(struct nfs4_client), 0, 0, NULL); |
| 4028 | if (client_slab == NULL) |
| 4029 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4030 | openowner_slab = kmem_cache_create("nfsd4_openowners", |
| 4031 | sizeof(struct nfs4_openowner), 0, 0, NULL); |
| 4032 | if (openowner_slab == NULL) |
Jeff Layton | 9258a2d | 2018-03-16 09:47:22 -0400 | [diff] [blame] | 4033 | goto out_free_client_slab; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4034 | lockowner_slab = kmem_cache_create("nfsd4_lockowners", |
Yanchuan Nian | 3c40794 | 2012-10-24 14:44:19 +0800 | [diff] [blame] | 4035 | sizeof(struct nfs4_lockowner), 0, 0, NULL); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4036 | if (lockowner_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 4037 | goto out_free_openowner_slab; |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 4038 | file_slab = kmem_cache_create("nfsd4_files", |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 4039 | sizeof(struct nfs4_file), 0, 0, NULL); |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 4040 | if (file_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 4041 | goto out_free_lockowner_slab; |
NeilBrown | 5ac049a | 2005-06-23 22:03:03 -0700 | [diff] [blame] | 4042 | stateid_slab = kmem_cache_create("nfsd4_stateids", |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4043 | sizeof(struct nfs4_ol_stateid), 0, 0, NULL); |
NeilBrown | 5ac049a | 2005-06-23 22:03:03 -0700 | [diff] [blame] | 4044 | if (stateid_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 4045 | goto out_free_file_slab; |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 4046 | deleg_slab = kmem_cache_create("nfsd4_delegations", |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 4047 | sizeof(struct nfs4_delegation), 0, 0, NULL); |
NeilBrown | 5b2d21c | 2005-06-23 22:03:04 -0700 | [diff] [blame] | 4048 | if (deleg_slab == NULL) |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 4049 | goto out_free_stateid_slab; |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 4050 | odstate_slab = kmem_cache_create("nfsd4_odstate", |
| 4051 | sizeof(struct nfs4_clnt_odstate), 0, 0, NULL); |
| 4052 | if (odstate_slab == NULL) |
| 4053 | goto out_free_deleg_slab; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4054 | return 0; |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 4055 | |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 4056 | out_free_deleg_slab: |
| 4057 | kmem_cache_destroy(deleg_slab); |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 4058 | out_free_stateid_slab: |
| 4059 | kmem_cache_destroy(stateid_slab); |
| 4060 | out_free_file_slab: |
| 4061 | kmem_cache_destroy(file_slab); |
| 4062 | out_free_lockowner_slab: |
| 4063 | kmem_cache_destroy(lockowner_slab); |
| 4064 | out_free_openowner_slab: |
| 4065 | kmem_cache_destroy(openowner_slab); |
Jeff Layton | 9258a2d | 2018-03-16 09:47:22 -0400 | [diff] [blame] | 4066 | out_free_client_slab: |
| 4067 | kmem_cache_destroy(client_slab); |
Christoph Hellwig | abf1135 | 2014-05-21 07:43:03 -0700 | [diff] [blame] | 4068 | out: |
NeilBrown | e60d439 | 2005-06-23 22:03:01 -0700 | [diff] [blame] | 4069 | dprintk("nfsd4: out of memory while initializing nfsv4\n"); |
| 4070 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4071 | } |
| 4072 | |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4073 | static void init_nfs4_replay(struct nfs4_replay *rp) |
| 4074 | { |
| 4075 | rp->rp_status = nfserr_serverfault; |
| 4076 | rp->rp_buflen = 0; |
| 4077 | rp->rp_buf = rp->rp_ibuf; |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 4078 | mutex_init(&rp->rp_mutex); |
| 4079 | } |
| 4080 | |
| 4081 | static void nfsd4_cstate_assign_replay(struct nfsd4_compound_state *cstate, |
| 4082 | struct nfs4_stateowner *so) |
| 4083 | { |
| 4084 | if (!nfsd4_has_session(cstate)) { |
| 4085 | mutex_lock(&so->so_replay.rp_mutex); |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 4086 | cstate->replay_owner = nfs4_get_stateowner(so); |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 4087 | } |
| 4088 | } |
| 4089 | |
| 4090 | void nfsd4_cstate_clear_replay(struct nfsd4_compound_state *cstate) |
| 4091 | { |
| 4092 | struct nfs4_stateowner *so = cstate->replay_owner; |
| 4093 | |
| 4094 | if (so != NULL) { |
| 4095 | cstate->replay_owner = NULL; |
| 4096 | mutex_unlock(&so->so_replay.rp_mutex); |
| 4097 | nfs4_put_stateowner(so); |
| 4098 | } |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4099 | } |
| 4100 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4101 | 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] | 4102 | { |
| 4103 | struct nfs4_stateowner *sop; |
| 4104 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4105 | sop = kmem_cache_alloc(slab, GFP_KERNEL); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4106 | if (!sop) |
| 4107 | return NULL; |
| 4108 | |
J. Bruce Fields | 6f4859b | 2019-06-19 14:30:33 -0400 | [diff] [blame] | 4109 | xdr_netobj_dup(&sop->so_owner, owner, GFP_KERNEL); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4110 | if (!sop->so_owner.data) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4111 | kmem_cache_free(slab, sop); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4112 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4113 | } |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4114 | |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4115 | INIT_LIST_HEAD(&sop->so_stateids); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4116 | sop->so_client = clp; |
| 4117 | init_nfs4_replay(&sop->so_replay); |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 4118 | atomic_set(&sop->so_count, 1); |
J. Bruce Fields | ff194bd | 2011-08-12 09:42:57 -0400 | [diff] [blame] | 4119 | return sop; |
| 4120 | } |
| 4121 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4122 | 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] | 4123 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 4124 | lockdep_assert_held(&clp->cl_lock); |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 4125 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 4126 | list_add(&oo->oo_owner.so_strhash, |
| 4127 | &clp->cl_ownerstr_hashtbl[strhashval]); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4128 | list_add(&oo->oo_perclient, &clp->cl_openowners); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4129 | } |
| 4130 | |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 4131 | static void nfs4_unhash_openowner(struct nfs4_stateowner *so) |
| 4132 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 4133 | unhash_openowner_locked(openowner(so)); |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 4134 | } |
| 4135 | |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 4136 | static void nfs4_free_openowner(struct nfs4_stateowner *so) |
| 4137 | { |
| 4138 | struct nfs4_openowner *oo = openowner(so); |
| 4139 | |
| 4140 | kmem_cache_free(openowner_slab, oo); |
| 4141 | } |
| 4142 | |
| 4143 | static const struct nfs4_stateowner_operations openowner_ops = { |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 4144 | .so_unhash = nfs4_unhash_openowner, |
| 4145 | .so_free = nfs4_free_openowner, |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 4146 | }; |
| 4147 | |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4148 | static struct nfs4_ol_stateid * |
| 4149 | nfsd4_find_existing_open(struct nfs4_file *fp, struct nfsd4_open *open) |
| 4150 | { |
| 4151 | struct nfs4_ol_stateid *local, *ret = NULL; |
| 4152 | struct nfs4_openowner *oo = open->op_openowner; |
| 4153 | |
| 4154 | lockdep_assert_held(&fp->fi_lock); |
| 4155 | |
| 4156 | list_for_each_entry(local, &fp->fi_stateids, st_perfile) { |
| 4157 | /* ignore lock owners */ |
| 4158 | if (local->st_stateowner->so_is_open_owner == 0) |
| 4159 | continue; |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 4160 | if (local->st_stateowner != &oo->oo_owner) |
| 4161 | continue; |
| 4162 | if (local->st_stid.sc_type == NFS4_OPEN_STID) { |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4163 | ret = local; |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 4164 | refcount_inc(&ret->st_stid.sc_count); |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4165 | break; |
| 4166 | } |
| 4167 | } |
| 4168 | return ret; |
| 4169 | } |
| 4170 | |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 4171 | static __be32 |
| 4172 | nfsd4_verify_open_stid(struct nfs4_stid *s) |
| 4173 | { |
| 4174 | __be32 ret = nfs_ok; |
| 4175 | |
| 4176 | switch (s->sc_type) { |
| 4177 | default: |
| 4178 | break; |
Trond Myklebust | 4f17641 | 2018-01-12 17:42:30 -0500 | [diff] [blame] | 4179 | case 0: |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 4180 | case NFS4_CLOSED_STID: |
| 4181 | case NFS4_CLOSED_DELEG_STID: |
| 4182 | ret = nfserr_bad_stateid; |
| 4183 | break; |
| 4184 | case NFS4_REVOKED_DELEG_STID: |
| 4185 | ret = nfserr_deleg_revoked; |
| 4186 | } |
| 4187 | return ret; |
| 4188 | } |
| 4189 | |
| 4190 | /* Lock the stateid st_mutex, and deal with races with CLOSE */ |
| 4191 | static __be32 |
| 4192 | nfsd4_lock_ol_stateid(struct nfs4_ol_stateid *stp) |
| 4193 | { |
| 4194 | __be32 ret; |
| 4195 | |
Andrew Elble | 4f34bd0 | 2017-11-08 17:29:51 -0500 | [diff] [blame] | 4196 | mutex_lock_nested(&stp->st_mutex, LOCK_STATEID_MUTEX); |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 4197 | ret = nfsd4_verify_open_stid(&stp->st_stid); |
| 4198 | if (ret != nfs_ok) |
| 4199 | mutex_unlock(&stp->st_mutex); |
| 4200 | return ret; |
| 4201 | } |
| 4202 | |
| 4203 | static struct nfs4_ol_stateid * |
| 4204 | nfsd4_find_and_lock_existing_open(struct nfs4_file *fp, struct nfsd4_open *open) |
| 4205 | { |
| 4206 | struct nfs4_ol_stateid *stp; |
| 4207 | for (;;) { |
| 4208 | spin_lock(&fp->fi_lock); |
| 4209 | stp = nfsd4_find_existing_open(fp, open); |
| 4210 | spin_unlock(&fp->fi_lock); |
| 4211 | if (!stp || nfsd4_lock_ol_stateid(stp) == nfs_ok) |
| 4212 | break; |
| 4213 | nfs4_put_stid(&stp->st_stid); |
| 4214 | } |
| 4215 | return stp; |
| 4216 | } |
| 4217 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4218 | static struct nfs4_openowner * |
Trond Myklebust | 13d6f66 | 2014-06-30 11:48:45 -0400 | [diff] [blame] | 4219 | alloc_init_open_stateowner(unsigned int strhashval, struct nfsd4_open *open, |
Jeff Layton | db24b3b | 2014-06-30 11:48:36 -0400 | [diff] [blame] | 4220 | struct nfsd4_compound_state *cstate) |
| 4221 | { |
Trond Myklebust | 13d6f66 | 2014-06-30 11:48:45 -0400 | [diff] [blame] | 4222 | struct nfs4_client *clp = cstate->clp; |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 4223 | struct nfs4_openowner *oo, *ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4224 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4225 | oo = alloc_stateowner(openowner_slab, &open->op_owner, clp); |
| 4226 | if (!oo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4227 | return NULL; |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 4228 | oo->oo_owner.so_ops = &openowner_ops; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4229 | oo->oo_owner.so_is_open_owner = 1; |
| 4230 | oo->oo_owner.so_seqid = open->op_seqid; |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 4231 | oo->oo_flags = 0; |
Jeff Layton | db24b3b | 2014-06-30 11:48:36 -0400 | [diff] [blame] | 4232 | if (nfsd4_has_session(cstate)) |
| 4233 | oo->oo_flags |= NFS4_OO_CONFIRMED; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4234 | oo->oo_time = 0; |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 4235 | oo->oo_last_closed_stid = NULL; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4236 | INIT_LIST_HEAD(&oo->oo_close_lru); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 4237 | spin_lock(&clp->cl_lock); |
| 4238 | ret = find_openstateowner_str_locked(strhashval, open, clp); |
Trond Myklebust | 7ffb588 | 2014-07-29 21:34:34 -0400 | [diff] [blame] | 4239 | if (ret == NULL) { |
| 4240 | hash_openowner(oo, clp, strhashval); |
| 4241 | ret = oo; |
| 4242 | } else |
Kinglong Mee | d50ffde | 2015-07-16 12:05:07 +0800 | [diff] [blame] | 4243 | nfs4_free_stateowner(&oo->oo_owner); |
| 4244 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 4245 | spin_unlock(&clp->cl_lock); |
Jeff Layton | c595233 | 2015-03-23 10:53:42 -0400 | [diff] [blame] | 4246 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4247 | } |
| 4248 | |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4249 | static struct nfs4_ol_stateid * |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 4250 | init_open_stateid(struct nfs4_file *fp, struct nfsd4_open *open) |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4251 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4252 | |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4253 | struct nfs4_openowner *oo = open->op_openowner; |
| 4254 | struct nfs4_ol_stateid *retstp = NULL; |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 4255 | struct nfs4_ol_stateid *stp; |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4256 | |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 4257 | stp = open->op_stp; |
Oleg Drokin | 5cc1fb2 | 2016-06-14 23:28:05 -0400 | [diff] [blame] | 4258 | /* We are moving these outside of the spinlocks to avoid the warnings */ |
| 4259 | mutex_init(&stp->st_mutex); |
Andrew Elble | 4f34bd0 | 2017-11-08 17:29:51 -0500 | [diff] [blame] | 4260 | mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX); |
Oleg Drokin | 5cc1fb2 | 2016-06-14 23:28:05 -0400 | [diff] [blame] | 4261 | |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 4262 | retry: |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4263 | spin_lock(&oo->oo_owner.so_client->cl_lock); |
| 4264 | spin_lock(&fp->fi_lock); |
| 4265 | |
| 4266 | retstp = nfsd4_find_existing_open(fp, open); |
| 4267 | if (retstp) |
| 4268 | goto out_unlock; |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 4269 | |
| 4270 | open->op_stp = NULL; |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 4271 | refcount_inc(&stp->st_stid.sc_count); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 4272 | stp->st_stid.sc_type = NFS4_OPEN_STID; |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 4273 | INIT_LIST_HEAD(&stp->st_locks); |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 4274 | stp->st_stateowner = nfs4_get_stateowner(&oo->oo_owner); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 4275 | get_nfs4_file(fp); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 4276 | stp->st_stid.sc_file = fp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4277 | stp->st_access_bmap = 0; |
| 4278 | stp->st_deny_bmap = 0; |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 4279 | stp->st_openstp = NULL; |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 4280 | list_add(&stp->st_perstateowner, &oo->oo_owner.so_stateids); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 4281 | list_add(&stp->st_perfile, &fp->fi_stateids); |
Andrew Elble | 7fc0564 | 2015-11-05 20:42:43 -0500 | [diff] [blame] | 4282 | |
| 4283 | out_unlock: |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 4284 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 4285 | spin_unlock(&oo->oo_owner.so_client->cl_lock); |
Oleg Drokin | 5cc1fb2 | 2016-06-14 23:28:05 -0400 | [diff] [blame] | 4286 | if (retstp) { |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 4287 | /* Handle races with CLOSE */ |
| 4288 | if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) { |
| 4289 | nfs4_put_stid(&retstp->st_stid); |
| 4290 | goto retry; |
| 4291 | } |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 4292 | /* To keep mutex tracking happy */ |
Oleg Drokin | 5cc1fb2 | 2016-06-14 23:28:05 -0400 | [diff] [blame] | 4293 | mutex_unlock(&stp->st_mutex); |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 4294 | stp = retstp; |
Oleg Drokin | 5cc1fb2 | 2016-06-14 23:28:05 -0400 | [diff] [blame] | 4295 | } |
Oleg Drokin | 8c7245ab | 2016-06-14 23:28:06 -0400 | [diff] [blame] | 4296 | return stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4297 | } |
| 4298 | |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 4299 | /* |
| 4300 | * In the 4.0 case we need to keep the owners around a little while to handle |
| 4301 | * CLOSE replay. We still do need to release any file access that is held by |
| 4302 | * them before returning however. |
| 4303 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4304 | static void |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 4305 | move_to_close_lru(struct nfs4_ol_stateid *s, struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4306 | { |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 4307 | struct nfs4_ol_stateid *last; |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 4308 | struct nfs4_openowner *oo = openowner(s->st_stateowner); |
| 4309 | struct nfsd_net *nn = net_generic(s->st_stid.sc_client->net, |
| 4310 | nfsd_net_id); |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 4311 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4312 | dprintk("NFSD: move_to_close_lru nfs4_openowner %p\n", oo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4313 | |
Jeff Layton | b401be22 | 2014-07-29 21:34:33 -0400 | [diff] [blame] | 4314 | /* |
| 4315 | * We know that we hold one reference via nfsd4_close, and another |
| 4316 | * "persistent" reference for the client. If the refcount is higher |
| 4317 | * than 2, then there are still calls in progress that are using this |
| 4318 | * stateid. We can't put the sc_file reference until they are finished. |
| 4319 | * Wait for the refcount to drop to 2. Since it has been unhashed, |
| 4320 | * there should be no danger of the refcount going back up again at |
| 4321 | * this point. |
| 4322 | */ |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 4323 | wait_event(close_wq, refcount_read(&s->st_stid.sc_count) == 2); |
Jeff Layton | b401be22 | 2014-07-29 21:34:33 -0400 | [diff] [blame] | 4324 | |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 4325 | release_all_access(s); |
| 4326 | if (s->st_stid.sc_file) { |
| 4327 | put_nfs4_file(s->st_stid.sc_file); |
| 4328 | s->st_stid.sc_file = NULL; |
| 4329 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 4330 | |
| 4331 | spin_lock(&nn->client_lock); |
| 4332 | last = oo->oo_last_closed_stid; |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 4333 | oo->oo_last_closed_stid = s; |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 4334 | list_move_tail(&oo->oo_close_lru, &nn->close_lru); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4335 | oo->oo_time = get_seconds(); |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 4336 | spin_unlock(&nn->client_lock); |
| 4337 | if (last) |
| 4338 | nfs4_put_stid(&last->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4339 | } |
| 4340 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4341 | /* search file_hashtbl[] for file */ |
| 4342 | static struct nfs4_file * |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 4343 | find_file_locked(struct knfsd_fh *fh, unsigned int hashval) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4344 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4345 | struct nfs4_file *fp; |
| 4346 | |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 4347 | hlist_for_each_entry_rcu(fp, &file_hashtbl[hashval], fi_hash) { |
Christoph Hellwig | 4d94c2e | 2014-08-14 08:41:48 +0200 | [diff] [blame] | 4348 | if (fh_match(&fp->fi_fhandle, fh)) { |
Elena Reshetova | 818a34e | 2017-10-20 12:53:30 +0300 | [diff] [blame] | 4349 | if (refcount_inc_not_zero(&fp->fi_ref)) |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 4350 | return fp; |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 4351 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4352 | } |
| 4353 | return NULL; |
| 4354 | } |
| 4355 | |
Christoph Hellwig | e6ba76e | 2014-08-14 08:50:16 +0200 | [diff] [blame] | 4356 | struct nfs4_file * |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 4357 | find_file(struct knfsd_fh *fh) |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 4358 | { |
| 4359 | struct nfs4_file *fp; |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 4360 | unsigned int hashval = file_hashval(fh); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 4361 | |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 4362 | rcu_read_lock(); |
| 4363 | fp = find_file_locked(fh, hashval); |
| 4364 | rcu_read_unlock(); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 4365 | return fp; |
| 4366 | } |
| 4367 | |
| 4368 | static struct nfs4_file * |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 4369 | find_or_add_file(struct nfs4_file *new, struct knfsd_fh *fh) |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 4370 | { |
| 4371 | struct nfs4_file *fp; |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 4372 | unsigned int hashval = file_hashval(fh); |
| 4373 | |
| 4374 | rcu_read_lock(); |
| 4375 | fp = find_file_locked(fh, hashval); |
| 4376 | rcu_read_unlock(); |
| 4377 | if (fp) |
| 4378 | return fp; |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 4379 | |
| 4380 | spin_lock(&state_lock); |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 4381 | fp = find_file_locked(fh, hashval); |
| 4382 | if (likely(fp == NULL)) { |
| 4383 | nfsd4_init_file(fh, hashval, new); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 4384 | fp = new; |
| 4385 | } |
| 4386 | spin_unlock(&state_lock); |
| 4387 | |
| 4388 | return fp; |
| 4389 | } |
| 4390 | |
J. Bruce Fields | 4f83aa3 | 2008-07-07 15:02:02 -0400 | [diff] [blame] | 4391 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4392 | * Called to check deny when READ with all zero stateid or |
| 4393 | * WRITE with all zero or all one stateid |
| 4394 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4395 | static __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4396 | nfs4_share_conflict(struct svc_fh *current_fh, unsigned int deny_type) |
| 4397 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4398 | struct nfs4_file *fp; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4399 | __be32 ret = nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4400 | |
Trond Myklebust | ca94321 | 2014-07-23 16:17:39 -0400 | [diff] [blame] | 4401 | fp = find_file(¤t_fh->fh_handle); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 4402 | if (!fp) |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4403 | return ret; |
| 4404 | /* Check for conflicting share reservations */ |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 4405 | spin_lock(&fp->fi_lock); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4406 | if (fp->fi_share_deny & deny_type) |
| 4407 | ret = nfserr_locked; |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 4408 | spin_unlock(&fp->fi_lock); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 4409 | put_nfs4_file(fp); |
| 4410 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4411 | } |
| 4412 | |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 4413 | static void nfsd4_cb_recall_prepare(struct nfsd4_callback *cb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4414 | { |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 4415 | struct nfs4_delegation *dp = cb_to_delegation(cb); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 4416 | struct nfsd_net *nn = net_generic(dp->dl_stid.sc_client->net, |
| 4417 | nfsd_net_id); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 4418 | |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 4419 | block_delegations(&dp->dl_stid.sc_file->fi_fhandle); |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 4420 | |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 4421 | /* |
| 4422 | * 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] | 4423 | * already holding inode->i_lock. |
| 4424 | * |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 4425 | * If the dl_time != 0, then we know that it has already been |
| 4426 | * queued for a lease break. Don't queue it again. |
| 4427 | */ |
Jeff Layton | f54fe96 | 2014-07-25 07:34:26 -0400 | [diff] [blame] | 4428 | spin_lock(&state_lock); |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 4429 | if (dp->dl_time == 0) { |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 4430 | dp->dl_time = get_seconds(); |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 4431 | list_add_tail(&dp->dl_recall_lru, &nn->del_recall_lru); |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 4432 | } |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 4433 | spin_unlock(&state_lock); |
| 4434 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4435 | |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 4436 | static int nfsd4_cb_recall_done(struct nfsd4_callback *cb, |
| 4437 | struct rpc_task *task) |
| 4438 | { |
| 4439 | struct nfs4_delegation *dp = cb_to_delegation(cb); |
| 4440 | |
Andrew Elble | a457974 | 2015-08-31 12:06:41 -0400 | [diff] [blame] | 4441 | if (dp->dl_stid.sc_type == NFS4_CLOSED_DELEG_STID) |
| 4442 | return 1; |
| 4443 | |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 4444 | switch (task->tk_status) { |
| 4445 | case 0: |
| 4446 | return 1; |
Scott Mayhew | 1c73b9d | 2019-05-02 13:32:12 -0400 | [diff] [blame] | 4447 | case -NFS4ERR_DELAY: |
| 4448 | rpc_delay(task, 2 * HZ); |
| 4449 | return 0; |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 4450 | case -EBADHANDLE: |
| 4451 | case -NFS4ERR_BAD_STATEID: |
| 4452 | /* |
| 4453 | * Race: client probably got cb_recall before open reply |
| 4454 | * granting delegation. |
| 4455 | */ |
| 4456 | if (dp->dl_retries--) { |
| 4457 | rpc_delay(task, 2 * HZ); |
| 4458 | return 0; |
| 4459 | } |
| 4460 | /*FALLTHRU*/ |
| 4461 | default: |
Scott Mayhew | 1c73b9d | 2019-05-02 13:32:12 -0400 | [diff] [blame] | 4462 | return 1; |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 4463 | } |
| 4464 | } |
| 4465 | |
| 4466 | static void nfsd4_cb_recall_release(struct nfsd4_callback *cb) |
| 4467 | { |
| 4468 | struct nfs4_delegation *dp = cb_to_delegation(cb); |
| 4469 | |
| 4470 | nfs4_put_stid(&dp->dl_stid); |
| 4471 | } |
| 4472 | |
Julia Lawall | c4cb897 | 2015-11-21 22:57:39 +0100 | [diff] [blame] | 4473 | static const struct nfsd4_callback_ops nfsd4_cb_recall_ops = { |
Christoph Hellwig | 0162ac2 | 2014-09-24 12:19:19 +0200 | [diff] [blame] | 4474 | .prepare = nfsd4_cb_recall_prepare, |
| 4475 | .done = nfsd4_cb_recall_done, |
| 4476 | .release = nfsd4_cb_recall_release, |
| 4477 | }; |
| 4478 | |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 4479 | static void nfsd_break_one_deleg(struct nfs4_delegation *dp) |
| 4480 | { |
| 4481 | /* |
| 4482 | * We're assuming the state code never drops its reference |
| 4483 | * without first removing the lease. Since we're in this lease |
J. Bruce Fields | 4a269efb | 2018-04-13 17:34:35 -0400 | [diff] [blame] | 4484 | * callback (and since the lease code is serialized by the |
| 4485 | * i_lock) we know the server hasn't removed the lease yet, and |
| 4486 | * we know it's safe to take a reference. |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 4487 | */ |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 4488 | refcount_inc(&dp->dl_stid.sc_count); |
Christoph Hellwig | f0b5de1 | 2014-09-24 12:19:18 +0200 | [diff] [blame] | 4489 | nfsd4_run_cb(&dp->dl_recall); |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 4490 | } |
| 4491 | |
Jeff Layton | 1c8c601 | 2013-06-21 08:58:15 -0400 | [diff] [blame] | 4492 | /* Called from break_lease() with i_lock held. */ |
Jeff Layton | 4d01b7f | 2014-09-01 15:06:54 -0400 | [diff] [blame] | 4493 | static bool |
| 4494 | nfsd_break_deleg_cb(struct file_lock *fl) |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 4495 | { |
Jeff Layton | 4d01b7f | 2014-09-01 15:06:54 -0400 | [diff] [blame] | 4496 | bool ret = false; |
J. Bruce Fields | 653e514 | 2018-02-15 22:08:45 -0500 | [diff] [blame] | 4497 | struct nfs4_delegation *dp = (struct nfs4_delegation *)fl->fl_owner; |
| 4498 | struct nfs4_file *fp = dp->dl_stid.sc_file; |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 4499 | |
J. Bruce Fields | 0272e1f | 2007-09-12 18:56:12 -0400 | [diff] [blame] | 4500 | /* |
| 4501 | * 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] | 4502 | * we'll remove it ourself if a delegation isn't returned |
J. Bruce Fields | 6b57d9c | 2011-01-31 11:54:04 -0500 | [diff] [blame] | 4503 | * in time: |
J. Bruce Fields | 0272e1f | 2007-09-12 18:56:12 -0400 | [diff] [blame] | 4504 | */ |
| 4505 | fl->fl_break_time = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4506 | |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 4507 | spin_lock(&fp->fi_lock); |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4508 | fp->fi_had_conflict = true; |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 4509 | nfsd_break_one_deleg(dp); |
Jeff Layton | 02e1215 | 2014-07-16 10:31:57 -0400 | [diff] [blame] | 4510 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 4d01b7f | 2014-09-01 15:06:54 -0400 | [diff] [blame] | 4511 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4512 | } |
| 4513 | |
Jeff Layton | c45198e | 2014-09-01 07:12:07 -0400 | [diff] [blame] | 4514 | static int |
Jeff Layton | 7448cc3 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 4515 | nfsd_change_deleg_cb(struct file_lock *onlist, int arg, |
| 4516 | struct list_head *dispose) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4517 | { |
| 4518 | if (arg & F_UNLCK) |
Jeff Layton | c45198e | 2014-09-01 07:12:07 -0400 | [diff] [blame] | 4519 | return lease_modify(onlist, arg, dispose); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4520 | else |
| 4521 | return -EAGAIN; |
| 4522 | } |
| 4523 | |
Alexey Dobriyan | 7b02196 | 2009-09-21 17:01:12 -0700 | [diff] [blame] | 4524 | static const struct lock_manager_operations nfsd_lease_mng_ops = { |
J. Bruce Fields | 8fb47a4 | 2011-07-20 20:21:59 -0400 | [diff] [blame] | 4525 | .lm_break = nfsd_break_deleg_cb, |
| 4526 | .lm_change = nfsd_change_deleg_cb, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4527 | }; |
| 4528 | |
J. Bruce Fields | 7a8711c | 2011-09-02 09:03:37 -0400 | [diff] [blame] | 4529 | static __be32 nfsd4_check_seqid(struct nfsd4_compound_state *cstate, struct nfs4_stateowner *so, u32 seqid) |
| 4530 | { |
| 4531 | if (nfsd4_has_session(cstate)) |
| 4532 | return nfs_ok; |
| 4533 | if (seqid == so->so_seqid - 1) |
| 4534 | return nfserr_replay_me; |
| 4535 | if (seqid == so->so_seqid) |
| 4536 | return nfs_ok; |
| 4537 | return nfserr_bad_seqid; |
| 4538 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4539 | |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4540 | static __be32 lookup_clientid(clientid_t *clid, |
| 4541 | struct nfsd4_compound_state *cstate, |
| 4542 | struct nfsd_net *nn) |
| 4543 | { |
| 4544 | struct nfs4_client *found; |
| 4545 | |
| 4546 | if (cstate->clp) { |
| 4547 | found = cstate->clp; |
| 4548 | if (!same_clid(&found->cl_clientid, clid)) |
| 4549 | return nfserr_stale_clientid; |
| 4550 | return nfs_ok; |
| 4551 | } |
| 4552 | |
| 4553 | if (STALE_CLIENTID(clid, nn)) |
| 4554 | return nfserr_stale_clientid; |
| 4555 | |
| 4556 | /* |
| 4557 | * For v4.1+ we get the client in the SEQUENCE op. If we don't have one |
| 4558 | * cached already then we know this is for is for v4.0 and "sessions" |
| 4559 | * will be false. |
| 4560 | */ |
| 4561 | WARN_ON_ONCE(cstate->session); |
Trond Myklebust | 3e339f9 | 2014-07-30 08:27:09 -0400 | [diff] [blame] | 4562 | spin_lock(&nn->client_lock); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4563 | found = find_confirmed_client(clid, false, nn); |
Trond Myklebust | 3e339f9 | 2014-07-30 08:27:09 -0400 | [diff] [blame] | 4564 | if (!found) { |
| 4565 | spin_unlock(&nn->client_lock); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4566 | return nfserr_expired; |
Trond Myklebust | 3e339f9 | 2014-07-30 08:27:09 -0400 | [diff] [blame] | 4567 | } |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 4568 | atomic_inc(&found->cl_rpc_users); |
Trond Myklebust | 3e339f9 | 2014-07-30 08:27:09 -0400 | [diff] [blame] | 4569 | spin_unlock(&nn->client_lock); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4570 | |
| 4571 | /* Cache the nfs4_client in cstate! */ |
| 4572 | cstate->clp = found; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 4573 | return nfs_ok; |
| 4574 | } |
| 4575 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4576 | __be32 |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 4577 | nfsd4_process_open1(struct nfsd4_compound_state *cstate, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 4578 | struct nfsd4_open *open, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4579 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4580 | clientid_t *clientid = &open->op_clientid; |
| 4581 | struct nfs4_client *clp = NULL; |
| 4582 | unsigned int strhashval; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4583 | struct nfs4_openowner *oo = NULL; |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 4584 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4585 | |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 4586 | if (STALE_CLIENTID(&open->op_clientid, nn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4587 | return nfserr_stale_clientid; |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 4588 | /* |
| 4589 | * In case we need it later, after we've already created the |
| 4590 | * file and don't want to risk a further failure: |
| 4591 | */ |
| 4592 | open->op_file = nfsd4_alloc_file(); |
| 4593 | if (open->op_file == NULL) |
| 4594 | return nfserr_jukebox; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4595 | |
Trond Myklebust | 2d91e89 | 2014-06-30 11:48:46 -0400 | [diff] [blame] | 4596 | status = lookup_clientid(clientid, cstate, nn); |
| 4597 | if (status) |
| 4598 | return status; |
| 4599 | clp = cstate->clp; |
| 4600 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 4601 | strhashval = ownerstr_hashval(&open->op_owner); |
| 4602 | oo = find_openstateowner_str(strhashval, open, clp); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4603 | open->op_openowner = oo; |
| 4604 | if (!oo) { |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 4605 | goto new_owner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4606 | } |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 4607 | if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) { |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 4608 | /* Replace unconfirmed owners without checking for replay. */ |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4609 | release_openowner(oo); |
| 4610 | open->op_openowner = NULL; |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 4611 | goto new_owner; |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 4612 | } |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 4613 | status = nfsd4_check_seqid(cstate, &oo->oo_owner, open->op_seqid); |
| 4614 | if (status) |
| 4615 | return status; |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 4616 | goto alloc_stateid; |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 4617 | new_owner: |
Trond Myklebust | 13d6f66 | 2014-06-30 11:48:45 -0400 | [diff] [blame] | 4618 | oo = alloc_init_open_stateowner(strhashval, open, cstate); |
J. Bruce Fields | bcf130f | 2011-10-12 20:44:20 -0400 | [diff] [blame] | 4619 | if (oo == NULL) |
| 4620 | return nfserr_jukebox; |
| 4621 | open->op_openowner = oo; |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 4622 | alloc_stateid: |
Jeff Layton | b49e084 | 2014-07-29 21:34:11 -0400 | [diff] [blame] | 4623 | open->op_stp = nfs4_alloc_open_stateid(clp); |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 4624 | if (!open->op_stp) |
| 4625 | return nfserr_jukebox; |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 4626 | |
| 4627 | if (nfsd4_has_session(cstate) && |
| 4628 | (cstate->current_fh.fh_export->ex_flags & NFSEXP_PNFS)) { |
| 4629 | open->op_odstate = alloc_clnt_odstate(clp); |
| 4630 | if (!open->op_odstate) |
| 4631 | return nfserr_jukebox; |
| 4632 | } |
| 4633 | |
J. Bruce Fields | 0f442aa | 2006-01-18 17:43:34 -0800 | [diff] [blame] | 4634 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4635 | } |
| 4636 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4637 | static inline __be32 |
NeilBrown | 4a6e43e | 2005-06-23 22:02:50 -0700 | [diff] [blame] | 4638 | nfs4_check_delegmode(struct nfs4_delegation *dp, int flags) |
| 4639 | { |
| 4640 | if ((flags & WR_STATE) && (dp->dl_type == NFS4_OPEN_DELEGATE_READ)) |
| 4641 | return nfserr_openmode; |
| 4642 | else |
| 4643 | return nfs_ok; |
| 4644 | } |
| 4645 | |
Daniel Mack | c47d832 | 2011-05-16 16:38:14 +0200 | [diff] [blame] | 4646 | static int share_access_to_flags(u32 share_access) |
J. Bruce Fields | 24a0111 | 2010-05-18 20:01:35 -0400 | [diff] [blame] | 4647 | { |
J. Bruce Fields | 24a0111 | 2010-05-18 20:01:35 -0400 | [diff] [blame] | 4648 | return share_access == NFS4_SHARE_ACCESS_READ ? RD_STATE : WR_STATE; |
| 4649 | } |
| 4650 | |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 4651 | 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] | 4652 | { |
| 4653 | struct nfs4_stid *ret; |
| 4654 | |
Andrew Elble | 95da1b3a | 2017-11-03 14:06:31 -0400 | [diff] [blame] | 4655 | ret = find_stateid_by_type(cl, s, |
| 4656 | NFS4_DELEG_STID|NFS4_REVOKED_DELEG_STID); |
J. Bruce Fields | f459e45 | 2011-09-09 09:06:12 -0400 | [diff] [blame] | 4657 | if (!ret) |
| 4658 | return NULL; |
| 4659 | return delegstateid(ret); |
| 4660 | } |
| 4661 | |
J. Bruce Fields | 8b289b2 | 2011-10-19 11:52:12 -0400 | [diff] [blame] | 4662 | static bool nfsd4_is_deleg_cur(struct nfsd4_open *open) |
| 4663 | { |
| 4664 | return open->op_claim_type == NFS4_OPEN_CLAIM_DELEGATE_CUR || |
| 4665 | open->op_claim_type == NFS4_OPEN_CLAIM_DELEG_CUR_FH; |
| 4666 | } |
| 4667 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4668 | static __be32 |
J. Bruce Fields | 41d2266 | 2013-03-21 15:49:47 -0400 | [diff] [blame] | 4669 | nfs4_check_deleg(struct nfs4_client *cl, struct nfsd4_open *open, |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 4670 | struct nfs4_delegation **dp) |
| 4671 | { |
| 4672 | int flags; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4673 | __be32 status = nfserr_bad_stateid; |
Trond Myklebust | dcd94cc | 2014-07-29 21:34:18 -0400 | [diff] [blame] | 4674 | struct nfs4_delegation *deleg; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 4675 | |
Trond Myklebust | dcd94cc | 2014-07-29 21:34:18 -0400 | [diff] [blame] | 4676 | deleg = find_deleg_stateid(cl, &open->op_delegate_stateid); |
| 4677 | if (deleg == NULL) |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 4678 | goto out; |
Andrew Elble | 95da1b3a | 2017-11-03 14:06:31 -0400 | [diff] [blame] | 4679 | if (deleg->dl_stid.sc_type == NFS4_REVOKED_DELEG_STID) { |
| 4680 | nfs4_put_stid(&deleg->dl_stid); |
| 4681 | if (cl->cl_minorversion) |
| 4682 | status = nfserr_deleg_revoked; |
| 4683 | goto out; |
| 4684 | } |
J. Bruce Fields | 24a0111 | 2010-05-18 20:01:35 -0400 | [diff] [blame] | 4685 | flags = share_access_to_flags(open->op_share_access); |
Trond Myklebust | dcd94cc | 2014-07-29 21:34:18 -0400 | [diff] [blame] | 4686 | status = nfs4_check_delegmode(deleg, flags); |
| 4687 | if (status) { |
| 4688 | nfs4_put_stid(&deleg->dl_stid); |
| 4689 | goto out; |
| 4690 | } |
| 4691 | *dp = deleg; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 4692 | out: |
J. Bruce Fields | 8b289b2 | 2011-10-19 11:52:12 -0400 | [diff] [blame] | 4693 | if (!nfsd4_is_deleg_cur(open)) |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 4694 | return nfs_ok; |
| 4695 | if (status) |
| 4696 | return status; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 4697 | open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 4698 | return nfs_ok; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 4699 | } |
| 4700 | |
J. Bruce Fields | 21fb401 | 2010-07-28 12:21:23 -0400 | [diff] [blame] | 4701 | static inline int nfs4_access_to_access(u32 nfs4_access) |
| 4702 | { |
| 4703 | int flags = 0; |
| 4704 | |
| 4705 | if (nfs4_access & NFS4_SHARE_ACCESS_READ) |
| 4706 | flags |= NFSD_MAY_READ; |
| 4707 | if (nfs4_access & NFS4_SHARE_ACCESS_WRITE) |
| 4708 | flags |= NFSD_MAY_WRITE; |
| 4709 | return flags; |
| 4710 | } |
| 4711 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4712 | static inline __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4713 | nfsd4_truncate(struct svc_rqst *rqstp, struct svc_fh *fh, |
| 4714 | struct nfsd4_open *open) |
| 4715 | { |
| 4716 | struct iattr iattr = { |
| 4717 | .ia_valid = ATTR_SIZE, |
| 4718 | .ia_size = 0, |
| 4719 | }; |
| 4720 | if (!open->op_truncate) |
| 4721 | return 0; |
| 4722 | if (!(open->op_share_access & NFS4_SHARE_ACCESS_WRITE)) |
Al Viro | 9246585 | 2006-01-18 17:43:46 -0800 | [diff] [blame] | 4723 | return nfserr_inval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4724 | return nfsd_setattr(rqstp, fh, &iattr, 0, (time_t)0); |
| 4725 | } |
| 4726 | |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4727 | 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] | 4728 | struct svc_fh *cur_fh, struct nfs4_ol_stateid *stp, |
| 4729 | struct nfsd4_open *open) |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4730 | { |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 4731 | struct nfsd_file *nf = NULL; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4732 | __be32 status; |
| 4733 | int oflag = nfs4_access_to_omode(open->op_share_access); |
| 4734 | int access = nfs4_access_to_access(open->op_share_access); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4735 | unsigned char old_access_bmap, old_deny_bmap; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4736 | |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4737 | spin_lock(&fp->fi_lock); |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4738 | |
| 4739 | /* |
| 4740 | * Are we trying to set a deny mode that would conflict with |
| 4741 | * current access? |
| 4742 | */ |
| 4743 | status = nfs4_file_check_deny(fp, open->op_share_deny); |
| 4744 | if (status != nfs_ok) { |
| 4745 | spin_unlock(&fp->fi_lock); |
| 4746 | goto out; |
| 4747 | } |
| 4748 | |
| 4749 | /* set access to the file */ |
| 4750 | status = nfs4_file_get_access(fp, open->op_share_access); |
| 4751 | if (status != nfs_ok) { |
| 4752 | spin_unlock(&fp->fi_lock); |
| 4753 | goto out; |
| 4754 | } |
| 4755 | |
| 4756 | /* Set access bits in stateid */ |
| 4757 | old_access_bmap = stp->st_access_bmap; |
| 4758 | set_access(open->op_share_access, stp); |
| 4759 | |
| 4760 | /* Set new deny mask */ |
| 4761 | old_deny_bmap = stp->st_deny_bmap; |
| 4762 | set_deny(open->op_share_deny, stp); |
| 4763 | fp->fi_share_deny |= (open->op_share_deny & NFS4_SHARE_DENY_BOTH); |
| 4764 | |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4765 | if (!fp->fi_fds[oflag]) { |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4766 | spin_unlock(&fp->fi_lock); |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 4767 | status = nfsd_file_acquire(rqstp, cur_fh, access, &nf); |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4768 | if (status) |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4769 | goto out_put_access; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4770 | spin_lock(&fp->fi_lock); |
| 4771 | if (!fp->fi_fds[oflag]) { |
Jeff Layton | fd4f83f | 2019-08-18 14:18:52 -0400 | [diff] [blame] | 4772 | fp->fi_fds[oflag] = nf; |
| 4773 | nf = NULL; |
Trond Myklebust | de18643 | 2014-07-10 14:07:26 -0400 | [diff] [blame] | 4774 | } |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4775 | } |
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 | if (nf) |
| 4778 | nfsd_file_put(nf); |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4779 | |
| 4780 | status = nfsd4_truncate(rqstp, cur_fh, open); |
| 4781 | if (status) |
| 4782 | goto out_put_access; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4783 | out: |
| 4784 | return status; |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4785 | out_put_access: |
| 4786 | stp->st_access_bmap = old_access_bmap; |
| 4787 | nfs4_file_put_access(fp, open->op_share_access); |
| 4788 | reset_union_bmap_deny(bmap_to_share_mode(old_deny_bmap), stp); |
| 4789 | goto out; |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4790 | } |
| 4791 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4792 | static __be32 |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 4793 | 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] | 4794 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 4795 | __be32 status; |
Arnd Bergmann | 6ac7536 | 2015-05-12 23:31:29 +0200 | [diff] [blame] | 4796 | unsigned char old_deny_bmap = stp->st_deny_bmap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4797 | |
Jeff Layton | 6eb3a1d | 2014-07-10 14:07:31 -0400 | [diff] [blame] | 4798 | if (!test_access(open->op_share_access, stp)) |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4799 | return nfs4_get_vfs_file(rqstp, fp, cur_fh, stp, open); |
Christoph Hellwig | 7e6a72e | 2014-06-30 11:48:30 -0400 | [diff] [blame] | 4800 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4801 | /* test and set deny mode */ |
| 4802 | spin_lock(&fp->fi_lock); |
| 4803 | status = nfs4_file_check_deny(fp, open->op_share_deny); |
| 4804 | if (status == nfs_ok) { |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4805 | set_deny(open->op_share_deny, stp); |
| 4806 | fp->fi_share_deny |= |
| 4807 | (open->op_share_deny & NFS4_SHARE_DENY_BOTH); |
| 4808 | } |
| 4809 | spin_unlock(&fp->fi_lock); |
| 4810 | |
| 4811 | if (status != nfs_ok) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4812 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4813 | |
Jeff Layton | baeb4ff | 2014-07-10 14:07:34 -0400 | [diff] [blame] | 4814 | status = nfsd4_truncate(rqstp, cur_fh, open); |
| 4815 | if (status != nfs_ok) |
| 4816 | reset_union_bmap_deny(old_deny_bmap, stp); |
| 4817 | return status; |
| 4818 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4819 | |
J. Bruce Fields | 14a24e9 | 2010-12-10 19:02:49 -0500 | [diff] [blame] | 4820 | /* Should we give out recallable state?: */ |
| 4821 | static bool nfsd4_cb_channel_good(struct nfs4_client *clp) |
| 4822 | { |
| 4823 | if (clp->cl_cb_state == NFSD4_CB_UP) |
| 4824 | return true; |
| 4825 | /* |
| 4826 | * In the sessions case, since we don't have to establish a |
| 4827 | * separate connection for callbacks, we assume it's OK |
| 4828 | * until we hear otherwise: |
| 4829 | */ |
| 4830 | return clp->cl_minorversion && clp->cl_cb_state == NFSD4_CB_UNKNOWN; |
| 4831 | } |
| 4832 | |
J. Bruce Fields | 653e514 | 2018-02-15 22:08:45 -0500 | [diff] [blame] | 4833 | static struct file_lock *nfs4_alloc_init_lease(struct nfs4_delegation *dp, |
| 4834 | int flag) |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 4835 | { |
| 4836 | struct file_lock *fl; |
| 4837 | |
| 4838 | fl = locks_alloc_lock(); |
| 4839 | if (!fl) |
| 4840 | return NULL; |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 4841 | fl->fl_lmops = &nfsd_lease_mng_ops; |
J. Bruce Fields | 617588d | 2011-07-01 15:18:34 -0400 | [diff] [blame] | 4842 | fl->fl_flags = FL_DELEG; |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 4843 | fl->fl_type = flag == NFS4_OPEN_DELEGATE_READ? F_RDLCK: F_WRLCK; |
| 4844 | fl->fl_end = OFFSET_MAX; |
J. Bruce Fields | 653e514 | 2018-02-15 22:08:45 -0500 | [diff] [blame] | 4845 | fl->fl_owner = (fl_owner_t)dp; |
J. Bruce Fields | 22d38c4 | 2011-01-31 11:55:12 -0500 | [diff] [blame] | 4846 | fl->fl_pid = current->tgid; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 4847 | 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] | 4848 | return fl; |
| 4849 | } |
| 4850 | |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 4851 | static struct nfs4_delegation * |
| 4852 | nfs4_set_delegation(struct nfs4_client *clp, struct svc_fh *fh, |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 4853 | struct nfs4_file *fp, struct nfs4_clnt_odstate *odstate) |
J. Bruce Fields | acfdf5c | 2011-01-31 19:20:39 -0500 | [diff] [blame] | 4854 | { |
J. Bruce Fields | 68b18f5 | 2018-02-19 11:38:33 -0500 | [diff] [blame] | 4855 | int status = 0; |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 4856 | struct nfs4_delegation *dp; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 4857 | struct nfsd_file *nf; |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 4858 | struct file_lock *fl; |
Jeff Layton | 417c662 | 2014-07-21 09:34:57 -0400 | [diff] [blame] | 4859 | |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 4860 | /* |
| 4861 | * The fi_had_conflict and nfs_get_existing_delegation checks |
| 4862 | * here are just optimizations; we'll need to recheck them at |
| 4863 | * the end: |
| 4864 | */ |
J. Bruce Fields | bf7bd3e | 2013-08-15 16:55:26 -0400 | [diff] [blame] | 4865 | if (fp->fi_had_conflict) |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 4866 | return ERR_PTR(-EAGAIN); |
| 4867 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 4868 | nf = find_readable_file(fp); |
| 4869 | if (!nf) { |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 4870 | /* We should always have a readable file here */ |
| 4871 | WARN_ON_ONCE(1); |
| 4872 | return ERR_PTR(-EBADF); |
| 4873 | } |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 4874 | spin_lock(&state_lock); |
| 4875 | spin_lock(&fp->fi_lock); |
J. Bruce Fields | 68b18f5 | 2018-02-19 11:38:33 -0500 | [diff] [blame] | 4876 | if (nfs4_delegation_exists(clp, fp)) |
| 4877 | status = -EAGAIN; |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 4878 | else if (!fp->fi_deleg_file) { |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 4879 | fp->fi_deleg_file = nf; |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 4880 | /* increment early to prevent fi_deleg_file from being |
| 4881 | * cleared */ |
| 4882 | fp->fi_delegees = 1; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 4883 | nf = NULL; |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 4884 | } else |
| 4885 | fp->fi_delegees++; |
| 4886 | spin_unlock(&fp->fi_lock); |
| 4887 | spin_unlock(&state_lock); |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 4888 | if (nf) |
| 4889 | nfsd_file_put(nf); |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 4890 | if (status) |
| 4891 | return ERR_PTR(status); |
| 4892 | |
| 4893 | status = -ENOMEM; |
| 4894 | dp = alloc_init_deleg(clp, fp, fh, odstate); |
| 4895 | if (!dp) |
| 4896 | goto out_delegees; |
| 4897 | |
| 4898 | fl = nfs4_alloc_init_lease(dp, NFS4_OPEN_DELEGATE_READ); |
| 4899 | if (!fl) |
Andrew Elble | bd8d725 | 2018-10-05 09:32:08 -0400 | [diff] [blame] | 4900 | goto out_clnt_odstate; |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 4901 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 4902 | 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] | 4903 | if (fl) |
| 4904 | locks_free_lock(fl); |
| 4905 | if (status) |
| 4906 | goto out_clnt_odstate; |
| 4907 | |
| 4908 | spin_lock(&state_lock); |
| 4909 | spin_lock(&fp->fi_lock); |
| 4910 | if (fp->fi_had_conflict) |
| 4911 | status = -EAGAIN; |
| 4912 | else |
| 4913 | status = hash_delegation_locked(dp, fp); |
Andrew Elble | 34ed987 | 2015-10-15 12:07:28 -0400 | [diff] [blame] | 4914 | spin_unlock(&fp->fi_lock); |
| 4915 | spin_unlock(&state_lock); |
| 4916 | |
| 4917 | if (status) |
Andrew Elble | 692ad28 | 2018-04-18 17:04:37 -0400 | [diff] [blame] | 4918 | goto out_unlock; |
| 4919 | |
Jeff Layton | 0b26693 | 2014-07-25 07:34:25 -0400 | [diff] [blame] | 4920 | return dp; |
Andrew Elble | 692ad28 | 2018-04-18 17:04:37 -0400 | [diff] [blame] | 4921 | out_unlock: |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 4922 | 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] | 4923 | out_clnt_odstate: |
| 4924 | put_clnt_odstate(dp->dl_clnt_odstate); |
J. Bruce Fields | 353601e | 2018-02-16 14:29:42 -0500 | [diff] [blame] | 4925 | nfs4_put_stid(&dp->dl_stid); |
| 4926 | out_delegees: |
| 4927 | put_deleg_file(fp); |
| 4928 | return ERR_PTR(status); |
J. Bruce Fields | edab978 | 2011-01-31 17:58:10 -0500 | [diff] [blame] | 4929 | } |
| 4930 | |
Benny Halevy | 4aa8913 | 2012-02-21 14:16:44 -0800 | [diff] [blame] | 4931 | static void nfsd4_open_deleg_none_ext(struct nfsd4_open *open, int status) |
| 4932 | { |
| 4933 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 4934 | if (status == -EAGAIN) |
| 4935 | open->op_why_no_deleg = WND4_CONTENTION; |
| 4936 | else { |
| 4937 | open->op_why_no_deleg = WND4_RESOURCE; |
| 4938 | switch (open->op_deleg_want) { |
| 4939 | case NFS4_SHARE_WANT_READ_DELEG: |
| 4940 | case NFS4_SHARE_WANT_WRITE_DELEG: |
| 4941 | case NFS4_SHARE_WANT_ANY_DELEG: |
| 4942 | break; |
| 4943 | case NFS4_SHARE_WANT_CANCEL: |
| 4944 | open->op_why_no_deleg = WND4_CANCELLED; |
| 4945 | break; |
| 4946 | case NFS4_SHARE_WANT_NO_DELEG: |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 4947 | WARN_ON_ONCE(1); |
Benny Halevy | 4aa8913 | 2012-02-21 14:16:44 -0800 | [diff] [blame] | 4948 | } |
| 4949 | } |
| 4950 | } |
| 4951 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4952 | /* |
| 4953 | * Attempt to hand out a delegation. |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4954 | * |
| 4955 | * Note we don't support write delegations, and won't until the vfs has |
| 4956 | * proper support for them. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4957 | */ |
| 4958 | static void |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame] | 4959 | nfs4_open_delegation(struct svc_fh *fh, struct nfsd4_open *open, |
| 4960 | struct nfs4_ol_stateid *stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4961 | { |
| 4962 | struct nfs4_delegation *dp; |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame] | 4963 | struct nfs4_openowner *oo = openowner(stp->st_stateowner); |
| 4964 | struct nfs4_client *clp = stp->st_stid.sc_client; |
J. Bruce Fields | 14a24e9 | 2010-12-10 19:02:49 -0500 | [diff] [blame] | 4965 | int cb_up; |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4966 | int status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4967 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 4968 | cb_up = nfsd4_cb_channel_good(oo->oo_owner.so_client); |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 4969 | open->op_recall = 0; |
| 4970 | switch (open->op_claim_type) { |
| 4971 | case NFS4_OPEN_CLAIM_PREVIOUS: |
J. Bruce Fields | 2bf2387 | 2010-03-08 12:37:27 -0500 | [diff] [blame] | 4972 | if (!cb_up) |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 4973 | open->op_recall = 1; |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4974 | if (open->op_delegate_type != NFS4_OPEN_DELEGATE_READ) |
| 4975 | goto out_no_deleg; |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 4976 | break; |
| 4977 | case NFS4_OPEN_CLAIM_NULL: |
Ming Chen | ed47b06 | 2014-01-09 21:26:10 +0000 | [diff] [blame] | 4978 | case NFS4_OPEN_CLAIM_FH: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4979 | /* |
| 4980 | * Let's not give out any delegations till everyone's |
J. Bruce Fields | c87fb4a | 2015-08-06 12:47:02 -0400 | [diff] [blame] | 4981 | * had the chance to reclaim theirs, *and* until |
| 4982 | * NLM locks have all been reclaimed: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4983 | */ |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame] | 4984 | if (locks_in_grace(clp->net)) |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4985 | goto out_no_deleg; |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 4986 | if (!cb_up || !(oo->oo_flags & NFS4_OO_CONFIRMED)) |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4987 | goto out_no_deleg; |
Steve Dickson | 9a0590a | 2013-05-15 14:51:49 -0400 | [diff] [blame] | 4988 | /* |
| 4989 | * Also, if the file was opened for write or |
| 4990 | * create, there's a good chance the client's |
| 4991 | * about to write to it, resulting in an |
| 4992 | * immediate recall (since we don't support |
| 4993 | * write delegations): |
| 4994 | */ |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 4995 | if (open->op_share_access & NFS4_SHARE_ACCESS_WRITE) |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 4996 | goto out_no_deleg; |
| 4997 | if (open->op_create == NFS4_OPEN_CREATE) |
| 4998 | goto out_no_deleg; |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 4999 | break; |
| 5000 | default: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 5001 | goto out_no_deleg; |
NeilBrown | 7b190fe | 2005-06-23 22:03:23 -0700 | [diff] [blame] | 5002 | } |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 5003 | 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] | 5004 | if (IS_ERR(dp)) |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 5005 | goto out_no_deleg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5006 | |
J. Bruce Fields | d5477a8 | 2011-09-08 12:07:44 -0400 | [diff] [blame] | 5007 | 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] | 5008 | |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 5009 | dprintk("NFSD: delegation stateid=" STATEID_FMT "\n", |
J. Bruce Fields | d5477a8 | 2011-09-08 12:07:44 -0400 | [diff] [blame] | 5010 | STATEID_VAL(&dp->dl_stid.sc_stateid)); |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 5011 | open->op_delegate_type = NFS4_OPEN_DELEGATE_READ; |
Trond Myklebust | 67cb127 | 2014-07-29 21:34:17 -0400 | [diff] [blame] | 5012 | nfs4_put_stid(&dp->dl_stid); |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 5013 | return; |
J. Bruce Fields | dd239cc | 2011-01-31 17:14:55 -0500 | [diff] [blame] | 5014 | out_no_deleg: |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 5015 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE; |
| 5016 | if (open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS && |
J. Bruce Fields | d08d32e | 2013-06-21 11:05:32 -0400 | [diff] [blame] | 5017 | open->op_delegate_type != NFS4_OPEN_DELEGATE_NONE) { |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 5018 | dprintk("NFSD: WARNING: refusing delegation reclaim\n"); |
J. Bruce Fields | d08d32e | 2013-06-21 11:05:32 -0400 | [diff] [blame] | 5019 | open->op_recall = 1; |
| 5020 | } |
J. Bruce Fields | 99c4151 | 2013-05-21 16:21:25 -0400 | [diff] [blame] | 5021 | |
| 5022 | /* 4.1 client asking for a delegation? */ |
| 5023 | if (open->op_deleg_want) |
| 5024 | nfsd4_open_deleg_none_ext(open, status); |
| 5025 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5026 | } |
| 5027 | |
Benny Halevy | e27f49c | 2012-02-21 14:16:54 -0800 | [diff] [blame] | 5028 | static void nfsd4_deleg_xgrade_none_ext(struct nfsd4_open *open, |
| 5029 | struct nfs4_delegation *dp) |
| 5030 | { |
| 5031 | if (open->op_deleg_want == NFS4_SHARE_WANT_READ_DELEG && |
| 5032 | dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) { |
| 5033 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 5034 | open->op_why_no_deleg = WND4_NOT_SUPP_DOWNGRADE; |
| 5035 | } else if (open->op_deleg_want == NFS4_SHARE_WANT_WRITE_DELEG && |
| 5036 | dp->dl_type == NFS4_OPEN_DELEGATE_WRITE) { |
| 5037 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 5038 | open->op_why_no_deleg = WND4_NOT_SUPP_UPGRADE; |
| 5039 | } |
| 5040 | /* Otherwise the client must be confused wanting a delegation |
| 5041 | * it already has, therefore we don't return |
| 5042 | * NFS4_OPEN_DELEGATE_NONE_EXT and reason. |
| 5043 | */ |
| 5044 | } |
| 5045 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5046 | __be32 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5047 | nfsd4_process_open2(struct svc_rqst *rqstp, struct svc_fh *current_fh, struct nfsd4_open *open) |
| 5048 | { |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 5049 | struct nfsd4_compoundres *resp = rqstp->rq_resp; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5050 | struct nfs4_client *cl = open->op_openowner->oo_owner.so_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5051 | struct nfs4_file *fp = NULL; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5052 | struct nfs4_ol_stateid *stp = NULL; |
NeilBrown | 567d982 | 2005-06-23 22:02:53 -0700 | [diff] [blame] | 5053 | struct nfs4_delegation *dp = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5054 | __be32 status; |
Trond Myklebust | d8a1a00 | 2017-11-03 08:00:11 -0400 | [diff] [blame] | 5055 | bool new_stp = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5056 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5057 | /* |
| 5058 | * Lookup file; if found, lookup stateid and check open request, |
| 5059 | * and check for delegations in the process of being recalled. |
| 5060 | * If not found, create the nfs4_file struct |
| 5061 | */ |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 5062 | fp = find_or_add_file(open->op_file, ¤t_fh->fh_handle); |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 5063 | if (fp != open->op_file) { |
J. Bruce Fields | 41d2266 | 2013-03-21 15:49:47 -0400 | [diff] [blame] | 5064 | status = nfs4_check_deleg(cl, open, &dp); |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 5065 | if (status) |
| 5066 | goto out; |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 5067 | stp = nfsd4_find_and_lock_existing_open(fp, open); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5068 | } else { |
Trond Myklebust | 950e011 | 2014-06-30 11:48:31 -0400 | [diff] [blame] | 5069 | open->op_file = NULL; |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 5070 | status = nfserr_bad_stateid; |
J. Bruce Fields | 8b289b2 | 2011-10-19 11:52:12 -0400 | [diff] [blame] | 5071 | if (nfsd4_is_deleg_cur(open)) |
NeilBrown | c44c5ee | 2005-06-23 22:02:54 -0700 | [diff] [blame] | 5072 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5073 | } |
| 5074 | |
Trond Myklebust | d8a1a00 | 2017-11-03 08:00:11 -0400 | [diff] [blame] | 5075 | if (!stp) { |
| 5076 | stp = init_open_stateid(fp, open); |
| 5077 | if (!open->op_stp) |
| 5078 | new_stp = true; |
| 5079 | } |
| 5080 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5081 | /* |
| 5082 | * OPEN the file, or upgrade an existing OPEN. |
| 5083 | * If truncate fails, the OPEN fails. |
Trond Myklebust | d8a1a00 | 2017-11-03 08:00:11 -0400 | [diff] [blame] | 5084 | * |
| 5085 | * stp is already locked. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5086 | */ |
Trond Myklebust | d8a1a00 | 2017-11-03 08:00:11 -0400 | [diff] [blame] | 5087 | if (!new_stp) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5088 | /* Stateid was found, this is an OPEN upgrade */ |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 5089 | status = nfs4_upgrade_open(rqstp, fp, current_fh, stp, open); |
Jeff Layton | 35a92fe | 2015-09-17 07:47:08 -0400 | [diff] [blame] | 5090 | if (status) { |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 5091 | mutex_unlock(&stp->st_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5092 | goto out; |
Jeff Layton | 35a92fe | 2015-09-17 07:47:08 -0400 | [diff] [blame] | 5093 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5094 | } else { |
Jeff Layton | 6eb3a1d | 2014-07-10 14:07:31 -0400 | [diff] [blame] | 5095 | status = nfs4_get_vfs_file(rqstp, fp, current_fh, stp, open); |
| 5096 | if (status) { |
Trond Myklebust | d8a1a00 | 2017-11-03 08:00:11 -0400 | [diff] [blame] | 5097 | stp->st_stid.sc_type = NFS4_CLOSED_STID; |
Jeff Layton | 6eb3a1d | 2014-07-10 14:07:31 -0400 | [diff] [blame] | 5098 | release_open_stateid(stp); |
Trond Myklebust | d8a1a00 | 2017-11-03 08:00:11 -0400 | [diff] [blame] | 5099 | mutex_unlock(&stp->st_mutex); |
Jeff Layton | 6eb3a1d | 2014-07-10 14:07:31 -0400 | [diff] [blame] | 5100 | goto out; |
| 5101 | } |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 5102 | |
| 5103 | stp->st_clnt_odstate = find_or_hash_clnt_odstate(fp, |
| 5104 | open->op_odstate); |
| 5105 | if (stp->st_clnt_odstate == open->op_odstate) |
| 5106 | open->op_odstate = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5107 | } |
Trond Myklebust | d8a1a00 | 2017-11-03 08:00:11 -0400 | [diff] [blame] | 5108 | |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 5109 | nfs4_inc_and_copy_stateid(&open->op_stateid, &stp->st_stid); |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 5110 | mutex_unlock(&stp->st_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5111 | |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 5112 | if (nfsd4_has_session(&resp->cstate)) { |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 5113 | if (open->op_deleg_want & NFS4_SHARE_WANT_NO_DELEG) { |
| 5114 | open->op_delegate_type = NFS4_OPEN_DELEGATE_NONE_EXT; |
| 5115 | open->op_why_no_deleg = WND4_NOT_WANTED; |
| 5116 | goto nodeleg; |
| 5117 | } |
| 5118 | } |
| 5119 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5120 | /* |
| 5121 | * Attempt to hand out a delegation. No error return, because the |
| 5122 | * OPEN succeeds even if we fail. |
| 5123 | */ |
Jeff Layton | 4cf5922 | 2014-07-25 07:34:24 -0400 | [diff] [blame] | 5124 | nfs4_open_delegation(current_fh, open, stp); |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 5125 | nodeleg: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5126 | status = nfs_ok; |
| 5127 | |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 5128 | dprintk("%s: stateid=" STATEID_FMT "\n", __func__, |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5129 | STATEID_VAL(&stp->st_stid.sc_stateid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5130 | out: |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 5131 | /* 4.1 client trying to upgrade/downgrade delegation? */ |
| 5132 | if (open->op_delegate_type == NFS4_OPEN_DELEGATE_NONE && dp && |
Benny Halevy | e27f49c | 2012-02-21 14:16:54 -0800 | [diff] [blame] | 5133 | open->op_deleg_want) |
| 5134 | nfsd4_deleg_xgrade_none_ext(open, dp); |
Benny Halevy | d24433c | 2012-02-16 20:57:17 +0200 | [diff] [blame] | 5135 | |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 5136 | if (fp) |
| 5137 | put_nfs4_file(fp); |
NeilBrown | 3751517 | 2005-07-07 17:59:16 -0700 | [diff] [blame] | 5138 | if (status == 0 && open->op_claim_type == NFS4_OPEN_CLAIM_PREVIOUS) |
Kinglong Mee | 8718602 | 2015-07-13 17:32:59 +0800 | [diff] [blame] | 5139 | open->op_openowner->oo_flags |= NFS4_OO_CONFIRMED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5140 | /* |
| 5141 | * To finish the open response, we just need to set the rflags. |
| 5142 | */ |
| 5143 | open->op_rflags = NFS4_OPEN_RESULT_LOCKTYPE_POSIX; |
Jeff Layton | 19e4c34 | 2016-09-16 16:28:27 -0400 | [diff] [blame] | 5144 | if (nfsd4_has_session(&resp->cstate)) |
| 5145 | open->op_rflags |= NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK; |
| 5146 | else if (!(open->op_openowner->oo_flags & NFS4_OO_CONFIRMED)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5147 | open->op_rflags |= NFS4_OPEN_RESULT_CONFIRM; |
Jeff Layton | 19e4c34 | 2016-09-16 16:28:27 -0400 | [diff] [blame] | 5148 | |
Trond Myklebust | dcd94cc | 2014-07-29 21:34:18 -0400 | [diff] [blame] | 5149 | if (dp) |
| 5150 | nfs4_put_stid(&dp->dl_stid); |
Trond Myklebust | d6f2bc5 | 2014-07-29 21:34:19 -0400 | [diff] [blame] | 5151 | if (stp) |
| 5152 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5153 | |
| 5154 | return status; |
| 5155 | } |
| 5156 | |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 5157 | void nfsd4_cleanup_open_state(struct nfsd4_compound_state *cstate, |
Jeff Layton | 4229789 | 2015-03-23 10:53:44 -0400 | [diff] [blame] | 5158 | struct nfsd4_open *open) |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 5159 | { |
| 5160 | if (open->op_openowner) { |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 5161 | struct nfs4_stateowner *so = &open->op_openowner->oo_owner; |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 5162 | |
Jeff Layton | d3134b1 | 2014-07-29 21:34:32 -0400 | [diff] [blame] | 5163 | nfsd4_cstate_assign_replay(cstate, so); |
| 5164 | nfs4_put_stateowner(so); |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 5165 | } |
J. Bruce Fields | 32513b4 | 2011-10-13 16:00:16 -0400 | [diff] [blame] | 5166 | if (open->op_file) |
Jeff Layton | 5b095e9 | 2014-10-23 08:01:02 -0400 | [diff] [blame] | 5167 | kmem_cache_free(file_slab, open->op_file); |
J. Bruce Fields | 4cdc951 | 2011-10-17 15:57:47 -0400 | [diff] [blame] | 5168 | if (open->op_stp) |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 5169 | nfs4_put_stid(&open->op_stp->st_stid); |
Sachin Bhamare | 8287f00 | 2015-04-27 14:50:14 +0200 | [diff] [blame] | 5170 | if (open->op_odstate) |
| 5171 | kmem_cache_free(odstate_slab, open->op_odstate); |
J. Bruce Fields | d29b20c | 2011-10-13 15:12:59 -0400 | [diff] [blame] | 5172 | } |
| 5173 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5174 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 5175 | nfsd4_renew(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5176 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5177 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5178 | clientid_t *clid = &u->renew; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5179 | struct nfs4_client *clp; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5180 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 5181 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5183 | dprintk("process_renew(%08x/%08x): starting\n", |
| 5184 | clid->cl_boot, clid->cl_id); |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 5185 | status = lookup_clientid(clid, cstate, nn); |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 5186 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5187 | goto out; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 5188 | clp = cstate->clp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5189 | status = nfserr_cb_path_down; |
NeilBrown | ea1da63 | 2005-06-23 22:04:17 -0700 | [diff] [blame] | 5190 | if (!list_empty(&clp->cl_delegations) |
J. Bruce Fields | 77a3569 | 2010-04-30 18:51:44 -0400 | [diff] [blame] | 5191 | && clp->cl_cb_state != NFSD4_CB_UP) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5192 | goto out; |
| 5193 | status = nfs_ok; |
| 5194 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5195 | return status; |
| 5196 | } |
| 5197 | |
Jeff Layton | 7f5ef2e | 2014-09-12 16:40:21 -0400 | [diff] [blame] | 5198 | void |
Stanislav Kinsbursky | 12760c6 | 2012-11-14 18:22:12 +0300 | [diff] [blame] | 5199 | nfsd4_end_grace(struct nfsd_net *nn) |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 5200 | { |
Jeff Layton | 33dcc48 | 2012-04-10 11:08:48 -0400 | [diff] [blame] | 5201 | /* do nothing if grace period already ended */ |
Stanislav Kinsbursky | a51c84e | 2012-07-25 16:57:37 +0400 | [diff] [blame] | 5202 | if (nn->grace_ended) |
Jeff Layton | 33dcc48 | 2012-04-10 11:08:48 -0400 | [diff] [blame] | 5203 | return; |
| 5204 | |
Stanislav Kinsbursky | a51c84e | 2012-07-25 16:57:37 +0400 | [diff] [blame] | 5205 | nn->grace_ended = true; |
J. Bruce Fields | 70b2823 | 2014-09-16 17:37:32 -0400 | [diff] [blame] | 5206 | /* |
| 5207 | * If the server goes down again right now, an NFSv4 |
| 5208 | * client will still be allowed to reclaim after it comes back up, |
| 5209 | * even if it hasn't yet had a chance to reclaim state this time. |
| 5210 | * |
| 5211 | */ |
Jeff Layton | 919b804 | 2014-09-12 16:40:20 -0400 | [diff] [blame] | 5212 | nfsd4_record_grace_done(nn); |
J. Bruce Fields | 70b2823 | 2014-09-16 17:37:32 -0400 | [diff] [blame] | 5213 | /* |
| 5214 | * At this point, NFSv4 clients can still reclaim. But if the |
| 5215 | * server crashes, any that have not yet reclaimed will be out |
| 5216 | * of luck on the next boot. |
| 5217 | * |
| 5218 | * (NFSv4.1+ clients are considered to have reclaimed once they |
| 5219 | * call RECLAIM_COMPLETE. NFSv4.0 clients are considered to |
| 5220 | * have reclaimed after their first OPEN.) |
| 5221 | */ |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 5222 | locks_end_grace(&nn->nfsd4_manager); |
J. Bruce Fields | 70b2823 | 2014-09-16 17:37:32 -0400 | [diff] [blame] | 5223 | /* |
| 5224 | * At this point, and once lockd and/or any other containers |
| 5225 | * exit their grace period, further reclaims will fail and |
| 5226 | * regular locking can resume. |
| 5227 | */ |
NeilBrown | a76b431 | 2005-06-23 22:04:01 -0700 | [diff] [blame] | 5228 | } |
| 5229 | |
J. Bruce Fields | 03f318c | 2018-06-08 12:28:47 -0400 | [diff] [blame] | 5230 | /* |
| 5231 | * If we've waited a lease period but there are still clients trying to |
| 5232 | * reclaim, wait a little longer to give them a chance to finish. |
| 5233 | */ |
| 5234 | static bool clients_still_reclaiming(struct nfsd_net *nn) |
| 5235 | { |
| 5236 | unsigned long now = get_seconds(); |
| 5237 | unsigned long double_grace_period_end = nn->boot_time + |
| 5238 | 2 * nn->nfsd4_lease; |
| 5239 | |
Scott Mayhew | 362063a | 2019-03-26 18:06:28 -0400 | [diff] [blame] | 5240 | if (nn->track_reclaim_completes && |
| 5241 | atomic_read(&nn->nr_reclaim_complete) == |
| 5242 | nn->reclaim_str_hashtbl_size) |
| 5243 | return false; |
J. Bruce Fields | 03f318c | 2018-06-08 12:28:47 -0400 | [diff] [blame] | 5244 | if (!nn->somebody_reclaimed) |
| 5245 | return false; |
| 5246 | nn->somebody_reclaimed = false; |
| 5247 | /* |
| 5248 | * If we've given them *two* lease times to reclaim, and they're |
| 5249 | * still not done, give up: |
| 5250 | */ |
| 5251 | if (time_after(now, double_grace_period_end)) |
| 5252 | return false; |
| 5253 | return true; |
| 5254 | } |
| 5255 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 5256 | static time_t |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 5257 | nfs4_laundromat(struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5258 | { |
| 5259 | struct nfs4_client *clp; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5260 | struct nfs4_openowner *oo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5261 | struct nfs4_delegation *dp; |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 5262 | struct nfs4_ol_stateid *stp; |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 5263 | struct nfsd4_blocked_lock *nbl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5264 | struct list_head *pos, *next, reaplist; |
Stanislav Kinsbursky | 3d73371 | 2012-11-27 14:11:44 +0300 | [diff] [blame] | 5265 | time_t cutoff = get_seconds() - nn->nfsd4_lease; |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 5266 | time_t t, new_timeo = nn->nfsd4_lease; |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame^] | 5267 | struct nfs4_cpntf_state *cps; |
| 5268 | copy_stateid_t *cps_t; |
| 5269 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5270 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5271 | dprintk("NFSD: laundromat service - starting\n"); |
J. Bruce Fields | 03f318c | 2018-06-08 12:28:47 -0400 | [diff] [blame] | 5272 | |
| 5273 | if (clients_still_reclaiming(nn)) { |
| 5274 | new_timeo = 0; |
| 5275 | goto out; |
| 5276 | } |
Scott Mayhew | 362063a | 2019-03-26 18:06:28 -0400 | [diff] [blame] | 5277 | dprintk("NFSD: end of grace period\n"); |
Stanislav Kinsbursky | 12760c6 | 2012-11-14 18:22:12 +0300 | [diff] [blame] | 5278 | nfsd4_end_grace(nn); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 5279 | INIT_LIST_HEAD(&reaplist); |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame^] | 5280 | |
| 5281 | spin_lock(&nn->s2s_cp_lock); |
| 5282 | idr_for_each_entry(&nn->s2s_cp_stateids, cps_t, i) { |
| 5283 | cps = container_of(cps_t, struct nfs4_cpntf_state, cp_stateid); |
| 5284 | if (cps->cp_stateid.sc_type == NFS4_COPYNOTIFY_STID && |
| 5285 | !time_after((unsigned long)cps->cpntf_time, |
| 5286 | (unsigned long)cutoff)) |
| 5287 | _free_cpntf_state_locked(nn, cps); |
| 5288 | } |
| 5289 | spin_unlock(&nn->s2s_cp_lock); |
| 5290 | |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 5291 | spin_lock(&nn->client_lock); |
Stanislav Kinsbursky | 5ed58bb | 2012-11-14 18:21:56 +0300 | [diff] [blame] | 5292 | list_for_each_safe(pos, next, &nn->client_lru) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5293 | clp = list_entry(pos, struct nfs4_client, cl_lru); |
| 5294 | if (time_after((unsigned long)clp->cl_time, (unsigned long)cutoff)) { |
| 5295 | t = clp->cl_time - cutoff; |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 5296 | new_timeo = min(new_timeo, t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5297 | break; |
| 5298 | } |
J. Bruce Fields | 221a687 | 2013-04-01 22:23:49 -0400 | [diff] [blame] | 5299 | if (mark_client_expired_locked(clp)) { |
Benny Halevy | d768298 | 2010-05-12 00:13:54 +0300 | [diff] [blame] | 5300 | dprintk("NFSD: client in use (clientid %08x)\n", |
| 5301 | clp->cl_clientid.cl_id); |
| 5302 | continue; |
| 5303 | } |
Trond Myklebust | 4864af9 | 2014-07-30 08:27:03 -0400 | [diff] [blame] | 5304 | list_add(&clp->cl_lru, &reaplist); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 5305 | } |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 5306 | spin_unlock(&nn->client_lock); |
Benny Halevy | 36acb66 | 2010-05-12 00:13:04 +0300 | [diff] [blame] | 5307 | list_for_each_safe(pos, next, &reaplist) { |
| 5308 | clp = list_entry(pos, struct nfs4_client, cl_lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5309 | dprintk("NFSD: purging unused client (clientid %08x)\n", |
| 5310 | clp->cl_clientid.cl_id); |
Trond Myklebust | 4864af9 | 2014-07-30 08:27:03 -0400 | [diff] [blame] | 5311 | list_del_init(&clp->cl_lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5312 | expire_client(clp); |
| 5313 | } |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5314 | spin_lock(&state_lock); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 5315 | list_for_each_safe(pos, next, &nn->del_recall_lru) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5316 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
| 5317 | if (time_after((unsigned long)dp->dl_time, (unsigned long)cutoff)) { |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 5318 | t = dp->dl_time - cutoff; |
| 5319 | new_timeo = min(new_timeo, t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5320 | break; |
| 5321 | } |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 5322 | WARN_ON(!unhash_delegation_locked(dp)); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 5323 | list_add(&dp->dl_recall_lru, &reaplist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5324 | } |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 5325 | spin_unlock(&state_lock); |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 5326 | while (!list_empty(&reaplist)) { |
| 5327 | dp = list_first_entry(&reaplist, struct nfs4_delegation, |
| 5328 | dl_recall_lru); |
| 5329 | list_del_init(&dp->dl_recall_lru); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 5330 | revoke_delegation(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5331 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 5332 | |
| 5333 | spin_lock(&nn->client_lock); |
| 5334 | while (!list_empty(&nn->close_lru)) { |
| 5335 | oo = list_first_entry(&nn->close_lru, struct nfs4_openowner, |
| 5336 | oo_close_lru); |
| 5337 | if (time_after((unsigned long)oo->oo_time, |
| 5338 | (unsigned long)cutoff)) { |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 5339 | t = oo->oo_time - cutoff; |
| 5340 | new_timeo = min(new_timeo, t); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5341 | break; |
| 5342 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 5343 | list_del_init(&oo->oo_close_lru); |
| 5344 | stp = oo->oo_last_closed_stid; |
| 5345 | oo->oo_last_closed_stid = NULL; |
| 5346 | spin_unlock(&nn->client_lock); |
| 5347 | nfs4_put_stid(&stp->st_stid); |
| 5348 | spin_lock(&nn->client_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5349 | } |
Jeff Layton | 217526e | 2014-07-30 08:27:11 -0400 | [diff] [blame] | 5350 | spin_unlock(&nn->client_lock); |
| 5351 | |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 5352 | /* |
| 5353 | * It's possible for a client to try and acquire an already held lock |
| 5354 | * that is being held for a long time, and then lose interest in it. |
| 5355 | * So, we clean out any un-revisited request after a lease period |
| 5356 | * under the assumption that the client is no longer interested. |
| 5357 | * |
| 5358 | * RFC5661, sec. 9.6 states that the client must not rely on getting |
| 5359 | * notifications and must continue to poll for locks, even when the |
| 5360 | * server supports them. Thus this shouldn't lead to clients blocking |
| 5361 | * indefinitely once the lock does become free. |
| 5362 | */ |
| 5363 | BUG_ON(!list_empty(&reaplist)); |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 5364 | spin_lock(&nn->blocked_locks_lock); |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 5365 | while (!list_empty(&nn->blocked_locks_lru)) { |
| 5366 | nbl = list_first_entry(&nn->blocked_locks_lru, |
| 5367 | struct nfsd4_blocked_lock, nbl_lru); |
| 5368 | if (time_after((unsigned long)nbl->nbl_time, |
| 5369 | (unsigned long)cutoff)) { |
| 5370 | t = nbl->nbl_time - cutoff; |
| 5371 | new_timeo = min(new_timeo, t); |
| 5372 | break; |
| 5373 | } |
| 5374 | list_move(&nbl->nbl_lru, &reaplist); |
| 5375 | list_del_init(&nbl->nbl_list); |
| 5376 | } |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 5377 | spin_unlock(&nn->blocked_locks_lock); |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 5378 | |
| 5379 | while (!list_empty(&reaplist)) { |
Naofumi Honda | 64ebe124 | 2017-11-09 10:57:16 -0500 | [diff] [blame] | 5380 | nbl = list_first_entry(&reaplist, |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 5381 | struct nfsd4_blocked_lock, nbl_lru); |
| 5382 | list_del_init(&nbl->nbl_lru); |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 5383 | free_blocked_lock(nbl); |
| 5384 | } |
J. Bruce Fields | 03f318c | 2018-06-08 12:28:47 -0400 | [diff] [blame] | 5385 | out: |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 5386 | new_timeo = max_t(time_t, new_timeo, NFSD_LAUNDROMAT_MINTIMEOUT); |
Jeff Layton | a832e7a | 2014-05-30 09:09:26 -0400 | [diff] [blame] | 5387 | return new_timeo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5388 | } |
| 5389 | |
Harvey Harrison | a254b24 | 2008-02-20 12:49:00 -0800 | [diff] [blame] | 5390 | static struct workqueue_struct *laundry_wq; |
| 5391 | static void laundromat_main(struct work_struct *); |
Harvey Harrison | a254b24 | 2008-02-20 12:49:00 -0800 | [diff] [blame] | 5392 | |
| 5393 | static void |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 5394 | laundromat_main(struct work_struct *laundry) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5395 | { |
| 5396 | time_t t; |
Geliang Tang | 2e55f3a | 2016-01-01 22:06:28 +0800 | [diff] [blame] | 5397 | struct delayed_work *dwork = to_delayed_work(laundry); |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 5398 | struct nfsd_net *nn = container_of(dwork, struct nfsd_net, |
| 5399 | laundromat_work); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5400 | |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 5401 | t = nfs4_laundromat(nn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5402 | dprintk("NFSD: laundromat_main - sleeping for %ld seconds\n", t); |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 5403 | queue_delayed_work(laundry_wq, &nn->laundromat_work, t*HZ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5404 | } |
| 5405 | |
Jeff Layton | 8fcd461 | 2015-07-30 06:57:46 -0400 | [diff] [blame] | 5406 | 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] | 5407 | { |
Jeff Layton | 8fcd461 | 2015-07-30 06:57:46 -0400 | [diff] [blame] | 5408 | if (!fh_match(&fhp->fh_handle, &stp->sc_file->fi_fhandle)) |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 5409 | return nfserr_bad_stateid; |
| 5410 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5411 | } |
| 5412 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5413 | static inline int |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 5414 | access_permit_read(struct nfs4_ol_stateid *stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5415 | { |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 5416 | return test_access(NFS4_SHARE_ACCESS_READ, stp) || |
| 5417 | test_access(NFS4_SHARE_ACCESS_BOTH, stp) || |
| 5418 | test_access(NFS4_SHARE_ACCESS_WRITE, stp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5419 | } |
| 5420 | |
| 5421 | static inline int |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 5422 | access_permit_write(struct nfs4_ol_stateid *stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5423 | { |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 5424 | return test_access(NFS4_SHARE_ACCESS_WRITE, stp) || |
| 5425 | test_access(NFS4_SHARE_ACCESS_BOTH, stp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5426 | } |
| 5427 | |
| 5428 | static |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5429 | __be32 nfs4_check_openmode(struct nfs4_ol_stateid *stp, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5430 | { |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5431 | __be32 status = nfserr_openmode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5432 | |
J. Bruce Fields | 0292191 | 2010-07-29 15:16:59 -0400 | [diff] [blame] | 5433 | /* For lock stateid's, we test the parent open, not the lock: */ |
| 5434 | if (stp->st_openstp) |
| 5435 | stp = stp->st_openstp; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 5436 | if ((flags & WR_STATE) && !access_permit_write(stp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5437 | goto out; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 5438 | if ((flags & RD_STATE) && !access_permit_read(stp)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5439 | goto out; |
| 5440 | status = nfs_ok; |
| 5441 | out: |
| 5442 | return status; |
| 5443 | } |
| 5444 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5445 | static inline __be32 |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 5446 | 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] | 5447 | { |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 5448 | if (ONE_STATEID(stateid) && (flags & RD_STATE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5449 | return nfs_ok; |
J. Bruce Fields | c87fb4a | 2015-08-06 12:47:02 -0400 | [diff] [blame] | 5450 | else if (opens_in_grace(net)) { |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 5451 | /* Answer in remaining cases depends on existence of |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5452 | * conflicting state; so we must wait out the grace period. */ |
| 5453 | return nfserr_grace; |
| 5454 | } else if (flags & WR_STATE) |
| 5455 | return nfs4_share_conflict(current_fh, |
| 5456 | NFS4_SHARE_DENY_WRITE); |
| 5457 | else /* (flags & RD_STATE) && ZERO_STATEID(stateid) */ |
| 5458 | return nfs4_share_conflict(current_fh, |
| 5459 | NFS4_SHARE_DENY_READ); |
| 5460 | } |
| 5461 | |
| 5462 | /* |
| 5463 | * Allow READ/WRITE during grace period on recovered state only for files |
| 5464 | * that are not able to provide mandatory locking. |
| 5465 | */ |
| 5466 | static inline int |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 5467 | grace_disallows_io(struct net *net, struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5468 | { |
J. Bruce Fields | c87fb4a | 2015-08-06 12:47:02 -0400 | [diff] [blame] | 5469 | return opens_in_grace(net) && mandatory_lock(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5470 | } |
| 5471 | |
J. Bruce Fields | 57b7b43 | 2012-04-25 17:58:50 -0400 | [diff] [blame] | 5472 | 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] | 5473 | { |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 5474 | /* |
| 5475 | * When sessions are used the stateid generation number is ignored |
| 5476 | * when it is zero. |
| 5477 | */ |
J. Bruce Fields | 28dde24 | 2011-08-22 10:07:12 -0400 | [diff] [blame] | 5478 | if (has_session && in->si_generation == 0) |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 5479 | return nfs_ok; |
| 5480 | |
| 5481 | if (in->si_generation == ref->si_generation) |
| 5482 | return nfs_ok; |
Andy Adamson | 6668958 | 2009-04-03 08:28:45 +0300 | [diff] [blame] | 5483 | |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 5484 | /* 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] | 5485 | if (nfsd4_stateid_generation_after(in, ref)) |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 5486 | return nfserr_bad_stateid; |
| 5487 | /* |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 5488 | * However, we could see a stateid from the past, even from a |
| 5489 | * non-buggy client. For example, if the client sends a lock |
| 5490 | * while some IO is outstanding, the lock may bump si_generation |
| 5491 | * while the IO is still in flight. The client could avoid that |
| 5492 | * situation by waiting for responses on all the IO requests, |
| 5493 | * but better performance may result in retrying IO that |
| 5494 | * receives an old_stateid error if requests are rarely |
| 5495 | * reordered in flight: |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 5496 | */ |
J. Bruce Fields | 81b8296 | 2011-08-23 11:03:29 -0400 | [diff] [blame] | 5497 | return nfserr_old_stateid; |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 5498 | } |
| 5499 | |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 5500 | static __be32 nfsd4_stid_check_stateid_generation(stateid_t *in, struct nfs4_stid *s, bool has_session) |
| 5501 | { |
| 5502 | __be32 ret; |
| 5503 | |
| 5504 | spin_lock(&s->sc_lock); |
| 5505 | ret = nfsd4_verify_open_stid(s); |
| 5506 | if (ret == nfs_ok) |
| 5507 | ret = check_stateid_generation(in, &s->sc_stateid, has_session); |
| 5508 | spin_unlock(&s->sc_lock); |
| 5509 | return ret; |
| 5510 | } |
| 5511 | |
Christoph Hellwig | ebe9cb3 | 2015-04-28 15:41:15 +0200 | [diff] [blame] | 5512 | static __be32 nfsd4_check_openowner_confirmed(struct nfs4_ol_stateid *ols) |
| 5513 | { |
| 5514 | if (ols->st_stateowner->so_is_open_owner && |
| 5515 | !(openowner(ols->st_stateowner)->oo_flags & NFS4_OO_CONFIRMED)) |
| 5516 | return nfserr_bad_stateid; |
| 5517 | return nfs_ok; |
| 5518 | } |
| 5519 | |
Chuck Lever | 7df302f | 2012-05-29 13:56:37 -0400 | [diff] [blame] | 5520 | static __be32 nfsd4_validate_stateid(struct nfs4_client *cl, stateid_t *stateid) |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 5521 | { |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 5522 | struct nfs4_stid *s; |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5523 | __be32 status = nfserr_bad_stateid; |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 5524 | |
Andrew Elble | ae254dac | 2017-11-09 13:41:10 -0500 | [diff] [blame] | 5525 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) || |
| 5526 | CLOSE_STATEID(stateid)) |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5527 | return status; |
Chuck Lever | 7df302f | 2012-05-29 13:56:37 -0400 | [diff] [blame] | 5528 | /* Client debugging aid. */ |
| 5529 | if (!same_clid(&stateid->si_opaque.so_clid, &cl->cl_clientid)) { |
| 5530 | char addr_str[INET6_ADDRSTRLEN]; |
| 5531 | rpc_ntop((struct sockaddr *)&cl->cl_addr, addr_str, |
| 5532 | sizeof(addr_str)); |
| 5533 | pr_warn_ratelimited("NFSD: client %s testing state ID " |
| 5534 | "with incorrect client ID\n", addr_str); |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5535 | return status; |
Chuck Lever | 7df302f | 2012-05-29 13:56:37 -0400 | [diff] [blame] | 5536 | } |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5537 | spin_lock(&cl->cl_lock); |
| 5538 | s = find_stateid_locked(cl, stateid); |
J. Bruce Fields | 97b7e3b | 2011-09-09 11:26:58 -0400 | [diff] [blame] | 5539 | if (!s) |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5540 | goto out_unlock; |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 5541 | status = nfsd4_stid_check_stateid_generation(stateid, s, 1); |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 5542 | if (status) |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5543 | goto out_unlock; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 5544 | switch (s->sc_type) { |
| 5545 | case NFS4_DELEG_STID: |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5546 | status = nfs_ok; |
| 5547 | break; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 5548 | case NFS4_REVOKED_DELEG_STID: |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5549 | status = nfserr_deleg_revoked; |
| 5550 | break; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 5551 | case NFS4_OPEN_STID: |
| 5552 | case NFS4_LOCK_STID: |
Christoph Hellwig | ebe9cb3 | 2015-04-28 15:41:15 +0200 | [diff] [blame] | 5553 | status = nfsd4_check_openowner_confirmed(openlockstateid(s)); |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5554 | break; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 5555 | default: |
| 5556 | printk("unknown stateid type %x\n", s->sc_type); |
Trond Myklebust | b0fc29d | 2014-07-16 10:31:59 -0400 | [diff] [blame] | 5557 | /* Fallthrough */ |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 5558 | case NFS4_CLOSED_STID: |
Trond Myklebust | b0fc29d | 2014-07-16 10:31:59 -0400 | [diff] [blame] | 5559 | case NFS4_CLOSED_DELEG_STID: |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5560 | status = nfserr_bad_stateid; |
J. Bruce Fields | 2334003 | 2013-04-09 17:42:28 -0400 | [diff] [blame] | 5561 | } |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5562 | out_unlock: |
| 5563 | spin_unlock(&cl->cl_lock); |
| 5564 | return status; |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 5565 | } |
| 5566 | |
Christoph Hellwig | cd61c52 | 2014-08-14 08:44:57 +0200 | [diff] [blame] | 5567 | __be32 |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 5568 | nfsd4_lookup_stateid(struct nfsd4_compound_state *cstate, |
| 5569 | stateid_t *stateid, unsigned char typemask, |
| 5570 | struct nfs4_stid **s, struct nfsd_net *nn) |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5571 | { |
J. Bruce Fields | 0eb6f20 | 2013-03-12 17:36:17 -0400 | [diff] [blame] | 5572 | __be32 status; |
Andrew Elble | 95da1b3a | 2017-11-03 14:06:31 -0400 | [diff] [blame] | 5573 | bool return_revoked = false; |
| 5574 | |
| 5575 | /* |
| 5576 | * only return revoked delegations if explicitly asked. |
| 5577 | * otherwise we report revoked or bad_stateid status. |
| 5578 | */ |
| 5579 | if (typemask & NFS4_REVOKED_DELEG_STID) |
| 5580 | return_revoked = true; |
| 5581 | else if (typemask & NFS4_DELEG_STID) |
| 5582 | typemask |= NFS4_REVOKED_DELEG_STID; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5583 | |
Andrew Elble | ae254dac | 2017-11-09 13:41:10 -0500 | [diff] [blame] | 5584 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid) || |
| 5585 | CLOSE_STATEID(stateid)) |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5586 | return nfserr_bad_stateid; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 5587 | status = lookup_clientid(&stateid->si_opaque.so_clid, cstate, nn); |
Trond Myklebust | a8a7c67 | 2014-03-29 14:43:38 -0400 | [diff] [blame] | 5588 | if (status == nfserr_stale_clientid) { |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 5589 | if (cstate->session) |
Trond Myklebust | a8a7c67 | 2014-03-29 14:43:38 -0400 | [diff] [blame] | 5590 | return nfserr_bad_stateid; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5591 | return nfserr_stale_stateid; |
Trond Myklebust | a8a7c67 | 2014-03-29 14:43:38 -0400 | [diff] [blame] | 5592 | } |
J. Bruce Fields | 0eb6f20 | 2013-03-12 17:36:17 -0400 | [diff] [blame] | 5593 | if (status) |
| 5594 | return status; |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 5595 | *s = find_stateid_by_type(cstate->clp, stateid, typemask); |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5596 | if (!*s) |
| 5597 | return nfserr_bad_stateid; |
Andrew Elble | 95da1b3a | 2017-11-03 14:06:31 -0400 | [diff] [blame] | 5598 | if (((*s)->sc_type == NFS4_REVOKED_DELEG_STID) && !return_revoked) { |
| 5599 | nfs4_put_stid(*s); |
| 5600 | if (cstate->minorversion) |
| 5601 | return nfserr_deleg_revoked; |
| 5602 | return nfserr_bad_stateid; |
| 5603 | } |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5604 | return nfs_ok; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5605 | } |
| 5606 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5607 | static struct nfsd_file * |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5608 | nfs4_find_file(struct nfs4_stid *s, int flags) |
| 5609 | { |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5610 | if (!s) |
| 5611 | return NULL; |
| 5612 | |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5613 | switch (s->sc_type) { |
| 5614 | case NFS4_DELEG_STID: |
| 5615 | if (WARN_ON_ONCE(!s->sc_file->fi_deleg_file)) |
| 5616 | return NULL; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5617 | return nfsd_file_get(s->sc_file->fi_deleg_file); |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5618 | case NFS4_OPEN_STID: |
| 5619 | case NFS4_LOCK_STID: |
| 5620 | if (flags & RD_STATE) |
| 5621 | return find_readable_file(s->sc_file); |
| 5622 | else |
| 5623 | return find_writeable_file(s->sc_file); |
| 5624 | break; |
| 5625 | } |
| 5626 | |
| 5627 | return NULL; |
| 5628 | } |
| 5629 | |
| 5630 | static __be32 |
J. Bruce Fields | d8836f7 | 2018-11-05 20:04:06 -0500 | [diff] [blame] | 5631 | nfs4_check_olstateid(struct nfs4_ol_stateid *ols, int flags) |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5632 | { |
| 5633 | __be32 status; |
| 5634 | |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5635 | status = nfsd4_check_openowner_confirmed(ols); |
| 5636 | if (status) |
| 5637 | return status; |
| 5638 | return nfs4_check_openmode(ols, flags); |
| 5639 | } |
| 5640 | |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5641 | static __be32 |
| 5642 | 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] | 5643 | struct nfsd_file **nfp, int flags) |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5644 | { |
| 5645 | int acc = (flags & RD_STATE) ? NFSD_MAY_READ : NFSD_MAY_WRITE; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5646 | struct nfsd_file *nf; |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5647 | __be32 status; |
| 5648 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5649 | nf = nfs4_find_file(s, flags); |
| 5650 | if (nf) { |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5651 | status = nfsd_permission(rqstp, fhp->fh_export, fhp->fh_dentry, |
| 5652 | acc | NFSD_MAY_OWNER_OVERRIDE); |
Jeff Layton | 5c4583b | 2019-08-18 14:18:54 -0400 | [diff] [blame] | 5653 | if (status) { |
| 5654 | nfsd_file_put(nf); |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5655 | goto out; |
Jeff Layton | 5c4583b | 2019-08-18 14:18:54 -0400 | [diff] [blame] | 5656 | } |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5657 | } else { |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5658 | status = nfsd_file_acquire(rqstp, fhp, acc, &nf); |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5659 | if (status) |
| 5660 | return status; |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5661 | } |
Jeff Layton | 5c4583b | 2019-08-18 14:18:54 -0400 | [diff] [blame] | 5662 | *nfp = nf; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5663 | out: |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 5664 | return status; |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5665 | } |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame^] | 5666 | static void |
| 5667 | _free_cpntf_state_locked(struct nfsd_net *nn, struct nfs4_cpntf_state *cps) |
| 5668 | { |
| 5669 | WARN_ON_ONCE(cps->cp_stateid.sc_type != NFS4_COPYNOTIFY_STID); |
| 5670 | if (!refcount_dec_and_test(&cps->cp_stateid.sc_count)) |
| 5671 | return; |
| 5672 | list_del(&cps->cp_list); |
| 5673 | idr_remove(&nn->s2s_cp_stateids, |
| 5674 | cps->cp_stateid.stid.si_opaque.so_id); |
| 5675 | kfree(cps); |
| 5676 | } |
| 5677 | |
| 5678 | void nfs4_put_cpntf_state(struct nfsd_net *nn, struct nfs4_cpntf_state *cps) |
| 5679 | { |
| 5680 | spin_lock(&nn->s2s_cp_lock); |
| 5681 | _free_cpntf_state_locked(nn, cps); |
| 5682 | spin_unlock(&nn->s2s_cp_lock); |
| 5683 | } |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5684 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5685 | /* |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5686 | * Checks for stateid operations |
| 5687 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5688 | __be32 |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5689 | nfs4_preprocess_stateid_op(struct svc_rqst *rqstp, |
Anna Schumaker | aa0d6ae | 2015-12-03 12:59:51 +0100 | [diff] [blame] | 5690 | struct nfsd4_compound_state *cstate, struct svc_fh *fhp, |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame^] | 5691 | stateid_t *stateid, int flags, struct nfsd_file **nfp, |
| 5692 | struct nfs4_stid **cstid) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5693 | { |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5694 | struct inode *ino = d_inode(fhp->fh_dentry); |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5695 | struct net *net = SVC_NET(rqstp); |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5696 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5697 | struct nfs4_stid *s = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5698 | __be32 status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5699 | |
Jeff Layton | 5c4583b | 2019-08-18 14:18:54 -0400 | [diff] [blame] | 5700 | if (nfp) |
| 5701 | *nfp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5702 | |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 5703 | if (grace_disallows_io(net, ino)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5704 | return nfserr_grace; |
| 5705 | |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5706 | if (ZERO_STATEID(stateid) || ONE_STATEID(stateid)) { |
| 5707 | status = check_special_stateids(net, fhp, stateid, flags); |
| 5708 | goto done; |
| 5709 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5710 | |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 5711 | status = nfsd4_lookup_stateid(cstate, stateid, |
Jeff Layton | db24b3b | 2014-06-30 11:48:36 -0400 | [diff] [blame] | 5712 | NFS4_DELEG_STID|NFS4_OPEN_STID|NFS4_LOCK_STID, |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 5713 | &s, nn); |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5714 | if (status) |
Trond Myklebust | c2d1d6a | 2014-07-30 08:27:25 -0400 | [diff] [blame] | 5715 | return status; |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 5716 | status = nfsd4_stid_check_stateid_generation(stateid, s, |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5717 | nfsd4_has_session(cstate)); |
J. Bruce Fields | 69064a2 | 2011-09-09 11:54:57 -0400 | [diff] [blame] | 5718 | if (status) |
| 5719 | goto out; |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5720 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 5721 | switch (s->sc_type) { |
| 5722 | case NFS4_DELEG_STID: |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5723 | status = nfs4_check_delegmode(delegstateid(s), flags); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 5724 | break; |
| 5725 | case NFS4_OPEN_STID: |
| 5726 | case NFS4_LOCK_STID: |
J. Bruce Fields | d8836f7 | 2018-11-05 20:04:06 -0500 | [diff] [blame] | 5727 | status = nfs4_check_olstateid(openlockstateid(s), flags); |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 5728 | break; |
| 5729 | default: |
Trond Myklebust | 14bcab1 | 2014-04-18 14:44:07 -0400 | [diff] [blame] | 5730 | status = nfserr_bad_stateid; |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5731 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5732 | } |
Jeff Layton | 8fcd461 | 2015-07-30 06:57:46 -0400 | [diff] [blame] | 5733 | if (status) |
| 5734 | goto out; |
| 5735 | status = nfs4_check_fh(fhp, s); |
Christoph Hellwig | a0649b2 | 2015-06-18 16:44:59 +0200 | [diff] [blame] | 5736 | |
Christoph Hellwig | af90f70 | 2015-06-18 16:45:00 +0200 | [diff] [blame] | 5737 | done: |
Jeff Layton | 5c4583b | 2019-08-18 14:18:54 -0400 | [diff] [blame] | 5738 | if (status == nfs_ok && nfp) |
| 5739 | status = nfs4_check_file(rqstp, fhp, s, nfp, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5740 | out: |
Olga Kornievskaia | 624322f | 2019-10-04 16:34:26 -0400 | [diff] [blame^] | 5741 | if (s) { |
| 5742 | if (!status && cstid) |
| 5743 | *cstid = s; |
| 5744 | else |
| 5745 | nfs4_put_stid(s); |
| 5746 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5747 | return status; |
| 5748 | } |
| 5749 | |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 5750 | /* |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 5751 | * Test if the stateid is valid |
| 5752 | */ |
| 5753 | __be32 |
| 5754 | nfsd4_test_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5755 | union nfsd4_op_u *u) |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 5756 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5757 | struct nfsd4_test_stateid *test_stateid = &u->test_stateid; |
Bryan Schumaker | 03cfb42 | 2012-01-27 10:22:49 -0500 | [diff] [blame] | 5758 | struct nfsd4_test_stateid_id *stateid; |
| 5759 | struct nfs4_client *cl = cstate->session->se_client; |
| 5760 | |
Bryan Schumaker | 03cfb42 | 2012-01-27 10:22:49 -0500 | [diff] [blame] | 5761 | 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] | 5762 | stateid->ts_id_status = |
| 5763 | nfsd4_validate_stateid(cl, &stateid->ts_id_stateid); |
Bryan Schumaker | 03cfb42 | 2012-01-27 10:22:49 -0500 | [diff] [blame] | 5764 | |
Bryan Schumaker | 1745680 | 2011-07-13 10:50:48 -0400 | [diff] [blame] | 5765 | return nfs_ok; |
| 5766 | } |
| 5767 | |
Chuck Lever | 4269139 | 2016-08-11 10:37:30 -0400 | [diff] [blame] | 5768 | static __be32 |
| 5769 | nfsd4_free_lock_stateid(stateid_t *stateid, struct nfs4_stid *s) |
| 5770 | { |
| 5771 | struct nfs4_ol_stateid *stp = openlockstateid(s); |
| 5772 | __be32 ret; |
| 5773 | |
Trond Myklebust | 659aefb | 2017-11-03 08:00:13 -0400 | [diff] [blame] | 5774 | ret = nfsd4_lock_ol_stateid(stp); |
| 5775 | if (ret) |
| 5776 | goto out_put_stid; |
Chuck Lever | 4269139 | 2016-08-11 10:37:30 -0400 | [diff] [blame] | 5777 | |
| 5778 | ret = check_stateid_generation(stateid, &s->sc_stateid, 1); |
| 5779 | if (ret) |
| 5780 | goto out; |
| 5781 | |
| 5782 | ret = nfserr_locks_held; |
| 5783 | if (check_for_locks(stp->st_stid.sc_file, |
| 5784 | lockowner(stp->st_stateowner))) |
| 5785 | goto out; |
| 5786 | |
| 5787 | release_lock_stateid(stp); |
| 5788 | ret = nfs_ok; |
| 5789 | |
| 5790 | out: |
| 5791 | mutex_unlock(&stp->st_mutex); |
Trond Myklebust | 659aefb | 2017-11-03 08:00:13 -0400 | [diff] [blame] | 5792 | out_put_stid: |
Chuck Lever | 4269139 | 2016-08-11 10:37:30 -0400 | [diff] [blame] | 5793 | nfs4_put_stid(s); |
| 5794 | return ret; |
| 5795 | } |
| 5796 | |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 5797 | __be32 |
| 5798 | nfsd4_free_stateid(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5799 | union nfsd4_op_u *u) |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 5800 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5801 | struct nfsd4_free_stateid *free_stateid = &u->free_stateid; |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 5802 | stateid_t *stateid = &free_stateid->fr_stateid; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 5803 | struct nfs4_stid *s; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 5804 | struct nfs4_delegation *dp; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5805 | struct nfs4_client *cl = cstate->session->se_client; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 5806 | __be32 ret = nfserr_bad_stateid; |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 5807 | |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5808 | spin_lock(&cl->cl_lock); |
| 5809 | s = find_stateid_locked(cl, stateid); |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 5810 | if (!s) |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5811 | goto out_unlock; |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 5812 | spin_lock(&s->sc_lock); |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 5813 | switch (s->sc_type) { |
| 5814 | case NFS4_DELEG_STID: |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 5815 | ret = nfserr_locks_held; |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5816 | break; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 5817 | case NFS4_OPEN_STID: |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5818 | ret = check_stateid_generation(stateid, &s->sc_stateid, 1); |
| 5819 | if (ret) |
| 5820 | break; |
| 5821 | ret = nfserr_locks_held; |
| 5822 | break; |
J. Bruce Fields | 2da1cec | 2011-09-16 18:56:20 -0400 | [diff] [blame] | 5823 | case NFS4_LOCK_STID: |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 5824 | spin_unlock(&s->sc_lock); |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 5825 | refcount_inc(&s->sc_count); |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5826 | spin_unlock(&cl->cl_lock); |
Chuck Lever | 4269139 | 2016-08-11 10:37:30 -0400 | [diff] [blame] | 5827 | ret = nfsd4_free_lock_stateid(stateid, s); |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5828 | goto out; |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 5829 | case NFS4_REVOKED_DELEG_STID: |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 5830 | spin_unlock(&s->sc_lock); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 5831 | dp = delegstateid(s); |
Jeff Layton | 2d4a532 | 2014-07-25 07:34:21 -0400 | [diff] [blame] | 5832 | list_del_init(&dp->dl_recall_lru); |
| 5833 | spin_unlock(&cl->cl_lock); |
Trond Myklebust | 6011695 | 2014-07-29 21:34:06 -0400 | [diff] [blame] | 5834 | nfs4_put_stid(s); |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 5835 | ret = nfs_ok; |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5836 | goto out; |
| 5837 | /* Default falls through and returns nfserr_bad_stateid */ |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 5838 | } |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 5839 | spin_unlock(&s->sc_lock); |
Jeff Layton | 1af71cc | 2014-07-29 21:34:14 -0400 | [diff] [blame] | 5840 | out_unlock: |
| 5841 | spin_unlock(&cl->cl_lock); |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 5842 | out: |
Bryan Schumaker | e1ca12d | 2011-07-13 11:04:21 -0400 | [diff] [blame] | 5843 | return ret; |
| 5844 | } |
| 5845 | |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 5846 | static inline int |
| 5847 | setlkflg (int type) |
| 5848 | { |
| 5849 | return (type == NFS4_READW_LT || type == NFS4_READ_LT) ? |
| 5850 | RD_STATE : WR_STATE; |
| 5851 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5852 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5853 | 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] | 5854 | { |
| 5855 | struct svc_fh *current_fh = &cstate->current_fh; |
| 5856 | struct nfs4_stateowner *sop = stp->st_stateowner; |
| 5857 | __be32 status; |
| 5858 | |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 5859 | status = nfsd4_check_seqid(cstate, sop, seqid); |
| 5860 | if (status) |
| 5861 | return status; |
Trond Myklebust | 9271d7e | 2017-11-03 08:00:15 -0400 | [diff] [blame] | 5862 | status = nfsd4_lock_ol_stateid(stp); |
| 5863 | if (status != nfs_ok) |
| 5864 | return status; |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 5865 | 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] | 5866 | if (status == nfs_ok) |
| 5867 | status = nfs4_check_fh(current_fh, &stp->st_stid); |
| 5868 | if (status != nfs_ok) |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 5869 | mutex_unlock(&stp->st_mutex); |
Jeff Layton | 35a92fe | 2015-09-17 07:47:08 -0400 | [diff] [blame] | 5870 | return status; |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 5871 | } |
| 5872 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5873 | /* |
| 5874 | * Checks for sequence id mutating operations. |
| 5875 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5876 | static __be32 |
Benny Halevy | dd453df | 2009-04-03 08:28:41 +0300 | [diff] [blame] | 5877 | nfs4_preprocess_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, |
J. Bruce Fields | 2288d0e | 2011-09-06 15:50:21 -0400 | [diff] [blame] | 5878 | stateid_t *stateid, char typemask, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5879 | struct nfs4_ol_stateid **stpp, |
| 5880 | struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5881 | { |
J. Bruce Fields | 0836f58 | 2008-01-26 19:08:12 -0500 | [diff] [blame] | 5882 | __be32 status; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 5883 | struct nfs4_stid *s; |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 5884 | struct nfs4_ol_stateid *stp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5885 | |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 5886 | dprintk("NFSD: %s: seqid=%d stateid = " STATEID_FMT "\n", __func__, |
| 5887 | seqid, STATEID_VAL(stateid)); |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 5888 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5889 | *stpp = NULL; |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 5890 | status = nfsd4_lookup_stateid(cstate, stateid, typemask, &s, nn); |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 5891 | if (status) |
| 5892 | return status; |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 5893 | stp = openlockstateid(s); |
Jeff Layton | 58fb12e | 2014-07-29 21:34:27 -0400 | [diff] [blame] | 5894 | nfsd4_cstate_assign_replay(cstate, stp->st_stateowner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5895 | |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 5896 | status = nfs4_seqid_op_checks(cstate, stateid, seqid, stp); |
Trond Myklebust | fd91101 | 2014-07-29 21:34:24 -0400 | [diff] [blame] | 5897 | if (!status) |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 5898 | *stpp = stp; |
Trond Myklebust | fd91101 | 2014-07-29 21:34:24 -0400 | [diff] [blame] | 5899 | else |
| 5900 | nfs4_put_stid(&stp->st_stid); |
Trond Myklebust | e17f99b | 2014-06-30 11:48:34 -0400 | [diff] [blame] | 5901 | return status; |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 5902 | } |
J. Bruce Fields | 39325bd | 2007-11-26 17:06:39 -0500 | [diff] [blame] | 5903 | |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5904 | static __be32 nfs4_preprocess_confirmed_seqid_op(struct nfsd4_compound_state *cstate, u32 seqid, |
| 5905 | 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] | 5906 | { |
| 5907 | __be32 status; |
| 5908 | struct nfs4_openowner *oo; |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 5909 | struct nfs4_ol_stateid *stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5910 | |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 5911 | status = nfs4_preprocess_seqid_op(cstate, seqid, stateid, |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 5912 | NFS4_OPEN_STID, &stp, nn); |
J. Bruce Fields | 7a8711c | 2011-09-02 09:03:37 -0400 | [diff] [blame] | 5913 | if (status) |
| 5914 | return status; |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 5915 | oo = openowner(stp->st_stateowner); |
| 5916 | if (!(oo->oo_flags & NFS4_OO_CONFIRMED)) { |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 5917 | mutex_unlock(&stp->st_mutex); |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 5918 | nfs4_put_stid(&stp->st_stid); |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 5919 | return nfserr_bad_stateid; |
Trond Myklebust | 4cbfc9f | 2014-07-29 21:34:23 -0400 | [diff] [blame] | 5920 | } |
| 5921 | *stpp = stp; |
NeilBrown | 3a4f98b | 2005-07-07 17:59:26 -0700 | [diff] [blame] | 5922 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5923 | } |
| 5924 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5925 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 5926 | nfsd4_open_confirm(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5927 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5928 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5929 | struct nfsd4_open_confirm *oc = &u->open_confirm; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5930 | __be32 status; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5931 | struct nfs4_openowner *oo; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5932 | struct nfs4_ol_stateid *stp; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5933 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5934 | |
Al Viro | a6a9f18 | 2013-09-16 10:57:01 -0400 | [diff] [blame] | 5935 | dprintk("NFSD: nfsd4_open_confirm on file %pd\n", |
| 5936 | cstate->current_fh.fh_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5937 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 5938 | status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0); |
J. Bruce Fields | a8cddc5 | 2006-06-30 01:56:13 -0700 | [diff] [blame] | 5939 | if (status) |
| 5940 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5941 | |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 5942 | status = nfs4_preprocess_seqid_op(cstate, |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 5943 | oc->oc_seqid, &oc->oc_req_stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 5944 | NFS4_OPEN_STID, &stp, nn); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 5945 | if (status) |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 5946 | goto out; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 5947 | oo = openowner(stp->st_stateowner); |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 5948 | status = nfserr_bad_stateid; |
Jeff Layton | 35a92fe | 2015-09-17 07:47:08 -0400 | [diff] [blame] | 5949 | if (oo->oo_flags & NFS4_OO_CONFIRMED) { |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 5950 | mutex_unlock(&stp->st_mutex); |
Trond Myklebust | 2585fc7 | 2014-07-29 21:34:21 -0400 | [diff] [blame] | 5951 | goto put_stateid; |
Jeff Layton | 35a92fe | 2015-09-17 07:47:08 -0400 | [diff] [blame] | 5952 | } |
J. Bruce Fields | dad1c06 | 2011-09-12 12:24:13 -0400 | [diff] [blame] | 5953 | oo->oo_flags |= NFS4_OO_CONFIRMED; |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 5954 | nfs4_inc_and_copy_stateid(&oc->oc_resp_stateid, &stp->st_stid); |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 5955 | mutex_unlock(&stp->st_mutex); |
Benny Halevy | 8c10cbd | 2009-10-19 12:04:53 +0200 | [diff] [blame] | 5956 | dprintk("NFSD: %s: success, seqid=%d stateid=" STATEID_FMT "\n", |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 5957 | __func__, oc->oc_seqid, STATEID_VAL(&stp->st_stid.sc_stateid)); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 5958 | |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 5959 | nfsd4_client_record_create(oo->oo_owner.so_client); |
J. Bruce Fields | 68b66e8 | 2011-09-02 12:19:43 -0400 | [diff] [blame] | 5960 | status = nfs_ok; |
Trond Myklebust | 2585fc7 | 2014-07-29 21:34:21 -0400 | [diff] [blame] | 5961 | put_stateid: |
| 5962 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5963 | out: |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 5964 | nfsd4_bump_seqid(cstate, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5965 | return status; |
| 5966 | } |
| 5967 | |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 5968 | 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] | 5969 | { |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 5970 | if (!test_access(access, stp)) |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 5971 | return; |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 5972 | nfs4_file_put_access(stp->st_stid.sc_file, access); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 5973 | clear_access(access, stp); |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 5974 | } |
J. Bruce Fields | f197c27 | 2011-06-29 08:23:50 -0400 | [diff] [blame] | 5975 | |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 5976 | static inline void nfs4_stateid_downgrade(struct nfs4_ol_stateid *stp, u32 to_access) |
| 5977 | { |
| 5978 | switch (to_access) { |
| 5979 | case NFS4_SHARE_ACCESS_READ: |
| 5980 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_WRITE); |
| 5981 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); |
| 5982 | break; |
| 5983 | case NFS4_SHARE_ACCESS_WRITE: |
| 5984 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_READ); |
| 5985 | nfs4_stateid_downgrade_bit(stp, NFS4_SHARE_ACCESS_BOTH); |
| 5986 | break; |
| 5987 | case NFS4_SHARE_ACCESS_BOTH: |
| 5988 | break; |
| 5989 | default: |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 5990 | WARN_ON_ONCE(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5991 | } |
| 5992 | } |
| 5993 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5994 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 5995 | nfsd4_open_downgrade(struct svc_rqst *rqstp, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5996 | struct nfsd4_compound_state *cstate, union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5997 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 5998 | struct nfsd4_open_downgrade *od = &u->open_downgrade; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 5999 | __be32 status; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 6000 | struct nfs4_ol_stateid *stp; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6001 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6002 | |
Al Viro | a6a9f18 | 2013-09-16 10:57:01 -0400 | [diff] [blame] | 6003 | dprintk("NFSD: nfsd4_open_downgrade on file %pd\n", |
| 6004 | cstate->current_fh.fh_dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6005 | |
J. Bruce Fields | c30e92d | 2011-10-10 17:34:31 -0400 | [diff] [blame] | 6006 | /* We don't yet support WANT bits: */ |
Benny Halevy | 2c8bd7e | 2012-02-16 20:57:09 +0200 | [diff] [blame] | 6007 | if (od->od_deleg_want) |
| 6008 | dprintk("NFSD: %s: od_deleg_want=0x%x ignored\n", __func__, |
| 6009 | od->od_deleg_want); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6010 | |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 6011 | status = nfs4_preprocess_confirmed_seqid_op(cstate, od->od_seqid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6012 | &od->od_stateid, &stp, nn); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 6013 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6014 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6015 | status = nfserr_inval; |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 6016 | if (!test_access(od->od_share_access, stp)) { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 6017 | 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] | 6018 | stp->st_access_bmap, od->od_share_access); |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 6019 | goto put_stateid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6020 | } |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 6021 | if (!test_deny(od->od_share_deny, stp)) { |
Jeff Layton | c11c591 | 2014-07-10 14:07:30 -0400 | [diff] [blame] | 6022 | 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] | 6023 | stp->st_deny_bmap, od->od_share_deny); |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 6024 | goto put_stateid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6025 | } |
J. Bruce Fields | 6409a5a | 2011-09-28 11:37:56 -0400 | [diff] [blame] | 6026 | nfs4_stateid_downgrade(stp, od->od_share_access); |
Jeff Layton | ce0fc43 | 2012-05-11 09:45:14 -0400 | [diff] [blame] | 6027 | reset_union_bmap_deny(od->od_share_deny, stp); |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 6028 | nfs4_inc_and_copy_stateid(&od->od_stateid, &stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6029 | status = nfs_ok; |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 6030 | put_stateid: |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 6031 | mutex_unlock(&stp->st_mutex); |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 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 | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 6038 | static void nfsd4_close_open_stateid(struct nfs4_ol_stateid *s) |
| 6039 | { |
Trond Myklebust | acf9295 | 2014-06-30 11:48:37 -0400 | [diff] [blame] | 6040 | struct nfs4_client *clp = s->st_stid.sc_client; |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 6041 | bool unhashed; |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 6042 | LIST_HEAD(reaplist); |
Trond Myklebust | acf9295 | 2014-06-30 11:48:37 -0400 | [diff] [blame] | 6043 | |
Jeff Layton | 2c41beb | 2014-07-29 21:34:41 -0400 | [diff] [blame] | 6044 | spin_lock(&clp->cl_lock); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 6045 | unhashed = unhash_open_stateid(s, &reaplist); |
Trond Myklebust | acf9295 | 2014-06-30 11:48:37 -0400 | [diff] [blame] | 6046 | |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 6047 | if (clp->cl_minorversion) { |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 6048 | if (unhashed) |
| 6049 | put_ol_stateid_locked(s, &reaplist); |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 6050 | spin_unlock(&clp->cl_lock); |
| 6051 | free_ol_stateid_reaplist(&reaplist); |
| 6052 | } else { |
| 6053 | spin_unlock(&clp->cl_lock); |
| 6054 | free_ol_stateid_reaplist(&reaplist); |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 6055 | if (unhashed) |
| 6056 | move_to_close_lru(s, clp->net); |
Jeff Layton | d83017f | 2014-07-29 21:34:42 -0400 | [diff] [blame] | 6057 | } |
J. Bruce Fields | 38c387b | 2011-09-16 17:42:48 -0400 | [diff] [blame] | 6058 | } |
| 6059 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6060 | /* |
| 6061 | * nfs4_unlock_state() called after encode |
| 6062 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6063 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6064 | nfsd4_close(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6065 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6066 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6067 | struct nfsd4_close *close = &u->close; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6068 | __be32 status; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 6069 | struct nfs4_ol_stateid *stp; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6070 | struct net *net = SVC_NET(rqstp); |
| 6071 | struct nfsd_net *nn = net_generic(net, 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_close 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 | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 6076 | status = nfs4_preprocess_seqid_op(cstate, close->cl_seqid, |
| 6077 | &close->cl_stateid, |
| 6078 | NFS4_OPEN_STID|NFS4_CLOSED_STID, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6079 | &stp, nn); |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 6080 | nfsd4_bump_seqid(cstate, status); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 6081 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6082 | goto out; |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 6083 | |
| 6084 | stp->st_stid.sc_type = NFS4_CLOSED_STID; |
Jeff Layton | bd2deca | 2018-02-15 15:16:18 -0500 | [diff] [blame] | 6085 | |
| 6086 | /* |
| 6087 | * Technically we don't _really_ have to increment or copy it, since |
| 6088 | * it should just be gone after this operation and we clobber the |
| 6089 | * copied value below, but we continue to do so here just to ensure |
| 6090 | * that racing ops see that there was a state change. |
| 6091 | */ |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 6092 | nfs4_inc_and_copy_stateid(&close->cl_stateid, &stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6093 | |
J. Bruce Fields | f7a4d87 | 2011-09-16 20:12:38 -0400 | [diff] [blame] | 6094 | nfsd4_close_open_stateid(stp); |
Trond Myklebust | 15ca08d | 2017-11-03 08:00:10 -0400 | [diff] [blame] | 6095 | mutex_unlock(&stp->st_mutex); |
Trond Myklebust | 8a0b589 | 2014-07-29 21:34:20 -0400 | [diff] [blame] | 6096 | |
Jeff Layton | bd2deca | 2018-02-15 15:16:18 -0500 | [diff] [blame] | 6097 | /* v4.1+ suggests that we send a special stateid in here, since the |
| 6098 | * clients should just ignore this anyway. Since this is not useful |
| 6099 | * for v4.0 clients either, we set it to the special close_stateid |
| 6100 | * universally. |
| 6101 | * |
| 6102 | * See RFC5661 section 18.2.4, and RFC7530 section 16.2.5 |
| 6103 | */ |
| 6104 | memcpy(&close->cl_stateid, &close_stateid, sizeof(close->cl_stateid)); |
Trond Myklebust | fb500a7 | 2017-11-03 08:00:12 -0400 | [diff] [blame] | 6105 | |
Trond Myklebust | 8a0b589 | 2014-07-29 21:34:20 -0400 | [diff] [blame] | 6106 | /* put reference from nfs4_preprocess_seqid_op */ |
| 6107 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6108 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6109 | return status; |
| 6110 | } |
| 6111 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6112 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6113 | nfsd4_delegreturn(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6114 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6115 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6116 | struct nfsd4_delegreturn *dr = &u->delegreturn; |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 6117 | struct nfs4_delegation *dp; |
| 6118 | stateid_t *stateid = &dr->dr_stateid; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 6119 | struct nfs4_stid *s; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6120 | __be32 status; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6121 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6122 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6123 | if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 6124 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6125 | |
Trond Myklebust | 2dd6e45 | 2014-06-30 11:48:43 -0400 | [diff] [blame] | 6126 | status = nfsd4_lookup_stateid(cstate, stateid, NFS4_DELEG_STID, &s, nn); |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 6127 | if (status) |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 6128 | goto out; |
J. Bruce Fields | 38c2f4b | 2011-09-23 17:01:19 -0400 | [diff] [blame] | 6129 | dp = delegstateid(s); |
Trond Myklebust | 03da316 | 2017-11-03 08:00:16 -0400 | [diff] [blame] | 6130 | 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] | 6131 | if (status) |
Trond Myklebust | fd91101 | 2014-07-29 21:34:24 -0400 | [diff] [blame] | 6132 | goto put_stateid; |
J. Bruce Fields | 203a8c8 | 2009-02-21 13:29:14 -0800 | [diff] [blame] | 6133 | |
J. Bruce Fields | 3bd64a5 | 2013-04-09 17:02:51 -0400 | [diff] [blame] | 6134 | destroy_delegation(dp); |
Trond Myklebust | fd91101 | 2014-07-29 21:34:24 -0400 | [diff] [blame] | 6135 | put_stateid: |
| 6136 | nfs4_put_stid(&dp->dl_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6137 | out: |
| 6138 | return status; |
| 6139 | } |
| 6140 | |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 6141 | static inline u64 |
| 6142 | end_offset(u64 start, u64 len) |
| 6143 | { |
| 6144 | u64 end; |
| 6145 | |
| 6146 | end = start + len; |
| 6147 | return end >= start ? end: NFS4_MAX_UINT64; |
| 6148 | } |
| 6149 | |
| 6150 | /* last octet in a range */ |
| 6151 | static inline u64 |
| 6152 | last_byte_offset(u64 start, u64 len) |
| 6153 | { |
| 6154 | u64 end; |
| 6155 | |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 6156 | WARN_ON_ONCE(!len); |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 6157 | end = start + len; |
| 6158 | return end > start ? end - 1: NFS4_MAX_UINT64; |
| 6159 | } |
| 6160 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6161 | /* |
| 6162 | * TODO: Linux file offsets are _signed_ 64-bit quantities, which means that |
| 6163 | * we can't properly handle lock requests that go beyond the (2^63 - 1)-th |
| 6164 | * byte, because of sign extension problems. Since NFSv4 calls for 64-bit |
| 6165 | * locking, this prevents us from being completely protocol-compliant. The |
| 6166 | * real solution to this problem is to start using unsigned file offsets in |
| 6167 | * the VFS, but this is a very deep change! |
| 6168 | */ |
| 6169 | static inline void |
| 6170 | nfs4_transform_lock_offset(struct file_lock *lock) |
| 6171 | { |
| 6172 | if (lock->fl_start < 0) |
| 6173 | lock->fl_start = OFFSET_MAX; |
| 6174 | if (lock->fl_end < 0) |
| 6175 | lock->fl_end = OFFSET_MAX; |
| 6176 | } |
| 6177 | |
Jeff Layton | cae80b3 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 6178 | static fl_owner_t |
| 6179 | nfsd4_fl_get_owner(fl_owner_t owner) |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6180 | { |
Jeff Layton | cae80b3 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 6181 | struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner; |
| 6182 | |
| 6183 | nfs4_get_stateowner(&lo->lo_owner); |
| 6184 | return owner; |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6185 | } |
| 6186 | |
Jeff Layton | cae80b3 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 6187 | static void |
| 6188 | nfsd4_fl_put_owner(fl_owner_t owner) |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6189 | { |
Jeff Layton | cae80b3 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 6190 | struct nfs4_lockowner *lo = (struct nfs4_lockowner *)owner; |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6191 | |
Jeff Layton | cae80b3 | 2015-04-03 09:04:04 -0400 | [diff] [blame] | 6192 | if (lo) |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6193 | nfs4_put_stateowner(&lo->lo_owner); |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6194 | } |
| 6195 | |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6196 | static void |
| 6197 | nfsd4_lm_notify(struct file_lock *fl) |
| 6198 | { |
| 6199 | struct nfs4_lockowner *lo = (struct nfs4_lockowner *)fl->fl_owner; |
| 6200 | struct net *net = lo->lo_owner.so_client->net; |
| 6201 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 6202 | struct nfsd4_blocked_lock *nbl = container_of(fl, |
| 6203 | struct nfsd4_blocked_lock, nbl_lock); |
| 6204 | bool queue = false; |
| 6205 | |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 6206 | /* 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] | 6207 | spin_lock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6208 | if (!list_empty(&nbl->nbl_list)) { |
| 6209 | list_del_init(&nbl->nbl_list); |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 6210 | list_del_init(&nbl->nbl_lru); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6211 | queue = true; |
| 6212 | } |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 6213 | spin_unlock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6214 | |
| 6215 | if (queue) |
| 6216 | nfsd4_run_cb(&nbl->nbl_cb); |
| 6217 | } |
| 6218 | |
Alexey Dobriyan | 7b02196 | 2009-09-21 17:01:12 -0700 | [diff] [blame] | 6219 | static const struct lock_manager_operations nfsd_posix_mng_ops = { |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6220 | .lm_notify = nfsd4_lm_notify, |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6221 | .lm_get_owner = nfsd4_fl_get_owner, |
| 6222 | .lm_put_owner = nfsd4_fl_put_owner, |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 6223 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6224 | |
| 6225 | static inline void |
| 6226 | nfs4_set_lock_denied(struct file_lock *fl, struct nfsd4_lock_denied *deny) |
| 6227 | { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6228 | struct nfs4_lockowner *lo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6229 | |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 6230 | if (fl->fl_lmops == &nfsd_posix_mng_ops) { |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6231 | lo = (struct nfs4_lockowner *) fl->fl_owner; |
J. Bruce Fields | 6f4859b | 2019-06-19 14:30:33 -0400 | [diff] [blame] | 6232 | xdr_netobj_dup(&deny->ld_owner, &lo->lo_owner.so_owner, |
| 6233 | GFP_KERNEL); |
J. Bruce Fields | 7c13f34 | 2011-08-30 22:15:47 -0400 | [diff] [blame] | 6234 | if (!deny->ld_owner.data) |
| 6235 | /* We just don't care that much */ |
| 6236 | goto nevermind; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6237 | deny->ld_clientid = lo->lo_owner.so_client->cl_clientid; |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 6238 | } else { |
J. Bruce Fields | 7c13f34 | 2011-08-30 22:15:47 -0400 | [diff] [blame] | 6239 | nevermind: |
| 6240 | deny->ld_owner.len = 0; |
| 6241 | deny->ld_owner.data = NULL; |
NeilBrown | d5b9026 | 2006-04-10 22:55:22 -0700 | [diff] [blame] | 6242 | deny->ld_clientid.cl_boot = 0; |
| 6243 | deny->ld_clientid.cl_id = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6244 | } |
| 6245 | deny->ld_start = fl->fl_start; |
Benny Halevy | 87df4de | 2008-12-15 19:42:03 +0200 | [diff] [blame] | 6246 | deny->ld_length = NFS4_MAX_UINT64; |
| 6247 | if (fl->fl_end != NFS4_MAX_UINT64) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6248 | deny->ld_length = fl->fl_end - fl->fl_start + 1; |
| 6249 | deny->ld_type = NFS4_READ_LT; |
| 6250 | if (fl->fl_type != F_RDLCK) |
| 6251 | deny->ld_type = NFS4_WRITE_LT; |
| 6252 | } |
| 6253 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6254 | static struct nfs4_lockowner * |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 6255 | find_lockowner_str_locked(struct nfs4_client *clp, struct xdr_netobj *owner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6256 | { |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6257 | unsigned int strhashval = ownerstr_hashval(owner); |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame] | 6258 | struct nfs4_stateowner *so; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6259 | |
Trond Myklebust | 0a880a2 | 2014-07-30 08:27:10 -0400 | [diff] [blame] | 6260 | lockdep_assert_held(&clp->cl_lock); |
| 6261 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6262 | list_for_each_entry(so, &clp->cl_ownerstr_hashtbl[strhashval], |
| 6263 | so_strhash) { |
Trond Myklebust | b3c32bc | 2014-06-30 11:48:40 -0400 | [diff] [blame] | 6264 | if (so->so_is_open_owner) |
| 6265 | continue; |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 6266 | if (same_owner_str(so, owner)) |
| 6267 | return lockowner(nfs4_get_stateowner(so)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6268 | } |
| 6269 | return NULL; |
| 6270 | } |
| 6271 | |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6272 | static struct nfs4_lockowner * |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 6273 | find_lockowner_str(struct nfs4_client *clp, struct xdr_netobj *owner) |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6274 | { |
| 6275 | struct nfs4_lockowner *lo; |
| 6276 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6277 | spin_lock(&clp->cl_lock); |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 6278 | lo = find_lockowner_str_locked(clp, owner); |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6279 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6280 | return lo; |
| 6281 | } |
| 6282 | |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 6283 | static void nfs4_unhash_lockowner(struct nfs4_stateowner *sop) |
| 6284 | { |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6285 | unhash_lockowner_locked(lockowner(sop)); |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 6286 | } |
| 6287 | |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 6288 | static void nfs4_free_lockowner(struct nfs4_stateowner *sop) |
| 6289 | { |
| 6290 | struct nfs4_lockowner *lo = lockowner(sop); |
| 6291 | |
| 6292 | kmem_cache_free(lockowner_slab, lo); |
| 6293 | } |
| 6294 | |
| 6295 | static const struct nfs4_stateowner_operations lockowner_ops = { |
Jeff Layton | 8f4b54c | 2014-07-29 21:34:29 -0400 | [diff] [blame] | 6296 | .so_unhash = nfs4_unhash_lockowner, |
| 6297 | .so_free = nfs4_free_lockowner, |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 6298 | }; |
| 6299 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6300 | /* |
| 6301 | * Alloc a lock owner structure. |
| 6302 | * 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] | 6303 | * occurred. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6304 | * |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 6305 | * strhashval = ownerstr_hashval |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6306 | */ |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6307 | static struct nfs4_lockowner * |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6308 | alloc_init_lock_stateowner(unsigned int strhashval, struct nfs4_client *clp, |
| 6309 | struct nfs4_ol_stateid *open_stp, |
| 6310 | struct nfsd4_lock *lock) |
| 6311 | { |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6312 | struct nfs4_lockowner *lo, *ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6313 | |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6314 | lo = alloc_stateowner(lockowner_slab, &lock->lk_new_owner, clp); |
| 6315 | if (!lo) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6316 | return NULL; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6317 | INIT_LIST_HEAD(&lo->lo_blocked); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6318 | INIT_LIST_HEAD(&lo->lo_owner.so_stateids); |
| 6319 | lo->lo_owner.so_is_open_owner = 0; |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6320 | lo->lo_owner.so_seqid = lock->lk_new_lock_seqid; |
Jeff Layton | 6b180f0 | 2014-07-29 21:34:26 -0400 | [diff] [blame] | 6321 | lo->lo_owner.so_ops = &lockowner_ops; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6322 | spin_lock(&clp->cl_lock); |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 6323 | ret = find_lockowner_str_locked(clp, &lock->lk_new_owner); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6324 | if (ret == NULL) { |
| 6325 | list_add(&lo->lo_owner.so_strhash, |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6326 | &clp->cl_ownerstr_hashtbl[strhashval]); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6327 | ret = lo; |
| 6328 | } else |
Kinglong Mee | d50ffde | 2015-07-16 12:05:07 +0800 | [diff] [blame] | 6329 | nfs4_free_stateowner(&lo->lo_owner); |
| 6330 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6331 | spin_unlock(&clp->cl_lock); |
J. Bruce Fields | 340f0ba | 2015-03-23 11:02:30 -0400 | [diff] [blame] | 6332 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6333 | } |
| 6334 | |
Trond Myklebust | fd1fd68 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6335 | static struct nfs4_ol_stateid * |
| 6336 | find_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fp) |
| 6337 | { |
| 6338 | struct nfs4_ol_stateid *lst; |
| 6339 | struct nfs4_client *clp = lo->lo_owner.so_client; |
| 6340 | |
| 6341 | lockdep_assert_held(&clp->cl_lock); |
| 6342 | |
| 6343 | list_for_each_entry(lst, &lo->lo_owner.so_stateids, st_perstateowner) { |
| 6344 | if (lst->st_stid.sc_type != NFS4_LOCK_STID) |
| 6345 | continue; |
| 6346 | if (lst->st_stid.sc_file == fp) { |
| 6347 | refcount_inc(&lst->st_stid.sc_count); |
| 6348 | return lst; |
| 6349 | } |
| 6350 | } |
| 6351 | return NULL; |
| 6352 | } |
| 6353 | |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6354 | static struct nfs4_ol_stateid * |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 6355 | init_lock_stateid(struct nfs4_ol_stateid *stp, struct nfs4_lockowner *lo, |
| 6356 | struct nfs4_file *fp, struct inode *inode, |
| 6357 | struct nfs4_ol_stateid *open_stp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6358 | { |
J. Bruce Fields | d3b313a | 2011-09-15 15:02:41 -0400 | [diff] [blame] | 6359 | struct nfs4_client *clp = lo->lo_owner.so_client; |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6360 | struct nfs4_ol_stateid *retstp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6361 | |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6362 | mutex_init(&stp->st_mutex); |
Andrew Elble | 4f34bd0 | 2017-11-08 17:29:51 -0500 | [diff] [blame] | 6363 | mutex_lock_nested(&stp->st_mutex, OPEN_STATEID_MUTEX); |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6364 | retry: |
| 6365 | spin_lock(&clp->cl_lock); |
| 6366 | spin_lock(&fp->fi_lock); |
| 6367 | retstp = find_lock_stateid(lo, fp); |
| 6368 | if (retstp) |
| 6369 | goto out_unlock; |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 6370 | |
Elena Reshetova | a15dfcd | 2017-10-20 12:53:28 +0300 | [diff] [blame] | 6371 | refcount_inc(&stp->st_stid.sc_count); |
J. Bruce Fields | 3abdb60 | 2013-02-03 12:23:01 -0500 | [diff] [blame] | 6372 | stp->st_stid.sc_type = NFS4_LOCK_STID; |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 6373 | stp->st_stateowner = nfs4_get_stateowner(&lo->lo_owner); |
NeilBrown | 13cd218 | 2005-06-23 22:03:10 -0700 | [diff] [blame] | 6374 | get_nfs4_file(fp); |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 6375 | stp->st_stid.sc_file = fp; |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 6376 | stp->st_access_bmap = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6377 | stp->st_deny_bmap = open_stp->st_deny_bmap; |
NeilBrown | 4c4cd22 | 2005-07-07 17:59:27 -0700 | [diff] [blame] | 6378 | stp->st_openstp = open_stp; |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 6379 | list_add(&stp->st_locks, &open_stp->st_locks); |
Jeff Layton | 1c755dc | 2014-07-29 21:34:12 -0400 | [diff] [blame] | 6380 | list_add(&stp->st_perstateowner, &lo->lo_owner.so_stateids); |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 6381 | list_add(&stp->st_perfile, &fp->fi_stateids); |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6382 | out_unlock: |
Trond Myklebust | 1d31a25 | 2014-07-10 14:07:25 -0400 | [diff] [blame] | 6383 | spin_unlock(&fp->fi_lock); |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6384 | spin_unlock(&clp->cl_lock); |
| 6385 | if (retstp) { |
| 6386 | if (nfsd4_lock_ol_stateid(retstp) != nfs_ok) { |
| 6387 | nfs4_put_stid(&retstp->st_stid); |
| 6388 | goto retry; |
| 6389 | } |
| 6390 | /* To keep mutex tracking happy */ |
| 6391 | mutex_unlock(&stp->st_mutex); |
| 6392 | stp = retstp; |
| 6393 | } |
| 6394 | return stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6395 | } |
| 6396 | |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 6397 | static struct nfs4_ol_stateid * |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 6398 | find_or_create_lock_stateid(struct nfs4_lockowner *lo, struct nfs4_file *fi, |
| 6399 | struct inode *inode, struct nfs4_ol_stateid *ost, |
| 6400 | bool *new) |
| 6401 | { |
| 6402 | struct nfs4_stid *ns = NULL; |
| 6403 | struct nfs4_ol_stateid *lst; |
| 6404 | struct nfs4_openowner *oo = openowner(ost->st_stateowner); |
| 6405 | struct nfs4_client *clp = oo->oo_owner.so_client; |
| 6406 | |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6407 | *new = false; |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 6408 | spin_lock(&clp->cl_lock); |
| 6409 | lst = find_lock_stateid(lo, fi); |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 6410 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6411 | if (lst != NULL) { |
| 6412 | if (nfsd4_lock_ol_stateid(lst) == nfs_ok) |
| 6413 | goto out; |
| 6414 | nfs4_put_stid(&lst->st_stid); |
| 6415 | } |
| 6416 | ns = nfs4_alloc_stid(clp, stateid_slab, nfs4_free_lock_stateid); |
| 6417 | if (ns == NULL) |
| 6418 | return NULL; |
| 6419 | |
| 6420 | lst = init_lock_stateid(openlockstateid(ns), lo, fi, inode, ost); |
| 6421 | if (lst == openlockstateid(ns)) |
| 6422 | *new = true; |
| 6423 | else |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 6424 | nfs4_put_stid(ns); |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6425 | out: |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 6426 | return lst; |
| 6427 | } |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 6428 | |
NeilBrown | fd39ca9 | 2005-06-23 22:04:03 -0700 | [diff] [blame] | 6429 | static int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6430 | check_lock_length(u64 offset, u64 length) |
| 6431 | { |
Kinglong Mee | e796931 | 2015-07-13 17:34:19 +0800 | [diff] [blame] | 6432 | return ((length == 0) || ((length != NFS4_MAX_UINT64) && |
| 6433 | (length > ~offset))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6434 | } |
| 6435 | |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 6436 | 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] | 6437 | { |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 6438 | struct nfs4_file *fp = lock_stp->st_stid.sc_file; |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 6439 | |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 6440 | lockdep_assert_held(&fp->fi_lock); |
| 6441 | |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 6442 | if (test_access(access, lock_stp)) |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 6443 | return; |
Jeff Layton | 1265965 | 2014-07-10 14:07:28 -0400 | [diff] [blame] | 6444 | __nfs4_file_get_access(fp, access); |
Jeff Layton | 82c5ff1 | 2012-05-11 09:45:13 -0400 | [diff] [blame] | 6445 | set_access(access, lock_stp); |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 6446 | } |
| 6447 | |
Jeff Layton | 356a95e | 2014-07-29 21:34:13 -0400 | [diff] [blame] | 6448 | static __be32 |
| 6449 | lookup_or_create_lock_state(struct nfsd4_compound_state *cstate, |
| 6450 | struct nfs4_ol_stateid *ost, |
| 6451 | struct nfsd4_lock *lock, |
Jeff Layton | dd25793 | 2016-08-11 10:37:39 -0400 | [diff] [blame] | 6452 | struct nfs4_ol_stateid **plst, bool *new) |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6453 | { |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6454 | __be32 status; |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 6455 | struct nfs4_file *fi = ost->st_stid.sc_file; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6456 | struct nfs4_openowner *oo = openowner(ost->st_stateowner); |
| 6457 | struct nfs4_client *cl = oo->oo_owner.so_client; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 6458 | struct inode *inode = d_inode(cstate->current_fh.fh_dentry); |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6459 | struct nfs4_lockowner *lo; |
Jeff Layton | dd25793 | 2016-08-11 10:37:39 -0400 | [diff] [blame] | 6460 | struct nfs4_ol_stateid *lst; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6461 | unsigned int strhashval; |
| 6462 | |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 6463 | lo = find_lockowner_str(cl, &lock->lk_new_owner); |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 6464 | if (!lo) { |
Kinglong Mee | 76f6c9e | 2015-07-13 17:35:10 +0800 | [diff] [blame] | 6465 | strhashval = ownerstr_hashval(&lock->lk_new_owner); |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 6466 | lo = alloc_init_lock_stateowner(strhashval, cl, ost, lock); |
| 6467 | if (lo == NULL) |
| 6468 | return nfserr_jukebox; |
| 6469 | } else { |
| 6470 | /* with an existing lockowner, seqids must be the same */ |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6471 | status = nfserr_bad_seqid; |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 6472 | if (!cstate->minorversion && |
| 6473 | lock->lk_new_lock_seqid != lo->lo_owner.so_seqid) |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6474 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6475 | } |
Jeff Layton | c53530d | 2014-06-30 11:48:39 -0400 | [diff] [blame] | 6476 | |
Jeff Layton | dd25793 | 2016-08-11 10:37:39 -0400 | [diff] [blame] | 6477 | lst = find_or_create_lock_stateid(lo, fi, inode, ost, new); |
| 6478 | if (lst == NULL) { |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6479 | status = nfserr_jukebox; |
| 6480 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6481 | } |
Jeff Layton | dd25793 | 2016-08-11 10:37:39 -0400 | [diff] [blame] | 6482 | |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6483 | status = nfs_ok; |
Jeff Layton | dd25793 | 2016-08-11 10:37:39 -0400 | [diff] [blame] | 6484 | *plst = lst; |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6485 | out: |
| 6486 | nfs4_put_stateowner(&lo->lo_owner); |
| 6487 | return status; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6488 | } |
| 6489 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6490 | /* |
| 6491 | * LOCK operation |
| 6492 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6493 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6494 | nfsd4_lock(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6495 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6496 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6497 | struct nfsd4_lock *lock = &u->lock; |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6498 | struct nfs4_openowner *open_sop = NULL; |
| 6499 | struct nfs4_lockowner *lock_sop = NULL; |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 6500 | struct nfs4_ol_stateid *lock_stp = NULL; |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 6501 | struct nfs4_ol_stateid *open_stp = NULL; |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 6502 | struct nfs4_file *fp; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6503 | struct nfsd_file *nf = NULL; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6504 | struct nfsd4_blocked_lock *nbl = NULL; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6505 | struct file_lock *file_lock = NULL; |
| 6506 | struct file_lock *conflock = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6507 | __be32 status = 0; |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 6508 | int lkflg; |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 6509 | int err; |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6510 | bool new = false; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6511 | unsigned char fl_type; |
| 6512 | unsigned int fl_flags = FL_POSIX; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6513 | struct net *net = SVC_NET(rqstp); |
| 6514 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6515 | |
| 6516 | dprintk("NFSD: nfsd4_lock: start=%Ld length=%Ld\n", |
| 6517 | (long long) lock->lk_offset, |
| 6518 | (long long) lock->lk_length); |
| 6519 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6520 | if (check_lock_length(lock->lk_offset, lock->lk_length)) |
| 6521 | return nfserr_inval; |
| 6522 | |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6523 | if ((status = fh_verify(rqstp, &cstate->current_fh, |
Miklos Szeredi | 8837abc | 2008-06-16 13:20:29 +0200 | [diff] [blame] | 6524 | S_IFREG, NFSD_MAY_LOCK))) { |
Andy Adamson | a6f6ef2 | 2006-01-18 17:43:17 -0800 | [diff] [blame] | 6525 | dprintk("NFSD: nfsd4_lock: permission denied!\n"); |
| 6526 | return status; |
| 6527 | } |
| 6528 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6529 | if (lock->lk_is_new) { |
J. Bruce Fields | 684e563 | 2011-11-04 17:08:10 -0400 | [diff] [blame] | 6530 | if (nfsd4_has_session(cstate)) |
| 6531 | /* See rfc 5661 18.10.3: given clientid is ignored: */ |
Kinglong Mee | 76f6c9e | 2015-07-13 17:35:10 +0800 | [diff] [blame] | 6532 | memcpy(&lock->lk_new_clientid, |
J. Bruce Fields | 684e563 | 2011-11-04 17:08:10 -0400 | [diff] [blame] | 6533 | &cstate->session->se_client->cl_clientid, |
| 6534 | sizeof(clientid_t)); |
| 6535 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6536 | status = nfserr_stale_clientid; |
Stanislav Kinsbursky | 2c142ba | 2012-07-25 16:57:45 +0400 | [diff] [blame] | 6537 | if (STALE_CLIENTID(&lock->lk_new_clientid, nn)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6538 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6539 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6540 | /* validate and update open stateid and open seqid */ |
J. Bruce Fields | c0a5d93 | 2011-09-06 15:19:46 -0400 | [diff] [blame] | 6541 | status = nfs4_preprocess_confirmed_seqid_op(cstate, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6542 | lock->lk_new_open_seqid, |
| 6543 | &lock->lk_new_open_stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6544 | &open_stp, nn); |
NeilBrown | 3751517 | 2005-07-07 17:59:16 -0700 | [diff] [blame] | 6545 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6546 | goto out; |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 6547 | mutex_unlock(&open_stp->st_mutex); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6548 | open_sop = openowner(open_stp->st_stateowner); |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 6549 | status = nfserr_bad_stateid; |
J. Bruce Fields | 684e563 | 2011-11-04 17:08:10 -0400 | [diff] [blame] | 6550 | if (!same_clid(&open_sop->oo_owner.so_client->cl_clientid, |
Kinglong Mee | 76f6c9e | 2015-07-13 17:35:10 +0800 | [diff] [blame] | 6551 | &lock->lk_new_clientid)) |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 6552 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6553 | status = lookup_or_create_lock_state(cstate, open_stp, lock, |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6554 | &lock_stp, &new); |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 6555 | } else { |
Benny Halevy | dd453df | 2009-04-03 08:28:41 +0300 | [diff] [blame] | 6556 | status = nfs4_preprocess_seqid_op(cstate, |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6557 | lock->lk_old_lock_seqid, |
| 6558 | &lock->lk_old_lock_stateid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6559 | NFS4_LOCK_STID, &lock_stp, nn); |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 6560 | } |
J. Bruce Fields | e1aaa89 | 2012-06-06 16:01:37 -0400 | [diff] [blame] | 6561 | if (status) |
| 6562 | goto out; |
J. Bruce Fields | 64a284d | 2011-10-20 06:57:46 -0400 | [diff] [blame] | 6563 | lock_sop = lockowner(lock_stp->st_stateowner); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6564 | |
J. Bruce Fields | b34f27a | 2011-08-22 13:13:31 -0400 | [diff] [blame] | 6565 | lkflg = setlkflg(lock->lk_type); |
| 6566 | status = nfs4_check_openmode(lock_stp, lkflg); |
| 6567 | if (status) |
| 6568 | goto out; |
| 6569 | |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 6570 | status = nfserr_grace; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6571 | if (locks_in_grace(net) && !lock->lk_reclaim) |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 6572 | goto out; |
| 6573 | status = nfserr_no_grace; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6574 | if (!locks_in_grace(net) && lock->lk_reclaim) |
NeilBrown | 0dd395d | 2005-07-07 17:59:15 -0700 | [diff] [blame] | 6575 | goto out; |
| 6576 | |
Trond Myklebust | 11b9164 | 2014-07-29 21:34:08 -0400 | [diff] [blame] | 6577 | fp = lock_stp->st_stid.sc_file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6578 | switch (lock->lk_type) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6579 | case NFS4_READW_LT: |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6580 | if (nfsd4_has_session(cstate)) |
| 6581 | fl_flags |= FL_SLEEP; |
| 6582 | /* Fallthrough */ |
| 6583 | case NFS4_READ_LT: |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 6584 | spin_lock(&fp->fi_lock); |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6585 | nf = find_readable_file_locked(fp); |
| 6586 | if (nf) |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 6587 | get_lock_access(lock_stp, NFS4_SHARE_ACCESS_READ); |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 6588 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6589 | fl_type = F_RDLCK; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 6590 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6591 | case NFS4_WRITEW_LT: |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6592 | if (nfsd4_has_session(cstate)) |
| 6593 | fl_flags |= FL_SLEEP; |
| 6594 | /* Fallthrough */ |
| 6595 | case NFS4_WRITE_LT: |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 6596 | spin_lock(&fp->fi_lock); |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6597 | nf = find_writeable_file_locked(fp); |
| 6598 | if (nf) |
J. Bruce Fields | 0997b17 | 2011-03-02 18:01:35 -0500 | [diff] [blame] | 6599 | get_lock_access(lock_stp, NFS4_SHARE_ACCESS_WRITE); |
Jeff Layton | 7214e86 | 2014-07-10 14:07:33 -0400 | [diff] [blame] | 6600 | spin_unlock(&fp->fi_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6601 | fl_type = F_WRLCK; |
J. Bruce Fields | 529d7b2 | 2011-03-02 23:48:33 -0500 | [diff] [blame] | 6602 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6603 | default: |
| 6604 | status = nfserr_inval; |
| 6605 | goto out; |
| 6606 | } |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6607 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6608 | if (!nf) { |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 6609 | status = nfserr_openmode; |
| 6610 | goto out; |
| 6611 | } |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6612 | |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6613 | nbl = find_or_allocate_block(lock_sop, &fp->fi_fhandle, nn); |
| 6614 | if (!nbl) { |
| 6615 | dprintk("NFSD: %s: unable to allocate block!\n", __func__); |
| 6616 | status = nfserr_jukebox; |
| 6617 | goto out; |
| 6618 | } |
| 6619 | |
| 6620 | file_lock = &nbl->nbl_lock; |
| 6621 | file_lock->fl_type = fl_type; |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6622 | 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] | 6623 | file_lock->fl_pid = current->tgid; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6624 | file_lock->fl_file = nf->nf_file; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6625 | file_lock->fl_flags = fl_flags; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6626 | file_lock->fl_lmops = &nfsd_posix_mng_ops; |
| 6627 | file_lock->fl_start = lock->lk_offset; |
| 6628 | file_lock->fl_end = last_byte_offset(lock->lk_offset, lock->lk_length); |
| 6629 | nfs4_transform_lock_offset(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6630 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6631 | conflock = locks_alloc_lock(); |
| 6632 | if (!conflock) { |
| 6633 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 6634 | status = nfserr_jukebox; |
| 6635 | goto out; |
| 6636 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6637 | |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6638 | if (fl_flags & FL_SLEEP) { |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 6639 | nbl->nbl_time = jiffies; |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 6640 | spin_lock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6641 | list_add_tail(&nbl->nbl_list, &lock_sop->lo_blocked); |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 6642 | list_add_tail(&nbl->nbl_lru, &nn->blocked_locks_lru); |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 6643 | spin_unlock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6644 | } |
| 6645 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6646 | err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, conflock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6647 | switch (err) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6648 | case 0: /* success! */ |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 6649 | 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] | 6650 | status = 0; |
J. Bruce Fields | 03f318c | 2018-06-08 12:28:47 -0400 | [diff] [blame] | 6651 | if (lock->lk_reclaim) |
| 6652 | nn->somebody_reclaimed = true; |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 6653 | break; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6654 | case FILE_LOCK_DEFERRED: |
| 6655 | nbl = NULL; |
| 6656 | /* Fallthrough */ |
| 6657 | case -EAGAIN: /* conflock holds conflicting lock */ |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 6658 | status = nfserr_denied; |
| 6659 | dprintk("NFSD: nfsd4_lock: conflicting lock found!\n"); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6660 | nfs4_set_lock_denied(conflock, &lock->lk_denied); |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 6661 | break; |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6662 | case -EDEADLK: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6663 | status = nfserr_deadlock; |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 6664 | break; |
J. Bruce Fields | 3e77246 | 2011-08-10 19:07:33 -0400 | [diff] [blame] | 6665 | default: |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 6666 | 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] | 6667 | status = nfserrno(err); |
Andy Adamson | eb76b3f | 2006-03-26 01:37:26 -0800 | [diff] [blame] | 6668 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6669 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6670 | out: |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6671 | if (nbl) { |
| 6672 | /* dequeue it if we queued it before */ |
| 6673 | if (fl_flags & FL_SLEEP) { |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 6674 | spin_lock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6675 | list_del_init(&nbl->nbl_list); |
Jeff Layton | 7919d0a | 2016-09-16 16:28:25 -0400 | [diff] [blame] | 6676 | list_del_init(&nbl->nbl_lru); |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 6677 | spin_unlock(&nn->blocked_locks_lock); |
Jeff Layton | 76d348f | 2016-09-16 16:28:24 -0400 | [diff] [blame] | 6678 | } |
| 6679 | free_blocked_lock(nbl); |
| 6680 | } |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6681 | if (nf) |
| 6682 | nfsd_file_put(nf); |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6683 | if (lock_stp) { |
| 6684 | /* Bump seqid manually if the 4.0 replay owner is openowner */ |
| 6685 | if (cstate->replay_owner && |
| 6686 | cstate->replay_owner != &lock_sop->lo_owner && |
| 6687 | seqid_mutating_err(ntohl(status))) |
| 6688 | lock_sop->lo_owner.so_seqid++; |
| 6689 | |
| 6690 | /* |
| 6691 | * If this is a new, never-before-used stateid, and we are |
| 6692 | * returning an error, then just go ahead and release it. |
| 6693 | */ |
J. Bruce Fields | 2502072 | 2018-01-17 16:25:59 -0500 | [diff] [blame] | 6694 | if (status && new) |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6695 | release_lock_stateid(lock_stp); |
Trond Myklebust | beeca19 | 2017-11-03 08:00:14 -0400 | [diff] [blame] | 6696 | |
| 6697 | mutex_unlock(&lock_stp->st_mutex); |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6698 | |
Trond Myklebust | 3d0fabd | 2014-07-29 21:34:15 -0400 | [diff] [blame] | 6699 | nfs4_put_stid(&lock_stp->st_stid); |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6700 | } |
Trond Myklebust | 0667b1e | 2014-07-29 21:34:22 -0400 | [diff] [blame] | 6701 | if (open_stp) |
| 6702 | nfs4_put_stid(&open_stp->st_stid); |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 6703 | nfsd4_bump_seqid(cstate, status); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6704 | if (conflock) |
| 6705 | locks_free_lock(conflock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6706 | return status; |
| 6707 | } |
| 6708 | |
| 6709 | /* |
J. Bruce Fields | 55ef127 | 2008-12-20 11:58:38 -0800 | [diff] [blame] | 6710 | * The NFSv4 spec allows a client to do a LOCKT without holding an OPEN, |
| 6711 | * so we do a temporary open here just to get an open file to pass to |
| 6712 | * vfs_test_lock. (Arguably perhaps test_lock should be done with an |
| 6713 | * inode operation.) |
| 6714 | */ |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 6715 | 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] | 6716 | { |
Jeff Layton | 6b556ca | 2019-08-18 14:18:55 -0400 | [diff] [blame] | 6717 | struct nfsd_file *nf; |
| 6718 | __be32 err = nfsd_file_acquire(rqstp, fhp, NFSD_MAY_READ, &nf); |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 6719 | if (!err) { |
Jeff Layton | 6b556ca | 2019-08-18 14:18:55 -0400 | [diff] [blame] | 6720 | err = nfserrno(vfs_test_lock(nf->nf_file, lock)); |
| 6721 | nfsd_file_put(nf); |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 6722 | } |
J. Bruce Fields | 55ef127 | 2008-12-20 11:58:38 -0800 | [diff] [blame] | 6723 | return err; |
| 6724 | } |
| 6725 | |
| 6726 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6727 | * LOCKT operation |
| 6728 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6729 | __be32 |
J.Bruce Fields | ca36431 | 2006-12-13 00:35:27 -0800 | [diff] [blame] | 6730 | nfsd4_lockt(struct svc_rqst *rqstp, struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6731 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6732 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6733 | struct nfsd4_lockt *lockt = &u->lockt; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6734 | struct file_lock *file_lock = NULL; |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6735 | struct nfs4_lockowner *lo = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6736 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 6737 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6738 | |
Stanislav Kinsbursky | 5ccb006 | 2012-07-25 16:57:22 +0400 | [diff] [blame] | 6739 | if (locks_in_grace(SVC_NET(rqstp))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6740 | return nfserr_grace; |
| 6741 | |
| 6742 | if (check_lock_length(lockt->lt_offset, lockt->lt_length)) |
| 6743 | return nfserr_inval; |
| 6744 | |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 6745 | if (!nfsd4_has_session(cstate)) { |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 6746 | status = lookup_clientid(&lockt->lt_clientid, cstate, nn); |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 6747 | if (status) |
| 6748 | goto out; |
| 6749 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6750 | |
J. Bruce Fields | 75c096f | 2011-08-15 18:39:32 -0400 | [diff] [blame] | 6751 | if ((status = fh_verify(rqstp, &cstate->current_fh, S_IFREG, 0))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6752 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6753 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6754 | file_lock = locks_alloc_lock(); |
| 6755 | if (!file_lock) { |
| 6756 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 6757 | status = nfserr_jukebox; |
| 6758 | goto out; |
| 6759 | } |
Kinglong Mee | 6cd9066 | 2014-08-15 08:02:55 +0800 | [diff] [blame] | 6760 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6761 | switch (lockt->lt_type) { |
| 6762 | case NFS4_READ_LT: |
| 6763 | case NFS4_READW_LT: |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6764 | file_lock->fl_type = F_RDLCK; |
Colin Ian King | f50c9d7 | 2018-11-03 16:12:53 +0000 | [diff] [blame] | 6765 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6766 | case NFS4_WRITE_LT: |
| 6767 | case NFS4_WRITEW_LT: |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6768 | file_lock->fl_type = F_WRLCK; |
Colin Ian King | f50c9d7 | 2018-11-03 16:12:53 +0000 | [diff] [blame] | 6769 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6770 | default: |
J. Bruce Fields | 2fdada0 | 2007-07-27 16:10:37 -0400 | [diff] [blame] | 6771 | dprintk("NFSD: nfs4_lockt: bad lock type!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6772 | status = nfserr_inval; |
Colin Ian King | f50c9d7 | 2018-11-03 16:12:53 +0000 | [diff] [blame] | 6773 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6774 | } |
| 6775 | |
Kinglong Mee | c862399 | 2015-07-13 17:35:37 +0800 | [diff] [blame] | 6776 | lo = find_lockowner_str(cstate->clp, &lockt->lt_owner); |
J. Bruce Fields | fe0750e | 2011-07-30 23:33:59 -0400 | [diff] [blame] | 6777 | if (lo) |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6778 | file_lock->fl_owner = (fl_owner_t)lo; |
| 6779 | file_lock->fl_pid = current->tgid; |
| 6780 | file_lock->fl_flags = FL_POSIX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6781 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6782 | file_lock->fl_start = lockt->lt_offset; |
| 6783 | 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] | 6784 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6785 | nfs4_transform_lock_offset(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6786 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6787 | status = nfsd_test_lock(rqstp, &cstate->current_fh, file_lock); |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 6788 | if (status) |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 6789 | goto out; |
Al Viro | 04da6e9 | 2012-04-13 00:00:04 -0400 | [diff] [blame] | 6790 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6791 | if (file_lock->fl_type != F_UNLCK) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6792 | status = nfserr_denied; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6793 | nfs4_set_lock_denied(file_lock, &lockt->lt_denied); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6794 | } |
| 6795 | out: |
Jeff Layton | 5db1c03 | 2014-07-29 21:34:28 -0400 | [diff] [blame] | 6796 | if (lo) |
| 6797 | nfs4_put_stateowner(&lo->lo_owner); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6798 | if (file_lock) |
| 6799 | locks_free_lock(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6800 | return status; |
| 6801 | } |
| 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_locku(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_locku *locku = &u->locku; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 6808 | struct nfs4_ol_stateid *stp; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6809 | struct nfsd_file *nf = NULL; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6810 | struct file_lock *file_lock = NULL; |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6811 | __be32 status; |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 6812 | int err; |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6813 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
| 6814 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6815 | dprintk("NFSD: nfsd4_locku: start=%Ld length=%Ld\n", |
| 6816 | (long long) locku->lu_offset, |
| 6817 | (long long) locku->lu_length); |
| 6818 | |
| 6819 | if (check_lock_length(locku->lu_offset, locku->lu_length)) |
| 6820 | return nfserr_inval; |
| 6821 | |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 6822 | status = nfs4_preprocess_seqid_op(cstate, locku->lu_seqid, |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 6823 | &locku->lu_stateid, NFS4_LOCK_STID, |
| 6824 | &stp, nn); |
J. Bruce Fields | 9072d5c | 2011-08-24 12:45:03 -0400 | [diff] [blame] | 6825 | if (status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6826 | goto out; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6827 | nf = find_any_file(stp->st_stid.sc_file); |
| 6828 | if (!nf) { |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 6829 | status = nfserr_lock_range; |
Trond Myklebust | 858cc57 | 2014-07-29 21:34:16 -0400 | [diff] [blame] | 6830 | goto put_stateid; |
J. Bruce Fields | f9d7562 | 2010-07-08 11:02:09 -0400 | [diff] [blame] | 6831 | } |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6832 | file_lock = locks_alloc_lock(); |
| 6833 | if (!file_lock) { |
| 6834 | dprintk("NFSD: %s: unable to allocate lock!\n", __func__); |
| 6835 | status = nfserr_jukebox; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6836 | goto put_file; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6837 | } |
Kinglong Mee | 6cd9066 | 2014-08-15 08:02:55 +0800 | [diff] [blame] | 6838 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6839 | file_lock->fl_type = F_UNLCK; |
Kinglong Mee | aef9583 | 2014-08-22 10:18:44 -0400 | [diff] [blame] | 6840 | 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] | 6841 | file_lock->fl_pid = current->tgid; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6842 | file_lock->fl_file = nf->nf_file; |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6843 | file_lock->fl_flags = FL_POSIX; |
| 6844 | file_lock->fl_lmops = &nfsd_posix_mng_ops; |
| 6845 | file_lock->fl_start = locku->lu_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6846 | |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6847 | file_lock->fl_end = last_byte_offset(locku->lu_offset, |
| 6848 | locku->lu_length); |
| 6849 | nfs4_transform_lock_offset(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6850 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6851 | err = vfs_lock_file(nf->nf_file, F_SETLK, file_lock, NULL); |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 6852 | if (err) { |
Marc Eshel | fd85b81 | 2006-11-28 16:26:41 -0500 | [diff] [blame] | 6853 | dprintk("NFSD: nfs4_locku: vfs_lock_file failed!\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6854 | goto out_nfserr; |
| 6855 | } |
Jeff Layton | 9767feb | 2015-10-01 09:05:50 -0400 | [diff] [blame] | 6856 | nfs4_inc_and_copy_stateid(&locku->lu_stateid, &stp->st_stid); |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6857 | put_file: |
| 6858 | nfsd_file_put(nf); |
Trond Myklebust | 858cc57 | 2014-07-29 21:34:16 -0400 | [diff] [blame] | 6859 | put_stateid: |
Oleg Drokin | feb9dad | 2016-06-14 23:28:04 -0400 | [diff] [blame] | 6860 | mutex_unlock(&stp->st_mutex); |
Trond Myklebust | 858cc57 | 2014-07-29 21:34:16 -0400 | [diff] [blame] | 6861 | nfs4_put_stid(&stp->st_stid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6862 | out: |
J. Bruce Fields | 9411b1d | 2013-04-01 16:37:12 -0400 | [diff] [blame] | 6863 | nfsd4_bump_seqid(cstate, status); |
Jeff Layton | 21179d8 | 2012-08-21 08:03:32 -0400 | [diff] [blame] | 6864 | if (file_lock) |
| 6865 | locks_free_lock(file_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6866 | return status; |
| 6867 | |
| 6868 | out_nfserr: |
Al Viro | b8dd7b9 | 2006-10-19 23:29:01 -0700 | [diff] [blame] | 6869 | status = nfserrno(err); |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6870 | goto put_file; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6871 | } |
| 6872 | |
| 6873 | /* |
| 6874 | * returns |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 6875 | * true: locks held by lockowner |
| 6876 | * false: no locks held by lockowner |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6877 | */ |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 6878 | static bool |
| 6879 | check_for_locks(struct nfs4_file *fp, struct nfs4_lockowner *lowner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6880 | { |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 6881 | struct file_lock *fl; |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 6882 | int status = false; |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6883 | struct nfsd_file *nf = find_any_file(fp); |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 6884 | struct inode *inode; |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 6885 | struct file_lock_context *flctx; |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 6886 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6887 | if (!nf) { |
Jeff Layton | f9c00c3 | 2014-07-23 16:17:41 -0400 | [diff] [blame] | 6888 | /* Any valid lock stateid should have some sort of access */ |
| 6889 | WARN_ON_ONCE(1); |
| 6890 | return status; |
| 6891 | } |
| 6892 | |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6893 | inode = locks_inode(nf->nf_file); |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 6894 | flctx = inode->i_flctx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6895 | |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 6896 | if (flctx && !list_empty_careful(&flctx->flc_posix)) { |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 6897 | spin_lock(&flctx->flc_lock); |
Jeff Layton | bd61e0a | 2015-01-16 15:05:55 -0500 | [diff] [blame] | 6898 | list_for_each_entry(fl, &flctx->flc_posix, fl_list) { |
| 6899 | if (fl->fl_owner == (fl_owner_t)lowner) { |
| 6900 | status = true; |
| 6901 | break; |
| 6902 | } |
J. Bruce Fields | 796dadf | 2006-01-18 17:43:22 -0800 | [diff] [blame] | 6903 | } |
Jeff Layton | 6109c85 | 2015-01-16 15:05:57 -0500 | [diff] [blame] | 6904 | spin_unlock(&flctx->flc_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6905 | } |
Jeff Layton | eb82dd3 | 2019-08-18 14:18:53 -0400 | [diff] [blame] | 6906 | nfsd_file_put(nf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6907 | return status; |
| 6908 | } |
| 6909 | |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6910 | __be32 |
J.Bruce Fields | b591480 | 2006-12-13 00:35:38 -0800 | [diff] [blame] | 6911 | nfsd4_release_lockowner(struct svc_rqst *rqstp, |
| 6912 | struct nfsd4_compound_state *cstate, |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6913 | union nfsd4_op_u *u) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6914 | { |
Christoph Hellwig | eb69853 | 2017-05-08 20:58:35 +0200 | [diff] [blame] | 6915 | struct nfsd4_release_lockowner *rlockowner = &u->release_lockowner; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6916 | clientid_t *clid = &rlockowner->rl_clientid; |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 6917 | struct nfs4_stateowner *sop; |
| 6918 | struct nfs4_lockowner *lo = NULL; |
J. Bruce Fields | dcef041 | 2011-09-07 16:06:42 -0400 | [diff] [blame] | 6919 | struct nfs4_ol_stateid *stp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6920 | struct xdr_netobj *owner = &rlockowner->rl_owner; |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6921 | unsigned int hashval = ownerstr_hashval(owner); |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 6922 | __be32 status; |
Stanislav Kinsbursky | 7f2210f | 2012-11-14 18:21:05 +0300 | [diff] [blame] | 6923 | struct nfsd_net *nn = net_generic(SVC_NET(rqstp), nfsd_net_id); |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6924 | struct nfs4_client *clp; |
Chuck Lever | 8858481 | 2016-07-13 16:40:14 -0400 | [diff] [blame] | 6925 | LIST_HEAD (reaplist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6926 | |
| 6927 | dprintk("nfsd4_release_lockowner clientid: (%08x/%08x):\n", |
| 6928 | clid->cl_boot, clid->cl_id); |
| 6929 | |
Jeff Layton | 4b24ca7 | 2014-06-30 11:48:44 -0400 | [diff] [blame] | 6930 | status = lookup_clientid(clid, cstate, nn); |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 6931 | if (status) |
Trond Myklebust | 51f5e78 | 2014-07-30 08:27:27 -0400 | [diff] [blame] | 6932 | return status; |
J. Bruce Fields | 9b2ef62 | 2012-12-03 17:24:41 -0500 | [diff] [blame] | 6933 | |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6934 | clp = cstate->clp; |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 6935 | /* Find the matching lock stateowner */ |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6936 | spin_lock(&clp->cl_lock); |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 6937 | list_for_each_entry(sop, &clp->cl_ownerstr_hashtbl[hashval], |
Trond Myklebust | d4f0489 | 2014-07-29 21:34:36 -0400 | [diff] [blame] | 6938 | so_strhash) { |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 6939 | |
| 6940 | if (sop->so_is_open_owner || !same_owner_str(sop, owner)) |
J. Bruce Fields | 16bfdaaf | 2011-11-07 17:23:30 -0500 | [diff] [blame] | 6941 | continue; |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 6942 | |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 6943 | /* see if there are still any locks associated with it */ |
| 6944 | lo = lockowner(sop); |
| 6945 | list_for_each_entry(stp, &sop->so_stateids, st_perstateowner) { |
| 6946 | if (check_for_locks(stp->st_stid.sc_file, lo)) { |
| 6947 | status = nfserr_locks_held; |
| 6948 | spin_unlock(&clp->cl_lock); |
Trond Myklebust | 51f5e78 | 2014-07-30 08:27:27 -0400 | [diff] [blame] | 6949 | return status; |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 6950 | } |
Jeff Layton | 5adfd88 | 2014-07-29 21:34:31 -0400 | [diff] [blame] | 6951 | } |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 6952 | |
Kinglong Mee | b5971af | 2014-08-22 10:18:43 -0400 | [diff] [blame] | 6953 | nfs4_get_stateowner(sop); |
Jeff Layton | 882e9d2 | 2014-07-29 21:34:37 -0400 | [diff] [blame] | 6954 | break; |
Jeff Layton | fd44907 | 2014-06-30 11:48:41 -0400 | [diff] [blame] | 6955 | } |
Chuck Lever | 8858481 | 2016-07-13 16:40:14 -0400 | [diff] [blame] | 6956 | if (!lo) { |
| 6957 | spin_unlock(&clp->cl_lock); |
| 6958 | return status; |
| 6959 | } |
| 6960 | |
| 6961 | unhash_lockowner_locked(lo); |
| 6962 | while (!list_empty(&lo->lo_owner.so_stateids)) { |
| 6963 | stp = list_first_entry(&lo->lo_owner.so_stateids, |
| 6964 | struct nfs4_ol_stateid, |
| 6965 | st_perstateowner); |
| 6966 | WARN_ON(!unhash_lock_stateid(stp)); |
| 6967 | put_ol_stateid_locked(stp, &reaplist); |
| 6968 | } |
Trond Myklebust | c58c661 | 2014-07-29 21:34:35 -0400 | [diff] [blame] | 6969 | spin_unlock(&clp->cl_lock); |
Chuck Lever | 8858481 | 2016-07-13 16:40:14 -0400 | [diff] [blame] | 6970 | free_ol_stateid_reaplist(&reaplist); |
Jeff Layton | 68ef3bc | 2018-03-16 11:32:02 -0400 | [diff] [blame] | 6971 | remove_blocked_locks(lo); |
Chuck Lever | 8858481 | 2016-07-13 16:40:14 -0400 | [diff] [blame] | 6972 | nfs4_put_stateowner(&lo->lo_owner); |
| 6973 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6974 | return status; |
| 6975 | } |
| 6976 | |
| 6977 | static inline struct nfs4_client_reclaim * |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 6978 | alloc_reclaim(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6979 | { |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 6980 | return kmalloc(sizeof(struct nfs4_client_reclaim), GFP_KERNEL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6981 | } |
| 6982 | |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 6983 | bool |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 6984 | nfs4_has_reclaimed_state(struct xdr_netobj name, struct nfsd_net *nn) |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 6985 | { |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 6986 | struct nfs4_client_reclaim *crp; |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 6987 | |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 6988 | crp = nfsd4_find_reclaim_client(name, nn); |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 6989 | return (crp && crp->cr_clp); |
NeilBrown | c7b9a45 | 2005-06-23 22:04:30 -0700 | [diff] [blame] | 6990 | } |
| 6991 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6992 | /* |
| 6993 | * failure => all reset bets are off, nfserr_no_grace... |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 6994 | * |
| 6995 | * The caller is responsible for freeing name.data if NULL is returned (it |
| 6996 | * will be freed in nfs4_remove_reclaim_record in the normal case). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6997 | */ |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 6998 | struct nfs4_client_reclaim * |
Scott Mayhew | 6ee95d1 | 2019-09-09 16:10:31 -0400 | [diff] [blame] | 6999 | nfs4_client_to_reclaim(struct xdr_netobj name, struct xdr_netobj princhash, |
| 7000 | struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7001 | { |
| 7002 | unsigned int strhashval; |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 7003 | struct nfs4_client_reclaim *crp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7004 | |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 7005 | dprintk("NFSD nfs4_client_to_reclaim NAME: %.*s\n", name.len, name.data); |
NeilBrown | a55370a | 2005-06-23 22:03:52 -0700 | [diff] [blame] | 7006 | crp = alloc_reclaim(); |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 7007 | if (crp) { |
| 7008 | strhashval = clientstr_hashval(name); |
| 7009 | INIT_LIST_HEAD(&crp->cr_strhash); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7010 | list_add(&crp->cr_strhash, &nn->reclaim_str_hashtbl[strhashval]); |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 7011 | crp->cr_name.data = name.data; |
| 7012 | crp->cr_name.len = name.len; |
Scott Mayhew | 6ee95d1 | 2019-09-09 16:10:31 -0400 | [diff] [blame] | 7013 | crp->cr_princhash.data = princhash.data; |
| 7014 | crp->cr_princhash.len = princhash.len; |
Jeff Layton | 0ce0c2b | 2012-11-12 15:00:55 -0500 | [diff] [blame] | 7015 | crp->cr_clp = NULL; |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7016 | nn->reclaim_str_hashtbl_size++; |
Jeff Layton | 772a9bb | 2012-11-12 15:00:54 -0500 | [diff] [blame] | 7017 | } |
| 7018 | return crp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7019 | } |
| 7020 | |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 7021 | void |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7022 | 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] | 7023 | { |
| 7024 | list_del(&crp->cr_strhash); |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 7025 | kfree(crp->cr_name.data); |
Scott Mayhew | 6ee95d1 | 2019-09-09 16:10:31 -0400 | [diff] [blame] | 7026 | kfree(crp->cr_princhash.data); |
Jeff Layton | ce30e53 | 2012-11-12 15:00:53 -0500 | [diff] [blame] | 7027 | kfree(crp); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7028 | nn->reclaim_str_hashtbl_size--; |
Jeff Layton | ce30e53 | 2012-11-12 15:00:53 -0500 | [diff] [blame] | 7029 | } |
| 7030 | |
| 7031 | void |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7032 | nfs4_release_reclaim(struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7033 | { |
| 7034 | struct nfs4_client_reclaim *crp = NULL; |
| 7035 | int i; |
| 7036 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7037 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7038 | while (!list_empty(&nn->reclaim_str_hashtbl[i])) { |
| 7039 | crp = list_entry(nn->reclaim_str_hashtbl[i].next, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7040 | struct nfs4_client_reclaim, cr_strhash); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7041 | nfs4_remove_reclaim_record(crp, nn); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7042 | } |
| 7043 | } |
J. Bruce Fields | 063b0fb | 2012-11-25 14:48:10 -0500 | [diff] [blame] | 7044 | WARN_ON_ONCE(nn->reclaim_str_hashtbl_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7045 | } |
| 7046 | |
| 7047 | /* |
| 7048 | * called from OPEN, CLAIM_PREVIOUS with a new clientid. */ |
Jeff Layton | 2a4317c | 2012-03-21 16:42:43 -0400 | [diff] [blame] | 7049 | struct nfs4_client_reclaim * |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 7050 | nfsd4_find_reclaim_client(struct xdr_netobj name, struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7051 | { |
| 7052 | unsigned int strhashval; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7053 | struct nfs4_client_reclaim *crp = NULL; |
| 7054 | |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 7055 | 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] | 7056 | |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 7057 | strhashval = clientstr_hashval(name); |
Stanislav Kinsbursky | 52e19c0 | 2012-11-14 18:21:16 +0300 | [diff] [blame] | 7058 | list_for_each_entry(crp, &nn->reclaim_str_hashtbl[strhashval], cr_strhash) { |
Scott Mayhew | 6b18910 | 2019-03-26 18:06:26 -0400 | [diff] [blame] | 7059 | if (compare_blob(&crp->cr_name, &name) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7060 | return crp; |
| 7061 | } |
| 7062 | } |
| 7063 | return NULL; |
| 7064 | } |
| 7065 | |
| 7066 | /* |
| 7067 | * Called from OPEN. Look for clientid in reclaim list. |
| 7068 | */ |
Al Viro | b37ad28 | 2006-10-19 23:28:59 -0700 | [diff] [blame] | 7069 | __be32 |
Trond Myklebust | 0fe492d | 2014-06-30 11:48:47 -0400 | [diff] [blame] | 7070 | nfs4_check_open_reclaim(clientid_t *clid, |
| 7071 | struct nfsd4_compound_state *cstate, |
| 7072 | struct nfsd_net *nn) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7073 | { |
Trond Myklebust | 0fe492d | 2014-06-30 11:48:47 -0400 | [diff] [blame] | 7074 | __be32 status; |
Jeff Layton | a52d726 | 2012-03-21 09:52:02 -0400 | [diff] [blame] | 7075 | |
| 7076 | /* find clientid in conf_id_hashtbl */ |
Trond Myklebust | 0fe492d | 2014-06-30 11:48:47 -0400 | [diff] [blame] | 7077 | status = lookup_clientid(clid, cstate, nn); |
| 7078 | if (status) |
Jeff Layton | a52d726 | 2012-03-21 09:52:02 -0400 | [diff] [blame] | 7079 | return nfserr_reclaim_bad; |
| 7080 | |
Jeff Layton | 3b3e7b7 | 2014-09-12 16:40:22 -0400 | [diff] [blame] | 7081 | if (test_bit(NFSD4_CLIENT_RECLAIM_COMPLETE, &cstate->clp->cl_flags)) |
| 7082 | return nfserr_no_grace; |
| 7083 | |
Trond Myklebust | 0fe492d | 2014-06-30 11:48:47 -0400 | [diff] [blame] | 7084 | if (nfsd4_client_record_check(cstate->clp)) |
| 7085 | return nfserr_reclaim_bad; |
| 7086 | |
| 7087 | return nfs_ok; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7088 | } |
| 7089 | |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 7090 | #ifdef CONFIG_NFSD_FAULT_INJECTION |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7091 | static inline void |
| 7092 | put_client(struct nfs4_client *clp) |
| 7093 | { |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 7094 | atomic_dec(&clp->cl_rpc_users); |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7095 | } |
| 7096 | |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7097 | static struct nfs4_client * |
| 7098 | nfsd_find_client(struct sockaddr_storage *addr, size_t addr_size) |
| 7099 | { |
| 7100 | struct nfs4_client *clp; |
| 7101 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7102 | nfsd_net_id); |
| 7103 | |
| 7104 | if (!nfsd_netns_ready(nn)) |
| 7105 | return NULL; |
| 7106 | |
| 7107 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { |
| 7108 | if (memcmp(&clp->cl_addr, addr, addr_size) == 0) |
| 7109 | return clp; |
| 7110 | } |
| 7111 | return NULL; |
| 7112 | } |
| 7113 | |
Jeff Layton | 7ec0e36 | 2014-07-30 08:27:17 -0400 | [diff] [blame] | 7114 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7115 | nfsd_inject_print_clients(void) |
Jeff Layton | 7ec0e36 | 2014-07-30 08:27:17 -0400 | [diff] [blame] | 7116 | { |
| 7117 | struct nfs4_client *clp; |
| 7118 | u64 count = 0; |
| 7119 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7120 | nfsd_net_id); |
| 7121 | char buf[INET6_ADDRSTRLEN]; |
| 7122 | |
| 7123 | if (!nfsd_netns_ready(nn)) |
| 7124 | return 0; |
| 7125 | |
| 7126 | spin_lock(&nn->client_lock); |
| 7127 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { |
| 7128 | rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf)); |
| 7129 | pr_info("NFS Client: %s\n", buf); |
| 7130 | ++count; |
| 7131 | } |
| 7132 | spin_unlock(&nn->client_lock); |
| 7133 | |
| 7134 | return count; |
| 7135 | } |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 7136 | |
Jeff Layton | a0926d1 | 2014-07-30 08:27:18 -0400 | [diff] [blame] | 7137 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7138 | nfsd_inject_forget_client(struct sockaddr_storage *addr, size_t addr_size) |
Jeff Layton | a0926d1 | 2014-07-30 08:27:18 -0400 | [diff] [blame] | 7139 | { |
| 7140 | u64 count = 0; |
| 7141 | struct nfs4_client *clp; |
| 7142 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7143 | nfsd_net_id); |
| 7144 | |
| 7145 | if (!nfsd_netns_ready(nn)) |
| 7146 | return count; |
| 7147 | |
| 7148 | spin_lock(&nn->client_lock); |
| 7149 | clp = nfsd_find_client(addr, addr_size); |
| 7150 | if (clp) { |
| 7151 | if (mark_client_expired_locked(clp) == nfs_ok) |
| 7152 | ++count; |
| 7153 | else |
| 7154 | clp = NULL; |
| 7155 | } |
| 7156 | spin_unlock(&nn->client_lock); |
| 7157 | |
| 7158 | if (clp) |
| 7159 | expire_client(clp); |
| 7160 | |
| 7161 | return count; |
| 7162 | } |
| 7163 | |
Jeff Layton | 69fc9ed | 2014-07-30 08:27:19 -0400 | [diff] [blame] | 7164 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7165 | nfsd_inject_forget_clients(u64 max) |
Jeff Layton | 69fc9ed | 2014-07-30 08:27:19 -0400 | [diff] [blame] | 7166 | { |
| 7167 | u64 count = 0; |
| 7168 | struct nfs4_client *clp, *next; |
| 7169 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7170 | nfsd_net_id); |
| 7171 | LIST_HEAD(reaplist); |
| 7172 | |
| 7173 | if (!nfsd_netns_ready(nn)) |
| 7174 | return count; |
| 7175 | |
| 7176 | spin_lock(&nn->client_lock); |
| 7177 | list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) { |
| 7178 | if (mark_client_expired_locked(clp) == nfs_ok) { |
| 7179 | list_add(&clp->cl_lru, &reaplist); |
| 7180 | if (max != 0 && ++count >= max) |
| 7181 | break; |
| 7182 | } |
| 7183 | } |
| 7184 | spin_unlock(&nn->client_lock); |
| 7185 | |
| 7186 | list_for_each_entry_safe(clp, next, &reaplist, cl_lru) |
| 7187 | expire_client(clp); |
| 7188 | |
| 7189 | return count; |
| 7190 | } |
| 7191 | |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 7192 | static void nfsd_print_count(struct nfs4_client *clp, unsigned int count, |
| 7193 | const char *type) |
| 7194 | { |
| 7195 | char buf[INET6_ADDRSTRLEN]; |
Bryan Schumaker | 0a5c33e | 2012-12-07 16:17:28 -0500 | [diff] [blame] | 7196 | rpc_ntop((struct sockaddr *)&clp->cl_addr, buf, sizeof(buf)); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 7197 | printk(KERN_INFO "NFS Client: %s has %u %s\n", buf, count, type); |
| 7198 | } |
| 7199 | |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7200 | static void |
| 7201 | nfsd_inject_add_lock_to_list(struct nfs4_ol_stateid *lst, |
| 7202 | struct list_head *collect) |
| 7203 | { |
| 7204 | struct nfs4_client *clp = lst->st_stid.sc_client; |
| 7205 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7206 | nfsd_net_id); |
| 7207 | |
| 7208 | if (!collect) |
| 7209 | return; |
| 7210 | |
| 7211 | lockdep_assert_held(&nn->client_lock); |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 7212 | atomic_inc(&clp->cl_rpc_users); |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7213 | list_add(&lst->st_locks, collect); |
| 7214 | } |
| 7215 | |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 7216 | static u64 nfsd_foreach_client_lock(struct nfs4_client *clp, u64 max, |
Jeff Layton | 3738d50 | 2014-07-30 08:27:20 -0400 | [diff] [blame] | 7217 | struct list_head *collect, |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 7218 | bool (*func)(struct nfs4_ol_stateid *)) |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 7219 | { |
| 7220 | struct nfs4_openowner *oop; |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 7221 | struct nfs4_ol_stateid *stp, *st_next; |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 7222 | struct nfs4_ol_stateid *lst, *lst_next; |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 7223 | u64 count = 0; |
| 7224 | |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7225 | spin_lock(&clp->cl_lock); |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 7226 | list_for_each_entry(oop, &clp->cl_openowners, oo_perclient) { |
Trond Myklebust | 3c87b9b | 2014-06-30 11:48:38 -0400 | [diff] [blame] | 7227 | list_for_each_entry_safe(stp, st_next, |
| 7228 | &oop->oo_owner.so_stateids, st_perstateowner) { |
| 7229 | list_for_each_entry_safe(lst, lst_next, |
| 7230 | &stp->st_locks, st_locks) { |
Jeff Layton | 3738d50 | 2014-07-30 08:27:20 -0400 | [diff] [blame] | 7231 | if (func) { |
Jeff Layton | e856873 | 2015-08-24 12:41:47 -0400 | [diff] [blame] | 7232 | if (func(lst)) |
| 7233 | nfsd_inject_add_lock_to_list(lst, |
| 7234 | collect); |
Jeff Layton | 3738d50 | 2014-07-30 08:27:20 -0400 | [diff] [blame] | 7235 | } |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7236 | ++count; |
| 7237 | /* |
| 7238 | * Despite the fact that these functions deal |
| 7239 | * with 64-bit integers for "count", we must |
| 7240 | * ensure that it doesn't blow up the |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 7241 | * clp->cl_rpc_users. Throw a warning if we |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7242 | * start to approach INT_MAX here. |
| 7243 | */ |
| 7244 | WARN_ON_ONCE(count == (INT_MAX / 2)); |
| 7245 | if (count == max) |
| 7246 | goto out; |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 7247 | } |
| 7248 | } |
| 7249 | } |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7250 | out: |
| 7251 | spin_unlock(&clp->cl_lock); |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 7252 | |
| 7253 | return count; |
| 7254 | } |
| 7255 | |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7256 | static u64 |
| 7257 | nfsd_collect_client_locks(struct nfs4_client *clp, struct list_head *collect, |
| 7258 | u64 max) |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 7259 | { |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7260 | return nfsd_foreach_client_lock(clp, max, collect, unhash_lock_stateid); |
Bryan Schumaker | fc29171 | 2012-11-29 11:40:40 -0500 | [diff] [blame] | 7261 | } |
| 7262 | |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7263 | static u64 |
| 7264 | nfsd_print_client_locks(struct nfs4_client *clp) |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 7265 | { |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7266 | u64 count = nfsd_foreach_client_lock(clp, 0, NULL, NULL); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 7267 | nfsd_print_count(clp, count, "locked files"); |
| 7268 | return count; |
| 7269 | } |
| 7270 | |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7271 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7272 | nfsd_inject_print_locks(void) |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7273 | { |
| 7274 | struct nfs4_client *clp; |
| 7275 | u64 count = 0; |
| 7276 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7277 | nfsd_net_id); |
| 7278 | |
| 7279 | if (!nfsd_netns_ready(nn)) |
| 7280 | return 0; |
| 7281 | |
| 7282 | spin_lock(&nn->client_lock); |
| 7283 | list_for_each_entry(clp, &nn->client_lru, cl_lru) |
| 7284 | count += nfsd_print_client_locks(clp); |
| 7285 | spin_unlock(&nn->client_lock); |
| 7286 | |
| 7287 | return count; |
| 7288 | } |
| 7289 | |
| 7290 | static void |
| 7291 | nfsd_reap_locks(struct list_head *reaplist) |
| 7292 | { |
| 7293 | struct nfs4_client *clp; |
| 7294 | struct nfs4_ol_stateid *stp, *next; |
| 7295 | |
| 7296 | list_for_each_entry_safe(stp, next, reaplist, st_locks) { |
| 7297 | list_del_init(&stp->st_locks); |
| 7298 | clp = stp->st_stid.sc_client; |
| 7299 | nfs4_put_stid(&stp->st_stid); |
| 7300 | put_client(clp); |
| 7301 | } |
| 7302 | } |
| 7303 | |
| 7304 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7305 | 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] | 7306 | { |
| 7307 | unsigned int count = 0; |
| 7308 | struct nfs4_client *clp; |
| 7309 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7310 | nfsd_net_id); |
| 7311 | LIST_HEAD(reaplist); |
| 7312 | |
| 7313 | if (!nfsd_netns_ready(nn)) |
| 7314 | return count; |
| 7315 | |
| 7316 | spin_lock(&nn->client_lock); |
| 7317 | clp = nfsd_find_client(addr, addr_size); |
| 7318 | if (clp) |
| 7319 | count = nfsd_collect_client_locks(clp, &reaplist, 0); |
| 7320 | spin_unlock(&nn->client_lock); |
| 7321 | nfsd_reap_locks(&reaplist); |
| 7322 | return count; |
| 7323 | } |
| 7324 | |
| 7325 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7326 | nfsd_inject_forget_locks(u64 max) |
Jeff Layton | 016200c | 2014-07-30 08:27:21 -0400 | [diff] [blame] | 7327 | { |
| 7328 | u64 count = 0; |
| 7329 | struct nfs4_client *clp; |
| 7330 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7331 | nfsd_net_id); |
| 7332 | LIST_HEAD(reaplist); |
| 7333 | |
| 7334 | if (!nfsd_netns_ready(nn)) |
| 7335 | return count; |
| 7336 | |
| 7337 | spin_lock(&nn->client_lock); |
| 7338 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { |
| 7339 | count += nfsd_collect_client_locks(clp, &reaplist, max - count); |
| 7340 | if (max != 0 && count >= max) |
| 7341 | break; |
| 7342 | } |
| 7343 | spin_unlock(&nn->client_lock); |
| 7344 | nfsd_reap_locks(&reaplist); |
| 7345 | return count; |
| 7346 | } |
| 7347 | |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7348 | static u64 |
| 7349 | nfsd_foreach_client_openowner(struct nfs4_client *clp, u64 max, |
| 7350 | struct list_head *collect, |
| 7351 | void (*func)(struct nfs4_openowner *)) |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 7352 | { |
| 7353 | struct nfs4_openowner *oop, *next; |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7354 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7355 | nfsd_net_id); |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 7356 | u64 count = 0; |
| 7357 | |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7358 | lockdep_assert_held(&nn->client_lock); |
| 7359 | |
| 7360 | spin_lock(&clp->cl_lock); |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 7361 | list_for_each_entry_safe(oop, next, &clp->cl_openowners, oo_perclient) { |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7362 | if (func) { |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 7363 | func(oop); |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7364 | if (collect) { |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 7365 | atomic_inc(&clp->cl_rpc_users); |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7366 | list_add(&oop->oo_perclient, collect); |
| 7367 | } |
| 7368 | } |
| 7369 | ++count; |
| 7370 | /* |
| 7371 | * Despite the fact that these functions deal with |
| 7372 | * 64-bit integers for "count", we must ensure that |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 7373 | * it doesn't blow up the clp->cl_rpc_users. Throw a |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7374 | * warning if we start to approach INT_MAX here. |
| 7375 | */ |
| 7376 | WARN_ON_ONCE(count == (INT_MAX / 2)); |
| 7377 | if (count == max) |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 7378 | break; |
| 7379 | } |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7380 | spin_unlock(&clp->cl_lock); |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 7381 | |
| 7382 | return count; |
| 7383 | } |
| 7384 | |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7385 | static u64 |
| 7386 | nfsd_print_client_openowners(struct nfs4_client *clp) |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 7387 | { |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7388 | u64 count = nfsd_foreach_client_openowner(clp, 0, NULL, NULL); |
| 7389 | |
| 7390 | nfsd_print_count(clp, count, "openowners"); |
| 7391 | return count; |
Bryan Schumaker | 4dbdbda | 2012-11-29 11:40:41 -0500 | [diff] [blame] | 7392 | } |
| 7393 | |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7394 | static u64 |
| 7395 | nfsd_collect_client_openowners(struct nfs4_client *clp, |
| 7396 | struct list_head *collect, u64 max) |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 7397 | { |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7398 | return nfsd_foreach_client_openowner(clp, max, collect, |
| 7399 | unhash_openowner_locked); |
| 7400 | } |
| 7401 | |
| 7402 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7403 | nfsd_inject_print_openowners(void) |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7404 | { |
| 7405 | struct nfs4_client *clp; |
| 7406 | u64 count = 0; |
| 7407 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7408 | nfsd_net_id); |
| 7409 | |
| 7410 | if (!nfsd_netns_ready(nn)) |
| 7411 | return 0; |
| 7412 | |
| 7413 | spin_lock(&nn->client_lock); |
| 7414 | list_for_each_entry(clp, &nn->client_lru, cl_lru) |
| 7415 | count += nfsd_print_client_openowners(clp); |
| 7416 | spin_unlock(&nn->client_lock); |
| 7417 | |
| 7418 | return count; |
| 7419 | } |
| 7420 | |
| 7421 | static void |
| 7422 | nfsd_reap_openowners(struct list_head *reaplist) |
| 7423 | { |
| 7424 | struct nfs4_client *clp; |
| 7425 | struct nfs4_openowner *oop, *next; |
| 7426 | |
| 7427 | list_for_each_entry_safe(oop, next, reaplist, oo_perclient) { |
| 7428 | list_del_init(&oop->oo_perclient); |
| 7429 | clp = oop->oo_owner.so_client; |
| 7430 | release_openowner(oop); |
| 7431 | put_client(clp); |
| 7432 | } |
| 7433 | } |
| 7434 | |
| 7435 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7436 | nfsd_inject_forget_client_openowners(struct sockaddr_storage *addr, |
| 7437 | size_t addr_size) |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7438 | { |
| 7439 | unsigned int count = 0; |
| 7440 | struct nfs4_client *clp; |
| 7441 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7442 | nfsd_net_id); |
| 7443 | LIST_HEAD(reaplist); |
| 7444 | |
| 7445 | if (!nfsd_netns_ready(nn)) |
| 7446 | return count; |
| 7447 | |
| 7448 | spin_lock(&nn->client_lock); |
| 7449 | clp = nfsd_find_client(addr, addr_size); |
| 7450 | if (clp) |
| 7451 | count = nfsd_collect_client_openowners(clp, &reaplist, 0); |
| 7452 | spin_unlock(&nn->client_lock); |
| 7453 | nfsd_reap_openowners(&reaplist); |
| 7454 | return count; |
| 7455 | } |
| 7456 | |
| 7457 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7458 | nfsd_inject_forget_openowners(u64 max) |
Jeff Layton | 82e05ef | 2014-07-30 08:27:22 -0400 | [diff] [blame] | 7459 | { |
| 7460 | u64 count = 0; |
| 7461 | struct nfs4_client *clp; |
| 7462 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7463 | nfsd_net_id); |
| 7464 | LIST_HEAD(reaplist); |
| 7465 | |
| 7466 | if (!nfsd_netns_ready(nn)) |
| 7467 | return count; |
| 7468 | |
| 7469 | spin_lock(&nn->client_lock); |
| 7470 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { |
| 7471 | count += nfsd_collect_client_openowners(clp, &reaplist, |
| 7472 | max - count); |
| 7473 | if (max != 0 && count >= max) |
| 7474 | break; |
| 7475 | } |
| 7476 | spin_unlock(&nn->client_lock); |
| 7477 | nfsd_reap_openowners(&reaplist); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 7478 | return count; |
| 7479 | } |
| 7480 | |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 7481 | static u64 nfsd_find_all_delegations(struct nfs4_client *clp, u64 max, |
| 7482 | struct list_head *victims) |
| 7483 | { |
| 7484 | struct nfs4_delegation *dp, *next; |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 7485 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7486 | nfsd_net_id); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 7487 | u64 count = 0; |
| 7488 | |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 7489 | lockdep_assert_held(&nn->client_lock); |
| 7490 | |
| 7491 | spin_lock(&state_lock); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 7492 | list_for_each_entry_safe(dp, next, &clp->cl_delegations, dl_perclnt) { |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 7493 | if (victims) { |
| 7494 | /* |
| 7495 | * It's not safe to mess with delegations that have a |
| 7496 | * non-zero dl_time. They might have already been broken |
| 7497 | * and could be processed by the laundromat outside of |
| 7498 | * the state_lock. Just leave them be. |
| 7499 | */ |
| 7500 | if (dp->dl_time != 0) |
| 7501 | continue; |
| 7502 | |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 7503 | atomic_inc(&clp->cl_rpc_users); |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 7504 | WARN_ON(!unhash_delegation_locked(dp)); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 7505 | list_add(&dp->dl_recall_lru, victims); |
Jeff Layton | dff1399 | 2014-07-08 14:02:49 -0400 | [diff] [blame] | 7506 | } |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 7507 | ++count; |
| 7508 | /* |
| 7509 | * Despite the fact that these functions deal with |
| 7510 | * 64-bit integers for "count", we must ensure that |
J. Bruce Fields | 14ed14c | 2019-03-20 11:54:11 -0400 | [diff] [blame] | 7511 | * it doesn't blow up the clp->cl_rpc_users. Throw a |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 7512 | * warning if we start to approach INT_MAX here. |
| 7513 | */ |
| 7514 | WARN_ON_ONCE(count == (INT_MAX / 2)); |
| 7515 | if (count == max) |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 7516 | break; |
| 7517 | } |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 7518 | spin_unlock(&state_lock); |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 7519 | return count; |
| 7520 | } |
| 7521 | |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 7522 | static u64 |
| 7523 | nfsd_print_client_delegations(struct nfs4_client *clp) |
Bryan Schumaker | 269de30 | 2012-11-29 11:40:42 -0500 | [diff] [blame] | 7524 | { |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 7525 | u64 count = nfsd_find_all_delegations(clp, 0, NULL); |
Bryan Schumaker | 184c184 | 2012-11-29 11:40:44 -0500 | [diff] [blame] | 7526 | |
| 7527 | nfsd_print_count(clp, count, "delegations"); |
| 7528 | return count; |
| 7529 | } |
| 7530 | |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 7531 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7532 | nfsd_inject_print_delegations(void) |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 7533 | { |
| 7534 | struct nfs4_client *clp; |
| 7535 | u64 count = 0; |
| 7536 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7537 | nfsd_net_id); |
| 7538 | |
| 7539 | if (!nfsd_netns_ready(nn)) |
| 7540 | return 0; |
| 7541 | |
| 7542 | spin_lock(&nn->client_lock); |
| 7543 | list_for_each_entry(clp, &nn->client_lru, cl_lru) |
| 7544 | count += nfsd_print_client_delegations(clp); |
| 7545 | spin_unlock(&nn->client_lock); |
| 7546 | |
| 7547 | return count; |
| 7548 | } |
| 7549 | |
| 7550 | static void |
| 7551 | nfsd_forget_delegations(struct list_head *reaplist) |
| 7552 | { |
| 7553 | struct nfs4_client *clp; |
| 7554 | struct nfs4_delegation *dp, *next; |
| 7555 | |
| 7556 | list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) { |
| 7557 | list_del_init(&dp->dl_recall_lru); |
| 7558 | clp = dp->dl_stid.sc_client; |
| 7559 | revoke_delegation(dp); |
| 7560 | put_client(clp); |
| 7561 | } |
| 7562 | } |
| 7563 | |
| 7564 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7565 | nfsd_inject_forget_client_delegations(struct sockaddr_storage *addr, |
| 7566 | size_t addr_size) |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 7567 | { |
| 7568 | u64 count = 0; |
| 7569 | struct nfs4_client *clp; |
| 7570 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7571 | nfsd_net_id); |
| 7572 | LIST_HEAD(reaplist); |
| 7573 | |
| 7574 | if (!nfsd_netns_ready(nn)) |
| 7575 | return count; |
| 7576 | |
| 7577 | spin_lock(&nn->client_lock); |
| 7578 | clp = nfsd_find_client(addr, addr_size); |
| 7579 | if (clp) |
| 7580 | count = nfsd_find_all_delegations(clp, 0, &reaplist); |
| 7581 | spin_unlock(&nn->client_lock); |
| 7582 | |
| 7583 | nfsd_forget_delegations(&reaplist); |
| 7584 | return count; |
| 7585 | } |
| 7586 | |
| 7587 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7588 | nfsd_inject_forget_delegations(u64 max) |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 7589 | { |
| 7590 | u64 count = 0; |
| 7591 | struct nfs4_client *clp; |
| 7592 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7593 | nfsd_net_id); |
| 7594 | LIST_HEAD(reaplist); |
| 7595 | |
| 7596 | if (!nfsd_netns_ready(nn)) |
| 7597 | return count; |
| 7598 | |
| 7599 | spin_lock(&nn->client_lock); |
| 7600 | list_for_each_entry(clp, &nn->client_lru, cl_lru) { |
| 7601 | count += nfsd_find_all_delegations(clp, max - count, &reaplist); |
| 7602 | if (max != 0 && count >= max) |
| 7603 | break; |
| 7604 | } |
| 7605 | spin_unlock(&nn->client_lock); |
| 7606 | nfsd_forget_delegations(&reaplist); |
| 7607 | return count; |
| 7608 | } |
| 7609 | |
| 7610 | static void |
| 7611 | nfsd_recall_delegations(struct list_head *reaplist) |
| 7612 | { |
| 7613 | struct nfs4_client *clp; |
| 7614 | struct nfs4_delegation *dp, *next; |
| 7615 | |
| 7616 | list_for_each_entry_safe(dp, next, reaplist, dl_recall_lru) { |
| 7617 | list_del_init(&dp->dl_recall_lru); |
| 7618 | clp = dp->dl_stid.sc_client; |
| 7619 | /* |
| 7620 | * We skipped all entries that had a zero dl_time before, |
| 7621 | * so we can now reset the dl_time back to 0. If a delegation |
| 7622 | * break comes in now, then it won't make any difference since |
| 7623 | * we're recalling it either way. |
| 7624 | */ |
| 7625 | spin_lock(&state_lock); |
| 7626 | dp->dl_time = 0; |
| 7627 | spin_unlock(&state_lock); |
| 7628 | nfsd_break_one_deleg(dp); |
| 7629 | put_client(clp); |
| 7630 | } |
| 7631 | } |
| 7632 | |
| 7633 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7634 | nfsd_inject_recall_client_delegations(struct sockaddr_storage *addr, |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 7635 | size_t addr_size) |
| 7636 | { |
| 7637 | u64 count = 0; |
| 7638 | struct nfs4_client *clp; |
| 7639 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7640 | nfsd_net_id); |
| 7641 | LIST_HEAD(reaplist); |
| 7642 | |
| 7643 | if (!nfsd_netns_ready(nn)) |
| 7644 | return count; |
| 7645 | |
| 7646 | spin_lock(&nn->client_lock); |
| 7647 | clp = nfsd_find_client(addr, addr_size); |
| 7648 | if (clp) |
| 7649 | count = nfsd_find_all_delegations(clp, 0, &reaplist); |
| 7650 | spin_unlock(&nn->client_lock); |
| 7651 | |
| 7652 | nfsd_recall_delegations(&reaplist); |
| 7653 | return count; |
| 7654 | } |
| 7655 | |
| 7656 | u64 |
Jeff Layton | 285abde | 2014-07-30 08:27:24 -0400 | [diff] [blame] | 7657 | nfsd_inject_recall_delegations(u64 max) |
Jeff Layton | 98d5c7c | 2014-07-30 08:27:23 -0400 | [diff] [blame] | 7658 | { |
| 7659 | u64 count = 0; |
| 7660 | struct nfs4_client *clp, *next; |
| 7661 | struct nfsd_net *nn = net_generic(current->nsproxy->net_ns, |
| 7662 | nfsd_net_id); |
| 7663 | LIST_HEAD(reaplist); |
| 7664 | |
| 7665 | if (!nfsd_netns_ready(nn)) |
| 7666 | return count; |
| 7667 | |
| 7668 | spin_lock(&nn->client_lock); |
| 7669 | list_for_each_entry_safe(clp, next, &nn->client_lru, cl_lru) { |
| 7670 | count += nfsd_find_all_delegations(clp, max - count, &reaplist); |
| 7671 | if (max != 0 && ++count >= max) |
| 7672 | break; |
| 7673 | } |
| 7674 | spin_unlock(&nn->client_lock); |
| 7675 | nfsd_recall_delegations(&reaplist); |
| 7676 | return count; |
| 7677 | } |
Bryan Schumaker | 65178db | 2011-11-01 13:35:21 -0400 | [diff] [blame] | 7678 | #endif /* CONFIG_NFSD_FAULT_INJECTION */ |
| 7679 | |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 7680 | /* |
| 7681 | * Since the lifetime of a delegation isn't limited to that of an open, a |
| 7682 | * client may quite reasonably hang on to a delegation as long as it has |
| 7683 | * the inode cached. This becomes an obvious problem the first time a |
| 7684 | * client's inode cache approaches the size of the server's total memory. |
| 7685 | * |
| 7686 | * For now we avoid this problem by imposing a hard limit on the number |
| 7687 | * of delegations, which varies according to the server's memory size. |
| 7688 | */ |
| 7689 | static void |
| 7690 | set_max_delegations(void) |
| 7691 | { |
| 7692 | /* |
| 7693 | * Allow at most 4 delegations per megabyte of RAM. Quick |
| 7694 | * estimates suggest that in the worst case (where every delegation |
| 7695 | * is for a different inode), a delegation could take about 1.5K, |
| 7696 | * giving a worst case usage of about 6% of memory. |
| 7697 | */ |
| 7698 | max_delegations = nr_free_buffer_pages() >> (20 - 2 - PAGE_SHIFT); |
| 7699 | } |
| 7700 | |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 7701 | static int nfs4_state_create_net(struct net *net) |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7702 | { |
| 7703 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 7704 | int i; |
| 7705 | |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 7706 | nn->conf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE, |
| 7707 | sizeof(struct list_head), |
| 7708 | GFP_KERNEL); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7709 | if (!nn->conf_id_hashtbl) |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 7710 | goto err; |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 7711 | nn->unconf_id_hashtbl = kmalloc_array(CLIENT_HASH_SIZE, |
| 7712 | sizeof(struct list_head), |
| 7713 | GFP_KERNEL); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 7714 | if (!nn->unconf_id_hashtbl) |
| 7715 | goto err_unconf_id; |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 7716 | nn->sessionid_hashtbl = kmalloc_array(SESSION_HASH_SIZE, |
| 7717 | sizeof(struct list_head), |
| 7718 | GFP_KERNEL); |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 7719 | if (!nn->sessionid_hashtbl) |
| 7720 | goto err_sessionid; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7721 | |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 7722 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7723 | INIT_LIST_HEAD(&nn->conf_id_hashtbl[i]); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 7724 | INIT_LIST_HEAD(&nn->unconf_id_hashtbl[i]); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 7725 | } |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 7726 | for (i = 0; i < SESSION_HASH_SIZE; i++) |
| 7727 | INIT_LIST_HEAD(&nn->sessionid_hashtbl[i]); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 7728 | nn->conf_name_tree = RB_ROOT; |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 7729 | nn->unconf_name_tree = RB_ROOT; |
Vasily Averin | 81833de | 2017-11-13 07:25:40 +0300 | [diff] [blame] | 7730 | nn->boot_time = get_seconds(); |
| 7731 | nn->grace_ended = false; |
| 7732 | nn->nfsd4_manager.block_opens = true; |
| 7733 | INIT_LIST_HEAD(&nn->nfsd4_manager.list); |
Stanislav Kinsbursky | 5ed58bb | 2012-11-14 18:21:56 +0300 | [diff] [blame] | 7734 | INIT_LIST_HEAD(&nn->client_lru); |
Stanislav Kinsbursky | 73758fed | 2012-11-14 18:22:01 +0300 | [diff] [blame] | 7735 | INIT_LIST_HEAD(&nn->close_lru); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 7736 | INIT_LIST_HEAD(&nn->del_recall_lru); |
Stanislav Kinsbursky | c9a4962 | 2012-11-26 15:21:58 +0300 | [diff] [blame] | 7737 | spin_lock_init(&nn->client_lock); |
Olga Kornievskaia | e0639dc | 2018-07-20 18:19:20 -0400 | [diff] [blame] | 7738 | spin_lock_init(&nn->s2s_cp_lock); |
| 7739 | idr_init(&nn->s2s_cp_stateids); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7740 | |
Jeff Layton | 0cc11a6 | 2016-10-20 09:34:31 -0400 | [diff] [blame] | 7741 | spin_lock_init(&nn->blocked_locks_lock); |
| 7742 | INIT_LIST_HEAD(&nn->blocked_locks_lru); |
| 7743 | |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 7744 | INIT_DELAYED_WORK(&nn->laundromat_work, laundromat_main); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 7745 | get_net(net); |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 7746 | |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7747 | return 0; |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 7748 | |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 7749 | err_sessionid: |
Stanislav Kinsbursky | 9b53113 | 2012-11-14 18:21:41 +0300 | [diff] [blame] | 7750 | kfree(nn->unconf_id_hashtbl); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 7751 | err_unconf_id: |
| 7752 | kfree(nn->conf_id_hashtbl); |
Stanislav Kinsbursky | 382a62e | 2012-11-14 18:21:26 +0300 | [diff] [blame] | 7753 | err: |
| 7754 | return -ENOMEM; |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7755 | } |
| 7756 | |
| 7757 | static void |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 7758 | nfs4_state_destroy_net(struct net *net) |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7759 | { |
| 7760 | int i; |
| 7761 | struct nfs4_client *clp = NULL; |
| 7762 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
| 7763 | |
| 7764 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
| 7765 | while (!list_empty(&nn->conf_id_hashtbl[i])) { |
| 7766 | clp = list_entry(nn->conf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); |
| 7767 | destroy_client(clp); |
| 7768 | } |
| 7769 | } |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 7770 | |
Jeff Layton | 68ef3bc | 2018-03-16 11:32:02 -0400 | [diff] [blame] | 7771 | WARN_ON(!list_empty(&nn->blocked_locks_lru)); |
| 7772 | |
Kinglong Mee | 2b90563 | 2014-03-26 22:09:30 +0800 | [diff] [blame] | 7773 | for (i = 0; i < CLIENT_HASH_SIZE; i++) { |
| 7774 | while (!list_empty(&nn->unconf_id_hashtbl[i])) { |
| 7775 | clp = list_entry(nn->unconf_id_hashtbl[i].next, struct nfs4_client, cl_idhash); |
| 7776 | destroy_client(clp); |
| 7777 | } |
Stanislav Kinsbursky | a99454a | 2012-11-14 18:21:36 +0300 | [diff] [blame] | 7778 | } |
| 7779 | |
Stanislav Kinsbursky | 1872de0 | 2012-11-14 18:21:51 +0300 | [diff] [blame] | 7780 | kfree(nn->sessionid_hashtbl); |
Stanislav Kinsbursky | 0a7ec37 | 2012-11-14 18:21:31 +0300 | [diff] [blame] | 7781 | kfree(nn->unconf_id_hashtbl); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7782 | kfree(nn->conf_id_hashtbl); |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 7783 | put_net(net); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7784 | } |
| 7785 | |
Stanislav Kinsbursky | f252bc6 | 2012-11-26 15:22:18 +0300 | [diff] [blame] | 7786 | int |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 7787 | nfs4_state_start_net(struct net *net) |
NeilBrown | ac4d8ff2 | 2005-06-23 22:03:30 -0700 | [diff] [blame] | 7788 | { |
Stanislav Kinsbursky | 5e1533c | 2012-07-25 16:56:58 +0400 | [diff] [blame] | 7789 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 7790 | int ret; |
| 7791 | |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 7792 | ret = nfs4_state_create_net(net); |
Stanislav Kinsbursky | 8daae4d | 2012-11-14 18:21:21 +0300 | [diff] [blame] | 7793 | if (ret) |
| 7794 | return ret; |
Jeff Layton | d4318ac | 2014-09-12 16:40:21 -0400 | [diff] [blame] | 7795 | locks_start_grace(net, &nn->nfsd4_manager); |
| 7796 | nfsd4_client_tracking_init(net); |
Scott Mayhew | 362063a | 2019-03-26 18:06:28 -0400 | [diff] [blame] | 7797 | if (nn->track_reclaim_completes && nn->reclaim_str_hashtbl_size == 0) |
| 7798 | goto skip_grace; |
Vasily Averin | 7e981a8 | 2017-11-06 16:46:04 +0300 | [diff] [blame] | 7799 | printk(KERN_INFO "NFSD: starting %ld-second grace period (net %x)\n", |
| 7800 | nn->nfsd4_grace, net->ns.inum); |
Stanislav Kinsbursky | 5284b44 | 2012-11-27 14:11:49 +0300 | [diff] [blame] | 7801 | queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_grace * HZ); |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 7802 | return 0; |
Scott Mayhew | 362063a | 2019-03-26 18:06:28 -0400 | [diff] [blame] | 7803 | |
| 7804 | skip_grace: |
| 7805 | printk(KERN_INFO "NFSD: no clients to reclaim, skipping NFSv4 grace period (net %x)\n", |
| 7806 | net->ns.inum); |
| 7807 | queue_delayed_work(laundry_wq, &nn->laundromat_work, nn->nfsd4_lease * HZ); |
| 7808 | nfsd4_end_grace(nn); |
| 7809 | return 0; |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 7810 | } |
| 7811 | |
| 7812 | /* initialization to perform when the nfsd service is started: */ |
| 7813 | |
| 7814 | int |
| 7815 | nfs4_state_start(void) |
| 7816 | { |
| 7817 | int ret; |
| 7818 | |
Jeff Layton | 51a5456 | 2015-08-20 07:17:01 -0400 | [diff] [blame] | 7819 | laundry_wq = alloc_workqueue("%s", WQ_UNBOUND, 0, "nfsd4"); |
Jeff Layton | a6d6b78 | 2012-03-05 11:42:36 -0500 | [diff] [blame] | 7820 | if (laundry_wq == NULL) { |
| 7821 | ret = -ENOMEM; |
Chuck Lever | a26dd64 | 2018-08-16 12:06:09 -0400 | [diff] [blame] | 7822 | goto out; |
Jeff Layton | a6d6b78 | 2012-03-05 11:42:36 -0500 | [diff] [blame] | 7823 | } |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 7824 | ret = nfsd4_create_callback_queue(); |
| 7825 | if (ret) |
| 7826 | goto out_free_laundry; |
Stanislav Kinsbursky | 0912128 | 2012-11-14 18:22:17 +0300 | [diff] [blame] | 7827 | |
Meelap Shah | c2f1a55 | 2007-07-17 04:04:39 -0700 | [diff] [blame] | 7828 | set_max_delegations(); |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 7829 | return 0; |
Stanislav Kinsbursky | d85ed44 | 2012-11-26 15:22:13 +0300 | [diff] [blame] | 7830 | |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 7831 | out_free_laundry: |
| 7832 | destroy_workqueue(laundry_wq); |
Chuck Lever | a26dd64 | 2018-08-16 12:06:09 -0400 | [diff] [blame] | 7833 | out: |
J. Bruce Fields | b5a1a81 | 2010-03-03 14:52:55 -0500 | [diff] [blame] | 7834 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7835 | } |
| 7836 | |
Stanislav Kinsbursky | f252bc6 | 2012-11-26 15:22:18 +0300 | [diff] [blame] | 7837 | void |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 7838 | nfs4_state_shutdown_net(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7839 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7840 | struct nfs4_delegation *dp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7841 | struct list_head *pos, *next, reaplist; |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 7842 | struct nfsd_net *nn = net_generic(net, nfsd_net_id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7843 | |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 7844 | cancel_delayed_work_sync(&nn->laundromat_work); |
| 7845 | locks_end_grace(&nn->nfsd4_manager); |
Jeff Layton | ac55fdc | 2012-11-12 15:00:56 -0500 | [diff] [blame] | 7846 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7847 | INIT_LIST_HEAD(&reaplist); |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 7848 | spin_lock(&state_lock); |
J. Bruce Fields | e8c69d1 | 2013-03-21 15:19:33 -0400 | [diff] [blame] | 7849 | list_for_each_safe(pos, next, &nn->del_recall_lru) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7850 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
Jeff Layton | 3fcbbd2 | 2015-08-24 12:41:48 -0400 | [diff] [blame] | 7851 | WARN_ON(!unhash_delegation_locked(dp)); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 7852 | list_add(&dp->dl_recall_lru, &reaplist); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7853 | } |
Benny Halevy | cdc97505 | 2014-05-30 09:09:30 -0400 | [diff] [blame] | 7854 | spin_unlock(&state_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7855 | list_for_each_safe(pos, next, &reaplist) { |
| 7856 | dp = list_entry (pos, struct nfs4_delegation, dl_recall_lru); |
Jeff Layton | 4269067 | 2014-07-25 07:34:20 -0400 | [diff] [blame] | 7857 | list_del_init(&dp->dl_recall_lru); |
J. Bruce Fields | 0af6e69 | 2018-02-21 15:11:03 -0500 | [diff] [blame] | 7858 | destroy_unhashed_deleg(dp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7859 | } |
| 7860 | |
Stanislav Kinsbursky | 3320fef19 | 2012-11-14 18:22:07 +0300 | [diff] [blame] | 7861 | nfsd4_client_tracking_exit(net); |
Stanislav Kinsbursky | 4dce0ac | 2012-11-26 15:22:08 +0300 | [diff] [blame] | 7862 | nfs4_state_destroy_net(net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7863 | } |
| 7864 | |
| 7865 | void |
| 7866 | nfs4_state_shutdown(void) |
| 7867 | { |
NeilBrown | 5e8d5c2 | 2006-04-10 22:55:37 -0700 | [diff] [blame] | 7868 | destroy_workqueue(laundry_wq); |
J. Bruce Fields | c3935e3 | 2010-06-04 16:42:08 -0400 | [diff] [blame] | 7869 | nfsd4_destroy_callback_queue(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7870 | } |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7871 | |
| 7872 | static void |
| 7873 | get_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) |
| 7874 | { |
Tigran Mkrtchyan | 37c593c | 2012-02-13 22:55:32 +0100 | [diff] [blame] | 7875 | if (HAS_STATE_ID(cstate, CURRENT_STATE_ID_FLAG) && CURRENT_STATEID(stateid)) |
| 7876 | memcpy(stateid, &cstate->current_stateid, sizeof(stateid_t)); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7877 | } |
| 7878 | |
| 7879 | static void |
| 7880 | put_stateid(struct nfsd4_compound_state *cstate, stateid_t *stateid) |
| 7881 | { |
Tigran Mkrtchyan | 37c593c | 2012-02-13 22:55:32 +0100 | [diff] [blame] | 7882 | if (cstate->minorversion) { |
| 7883 | memcpy(&cstate->current_stateid, stateid, sizeof(stateid_t)); |
| 7884 | SET_STATE_ID(cstate, CURRENT_STATE_ID_FLAG); |
| 7885 | } |
| 7886 | } |
| 7887 | |
| 7888 | void |
| 7889 | clear_current_stateid(struct nfsd4_compound_state *cstate) |
| 7890 | { |
| 7891 | CLEAR_STATE_ID(cstate, CURRENT_STATE_ID_FLAG); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7892 | } |
| 7893 | |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 7894 | /* |
| 7895 | * functions to set current state id |
| 7896 | */ |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7897 | void |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7898 | nfsd4_set_opendowngradestateid(struct nfsd4_compound_state *cstate, |
| 7899 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 7900 | { |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7901 | put_stateid(cstate, &u->open_downgrade.od_stateid); |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 7902 | } |
| 7903 | |
| 7904 | void |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7905 | nfsd4_set_openstateid(struct nfsd4_compound_state *cstate, |
| 7906 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7907 | { |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7908 | put_stateid(cstate, &u->open.op_stateid); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7909 | } |
| 7910 | |
| 7911 | void |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7912 | nfsd4_set_closestateid(struct nfsd4_compound_state *cstate, |
| 7913 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 7914 | { |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7915 | put_stateid(cstate, &u->close.cl_stateid); |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 7916 | } |
| 7917 | |
| 7918 | void |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7919 | nfsd4_set_lockstateid(struct nfsd4_compound_state *cstate, |
| 7920 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 7921 | { |
Christoph Hellwig | b60e985 | 2017-05-08 20:03:15 +0200 | [diff] [blame] | 7922 | put_stateid(cstate, &u->lock.lk_resp_stateid); |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 7923 | } |
| 7924 | |
| 7925 | /* |
| 7926 | * functions to consume current state id |
| 7927 | */ |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 7928 | |
| 7929 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7930 | nfsd4_get_opendowngradestateid(struct nfsd4_compound_state *cstate, |
| 7931 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 7932 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7933 | get_stateid(cstate, &u->open_downgrade.od_stateid); |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 7934 | } |
| 7935 | |
| 7936 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7937 | nfsd4_get_delegreturnstateid(struct nfsd4_compound_state *cstate, |
| 7938 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 7939 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7940 | get_stateid(cstate, &u->delegreturn.dr_stateid); |
Tigran Mkrtchyan | 9428fe1 | 2012-02-13 22:55:31 +0100 | [diff] [blame] | 7941 | } |
| 7942 | |
| 7943 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7944 | nfsd4_get_freestateid(struct nfsd4_compound_state *cstate, |
| 7945 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 7946 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7947 | get_stateid(cstate, &u->free_stateid.fr_stateid); |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 7948 | } |
| 7949 | |
| 7950 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7951 | nfsd4_get_setattrstateid(struct nfsd4_compound_state *cstate, |
| 7952 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 7953 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7954 | get_stateid(cstate, &u->setattr.sa_stateid); |
Tigran Mkrtchyan | 1e97b51 | 2012-02-13 22:55:30 +0100 | [diff] [blame] | 7955 | } |
| 7956 | |
Tigran Mkrtchyan | 62cd4a5 | 2012-02-13 22:55:25 +0100 | [diff] [blame] | 7957 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7958 | nfsd4_get_closestateid(struct nfsd4_compound_state *cstate, |
| 7959 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7960 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7961 | get_stateid(cstate, &u->close.cl_stateid); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7962 | } |
| 7963 | |
| 7964 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7965 | nfsd4_get_lockustateid(struct nfsd4_compound_state *cstate, |
| 7966 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7967 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7968 | get_stateid(cstate, &u->locku.lu_stateid); |
Tigran Mkrtchyan | 8b70484 | 2012-02-13 22:55:24 +0100 | [diff] [blame] | 7969 | } |
Tigran Mkrtchyan | 30813e2 | 2012-02-13 22:55:26 +0100 | [diff] [blame] | 7970 | |
| 7971 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7972 | nfsd4_get_readstateid(struct nfsd4_compound_state *cstate, |
| 7973 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 30813e2 | 2012-02-13 22:55:26 +0100 | [diff] [blame] | 7974 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7975 | get_stateid(cstate, &u->read.rd_stateid); |
Tigran Mkrtchyan | 30813e2 | 2012-02-13 22:55:26 +0100 | [diff] [blame] | 7976 | } |
| 7977 | |
| 7978 | void |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7979 | nfsd4_get_writestateid(struct nfsd4_compound_state *cstate, |
| 7980 | union nfsd4_op_u *u) |
Tigran Mkrtchyan | 30813e2 | 2012-02-13 22:55:26 +0100 | [diff] [blame] | 7981 | { |
Christoph Hellwig | 57832e7 | 2017-05-08 20:37:33 +0200 | [diff] [blame] | 7982 | get_stateid(cstate, &u->write.wr_stateid); |
Tigran Mkrtchyan | 30813e2 | 2012-02-13 22:55:26 +0100 | [diff] [blame] | 7983 | } |